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