/
cpu.c
322 lines (278 loc) · 9.32 KB
/
cpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
System-wide CPU related functions.
Original code was refactored and moved from psutil/_psutil_osx.c in 2020
right before a4c0a0eb0d2a872ab7a45e47fcf37ef1fde5b012.
For reference, here's the git history with original implementations:
- CPU count logical: 3d291d425b856077e65163e43244050fb188def1
- CPU count physical: 4263e354bb4984334bc44adf5dd2f32013d69fba
- CPU times: 32488bdf54aed0f8cef90d639c1667ffaa3c31c7
- CPU stat: fa00dfb961ef63426c7818899340866ced8d2418
- CPU frequency: 6ba1ac4ebfcd8c95fca324b15606ab0ec1412d39
*/
#include <Python.h>
#include <mach/mach_error.h>
#include <mach/mach_host.h>
#include <mach/mach_port.h>
#include <mach/mach_vm.h>
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
#include <mach/mach.h>
#if defined(__arm64__) || defined(__aarch64__)
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#endif
#include "../../_psutil_common.h"
#include "../../_psutil_posix.h"
PyObject *
psutil_cpu_count_logical(PyObject *self, PyObject *args) {
int num;
size_t size = sizeof(int);
if (sysctlbyname("hw.logicalcpu", &num, &size, NULL, 2))
Py_RETURN_NONE; // mimic os.cpu_count()
else
return Py_BuildValue("i", num);
}
PyObject *
psutil_cpu_count_cores(PyObject *self, PyObject *args) {
int num;
size_t size = sizeof(int);
if (sysctlbyname("hw.physicalcpu", &num, &size, NULL, 0))
Py_RETURN_NONE; // mimic os.cpu_count()
else
return Py_BuildValue("i", num);
}
PyObject *
psutil_cpu_times(PyObject *self, PyObject *args) {
mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
kern_return_t error;
host_cpu_load_info_data_t r_load;
mach_port_t host_port = mach_host_self();
error = host_statistics(host_port, HOST_CPU_LOAD_INFO,
(host_info_t)&r_load, &count);
if (error != KERN_SUCCESS) {
return PyErr_Format(
PyExc_RuntimeError,
"host_statistics(HOST_CPU_LOAD_INFO) syscall failed: %s",
mach_error_string(error));
}
mach_port_deallocate(mach_task_self(), host_port);
return Py_BuildValue(
"(dddd)",
(double)r_load.cpu_ticks[CPU_STATE_USER] / CLK_TCK,
(double)r_load.cpu_ticks[CPU_STATE_NICE] / CLK_TCK,
(double)r_load.cpu_ticks[CPU_STATE_SYSTEM] / CLK_TCK,
(double)r_load.cpu_ticks[CPU_STATE_IDLE] / CLK_TCK
);
}
PyObject *
psutil_cpu_stats(PyObject *self, PyObject *args) {
struct vmmeter vmstat;
kern_return_t ret;
mach_msg_type_number_t count = sizeof(vmstat) / sizeof(integer_t);
mach_port_t mport = mach_host_self();
ret = host_statistics(mport, HOST_VM_INFO, (host_info_t)&vmstat, &count);
if (ret != KERN_SUCCESS) {
PyErr_Format(
PyExc_RuntimeError,
"host_statistics(HOST_VM_INFO) failed: %s",
mach_error_string(ret));
return NULL;
}
mach_port_deallocate(mach_task_self(), mport);
return Py_BuildValue(
"IIIII",
vmstat.v_swtch, // ctx switches
vmstat.v_intr, // interrupts
vmstat.v_soft, // software interrupts
vmstat.v_syscall, // syscalls
vmstat.v_trap // traps
);
}
#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;
io_name_t name;
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)) != 0) {
status = IORegistryEntryGetName(entry, name);
if (status != KERN_SUCCESS) {
IOObjectRelease(entry);
continue;
}
if (strcmp(name, "pmgr") == 0) {
break;
}
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 == NULL) {
PyErr_Format(
PyExc_RuntimeError, "'voltage-states5-sram' property not found");
goto error;
}
eCoreRef = IORegistryEntryCreateCFProperty(
entry, CFSTR("voltage-states1-sram"), kCFAllocatorDefault, 0);
if (eCoreRef == NULL) {
PyErr_Format(
PyExc_RuntimeError, "'voltage-states1-sram' property not found");
goto error;
}
size_t pCoreLength = CFDataGetLength(pCoreRef);
size_t eCoreLength = CFDataGetLength(eCoreRef);
if (pCoreLength < 8) {
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);
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 != 0)
IOObjectRelease(iter);
if (entry != 0)
IOObjectRelease(entry);
return NULL;
}
#else
PyObject *
psutil_cpu_freq(PyObject *self, PyObject *args) {
unsigned int curr;
int64_t min = 0;
int64_t max = 0;
int mib[2];
size_t len = sizeof(curr);
size_t size = sizeof(min);
// also available 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("sysctl('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(
"IKK",
curr / 1000 / 1000,
min / 1000 / 1000,
max / 1000 / 1000);
}
#endif
PyObject *
psutil_per_cpu_times(PyObject *self, PyObject *args) {
natural_t cpu_count;
natural_t i;
processor_info_array_t info_array;
mach_msg_type_number_t info_count;
kern_return_t error;
processor_cpu_load_info_data_t *cpu_load_info = NULL;
int ret;
PyObject *py_retlist = PyList_New(0);
PyObject *py_cputime = NULL;
if (py_retlist == NULL)
return NULL;
mach_port_t host_port = mach_host_self();
error = host_processor_info(host_port, PROCESSOR_CPU_LOAD_INFO,
&cpu_count, &info_array, &info_count);
if (error != KERN_SUCCESS) {
PyErr_Format(
PyExc_RuntimeError,
"host_processor_info(PROCESSOR_CPU_LOAD_INFO) syscall failed: %s",
mach_error_string(error));
goto error;
}
mach_port_deallocate(mach_task_self(), host_port);
cpu_load_info = (processor_cpu_load_info_data_t *) info_array;
for (i = 0; i < cpu_count; i++) {
py_cputime = Py_BuildValue(
"(dddd)",
(double)cpu_load_info[i].cpu_ticks[CPU_STATE_USER] / CLK_TCK,
(double)cpu_load_info[i].cpu_ticks[CPU_STATE_NICE] / CLK_TCK,
(double)cpu_load_info[i].cpu_ticks[CPU_STATE_SYSTEM] / CLK_TCK,
(double)cpu_load_info[i].cpu_ticks[CPU_STATE_IDLE] / CLK_TCK
);
if (!py_cputime)
goto error;
if (PyList_Append(py_retlist, py_cputime))
goto error;
Py_CLEAR(py_cputime);
}
ret = vm_deallocate(mach_task_self(), (vm_address_t)info_array,
info_count * sizeof(int));
if (ret != KERN_SUCCESS)
PyErr_WarnEx(PyExc_RuntimeWarning, "vm_deallocate() failed", 2);
return py_retlist;
error:
Py_XDECREF(py_cputime);
Py_DECREF(py_retlist);
if (cpu_load_info != NULL) {
ret = vm_deallocate(mach_task_self(), (vm_address_t)info_array,
info_count * sizeof(int));
if (ret != KERN_SUCCESS)
PyErr_WarnEx(PyExc_RuntimeWarning, "vm_deallocate() failed", 2);
}
return NULL;
}