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