summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-01-24 17:22:34 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2017-01-24 17:22:34 +0100
commit1818df036f4ab4b851e8cc0dd15f0bf28d8640f8 (patch)
tree8883de84ce6186edd4a26dbf7b28d45ab4d48e84
parentc69f2591f07341681cc56dbf93cf08cbb3cba48a (diff)
parent8e81948e711e693536d98c229158cbda2c080bca (diff)
downloadpsutil-1818df036f4ab4b851e8cc0dd15f0bf28d8640f8.tar.gz
Merge branch 'master' into 371-temperatures
-rw-r--r--psutil/__init__.py34
-rwxr-xr-xpsutil/tests/test_memory_leaks.py34
-rwxr-xr-xpsutil/tests/test_misc.py3
-rwxr-xr-xpsutil/tests/test_system.py1
4 files changed, 61 insertions, 11 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py
index afd30e90..31f79f46 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -1879,18 +1879,30 @@ if hasattr(_psplatform, "cpu_freq"):
if percpu:
return ret
else:
- num_cpus = len(ret)
- if num_cpus == 1:
+ num_cpus = float(len(ret))
+ if num_cpus == 0:
+ return []
+ elif num_cpus == 1:
return ret[0]
- currs, mins, maxs = [], [], []
- for cpu in ret:
- currs.append(cpu.current)
- mins.append(cpu.min)
- maxs.append(cpu.max)
- return _common.scpufreq(
- sum(currs) / num_cpus,
- sum(mins) / num_cpus,
- sum(maxs) / num_cpus)
+ else:
+ currs, mins, maxs = 0.0, 0.0, 0.0
+ for cpu in ret:
+ currs += cpu.current
+ mins += cpu.min
+ maxs += cpu.max
+ try:
+ current = currs / num_cpus
+ except ZeroDivisionError:
+ current = 0.0
+ try:
+ min_ = mins / num_cpus
+ except ZeroDivisionError:
+ min_ = 0.0
+ try:
+ max_ = maxs / num_cpus
+ except ZeroDivisionError:
+ max_ = 0.0
+ return _common.scpufreq(current, min_, max_)
__all__.append("cpu_freq")
diff --git a/psutil/tests/test_memory_leaks.py b/psutil/tests/test_memory_leaks.py
index 6f724339..44be1ec5 100755
--- a/psutil/tests/test_memory_leaks.py
+++ b/psutil/tests/test_memory_leaks.py
@@ -179,6 +179,19 @@ class TestProcessObjectLeaks(TestMemLeak):
proc = thisproc
+ def test_coverage(self):
+ skip = set((
+ "pid", "as_dict", "children", "cpu_affinity", "cpu_percent",
+ "ionice", "is_running", "kill", "memory_info_ex", "memory_percent",
+ "nice", "oneshot", "parent", "rlimit", "send_signal", "suspend",
+ "terminate", "wait"))
+ for name in dir(psutil.Process):
+ if name.startswith('_'):
+ continue
+ if name in skip:
+ continue
+ self.assertTrue(hasattr(self, "test_" + name), msg=name)
+
@skip_if_linux()
def test_name(self):
self.execute(self.proc.name)
@@ -259,6 +272,10 @@ class TestProcessObjectLeaks(TestMemLeak):
self.execute(self.proc.num_fds)
@skip_if_linux()
+ def test_num_ctx_switches(self):
+ self.execute(self.proc.num_ctx_switches)
+
+ @skip_if_linux()
def test_threads(self):
self.execute(self.proc.threads)
@@ -443,6 +460,17 @@ class TestTerminatedProcessLeaks(TestProcessObjectLeaks):
class TestModuleFunctionsLeaks(TestMemLeak):
"""Test leaks of psutil module functions."""
+ def test_coverage(self):
+ skip = set((
+ "version_info", "__version__", "process_iter", "wait_procs",
+ "cpu_percent", "cpu_times_percent", "cpu_count"))
+ for name in psutil.__all__:
+ if not name.islower():
+ continue
+ if name in skip:
+ continue
+ self.assertTrue(hasattr(self, "test_" + name), msg=name)
+
# --- cpu
@skip_if_linux()
@@ -501,6 +529,12 @@ class TestModuleFunctionsLeaks(TestMemLeak):
def test_disk_io_counters(self):
self.execute(psutil.disk_io_counters)
+ # --- proc
+
+ @skip_if_linux()
+ def test_pids(self):
+ self.execute(psutil.pids)
+
# --- net
@skip_if_linux()
diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py
index 6db776ee..0b696f8c 100755
--- a/psutil/tests/test_misc.py
+++ b/psutil/tests/test_misc.py
@@ -466,6 +466,9 @@ class TestScripts(unittest.TestCase):
def test_winservices(self):
self.assert_stdout('winservices.py')
+ def test_cpu_distribution(self):
+ self.assert_syntax('cpu_distribution.py')
+
# ===================================================================
# --- Unit tests for test utilities.
diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py
index 0ee4b6b6..de2ddef0 100755
--- a/psutil/tests/test_system.py
+++ b/psutil/tests/test_system.py
@@ -705,6 +705,7 @@ class TestSystemAPIs(unittest.TestCase):
self.assertLessEqual(nt.current, nt.max)
for name in nt._fields:
value = getattr(nt, name)
+ self.assertIsInstance(value, (int, long, float))
self.assertGreaterEqual(value, 0)
ls = psutil.cpu_freq(percpu=True)