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