]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/darwintests/task_info.c
xnu-4570.41.2.tar.gz
[apple/xnu.git] / tools / tests / darwintests / task_info.c
CommitLineData
cc8bc92a
A
1#include <darwintest.h>
2#include <darwintest_utils.h>
3#include <errno.h>
5ba3f43e 4#include <mach/mach.h>
cc8bc92a
A
5#include <mach/mach_error.h>
6#include <mach/policy.h>
5ba3f43e
A
7#include <mach/task_info.h>
8#include <mach/thread_info.h>
5ba3f43e 9#include <stdio.h>
5ba3f43e
A
10#include <stdlib.h>
11#include <sys/mman.h>
5ba3f43e 12#include <sys/sysctl.h>
cc8bc92a 13#include <unistd.h>
5ba3f43e
A
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
38enum info_kind { INFO_32, INFO_64, INFO_32_2, INFO_64_2, INFO_MACH, INFO_MAX };
39
40enum 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 */
45static void do_factorial_task(void);
46
47void test_task_basic_info_32(void);
48void test_task_basic_info_64(void);
49void task_basic_info_32_debug(void);
50void task_basic2_info_32_warmup(void);
51static int is_development_kernel(void);
52void test_task_basic_info(enum info_kind kind);
53uint64_t info_get(enum info_kind kind, enum info_get get, void * data);
54
55T_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
167T_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
185T_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
203T_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
221static void
222do_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
234T_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
277T_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
cc8bc92a
A
314 if (absolute_time_info_data.threads_user <= 0) {
315 int precise_time_val = 0;
316 size_t len = sizeof(size_t);
317
318 T_LOG("User threads time is zero. This should only happen rarely and when precise_user_time is off");
5ba3f43e 319
cc8bc92a
A
320 err = sysctlbyname("kern.precise_user_kernel_time", &precise_time_val, &len, NULL, 0);
321
322 T_EXPECT_POSIX_SUCCESS(err, "performing sysctl to check precise_user_time");
323
324 T_LOG("kern.precise_user_kernel_time val = %d", precise_time_val);
325
326 T_EXPECT_FALSE(precise_time_val, "user thread time should only be zero when precise_user_kernel_time is disabled");
327 } else {
328 T_PASS("task_info should return non-zero value for user threads time = %llu", absolute_time_info_data.threads_user);
329 }
5ba3f43e
A
330
331#if !(defined(__arm__) || defined(__arm64__))
332 /*
333 * On iOS, system threads are always zero. On OS X this value can be some large positive number.
334 * There is no real way to estimate the exact amount.
335 */
cc8bc92a
A
336 T_EXPECT_NE(absolute_time_info_data.threads_system, 0ULL,
337 "task_info should return non-zero value for system threads time = %llu", absolute_time_info_data.threads_system);
5ba3f43e
A
338#endif
339
340 /*
341 * This is a negative case.
342 */
343 count--;
344 err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data_new, &count);
345 T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
346 "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
347}
348
349T_DECL(task_affinity_tag_info, "tests task_affinity_tag_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
350{
351 T_SETUPBEGIN;
352 int is_dev = is_development_kernel();
353 T_QUIET;
354 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
355 T_SETUPEND;
356
357 kern_return_t err;
358 task_affinity_tag_info_data_t affinity_tag_info_data;
359 mach_msg_type_number_t count = TASK_AFFINITY_TAG_INFO_COUNT;
360
361 err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count);
362
363 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
364
365 /*
366 * The affinity is not set by default, hence expecting a zero value.
367 */
368 T_ASSERT_FALSE(affinity_tag_info_data.min != 0 || affinity_tag_info_data.max != 0,
369 "task_info call returns non-zero min or max value");
370
371 /*
372 * This is a negative case.
373 */
374 count--;
375 err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count);
376 T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
377 "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
378}
379
380T_DECL(task_flags_info, "tests task_flags_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
381{
382 T_SETUPBEGIN;
383 int is_dev = is_development_kernel();
384 T_QUIET;
385 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
386 T_SETUPEND;
387
388 kern_return_t err;
389 task_flags_info_data_t flags_info_data;
390 mach_msg_type_number_t count = TASK_FLAGS_INFO_COUNT;
391
392 err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count);
393
394 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
395
396 /* Change for 32-bit arch possibility?*/
397 T_ASSERT_EQ((flags_info_data.flags & (unsigned int)(~TF_LP64)), 0U, "task_info should only give out 64-bit addr flag");
398
399 /*
400 * This is a negative case.
401 */
402
403 count--;
404 err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count);
405 T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
406 "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
407}
408
409T_DECL(task_power_info_v2, "tests task_power_info_v2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
410{
411 T_SETUPBEGIN;
412 int is_dev = is_development_kernel();
413 T_QUIET;
414 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
415 T_SETUPEND;
416
417 kern_return_t err;
418 task_power_info_v2_data_t power_info_data_v2;
419 task_power_info_v2_data_t power_info_data_v2_new;
420 mach_msg_type_number_t count = TASK_POWER_INFO_V2_COUNT;
421
422 sleep(1);
423
424 err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
425
426 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
427
428 T_ASSERT_LE(power_info_data_v2.gpu_energy.task_gpu_utilisation, 0ULL,
429 "verified task_info call shows zero GPU utilization for non-GPU task");
430
431 do_factorial_task();
432
433 /*
434 * Verify the cpu_energy parameters.
435 */
436 err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2_new, &count);
437 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
438
439#if !(defined(__arm__) || defined(__arm64__))
440 /*
441 * iOS does not have system_time.
442 */
443 T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_user, power_info_data_v2.cpu_energy.total_user,
444 "task_info call returns valid user time");
445 T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_system, power_info_data_v2.cpu_energy.total_system,
446 "task_info call returns valid system time");
447#endif
448
449 T_ASSERT_GE(power_info_data_v2.cpu_energy.task_interrupt_wakeups, 1ULL,
450 "verify task_info call returns non-zero value for interrupt_wakeup (ret value = %llu)",
451 power_info_data_v2.cpu_energy.task_interrupt_wakeups);
452
453#if !(defined(__arm__) || defined(__arm64__))
454 if (power_info_data_v2.cpu_energy.task_platform_idle_wakeups != 0) {
455 T_LOG("task_info call returned %llu for platform_idle_wakeup", power_info_data_v2.cpu_energy.task_platform_idle_wakeups);
456 }
457#endif
458
459 count = TASK_POWER_INFO_V2_COUNT_OLD;
460 err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
461
462 T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
463
464 /*
465 * This is a negative case.
466 */
467 count--;
468 err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
469
470 T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
471 "Negative test case: task_info should verify that count is at least equal to what is defined in API. Call "
472 "returns errno %d:%s",
473 err, mach_error_string(err));
474}
475
476T_DECL(test_task_basic_info_32, "tests TASK_BASIC_INFO_32", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
477{
478 test_task_basic_info(INFO_32);
479}
480
481T_DECL(test_task_basic_info_32_2, "tests TASK_BASIC_INFO_32_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
482{
483 test_task_basic_info(INFO_32_2);
484}
485
486#if defined(__arm__) || defined(__arm64__)
487T_DECL(test_task_basic_info_64i_2, "tests TASK_BASIC_INFO_64_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
488{
489 test_task_basic_info(INFO_64_2);
490}
491#else
492T_DECL(test_task_basic_info_64, "tests TASK_BASIC_INFO_64", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
493{
494 test_task_basic_info(INFO_64);
495}
496#endif /* defined(__arm__) || defined(__arm64__) */
497
498T_DECL(test_mach_task_basic_info, "tests MACH_TASK_BASIC_INFO", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
499{
500 test_task_basic_info(INFO_MACH);
501}
502
503void
504test_task_basic_info(enum info_kind kind)
505{
506#define BEFORE 0
507#define AFTER 1
508
509 T_SETUPBEGIN;
510 int is_dev = is_development_kernel();
511 T_QUIET;
512 T_ASSERT_TRUE(is_dev, "verify development kernel is running");
513 T_SETUPEND;
514
515 task_info_t info_data[2];
516 task_basic_info_32_data_t basic_info_32_data[2];
517#if defined(__arm__) || defined(__arm64__)
518 task_basic_info_64_2_data_t basic_info_64_2_data[2];
519#else
520 task_basic_info_64_data_t basic_info_64_data[2];
521#endif /* defined(__arm__) || defined(__arm64__) */
522 mach_task_basic_info_data_t mach_basic_info_data[2];
523
524 kern_return_t kr;
525 mach_msg_type_number_t count;
526 task_flavor_t flavor = 0;
527 integer_t suspend_count;
528 uint64_t resident_size_diff;
529 uint64_t virtual_size_diff;
530
531 void * tmp_map = NULL;
532 pid_t child_pid;
533 mach_port_name_t child_task;
534 /*for dt_waitpid*/
535 int timeout = 10; // change to max timeout
536 int exit_status = 0;
537
538 switch (kind) {
539 case INFO_32:
540 case INFO_32_2:
541 info_data[BEFORE] = (task_info_t)&basic_info_32_data[BEFORE];
542 info_data[AFTER] = (task_info_t)&basic_info_32_data[AFTER];
543 count = TASK_BASIC_INFO_32_COUNT;
544 flavor = TASK_BASIC_INFO_32;
545
546 if (kind == INFO_32_2) {
547 flavor = TASK_BASIC2_INFO_32;
548 }
549
550 break;
551#if defined(__arm__) || defined(__arm64__)
552 case INFO_64:
553 T_ASSERT_FAIL("invalid basic info kind");
554 break;
555
556 case INFO_64_2:
557 info_data[BEFORE] = (task_info_t)&basic_info_64_2_data[BEFORE];
558 info_data[AFTER] = (task_info_t)&basic_info_64_2_data[AFTER];
559 count = TASK_BASIC_INFO_64_2_COUNT;
560 flavor = TASK_BASIC_INFO_64_2;
561 break;
562
563#else
564 case INFO_64:
565 info_data[BEFORE] = (task_info_t)&basic_info_64_data[BEFORE];
566 info_data[AFTER] = (task_info_t)&basic_info_64_data[AFTER];
567 count = TASK_BASIC_INFO_64_COUNT;
568 flavor = TASK_BASIC_INFO_64;
569 break;
570
571 case INFO_64_2:
572 T_ASSERT_FAIL("invalid basic info kind");
573 break;
574#endif /* defined(__arm__) || defined(__arm64__) */
575 case INFO_MACH:
576 info_data[BEFORE] = (task_info_t)&mach_basic_info_data[BEFORE];
577 info_data[AFTER] = (task_info_t)&mach_basic_info_data[AFTER];
578 count = MACH_TASK_BASIC_INFO_COUNT;
579 flavor = MACH_TASK_BASIC_INFO;
580 break;
581 case INFO_MAX:
582 default:
583 T_ASSERT_FAIL("invalid basic info kind");
584 break;
585 }
586
587 kr = task_info(mach_task_self(), flavor, info_data[BEFORE], &count);
588
589 T_ASSERT_MACH_SUCCESS(kr, "verify task_info succeeded");
590
591 do_factorial_task();
592
593 /*
594 * Allocate virtual and resident memory.
595 */
596 tmp_map = mmap(0, PAGE_SIZE, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
597
598 T_WITH_ERRNO;
599 T_EXPECT_NE(tmp_map, MAP_FAILED, "verify mmap call is successful");
600
601 memset(tmp_map, 'm', PAGE_SIZE);
602
603 child_pid = fork();
604
605 T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked");
606
607 if (child_pid == 0) {
608 /*
609 * This will suspend the child process.
610 */
611 kr = task_suspend(mach_task_self());
612 exit(kr);
613 }
614
615 /*
616 * Wait for the child process to suspend itself.
617 */
618 sleep(1);
619
620 kr = task_for_pid(mach_task_self(), child_pid, &child_task);
621 T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded. check sudo if failed");
622
623 /*
624 * Verify the suspend_count for child and resume it.
625 */
626
627 kr = task_info(child_task, flavor, info_data[AFTER], &count);
628 T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
629
630 suspend_count = (integer_t)(info_get(kind, GET_SUSPEND_COUNT, info_data[AFTER]));
631 T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct suspend_count");
632
633 kr = task_resume(child_task);
634 T_ASSERT_MACH_SUCCESS(kr, "verify task_resume succeeded");
635
636 /*
637 * reap kr from task_suspend call in child
638 */
639 if (dt_waitpid(child_pid, &exit_status, NULL, timeout)) {
640 T_ASSERT_MACH_SUCCESS(exit_status, "verify child task_suspend is successful");
641 } else {
642 T_FAIL("dt_waitpid failed");
643 }
644
645 kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count);
646 T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
647
648 resident_size_diff = info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]) - info_get(kind, GET_RESIDENT_SIZE, info_data[BEFORE]);
649 virtual_size_diff = info_get(kind, GET_VIRTUAL_SIZE, info_data[AFTER]) - info_get(kind, GET_VIRTUAL_SIZE, info_data[BEFORE]);
650
651 /*
652 * INFO_32_2 gets the max resident size instead of the current resident size
653 * 32 KB tolerance built into test. The returned value is generally between 0 and 16384
654 *
655 * max resident size is a discrete field in INFO_MACH, so it's handled differently
656 */
657 if (kind == INFO_32_2) {
658 T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size");
659 T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size");
660 T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "verify task_info returns valid virtual_size");
661 } else {
662 T_EXPECT_GE(resident_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid resident_size");
663 T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid virtual_size");
664 }
665
666 if (kind == INFO_MACH) {
667 resident_size_diff = info_get(kind, GET_MAX_RES, info_data[AFTER]) - info_get(kind, GET_MAX_RES, info_data[BEFORE]);
668 T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size");
669 T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size");
670 T_EXPECT_GE(info_get(kind, GET_MAX_RES, info_data[AFTER]), info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]),
671 "verify max resident size is greater than or equal to curr resident size");
672 }
673
674 do_factorial_task();
675
676 /*
677 * These counters give time for threads that have terminated. We dont have any, so checking for zero.
678 */
679
680 time_value_t * user_tv = (time_value_t *)(info_get(kind, GET_USER_TIME, info_data[BEFORE]));
681 T_EXPECT_EQ((user_tv->seconds + user_tv->microseconds / 1000000), 0, "verify task_info shows valid user time");
682
683 time_value_t * sys_tv = (time_value_t *)(info_get(kind, GET_SYS_TIME, info_data[BEFORE]));
684 T_EXPECT_EQ(sys_tv->seconds + (sys_tv->microseconds / 1000000), 0, "verify task_info shows valid system time");
685
686 /*
687 * The default value for non-kernel tasks is TIMESHARE.
688 */
689
690 policy_t pt = (policy_t)info_get(kind, GET_POLICY, info_data[BEFORE]);
691
692 T_EXPECT_EQ(pt, POLICY_TIMESHARE, "verify task_info shows valid policy");
693
694 /*
695 * This is a negative case.
696 */
697
698 count--;
699 kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count);
700
701 T_ASSERT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT,
702 "Negative test case: task_info should verify that count is at least equal to what is defined in API");
703
704 /*
705 * deallocate memory
706 */
707 munmap(tmp_map, PAGE_SIZE);
708
709 return;
710
711#undef BEFORE
712#undef AFTER
713}
714
715uint64_t
716info_get(enum info_kind kind, enum info_get get, void * data)
717{
718 switch (get) {
719 case GET_SUSPEND_COUNT:
720 switch (kind) {
721 case INFO_32:
722 case INFO_32_2:
723 return (uint64_t)(((task_basic_info_32_t)data)->suspend_count);
724#if defined(__arm__) || defined(__arm64__)
725 case INFO_64:
726 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
727 break;
728
729 case INFO_64_2:
730 return (uint64_t)(((task_basic_info_64_2_t)data)->suspend_count);
731#else
732 case INFO_64:
733 return (uint64_t)(((task_basic_info_64_t)data)->suspend_count);
734
735 case INFO_64_2:
736 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
737 break;
738#endif /* defined(__arm__) || defined(__arm64__) */
739 case INFO_MACH:
740 return (uint64_t)(((mach_task_basic_info_t)data)->suspend_count);
741 case INFO_MAX:
742 default:
743 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
744 }
745 case GET_RESIDENT_SIZE:
746 switch (kind) {
747 case INFO_32:
748 case INFO_32_2:
749 return (uint64_t)(((task_basic_info_32_t)data)->resident_size);
750#if defined(__arm__) || defined(__arm64__)
751 case INFO_64:
752 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
753 break;
754
755 case INFO_64_2:
756 return (uint64_t)(((task_basic_info_64_2_t)data)->resident_size);
757#else
758 case INFO_64:
759 return (uint64_t)(((task_basic_info_64_t)data)->resident_size);
760
761 case INFO_64_2:
762 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
763 break;
764#endif /* defined(__arm__) || defined(__arm64__) */
765 case INFO_MACH:
766 return (uint64_t)(((mach_task_basic_info_t)data)->resident_size);
767 case INFO_MAX:
768 default:
769 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
770 }
771 case GET_VIRTUAL_SIZE:
772 switch (kind) {
773 case INFO_32:
774 case INFO_32_2:
775 return (uint64_t)(((task_basic_info_32_t)data)->virtual_size);
776#if defined(__arm__) || defined(__arm64__)
777 case INFO_64:
778 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
779 break;
780
781 case INFO_64_2:
782 return (uint64_t)(((task_basic_info_64_2_t)data)->virtual_size);
783#else
784 case INFO_64:
785 return (uint64_t)(((task_basic_info_64_t)data)->virtual_size);
786
787 case INFO_64_2:
788 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
789 break;
790#endif /* defined(__arm__) || defined(__arm64__) */
791 case INFO_MACH:
792 return (uint64_t)(((mach_task_basic_info_t)data)->virtual_size);
793
794 case INFO_MAX:
795 default:
796 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
797 }
798 case GET_USER_TIME:
799 switch (kind) {
800 case INFO_32:
801 case INFO_32_2:
802 return (uint64_t) & (((task_basic_info_32_t)data)->user_time);
803#if defined(__arm__) || defined(__arm64__)
804 case INFO_64:
805 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
806 break;
807
808 case INFO_64_2:
809 return (uint64_t) & (((task_basic_info_64_2_t)data)->user_time);
810#else
811 case INFO_64:
812 return (uint64_t) & (((task_basic_info_64_t)data)->user_time);
813
814 case INFO_64_2:
815 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
816 break;
817#endif /* defined(__arm__) || defined(__arm64__) */
818 case INFO_MACH:
819 return (uint64_t) & (((mach_task_basic_info_t)data)->user_time);
820
821 case INFO_MAX:
822 default:
823 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
824 }
825 case GET_SYS_TIME:
826 switch (kind) {
827 case INFO_32:
828 case INFO_32_2:
829 return (uint64_t) & (((task_basic_info_32_t)data)->system_time);
830#if defined(__arm__) || defined(__arm64__)
831 case INFO_64:
832 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
833 break;
834
835 case INFO_64_2:
836 return (uint64_t) & (((task_basic_info_64_2_t)data)->system_time);
837#else
838 case INFO_64:
839 return (uint64_t) & (((task_basic_info_64_t)data)->system_time);
840
841 case INFO_64_2:
842 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
843 break;
844#endif /* defined(__arm__) || defined(__arm64__) */
845 case INFO_MACH:
846 return (uint64_t) & (((mach_task_basic_info_t)data)->user_time);
847 case INFO_MAX:
848 default:
849 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
850 }
851 case GET_POLICY:
852 switch (kind) {
853 case INFO_32:
854 case INFO_32_2:
855 return (uint64_t)(((task_basic_info_32_t)data)->policy);
856#if defined(__arm__) || defined(__arm64__)
857 case INFO_64:
858 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
859 break;
860
861 case INFO_64_2:
862 return (uint64_t)(((task_basic_info_64_2_t)data)->policy);
863#else
864 case INFO_64:
865 return (uint64_t)(((task_basic_info_64_t)data)->policy);
866
867 case INFO_64_2:
868 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
869 break;
870#endif /* defined(__arm__) || defined(__arm64__) */
871 case INFO_MACH:
872 return (uint64_t)(((mach_task_basic_info_t)data)->policy);
873
874 case INFO_MAX:
875 default:
876 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
877 }
878 case GET_MAX_RES:
879 switch (kind) {
880 case INFO_32:
881 case INFO_32_2:
882 case INFO_64:
883 case INFO_64_2:
884 T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
885 case INFO_MACH:
886 return (uint64_t)(((mach_task_basic_info_t)data)->resident_size_max);
887 case INFO_MAX:
888 default:
889 T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
890 }
891 }
892
893 __builtin_unreachable();
894}
895
896/*
897 * Determines whether we're running on a development kernel
898 */
899static int
900is_development_kernel(void)
901{
902#define NOTSET -1
903
904 static int is_dev = NOTSET;
905
906 if (is_dev == NOTSET) {
907 int dev;
908 size_t dev_size = sizeof(dev);
909
910 T_QUIET;
911 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.development", &dev, &dev_size, NULL, 0), NULL);
912 is_dev = (dev != 0);
913
914 return is_dev;
915 } else {
916 return is_dev;
917 }
918#undef NOTSET
919}