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