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");
148 atomic_store_explicit(&keep_going
, 0, memory_order_relaxed
);
150 while (atomic_load_explicit(&keep_going
, memory_order_relaxed
)) {
151 if (thread_switch(THREAD_NULL
, SWITCH_OPTION_NONE
, 0))
152 T_ASSERT_FAIL("thread_switch");
158 void check_device_temperature(void)
161 FILE *pipe
= popen("powerctrl Factor1", "r");
164 T_FAIL("Failed to check device temperature");
168 fgets(buffer
, sizeof(buffer
), pipe
);
170 if (strncmp(POWERCTRL_SUCCESS_STR
, buffer
, strlen(POWERCTRL_SUCCESS_STR
))) {
171 T_PERF("temperature", 0.0, "factor", "device temperature");
173 T_PASS("Device temperature check pass");
174 T_PERF("temperature", 1.0, "factor", "device temperature");
179 void record_perfcontrol_stats(const char *sysctlname
, const char *units
, const char *info
)
182 size_t data_size
= sizeof(data
);
183 T_ASSERT_POSIX_ZERO(sysctlbyname(sysctlname
,
184 &data
, &data_size
, NULL
, 0),
186 T_PERF(info
, data
, units
, info
);
190 T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"));
192 /* Disable the test on MacOS for now */
193 T_DECL(perf_csw
, "context switch performance", T_META_TYPE_PERF
, T_META_CHECK_LEAKS(NO
), T_META_ASROOT(YES
))
197 T_SKIP("Not supported on MacOS");
199 #endif /* CONFIG_EMBEDDED */
200 check_device_temperature();
202 T_ATEND(csw_perf_test_cleanup
);
204 csw_perf_test_init();
205 pthread_setname_np("main thread");
207 T_ASSERT_MACH_SUCCESS(mach_timebase_info(&timebase_info
), "mach_timebase_info");
209 struct sched_param param
= {.sched_priority
= 48};
211 T_ASSERT_POSIX_ZERO(pthread_setschedparam(pthread_self(), SCHED_FIFO
, ¶m
),
212 "pthread_setschedparam");
214 T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &semaphore
,
215 SYNC_POLICY_FIFO
, 0), "semaphore_create");
217 T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &worker_sem
,
218 SYNC_POLICY_FIFO
, 0), "semaphore_create");
220 size_t ncpu_size
= sizeof(g_numcpus
);
221 T_ASSERT_POSIX_ZERO(sysctlbyname("hw.ncpu", &g_numcpus
, &ncpu_size
, NULL
, 0),
222 "sysctlbyname hw.ncpu");
224 printf("hw.ncpu: %d\n", g_numcpus
);
225 uint32_t n_spinners
= g_numcpus
- 1;
227 int mt_supported
= 0;
228 size_t mt_supported_size
= sizeof(mt_supported
);
229 T_ASSERT_POSIX_ZERO(sysctlbyname("kern.monotonic.supported", &mt_supported
,
230 &mt_supported_size
, NULL
, 0), "sysctlbyname kern.monotonic.supported");
232 for (uint32_t thread_id
= 0; thread_id
< n_spinners
; thread_id
++) {
233 threads
[thread_id
].thread
= create_thread(thread_id
, THR_SPINNER_PRI
,
237 s
= dt_stat_time_create("context switch time");
239 create_thread(n_spinners
, THR_MANAGER_PRI
, true, &thread
);
240 threads
[n_spinners
].measure_thread
= true;
241 create_thread(n_spinners
+ 1, THR_MANAGER_PRI
, true, &thread
);
243 /* Allow the context switch threads to get into sem_wait() */
244 for (uint32_t thread_id
= 0; thread_id
< n_spinners
+ 2; thread_id
++) {
245 T_ASSERT_MACH_SUCCESS(semaphore_wait(worker_sem
), "semaphore_wait");
248 int enable_callout_stats
= 1;
249 size_t enable_size
= sizeof(enable_callout_stats
);
252 /* Enable callout stat collection */
253 T_ASSERT_POSIX_ZERO(sysctlbyname("kern.perfcontrol_callout.stats_enabled",
254 NULL
, 0, &enable_callout_stats
, enable_size
),
255 "sysctlbyname kern.perfcontrol_callout.stats_enabled");
258 T_ASSERT_MACH_SUCCESS(semaphore_signal_all(semaphore
), "semaphore_signal");
261 for (uint32_t thread_id
= 0; thread_id
< n_spinners
+ 2; thread_id
++) {
262 T_ASSERT_POSIX_ZERO(pthread_join(threads
[thread_id
].thread
, NULL
),
263 "pthread_join %d", thread_id
);
267 record_perfcontrol_stats("kern.perfcontrol_callout.oncore_instr",
268 "instructions", "oncore.instructions");
269 record_perfcontrol_stats("kern.perfcontrol_callout.offcore_instr",
270 "instructions", "offcore.instructions");
271 record_perfcontrol_stats("kern.perfcontrol_callout.oncore_cycles",
272 "cycles", "oncore.cycles");
273 record_perfcontrol_stats("kern.perfcontrol_callout.offcore_cycles",
274 "cycles", "offcore.cycles");
276 /* Disable callout stat collection */
277 enable_callout_stats
= 0;
278 T_ASSERT_POSIX_ZERO(sysctlbyname("kern.perfcontrol_callout.stats_enabled",
279 NULL
, 0, &enable_callout_stats
, enable_size
),
280 "sysctlbyname kern.perfcontrol_callout.stats_enabled");
283 check_device_temperature();