]> git.saurik.com Git - apple/xnu.git/blob - tests/coalition_info.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / tests / coalition_info.c
1 #include <darwintest.h>
2 #include <inttypes.h>
3 #include <mach/coalition.h>
4 #include <stdint.h>
5 #include <sys/coalition.h>
6 #include <sys/sysctl.h>
7 #include <libproc.h>
8 #include <unistd.h>
9
10 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
11
12 static void
13 skip_if_monotonic_unsupported(void)
14 {
15 int r;
16 int supported = 0;
17 size_t supported_size = sizeof(supported);
18
19 r = sysctlbyname("kern.monotonic.supported", &supported, &supported_size,
20 NULL, 0);
21 if (r < 0) {
22 T_WITH_ERRNO;
23 T_SKIP("could not find \"kern.monotonic.supported\" sysctl");
24 }
25
26 if (!supported) {
27 T_SKIP("monotonic is not supported on this platform");
28 }
29 }
30
31 T_DECL(coalition_resource_info_counters,
32 "ensure that coalition resource info produces valid counter data")
33 {
34 skip_if_monotonic_unsupported();
35
36 struct proc_pidcoalitioninfo idinfo = {};
37 int ret = proc_pidinfo(getpid(), PROC_PIDCOALITIONINFO, 0,
38 &idinfo, sizeof(idinfo));
39 T_ASSERT_POSIX_SUCCESS(ret, "proc_pidinfo(... PROC_PIDCOALITIONINFO ...)");
40
41 uint64_t resid = idinfo.coalition_id[COALITION_TYPE_RESOURCE];
42
43 struct coalition_resource_usage coalusage[2] = {};
44 ret = coalition_info_resource_usage(resid, &coalusage[0],
45 sizeof(coalusage[0]));
46 T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_resource_usage()");
47 T_EXPECT_GT(coalusage[0].cpu_instructions, UINT64_C(0),
48 "instruction count is non-zero");
49 T_EXPECT_GT(coalusage[0].cpu_cycles, UINT64_C(0),
50 "cycle count is non-zero");
51
52 sleep(1);
53
54 ret = coalition_info_resource_usage(resid, &coalusage[1],
55 sizeof(coalusage[1]));
56 T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_resource_usage()");
57
58 T_EXPECT_GE(coalusage[1].cpu_instructions, coalusage[0].cpu_instructions,
59 "instruction count is monotonically increasing (+%" PRIu64 ")",
60 coalusage[1].cpu_instructions - coalusage[0].cpu_instructions);
61 T_EXPECT_GE(coalusage[1].cpu_cycles, coalusage[0].cpu_cycles,
62 "cycle count is monotonically increasing (+%" PRIu64 ")",
63 coalusage[1].cpu_cycles - coalusage[0].cpu_cycles);
64 }