Skip to content

Commit 9a5e937

Browse files
authored
Rollup merge of #93657 - Mark-Simulacrum:apple-measurement, r=pietroalbini
Update CPU idle tracking for apple hosts The previous setup did not properly consider hyperthreads (at least in local testing), which likely skews CI results as well. The new code is both simpler and hopefully will produce more accurate results; locally it matches behavior of the Linux version of this script.
2 parents 520bd35 + 6756ff9 commit 9a5e937

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/ci/cpu-usage-over-time.py

+22-22
Original file line numberDiff line numberDiff line change
@@ -108,37 +108,37 @@ def idle_since(self, prev):
108108
from ctypes import *
109109
libc = cdll.LoadLibrary('/usr/lib/libc.dylib')
110110

111-
PROESSOR_CPU_LOAD_INFO = c_int(2)
111+
class host_cpu_load_info_data_t(Structure):
112+
_fields_ = [("cpu_ticks", c_uint * 4)]
113+
114+
host_statistics = libc.host_statistics
115+
host_statistics.argtypes = [
116+
c_uint,
117+
c_int,
118+
POINTER(host_cpu_load_info_data_t),
119+
POINTER(c_int)
120+
]
121+
host_statistics.restype = c_int
122+
112123
CPU_STATE_USER = 0
113124
CPU_STATE_SYSTEM = 1
114125
CPU_STATE_IDLE = 2
115126
CPU_STATE_NICE = 3
116-
c_int_p = POINTER(c_int)
117-
118127
class State:
119128
def __init__(self):
120-
num_cpus_u = c_uint(0)
121-
cpu_info = c_int_p()
122-
cpu_info_cnt = c_int(0)
123-
err = libc.host_processor_info(
129+
stats = host_cpu_load_info_data_t()
130+
count = c_int(4) # HOST_CPU_LOAD_INFO_COUNT
131+
err = libc.host_statistics(
124132
libc.mach_host_self(),
125-
PROESSOR_CPU_LOAD_INFO,
126-
byref(num_cpus_u),
127-
byref(cpu_info),
128-
byref(cpu_info_cnt),
133+
c_int(3), # HOST_CPU_LOAD_INFO
134+
byref(stats),
135+
byref(count),
129136
)
130137
assert err == 0
131-
self.user = 0
132-
self.system = 0
133-
self.idle = 0
134-
self.nice = 0
135-
cur = 0
136-
while cur < cpu_info_cnt.value:
137-
self.user += cpu_info[cur + CPU_STATE_USER]
138-
self.system += cpu_info[cur + CPU_STATE_SYSTEM]
139-
self.idle += cpu_info[cur + CPU_STATE_IDLE]
140-
self.nice += cpu_info[cur + CPU_STATE_NICE]
141-
cur += num_cpus_u.value
138+
self.system = stats.cpu_ticks[CPU_STATE_SYSTEM]
139+
self.user = stats.cpu_ticks[CPU_STATE_USER]
140+
self.idle = stats.cpu_ticks[CPU_STATE_IDLE]
141+
self.nice = stats.cpu_ticks[CPU_STATE_NICE]
142142

143143
def idle_since(self, prev):
144144
user = self.user - prev.user

0 commit comments

Comments
 (0)