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