1*2c2f96dcSApple OSS Distributions #include <unistd.h>
2*2c2f96dcSApple OSS Distributions #include <stdlib.h>
3*2c2f96dcSApple OSS Distributions #include <pthread.h>
4*2c2f96dcSApple OSS Distributions #include <spawn.h>
5*2c2f96dcSApple OSS Distributions #include <string.h>
6*2c2f96dcSApple OSS Distributions #include <mach/mach.h>
7*2c2f96dcSApple OSS Distributions #include <mach/mach_time.h>
8*2c2f96dcSApple OSS Distributions #include <TargetConditionals.h>
9*2c2f96dcSApple OSS Distributions #include <sys/work_interval.h>
10*2c2f96dcSApple OSS Distributions #include <sys/stat.h>
11*2c2f96dcSApple OSS Distributions #include <sys/sysctl.h>
12*2c2f96dcSApple OSS Distributions #include <sys/time.h>
13*2c2f96dcSApple OSS Distributions #include <stdatomic.h>
14*2c2f96dcSApple OSS Distributions #include <time.h>
15*2c2f96dcSApple OSS Distributions
16*2c2f96dcSApple OSS Distributions #include <darwintest.h>
17*2c2f96dcSApple OSS Distributions #include <darwintest_utils.h>
18*2c2f96dcSApple OSS Distributions #include <perfdata/perfdata.h>
19*2c2f96dcSApple OSS Distributions
20*2c2f96dcSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
21*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
22*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("scheduler"),
23*2c2f96dcSApple OSS Distributions T_META_TAG_PERF);
24*2c2f96dcSApple OSS Distributions
25*2c2f96dcSApple OSS Distributions /* Code and logic taken from Daniel Chimene's yield-aggressor.c test (rdar://47327537) */
26*2c2f96dcSApple OSS Distributions
27*2c2f96dcSApple OSS Distributions static const size_t MAX_PDJ_PATH_LEN = 256;
28*2c2f96dcSApple OSS Distributions
29*2c2f96dcSApple OSS Distributions static void
sched_yield_loop(uint64_t iterations)30*2c2f96dcSApple OSS Distributions sched_yield_loop(uint64_t iterations)
31*2c2f96dcSApple OSS Distributions {
32*2c2f96dcSApple OSS Distributions for (uint64_t i = 0; i < iterations; i++) {
33*2c2f96dcSApple OSS Distributions sched_yield();
34*2c2f96dcSApple OSS Distributions }
35*2c2f96dcSApple OSS Distributions }
36*2c2f96dcSApple OSS Distributions
37*2c2f96dcSApple OSS Distributions static void
swtch_loop(uint64_t iterations)38*2c2f96dcSApple OSS Distributions swtch_loop(uint64_t iterations)
39*2c2f96dcSApple OSS Distributions {
40*2c2f96dcSApple OSS Distributions for (uint64_t i = 0; i < iterations; i++) {
41*2c2f96dcSApple OSS Distributions swtch();
42*2c2f96dcSApple OSS Distributions }
43*2c2f96dcSApple OSS Distributions }
44*2c2f96dcSApple OSS Distributions
45*2c2f96dcSApple OSS Distributions static void
swtch_pri_loop(uint64_t iterations)46*2c2f96dcSApple OSS Distributions swtch_pri_loop(uint64_t iterations)
47*2c2f96dcSApple OSS Distributions {
48*2c2f96dcSApple OSS Distributions for (uint64_t i = 0; i < iterations; i++) {
49*2c2f96dcSApple OSS Distributions swtch_pri(0);
50*2c2f96dcSApple OSS Distributions }
51*2c2f96dcSApple OSS Distributions }
52*2c2f96dcSApple OSS Distributions
53*2c2f96dcSApple OSS Distributions static void
thread_switch_loop(uint64_t iterations)54*2c2f96dcSApple OSS Distributions thread_switch_loop(uint64_t iterations)
55*2c2f96dcSApple OSS Distributions {
56*2c2f96dcSApple OSS Distributions for (uint64_t i = 0; i < iterations; i++) {
57*2c2f96dcSApple OSS Distributions thread_switch(MACH_PORT_NULL, SWITCH_OPTION_NONE, MACH_MSG_TIMEOUT_NONE);
58*2c2f96dcSApple OSS Distributions }
59*2c2f96dcSApple OSS Distributions }
60*2c2f96dcSApple OSS Distributions
61*2c2f96dcSApple OSS Distributions static void
thread_switch_wait_loop(uint64_t iterations)62*2c2f96dcSApple OSS Distributions thread_switch_wait_loop(uint64_t iterations)
63*2c2f96dcSApple OSS Distributions {
64*2c2f96dcSApple OSS Distributions for (uint64_t i = 0; i < iterations; i++) {
65*2c2f96dcSApple OSS Distributions thread_switch(MACH_PORT_NULL, SWITCH_OPTION_WAIT, MACH_MSG_TIMEOUT_NONE);
66*2c2f96dcSApple OSS Distributions }
67*2c2f96dcSApple OSS Distributions }
68*2c2f96dcSApple OSS Distributions
69*2c2f96dcSApple OSS Distributions static void
thread_switch_depress_loop(uint64_t iterations)70*2c2f96dcSApple OSS Distributions thread_switch_depress_loop(uint64_t iterations)
71*2c2f96dcSApple OSS Distributions {
72*2c2f96dcSApple OSS Distributions for (uint64_t i = 0; i < iterations; i++) {
73*2c2f96dcSApple OSS Distributions thread_switch(MACH_PORT_NULL, SWITCH_OPTION_DEPRESS, MACH_MSG_TIMEOUT_NONE);
74*2c2f96dcSApple OSS Distributions }
75*2c2f96dcSApple OSS Distributions }
76*2c2f96dcSApple OSS Distributions
77*2c2f96dcSApple OSS Distributions typedef enum yield_type {
78*2c2f96dcSApple OSS Distributions SCHED_YIELD = 0,
79*2c2f96dcSApple OSS Distributions SWTCH = 1,
80*2c2f96dcSApple OSS Distributions SWTCH_PRI = 2,
81*2c2f96dcSApple OSS Distributions THREAD_SWITCH = 3,
82*2c2f96dcSApple OSS Distributions THREAD_SWITCH_WAIT = 4,
83*2c2f96dcSApple OSS Distributions THREAD_SWITCH_DEPRESS = 5
84*2c2f96dcSApple OSS Distributions } yield_type_t;
85*2c2f96dcSApple OSS Distributions
86*2c2f96dcSApple OSS Distributions static const int NUM_YIELD_TYPES = 6;
87*2c2f96dcSApple OSS Distributions
88*2c2f96dcSApple OSS Distributions static char* name_table[NUM_YIELD_TYPES] = {
89*2c2f96dcSApple OSS Distributions [SCHED_YIELD] = "sched_yield",
90*2c2f96dcSApple OSS Distributions [SWTCH] = "swtch",
91*2c2f96dcSApple OSS Distributions [SWTCH_PRI] = "swtch_pri",
92*2c2f96dcSApple OSS Distributions [THREAD_SWITCH] = "thread_switch(none)",
93*2c2f96dcSApple OSS Distributions [THREAD_SWITCH_WAIT] = "thread_switch(wait)",
94*2c2f96dcSApple OSS Distributions [THREAD_SWITCH_DEPRESS] = "thread_switch(depress)",
95*2c2f96dcSApple OSS Distributions };
96*2c2f96dcSApple OSS Distributions
97*2c2f96dcSApple OSS Distributions static void (*fn_table[NUM_YIELD_TYPES])(uint64_t) = {
98*2c2f96dcSApple OSS Distributions [SCHED_YIELD] = sched_yield_loop,
99*2c2f96dcSApple OSS Distributions [SWTCH] = swtch_loop,
100*2c2f96dcSApple OSS Distributions [SWTCH_PRI] = swtch_pri_loop,
101*2c2f96dcSApple OSS Distributions [THREAD_SWITCH] = thread_switch_loop,
102*2c2f96dcSApple OSS Distributions [THREAD_SWITCH_WAIT] = thread_switch_wait_loop,
103*2c2f96dcSApple OSS Distributions [THREAD_SWITCH_DEPRESS] = thread_switch_depress_loop,
104*2c2f96dcSApple OSS Distributions };
105*2c2f96dcSApple OSS Distributions
106*2c2f96dcSApple OSS Distributions static semaphore_t ready_sem, go_sem;
107*2c2f96dcSApple OSS Distributions static unsigned int num_iterations, num_threads;
108*2c2f96dcSApple OSS Distributions static _Atomic unsigned int done_threads;
109*2c2f96dcSApple OSS Distributions static yield_type_t curr_yield_type;
110*2c2f96dcSApple OSS Distributions
111*2c2f96dcSApple OSS Distributions static void *
thread_fn(__unused void * arg)112*2c2f96dcSApple OSS Distributions thread_fn(__unused void *arg)
113*2c2f96dcSApple OSS Distributions {
114*2c2f96dcSApple OSS Distributions kern_return_t kr;
115*2c2f96dcSApple OSS Distributions
116*2c2f96dcSApple OSS Distributions kr = semaphore_wait_signal(go_sem, ready_sem);
117*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_wait_signal");
118*2c2f96dcSApple OSS Distributions
119*2c2f96dcSApple OSS Distributions fn_table[curr_yield_type](num_iterations);
120*2c2f96dcSApple OSS Distributions
121*2c2f96dcSApple OSS Distributions if (atomic_fetch_add(&done_threads, 1) == num_threads - 1) {
122*2c2f96dcSApple OSS Distributions kr = semaphore_wait_signal(go_sem, ready_sem);
123*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_wait_signal");
124*2c2f96dcSApple OSS Distributions } else {
125*2c2f96dcSApple OSS Distributions kr = semaphore_wait(go_sem);
126*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_wait");
127*2c2f96dcSApple OSS Distributions }
128*2c2f96dcSApple OSS Distributions return NULL;
129*2c2f96dcSApple OSS Distributions }
130*2c2f96dcSApple OSS Distributions
131*2c2f96dcSApple OSS Distributions static void
start_threads(pthread_t * threads,void * (* start_routine)(void *),int priority)132*2c2f96dcSApple OSS Distributions start_threads(pthread_t *threads, void *(*start_routine)(void *), int priority)
133*2c2f96dcSApple OSS Distributions {
134*2c2f96dcSApple OSS Distributions int rv;
135*2c2f96dcSApple OSS Distributions pthread_attr_t attr;
136*2c2f96dcSApple OSS Distributions
137*2c2f96dcSApple OSS Distributions rv = pthread_attr_init(&attr);
138*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(rv, "pthread_attr_init");
139*2c2f96dcSApple OSS Distributions
140*2c2f96dcSApple OSS Distributions for (unsigned int i = 0; i < num_threads; i++) {
141*2c2f96dcSApple OSS Distributions struct sched_param param = { .sched_priority = (int)priority };
142*2c2f96dcSApple OSS Distributions
143*2c2f96dcSApple OSS Distributions rv = pthread_attr_setschedparam(&attr, ¶m);
144*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(rv, "pthread_attr_setschedparam");
145*2c2f96dcSApple OSS Distributions
146*2c2f96dcSApple OSS Distributions rv = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
147*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(rv, "pthread_attr_setdetachstate");
148*2c2f96dcSApple OSS Distributions
149*2c2f96dcSApple OSS Distributions rv = pthread_create(&threads[i], &attr, start_routine, NULL);
150*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(rv, "pthread_create");
151*2c2f96dcSApple OSS Distributions }
152*2c2f96dcSApple OSS Distributions
153*2c2f96dcSApple OSS Distributions rv = pthread_attr_destroy(&attr);
154*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(rv, "pthread_attr_destroy");
155*2c2f96dcSApple OSS Distributions }
156*2c2f96dcSApple OSS Distributions
157*2c2f96dcSApple OSS Distributions struct cpu_time {
158*2c2f96dcSApple OSS Distributions natural_t sys;
159*2c2f96dcSApple OSS Distributions natural_t user;
160*2c2f96dcSApple OSS Distributions natural_t idle;
161*2c2f96dcSApple OSS Distributions };
162*2c2f96dcSApple OSS Distributions
163*2c2f96dcSApple OSS Distributions static void
record_cpu_time(struct cpu_time * cpu_time)164*2c2f96dcSApple OSS Distributions record_cpu_time(struct cpu_time *cpu_time)
165*2c2f96dcSApple OSS Distributions {
166*2c2f96dcSApple OSS Distributions host_cpu_load_info_data_t load;
167*2c2f96dcSApple OSS Distributions kern_return_t kr;
168*2c2f96dcSApple OSS Distributions mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
169*2c2f96dcSApple OSS Distributions
170*2c2f96dcSApple OSS Distributions kr = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (int *)&load, &count);
171*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_statistics");
172*2c2f96dcSApple OSS Distributions
173*2c2f96dcSApple OSS Distributions cpu_time->sys = load.cpu_ticks[CPU_STATE_SYSTEM];
174*2c2f96dcSApple OSS Distributions cpu_time->user = load.cpu_ticks[CPU_STATE_USER] + load.cpu_ticks[CPU_STATE_NICE];
175*2c2f96dcSApple OSS Distributions cpu_time->idle = load.cpu_ticks[CPU_STATE_IDLE];
176*2c2f96dcSApple OSS Distributions }
177*2c2f96dcSApple OSS Distributions
178*2c2f96dcSApple OSS Distributions static void
write_independent_variables(pdwriter_t writer)179*2c2f96dcSApple OSS Distributions write_independent_variables(pdwriter_t writer)
180*2c2f96dcSApple OSS Distributions {
181*2c2f96dcSApple OSS Distributions pdwriter_record_variable_str(writer, "yield_variant", name_table[curr_yield_type]);
182*2c2f96dcSApple OSS Distributions pdwriter_record_variable_dbl(writer, "num_iterations", num_iterations);
183*2c2f96dcSApple OSS Distributions pdwriter_record_variable_dbl(writer, "num_threads", num_threads);
184*2c2f96dcSApple OSS Distributions }
185*2c2f96dcSApple OSS Distributions
186*2c2f96dcSApple OSS Distributions static const double MS_PER_CPU_TICK = 10.0;
187*2c2f96dcSApple OSS Distributions
188*2c2f96dcSApple OSS Distributions static void
write_time_values(pdwriter_t writer,struct cpu_time * delta_times,uint64_t elapsed_usecs,double idle_ratio)189*2c2f96dcSApple OSS Distributions write_time_values(pdwriter_t writer, struct cpu_time *delta_times, uint64_t elapsed_usecs, double idle_ratio)
190*2c2f96dcSApple OSS Distributions {
191*2c2f96dcSApple OSS Distributions pdwriter_new_value(writer, "system_time", pdunit_milliseconds_cpu, delta_times->sys * MS_PER_CPU_TICK);
192*2c2f96dcSApple OSS Distributions write_independent_variables(writer);
193*2c2f96dcSApple OSS Distributions
194*2c2f96dcSApple OSS Distributions pdwriter_new_value(writer, "user_time", pdunit_milliseconds_cpu, delta_times->user * MS_PER_CPU_TICK);
195*2c2f96dcSApple OSS Distributions write_independent_variables(writer);
196*2c2f96dcSApple OSS Distributions
197*2c2f96dcSApple OSS Distributions pdwriter_new_value(writer, "idle_time", pdunit_milliseconds_cpu, delta_times->idle * MS_PER_CPU_TICK);
198*2c2f96dcSApple OSS Distributions write_independent_variables(writer);
199*2c2f96dcSApple OSS Distributions
200*2c2f96dcSApple OSS Distributions pdwriter_new_value(writer, "wall_clock_time", pdunit_microseconds, elapsed_usecs);
201*2c2f96dcSApple OSS Distributions write_independent_variables(writer);
202*2c2f96dcSApple OSS Distributions
203*2c2f96dcSApple OSS Distributions /* Main metric of note, with a threshold in perfmeta to guard against regression */
204*2c2f96dcSApple OSS Distributions pdwriter_new_value(writer, "idle_time_ratio", pdunit_percent_cpus, idle_ratio);
205*2c2f96dcSApple OSS Distributions write_independent_variables(writer);
206*2c2f96dcSApple OSS Distributions }
207*2c2f96dcSApple OSS Distributions
208*2c2f96dcSApple OSS Distributions static void
run_yielding_test(yield_type_t yield_type,unsigned int num_iters,unsigned int thread_count,int thread_pri,pdwriter_t writer)209*2c2f96dcSApple OSS Distributions run_yielding_test(yield_type_t yield_type, unsigned int num_iters, unsigned int thread_count,
210*2c2f96dcSApple OSS Distributions int thread_pri, pdwriter_t writer)
211*2c2f96dcSApple OSS Distributions {
212*2c2f96dcSApple OSS Distributions T_SETUPBEGIN;
213*2c2f96dcSApple OSS Distributions
214*2c2f96dcSApple OSS Distributions T_LOG("===== Yield Variety: %s", name_table[yield_type]);
215*2c2f96dcSApple OSS Distributions
216*2c2f96dcSApple OSS Distributions kern_return_t kr;
217*2c2f96dcSApple OSS Distributions
218*2c2f96dcSApple OSS Distributions num_iterations = num_iters;
219*2c2f96dcSApple OSS Distributions num_threads = thread_count;
220*2c2f96dcSApple OSS Distributions curr_yield_type = yield_type;
221*2c2f96dcSApple OSS Distributions done_threads = 0;
222*2c2f96dcSApple OSS Distributions
223*2c2f96dcSApple OSS Distributions kr = semaphore_create(mach_task_self(), &ready_sem, SYNC_POLICY_FIFO, 0);
224*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_create");
225*2c2f96dcSApple OSS Distributions kr = semaphore_create(mach_task_self(), &go_sem, SYNC_POLICY_FIFO, 0);
226*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_create");
227*2c2f96dcSApple OSS Distributions
228*2c2f96dcSApple OSS Distributions pthread_t threads[num_threads];
229*2c2f96dcSApple OSS Distributions start_threads(threads, &thread_fn, thread_pri);
230*2c2f96dcSApple OSS Distributions
231*2c2f96dcSApple OSS Distributions for (uint32_t i = 0; i < num_threads; i++) {
232*2c2f96dcSApple OSS Distributions kr = semaphore_wait(ready_sem);
233*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_wait");
234*2c2f96dcSApple OSS Distributions }
235*2c2f96dcSApple OSS Distributions
236*2c2f96dcSApple OSS Distributions /* Wait 100ms for the system to settle down */
237*2c2f96dcSApple OSS Distributions usleep(100000);
238*2c2f96dcSApple OSS Distributions
239*2c2f96dcSApple OSS Distributions T_SETUPEND;
240*2c2f96dcSApple OSS Distributions
241*2c2f96dcSApple OSS Distributions struct cpu_time start_times, finish_times, delta_times;
242*2c2f96dcSApple OSS Distributions uint64_t before_nsec, after_nsec;
243*2c2f96dcSApple OSS Distributions
244*2c2f96dcSApple OSS Distributions record_cpu_time(&start_times);
245*2c2f96dcSApple OSS Distributions before_nsec = clock_gettime_nsec_np(CLOCK_REALTIME);
246*2c2f96dcSApple OSS Distributions
247*2c2f96dcSApple OSS Distributions /* Signal threads to begin yielding "work" */
248*2c2f96dcSApple OSS Distributions kr = semaphore_signal_all(go_sem);
249*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_signal_all");
250*2c2f96dcSApple OSS Distributions
251*2c2f96dcSApple OSS Distributions kr = semaphore_wait(ready_sem);
252*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_wait");
253*2c2f96dcSApple OSS Distributions
254*2c2f96dcSApple OSS Distributions /* Capture cpu stats after yielding "work" has finished */
255*2c2f96dcSApple OSS Distributions after_nsec = clock_gettime_nsec_np(CLOCK_REALTIME);
256*2c2f96dcSApple OSS Distributions record_cpu_time(&finish_times);
257*2c2f96dcSApple OSS Distributions
258*2c2f96dcSApple OSS Distributions uint64_t elapsed_usecs = (after_nsec - before_nsec) / 1000;
259*2c2f96dcSApple OSS Distributions T_LOG("All %u threads finished yielding %u times each", num_threads, num_iterations);
260*2c2f96dcSApple OSS Distributions T_LOG("Elapsed Runtime: %f seconds", ((double) elapsed_usecs) / USEC_PER_SEC);
261*2c2f96dcSApple OSS Distributions
262*2c2f96dcSApple OSS Distributions delta_times.sys = finish_times.sys - start_times.sys;
263*2c2f96dcSApple OSS Distributions delta_times.user = finish_times.user - start_times.user;
264*2c2f96dcSApple OSS Distributions delta_times.idle = finish_times.idle - start_times.idle;
265*2c2f96dcSApple OSS Distributions T_LOG("System CPU ticks: %d", delta_times.sys);
266*2c2f96dcSApple OSS Distributions T_LOG("User CPU ticks: %d", delta_times.user);
267*2c2f96dcSApple OSS Distributions T_LOG("Idle CPU ticks: %d", delta_times.idle);
268*2c2f96dcSApple OSS Distributions
269*2c2f96dcSApple OSS Distributions natural_t total_ticks = delta_times.sys + delta_times.user + delta_times.idle;
270*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_GT(total_ticks, 0, "CPU load stats failed to update, likely due to host_statistics() rate limit");
271*2c2f96dcSApple OSS Distributions
272*2c2f96dcSApple OSS Distributions double cpu_idle_ratio = delta_times.idle * 1.0 / total_ticks;
273*2c2f96dcSApple OSS Distributions T_LOG("*** Ratio of Idle CPU time: %f\n\n", cpu_idle_ratio);
274*2c2f96dcSApple OSS Distributions
275*2c2f96dcSApple OSS Distributions write_time_values(writer, &delta_times, elapsed_usecs, cpu_idle_ratio);
276*2c2f96dcSApple OSS Distributions }
277*2c2f96dcSApple OSS Distributions
278*2c2f96dcSApple OSS Distributions static const int DEFAULT_THREAD_PRI = 31;
279*2c2f96dcSApple OSS Distributions static const int DEFAULT_NUM_ITERS = 100000;
280*2c2f96dcSApple OSS Distributions
281*2c2f96dcSApple OSS Distributions #define KERNEL_BOOTARGS_MAX_SIZE 1024
282*2c2f96dcSApple OSS Distributions static char kernel_bootargs[KERNEL_BOOTARGS_MAX_SIZE];
283*2c2f96dcSApple OSS Distributions
284*2c2f96dcSApple OSS Distributions T_DECL(yield_aggressor,
285*2c2f96dcSApple OSS Distributions "Ensure that CPUs do not go idle when there are many threads all yielding "
286*2c2f96dcSApple OSS Distributions "in a loop (for different varieties of yield)",
287*2c2f96dcSApple OSS Distributions /* Required to get around the rate limit for host_statistics() */
288*2c2f96dcSApple OSS Distributions T_META_BOOTARGS_SET("amfi_get_out_of_my_way=1"),
289*2c2f96dcSApple OSS Distributions T_META_ASROOT(true))
290*2c2f96dcSApple OSS Distributions {
291*2c2f96dcSApple OSS Distributions /* Warn if amfi_get_out_of_my_way is not set and fail later on if we actually run into the rate limit */
292*2c2f96dcSApple OSS Distributions size_t kernel_bootargs_size = sizeof(kernel_bootargs);
293*2c2f96dcSApple OSS Distributions int rv = sysctlbyname("kern.bootargs", kernel_bootargs, &kernel_bootargs_size, NULL, 0);
294*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kern.bootargs");
295*2c2f96dcSApple OSS Distributions if (strstr(kernel_bootargs, "amfi_get_out_of_my_way=1") == NULL) {
296*2c2f96dcSApple OSS Distributions T_LOG("WARNING: amfi_get_out_of_my_way=1 boot-arg is missing, required to reliably capture CPU load data");
297*2c2f96dcSApple OSS Distributions }
298*2c2f96dcSApple OSS Distributions
299*2c2f96dcSApple OSS Distributions char pdj_path[MAX_PDJ_PATH_LEN];
300*2c2f96dcSApple OSS Distributions pdwriter_t writer = pdwriter_open_tmp("xnu", "scheduler.yield_aggressor", 0, 0, pdj_path, MAX_PDJ_PATH_LEN);
301*2c2f96dcSApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(writer, "pdwriter_open_tmp");
302*2c2f96dcSApple OSS Distributions
303*2c2f96dcSApple OSS Distributions /*
304*2c2f96dcSApple OSS Distributions * Thread count is NCPU * 3 in order to ensure that there are enough yielding threads
305*2c2f96dcSApple OSS Distributions * to keep all of the cores busy context-switching between them. NCPU * 1 threads would
306*2c2f96dcSApple OSS Distributions * not be sufficient to guarantee this, because a core temporarily keeps two threads
307*2c2f96dcSApple OSS Distributions * off of the run-queues at a time while performing a context-switch (rather than only
308*2c2f96dcSApple OSS Distributions * the one thread it is running during normal execution). Lastly, we choose NCPU * 3
309*2c2f96dcSApple OSS Distributions * rather than NCPU * 2 because doing so empirically reduces the variance of values
310*2c2f96dcSApple OSS Distributions * betweens runs.
311*2c2f96dcSApple OSS Distributions */
312*2c2f96dcSApple OSS Distributions unsigned int thread_count = (unsigned int) dt_ncpu() * 3;
313*2c2f96dcSApple OSS Distributions
314*2c2f96dcSApple OSS Distributions for (yield_type_t yield_type = SCHED_YIELD; yield_type <= THREAD_SWITCH_DEPRESS; yield_type++) {
315*2c2f96dcSApple OSS Distributions run_yielding_test(yield_type, DEFAULT_NUM_ITERS, thread_count, DEFAULT_THREAD_PRI, writer);
316*2c2f96dcSApple OSS Distributions }
317*2c2f96dcSApple OSS Distributions
318*2c2f96dcSApple OSS Distributions T_LOG("Perfdata file written to: %s", pdj_path);
319*2c2f96dcSApple OSS Distributions pdwriter_close(writer);
320*2c2f96dcSApple OSS Distributions
321*2c2f96dcSApple OSS Distributions T_END;
322*2c2f96dcSApple OSS Distributions }
323