xref: /xnu-8020.121.3/tests/task_vm_info_decompressions.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions #include <stdlib.h>
2*fdd8201dSApple OSS Distributions #include <stdio.h>
3*fdd8201dSApple OSS Distributions #include <mach/task_info.h>
4*fdd8201dSApple OSS Distributions #include <mach/mach.h>
5*fdd8201dSApple OSS Distributions #include <unistd.h>
6*fdd8201dSApple OSS Distributions #include <signal.h>
7*fdd8201dSApple OSS Distributions #include <errno.h>
8*fdd8201dSApple OSS Distributions #include <sys/kern_memorystatus.h>
9*fdd8201dSApple OSS Distributions #include <sys/sysctl.h>
10*fdd8201dSApple OSS Distributions #include <stdatomic.h>
11*fdd8201dSApple OSS Distributions 
12*fdd8201dSApple OSS Distributions #include <darwintest.h>
13*fdd8201dSApple OSS Distributions #include <TargetConditionals.h>
14*fdd8201dSApple OSS Distributions 
15*fdd8201dSApple OSS Distributions T_GLOBAL_META(
16*fdd8201dSApple OSS Distributions 	T_META_NAMESPACE("xnu.vm"),
17*fdd8201dSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
18*fdd8201dSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"));
19*fdd8201dSApple OSS Distributions 
20*fdd8201dSApple OSS Distributions #define KB 1024
21*fdd8201dSApple OSS Distributions #define MALLOC_SIZE_PER_THREAD (64 * KB)
22*fdd8201dSApple OSS Distributions #define freezer_path "/usr/local/bin/freeze"
23*fdd8201dSApple OSS Distributions 
24*fdd8201dSApple OSS Distributions /* BridgeOS could spend more time execv freezer */
25*fdd8201dSApple OSS Distributions #if TARGET_OS_BRIDGE
26*fdd8201dSApple OSS Distributions static int timeout = 600;
27*fdd8201dSApple OSS Distributions #else
28*fdd8201dSApple OSS Distributions static int timeout = 120;
29*fdd8201dSApple OSS Distributions #endif
30*fdd8201dSApple OSS Distributions 
31*fdd8201dSApple OSS Distributions static _Atomic int thread_malloc_count = 0;
32*fdd8201dSApple OSS Distributions static _Atomic int thread_thawed_count = 0;
33*fdd8201dSApple OSS Distributions static _Atomic int phase = 0;
34*fdd8201dSApple OSS Distributions 
35*fdd8201dSApple OSS Distributions struct thread_args {
36*fdd8201dSApple OSS Distributions 	int    id;
37*fdd8201dSApple OSS Distributions };
38*fdd8201dSApple OSS Distributions 
39*fdd8201dSApple OSS Distributions static void
freeze_pid(pid_t pid)40*fdd8201dSApple OSS Distributions freeze_pid(pid_t pid)
41*fdd8201dSApple OSS Distributions {
42*fdd8201dSApple OSS Distributions 	char pid_str[6];
43*fdd8201dSApple OSS Distributions 	char *args[3];
44*fdd8201dSApple OSS Distributions 	pid_t child_pid;
45*fdd8201dSApple OSS Distributions 	int status;
46*fdd8201dSApple OSS Distributions 
47*fdd8201dSApple OSS Distributions 	sprintf(pid_str, "%d", pid);
48*fdd8201dSApple OSS Distributions 	child_pid = fork();
49*fdd8201dSApple OSS Distributions 	if (child_pid == 0) {
50*fdd8201dSApple OSS Distributions 		/* Launch freezer */
51*fdd8201dSApple OSS Distributions 		args[0] = freezer_path;
52*fdd8201dSApple OSS Distributions 		args[1] = pid_str;
53*fdd8201dSApple OSS Distributions 		args[2] = NULL;
54*fdd8201dSApple OSS Distributions 		execv(freezer_path, args);
55*fdd8201dSApple OSS Distributions 		/* execve() does not return on success */
56*fdd8201dSApple OSS Distributions 		perror("execve");
57*fdd8201dSApple OSS Distributions 		T_FAIL("execve() failed");
58*fdd8201dSApple OSS Distributions 	}
59*fdd8201dSApple OSS Distributions 
60*fdd8201dSApple OSS Distributions 	/* Wait for freezer to complete */
61*fdd8201dSApple OSS Distributions 	T_LOG("Waiting for freezer %d to complete", child_pid);
62*fdd8201dSApple OSS Distributions 	while (0 == waitpid(child_pid, &status, WNOHANG)) {
63*fdd8201dSApple OSS Distributions 		if (timeout < 0) {
64*fdd8201dSApple OSS Distributions 			kill(child_pid, SIGKILL);
65*fdd8201dSApple OSS Distributions 			T_FAIL("Freezer took too long to freeze the test");
66*fdd8201dSApple OSS Distributions 		}
67*fdd8201dSApple OSS Distributions 		sleep(1);
68*fdd8201dSApple OSS Distributions 		timeout--;
69*fdd8201dSApple OSS Distributions 	}
70*fdd8201dSApple OSS Distributions 	if (WIFEXITED(status) != 1 || WEXITSTATUS(status) != 0) {
71*fdd8201dSApple OSS Distributions 		T_FAIL("Freezer error'd out");
72*fdd8201dSApple OSS Distributions 	}
73*fdd8201dSApple OSS Distributions }
74*fdd8201dSApple OSS Distributions static void *
worker_thread_function(void * args)75*fdd8201dSApple OSS Distributions worker_thread_function(void *args)
76*fdd8201dSApple OSS Distributions {
77*fdd8201dSApple OSS Distributions 	struct thread_args *targs = args;
78*fdd8201dSApple OSS Distributions 	int thread_id = targs->id;
79*fdd8201dSApple OSS Distributions 	char *array;
80*fdd8201dSApple OSS Distributions 
81*fdd8201dSApple OSS Distributions 	/* Allocate memory */
82*fdd8201dSApple OSS Distributions 	array = malloc(MALLOC_SIZE_PER_THREAD);
83*fdd8201dSApple OSS Distributions 	T_EXPECT_NOTNULL(array, "thread %d allocated heap memory to be dirtied", thread_id);
84*fdd8201dSApple OSS Distributions 
85*fdd8201dSApple OSS Distributions 	/* Waiting for phase 1 (touch pages) to start */
86*fdd8201dSApple OSS Distributions 	while (atomic_load(&phase) != 1) {
87*fdd8201dSApple OSS Distributions 		;
88*fdd8201dSApple OSS Distributions 	}
89*fdd8201dSApple OSS Distributions 
90*fdd8201dSApple OSS Distributions 	/* Phase 1: touch pages */
91*fdd8201dSApple OSS Distributions 	T_LOG("thread %d phase 1: dirtying %d heap pages (%d bytes)", thread_id, MALLOC_SIZE_PER_THREAD / (int)PAGE_SIZE, MALLOC_SIZE_PER_THREAD);
92*fdd8201dSApple OSS Distributions 	memset(&array[0], 1, MALLOC_SIZE_PER_THREAD);
93*fdd8201dSApple OSS Distributions 	atomic_fetch_add(&thread_malloc_count, 1);
94*fdd8201dSApple OSS Distributions 
95*fdd8201dSApple OSS Distributions 	/* Wait for process to be frozen */
96*fdd8201dSApple OSS Distributions 	while (atomic_load(&phase) != 2) {
97*fdd8201dSApple OSS Distributions 		;
98*fdd8201dSApple OSS Distributions 	}
99*fdd8201dSApple OSS Distributions 
100*fdd8201dSApple OSS Distributions 	/* Phase 2, process thawed, trigger decompressions by re-faulting pages */
101*fdd8201dSApple OSS Distributions 	T_LOG("thread %d phase 2: faulting pages back in to trigger decompressions", thread_id);
102*fdd8201dSApple OSS Distributions 	memset(&array[0], 1, MALLOC_SIZE_PER_THREAD);
103*fdd8201dSApple OSS Distributions 
104*fdd8201dSApple OSS Distributions 	/* Main thread will retrieve vm statistics once all threads are thawed */
105*fdd8201dSApple OSS Distributions 	atomic_fetch_add(&thread_thawed_count, 1);
106*fdd8201dSApple OSS Distributions 
107*fdd8201dSApple OSS Distributions 	free(array);
108*fdd8201dSApple OSS Distributions 
109*fdd8201dSApple OSS Distributions 
110*fdd8201dSApple OSS Distributions #if 0 /* Test if the thread's decompressions counter was added to the task decompressions counter when a thread terminates */
111*fdd8201dSApple OSS Distributions 	if (thread_id < 2) {
112*fdd8201dSApple OSS Distributions 		sleep(10);
113*fdd8201dSApple OSS Distributions 	}
114*fdd8201dSApple OSS Distributions #endif
115*fdd8201dSApple OSS Distributions 
116*fdd8201dSApple OSS Distributions 	return NULL;
117*fdd8201dSApple OSS Distributions }
118*fdd8201dSApple OSS Distributions 
119*fdd8201dSApple OSS Distributions static pthread_t*
create_threads(int nthreads,pthread_t * threads,struct thread_args * targs)120*fdd8201dSApple OSS Distributions create_threads(int nthreads, pthread_t *threads, struct thread_args *targs)
121*fdd8201dSApple OSS Distributions {
122*fdd8201dSApple OSS Distributions 	int i;
123*fdd8201dSApple OSS Distributions 	int err;
124*fdd8201dSApple OSS Distributions 	pthread_attr_t attr;
125*fdd8201dSApple OSS Distributions 
126*fdd8201dSApple OSS Distributions 	err = pthread_attr_init(&attr);
127*fdd8201dSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(err, "pthread_attr_init");
128*fdd8201dSApple OSS Distributions 	for (i = 0; i < nthreads; i++) {
129*fdd8201dSApple OSS Distributions 		targs[i].id = i;
130*fdd8201dSApple OSS Distributions 		err = pthread_create(&threads[i], &attr, worker_thread_function, (void*)&targs[i]);
131*fdd8201dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(err, "pthread_create");
132*fdd8201dSApple OSS Distributions 	}
133*fdd8201dSApple OSS Distributions 
134*fdd8201dSApple OSS Distributions 	return threads;
135*fdd8201dSApple OSS Distributions }
136*fdd8201dSApple OSS Distributions 
137*fdd8201dSApple OSS Distributions static void
join_threads(int nthreads,pthread_t * threads)138*fdd8201dSApple OSS Distributions join_threads(int nthreads, pthread_t *threads)
139*fdd8201dSApple OSS Distributions {
140*fdd8201dSApple OSS Distributions 	int i;
141*fdd8201dSApple OSS Distributions 	int err;
142*fdd8201dSApple OSS Distributions 
143*fdd8201dSApple OSS Distributions 	for (i = 0; i < nthreads; i++) {
144*fdd8201dSApple OSS Distributions 		err = pthread_join(threads[i], NULL);
145*fdd8201dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(err, "pthread_join");
146*fdd8201dSApple OSS Distributions 	}
147*fdd8201dSApple OSS Distributions }
148*fdd8201dSApple OSS Distributions 
149*fdd8201dSApple OSS Distributions T_DECL(task_vm_info_decompressions,
150*fdd8201dSApple OSS Distributions     "Test multithreaded per-task decompressions counter")
151*fdd8201dSApple OSS Distributions {
152*fdd8201dSApple OSS Distributions 	int     err;
153*fdd8201dSApple OSS Distributions 	int     ncpu;
154*fdd8201dSApple OSS Distributions 	size_t  ncpu_size = sizeof(ncpu);
155*fdd8201dSApple OSS Distributions 	int     npages;
156*fdd8201dSApple OSS Distributions 	int     compressor_mode;
157*fdd8201dSApple OSS Distributions 	size_t  compressor_mode_size = sizeof(compressor_mode);
158*fdd8201dSApple OSS Distributions 	task_vm_info_data_t vm_info;
159*fdd8201dSApple OSS Distributions 	mach_msg_type_number_t count;
160*fdd8201dSApple OSS Distributions 	pthread_t *threads;
161*fdd8201dSApple OSS Distributions 	struct thread_args *targs;
162*fdd8201dSApple OSS Distributions 
163*fdd8201dSApple OSS Distributions 	T_SETUPBEGIN;
164*fdd8201dSApple OSS Distributions 
165*fdd8201dSApple OSS Distributions 	/* Make sure freezer is enabled on target machine */
166*fdd8201dSApple OSS Distributions 	err = sysctlbyname("vm.compressor_mode", &compressor_mode, &compressor_mode_size, NULL, 0);
167*fdd8201dSApple OSS Distributions 	if (compressor_mode < 8) {
168*fdd8201dSApple OSS Distributions 		T_SKIP("This test requires freezer which is not available on the testing platform (vm.compressor_mode is set to %d)", compressor_mode);
169*fdd8201dSApple OSS Distributions 	}
170*fdd8201dSApple OSS Distributions #if TARGET_OS_BRIDGE
171*fdd8201dSApple OSS Distributions 	T_SKIP("This test requires freezer which is not available on bridgeOS (vm.compressor_mode is set to %d)", compressor_mode);
172*fdd8201dSApple OSS Distributions #endif
173*fdd8201dSApple OSS Distributions 
174*fdd8201dSApple OSS Distributions 	/* Set number of threads to ncpu available on testing device */
175*fdd8201dSApple OSS Distributions 	err = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
176*fdd8201dSApple OSS Distributions 	T_EXPECT_EQ_INT(0, err, "Detected %d cpus\n", ncpu);
177*fdd8201dSApple OSS Distributions 
178*fdd8201dSApple OSS Distributions 	/* Set total number of pages to be frozen */
179*fdd8201dSApple OSS Distributions 	npages = ncpu * MALLOC_SIZE_PER_THREAD / (int)PAGE_SIZE;
180*fdd8201dSApple OSS Distributions 	T_LOG("Test will be freezing at least %d heap pages\n", npages);
181*fdd8201dSApple OSS Distributions 
182*fdd8201dSApple OSS Distributions 	/* Change state to freezable */
183*fdd8201dSApple OSS Distributions 	err = memorystatus_control(MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE, getpid(), (uint32_t)1, NULL, 0);
184*fdd8201dSApple OSS Distributions 	T_EXPECT_EQ(KERN_SUCCESS, err, "set pid %d to be freezable", getpid());
185*fdd8201dSApple OSS Distributions 
186*fdd8201dSApple OSS Distributions 	/* Call into kernel to retrieve vm_info and make sure we do not have any decompressions before the test */
187*fdd8201dSApple OSS Distributions 	count = TASK_VM_INFO_COUNT;
188*fdd8201dSApple OSS Distributions 	err = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vm_info, &count);
189*fdd8201dSApple OSS Distributions 	T_EXPECT_EQ(count, TASK_VM_INFO_COUNT, "count == TASK_VM_INFO_COUNT: %d", count);
190*fdd8201dSApple OSS Distributions 	T_EXPECT_EQ_INT(0, err, "task_info(TASK_VM_INFO) returned 0");
191*fdd8201dSApple OSS Distributions 	T_EXPECT_EQ_INT(0, vm_info.decompressions, "Expected 0 decompressions before test starts");
192*fdd8201dSApple OSS Distributions 
193*fdd8201dSApple OSS Distributions 	/* Thread data */
194*fdd8201dSApple OSS Distributions 	threads = malloc(sizeof(pthread_t) * (size_t)ncpu);
195*fdd8201dSApple OSS Distributions 	targs = malloc(sizeof(struct thread_args) * (size_t)ncpu);
196*fdd8201dSApple OSS Distributions 
197*fdd8201dSApple OSS Distributions 	T_SETUPEND;
198*fdd8201dSApple OSS Distributions 
199*fdd8201dSApple OSS Distributions 	/* Phase 1: create threads to write to malloc memory */
200*fdd8201dSApple OSS Distributions 	create_threads(ncpu, threads, targs);
201*fdd8201dSApple OSS Distributions 	atomic_fetch_add(&phase, 1);
202*fdd8201dSApple OSS Distributions 
203*fdd8201dSApple OSS Distributions 	/* Wait for all threads to dirty their malloc pages */
204*fdd8201dSApple OSS Distributions 	while (atomic_load(&thread_malloc_count) != ncpu) {
205*fdd8201dSApple OSS Distributions 		sleep(1);
206*fdd8201dSApple OSS Distributions 	}
207*fdd8201dSApple OSS Distributions 	T_EXPECT_EQ(ncpu, atomic_load(&thread_malloc_count), "%d threads finished writing to malloc pages\n", ncpu);
208*fdd8201dSApple OSS Distributions 
209*fdd8201dSApple OSS Distributions 	/* Launch freezer to compress the dirty pages */
210*fdd8201dSApple OSS Distributions 	T_LOG("Running freezer to compress pages for pid %d", getpid());
211*fdd8201dSApple OSS Distributions 	freeze_pid(getpid());
212*fdd8201dSApple OSS Distributions 
213*fdd8201dSApple OSS Distributions 	/* Phase 2: triger decompression in threads */
214*fdd8201dSApple OSS Distributions 	atomic_fetch_add(&phase, 1);
215*fdd8201dSApple OSS Distributions 
216*fdd8201dSApple OSS Distributions 	/* Wait for all threads to decompress their malloc pages */
217*fdd8201dSApple OSS Distributions 	while (atomic_load(&thread_thawed_count) != ncpu) {
218*fdd8201dSApple OSS Distributions 		sleep(1);
219*fdd8201dSApple OSS Distributions 	}
220*fdd8201dSApple OSS Distributions 
221*fdd8201dSApple OSS Distributions 	/* Phase 3: Call into kernel to retrieve vm_info and to get the updated decompressions counter */
222*fdd8201dSApple OSS Distributions 	count = TASK_VM_INFO_COUNT;
223*fdd8201dSApple OSS Distributions 	err = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vm_info, &count);
224*fdd8201dSApple OSS Distributions 	T_EXPECT_EQ(count, TASK_VM_INFO_COUNT, "count == TASK_VM_INFO_COUNT: %d", count);
225*fdd8201dSApple OSS Distributions 	T_EXPECT_EQ(0, err, "task_info(TASK_VM_INFO) returned 0");
226*fdd8201dSApple OSS Distributions 
227*fdd8201dSApple OSS Distributions 	/* Make sure this task has decompressed at least all of the dirtied memory */
228*fdd8201dSApple OSS Distributions 	T_EXPECT_GE_INT(vm_info.decompressions, npages, "decompressed %d pages (>= heap pages: %d)", vm_info.decompressions, npages);
229*fdd8201dSApple OSS Distributions 	T_PASS("Correctly retrieve per-task decompressions stats");
230*fdd8201dSApple OSS Distributions 
231*fdd8201dSApple OSS Distributions 	/* Cleanup */
232*fdd8201dSApple OSS Distributions 	join_threads(ncpu, threads);
233*fdd8201dSApple OSS Distributions 	free(threads);
234*fdd8201dSApple OSS Distributions 	free(targs);
235*fdd8201dSApple OSS Distributions }
236