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