4 #include <System/sys/codesign.h>
5 #include <mach/mach_time.h>
7 #include <darwintest.h>
10 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
12 #if !defined(CS_OPS_CLEARPLATFORM)
13 #define CS_OPS_CLEARPLATFORM 13
16 #define WINDOW 1 /* seconds */
17 #define MAX_ATTEMP_PER_SEC 10
22 remove_platform_binary(void)
27 T_QUIET
; T_ASSERT_POSIX_ZERO(csops(getpid(), CS_OPS_STATUS
, &my_csflags
, sizeof(my_csflags
)), NULL
);
29 if (!(my_csflags
& CS_PLATFORM_BINARY
)) {
33 ret
= csops(getpid(), CS_OPS_CLEARPLATFORM
, NULL
, 0);
37 T_LOG("clearing platform binary not supported, skipping test");
40 T_LOG("csops failed with flag CS_OPS_CLEARPLATFORM");
46 T_QUIET
; T_ASSERT_POSIX_ZERO(csops(getpid(), CS_OPS_STATUS
, &my_csflags
, sizeof(my_csflags
)), NULL
);
48 if (my_csflags
& CS_PLATFORM_BINARY
) {
49 T_LOG("platform binary flag still set");
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
;
70 check_host_info(struct all_host_info
* data
, unsigned long iter
, char lett
)
75 /* check that for the shorter revisions no data is copied on the bytes of diff with the longer */
76 for (j
= 0; j
< iter
; j
++) {
77 datap
= (char*) &data
[j
].host_vm_info64_rev0
;
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
);
82 datap
= (char*) &data
[j
].host_vm_info_rev0
;
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
);
87 datap
= (char*) &data
[j
].host_vm_info_rev1
;
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
);
92 datap
= (char*) &data
[j
].host_expired_task_info
;
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
);
97 T_LOG("No data overflow");
101 /* check that after MAX_ATTEMP_PER_SEC data are all the same */
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);
108 T_LOG("Data was cached");
112 get_host_info(struct all_host_info
* data
, host_t self
, int iter
)
116 for (i
= 0; i
< iter
; i
++) {
117 count
= HOST_VM_INFO64_REV0_COUNT
;
118 T_QUIET
; T_ASSERT_POSIX_ZERO(host_statistics64(self
, HOST_VM_INFO64
, (host_info64_t
)&data
[i
].host_vm_info64_rev0
, &count
), NULL
);
119 count
= HOST_VM_INFO64_REV1_COUNT
;
120 T_QUIET
; T_ASSERT_POSIX_ZERO(host_statistics64(self
, HOST_VM_INFO64
, (host_info64_t
)&data
[i
].host_vm_info64_rev1
, &count
), NULL
);
121 count
= HOST_EXTMOD_INFO64_COUNT
;
122 T_QUIET
; T_ASSERT_POSIX_ZERO(host_statistics64(self
, HOST_EXTMOD_INFO64
, (host_info64_t
)&data
[i
].host_extmod_info64
, &count
), NULL
);
123 count
= HOST_LOAD_INFO_COUNT
;
124 T_QUIET
; T_ASSERT_POSIX_ZERO(host_statistics(self
, HOST_LOAD_INFO
, (host_info_t
)&data
[i
].host_load_info
, &count
), NULL
);
125 count
= HOST_VM_INFO_REV0_COUNT
;
126 T_QUIET
; T_ASSERT_POSIX_ZERO(host_statistics(self
, HOST_VM_INFO
, (host_info_t
)&data
[i
].host_vm_info_rev0
, &count
), NULL
);
127 count
= HOST_VM_INFO_REV1_COUNT
;
128 T_QUIET
; T_ASSERT_POSIX_ZERO(host_statistics(self
, HOST_VM_INFO
, (host_info_t
)&data
[i
].host_vm_info_rev1
, &count
), NULL
);
129 count
= HOST_VM_INFO_REV2_COUNT
;
130 T_QUIET
; T_ASSERT_POSIX_ZERO(host_statistics(self
, HOST_VM_INFO
, (host_info_t
)&data
[i
].host_vm_info_rev2
, &count
), NULL
);
131 count
= HOST_CPU_LOAD_INFO_COUNT
;
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
);
133 count
= TASK_POWER_INFO_COUNT
;
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
);
135 count
= TASK_POWER_INFO_V2_COUNT
;
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
);
140 T_DECL(test_host_statistics
, "testing rate limit for host_statistics",
141 T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(true))
143 unsigned long long start
, end
, window
;
147 struct all_host_info
* data
;
148 mach_timebase_info_data_t timebaseInfo
= { 0, 0 };
150 if (remove_platform_binary()) {
151 T_SKIP("Failed to remove platform binary");
154 data
= malloc(ITER
* sizeof(struct all_host_info
));
155 T_QUIET
; T_ASSERT_NE(data
, NULL
, "malloc");
157 /* check the size of the data structure against the bytes in COUNT*/
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");
165 /* check that the latest revision is the COUNT */
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");
169 /* check that the previous revision are smaller than the latest */
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");
175 memset(data
, lett
, ITER
* sizeof(struct all_host_info
));
176 self
= mach_host_self();
178 T_QUIET
; T_ASSERT_EQ(mach_timebase_info(&timebaseInfo
), KERN_SUCCESS
, NULL
);
179 window
= (WINDOW
* NSEC_PER_SEC
* timebaseInfo
.denom
) / timebaseInfo
.numer
;
182 /* try to get ITER copies of host_info within window time, in such a way we should hit for sure a cached copy */
184 start
= mach_continuous_time();
185 get_host_info(data
, self
, ITER
);
186 end
= mach_continuous_time();
188 } while ((end
- start
> window
) && retry
<= RETRY
);
190 if (retry
<= RETRY
) {
191 check_host_info(data
, ITER
, lett
);
193 T_SKIP("Failed to find window for test");