]> git.saurik.com Git - apple/xnu.git/blob - tests/host_statistics_rate_limiting.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / host_statistics_rate_limiting.c
1 #include <unistd.h>
2 #include <stdint.h>
3 #include <sys/time.h>
4 #include <System/sys/codesign.h>
5 #include <mach/mach_time.h>
6 #include <mach/mach.h>
7 #include <darwintest.h>
8 #include <stdlib.h>
9
10 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
11
12 #if !defined(CS_OPS_CLEARPLATFORM)
13 #define CS_OPS_CLEARPLATFORM 13
14 #endif
15
16 #define WINDOW 1 /* seconds */
17 #define MAX_ATTEMP_PER_SEC 10
18 #define ITER 30
19 #define RETRY 5
20
21 static int
22 remove_platform_binary(void)
23 {
24 int ret;
25 uint32_t my_csflags;
26
27 T_QUIET; T_ASSERT_POSIX_ZERO(csops(getpid(), CS_OPS_STATUS, &my_csflags, sizeof(my_csflags)), NULL);
28
29 if (!(my_csflags & CS_PLATFORM_BINARY)) {
30 return 0;
31 }
32
33 ret = csops(getpid(), CS_OPS_CLEARPLATFORM, NULL, 0);
34 if (ret) {
35 switch (errno) {
36 case ENOTSUP:
37 T_LOG("clearing platform binary not supported, skipping test");
38 return -1;
39 default:
40 T_LOG("csops failed with flag CS_OPS_CLEARPLATFORM");
41 return -1;
42 }
43 }
44
45 my_csflags = 0;
46 T_QUIET; T_ASSERT_POSIX_ZERO(csops(getpid(), CS_OPS_STATUS, &my_csflags, sizeof(my_csflags)), NULL);
47
48 if (my_csflags & CS_PLATFORM_BINARY) {
49 T_LOG("platform binary flag still set");
50 return -1;
51 }
52
53 return 0;
54 }
55
56 struct all_host_info {
57 vm_statistics64_data_t host_vm_info64_rev0;
58 vm_statistics64_data_t host_vm_info64_rev1;
59 vm_extmod_statistics_data_t host_extmod_info64;
60 host_load_info_data_t host_load_info;
61 vm_statistics_data_t host_vm_info_rev0;
62 vm_statistics_data_t host_vm_info_rev1;
63 vm_statistics_data_t host_vm_info_rev2;
64 host_cpu_load_info_data_t host_cpu_load_info;
65 task_power_info_v2_data_t host_expired_task_info;
66 task_power_info_v2_data_t host_expired_task_info2;
67 };
68
69 static void
70 check_host_info(struct all_host_info* data, unsigned long iter, char lett)
71 {
72 char* datap;
73 unsigned long i, j;
74
75 /* check that for the shorter revisions no data is copied on the bytes of diff with the longer */
76 for (j = 0; j < iter; j++) {
77 datap = (char*) &data[j].host_vm_info64_rev0;
78 for (i = (HOST_VM_INFO64_REV0_COUNT * sizeof(int)); i < (HOST_VM_INFO64_REV1_COUNT * sizeof(int)); i++) {
79 T_QUIET; T_ASSERT_EQ(datap[i], lett, "HOST_VM_INFO64_REV0 byte %lu iter %lu", i, j);
80 }
81
82 datap = (char*) &data[j].host_vm_info_rev0;
83 for (i = (HOST_VM_INFO_REV0_COUNT * sizeof(int)); i < (HOST_VM_INFO_REV2_COUNT * sizeof(int)); i++) {
84 T_QUIET; T_ASSERT_EQ(datap[i], lett, "HOST_VM_INFO_REV0 byte %lu iter %lu", i, j);
85 }
86
87 datap = (char*) &data[j].host_vm_info_rev1;
88 for (i = (HOST_VM_INFO_REV1_COUNT * sizeof(int)); i < (HOST_VM_INFO_REV2_COUNT * sizeof(int)); i++) {
89 T_QUIET; T_ASSERT_EQ(datap[i], lett, "HOST_VM_INFO_REV1 byte %lu iter %lu", i, j);
90 }
91
92 datap = (char*) &data[j].host_expired_task_info;
93 for (i = (TASK_POWER_INFO_COUNT * sizeof(int)); i < (TASK_POWER_INFO_V2_COUNT * sizeof(int)); i++) {
94 T_QUIET; T_ASSERT_EQ(datap[i], lett, "TASK_POWER_INFO_COUNT byte %lu iter %lu", i, j);
95 }
96 }
97 T_LOG("No data overflow");
98
99 datap = (char*) data;
100
101 /* check that after MAX_ATTEMP_PER_SEC data are all the same */
102 for (i = 0; i < sizeof(struct all_host_info); i++) {
103 for (j = MAX_ATTEMP_PER_SEC - 1; j < iter - 1; j++) {
104 T_QUIET; T_ASSERT_EQ(datap[i + (j * sizeof(struct all_host_info))], datap[i + ((j + 1) * sizeof(struct all_host_info))], "all_host_info iter %lu does not match iter %lu", j, j + 1);
105 }
106 }
107
108 T_LOG("Data was cached");
109 }
110
111 static void
112 get_host_info(struct all_host_info* data, host_t self, int iter)
113 {
114 int i;
115 unsigned int count;
116 for (i = 0; i < iter; i++) {
117 count = HOST_VM_INFO64_REV0_COUNT;
118 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics64(self, HOST_VM_INFO64, (host_info64_t)&data[i].host_vm_info64_rev0, &count), NULL);
119 count = HOST_VM_INFO64_REV1_COUNT;
120 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics64(self, HOST_VM_INFO64, (host_info64_t)&data[i].host_vm_info64_rev1, &count), NULL);
121 count = HOST_EXTMOD_INFO64_COUNT;
122 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics64(self, HOST_EXTMOD_INFO64, (host_info64_t)&data[i].host_extmod_info64, &count), NULL);
123 count = HOST_LOAD_INFO_COUNT;
124 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics(self, HOST_LOAD_INFO, (host_info_t)&data[i].host_load_info, &count), NULL);
125 count = HOST_VM_INFO_REV0_COUNT;
126 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics(self, HOST_VM_INFO, (host_info_t)&data[i].host_vm_info_rev0, &count), NULL);
127 count = HOST_VM_INFO_REV1_COUNT;
128 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics(self, HOST_VM_INFO, (host_info_t)&data[i].host_vm_info_rev1, &count), NULL);
129 count = HOST_VM_INFO_REV2_COUNT;
130 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics(self, HOST_VM_INFO, (host_info_t)&data[i].host_vm_info_rev2, &count), NULL);
131 count = HOST_CPU_LOAD_INFO_COUNT;
132 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics(self, HOST_CPU_LOAD_INFO, (host_info_t)&data[i].host_cpu_load_info, &count), NULL);
133 count = TASK_POWER_INFO_COUNT;
134 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics(self, HOST_EXPIRED_TASK_INFO, (host_info_t)&data[i].host_expired_task_info, &count), NULL);
135 count = TASK_POWER_INFO_V2_COUNT;
136 T_QUIET; T_ASSERT_POSIX_ZERO(host_statistics(self, HOST_EXPIRED_TASK_INFO, (host_info_t)&data[i].host_expired_task_info2, &count), NULL);
137 }
138 }
139
140 T_DECL(test_host_statistics, "testing rate limit for host_statistics",
141 T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(true))
142 {
143 unsigned long long start, end, window;
144 int retry = 0;
145 host_t self;
146 char lett = 'a';
147 struct all_host_info* data;
148 mach_timebase_info_data_t timebaseInfo = { 0, 0 };
149
150 if (remove_platform_binary()) {
151 T_SKIP("Failed to remove platform binary");
152 }
153
154 data = malloc(ITER * sizeof(struct all_host_info));
155 T_QUIET; T_ASSERT_NE(data, NULL, "malloc");
156
157 /* check the size of the data structure against the bytes in COUNT*/
158 T_QUIET; T_ASSERT_EQ(sizeof(data[0].host_vm_info64_rev0), HOST_VM_INFO64_COUNT * sizeof(int), "HOST_VM_INFO64_COUNT");
159 T_QUIET; T_ASSERT_EQ(sizeof(data[0].host_extmod_info64), HOST_EXTMOD_INFO64_COUNT * sizeof(int), "HOST_EXTMOD_INFO64_COUNT");
160 T_QUIET; T_ASSERT_EQ(sizeof(data[0].host_load_info), HOST_LOAD_INFO_COUNT * sizeof(int), "HOST_LOAD_INFO_COUNT");
161 T_QUIET; T_ASSERT_EQ(sizeof(data[0].host_vm_info_rev0), HOST_VM_INFO_COUNT * sizeof(int), "HOST_VM_INFO_COUNT");
162 T_QUIET; T_ASSERT_EQ(sizeof(data[0].host_cpu_load_info), HOST_CPU_LOAD_INFO_COUNT * sizeof(int), "HOST_CPU_LOAD_INFO_COUNT");
163 T_QUIET; T_ASSERT_EQ(sizeof(data[0].host_expired_task_info2), TASK_POWER_INFO_V2_COUNT * sizeof(int), "TASK_POWER_INFO_V2_COUNT");
164
165 /* check that the latest revision is the COUNT */
166 T_QUIET; T_ASSERT_EQ(HOST_VM_INFO64_REV1_COUNT, HOST_VM_INFO64_COUNT, "HOST_VM_INFO64_REV1_COUNT");
167 T_QUIET; T_ASSERT_EQ(HOST_VM_INFO_REV2_COUNT, HOST_VM_INFO_COUNT, "HOST_VM_INFO_REV2_COUNT");
168
169 /* check that the previous revision are smaller than the latest */
170 T_QUIET; T_ASSERT_LE(HOST_VM_INFO64_REV0_COUNT, HOST_VM_INFO64_REV1_COUNT, "HOST_VM_INFO64_REV0");
171 T_QUIET; T_ASSERT_LE(HOST_VM_INFO_REV0_COUNT, HOST_VM_INFO_REV2_COUNT, "HOST_VM_INFO_REV0_COUNT");
172 T_QUIET; T_ASSERT_LE(HOST_VM_INFO_REV1_COUNT, HOST_VM_INFO_REV2_COUNT, "HOST_VM_INFO_REV1_COUNT");
173 T_QUIET; T_ASSERT_LE(TASK_POWER_INFO_COUNT, TASK_POWER_INFO_V2_COUNT, "TASK_POWER_INFO_COUNT");
174
175 memset(data, lett, ITER * sizeof(struct all_host_info));
176 self = mach_host_self();
177
178 T_QUIET; T_ASSERT_EQ(mach_timebase_info(&timebaseInfo), KERN_SUCCESS, NULL);
179 window = (WINDOW * NSEC_PER_SEC * timebaseInfo.denom) / timebaseInfo.numer;
180 retry = 0;
181
182 /* try to get ITER copies of host_info within window time, in such a way we should hit for sure a cached copy */
183 do {
184 start = mach_continuous_time();
185 get_host_info(data, self, ITER);
186 end = mach_continuous_time();
187 retry++;
188 } while ((end - start > window) && retry <= RETRY);
189
190 if (retry <= RETRY) {
191 check_host_info(data, ITER, lett);
192 } else {
193 T_SKIP("Failed to find window for test");
194 }
195 }