1 // Copyright 2021-2022 (c) Apple Inc. All rights reserved.
2
3 #include <darwintest.h>
4 #include <darwintest_utils.h>
5 #include <inttypes.h>
6 #include <libproc.h>
7 #include <mach/mach.h>
8 #include <mach/task_info.h>
9 #include <mach/thread_info.h>
10 #include <stdint.h>
11 #include <sys/resource.h>
12 #include <unistd.h>
13
14 #include "test_utils.h"
15 #include "recount_test_utils.h"
16
17 T_GLOBAL_META(
18 T_META_RADAR_COMPONENT_NAME("xnu"),
19 T_META_RADAR_COMPONENT_VERSION("RM"),
20 T_META_OWNER("mwidmann"),
21 T_META_CHECK_LEAKS(false));
22
23 static void
proc_pidtaskinfo_increasing(pid_t pid,struct proc_taskinfo * last,const char * desc)24 proc_pidtaskinfo_increasing(pid_t pid, struct proc_taskinfo *last,
25 const char *desc)
26 {
27 struct proc_taskinfo info = { 0 };
28 T_SETUPBEGIN;
29 int ret = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &info, sizeof(info));
30 T_QUIET;
31 T_ASSERT_POSIX_SUCCESS(ret, "proc_pidinfo(..., PROC_PIDTASKINFO, ...)");
32 T_SETUPEND;
33
34 const char *name = "PROC_PIDTASKINFO";
35 T_LOG("%s: usr = %llu, sys = %llu, th_usr = %llu, th_sys = %llu, "
36 "term_usr = %llu, term_sys = %llu", name, info.pti_total_user,
37 info.pti_total_system, info.pti_threads_user,
38 info.pti_threads_system,
39 info.pti_total_user - info.pti_threads_user,
40 info.pti_total_system - info.pti_threads_system);
41 T_EXPECT_GE(info.pti_total_user, last->pti_total_user,
42 "%s user time should increase %s", name, desc);
43 T_EXPECT_GE(info.pti_total_system, last->pti_total_system,
44 "%s system time should increase %s", name, desc);
45 *last = info;
46 }
47
48 static void *
spin_thread(void * arg)49 spin_thread(void *arg)
50 {
51 volatile int *spin = arg;
52 while (*spin);
53 return NULL;
54 }
55
56 static void *
sleep_thread(void * arg)57 sleep_thread(void *arg)
58 {
59 volatile int *keep_going = arg;
60 while (*keep_going) {
61 usleep(100000);
62 }
63 return NULL;
64 }
65
66 enum usage_style {
67 USAGE_SPIN,
68 USAGE_SLEEP,
69 };
70
71 struct usage_thread {
72 enum usage_style ut_style;
73 const char *ut_name;
74 uintptr_t ut_arg;
75 pthread_t ut_thread;
76 };
77
78 static void
thread_start(struct usage_thread * th,const char * name,enum usage_style style)79 thread_start(struct usage_thread *th, const char *name, enum usage_style style)
80 {
81 th->ut_style = style;
82 th->ut_name = name;
83 th->ut_arg = 1;
84 T_SETUPBEGIN;
85 int error = pthread_create(&th->ut_thread, NULL,
86 style == USAGE_SPIN ? spin_thread : sleep_thread, &th->ut_arg);
87 T_QUIET; T_ASSERT_POSIX_ZERO(error, "pthread_create");
88 T_LOG("created %s thread to %s", name,
89 style == USAGE_SPIN ? "spin" : "sleep");
90 T_SETUPEND;
91 }
92
93 static void
thread_end(struct usage_thread * th)94 thread_end(struct usage_thread *th)
95 {
96 th->ut_arg = 0;
97 T_SETUPBEGIN;
98 int error = pthread_join(th->ut_thread, NULL);
99 T_QUIET; T_ASSERT_POSIX_ZERO(error, "pthread_join");
100 T_LOG("terminated %s thread", th->ut_name);
101 T_SETUPEND;
102 }
103
104 T_DECL(proc_pidtaskinfo_sanity, "ensure proc_pidtaskinfo CPU times are sane")
105 {
106 struct proc_taskinfo prev = { 0 };
107 struct usage_thread first = { 0 };
108 struct usage_thread second = { 0 };
109
110 proc_pidtaskinfo_increasing(getpid(), &prev, "initially");
111 thread_start(&first, "first", USAGE_SPIN);
112 proc_pidtaskinfo_increasing(getpid(), &prev,
113 "after first thread has been created");
114 thread_start(&second, "second", USAGE_SPIN);
115 proc_pidtaskinfo_increasing(getpid(), &prev,
116 "after second thread has been created");
117 // Sleep for ~10 quanta.
118 usleep(100 * 1000);
119 thread_end(&first);
120 proc_pidtaskinfo_increasing(getpid(), &prev,
121 "after first thread has terminated");
122 thread_end(&second);
123 proc_pidtaskinfo_increasing(getpid(), &prev,
124 "after all threads have terminated");
125 }
126
127 struct usr_sys_times {
128 uint64_t usr_time;
129 uint64_t sys_time;
130 };
131
132 static void
_assert_increasing(struct usr_sys_times * before,struct usr_sys_times * after,const char * name,const char * desc)133 _assert_increasing(struct usr_sys_times *before, struct usr_sys_times *after,
134 const char *name, const char *desc)
135 {
136 T_EXPECT_GE(after->usr_time, before->usr_time,
137 "%s user time should increase %s", name, desc);
138 T_EXPECT_GE(after->sys_time, before->sys_time,
139 "%s system time should increase %s", name, desc);
140 }
141
142 static void
test_usr_sys_time_sanity(struct usr_sys_times (* fn)(pid_t),const char * name)143 test_usr_sys_time_sanity(struct usr_sys_times (*fn)(pid_t), const char *name)
144 {
145 struct usr_sys_times init = fn(getpid());
146 struct usage_thread first = { 0 };
147 thread_start(&first, "first", USAGE_SLEEP);
148
149 struct usr_sys_times thread_active = fn(getpid());
150 _assert_increasing(&init, &thread_active, name,
151 "after first thread has been created");
152
153 struct usage_thread second = { 0 };
154 thread_start(&second, "second", USAGE_SLEEP);
155
156 struct usr_sys_times thread_top_active = fn(getpid());
157 _assert_increasing(&thread_active, &thread_top_active, name,
158 "after second thread has been created");
159
160 thread_end(&first);
161
162 struct usr_sys_times thread_top_gone = fn(getpid());
163 _assert_increasing(&thread_top_active, &thread_top_gone, name,
164 "after first thread has terminated");
165
166 thread_end(&second);
167
168 struct usr_sys_times thread_gone = fn(getpid());
169 _assert_increasing(&thread_top_gone, &thread_gone, name,
170 "after all threads have terminated");
171 }
172
173 static void
_get_proc_pid_rusage(pid_t pid,struct rusage_info_v6 * info)174 _get_proc_pid_rusage(pid_t pid, struct rusage_info_v6 *info)
175 {
176 T_SETUPBEGIN;
177 int ret = proc_pid_rusage(pid, RUSAGE_INFO_V6, (rusage_info_t *)info);
178 T_QUIET;
179 T_ASSERT_POSIX_SUCCESS(ret, "proc_pid_rusage");
180 T_SETUPEND;
181 }
182
183 static struct usr_sys_times
proc_pid_rusage_times(pid_t pid)184 proc_pid_rusage_times(pid_t pid)
185 {
186 struct rusage_info_v6 info = { 0 };
187 _get_proc_pid_rusage(pid, &info);
188 return (struct usr_sys_times){
189 .usr_time = info.ri_user_time,
190 .sys_time = info.ri_system_time,
191 };
192 }
193
194 T_DECL(proc_pid_rusage_sanity, "ensure proc_pidtaskinfo CPU times are sane")
195 {
196 test_usr_sys_time_sanity(proc_pid_rusage_times, "proc_pid_rusage");
197 }
198
199 static struct usr_sys_times
task_basic_info_times(pid_t __unused pid)200 task_basic_info_times(pid_t __unused pid)
201 {
202 struct task_basic_info_64 info = { 0 };
203 mach_msg_type_number_t info_count = TASK_BASIC_INFO_64_COUNT;
204
205 T_SETUPBEGIN;
206 kern_return_t kr = task_info(mach_task_self(), TASK_BASIC_INFO_64,
207 (task_info_t)&info, &info_count);
208 T_QUIET;
209 T_ASSERT_MACH_SUCCESS(kr, "task_info(... TASK_BASIC_INFO_64 ...)");
210 T_SETUPEND;
211
212 return (struct usr_sys_times){
213 .usr_time = ns_from_time_value(info.user_time),
214 .sys_time = ns_from_time_value(info.system_time),
215 };
216 }
217
218 T_DECL(task_basic_info_sanity, "ensure TASK_BASIC_INFO CPU times are sane")
219 {
220 test_usr_sys_time_sanity(task_basic_info_times, "TASK_BASIC_INFO");
221 }
222
223 static struct usr_sys_times
task_power_info_times(pid_t __unused pid)224 task_power_info_times(pid_t __unused pid)
225 {
226 struct task_power_info info = { 0 };
227 mach_msg_type_number_t info_count = TASK_POWER_INFO_COUNT;
228 kern_return_t kr = task_info(mach_task_self(), TASK_POWER_INFO,
229 (task_info_t)&info, &info_count);
230
231 T_SETUPBEGIN;
232 T_QUIET;
233 T_ASSERT_MACH_SUCCESS(kr, "task_info(... TASK_POWER_INFO ...)");
234 T_SETUPEND;
235
236 return (struct usr_sys_times){
237 .usr_time = ns_from_mach(info.total_user),
238 .sys_time = ns_from_mach(info.total_system),
239 };
240 }
241
242 T_DECL(task_power_info_sanity, "ensure TASK_POWER_INFO CPU times are sane")
243 {
244 test_usr_sys_time_sanity(task_power_info_times, "TASK_POWER_INFO");
245 }
246
247 static struct usr_sys_times
task_absolutetime_info_times(pid_t __unused pid)248 task_absolutetime_info_times(pid_t __unused pid)
249 {
250 task_absolutetime_info_data_t info = { 0 };
251 mach_msg_type_number_t info_count = TASK_ABSOLUTETIME_INFO_COUNT;
252 kern_return_t kr = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO,
253 (task_info_t)&info, &info_count);
254
255 T_SETUPBEGIN;
256 T_QUIET;
257 T_ASSERT_MACH_SUCCESS(kr, "task_info(... TASK_ABSOLUTETIME_INFO ...)");
258 T_SETUPEND;
259
260 return (struct usr_sys_times){
261 .usr_time = ns_from_mach(info.total_user),
262 .sys_time = ns_from_mach(info.total_system),
263 };
264 }
265
266 T_DECL(task_absolutetime_info_sanity,
267 "ensure TASK_ABSOLUTETIME_INFO CPU times are sane")
268 {
269 test_usr_sys_time_sanity(task_absolutetime_info_times,
270 "TASK_ABSOLUTETIME_INFO");
271 }
272
273 static struct usr_sys_times
getrusage_times(pid_t __unused pid)274 getrusage_times(pid_t __unused pid)
275 {
276 struct rusage usage = { 0 };
277 int ret = getrusage(RUSAGE_SELF, &usage);
278
279 T_SETUPBEGIN;
280 T_QUIET;
281 T_ASSERT_POSIX_SUCCESS(ret, "getrusage(RUSAGE_SELF ...)");
282 T_SETUPEND;
283
284 return (struct usr_sys_times){
285 .usr_time = ns_from_timeval(usage.ru_utime),
286 .sys_time = ns_from_timeval(usage.ru_stime),
287 };
288 }
289
290 T_DECL(getrusage_sanity, "ensure getrusage CPU times are sane")
291 {
292 test_usr_sys_time_sanity(getrusage_times, "getrusage");
293 }
294
295 T_DECL(thread_selfusage_sanity, "ensure thread_selfusage times are sane")
296 {
297 uint64_t before = __thread_selfusage();
298 uint64_t after = __thread_selfusage();
299 T_ASSERT_GT(after, before, "thread_selfusage is increasing");
300 before = __thread_selfusage();
301 for (int i = 0; i < 5; i++) {
302 usleep(1000);
303 }
304 after = __thread_selfusage();
305 T_ASSERT_GT(after, before, "thread_selfusage increases after sleeping");
306 }
307
308 T_DECL(proc_pid_rusage_perf_levels,
309 "ensure proc_pid_rusage fills in per-perf level information",
310 REQUIRE_RECOUNT_PMCS,
311 REQUIRE_MULTIPLE_PERF_LEVELS,
312 SET_THREAD_BIND_BOOTARG)
313 {
314 struct rusage_info_v6 before = { 0 };
315 struct rusage_info_v6 after = { 0 };
316
317 _get_proc_pid_rusage(getpid(), &before);
318 run_on_all_perf_levels();
319 _get_proc_pid_rusage(getpid(), &after);
320
321 T_EXPECT_GE(after.ri_cycles, before.ri_cycles, "cycles increasing");
322 T_EXPECT_GE(after.ri_instructions, before.ri_instructions,
323 "instructions increasing");
324 T_EXPECT_GE(after.ri_user_time, before.ri_user_time,
325 "user_time increasing");
326 T_EXPECT_GE(after.ri_system_time, before.ri_system_time,
327 "system_time increasing");
328
329 T_EXPECT_GE(after.ri_pcycles, before.ri_pcycles, "cycles_p increasing");
330 T_EXPECT_GE(after.ri_pinstructions, before.ri_pinstructions,
331 "instructions_p increasing");
332 T_EXPECT_GE(after.ri_user_ptime, before.ri_user_ptime,
333 "user_time_p increasing");
334 T_EXPECT_GE(after.ri_system_ptime, before.ri_system_ptime,
335 "system_time_p increasing");
336
337 if (has_energy()) {
338 T_EXPECT_GE(after.ri_energy_nj, before.ri_energy_nj,
339 "energy_nj increasing");
340 T_EXPECT_GE(after.ri_penergy_nj, before.ri_penergy_nj,
341 "penergy_nj increasing");
342 }
343 }
344
345 static void
_proc_pidthreadcounts_increasing(struct proc_threadcounts_data * before,struct proc_threadcounts_data * after,const char * perf_level)346 _proc_pidthreadcounts_increasing(struct proc_threadcounts_data *before,
347 struct proc_threadcounts_data *after, const char *perf_level)
348 {
349 const char *name = "PROC_PIDTHREADCOUNTS";
350 T_LOG("%s %s before: usr = %llu, sys = %llu, instrs = %llu, cycles = %llu, "
351 "energy = %llu", name, perf_level, before->ptcd_user_time_mach,
352 before->ptcd_system_time_mach, before->ptcd_instructions,
353 before->ptcd_cycles, before->ptcd_energy_nj);
354 T_LOG("%s %s after: usr = %llu, sys = %llu, instrs = %llu, cycles = %llu, "
355 "energy = %llu", name, perf_level, after->ptcd_user_time_mach,
356 after->ptcd_system_time_mach, after->ptcd_instructions,
357 after->ptcd_cycles, after->ptcd_energy_nj);
358
359 T_EXPECT_NE(before->ptcd_user_time_mach, 0ULL,
360 "%s user time should be non-zero", perf_level);
361 T_EXPECT_NE(before->ptcd_system_time_mach, 0ULL,
362 "%s system time should be non-zero", perf_level);
363 T_EXPECT_NE(before->ptcd_instructions, 0ULL,
364 "%s instructions should be non-zero", perf_level);
365 T_EXPECT_NE(before->ptcd_cycles, 0ULL,
366 "%s cycles should be non-zero", perf_level);
367
368 T_EXPECT_GT(after->ptcd_user_time_mach, before->ptcd_user_time_mach,
369 "%s user time should increase", perf_level);
370 T_EXPECT_GT(after->ptcd_system_time_mach, before->ptcd_system_time_mach,
371 "%s system time should increase", perf_level);
372 T_EXPECT_GT(after->ptcd_instructions, before->ptcd_instructions,
373 "%s instructions should increase", perf_level);
374 T_EXPECT_GT(after->ptcd_cycles, before->ptcd_cycles,
375 "%s cycles should increase", perf_level);
376
377 if (has_energy()) {
378 T_EXPECT_GT(after->ptcd_energy_nj, before->ptcd_energy_nj,
379 "%s energy should increase", perf_level);
380 }
381 }
382
383 static void
_threadcounts_to_rusage_info(struct proc_threadcounts_data * counts,struct rusage_info_v6 * info)384 _threadcounts_to_rusage_info(struct proc_threadcounts_data *counts,
385 struct rusage_info_v6 *info)
386 {
387 unsigned int level_count = perf_level_count();
388 for (unsigned int i = 0; i < level_count; i++) {
389 struct proc_threadcounts_data *count = &counts[i];
390 if (perf_level_name(i)[0] == 'P') {
391 info->ri_system_ptime += count->ptcd_system_time_mach;
392 info->ri_user_ptime += count->ptcd_user_time_mach;
393 info->ri_pinstructions += count->ptcd_instructions;
394 info->ri_pcycles += count->ptcd_cycles;
395 }
396 info->ri_system_time += count->ptcd_system_time_mach;
397 info->ri_user_time += count->ptcd_user_time_mach;
398 info->ri_instructions += count->ptcd_instructions;
399 info->ri_cycles += count->ptcd_cycles;
400 }
401 }
402
403 static void
_rusage_info_le(struct rusage_info_v6 * lhs,const char * lhs_name,struct rusage_info_v6 * rhs,const char * rhs_name)404 _rusage_info_le(struct rusage_info_v6 *lhs, const char *lhs_name,
405 struct rusage_info_v6 *rhs, const char *rhs_name)
406 {
407 T_EXPECT_LE(lhs->ri_user_time, rhs->ri_user_time,
408 "%s user time <= %s", lhs_name, rhs_name);
409 T_EXPECT_LE(lhs->ri_system_time, rhs->ri_system_time,
410 "%s system time <= %s", lhs_name, rhs_name);
411 T_EXPECT_LE(lhs->ri_instructions, rhs->ri_instructions,
412 "%s instructions <= %s", lhs_name, rhs_name);
413 T_EXPECT_LE(lhs->ri_cycles, rhs->ri_cycles,
414 "%s cycles <= %s", lhs_name, rhs_name);
415 T_EXPECT_LE(lhs->ri_energy_nj, rhs->ri_energy_nj,
416 "%s energy <= %s", lhs_name, rhs_name);
417
418 T_EXPECT_LE(lhs->ri_user_ptime, rhs->ri_user_ptime,
419 "%s P-core user time <= %s", lhs_name, rhs_name);
420 T_EXPECT_LE(lhs->ri_system_ptime, rhs->ri_system_ptime,
421 "%s P-core system time <= %s", lhs_name, rhs_name);
422 T_EXPECT_LE(lhs->ri_pinstructions, rhs->ri_pinstructions,
423 "%s P-core instructions <= %s", lhs_name, rhs_name);
424 T_EXPECT_LE(lhs->ri_pcycles, rhs->ri_pcycles,
425 "%s P-core cycles <= %s", lhs_name, rhs_name);
426 T_EXPECT_LE(lhs->ri_penergy_nj, rhs->ri_penergy_nj,
427 "%s energy <= %s", lhs_name, rhs_name);
428 }
429
430 struct thread_sequence {
431 dispatch_semaphore_t child_sema;
432 dispatch_semaphore_t parent_sema;
433 };
434
435 static void *
_thread_runs_on_perf_levels(void * vsequence)436 _thread_runs_on_perf_levels(void *vsequence)
437 {
438 struct thread_sequence *seq = vsequence;
439
440 run_on_all_perf_levels();
441 dispatch_semaphore_signal(seq->parent_sema);
442 dispatch_semaphore_wait(seq->child_sema, DISPATCH_TIME_FOREVER);
443
444 run_on_all_perf_levels();
445 dispatch_semaphore_signal(seq->parent_sema);
446 dispatch_semaphore_wait(seq->child_sema, DISPATCH_TIME_FOREVER);
447 return NULL;
448 }
449
450 T_DECL(proc_pidthreadcounts_sanity,
451 "check per-perf level time and CPI from proc_pidthreadcounts",
452 REQUIRE_RECOUNT_PMCS,
453 SET_THREAD_BIND_BOOTARG,
454 // Select the most comprehensive test to run on each SoC.
455 XNU_T_META_SOC_SPECIFIC,
456 T_META_ASROOT(true))
457 {
458 T_SETUPBEGIN;
459
460 unsigned int level_count = perf_level_count();
461 T_LOG("found %u perf levels", level_count);
462 int counts_size = (int)sizeof(struct proc_threadcounts) +
463 (int)level_count * (int)sizeof(struct proc_threadcounts_data);
464 struct proc_threadcounts *before = malloc((unsigned int)counts_size);
465 T_QUIET; T_ASSERT_NOTNULL(before, "allocate before counts");
466 memset(before, 0, counts_size);
467 struct proc_threadcounts *after = malloc((unsigned int)counts_size);
468 T_QUIET; T_ASSERT_NOTNULL(before, "allocate after counts");
469 memset(after, 0, counts_size);
470 pthread_t target_thread = NULL;
471 uint64_t target_tid = 0;
472
473 struct thread_sequence seq = {
474 .parent_sema = dispatch_semaphore_create(0),
475 .child_sema = dispatch_semaphore_create(0),
476 };
477 int error = pthread_create(&target_thread, NULL,
478 _thread_runs_on_perf_levels, &seq);
479 T_QUIET; T_ASSERT_POSIX_ZERO(error, "pthread_create");
480 error = pthread_threadid_np(target_thread, &target_tid);
481 T_QUIET; T_ASSERT_POSIX_ZERO(error, "pthread_threadid_np");
482 T_LOG("created thread to run on all perf levels with ID %" PRIx64,
483 target_tid);
484
485 dispatch_semaphore_wait(seq.parent_sema, DISPATCH_TIME_FOREVER);
486
487 T_SETUPEND;
488
489 int size = proc_pidinfo(getpid(), PROC_PIDTHREADCOUNTS, target_tid, before,
490 counts_size);
491 T_WITH_ERRNO;
492 T_ASSERT_EQ(size, counts_size,
493 "proc_pidinfo(..., PROC_PIDTHREADCOUNTS, ...)");
494
495 dispatch_semaphore_signal(seq.child_sema);
496 dispatch_semaphore_wait(seq.parent_sema, DISPATCH_TIME_FOREVER);
497
498 size = proc_pidinfo(getpid(), PROC_PIDTHREADCOUNTS, target_tid, after,
499 counts_size);
500 T_WITH_ERRNO;
501 T_ASSERT_EQ(size, counts_size,
502 "proc_pidinfo(..., PROC_PIDTHREADCOUNTS, ...)");
503
504 struct rusage_info_v6 proc_usage = { 0 };
505 _get_proc_pid_rusage(getpid(), &proc_usage);
506
507
508 dispatch_semaphore_signal(seq.child_sema);
509
510 for (unsigned int i = 0; i < level_count; i++) {
511 _proc_pidthreadcounts_increasing(&before->ptc_counts[i],
512 &after->ptc_counts[i], perf_level_name(i));
513 }
514 struct rusage_info_v6 thread_usage = { 0 };
515 _threadcounts_to_rusage_info(after->ptc_counts, &thread_usage);
516 _rusage_info_le(&thread_usage, "thread", &proc_usage, "process");
517
518 (void)pthread_join(target_thread, NULL);
519 free(before);
520 free(after);
521 }
522
523 T_DECL(proc_pidthreadcounts_invalid_tid,
524 "check that proc_pidthreadcounts returns ESRCH on invalid thread",
525 T_META_ASROOT(true))
526 {
527 T_SETUPBEGIN;
528 unsigned int level_count = perf_level_count();
529 int counts_size = (int)sizeof(struct proc_threadcounts) +
530 (int)level_count * (int)sizeof(struct proc_threadcounts_data);
531 struct proc_threadcounts *counts = malloc((unsigned int)counts_size);
532 T_QUIET; T_ASSERT_NOTNULL(counts, "allocate counts");
533 T_SETUPEND;
534
535 // proc_pidinfo has a unique return value protocol: it returns the size
536 // that was copied out and 0 if an error occurs, with errno set.
537 int size = proc_pidinfo(getpid(), PROC_PIDTHREADCOUNTS, UINT64_MAX, counts,
538 counts_size);
539 T_ASSERT_EQ(size, 0,
540 "proc_pidinfo(..., PROC_PIDTHREADCOUNTS, UINT64_MAX, ...) should "
541 "fail");
542 T_ASSERT_EQ(errno, ESRCH, "should fail with ESRCH");
543 }
544