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