13 #include <sys/sysctl.h>
14 #include <mach/mach_time.h>
15 #include <mach/mach.h>
16 #include <mach/semaphore.h>
17 #include <TargetConditionals.h>
23 #include <darwintest.h>
24 #include <stdatomic.h>
26 #define MAX_THREADS 32
28 #define THR_SPINNER_PRI 63
29 #define THR_MANAGER_PRI 62
30 #define WARMUP_ITERATIONS 100
31 #define POWERCTRL_SUCCESS_STR "Factor1: 1.000000"
33 static mach_timebase_info_data_t timebase_info
;
34 static semaphore_t semaphore
;
35 static semaphore_t worker_sem
;
36 static uint32_t g_numcpus
;
37 static _Atomic
uint32_t keep_going
= 1;
38 static dt_stat_time_t s
;
43 } threads
[MAX_THREADS
];
46 nanos_to_abs(uint64_t nanos
)
48 return nanos
* timebase_info
.denom
/ timebase_info
.numer
;
51 extern char **environ
;
54 csw_perf_test_init(void)
57 char *const clpcctrl_args
[] = {"/usr/local/bin/clpcctrl", "-f", "5000", NULL
};
58 spawn_ret
= posix_spawn(&pid
, clpcctrl_args
[0], NULL
, NULL
, clpcctrl_args
, environ
);
59 waitpid(pid
, &spawn_ret
, 0);
63 csw_perf_test_cleanup(void)
66 char *const clpcctrl_args
[] = {"/usr/local/bin/clpcctrl", "-d", NULL
};
67 spawn_ret
= posix_spawn(&pid
, clpcctrl_args
[0], NULL
, NULL
, clpcctrl_args
, environ
);
68 waitpid(pid
, &spawn_ret
, 0);
72 create_thread(uint32_t thread_id
, uint32_t priority
, bool fixpri
,
73 void *(*start_routine
)(void *))
77 struct sched_param param
= { .sched_priority
= (int)priority
};
80 T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr
), "pthread_attr_init");
82 T_ASSERT_POSIX_ZERO(pthread_attr_setschedparam(&attr
, ¶m
),
83 "pthread_attr_setschedparam");
86 T_ASSERT_POSIX_ZERO(pthread_attr_setschedpolicy(&attr
, SCHED_RR
),
87 "pthread_attr_setschedpolicy");
90 T_ASSERT_POSIX_ZERO(pthread_create(&new_thread
, &attr
, start_routine
,
91 (void*)(uintptr_t)thread_id
), "pthread_create");
93 T_ASSERT_POSIX_ZERO(pthread_attr_destroy(&attr
), "pthread_attr_destroy");
95 threads
[thread_id
].thread
= new_thread
;
100 /* Spin until a specified number of seconds elapses */
102 spin_for_duration(uint32_t seconds
)
104 uint64_t duration
= nanos_to_abs((uint64_t)seconds
* NSEC_PER_SEC
);
105 uint64_t current_time
= mach_absolute_time();
106 uint64_t timeout
= duration
+ current_time
;
108 uint64_t spin_count
= 0;
110 while (mach_absolute_time() < timeout
&& atomic_load_explicit(&keep_going
,
111 memory_order_relaxed
)) {
117 spin_thread(void *arg
)
119 uint32_t thread_id
= (uint32_t) arg
;
122 snprintf(name
, sizeof(name
), "spin thread %2d", thread_id
);
123 pthread_setname_np(name
);
124 T_ASSERT_MACH_SUCCESS(semaphore_wait_signal(semaphore
, worker_sem
),
125 "semaphore_wait_signal");
126 spin_for_duration(SPIN_SECS
);
133 uint32_t thread_id
= (uint32_t) arg
;
136 snprintf(name
, sizeof(name
), "thread %2d", thread_id
);
137 pthread_setname_np(name
);
138 T_ASSERT_MACH_SUCCESS(semaphore_wait_signal(semaphore
, worker_sem
), "semaphore_wait");
140 if (threads
[thread_id
].measure_thread
) {
141 for (int i
= 0; i
< WARMUP_ITERATIONS
; i
++) {
142 thread_switch(THREAD_NULL
, SWITCH_OPTION_NONE
, 0);
144 T_STAT_MEASURE_LOOP(s
) {
145 if (thread_switch(THREAD_NULL
, SWITCH_OPTION_NONE
, 0)) {
146 T_ASSERT_FAIL("thread_switch");
149 atomic_store_explicit(&keep_going
, 0, memory_order_relaxed
);
151 while (atomic_load_explicit(&keep_going
, memory_order_relaxed
)) {
152 if (thread_switch(THREAD_NULL
, SWITCH_OPTION_NONE
, 0)) {
153 T_ASSERT_FAIL("thread_switch");
161 check_device_temperature(void)
164 FILE *pipe
= popen("powerctrl Factor1", "r");
167 T_FAIL("Failed to check device temperature");
171 fgets(buffer
, sizeof(buffer
), pipe
);
173 if (strncmp(POWERCTRL_SUCCESS_STR
, buffer
, strlen(POWERCTRL_SUCCESS_STR
))) {
174 T_PERF("temperature", 0.0, "factor", "device temperature");
176 T_PASS("Device temperature check pass");
177 T_PERF("temperature", 1.0, "factor", "device temperature");
183 record_perfcontrol_stats(const char *sysctlname
, const char *units
, const char *info
)
186 size_t data_size
= sizeof(data
);
187 T_ASSERT_POSIX_ZERO(sysctlbyname(sysctlname
,
188 &data
, &data_size
, NULL
, 0),
190 T_PERF(info
, data
, units
, info
);
194 T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"));
196 /* Disable the test on MacOS for now */
197 T_DECL(perf_csw
, "context switch performance", T_META_TAG_PERF
, T_META_CHECK_LEAKS(false), T_META_ASROOT(true))
199 #if !defined (__arm__) && !defined(__arm64__)
200 T_SKIP("Not supported on Intel platforms");
202 #endif /* !defined (__arm__) && !defined(__arm64__) */
203 check_device_temperature();
205 T_ATEND(csw_perf_test_cleanup
);
207 csw_perf_test_init();
208 pthread_setname_np("main thread");
210 T_ASSERT_MACH_SUCCESS(mach_timebase_info(&timebase_info
), "mach_timebase_info");
212 struct sched_param param
= {.sched_priority
= 48};
214 T_ASSERT_POSIX_ZERO(pthread_setschedparam(pthread_self(), SCHED_FIFO
, ¶m
),
215 "pthread_setschedparam");
217 T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &semaphore
,
218 SYNC_POLICY_FIFO
, 0), "semaphore_create");
220 T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &worker_sem
,
221 SYNC_POLICY_FIFO
, 0), "semaphore_create");
223 size_t ncpu_size
= sizeof(g_numcpus
);
224 T_ASSERT_POSIX_ZERO(sysctlbyname("hw.ncpu", &g_numcpus
, &ncpu_size
, NULL
, 0),
225 "sysctlbyname hw.ncpu");
227 printf("hw.ncpu: %d\n", g_numcpus
);
228 uint32_t n_spinners
= g_numcpus
- 1;
230 int mt_supported
= 0;
231 size_t mt_supported_size
= sizeof(mt_supported
);
232 T_ASSERT_POSIX_ZERO(sysctlbyname("kern.monotonic.supported", &mt_supported
,
233 &mt_supported_size
, NULL
, 0), "sysctlbyname kern.monotonic.supported");
235 for (uint32_t thread_id
= 0; thread_id
< n_spinners
; thread_id
++) {
236 threads
[thread_id
].thread
= create_thread(thread_id
, THR_SPINNER_PRI
,
240 s
= dt_stat_time_create("context switch time");
242 create_thread(n_spinners
, THR_MANAGER_PRI
, true, &thread
);
243 threads
[n_spinners
].measure_thread
= true;
244 create_thread(n_spinners
+ 1, THR_MANAGER_PRI
, true, &thread
);
246 /* Allow the context switch threads to get into sem_wait() */
247 for (uint32_t thread_id
= 0; thread_id
< n_spinners
+ 2; thread_id
++) {
248 T_ASSERT_MACH_SUCCESS(semaphore_wait(worker_sem
), "semaphore_wait");
251 int enable_callout_stats
= 1;
252 size_t enable_size
= sizeof(enable_callout_stats
);
255 /* Enable callout stat collection */
256 T_ASSERT_POSIX_ZERO(sysctlbyname("kern.perfcontrol_callout.stats_enabled",
257 NULL
, 0, &enable_callout_stats
, enable_size
),
258 "sysctlbyname kern.perfcontrol_callout.stats_enabled");
261 T_ASSERT_MACH_SUCCESS(semaphore_signal_all(semaphore
), "semaphore_signal");
264 for (uint32_t thread_id
= 0; thread_id
< n_spinners
+ 2; thread_id
++) {
265 T_ASSERT_POSIX_ZERO(pthread_join(threads
[thread_id
].thread
, NULL
),
266 "pthread_join %d", thread_id
);
270 record_perfcontrol_stats("kern.perfcontrol_callout.oncore_instr",
271 "instructions", "oncore.instructions");
272 record_perfcontrol_stats("kern.perfcontrol_callout.offcore_instr",
273 "instructions", "offcore.instructions");
274 record_perfcontrol_stats("kern.perfcontrol_callout.oncore_cycles",
275 "cycles", "oncore.cycles");
276 record_perfcontrol_stats("kern.perfcontrol_callout.offcore_cycles",
277 "cycles", "offcore.cycles");
279 /* Disable callout stat collection */
280 enable_callout_stats
= 0;
281 T_ASSERT_POSIX_ZERO(sysctlbyname("kern.perfcontrol_callout.stats_enabled",
282 NULL
, 0, &enable_callout_stats
, enable_size
),
283 "sysctlbyname kern.perfcontrol_callout.stats_enabled");
286 check_device_temperature();