Skip to content

Commit

Permalink
#941: implement cpu_freq() for OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Dec 3, 2016
1 parent d975e80 commit 6ba1ac4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
2 changes: 0 additions & 2 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,6 @@ def cpu_freq():
# 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
Expand Down
5 changes: 5 additions & 0 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# =====================================================================
Expand Down
31 changes: 31 additions & 0 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,35 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) {
}


/*
* 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.
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions psutil/tests/test_osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def test_process_create_time(self):
@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"
Expand Down Expand Up @@ -138,6 +140,8 @@ def df(path):
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))
Expand All @@ -146,6 +150,15 @@ def test_cpu_count_physical(self):
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):
Expand Down Expand Up @@ -206,6 +219,8 @@ def test_swapmem_sout(self):
# 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:
Expand Down
3 changes: 2 additions & 1 deletion psutil/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,8 @@ def test_cpu_stats(self):
"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)
Expand Down

0 comments on commit 6ba1ac4

Please sign in to comment.