]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/task_info.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / tools / tests / darwintests / task_info.c
1 #include <mach/mach.h>
2 #include <mach/task_info.h>
3 #include <mach/thread_info.h>
4 #include <mach/mach_error.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <sys/mman.h>
10 #include <mach/policy.h>
11 #include <darwintest.h>
12 #include <sys/sysctl.h>
13 #include <darwintest_utils.h>
14
15 /* *************************************************************************************
16 * Test the task_info API.
17 *
18 * This is a functional test of the following APIs:
19 * TASK_BASIC_INFO_32
20 * TASK_BASIC2_INFO_32
21 * TASK_BASIC_INFO_64
22 * TASK_BASIC_INFO_64_2
23 * TASK_POWER_INFO_V2
24 * TASK_FLAGS_INFO
25 * TASK_AFFINITY_TAG_INFO
26 * TASK_THREAD_TIMES_INFO
27 * TASK_ABSOLUTE_TIME_INFO
28 * <rdar://problem/22242021> Add tests to increase code coverage for the task_info API
29 * *************************************************************************************
30 */
31 #define TESTPHYSFOOTPRINTVAL 5
32 #define CANARY 0x0f0f0f0f0f0f0f0fULL
33 #if !defined(CONFIG_EMBEDDED)
34 #define ABSOLUTE_MIN_USER_TIME_DIFF 150
35 #define ABSOLUTE_MIN_SYSTEM_TIME_DIFF 300
36 #endif
37
38 enum info_kind { INFO_32, INFO_64, INFO_32_2, INFO_64_2, INFO_MACH, INFO_MAX };
39
40 enum info_get { GET_SUSPEND_COUNT, GET_RESIDENT_SIZE, GET_VIRTUAL_SIZE, GET_USER_TIME, GET_SYS_TIME, GET_POLICY, GET_MAX_RES };
41
42 /*
43 * This function uses CPU cycles by doing a factorial computation.
44 */
45 static void do_factorial_task(void);
46
47 void test_task_basic_info_32(void);
48 void test_task_basic_info_64(void);
49 void task_basic_info_32_debug(void);
50 void task_basic2_info_32_warmup(void);
51 static int is_development_kernel(void);
52 void test_task_basic_info(enum info_kind kind);
53 uint64_t info_get(enum info_kind kind, enum info_get get, void * data);
54
55 T_DECL(task_vm_info, "tests task vm info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
56 {
57 kern_return_t err;
58 task_vm_info_data_t vm_info;
59
60 mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
61
62 err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
63
64 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
65
66 T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info return value !=0 for virtual_size\n");
67
68 T_EXPECT_NE(vm_info.phys_footprint, 0ULL, "task_info return value !=0 for phys_footprint\n");
69
70 /*
71 * Test the REV0 version of TASK_VM_INFO. It should not change the value of phys_footprint.
72 */
73
74 count = TASK_VM_INFO_REV0_COUNT;
75 vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
76 vm_info.min_address = CANARY;
77 vm_info.max_address = CANARY;
78
79 err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
80
81 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
82
83 T_EXPECT_EQ(count, TASK_VM_INFO_REV0_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV0_COUNT", count);
84
85 T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev0 call does not return 0 for virtual_size");
86
87 T_EXPECT_EQ(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
88 "task_info --rev0 call returned value %llu for vm_info.phys_footprint. Expected %u since this value should not be "
89 "modified by rev0",
90 vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
91
92 T_EXPECT_EQ(vm_info.min_address, CANARY,
93 "task_info --rev0 call returned value 0x%llx for vm_info.min_address. Expected 0x%llx since this value should not "
94 "be modified by rev0",
95 vm_info.min_address, CANARY);
96
97 T_EXPECT_EQ(vm_info.max_address, CANARY,
98 "task_info --rev0 call returned value 0x%llx for vm_info.max_address. Expected 0x%llx since this value should not "
99 "be modified by rev0",
100 vm_info.max_address, CANARY);
101
102 /*
103 * Test the REV1 version of TASK_VM_INFO.
104 */
105
106 count = TASK_VM_INFO_REV1_COUNT;
107 vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
108 vm_info.min_address = CANARY;
109 vm_info.max_address = CANARY;
110
111 err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
112
113 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
114
115 T_EXPECT_EQ(count, TASK_VM_INFO_REV1_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV1_COUNT", count);
116
117 T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev1 call does not return 0 for virtual_size");
118
119 T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
120 "task_info --rev1 call returned value %llu for vm_info.phys_footprint. Expected value is anything other than %u "
121 "since this value should not be modified by rev1",
122 vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
123
124 T_EXPECT_EQ(vm_info.min_address, CANARY,
125 "task_info --rev1 call returned value 0x%llx for vm_info.min_address. Expected 0x%llx since this value should not "
126 "be modified by rev1",
127 vm_info.min_address, CANARY);
128
129 T_EXPECT_EQ(vm_info.max_address, CANARY,
130 "task_info --rev1 call returned value 0x%llx for vm_info.max_address. Expected 0x%llx since this value should not "
131 "be modified by rev1",
132 vm_info.max_address, CANARY);
133
134 /*
135 * Test the REV2 version of TASK_VM_INFO.
136 */
137
138 count = TASK_VM_INFO_REV2_COUNT;
139 vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
140 vm_info.min_address = CANARY;
141 vm_info.max_address = CANARY;
142
143 err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
144
145 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
146
147 T_EXPECT_EQ(count, TASK_VM_INFO_REV2_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV2_COUNT\n", count);
148
149 T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev2 call does not return 0 for virtual_size\n");
150
151 T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
152 "task_info --rev2 call returned value %llu for vm_info.phys_footprint. Expected anything other than %u since this "
153 "value should be modified by rev2",
154 vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
155
156 T_EXPECT_NE(vm_info.min_address, CANARY,
157 "task_info --rev2 call returned value 0x%llx for vm_info.min_address. Expected anything other than 0x%llx since "
158 "this value should be modified by rev2",
159 vm_info.min_address, CANARY);
160
161 T_EXPECT_NE(vm_info.max_address, CANARY,
162 "task_info --rev2 call returned value 0x%llx for vm_info.max_address. Expected anything other than 0x%llx since "
163 "this value should be modified by rev2",
164 vm_info.max_address, CANARY);
165 }
166
167 T_DECL(host_debug_info, "tests host debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
168 {
169 T_SETUPBEGIN;
170 int is_dev = is_development_kernel();
171 T_QUIET;
172 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
173 T_SETUPEND;
174
175 kern_return_t err;
176 mach_port_t host;
177 host_debug_info_internal_data_t debug_info;
178 mach_msg_type_number_t count = HOST_DEBUG_INFO_INTERNAL_COUNT;
179 host = mach_host_self();
180 err = host_info(host, HOST_DEBUG_INFO_INTERNAL, (host_info_t)&debug_info, &count);
181
182 T_ASSERT_MACH_SUCCESS(err, "verify host_info call succeeded");
183 }
184
185 T_DECL(task_debug_info, "tests task debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
186 {
187 T_SETUPBEGIN;
188 int is_dev = is_development_kernel();
189 T_QUIET;
190 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
191 T_SETUPEND;
192
193 kern_return_t err;
194 task_debug_info_internal_data_t debug_info;
195
196 mach_msg_type_number_t count = TASK_DEBUG_INFO_INTERNAL_COUNT;
197
198 err = task_info(mach_task_self(), TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &count);
199
200 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
201 }
202
203 T_DECL(thread_debug_info, "tests thread debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
204 {
205 T_SETUPBEGIN;
206 int is_dev = is_development_kernel();
207 T_QUIET;
208 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
209 T_SETUPEND;
210
211 kern_return_t err;
212 thread_debug_info_internal_data_t debug_info;
213
214 mach_msg_type_number_t count = THREAD_DEBUG_INFO_INTERNAL_COUNT;
215
216 err = thread_info(mach_thread_self(), THREAD_DEBUG_INFO_INTERNAL, (thread_info_t)&debug_info, &count);
217
218 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
219 }
220
221 static void
222 do_factorial_task()
223 {
224 int number = 20;
225 int factorial = 1;
226 int i;
227 for (i = 1; i <= number; i++) {
228 factorial *= i;
229 }
230
231 return;
232 }
233
234 T_DECL(task_thread_times_info, "tests task thread times info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
235 {
236 T_SETUPBEGIN;
237 int is_dev = is_development_kernel();
238 T_QUIET;
239 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
240 T_SETUPEND;
241
242 kern_return_t err;
243 task_thread_times_info_data_t thread_times_info_data;
244 task_thread_times_info_data_t thread_times_info_data_new;
245 mach_msg_type_number_t count = TASK_THREAD_TIMES_INFO_COUNT;
246
247 err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data, &count);
248
249 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
250
251 do_factorial_task();
252
253 err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data_new, &count);
254
255 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
256
257 /*
258 * The difference is observed to be less than 30 microseconds for user_time
259 * and less than 50 microseconds for system_time. This observation was done for over
260 * 1000 runs.
261 */
262
263 T_EXPECT_FALSE((thread_times_info_data_new.user_time.seconds - thread_times_info_data.user_time.seconds) != 0 ||
264 (thread_times_info_data_new.system_time.seconds - thread_times_info_data.system_time.seconds) != 0,
265 "Tests whether the difference between thread times is greater than the allowed limit");
266
267 /*
268 * This is a negative case.
269 */
270
271 count--;
272 err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data, &count);
273 T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
274 "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
275 }
276
277 T_DECL(task_absolutetime_info, "tests task absolute time info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
278 {
279 T_SETUPBEGIN;
280 int is_dev = is_development_kernel();
281 T_QUIET;
282 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
283 T_SETUPEND;
284
285 kern_return_t err;
286 uint64_t user_time_diff, system_time_diff;
287 task_absolutetime_info_data_t absolute_time_info_data;
288 task_absolutetime_info_data_t absolute_time_info_data_new;
289 mach_msg_type_number_t count = TASK_ABSOLUTETIME_INFO_COUNT;
290
291 err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data, &count);
292
293 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
294
295 do_factorial_task();
296
297 err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data_new, &count);
298
299 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
300
301 user_time_diff = absolute_time_info_data_new.total_user - absolute_time_info_data.total_user;
302 system_time_diff = absolute_time_info_data_new.total_system - absolute_time_info_data.total_system;
303
304 #if !(defined(__arm__) || defined(__arm64__))
305 /*
306 * On embedded devices the difference is always zero.
307 * On non-embedded devices the difference occurs in this range. This was observed over ~10000 runs.
308 */
309
310 T_EXPECT_FALSE(user_time_diff < ABSOLUTE_MIN_USER_TIME_DIFF || system_time_diff < ABSOLUTE_MIN_SYSTEM_TIME_DIFF,
311 "Tests whether the difference between thread times is greater than the expected range");
312 #endif
313
314 /*
315 * There is no way of estimating the exact number of threads, hence checking the counter to be non-zero for now.
316 */
317
318 T_EXPECT_NE(absolute_time_info_data.threads_user, 0ULL, "task_info should return non-zero number of user threads");
319
320 #if !(defined(__arm__) || defined(__arm64__))
321 /*
322 * On iOS, system threads are always zero. On OS X this value can be some large positive number.
323 * There is no real way to estimate the exact amount.
324 */
325 T_EXPECT_NE(absolute_time_info_data.threads_system, 0ULL, "task_info should return non-zero number of system threads");
326 #endif
327
328 /*
329 * This is a negative case.
330 */
331 count--;
332 err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data_new, &count);
333 T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
334 "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
335 }
336
337 T_DECL(task_affinity_tag_info, "tests task_affinity_tag_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
338 {
339 T_SETUPBEGIN;
340 int is_dev = is_development_kernel();
341 T_QUIET;
342 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
343 T_SETUPEND;
344
345 kern_return_t err;
346 task_affinity_tag_info_data_t affinity_tag_info_data;
347 mach_msg_type_number_t count = TASK_AFFINITY_TAG_INFO_COUNT;
348
349 err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count);
350
351 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
352
353 /*
354 * The affinity is not set by default, hence expecting a zero value.
355 */
356 T_ASSERT_FALSE(affinity_tag_info_data.min != 0 || affinity_tag_info_data.max != 0,
357 "task_info call returns non-zero min or max value");
358
359 /*
360 * This is a negative case.
361 */
362 count--;
363 err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count);
364 T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
365 "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
366 }
367
368 T_DECL(task_flags_info, "tests task_flags_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
369 {
370 T_SETUPBEGIN;
371 int is_dev = is_development_kernel();
372 T_QUIET;
373 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
374 T_SETUPEND;
375
376 kern_return_t err;
377 task_flags_info_data_t flags_info_data;
378 mach_msg_type_number_t count = TASK_FLAGS_INFO_COUNT;
379
380 err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count);
381
382 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
383
384 /* Change for 32-bit arch possibility?*/
385 T_ASSERT_EQ((flags_info_data.flags & (unsigned int)(~TF_LP64)), 0U, "task_info should only give out 64-bit addr flag");
386
387 /*
388 * This is a negative case.
389 */
390
391 count--;
392 err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count);
393 T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
394 "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
395 }
396
397 T_DECL(task_power_info_v2, "tests task_power_info_v2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
398 {
399 T_SETUPBEGIN;
400 int is_dev = is_development_kernel();
401 T_QUIET;
402 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
403 T_SETUPEND;
404
405 kern_return_t err;
406 task_power_info_v2_data_t power_info_data_v2;
407 task_power_info_v2_data_t power_info_data_v2_new;
408 mach_msg_type_number_t count = TASK_POWER_INFO_V2_COUNT;
409
410 sleep(1);
411
412 err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
413
414 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
415
416 T_ASSERT_LE(power_info_data_v2.gpu_energy.task_gpu_utilisation, 0ULL,
417 "verified task_info call shows zero GPU utilization for non-GPU task");
418
419 do_factorial_task();
420
421 /*
422 * Verify the cpu_energy parameters.
423 */
424 err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2_new, &count);
425 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
426
427 #if !(defined(__arm__) || defined(__arm64__))
428 /*
429 * iOS does not have system_time.
430 */
431 T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_user, power_info_data_v2.cpu_energy.total_user,
432 "task_info call returns valid user time");
433 T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_system, power_info_data_v2.cpu_energy.total_system,
434 "task_info call returns valid system time");
435 #endif
436
437 T_ASSERT_GE(power_info_data_v2.cpu_energy.task_interrupt_wakeups, 1ULL,
438 "verify task_info call returns non-zero value for interrupt_wakeup (ret value = %llu)",
439 power_info_data_v2.cpu_energy.task_interrupt_wakeups);
440
441 #if !(defined(__arm__) || defined(__arm64__))
442 if (power_info_data_v2.cpu_energy.task_platform_idle_wakeups != 0) {
443 T_LOG("task_info call returned %llu for platform_idle_wakeup", power_info_data_v2.cpu_energy.task_platform_idle_wakeups);
444 }
445 #endif
446
447 count = TASK_POWER_INFO_V2_COUNT_OLD;
448 err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
449
450 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
451
452 /*
453 * This is a negative case.
454 */
455 count--;
456 err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
457
458 T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
459 "Negative test case: task_info should verify that count is at least equal to what is defined in API. Call "
460 "returns errno %d:%s",
461 err, mach_error_string(err));
462 }
463
464 T_DECL(test_task_basic_info_32, "tests TASK_BASIC_INFO_32", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
465 {
466 test_task_basic_info(INFO_32);
467 }
468
469 T_DECL(test_task_basic_info_32_2, "tests TASK_BASIC_INFO_32_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
470 {
471 test_task_basic_info(INFO_32_2);
472 }
473
474 #if defined(__arm__) || defined(__arm64__)
475 T_DECL(test_task_basic_info_64i_2, "tests TASK_BASIC_INFO_64_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
476 {
477 test_task_basic_info(INFO_64_2);
478 }
479 #else
480 T_DECL(test_task_basic_info_64, "tests TASK_BASIC_INFO_64", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
481 {
482 test_task_basic_info(INFO_64);
483 }
484 #endif /* defined(__arm__) || defined(__arm64__) */
485
486 T_DECL(test_mach_task_basic_info, "tests MACH_TASK_BASIC_INFO", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
487 {
488 test_task_basic_info(INFO_MACH);
489 }
490
491 void
492 test_task_basic_info(enum info_kind kind)
493 {
494 #define BEFORE 0
495 #define AFTER 1
496
497 T_SETUPBEGIN;
498 int is_dev = is_development_kernel();
499 T_QUIET;
500 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
501 T_SETUPEND;
502
503 task_info_t info_data[2];
504 task_basic_info_32_data_t basic_info_32_data[2];
505 #if defined(__arm__) || defined(__arm64__)
506 task_basic_info_64_2_data_t basic_info_64_2_data[2];
507 #else
508 task_basic_info_64_data_t basic_info_64_data[2];
509 #endif /* defined(__arm__) || defined(__arm64__) */
510 mach_task_basic_info_data_t mach_basic_info_data[2];
511
512 kern_return_t kr;
513 mach_msg_type_number_t count;
514 task_flavor_t flavor = 0;
515 integer_t suspend_count;
516 uint64_t resident_size_diff;
517 uint64_t virtual_size_diff;
518
519 void * tmp_map = NULL;
520 pid_t child_pid;
521 mach_port_name_t child_task;
522 /*for dt_waitpid*/
523 int timeout = 10; // change to max timeout
524 int exit_status = 0;
525
526 switch (kind) {
527 case INFO_32:
528 case INFO_32_2:
529 info_data[BEFORE] = (task_info_t)&basic_info_32_data[BEFORE];
530 info_data[AFTER] = (task_info_t)&basic_info_32_data[AFTER];
531 count = TASK_BASIC_INFO_32_COUNT;
532 flavor = TASK_BASIC_INFO_32;
533
534 if (kind == INFO_32_2) {
535 flavor = TASK_BASIC2_INFO_32;
536 }
537
538 break;
539 #if defined(__arm__) || defined(__arm64__)
540 case INFO_64:
541 T_ASSERT_FAIL("invalid basic info kind");
542 break;
543
544 case INFO_64_2:
545 info_data[BEFORE] = (task_info_t)&basic_info_64_2_data[BEFORE];
546 info_data[AFTER] = (task_info_t)&basic_info_64_2_data[AFTER];
547 count = TASK_BASIC_INFO_64_2_COUNT;
548 flavor = TASK_BASIC_INFO_64_2;
549 break;
550
551 #else
552 case INFO_64:
553 info_data[BEFORE] = (task_info_t)&basic_info_64_data[BEFORE];
554 info_data[AFTER] = (task_info_t)&basic_info_64_data[AFTER];
555 count = TASK_BASIC_INFO_64_COUNT;
556 flavor = TASK_BASIC_INFO_64;
557 break;
558
559 case INFO_64_2:
560 T_ASSERT_FAIL("invalid basic info kind");
561 break;
562 #endif /* defined(__arm__) || defined(__arm64__) */
563 case INFO_MACH:
564 info_data[BEFORE] = (task_info_t)&mach_basic_info_data[BEFORE];
565 info_data[AFTER] = (task_info_t)&mach_basic_info_data[AFTER];
566 count = MACH_TASK_BASIC_INFO_COUNT;
567 flavor = MACH_TASK_BASIC_INFO;
568 break;
569 case INFO_MAX:
570 default:
571 T_ASSERT_FAIL("invalid basic info kind");
572 break;
573 }
574
575 kr = task_info(mach_task_self(), flavor, info_data[BEFORE], &count);
576
577 T_ASSERT_MACH_SUCCESS(kr, "verify task_info succeeded");
578
579 do_factorial_task();
580
581 /*
582 * Allocate virtual and resident memory.
583 */
584 tmp_map = mmap(0, PAGE_SIZE, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
585
586 T_WITH_ERRNO;
587 T_EXPECT_NE(tmp_map, MAP_FAILED, "verify mmap call is successful");
588
589 memset(tmp_map, 'm', PAGE_SIZE);
590
591 child_pid = fork();
592
593 T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked");
594
595 if (child_pid == 0) {
596 /*
597 * This will suspend the child process.
598 */
599 kr = task_suspend(mach_task_self());
600 exit(kr);
601 }
602
603 /*
604 * Wait for the child process to suspend itself.
605 */
606 sleep(1);
607
608 kr = task_for_pid(mach_task_self(), child_pid, &child_task);
609 T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded. check sudo if failed");
610
611 /*
612 * Verify the suspend_count for child and resume it.
613 */
614
615 kr = task_info(child_task, flavor, info_data[AFTER], &count);
616 T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
617
618 suspend_count = (integer_t)(info_get(kind, GET_SUSPEND_COUNT, info_data[AFTER]));
619 T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct suspend_count");
620
621 kr = task_resume(child_task);
622 T_ASSERT_MACH_SUCCESS(kr, "verify task_resume succeeded");
623
624 /*
625 * reap kr from task_suspend call in child
626 */
627 if (dt_waitpid(child_pid, &exit_status, NULL, timeout)) {
628 T_ASSERT_MACH_SUCCESS(exit_status, "verify child task_suspend is successful");
629 } else {
630 T_FAIL("dt_waitpid failed");
631 }
632
633 kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count);
634 T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
635
636 resident_size_diff = info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]) - info_get(kind, GET_RESIDENT_SIZE, info_data[BEFORE]);
637 virtual_size_diff = info_get(kind, GET_VIRTUAL_SIZE, info_data[AFTER]) - info_get(kind, GET_VIRTUAL_SIZE, info_data[BEFORE]);
638
639 /*
640 * INFO_32_2 gets the max resident size instead of the current resident size
641 * 32 KB tolerance built into test. The returned value is generally between 0 and 16384
642 *
643 * max resident size is a discrete field in INFO_MACH, so it's handled differently
644 */
645 if (kind == INFO_32_2) {
646 T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size");
647 T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size");
648 T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "verify task_info returns valid virtual_size");
649 } else {
650 T_EXPECT_GE(resident_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid resident_size");
651 T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid virtual_size");
652 }
653
654 if (kind == INFO_MACH) {
655 resident_size_diff = info_get(kind, GET_MAX_RES, info_data[AFTER]) - info_get(kind, GET_MAX_RES, info_data[BEFORE]);
656 T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size");
657 T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size");
658 T_EXPECT_GE(info_get(kind, GET_MAX_RES, info_data[AFTER]), info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]),
659 "verify max resident size is greater than or equal to curr resident size");
660 }
661
662 do_factorial_task();
663
664 /*
665 * These counters give time for threads that have terminated. We dont have any, so checking for zero.
666 */
667
668 time_value_t * user_tv = (time_value_t *)(info_get(kind, GET_USER_TIME, info_data[BEFORE]));
669 T_EXPECT_EQ((user_tv->seconds + user_tv->microseconds / 1000000), 0, "verify task_info shows valid user time");
670
671 time_value_t * sys_tv = (time_value_t *)(info_get(kind, GET_SYS_TIME, info_data[BEFORE]));
672 T_EXPECT_EQ(sys_tv->seconds + (sys_tv->microseconds / 1000000), 0, "verify task_info shows valid system time");
673
674 /*
675 * The default value for non-kernel tasks is TIMESHARE.
676 */
677
678 policy_t pt = (policy_t)info_get(kind, GET_POLICY, info_data[BEFORE]);
679
680 T_EXPECT_EQ(pt, POLICY_TIMESHARE, "verify task_info shows valid policy");
681
682 /*
683 * This is a negative case.
684 */
685
686 count--;
687 kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count);
688
689 T_ASSERT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT,
690 "Negative test case: task_info should verify that count is at least equal to what is defined in API");
691
692 /*
693 * deallocate memory
694 */
695 munmap(tmp_map, PAGE_SIZE);
696
697 return;
698
699 #undef BEFORE
700 #undef AFTER
701 }
702
703 uint64_t
704 info_get(enum info_kind kind, enum info_get get, void * data)
705 {
706 switch (get) {
707 case GET_SUSPEND_COUNT:
708 switch (kind) {
709 case INFO_32:
710 case INFO_32_2:
711 return (uint64_t)(((task_basic_info_32_t)data)->suspend_count);
712 #if defined(__arm__) || defined(__arm64__)
713 case INFO_64:
714 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
715 break;
716
717 case INFO_64_2:
718 return (uint64_t)(((task_basic_info_64_2_t)data)->suspend_count);
719 #else
720 case INFO_64:
721 return (uint64_t)(((task_basic_info_64_t)data)->suspend_count);
722
723 case INFO_64_2:
724 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
725 break;
726 #endif /* defined(__arm__) || defined(__arm64__) */
727 case INFO_MACH:
728 return (uint64_t)(((mach_task_basic_info_t)data)->suspend_count);
729 case INFO_MAX:
730 default:
731 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
732 }
733 case GET_RESIDENT_SIZE:
734 switch (kind) {
735 case INFO_32:
736 case INFO_32_2:
737 return (uint64_t)(((task_basic_info_32_t)data)->resident_size);
738 #if defined(__arm__) || defined(__arm64__)
739 case INFO_64:
740 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
741 break;
742
743 case INFO_64_2:
744 return (uint64_t)(((task_basic_info_64_2_t)data)->resident_size);
745 #else
746 case INFO_64:
747 return (uint64_t)(((task_basic_info_64_t)data)->resident_size);
748
749 case INFO_64_2:
750 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
751 break;
752 #endif /* defined(__arm__) || defined(__arm64__) */
753 case INFO_MACH:
754 return (uint64_t)(((mach_task_basic_info_t)data)->resident_size);
755 case INFO_MAX:
756 default:
757 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
758 }
759 case GET_VIRTUAL_SIZE:
760 switch (kind) {
761 case INFO_32:
762 case INFO_32_2:
763 return (uint64_t)(((task_basic_info_32_t)data)->virtual_size);
764 #if defined(__arm__) || defined(__arm64__)
765 case INFO_64:
766 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
767 break;
768
769 case INFO_64_2:
770 return (uint64_t)(((task_basic_info_64_2_t)data)->virtual_size);
771 #else
772 case INFO_64:
773 return (uint64_t)(((task_basic_info_64_t)data)->virtual_size);
774
775 case INFO_64_2:
776 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
777 break;
778 #endif /* defined(__arm__) || defined(__arm64__) */
779 case INFO_MACH:
780 return (uint64_t)(((mach_task_basic_info_t)data)->virtual_size);
781
782 case INFO_MAX:
783 default:
784 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
785 }
786 case GET_USER_TIME:
787 switch (kind) {
788 case INFO_32:
789 case INFO_32_2:
790 return (uint64_t) & (((task_basic_info_32_t)data)->user_time);
791 #if defined(__arm__) || defined(__arm64__)
792 case INFO_64:
793 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
794 break;
795
796 case INFO_64_2:
797 return (uint64_t) & (((task_basic_info_64_2_t)data)->user_time);
798 #else
799 case INFO_64:
800 return (uint64_t) & (((task_basic_info_64_t)data)->user_time);
801
802 case INFO_64_2:
803 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
804 break;
805 #endif /* defined(__arm__) || defined(__arm64__) */
806 case INFO_MACH:
807 return (uint64_t) & (((mach_task_basic_info_t)data)->user_time);
808
809 case INFO_MAX:
810 default:
811 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
812 }
813 case GET_SYS_TIME:
814 switch (kind) {
815 case INFO_32:
816 case INFO_32_2:
817 return (uint64_t) & (((task_basic_info_32_t)data)->system_time);
818 #if defined(__arm__) || defined(__arm64__)
819 case INFO_64:
820 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
821 break;
822
823 case INFO_64_2:
824 return (uint64_t) & (((task_basic_info_64_2_t)data)->system_time);
825 #else
826 case INFO_64:
827 return (uint64_t) & (((task_basic_info_64_t)data)->system_time);
828
829 case INFO_64_2:
830 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
831 break;
832 #endif /* defined(__arm__) || defined(__arm64__) */
833 case INFO_MACH:
834 return (uint64_t) & (((mach_task_basic_info_t)data)->user_time);
835 case INFO_MAX:
836 default:
837 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
838 }
839 case GET_POLICY:
840 switch (kind) {
841 case INFO_32:
842 case INFO_32_2:
843 return (uint64_t)(((task_basic_info_32_t)data)->policy);
844 #if defined(__arm__) || defined(__arm64__)
845 case INFO_64:
846 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
847 break;
848
849 case INFO_64_2:
850 return (uint64_t)(((task_basic_info_64_2_t)data)->policy);
851 #else
852 case INFO_64:
853 return (uint64_t)(((task_basic_info_64_t)data)->policy);
854
855 case INFO_64_2:
856 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
857 break;
858 #endif /* defined(__arm__) || defined(__arm64__) */
859 case INFO_MACH:
860 return (uint64_t)(((mach_task_basic_info_t)data)->policy);
861
862 case INFO_MAX:
863 default:
864 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
865 }
866 case GET_MAX_RES:
867 switch (kind) {
868 case INFO_32:
869 case INFO_32_2:
870 case INFO_64:
871 case INFO_64_2:
872 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
873 case INFO_MACH:
874 return (uint64_t)(((mach_task_basic_info_t)data)->resident_size_max);
875 case INFO_MAX:
876 default:
877 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
878 }
879 }
880
881 __builtin_unreachable();
882 }
883
884 /*
885 * Determines whether we're running on a development kernel
886 */
887 static int
888 is_development_kernel(void)
889 {
890 #define NOTSET -1
891
892 static int is_dev = NOTSET;
893
894 if (is_dev == NOTSET) {
895 int dev;
896 size_t dev_size = sizeof(dev);
897
898 T_QUIET;
899 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.development", &dev, &dev_size, NULL, 0), NULL);
900 is_dev = (dev != 0);
901
902 return is_dev;
903 } else {
904 return is_dev;
905 }
906 #undef NOTSET
907 }