Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cpu_freq for Apple silicon #2222

Merged
merged 22 commits into from Jan 6, 2024
Merged
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
68cfe12
Fix cpu_freq for Apple silicon
snOm3ad Apr 8, 2023
44ec453
Merge branch 'master' into fix-cpu-freq-apple-silicon
snOm3ad Apr 15, 2023
9a51fb0
Remove psutil_arm_cpu_freq
snOm3ad Apr 15, 2023
0966429
Merge branch 'master' into fix-cpu-freq-apple-silicon
snOm3ad Apr 16, 2023
971ea61
add error checks and replaces strcmp
snOm3ad Apr 16, 2023
56c5bf4
Merge branch 'master' into fix-cpu-freq-apple-silicon
snOm3ad Apr 18, 2023
2dfa630
Fixed error handling
snOm3ad Apr 18, 2023
a147de7
remove EOL space
snOm3ad Apr 18, 2023
182419f
add check for buffer and fix mem leak
snOm3ad Apr 18, 2023
80efd7c
Merge branch 'master' into fix-cpu-freq-apple-silicon
snOm3ad Apr 18, 2023
27efe3b
removed conditional during cleanup
snOm3ad Apr 18, 2023
01da6b9
Merge remote-tracking branch 'origin/master' into fix-cpu-freq-apple-…
snOm3ad Apr 18, 2023
23d87b9
convert all checks to explicit form
snOm3ad Apr 18, 2023
443e5f1
Merge branch 'master' into fix-cpu-freq-apple-silicon
snOm3ad Apr 22, 2023
24c707f
Updated HISTORY
snOm3ad Apr 22, 2023
44f8c81
Fix formatting
giampaolo Apr 22, 2023
d49409a
Update cpu.c
giampaolo Apr 22, 2023
eaacca9
move includes up in the file
giampaolo Jan 5, 2024
ef4f677
Merge remote-tracking branch 'upstream/master' into fix-cpu-freq-appl…
snOm3ad Jan 6, 2024
cc72f4e
Added entry to CREDITS and fix HISTORY.rst
snOm3ad Jan 6, 2024
f32b2c5
Merge remote-tracking branch 'refs/remotes/origin/fix-cpu-freq-apple-…
snOm3ad Jan 6, 2024
2447a89
Delete dev files
snOm3ad Jan 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 102 additions & 0 deletions psutil/arch/osx/cpu.c
Expand Up @@ -29,6 +29,11 @@ For reference, here's the git history with original implementations:
#include "../../_psutil_common.h"
#include "../../_psutil_posix.h"

#if defined(__arm64__) || defined(__aarch64__)
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#endif



PyObject *
Expand Down Expand Up @@ -109,7 +114,103 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
);
}

#if defined(__arm64__) || defined(__aarch64__)
PyObject *
psutil_cpu_freq(PyObject *self, PyObject *args) {
uint32_t min;
uint32_t curr;
uint32_t pMin;
uint32_t eMin;
uint32_t max;
kern_return_t status;
CFDictionaryRef matching = NULL;
CFTypeRef pCoreRef = NULL;
CFTypeRef eCoreRef = NULL;
io_iterator_t iter = 0;
io_registry_entry_t entry = 0;

matching = IOServiceMatching("AppleARMIODevice");
if (matching == 0) {
return PyErr_Format(PyExc_RuntimeError, "IOServiceMatching call failed, 'AppleARMIODevice' not found");
}

status = IOServiceGetMatchingServices(kIOMainPortDefault, matching, &iter);
if (status != KERN_SUCCESS) {
PyErr_Format(PyExc_RuntimeError, "IOServiceGetMatchingServices call failed");
goto error;
}

while ((entry = IOIteratorNext(iter))) {
snOm3ad marked this conversation as resolved.
Show resolved Hide resolved
giampaolo marked this conversation as resolved.
Show resolved Hide resolved
io_name_t name;
status = IORegistryEntryGetName(entry, name);
if (status != KERN_SUCCESS) {
if (entry)
giampaolo marked this conversation as resolved.
Show resolved Hide resolved
IOObjectRelease(entry);
continue;
}
if (strcmp(name, "pmgr") == 0) {
break;
}
snOm3ad marked this conversation as resolved.
Show resolved Hide resolved
IOObjectRelease(entry);
}

if (entry == 0) {
PyErr_Format(PyExc_RuntimeError, "'pmgr' entry was not found in AppleARMIODevice service");
goto error;
}

pCoreRef = IORegistryEntryCreateCFProperty(entry, CFSTR("voltage-states5-sram"), kCFAllocatorDefault, 0);
if (!pCoreRef) {
PyErr_Format(PyExc_RuntimeError, "'voltage-states5-sram' property not found");
goto error;
}

eCoreRef = IORegistryEntryCreateCFProperty(entry, CFSTR("voltage-states1-sram"), kCFAllocatorDefault, 0);
if (!eCoreRef) {
PyErr_Format(PyExc_RuntimeError, "'voltage-states1-sram' property not found");
goto error;
}

snOm3ad marked this conversation as resolved.
Show resolved Hide resolved
size_t pCoreLength = CFDataGetLength(pCoreRef);
size_t eCoreLength = CFDataGetLength(eCoreRef);
if (pCoreLength < 8) {
snOm3ad marked this conversation as resolved.
Show resolved Hide resolved
PyErr_Format(PyExc_RuntimeError, "expected 'voltage-states5-sram' buffer to have at least size 8");
goto error;
}
if (eCoreLength < 4) {
PyErr_Format(PyExc_RuntimeError, "expected 'voltage-states1-sram' buffer to have at least size 4");
goto error;
}

CFDataGetBytes(pCoreRef, CFRangeMake(0, 4), (UInt8 *) &pMin);
CFDataGetBytes(eCoreRef, CFRangeMake(0, 4), (UInt8 *) &eMin);
CFDataGetBytes(pCoreRef, CFRangeMake(pCoreLength - 8, 4), (UInt8 *) &max);
snOm3ad marked this conversation as resolved.
Show resolved Hide resolved

min = pMin < eMin ? pMin : eMin;
curr = max;

CFRelease(pCoreRef);
CFRelease(eCoreRef);
IOObjectRelease(iter);
IOObjectRelease(entry);

return Py_BuildValue(
"IKK",
curr / 1000 / 1000,
min / 1000 / 1000,
max / 1000 / 1000);
error:
if (pCoreRef != NULL)
CFRelease(pCoreRef);
if (eCoreRef != NULL)
CFRelease(eCoreRef);
if (iter)
giampaolo marked this conversation as resolved.
Show resolved Hide resolved
IOObjectRelease(iter);
if (entry)
giampaolo marked this conversation as resolved.
Show resolved Hide resolved
IOObjectRelease(entry);
return NULL;
}
#else
PyObject *
psutil_cpu_freq(PyObject *self, PyObject *args) {
unsigned int curr;
Expand Down Expand Up @@ -138,3 +239,4 @@ psutil_cpu_freq(PyObject *self, PyObject *args) {
min / 1000 / 1000,
max / 1000 / 1000);
}
#endif