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