summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-12-20 01:48:53 +0100
committerGitHub <noreply@github.com>2020-12-20 01:48:53 +0100
commitc70ceea3757a0664131605a012b2d41d81edb935 (patch)
tree6170cf9275a2f92da604244002a04c929e2b8956
parentc0e9e1b4ff694c4dc88540f9868a5a518cbc1604 (diff)
downloadpsutil-c70ceea3757a0664131605a012b2d41d81edb935.tar.gz
#1892 fix cpu_freq() broken on Apple M1 (#1895)
fix #1456: [macOS] psutil.cpu_freq()'s min and max are set to 0 if can't be determined (instead of crashing). fix #1892: [macOS] psutil.cpu_freq() broken on Apple M1.
-rw-r--r--HISTORY.rst12
-rw-r--r--psutil/_psutil_osx.c39
2 files changed, 33 insertions, 18 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index 5aee3a95..66efe586 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -1,5 +1,17 @@
*Bug tracker at https://github.com/giampaolo/psutil/issues*
+5.8.0 (IN DEVELOPMENT)
+======================
+
+XXXX-XX-XX
+
+**Bug fixes**
+
+- 1456_: [macOS] psutil.cpu_freq()'s min and max are set to 0 if can't be
+ determined (instead of crashing).
+- 1892_: [macOS] psutil.cpu_freq() broken on Apple M1.
+
+
5.8.0
=====
diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c
index 13d0bb68..5720de9f 100644
--- a/psutil/_psutil_osx.c
+++ b/psutil/_psutil_osx.c
@@ -684,30 +684,33 @@ error:
/*
- * Retrieve CPU frequency.
+ * Retrieve CPU frequency. Note: all of these are static vendor values
+ * that never change.
*/
static PyObject *
psutil_cpu_freq(PyObject *self, PyObject *args) {
- int64_t curr;
- int64_t min;
- int64_t max;
- size_t size = sizeof(int64_t);
+ unsigned int curr;
+ int64_t min = 0;
+ int64_t max = 0;
+ int mib[2];
+ size_t len = sizeof(curr);
+ size_t size = sizeof(min);
- if (sysctlbyname("hw.cpufrequency", &curr, &size, NULL, 0)) {
- return PyErr_SetFromOSErrnoWithSyscall(
- "sysctlbyname('hw.cpufrequency')");
- }
- if (sysctlbyname("hw.cpufrequency_min", &min, &size, NULL, 0)) {
- return PyErr_SetFromOSErrnoWithSyscall(
- "sysctlbyname('hw.cpufrequency_min')");
- }
- if (sysctlbyname("hw.cpufrequency_max", &max, &size, NULL, 0)) {
- return PyErr_SetFromOSErrnoWithSyscall(
- "sysctlbyname('hw.cpufrequency_max')");
- }
+ // also availble as "hw.cpufrequency" but it's deprecated
+ mib[0] = CTL_HW;
+ mib[1] = HW_CPU_FREQ;
+
+ if (sysctl(mib, 2, &curr, &len, NULL, 0) < 0)
+ return PyErr_SetFromOSErrnoWithSyscall("sysctl(HW_CPU_FREQ)");
+
+ if (sysctlbyname("hw.cpufrequency_min", &min, &size, NULL, 0))
+ psutil_debug("sysct('hw.cpufrequency_min') failed (set to 0)");
+
+ if (sysctlbyname("hw.cpufrequency_max", &max, &size, NULL, 0))
+ psutil_debug("sysctl('hw.cpufrequency_min') failed (set to 0)");
return Py_BuildValue(
- "KKK",
+ "IKK",
curr / 1000 / 1000,
min / 1000 / 1000,
max / 1000 / 1000);