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