1 // Copyright 2021 (c) Apple Inc. All rights reserved. 2 3 #include <darwintest.h> 4 #include <darwintest_posix.h> 5 #include <libproc.h> 6 #include <stdint.h> 7 #include <sys/resource.h> 8 #include <unistd.h> 9 10 #include "test_utils.h" 11 #include "recount_test_utils.h" 12 13 T_GLOBAL_META( 14 T_META_RADAR_COMPONENT_NAME("xnu"), 15 T_META_RADAR_COMPONENT_VERSION("RM"), 16 T_META_OWNER("mwidmann"), 17 T_META_CHECK_LEAKS(false)); 18 19 T_DECL(rusage_kernel_cpu_time_sanity, 20 "ensure the CPU time for kernel_task is sane", T_META_ASROOT(true)) 21 { 22 struct rusage_info_v5 usage_info = { 0 }; 23 T_SETUPBEGIN; 24 int ret = proc_pid_rusage(0, RUSAGE_INFO_V5, (void *)&usage_info); 25 T_ASSERT_POSIX_SUCCESS(ret, "proc_pid_rusage on kernel_task"); 26 T_SETUPEND; 27 28 T_EXPECT_GT(usage_info.ri_system_time + usage_info.ri_user_time, 29 UINT64_C(0), "kernel CPU time should be non-zero"); 30 if (has_user_system_times()) { 31 T_EXPECT_EQ(usage_info.ri_user_time, 32 UINT64_C(0), "kernel user CPU time should be zero"); 33 } 34 } 35 36 T_DECL(rusage_user_time_sanity, 37 "ensure the user CPU time for a user space task is sane") 38 { 39 struct rusage_info_v5 usage_info = { 0 }; 40 T_SETUPBEGIN; 41 int ret = proc_pid_rusage(getpid(), RUSAGE_INFO_V5, (void *)&usage_info); 42 T_ASSERT_POSIX_SUCCESS(ret, "proc_pid_rusage on self"); 43 T_SETUPEND; 44 45 T_EXPECT_NE(usage_info.ri_user_time, UINT64_C(0), 46 "user space CPU time should be non-zero"); 47 if (has_user_system_times()) { 48 T_EXPECT_GT(usage_info.ri_system_time, UINT64_C(0), 49 "system time should be non-zero"); 50 } 51 } 52