diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-12-03 18:05:07 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-12-03 18:05:07 +0100 |
commit | 6ba1ac4ebfcd8c95fca324b15606ab0ec1412d39 (patch) | |
tree | bb9230ade3eb9ab09efd78b367041bea38fcb761 | |
parent | d975e807aee0ac902d8105d584c22fb526ecd87d (diff) | |
download | psutil-6ba1ac4ebfcd8c95fca324b15606ab0ec1412d39.tar.gz |
#941: implement cpu_freq() for OSX
-rw-r--r-- | psutil/_pslinux.py | 2 | ||||
-rw-r--r-- | psutil/_psosx.py | 5 | ||||
-rw-r--r-- | psutil/_psutil_osx.c | 31 | ||||
-rwxr-xr-x | psutil/tests/test_osx.py | 15 | ||||
-rwxr-xr-x | psutil/tests/test_system.py | 3 |
5 files changed, 53 insertions, 3 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index add06a11..e073b06e 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -628,8 +628,6 @@ if os.path.exists("/sys/devices/system/cpu/cpufreq"): # scaling_* files seem preferable to cpuinfo_*, see: # http://unix.stackexchange.com/a/87537/168884 ret = [] - # XXX - print(os.listdir("/sys/devices/system/cpu/cpufreq/")) ls = glob.glob("/sys/devices/system/cpu/cpufreq/policy*") # Sort the list so that '10' comes after '2'. This should # ensure the CPU order is consistent with other CPU functions diff --git a/psutil/_psosx.py b/psutil/_psosx.py index 2665080e..0778b5fb 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -165,6 +165,11 @@ def cpu_stats(): ctx_switches, interrupts, soft_interrupts, syscalls) +def cpu_freq(): + curr, min_, max_ = cext.cpu_freq() + return [_common.scpufreq(curr, min_, max_)] + + # ===================================================================== # --- disks # ===================================================================== diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index a1168c29..fb26dc9b 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -809,6 +809,35 @@ error: /* + * Retrieve CPU frequency. + */ +static PyObject * +psutil_cpu_freq(PyObject *self, PyObject *args) { + int64_t curr; + int64_t min; + int64_t max; + size_t size = sizeof(int64_t); + + if (sysctlbyname("hw.cpufrequency", &curr, &size, NULL, 0)) + goto error; + if (sysctlbyname("hw.cpufrequency_min", &min, &size, NULL, 0)) + goto error; + if (sysctlbyname("hw.cpufrequency_max", &max, &size, NULL, 0)) + goto error; + + return Py_BuildValue( + "KKK", + curr / 1000 / 1000, + min / 1000 / 1000, + max / 1000 / 1000); + +error: + PyErr_SetFromErrno(PyExc_OSError); + return NULL; +} + + +/* * Return a Python float indicating the system boot time expressed in * seconds since the epoch. */ @@ -1778,6 +1807,8 @@ PsutilMethods[] = { "Return system cpu times as a tuple (user, system, nice, idle, irc)"}, {"per_cpu_times", psutil_per_cpu_times, METH_VARARGS, "Return system per-cpu times as a list of tuples"}, + {"cpu_freq", psutil_cpu_freq, METH_VARARGS, + "Return cpu current frequency"}, {"boot_time", psutil_boot_time, METH_VARARGS, "Return the system boot time expressed in seconds since the epoch."}, {"disk_partitions", psutil_disk_partitions, METH_VARARGS, diff --git a/psutil/tests/test_osx.py b/psutil/tests/test_osx.py index 7b61bc74..02fa430b 100755 --- a/psutil/tests/test_osx.py +++ b/psutil/tests/test_osx.py @@ -111,6 +111,8 @@ class TestProcess(unittest.TestCase): @unittest.skipUnless(OSX, "OSX only") class TestSystemAPIs(unittest.TestCase): + # --- disk + def test_disks(self): # test psutil.disk_usage() and psutil.disk_partitions() # against "df -a" @@ -138,6 +140,8 @@ class TestSystemAPIs(unittest.TestCase): if abs(usage.used - used) > 10 * 1024 * 1024: self.fail("psutil=%s, df=%s" % usage.used, used) + # --- cpu + def test_cpu_count_logical(self): num = sysctl("sysctl hw.logicalcpu") self.assertEqual(num, psutil.cpu_count(logical=True)) @@ -146,6 +150,15 @@ class TestSystemAPIs(unittest.TestCase): num = sysctl("sysctl hw.physicalcpu") self.assertEqual(num, psutil.cpu_count(logical=False)) + def test_cpu_freq(self): + freq = psutil.cpu_freq()[0] + self.assertEqual( + freq.curr * 1000 * 1000, sysctl("sysctl hw.cpufrequency")) + self.assertEqual( + freq.min * 1000 * 1000, sysctl("sysctl hw.cpufrequency_min")) + self.assertEqual( + freq.max * 1000 * 1000, sysctl("sysctl hw.cpufrequency_max")) + # --- virtual mem def test_vmem_total(self): @@ -206,6 +219,8 @@ class TestSystemAPIs(unittest.TestCase): # self.assertEqual(psutil_smem.used, human2bytes(used)) # self.assertEqual(psutil_smem.free, human2bytes(free)) + # --- network + def test_net_if_stats(self): for name, stats in psutil.net_if_stats().items(): try: diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py index ac63dc7c..d1b81838 100755 --- a/psutil/tests/test_system.py +++ b/psutil/tests/test_system.py @@ -701,7 +701,8 @@ class TestSystemAPIs(unittest.TestCase): "platform not suported") def test_cpu_freq(self): ls = psutil.cpu_freq() - assert ls, ls + if not TRAVIS: + assert ls, ls for nt in ls: for name in nt._fields: value = getattr(nt, name) |