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