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