xref: /xnu-10063.101.15/tests/perf_compressor.c (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions #include <stdio.h>
2*94d3b452SApple OSS Distributions #include <signal.h>
3*94d3b452SApple OSS Distributions #include <sys/sysctl.h>
4*94d3b452SApple OSS Distributions #include <sys/kern_memorystatus.h>
5*94d3b452SApple OSS Distributions #include <mach-o/dyld.h>
6*94d3b452SApple OSS Distributions #include <perfcheck_keys.h>
7*94d3b452SApple OSS Distributions 
8*94d3b452SApple OSS Distributions #ifdef T_NAMESPACE
9*94d3b452SApple OSS Distributions #undef T_NAMESPACE
10*94d3b452SApple OSS Distributions #endif
11*94d3b452SApple OSS Distributions #include <darwintest.h>
12*94d3b452SApple OSS Distributions #include <darwintest_utils.h>
13*94d3b452SApple OSS Distributions 
14*94d3b452SApple OSS Distributions T_GLOBAL_META(
15*94d3b452SApple OSS Distributions 	T_META_NAMESPACE("xnu.vm.perf"),
16*94d3b452SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
17*94d3b452SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"),
18*94d3b452SApple OSS Distributions 	T_META_CHECK_LEAKS(false),
19*94d3b452SApple OSS Distributions 	T_META_TAG_PERF,
20*94d3b452SApple OSS Distributions 	T_META_ENABLED(false) /* rdar://84443533 */
21*94d3b452SApple OSS Distributions 	);
22*94d3b452SApple OSS Distributions 
23*94d3b452SApple OSS Distributions enum {
24*94d3b452SApple OSS Distributions 	ALL_ZEROS,
25*94d3b452SApple OSS Distributions 	MOSTLY_ZEROS,
26*94d3b452SApple OSS Distributions 	RANDOM,
27*94d3b452SApple OSS Distributions 	TYPICAL
28*94d3b452SApple OSS Distributions };
29*94d3b452SApple OSS Distributions 
30*94d3b452SApple OSS Distributions #define CREATE_LIST(X) \
31*94d3b452SApple OSS Distributions 	X(SUCCESS) \
32*94d3b452SApple OSS Distributions 	X(TOO_FEW_ARGUMENTS) \
33*94d3b452SApple OSS Distributions 	X(SYSCTL_VM_PAGESIZE_FAILED) \
34*94d3b452SApple OSS Distributions 	X(VM_PAGESIZE_IS_ZERO) \
35*94d3b452SApple OSS Distributions 	X(UNKNOWN_PAGE_TYPE) \
36*94d3b452SApple OSS Distributions 	X(DISPATCH_SOURCE_CREATE_FAILED) \
37*94d3b452SApple OSS Distributions 	X(INITIAL_SIGNAL_TO_PARENT_FAILED) \
38*94d3b452SApple OSS Distributions 	X(SIGNAL_TO_PARENT_FAILED) \
39*94d3b452SApple OSS Distributions 	X(MEMORYSTATUS_CONTROL_FAILED) \
40*94d3b452SApple OSS Distributions 	X(IS_FREEZABLE_NOT_AS_EXPECTED) \
41*94d3b452SApple OSS Distributions 	X(EXIT_CODE_MAX)
42*94d3b452SApple OSS Distributions 
43*94d3b452SApple OSS Distributions #define EXIT_CODES_ENUM(VAR) VAR,
44*94d3b452SApple OSS Distributions enum exit_codes_num {
45*94d3b452SApple OSS Distributions 	CREATE_LIST(EXIT_CODES_ENUM)
46*94d3b452SApple OSS Distributions };
47*94d3b452SApple OSS Distributions 
48*94d3b452SApple OSS Distributions #define EXIT_CODES_STRING(VAR) #VAR,
49*94d3b452SApple OSS Distributions static const char *exit_codes_str[] = {
50*94d3b452SApple OSS Distributions 	CREATE_LIST(EXIT_CODES_STRING)
51*94d3b452SApple OSS Distributions };
52*94d3b452SApple OSS Distributions 
53*94d3b452SApple OSS Distributions #define SYSCTL_FREEZE_TO_MEMORY         "kern.memorystatus_freeze_to_memory=1"
54*94d3b452SApple OSS Distributions 
55*94d3b452SApple OSS Distributions static pid_t pid = -1;
56*94d3b452SApple OSS Distributions static dt_stat_t ratio;
57*94d3b452SApple OSS Distributions static dt_stat_time_t compr_time;
58*94d3b452SApple OSS Distributions static dt_stat_time_t decompr_time;
59*94d3b452SApple OSS Distributions 
60*94d3b452SApple OSS Distributions void allocate_zero_pages(char **buf, int num_pages, int vmpgsize);
61*94d3b452SApple OSS Distributions void allocate_mostly_zero_pages(char **buf, int num_pages, int vmpgsize);
62*94d3b452SApple OSS Distributions void allocate_random_pages(char **buf, int num_pages, int vmpgsize);
63*94d3b452SApple OSS Distributions void allocate_representative_pages(char **buf, int num_pages, int vmpgsize);
64*94d3b452SApple OSS Distributions void run_compressor_test(int size_mb, int page_type);
65*94d3b452SApple OSS Distributions void freeze_helper_process(void);
66*94d3b452SApple OSS Distributions void cleanup(void);
67*94d3b452SApple OSS Distributions 
68*94d3b452SApple OSS Distributions void
allocate_zero_pages(char ** buf,int num_pages,int vmpgsize)69*94d3b452SApple OSS Distributions allocate_zero_pages(char **buf, int num_pages, int vmpgsize)
70*94d3b452SApple OSS Distributions {
71*94d3b452SApple OSS Distributions 	int i;
72*94d3b452SApple OSS Distributions 
73*94d3b452SApple OSS Distributions 	for (i = 0; i < num_pages; i++) {
74*94d3b452SApple OSS Distributions 		buf[i] = (char*)malloc((size_t)vmpgsize * sizeof(char));
75*94d3b452SApple OSS Distributions 		memset(buf[i], 0, vmpgsize);
76*94d3b452SApple OSS Distributions 	}
77*94d3b452SApple OSS Distributions }
78*94d3b452SApple OSS Distributions 
79*94d3b452SApple OSS Distributions void
allocate_mostly_zero_pages(char ** buf,int num_pages,int vmpgsize)80*94d3b452SApple OSS Distributions allocate_mostly_zero_pages(char **buf, int num_pages, int vmpgsize)
81*94d3b452SApple OSS Distributions {
82*94d3b452SApple OSS Distributions 	int i, j;
83*94d3b452SApple OSS Distributions 
84*94d3b452SApple OSS Distributions 	for (i = 0; i < num_pages; i++) {
85*94d3b452SApple OSS Distributions 		buf[i] = (char*)malloc((size_t)vmpgsize * sizeof(char));
86*94d3b452SApple OSS Distributions 		memset(buf[i], 0, vmpgsize);
87*94d3b452SApple OSS Distributions 		for (j = 0; j < 40; j++) {
88*94d3b452SApple OSS Distributions 			buf[i][j] = (char)(j + 1);
89*94d3b452SApple OSS Distributions 		}
90*94d3b452SApple OSS Distributions 	}
91*94d3b452SApple OSS Distributions }
92*94d3b452SApple OSS Distributions 
93*94d3b452SApple OSS Distributions void
allocate_random_pages(char ** buf,int num_pages,int vmpgsize)94*94d3b452SApple OSS Distributions allocate_random_pages(char **buf, int num_pages, int vmpgsize)
95*94d3b452SApple OSS Distributions {
96*94d3b452SApple OSS Distributions 	int i;
97*94d3b452SApple OSS Distributions 
98*94d3b452SApple OSS Distributions 	for (i = 0; i < num_pages; i++) {
99*94d3b452SApple OSS Distributions 		buf[i] = (char*)malloc((size_t)vmpgsize * sizeof(char));
100*94d3b452SApple OSS Distributions 		arc4random_buf((void*)buf[i], (size_t)vmpgsize);
101*94d3b452SApple OSS Distributions 	}
102*94d3b452SApple OSS Distributions }
103*94d3b452SApple OSS Distributions 
104*94d3b452SApple OSS Distributions // Gives us the compression ratio we see in the typical case (~2.7)
105*94d3b452SApple OSS Distributions void
allocate_representative_pages(char ** buf,int num_pages,int vmpgsize)106*94d3b452SApple OSS Distributions allocate_representative_pages(char **buf, int num_pages, int vmpgsize)
107*94d3b452SApple OSS Distributions {
108*94d3b452SApple OSS Distributions 	int i, j;
109*94d3b452SApple OSS Distributions 	char val;
110*94d3b452SApple OSS Distributions 
111*94d3b452SApple OSS Distributions 	for (j = 0; j < num_pages; j++) {
112*94d3b452SApple OSS Distributions 		buf[j] = (char*)malloc((size_t)vmpgsize * sizeof(char));
113*94d3b452SApple OSS Distributions 		val = 0;
114*94d3b452SApple OSS Distributions 		for (i = 0; i < vmpgsize; i += 16) {
115*94d3b452SApple OSS Distributions 			memset(&buf[j][i], val, 16);
116*94d3b452SApple OSS Distributions 			if (i < 3400 * (vmpgsize / 4096)) {
117*94d3b452SApple OSS Distributions 				val++;
118*94d3b452SApple OSS Distributions 			}
119*94d3b452SApple OSS Distributions 		}
120*94d3b452SApple OSS Distributions 	}
121*94d3b452SApple OSS Distributions }
122*94d3b452SApple OSS Distributions 
123*94d3b452SApple OSS Distributions void
freeze_helper_process(void)124*94d3b452SApple OSS Distributions freeze_helper_process(void)
125*94d3b452SApple OSS Distributions {
126*94d3b452SApple OSS Distributions 	int ret, freeze_enabled;
127*94d3b452SApple OSS Distributions 	int64_t compressed_before, compressed_after, input_before, input_after;
128*94d3b452SApple OSS Distributions 	size_t length;
129*94d3b452SApple OSS Distributions 	int errno_sysctl_freeze;
130*94d3b452SApple OSS Distributions 
131*94d3b452SApple OSS Distributions 	length = sizeof(compressed_before);
132*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctlbyname("vm.compressor_compressed_bytes", &compressed_before, &length, NULL, 0),
133*94d3b452SApple OSS Distributions 	    "failed to query vm.compressor_compressed_bytes");
134*94d3b452SApple OSS Distributions 	length = sizeof(input_before);
135*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctlbyname("vm.compressor_input_bytes", &input_before, &length, NULL, 0),
136*94d3b452SApple OSS Distributions 	    "failed to query vm.compressor_input_bytes");
137*94d3b452SApple OSS Distributions 
138*94d3b452SApple OSS Distributions 	T_STAT_MEASURE(compr_time) {
139*94d3b452SApple OSS Distributions 		ret = sysctlbyname("kern.memorystatus_freeze", NULL, NULL, &pid, sizeof(pid));
140*94d3b452SApple OSS Distributions 		errno_sysctl_freeze = errno;
141*94d3b452SApple OSS Distributions 	};
142*94d3b452SApple OSS Distributions 
143*94d3b452SApple OSS Distributions 	length = sizeof(compressed_after);
144*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctlbyname("vm.compressor_compressed_bytes", &compressed_after, &length, NULL, 0),
145*94d3b452SApple OSS Distributions 	    "failed to query vm.compressor_compressed_bytes");
146*94d3b452SApple OSS Distributions 	length = sizeof(input_after);
147*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctlbyname("vm.compressor_input_bytes", &input_after, &length, NULL, 0),
148*94d3b452SApple OSS Distributions 	    "failed to query vm.compressor_input_bytes");
149*94d3b452SApple OSS Distributions 
150*94d3b452SApple OSS Distributions 	length = sizeof(freeze_enabled);
151*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctlbyname("vm.freeze_enabled", &freeze_enabled, &length, NULL, 0),
152*94d3b452SApple OSS Distributions 	    "failed to query vm.freeze_enabled");
153*94d3b452SApple OSS Distributions 	if (freeze_enabled) {
154*94d3b452SApple OSS Distributions 		errno = errno_sysctl_freeze;
155*94d3b452SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl kern.memorystatus_freeze failed");
156*94d3b452SApple OSS Distributions 	} else {
157*94d3b452SApple OSS Distributions 		/* If freezer is disabled, skip the test. This can happen due to disk space shortage. */
158*94d3b452SApple OSS Distributions 		T_LOG("Freeze has been disabled. Terminating early.");
159*94d3b452SApple OSS Distributions 		T_END;
160*94d3b452SApple OSS Distributions 	}
161*94d3b452SApple OSS Distributions 
162*94d3b452SApple OSS Distributions 	dt_stat_add(ratio, (double)(input_after - input_before) / (double)(compressed_after - compressed_before));
163*94d3b452SApple OSS Distributions 
164*94d3b452SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus_thaw", NULL, NULL, &pid, sizeof(pid));
165*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl kern.memorystatus_thaw failed");
166*94d3b452SApple OSS Distributions 
167*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(pid, SIGUSR1), "failed to send SIGUSR1 to child process");
168*94d3b452SApple OSS Distributions }
169*94d3b452SApple OSS Distributions 
170*94d3b452SApple OSS Distributions void
cleanup(void)171*94d3b452SApple OSS Distributions cleanup(void)
172*94d3b452SApple OSS Distributions {
173*94d3b452SApple OSS Distributions 	/* No helper process. */
174*94d3b452SApple OSS Distributions 	if (pid == -1) {
175*94d3b452SApple OSS Distributions 		return;
176*94d3b452SApple OSS Distributions 	}
177*94d3b452SApple OSS Distributions 	/* Kill the helper process. */
178*94d3b452SApple OSS Distributions 	kill(pid, SIGKILL);
179*94d3b452SApple OSS Distributions }
180*94d3b452SApple OSS Distributions 
181*94d3b452SApple OSS Distributions void
run_compressor_test(int size_mb,int page_type)182*94d3b452SApple OSS Distributions run_compressor_test(int size_mb, int page_type)
183*94d3b452SApple OSS Distributions {
184*94d3b452SApple OSS Distributions 	int ret;
185*94d3b452SApple OSS Distributions 	char sz_str[50];
186*94d3b452SApple OSS Distributions 	char pt_str[50];
187*94d3b452SApple OSS Distributions 	char **launch_tool_args;
188*94d3b452SApple OSS Distributions 	char testpath[PATH_MAX];
189*94d3b452SApple OSS Distributions 	uint32_t testpath_buf_size;
190*94d3b452SApple OSS Distributions 	dispatch_source_t ds_freeze, ds_proc, ds_decompr;
191*94d3b452SApple OSS Distributions 	int freeze_enabled;
192*94d3b452SApple OSS Distributions 	size_t length;
193*94d3b452SApple OSS Distributions 	__block bool decompr_latency_is_stable = false;
194*94d3b452SApple OSS Distributions 
195*94d3b452SApple OSS Distributions 	length = sizeof(freeze_enabled);
196*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctlbyname("vm.freeze_enabled", &freeze_enabled, &length, NULL, 0),
197*94d3b452SApple OSS Distributions 	    "failed to query vm.freeze_enabled");
198*94d3b452SApple OSS Distributions 	if (!freeze_enabled) {
199*94d3b452SApple OSS Distributions 		/* If freezer is disabled, skip the test. This can happen due to disk space shortage. */
200*94d3b452SApple OSS Distributions 		T_SKIP("Freeze has been disabled. Skipping test.");
201*94d3b452SApple OSS Distributions 	}
202*94d3b452SApple OSS Distributions 
203*94d3b452SApple OSS Distributions 	T_ATEND(cleanup);
204*94d3b452SApple OSS Distributions 
205*94d3b452SApple OSS Distributions 	ratio = dt_stat_create("(input bytes / compressed bytes)", "compression_ratio");
206*94d3b452SApple OSS Distributions 	compr_time = dt_stat_time_create("compressor_latency");
207*94d3b452SApple OSS Distributions 
208*94d3b452SApple OSS Distributions 	// This sets the A/B failure threshold at 50% of baseline for compressor_latency
209*94d3b452SApple OSS Distributions 	dt_stat_set_variable((struct dt_stat *)compr_time, kPCFailureThresholdPctVar, 50.0);
210*94d3b452SApple OSS Distributions 
211*94d3b452SApple OSS Distributions 	signal(SIGUSR2, SIG_IGN);
212*94d3b452SApple OSS Distributions 	ds_decompr = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR2, 0, dispatch_get_main_queue());
213*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(ds_decompr, "dispatch_source_create (ds_decompr)");
214*94d3b452SApple OSS Distributions 
215*94d3b452SApple OSS Distributions 	dispatch_source_set_event_handler(ds_decompr, ^{
216*94d3b452SApple OSS Distributions 		decompr_latency_is_stable = true;
217*94d3b452SApple OSS Distributions 	});
218*94d3b452SApple OSS Distributions 	dispatch_activate(ds_decompr);
219*94d3b452SApple OSS Distributions 
220*94d3b452SApple OSS Distributions 	signal(SIGUSR1, SIG_IGN);
221*94d3b452SApple OSS Distributions 	ds_freeze = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dispatch_get_main_queue());
222*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(ds_freeze, "dispatch_source_create (ds_freeze)");
223*94d3b452SApple OSS Distributions 
224*94d3b452SApple OSS Distributions 	dispatch_source_set_event_handler(ds_freeze, ^{
225*94d3b452SApple OSS Distributions 		if (!(dt_stat_stable(compr_time) && decompr_latency_is_stable)) {
226*94d3b452SApple OSS Distributions 		        freeze_helper_process();
227*94d3b452SApple OSS Distributions 		} else {
228*94d3b452SApple OSS Distributions 		        dt_stat_finalize(compr_time);
229*94d3b452SApple OSS Distributions 		        dt_stat_finalize(ratio);
230*94d3b452SApple OSS Distributions 
231*94d3b452SApple OSS Distributions 		        kill(pid, SIGKILL);
232*94d3b452SApple OSS Distributions 		        dispatch_source_cancel(ds_freeze);
233*94d3b452SApple OSS Distributions 		        dispatch_source_cancel(ds_decompr);
234*94d3b452SApple OSS Distributions 		}
235*94d3b452SApple OSS Distributions 	});
236*94d3b452SApple OSS Distributions 	dispatch_activate(ds_freeze);
237*94d3b452SApple OSS Distributions 
238*94d3b452SApple OSS Distributions 	testpath_buf_size = sizeof(testpath);
239*94d3b452SApple OSS Distributions 	ret = _NSGetExecutablePath(testpath, &testpath_buf_size);
240*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ret, "_NSGetExecutablePath");
241*94d3b452SApple OSS Distributions 	T_LOG("Executable path: %s", testpath);
242*94d3b452SApple OSS Distributions 
243*94d3b452SApple OSS Distributions 	sprintf(sz_str, "%d", size_mb);
244*94d3b452SApple OSS Distributions 	sprintf(pt_str, "%d", page_type);
245*94d3b452SApple OSS Distributions 	launch_tool_args = (char *[]){
246*94d3b452SApple OSS Distributions 		testpath,
247*94d3b452SApple OSS Distributions 		"-n",
248*94d3b452SApple OSS Distributions 		"allocate_pages",
249*94d3b452SApple OSS Distributions 		"--",
250*94d3b452SApple OSS Distributions 		sz_str,
251*94d3b452SApple OSS Distributions 		pt_str,
252*94d3b452SApple OSS Distributions 		NULL
253*94d3b452SApple OSS Distributions 	};
254*94d3b452SApple OSS Distributions 
255*94d3b452SApple OSS Distributions 	/* Spawn the child process. Suspend after launch until the exit proc handler has been set up. */
256*94d3b452SApple OSS Distributions 	ret = dt_launch_tool(&pid, launch_tool_args, true, NULL, NULL);
257*94d3b452SApple OSS Distributions 	if (ret != 0) {
258*94d3b452SApple OSS Distributions 		T_LOG("dt_launch tool returned %d with error code %d", ret, errno);
259*94d3b452SApple OSS Distributions 	}
260*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "dt_launch_tool");
261*94d3b452SApple OSS Distributions 
262*94d3b452SApple OSS Distributions 	ds_proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, (uintptr_t)pid, DISPATCH_PROC_EXIT, dispatch_get_main_queue());
263*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(ds_proc, "dispatch_source_create (ds_proc)");
264*94d3b452SApple OSS Distributions 
265*94d3b452SApple OSS Distributions 	dispatch_source_set_event_handler(ds_proc, ^{
266*94d3b452SApple OSS Distributions 		int status = 0, code = 0;
267*94d3b452SApple OSS Distributions 		pid_t rc = waitpid(pid, &status, 0);
268*94d3b452SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(rc, pid, "waitpid");
269*94d3b452SApple OSS Distributions 		code = WEXITSTATUS(status);
270*94d3b452SApple OSS Distributions 
271*94d3b452SApple OSS Distributions 		if (code == 0) {
272*94d3b452SApple OSS Distributions 		        T_END;
273*94d3b452SApple OSS Distributions 		} else if (code > 0 && code < EXIT_CODE_MAX) {
274*94d3b452SApple OSS Distributions 		        T_ASSERT_FAIL("Child exited with %s", exit_codes_str[code]);
275*94d3b452SApple OSS Distributions 		} else {
276*94d3b452SApple OSS Distributions 		        T_ASSERT_FAIL("Child exited with unknown exit code %d", code);
277*94d3b452SApple OSS Distributions 		}
278*94d3b452SApple OSS Distributions 	});
279*94d3b452SApple OSS Distributions 	dispatch_activate(ds_proc);
280*94d3b452SApple OSS Distributions 
281*94d3b452SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kill(pid, SIGCONT), "failed to send SIGCONT to child process");
282*94d3b452SApple OSS Distributions 	dispatch_main();
283*94d3b452SApple OSS Distributions }
284*94d3b452SApple OSS Distributions 
285*94d3b452SApple OSS Distributions T_HELPER_DECL(allocate_pages, "allocates pages to compress") {
286*94d3b452SApple OSS Distributions 	int i, j, ret, size_mb, page_type, vmpgsize, freezable_state;
287*94d3b452SApple OSS Distributions 	size_t vmpgsize_length;
288*94d3b452SApple OSS Distributions 	__block int num_pages;
289*94d3b452SApple OSS Distributions 	__block char **buf;
290*94d3b452SApple OSS Distributions 	dispatch_source_t ds_signal;
291*94d3b452SApple OSS Distributions 
292*94d3b452SApple OSS Distributions 	vmpgsize_length = sizeof(vmpgsize);
293*94d3b452SApple OSS Distributions 	ret = sysctlbyname("vm.pagesize", &vmpgsize, &vmpgsize_length, NULL, 0);
294*94d3b452SApple OSS Distributions 	if (ret != 0) {
295*94d3b452SApple OSS Distributions 		exit(SYSCTL_VM_PAGESIZE_FAILED);
296*94d3b452SApple OSS Distributions 	}
297*94d3b452SApple OSS Distributions 	if (vmpgsize == 0) {
298*94d3b452SApple OSS Distributions 		exit(VM_PAGESIZE_IS_ZERO);
299*94d3b452SApple OSS Distributions 	}
300*94d3b452SApple OSS Distributions 
301*94d3b452SApple OSS Distributions 	if (argc < 2) {
302*94d3b452SApple OSS Distributions 		exit(TOO_FEW_ARGUMENTS);
303*94d3b452SApple OSS Distributions 	}
304*94d3b452SApple OSS Distributions 
305*94d3b452SApple OSS Distributions 	size_mb = atoi(argv[0]);
306*94d3b452SApple OSS Distributions 	page_type = atoi(argv[1]);
307*94d3b452SApple OSS Distributions 	num_pages = size_mb * 1024 * 1024 / vmpgsize;
308*94d3b452SApple OSS Distributions 	buf = (char**)malloc(sizeof(char*) * (size_t)num_pages);
309*94d3b452SApple OSS Distributions 
310*94d3b452SApple OSS Distributions 	// Switch on the type of page requested
311*94d3b452SApple OSS Distributions 	switch (page_type) {
312*94d3b452SApple OSS Distributions 	case ALL_ZEROS:
313*94d3b452SApple OSS Distributions 		allocate_zero_pages(buf, num_pages, vmpgsize);
314*94d3b452SApple OSS Distributions 		break;
315*94d3b452SApple OSS Distributions 	case MOSTLY_ZEROS:
316*94d3b452SApple OSS Distributions 		allocate_mostly_zero_pages(buf, num_pages, vmpgsize);
317*94d3b452SApple OSS Distributions 		break;
318*94d3b452SApple OSS Distributions 	case RANDOM:
319*94d3b452SApple OSS Distributions 		allocate_random_pages(buf, num_pages, vmpgsize);
320*94d3b452SApple OSS Distributions 		break;
321*94d3b452SApple OSS Distributions 	case TYPICAL:
322*94d3b452SApple OSS Distributions 		allocate_representative_pages(buf, num_pages, vmpgsize);
323*94d3b452SApple OSS Distributions 		break;
324*94d3b452SApple OSS Distributions 	default:
325*94d3b452SApple OSS Distributions 		exit(UNKNOWN_PAGE_TYPE);
326*94d3b452SApple OSS Distributions 	}
327*94d3b452SApple OSS Distributions 
328*94d3b452SApple OSS Distributions 	for (j = 0; j < num_pages; j++) {
329*94d3b452SApple OSS Distributions 		i = buf[j][0];
330*94d3b452SApple OSS Distributions 	}
331*94d3b452SApple OSS Distributions 
332*94d3b452SApple OSS Distributions 	decompr_time = dt_stat_time_create("decompression_latency");
333*94d3b452SApple OSS Distributions 
334*94d3b452SApple OSS Distributions 	/* Opt in to freezing. */
335*94d3b452SApple OSS Distributions 	printf("[%d] Setting state to freezable\n", getpid());
336*94d3b452SApple OSS Distributions 	if (memorystatus_control(MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE, getpid(), 1, NULL, 0) != KERN_SUCCESS) {
337*94d3b452SApple OSS Distributions 		exit(MEMORYSTATUS_CONTROL_FAILED);
338*94d3b452SApple OSS Distributions 	}
339*94d3b452SApple OSS Distributions 
340*94d3b452SApple OSS Distributions 	/* Verify that the state has been set correctly */
341*94d3b452SApple OSS Distributions 	freezable_state = memorystatus_control(MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE, getpid(), 0, NULL, 0);
342*94d3b452SApple OSS Distributions 	if (freezable_state != 1) {
343*94d3b452SApple OSS Distributions 		exit(IS_FREEZABLE_NOT_AS_EXPECTED);
344*94d3b452SApple OSS Distributions 	}
345*94d3b452SApple OSS Distributions 
346*94d3b452SApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), ^{
347*94d3b452SApple OSS Distributions 		/* Signal to the parent that we're done allocating and it's ok to freeze us */
348*94d3b452SApple OSS Distributions 		printf("[%d] Sending initial signal to parent to begin freezing\n", getpid());
349*94d3b452SApple OSS Distributions 		if (kill(getppid(), SIGUSR1) != 0) {
350*94d3b452SApple OSS Distributions 		        exit(INITIAL_SIGNAL_TO_PARENT_FAILED);
351*94d3b452SApple OSS Distributions 		}
352*94d3b452SApple OSS Distributions 	});
353*94d3b452SApple OSS Distributions 
354*94d3b452SApple OSS Distributions 	signal(SIGUSR1, SIG_IGN);
355*94d3b452SApple OSS Distributions 	ds_signal = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dispatch_get_main_queue());
356*94d3b452SApple OSS Distributions 	if (ds_signal == NULL) {
357*94d3b452SApple OSS Distributions 		exit(DISPATCH_SOURCE_CREATE_FAILED);
358*94d3b452SApple OSS Distributions 	}
359*94d3b452SApple OSS Distributions 
360*94d3b452SApple OSS Distributions 	__block bool collect_dt_stat_measurements = true;
361*94d3b452SApple OSS Distributions 
362*94d3b452SApple OSS Distributions 	dispatch_source_set_event_handler(ds_signal, ^{
363*94d3b452SApple OSS Distributions 		volatile int tmp;
364*94d3b452SApple OSS Distributions 		uint64_t decompr_start_time, decompr_end_time;
365*94d3b452SApple OSS Distributions 
366*94d3b452SApple OSS Distributions 		decompr_start_time = mach_absolute_time();
367*94d3b452SApple OSS Distributions 
368*94d3b452SApple OSS Distributions 		/* Make sure all the pages are accessed before trying to freeze again */
369*94d3b452SApple OSS Distributions 		for (int x = 0; x < num_pages; x++) {
370*94d3b452SApple OSS Distributions 		        tmp = buf[x][0];
371*94d3b452SApple OSS Distributions 		}
372*94d3b452SApple OSS Distributions 
373*94d3b452SApple OSS Distributions 		decompr_end_time = mach_absolute_time();
374*94d3b452SApple OSS Distributions 
375*94d3b452SApple OSS Distributions 		if (collect_dt_stat_measurements) {
376*94d3b452SApple OSS Distributions 			if (dt_stat_stable(decompr_time)) {
377*94d3b452SApple OSS Distributions 				collect_dt_stat_measurements = false;
378*94d3b452SApple OSS Distributions 				dt_stat_finalize(decompr_time);
379*94d3b452SApple OSS Distributions 				if (kill(getppid(), SIGUSR2) != 0) {
380*94d3b452SApple OSS Distributions 					exit(SIGNAL_TO_PARENT_FAILED);
381*94d3b452SApple OSS Distributions 				}
382*94d3b452SApple OSS Distributions 			} else {
383*94d3b452SApple OSS Distributions 				dt_stat_mach_time_add(decompr_time, decompr_end_time - decompr_start_time);
384*94d3b452SApple OSS Distributions 			}
385*94d3b452SApple OSS Distributions 		}
386*94d3b452SApple OSS Distributions 
387*94d3b452SApple OSS Distributions 		if (kill(getppid(), SIGUSR1) != 0) {
388*94d3b452SApple OSS Distributions 		        exit(SIGNAL_TO_PARENT_FAILED);
389*94d3b452SApple OSS Distributions 		}
390*94d3b452SApple OSS Distributions 	});
391*94d3b452SApple OSS Distributions 	dispatch_activate(ds_signal);
392*94d3b452SApple OSS Distributions 
393*94d3b452SApple OSS Distributions 	dispatch_main();
394*94d3b452SApple OSS Distributions }
395*94d3b452SApple OSS Distributions 
396*94d3b452SApple OSS Distributions // Numbers for 10MB and above are fairly reproducible. Anything smaller shows a lot of variation.
397*94d3b452SApple OSS Distributions 
398*94d3b452SApple OSS Distributions // Keeping just the 100MB version for iOSMark
399*94d3b452SApple OSS Distributions #ifndef DT_IOSMARK
400*94d3b452SApple OSS Distributions T_DECL(compr_10MB_zero,
401*94d3b452SApple OSS Distributions     "Compression latency for 10MB - zero pages",
402*94d3b452SApple OSS Distributions     T_META_ASROOT(true),
403*94d3b452SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_FREEZE_TO_MEMORY)) {
404*94d3b452SApple OSS Distributions 	run_compressor_test(10, ALL_ZEROS);
405*94d3b452SApple OSS Distributions }
406*94d3b452SApple OSS Distributions 
407*94d3b452SApple OSS Distributions T_DECL(compr_10MB_mostly_zero,
408*94d3b452SApple OSS Distributions     "Compression latency for 10MB - mostly zero pages",
409*94d3b452SApple OSS Distributions     T_META_ASROOT(true),
410*94d3b452SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_FREEZE_TO_MEMORY)) {
411*94d3b452SApple OSS Distributions 	run_compressor_test(10, MOSTLY_ZEROS);
412*94d3b452SApple OSS Distributions }
413*94d3b452SApple OSS Distributions 
414*94d3b452SApple OSS Distributions T_DECL(compr_10MB_random,
415*94d3b452SApple OSS Distributions     "Compression latency for 10MB - random pages",
416*94d3b452SApple OSS Distributions     T_META_ASROOT(true),
417*94d3b452SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_FREEZE_TO_MEMORY)) {
418*94d3b452SApple OSS Distributions 	run_compressor_test(10, RANDOM);
419*94d3b452SApple OSS Distributions }
420*94d3b452SApple OSS Distributions 
421*94d3b452SApple OSS Distributions T_DECL(compr_10MB_typical,
422*94d3b452SApple OSS Distributions     "Compression latency for 10MB - typical pages",
423*94d3b452SApple OSS Distributions     T_META_ASROOT(true),
424*94d3b452SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_FREEZE_TO_MEMORY)) {
425*94d3b452SApple OSS Distributions 	run_compressor_test(10, TYPICAL);
426*94d3b452SApple OSS Distributions }
427*94d3b452SApple OSS Distributions 
428*94d3b452SApple OSS Distributions T_DECL(compr_100MB_zero,
429*94d3b452SApple OSS Distributions     "Compression latency for 100MB - zero pages",
430*94d3b452SApple OSS Distributions     T_META_ASROOT(true),
431*94d3b452SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_FREEZE_TO_MEMORY)) {
432*94d3b452SApple OSS Distributions 	run_compressor_test(100, ALL_ZEROS);
433*94d3b452SApple OSS Distributions }
434*94d3b452SApple OSS Distributions 
435*94d3b452SApple OSS Distributions T_DECL(compr_100MB_mostly_zero,
436*94d3b452SApple OSS Distributions     "Compression latency for 100MB - mostly zero pages",
437*94d3b452SApple OSS Distributions     T_META_ASROOT(true),
438*94d3b452SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_FREEZE_TO_MEMORY)) {
439*94d3b452SApple OSS Distributions 	run_compressor_test(100, MOSTLY_ZEROS);
440*94d3b452SApple OSS Distributions }
441*94d3b452SApple OSS Distributions 
442*94d3b452SApple OSS Distributions T_DECL(compr_100MB_random,
443*94d3b452SApple OSS Distributions     "Compression latency for 100MB - random pages",
444*94d3b452SApple OSS Distributions     T_META_ASROOT(true),
445*94d3b452SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_FREEZE_TO_MEMORY)) {
446*94d3b452SApple OSS Distributions 	run_compressor_test(100, RANDOM);
447*94d3b452SApple OSS Distributions }
448*94d3b452SApple OSS Distributions #endif
449*94d3b452SApple OSS Distributions 
450*94d3b452SApple OSS Distributions T_DECL(compr_100MB_typical,
451*94d3b452SApple OSS Distributions     "Compression latency for 100MB - typical pages",
452*94d3b452SApple OSS Distributions     T_META_ASROOT(true),
453*94d3b452SApple OSS Distributions     T_META_SYSCTL_INT(SYSCTL_FREEZE_TO_MEMORY)) {
454*94d3b452SApple OSS Distributions 	run_compressor_test(100, TYPICAL);
455*94d3b452SApple OSS Distributions }
456