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