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