xref: /xnu-10002.81.5/tests/memorystatus_zone_test.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions #include <stdio.h>
2*5e3eaea3SApple OSS Distributions #include <mach/mach_vm.h>
3*5e3eaea3SApple OSS Distributions #include <mach/mach_port.h>
4*5e3eaea3SApple OSS Distributions #include <mach/mach_host.h>
5*5e3eaea3SApple OSS Distributions #include <mach/mach_error.h>
6*5e3eaea3SApple OSS Distributions #include <mach-o/dyld.h>
7*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
8*5e3eaea3SApple OSS Distributions #include <sys/kdebug.h>
9*5e3eaea3SApple OSS Distributions #include <sys/mman.h>
10*5e3eaea3SApple OSS Distributions #include <sys/kern_memorystatus.h>
11*5e3eaea3SApple OSS Distributions #include <ktrace/session.h>
12*5e3eaea3SApple OSS Distributions #include <dispatch/private.h>
13*5e3eaea3SApple OSS Distributions 
14*5e3eaea3SApple OSS Distributions #ifdef T_NAMESPACE
15*5e3eaea3SApple OSS Distributions #undef T_NAMESPACE
16*5e3eaea3SApple OSS Distributions #endif
17*5e3eaea3SApple OSS Distributions #include <darwintest.h>
18*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
19*5e3eaea3SApple OSS Distributions 
20*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
21*5e3eaea3SApple OSS Distributions 	T_META_NAMESPACE("xnu.vm"),
22*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
23*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"),
24*5e3eaea3SApple OSS Distributions 	T_META_CHECK_LEAKS(false)
25*5e3eaea3SApple OSS Distributions 	);
26*5e3eaea3SApple OSS Distributions 
27*5e3eaea3SApple OSS Distributions #define TIMEOUT_SECS                                    10 * 60 /* abort if test takes > 10 minutes */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
30*5e3eaea3SApple OSS Distributions #define ALLOCATION_SIZE_VM_REGION                       (16*1024)               /* 16 KB */
31*5e3eaea3SApple OSS Distributions #define ALLOCATION_SIZE_VM_OBJECT                       ALLOCATION_SIZE_VM_REGION
32*5e3eaea3SApple OSS Distributions #else
33*5e3eaea3SApple OSS Distributions #define ALLOCATION_SIZE_VM_REGION                       (1024*1024*100) /* 100 MB */
34*5e3eaea3SApple OSS Distributions #define ALLOCATION_SIZE_VM_OBJECT                       (16*1024)               /* 16 KB */
35*5e3eaea3SApple OSS Distributions #endif
36*5e3eaea3SApple OSS Distributions #define MAX_CHILD_PROCS                                 100
37*5e3eaea3SApple OSS Distributions 
38*5e3eaea3SApple OSS Distributions #define NUM_GIVE_BACK                                   5
39*5e3eaea3SApple OSS Distributions #define NUM_GIVE_BACK_PORTS                             20
40*5e3eaea3SApple OSS Distributions 
41*5e3eaea3SApple OSS Distributions /* 60% is too high on bridgeOS to achieve without vm-pageshortage jetsams. Set it to 40%. */
42*5e3eaea3SApple OSS Distributions #if TARGET_OS_BRIDGE
43*5e3eaea3SApple OSS Distributions #define ZONEMAP_JETSAM_LIMIT_SYSCTL                     "kern.zone_map_jetsam_limit=40"
44*5e3eaea3SApple OSS Distributions #else
45*5e3eaea3SApple OSS Distributions #define ZONEMAP_JETSAM_LIMIT_SYSCTL                     "kern.zone_map_jetsam_limit=60"
46*5e3eaea3SApple OSS Distributions #endif
47*5e3eaea3SApple OSS Distributions 
48*5e3eaea3SApple OSS Distributions #define VME_ZONE_TEST_OPT                               "allocate_vm_regions"
49*5e3eaea3SApple OSS Distributions #define VM_OBJECTS_ZONE_TEST_OPT                        "allocate_vm_objects"
50*5e3eaea3SApple OSS Distributions #define GENERIC_ZONE_TEST_OPT                           "allocate_from_generic_zone"
51*5e3eaea3SApple OSS Distributions 
52*5e3eaea3SApple OSS Distributions #define VME_ZONE                                        "VM map entries"
53*5e3eaea3SApple OSS Distributions #define VMOBJECTS_ZONE                                  "vm objects"
54*5e3eaea3SApple OSS Distributions #define VMENTRY_TO_VMOBJECT_COMPARISON_RATIO            98
55*5e3eaea3SApple OSS Distributions 
56*5e3eaea3SApple OSS Distributions #define VM_TAG1                                         100
57*5e3eaea3SApple OSS Distributions #define VM_TAG2                                         101
58*5e3eaea3SApple OSS Distributions 
59*5e3eaea3SApple OSS Distributions enum {
60*5e3eaea3SApple OSS Distributions 	VME_ZONE_TEST = 0,
61*5e3eaea3SApple OSS Distributions 	VM_OBJECTS_ZONE_TEST,
62*5e3eaea3SApple OSS Distributions 	GENERIC_ZONE_TEST,
63*5e3eaea3SApple OSS Distributions };
64*5e3eaea3SApple OSS Distributions 
65*5e3eaea3SApple OSS Distributions typedef struct test_config_struct {
66*5e3eaea3SApple OSS Distributions 	int test_index;
67*5e3eaea3SApple OSS Distributions 	int num_zones;
68*5e3eaea3SApple OSS Distributions 	const char *helper_func;
69*5e3eaea3SApple OSS Distributions 	mach_zone_name_array_t zone_names;
70*5e3eaea3SApple OSS Distributions } test_config_struct;
71*5e3eaea3SApple OSS Distributions 
72*5e3eaea3SApple OSS Distributions static test_config_struct current_test;
73*5e3eaea3SApple OSS Distributions static dispatch_source_t ds_signal = NULL;
74*5e3eaea3SApple OSS Distributions static dispatch_source_t ds_timer = NULL;
75*5e3eaea3SApple OSS Distributions static dispatch_queue_t dq_spawn = NULL;
76*5e3eaea3SApple OSS Distributions static ktrace_session_t session = NULL;
77*5e3eaea3SApple OSS Distributions 
78*5e3eaea3SApple OSS Distributions static mach_zone_info_array_t zone_info_array = NULL;
79*5e3eaea3SApple OSS Distributions static mach_zone_name_t largest_zone_name;
80*5e3eaea3SApple OSS Distributions static mach_zone_info_t largest_zone_info;
81*5e3eaea3SApple OSS Distributions 
82*5e3eaea3SApple OSS Distributions static pthread_mutex_t test_mtx = PTHREAD_MUTEX_INITIALIZER;   /* protects the next 3 things */
83*5e3eaea3SApple OSS Distributions static bool test_ending = false;
84*5e3eaea3SApple OSS Distributions static int num_children = 0;
85*5e3eaea3SApple OSS Distributions static pid_t child_pids[MAX_CHILD_PROCS];
86*5e3eaea3SApple OSS Distributions 
87*5e3eaea3SApple OSS Distributions static char testpath[PATH_MAX];
88*5e3eaea3SApple OSS Distributions static void allocate_vm_stuff(int);
89*5e3eaea3SApple OSS Distributions static void allocate_from_generic_zone(void);
90*5e3eaea3SApple OSS Distributions static void begin_test_teardown(void);
91*5e3eaea3SApple OSS Distributions static void cleanup_and_end_test(void);
92*5e3eaea3SApple OSS Distributions static void setup_ktrace_session(void);
93*5e3eaea3SApple OSS Distributions static void spawn_child_process(void);
94*5e3eaea3SApple OSS Distributions static void run_test(void);
95*5e3eaea3SApple OSS Distributions static bool verify_generic_jetsam_criteria(void);
96*5e3eaea3SApple OSS Distributions static bool vme_zone_compares_to_vm_objects(void);
97*5e3eaea3SApple OSS Distributions static void query_zone_info(void);
98*5e3eaea3SApple OSS Distributions static void print_zone_info(mach_zone_name_t *zn, mach_zone_info_t *zi);
99*5e3eaea3SApple OSS Distributions 
100*5e3eaea3SApple OSS Distributions extern void mach_zone_force_gc(host_t host);
101*5e3eaea3SApple OSS Distributions extern kern_return_t mach_zone_info_for_largest_zone(
102*5e3eaea3SApple OSS Distributions 	host_priv_t host,
103*5e3eaea3SApple OSS Distributions 	mach_zone_name_t *name,
104*5e3eaea3SApple OSS Distributions 	mach_zone_info_t *info
105*5e3eaea3SApple OSS Distributions 	);
106*5e3eaea3SApple OSS Distributions 
107*5e3eaea3SApple OSS Distributions static bool
check_time(time_t start,int timeout)108*5e3eaea3SApple OSS Distributions check_time(time_t start, int timeout)
109*5e3eaea3SApple OSS Distributions {
110*5e3eaea3SApple OSS Distributions 	return start + timeout < time(NULL);
111*5e3eaea3SApple OSS Distributions }
112*5e3eaea3SApple OSS Distributions 
113*5e3eaea3SApple OSS Distributions /*
114*5e3eaea3SApple OSS Distributions  * flag values for allocate_vm_stuff()
115*5e3eaea3SApple OSS Distributions  */
116*5e3eaea3SApple OSS Distributions #define REGIONS 1
117*5e3eaea3SApple OSS Distributions #define OBJECTS 2
118*5e3eaea3SApple OSS Distributions 
119*5e3eaea3SApple OSS Distributions static void
allocate_vm_stuff(int flags)120*5e3eaea3SApple OSS Distributions allocate_vm_stuff(int flags)
121*5e3eaea3SApple OSS Distributions {
122*5e3eaea3SApple OSS Distributions 	uint64_t alloc_size, i;
123*5e3eaea3SApple OSS Distributions 	time_t start = time(NULL);
124*5e3eaea3SApple OSS Distributions 	mach_vm_address_t give_back[NUM_GIVE_BACK];
125*5e3eaea3SApple OSS Distributions 	char *msg;
126*5e3eaea3SApple OSS Distributions 
127*5e3eaea3SApple OSS Distributions 	if (flags == REGIONS) {
128*5e3eaea3SApple OSS Distributions 		alloc_size = ALLOCATION_SIZE_VM_REGION;
129*5e3eaea3SApple OSS Distributions 		msg = "";
130*5e3eaea3SApple OSS Distributions 	} else {
131*5e3eaea3SApple OSS Distributions 		alloc_size = ALLOCATION_SIZE_VM_OBJECT;
132*5e3eaea3SApple OSS Distributions 		msg = " each region backed by a VM object";
133*5e3eaea3SApple OSS Distributions 	}
134*5e3eaea3SApple OSS Distributions 
135*5e3eaea3SApple OSS Distributions 	printf("[%d] Allocating VM regions, each of size %lld KB%s\n", getpid(), (alloc_size >> 10), msg);
136*5e3eaea3SApple OSS Distributions 
137*5e3eaea3SApple OSS Distributions 	for (i = 0;; i++) {
138*5e3eaea3SApple OSS Distributions 		mach_vm_address_t addr = (mach_vm_address_t)NULL;
139*5e3eaea3SApple OSS Distributions 
140*5e3eaea3SApple OSS Distributions 		/* Alternate VM tags between consecutive regions to prevent coalescing */
141*5e3eaea3SApple OSS Distributions 		int vmflags = VM_MAKE_TAG((i % 2)? VM_TAG1: VM_TAG2) | VM_FLAGS_ANYWHERE;
142*5e3eaea3SApple OSS Distributions 
143*5e3eaea3SApple OSS Distributions 		if ((mach_vm_allocate(mach_task_self(), &addr, (mach_vm_size_t)alloc_size, vmflags)) != KERN_SUCCESS) {
144*5e3eaea3SApple OSS Distributions 			break;
145*5e3eaea3SApple OSS Distributions 		}
146*5e3eaea3SApple OSS Distributions 
147*5e3eaea3SApple OSS Distributions 		/*
148*5e3eaea3SApple OSS Distributions 		 * If interested in objects, touch the region so the VM object is created,
149*5e3eaea3SApple OSS Distributions 		 * then free this page. Keeps us from holding a lot of dirty pages.
150*5e3eaea3SApple OSS Distributions 		 */
151*5e3eaea3SApple OSS Distributions 		if (flags == OBJECTS) {
152*5e3eaea3SApple OSS Distributions 			*((int *)addr) = 0;
153*5e3eaea3SApple OSS Distributions 			madvise((void *)addr, (size_t)alloc_size, MADV_FREE);
154*5e3eaea3SApple OSS Distributions 		}
155*5e3eaea3SApple OSS Distributions 
156*5e3eaea3SApple OSS Distributions 		if (check_time(start, TIMEOUT_SECS)) {
157*5e3eaea3SApple OSS Distributions 			printf("[%d] child timeout during allocations\n", getpid());
158*5e3eaea3SApple OSS Distributions 			exit(0);
159*5e3eaea3SApple OSS Distributions 		}
160*5e3eaea3SApple OSS Distributions 
161*5e3eaea3SApple OSS Distributions 		if (i < NUM_GIVE_BACK) {
162*5e3eaea3SApple OSS Distributions 			give_back[i] = addr;
163*5e3eaea3SApple OSS Distributions 		}
164*5e3eaea3SApple OSS Distributions 	}
165*5e3eaea3SApple OSS Distributions 
166*5e3eaea3SApple OSS Distributions 	/* return some of the resource to avoid O-O-M problems */
167*5e3eaea3SApple OSS Distributions 	for (uint64_t j = 0; j < NUM_GIVE_BACK && j < i; ++j) {
168*5e3eaea3SApple OSS Distributions 		mach_vm_deallocate(mach_task_self(), give_back[j], (mach_vm_size_t)alloc_size);
169*5e3eaea3SApple OSS Distributions 	}
170*5e3eaea3SApple OSS Distributions 
171*5e3eaea3SApple OSS Distributions 	printf("[%d] Number of allocations: %lld\n", getpid(), i);
172*5e3eaea3SApple OSS Distributions 
173*5e3eaea3SApple OSS Distributions 	/* Signal to the parent that we're done allocating */
174*5e3eaea3SApple OSS Distributions 	kill(getppid(), SIGUSR1);
175*5e3eaea3SApple OSS Distributions 
176*5e3eaea3SApple OSS Distributions 	while (1) {
177*5e3eaea3SApple OSS Distributions 		usleep(500 * 1000);
178*5e3eaea3SApple OSS Distributions 		/* Exit if parent has exited. Ensures child processes don't linger around after the test exits */
179*5e3eaea3SApple OSS Distributions 		if (getppid() == 1) {
180*5e3eaea3SApple OSS Distributions 			exit(0);
181*5e3eaea3SApple OSS Distributions 		}
182*5e3eaea3SApple OSS Distributions 		if (check_time(start, TIMEOUT_SECS)) {
183*5e3eaea3SApple OSS Distributions 			printf("[%d] child timeout while waiting\n", getpid());
184*5e3eaea3SApple OSS Distributions 			exit(0);
185*5e3eaea3SApple OSS Distributions 		}
186*5e3eaea3SApple OSS Distributions 	}
187*5e3eaea3SApple OSS Distributions }
188*5e3eaea3SApple OSS Distributions 
189*5e3eaea3SApple OSS Distributions 
190*5e3eaea3SApple OSS Distributions static void
allocate_from_generic_zone(void)191*5e3eaea3SApple OSS Distributions allocate_from_generic_zone(void)
192*5e3eaea3SApple OSS Distributions {
193*5e3eaea3SApple OSS Distributions 	uint64_t i = 0;
194*5e3eaea3SApple OSS Distributions 	time_t start = time(NULL);
195*5e3eaea3SApple OSS Distributions 	mach_port_t give_back[NUM_GIVE_BACK_PORTS];
196*5e3eaea3SApple OSS Distributions 	int old_limit = 0;
197*5e3eaea3SApple OSS Distributions 
198*5e3eaea3SApple OSS Distributions 	printf("[%d] Allocating mach_ports\n", getpid());
199*5e3eaea3SApple OSS Distributions 
200*5e3eaea3SApple OSS Distributions 	size_t size = sizeof(old_limit);
201*5e3eaea3SApple OSS Distributions 	int kr = sysctlbyname("machdep.max_port_table_size", &old_limit, &size, NULL, 0);
202*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kr, "sysctl kern.max_port_table_size failed");
203*5e3eaea3SApple OSS Distributions 	T_LOG("machdep.max_port_table_size = %d", old_limit);
204*5e3eaea3SApple OSS Distributions 
205*5e3eaea3SApple OSS Distributions 	/* Avoid hitting the resource limit exception */
206*5e3eaea3SApple OSS Distributions 	uint64_t limit = (uint64_t)(old_limit * 7 / 8);
207*5e3eaea3SApple OSS Distributions 
208*5e3eaea3SApple OSS Distributions 	for (i = 0; i < limit; i++) {
209*5e3eaea3SApple OSS Distributions 		mach_port_t port;
210*5e3eaea3SApple OSS Distributions 
211*5e3eaea3SApple OSS Distributions 		if ((mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port)) != KERN_SUCCESS) {
212*5e3eaea3SApple OSS Distributions 			break;
213*5e3eaea3SApple OSS Distributions 		}
214*5e3eaea3SApple OSS Distributions 
215*5e3eaea3SApple OSS Distributions 		if (check_time(start, TIMEOUT_SECS)) {
216*5e3eaea3SApple OSS Distributions 			printf("[%d] child timeout during allocations\n", getpid());
217*5e3eaea3SApple OSS Distributions 			exit(0);
218*5e3eaea3SApple OSS Distributions 		}
219*5e3eaea3SApple OSS Distributions 
220*5e3eaea3SApple OSS Distributions 		if (i < NUM_GIVE_BACK_PORTS) {
221*5e3eaea3SApple OSS Distributions 			give_back[i] = port;
222*5e3eaea3SApple OSS Distributions 		}
223*5e3eaea3SApple OSS Distributions 	}
224*5e3eaea3SApple OSS Distributions 
225*5e3eaea3SApple OSS Distributions 	/* return some of the resource to avoid O-O-M problems */
226*5e3eaea3SApple OSS Distributions 	for (uint64_t j = 0; j < NUM_GIVE_BACK_PORTS && j < i; ++j) {
227*5e3eaea3SApple OSS Distributions 		int ret;
228*5e3eaea3SApple OSS Distributions 		ret = mach_port_mod_refs(mach_task_self(), give_back[j], MACH_PORT_RIGHT_RECEIVE, -1);
229*5e3eaea3SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(ret, "mach_port_mod_refs(RECV_RIGHT, -1)");
230*5e3eaea3SApple OSS Distributions 	}
231*5e3eaea3SApple OSS Distributions 	printf("[%d] Number of allocations: %lld\n", getpid(), i);
232*5e3eaea3SApple OSS Distributions 
233*5e3eaea3SApple OSS Distributions 	/* Signal to the parent that we're done allocating */
234*5e3eaea3SApple OSS Distributions 	kill(getppid(), SIGUSR1);
235*5e3eaea3SApple OSS Distributions 
236*5e3eaea3SApple OSS Distributions 	while (1) {
237*5e3eaea3SApple OSS Distributions 		usleep(500 * 1000);
238*5e3eaea3SApple OSS Distributions 		/* Exit if parent has exited. Ensures child processes don't linger around after the test exits */
239*5e3eaea3SApple OSS Distributions 		if (getppid() == 1) {
240*5e3eaea3SApple OSS Distributions 			exit(0);
241*5e3eaea3SApple OSS Distributions 		}
242*5e3eaea3SApple OSS Distributions 
243*5e3eaea3SApple OSS Distributions 		if (check_time(start, TIMEOUT_SECS)) {
244*5e3eaea3SApple OSS Distributions 			printf("[%d] child timeout while waiting\n", getpid());
245*5e3eaea3SApple OSS Distributions 			exit(0);
246*5e3eaea3SApple OSS Distributions 		}
247*5e3eaea3SApple OSS Distributions 	}
248*5e3eaea3SApple OSS Distributions }
249*5e3eaea3SApple OSS Distributions 
250*5e3eaea3SApple OSS Distributions static void
print_zone_info(mach_zone_name_t * zn,mach_zone_info_t * zi)251*5e3eaea3SApple OSS Distributions print_zone_info(mach_zone_name_t *zn, mach_zone_info_t *zi)
252*5e3eaea3SApple OSS Distributions {
253*5e3eaea3SApple OSS Distributions 	T_LOG("ZONE NAME: %-35sSIZE: %-25lluELEMENTS: %llu",
254*5e3eaea3SApple OSS Distributions 	    zn->mzn_name, zi->mzi_cur_size, zi->mzi_count);
255*5e3eaea3SApple OSS Distributions }
256*5e3eaea3SApple OSS Distributions 
257*5e3eaea3SApple OSS Distributions static time_t main_start;
258*5e3eaea3SApple OSS Distributions 
259*5e3eaea3SApple OSS Distributions static void
query_zone_info(void)260*5e3eaea3SApple OSS Distributions query_zone_info(void)
261*5e3eaea3SApple OSS Distributions {
262*5e3eaea3SApple OSS Distributions 	int i;
263*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
264*5e3eaea3SApple OSS Distributions 	static uint64_t num_calls = 0;
265*5e3eaea3SApple OSS Distributions 
266*5e3eaea3SApple OSS Distributions 	if (check_time(main_start, TIMEOUT_SECS)) {
267*5e3eaea3SApple OSS Distributions 		T_ASSERT_FAIL("Global timeout expired");
268*5e3eaea3SApple OSS Distributions 	}
269*5e3eaea3SApple OSS Distributions 	for (i = 0; i < current_test.num_zones; i++) {
270*5e3eaea3SApple OSS Distributions 		kr = mach_zone_info_for_zone(mach_host_self(), current_test.zone_names[i], &(zone_info_array[i]));
271*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_zone_info_for_zone(%s) returned %d [%s]", current_test.zone_names[i].mzn_name, kr, mach_error_string(kr));
272*5e3eaea3SApple OSS Distributions 	}
273*5e3eaea3SApple OSS Distributions 	kr = mach_zone_info_for_largest_zone(mach_host_self(), &largest_zone_name, &largest_zone_info);
274*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_zone_info_for_largest_zone returned %d [%s]", kr, mach_error_string(kr));
275*5e3eaea3SApple OSS Distributions 
276*5e3eaea3SApple OSS Distributions 	num_calls++;
277*5e3eaea3SApple OSS Distributions 	if (num_calls % 5 != 0) {
278*5e3eaea3SApple OSS Distributions 		return;
279*5e3eaea3SApple OSS Distributions 	}
280*5e3eaea3SApple OSS Distributions 
281*5e3eaea3SApple OSS Distributions 	/* Print out size and element count for zones relevant to the test */
282*5e3eaea3SApple OSS Distributions 	for (i = 0; i < current_test.num_zones; i++) {
283*5e3eaea3SApple OSS Distributions 		print_zone_info(&(current_test.zone_names[i]), &(zone_info_array[i]));
284*5e3eaea3SApple OSS Distributions 	}
285*5e3eaea3SApple OSS Distributions }
286*5e3eaea3SApple OSS Distributions 
287*5e3eaea3SApple OSS Distributions static bool
vme_zone_compares_to_vm_objects(void)288*5e3eaea3SApple OSS Distributions vme_zone_compares_to_vm_objects(void)
289*5e3eaea3SApple OSS Distributions {
290*5e3eaea3SApple OSS Distributions 	int i;
291*5e3eaea3SApple OSS Distributions 	uint64_t vm_object_element_count = 0, vm_map_entry_element_count = 0;
292*5e3eaea3SApple OSS Distributions 
293*5e3eaea3SApple OSS Distributions 	T_LOG("Comparing element counts of \"VM map entries\" and \"vm objects\" zones");
294*5e3eaea3SApple OSS Distributions 	for (i = 0; i < current_test.num_zones; i++) {
295*5e3eaea3SApple OSS Distributions 		if (!strcmp(current_test.zone_names[i].mzn_name, VME_ZONE)) {
296*5e3eaea3SApple OSS Distributions 			vm_map_entry_element_count = zone_info_array[i].mzi_count;
297*5e3eaea3SApple OSS Distributions 		} else if (!strcmp(current_test.zone_names[i].mzn_name, VMOBJECTS_ZONE)) {
298*5e3eaea3SApple OSS Distributions 			vm_object_element_count = zone_info_array[i].mzi_count;
299*5e3eaea3SApple OSS Distributions 		}
300*5e3eaea3SApple OSS Distributions 		print_zone_info(&(current_test.zone_names[i]), &(zone_info_array[i]));
301*5e3eaea3SApple OSS Distributions 	}
302*5e3eaea3SApple OSS Distributions 
303*5e3eaea3SApple OSS Distributions 	T_LOG("# VM map entries as percentage of # vm objects = %llu", (vm_map_entry_element_count * 100) / vm_object_element_count);
304*5e3eaea3SApple OSS Distributions 	if (vm_map_entry_element_count >= ((vm_object_element_count * VMENTRY_TO_VMOBJECT_COMPARISON_RATIO) / 100)) {
305*5e3eaea3SApple OSS Distributions 		T_LOG("Number of VM map entries is comparable to vm objects\n\n");
306*5e3eaea3SApple OSS Distributions 		return true;
307*5e3eaea3SApple OSS Distributions 	}
308*5e3eaea3SApple OSS Distributions 	T_LOG("Number of VM map entries is NOT comparable to vm objects\n\n");
309*5e3eaea3SApple OSS Distributions 	return false;
310*5e3eaea3SApple OSS Distributions }
311*5e3eaea3SApple OSS Distributions 
312*5e3eaea3SApple OSS Distributions static bool
verify_generic_jetsam_criteria(void)313*5e3eaea3SApple OSS Distributions verify_generic_jetsam_criteria(void)
314*5e3eaea3SApple OSS Distributions {
315*5e3eaea3SApple OSS Distributions 	T_LOG("Largest zone info");
316*5e3eaea3SApple OSS Distributions 	print_zone_info(&largest_zone_name, &largest_zone_info);
317*5e3eaea3SApple OSS Distributions 
318*5e3eaea3SApple OSS Distributions 	/* If VM map entries is not the largest zone */
319*5e3eaea3SApple OSS Distributions 	if (strcmp(largest_zone_name.mzn_name, VME_ZONE)) {
320*5e3eaea3SApple OSS Distributions 		/* If vm objects is the largest zone and the VM map entries zone had comparable # of elements, return false */
321*5e3eaea3SApple OSS Distributions 		if (!strcmp(largest_zone_name.mzn_name, VMOBJECTS_ZONE) && vme_zone_compares_to_vm_objects()) {
322*5e3eaea3SApple OSS Distributions 			return false;
323*5e3eaea3SApple OSS Distributions 		}
324*5e3eaea3SApple OSS Distributions 		return true;
325*5e3eaea3SApple OSS Distributions 	}
326*5e3eaea3SApple OSS Distributions 	return false;
327*5e3eaea3SApple OSS Distributions }
328*5e3eaea3SApple OSS Distributions 
329*5e3eaea3SApple OSS Distributions static void
begin_test_teardown(void)330*5e3eaea3SApple OSS Distributions begin_test_teardown(void)
331*5e3eaea3SApple OSS Distributions {
332*5e3eaea3SApple OSS Distributions 	int ret, old_limit = 95;
333*5e3eaea3SApple OSS Distributions 
334*5e3eaea3SApple OSS Distributions 	/*
335*5e3eaea3SApple OSS Distributions 	 * Restore kern.zone_map_jetsam_limit to the default high value, to prevent further jetsams.
336*5e3eaea3SApple OSS Distributions 	 * We should change the value of old_limit if ZONE_MAP_JETSAM_LIMIT_DEFAULT changes in the kernel.
337*5e3eaea3SApple OSS Distributions 	 * We don't have a way to capture what the original value was before the test, because the
338*5e3eaea3SApple OSS Distributions 	 * T_META_SYSCTL_INT macro will have changed the value before the test starts running.
339*5e3eaea3SApple OSS Distributions 	 */
340*5e3eaea3SApple OSS Distributions 	ret = sysctlbyname("kern.zone_map_jetsam_limit", NULL, NULL, &old_limit, sizeof(old_limit));
341*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl kern.zone_map_jetsam_limit failed");
342*5e3eaea3SApple OSS Distributions 	T_LOG("kern.zone_map_jetsam_limit set to %d%%", old_limit);
343*5e3eaea3SApple OSS Distributions 
344*5e3eaea3SApple OSS Distributions 
345*5e3eaea3SApple OSS Distributions 	/* End ktrace session */
346*5e3eaea3SApple OSS Distributions 	if (session != NULL) {
347*5e3eaea3SApple OSS Distributions 		T_LOG("Ending ktrace session...");
348*5e3eaea3SApple OSS Distributions 		ktrace_end(session, 1);
349*5e3eaea3SApple OSS Distributions 	}
350*5e3eaea3SApple OSS Distributions 
351*5e3eaea3SApple OSS Distributions 	dispatch_sync(dq_spawn, ^{
352*5e3eaea3SApple OSS Distributions 		T_LOG("Cancelling dispatch sources...");
353*5e3eaea3SApple OSS Distributions 
354*5e3eaea3SApple OSS Distributions 		/* Disable the timer that queries and prints zone info periodically */
355*5e3eaea3SApple OSS Distributions 		if (ds_timer != NULL) {
356*5e3eaea3SApple OSS Distributions 		        dispatch_source_cancel(ds_timer);
357*5e3eaea3SApple OSS Distributions 		}
358*5e3eaea3SApple OSS Distributions 
359*5e3eaea3SApple OSS Distributions 		/* Disable signal handler that spawns child processes */
360*5e3eaea3SApple OSS Distributions 		if (ds_signal != NULL) {
361*5e3eaea3SApple OSS Distributions 		        /*
362*5e3eaea3SApple OSS Distributions 		         * No need for a dispatch_source_cancel_and_wait here.
363*5e3eaea3SApple OSS Distributions 		         * We're queueing this on the spawn queue, so no further
364*5e3eaea3SApple OSS Distributions 		         * processes will be spawned after the source is cancelled.
365*5e3eaea3SApple OSS Distributions 		         */
366*5e3eaea3SApple OSS Distributions 		        dispatch_source_cancel(ds_signal);
367*5e3eaea3SApple OSS Distributions 		}
368*5e3eaea3SApple OSS Distributions 	});
369*5e3eaea3SApple OSS Distributions }
370*5e3eaea3SApple OSS Distributions 
371*5e3eaea3SApple OSS Distributions static void
cleanup_and_end_test(void)372*5e3eaea3SApple OSS Distributions cleanup_and_end_test(void)
373*5e3eaea3SApple OSS Distributions {
374*5e3eaea3SApple OSS Distributions 	int i;
375*5e3eaea3SApple OSS Distributions 
376*5e3eaea3SApple OSS Distributions 	/*
377*5e3eaea3SApple OSS Distributions 	 * The atend handler executes on a different dispatch queue.
378*5e3eaea3SApple OSS Distributions 	 * We want to do the cleanup only once.
379*5e3eaea3SApple OSS Distributions 	 */
380*5e3eaea3SApple OSS Distributions 	pthread_mutex_lock(&test_mtx);
381*5e3eaea3SApple OSS Distributions 	if (test_ending) {
382*5e3eaea3SApple OSS Distributions 		pthread_mutex_unlock(&test_mtx);
383*5e3eaea3SApple OSS Distributions 		return;
384*5e3eaea3SApple OSS Distributions 	}
385*5e3eaea3SApple OSS Distributions 	test_ending = TRUE;
386*5e3eaea3SApple OSS Distributions 	pthread_mutex_unlock(&test_mtx);
387*5e3eaea3SApple OSS Distributions 
388*5e3eaea3SApple OSS Distributions 	dispatch_async(dq_spawn, ^{
389*5e3eaea3SApple OSS Distributions 		/*
390*5e3eaea3SApple OSS Distributions 		 * If the test succeeds, we will call dispatch_source_cancel twice, which is fine since
391*5e3eaea3SApple OSS Distributions 		 * the operation is idempotent. Just make sure to not drop all references to the dispatch sources
392*5e3eaea3SApple OSS Distributions 		 * (in this case we're not, we have globals holding references to them), or we can end up with
393*5e3eaea3SApple OSS Distributions 		 * use-after-frees which would be a problem.
394*5e3eaea3SApple OSS Distributions 		 */
395*5e3eaea3SApple OSS Distributions 		/* Disable the timer that queries and prints zone info periodically */
396*5e3eaea3SApple OSS Distributions 		if (ds_timer != NULL) {
397*5e3eaea3SApple OSS Distributions 		        dispatch_source_cancel(ds_timer);
398*5e3eaea3SApple OSS Distributions 		}
399*5e3eaea3SApple OSS Distributions 
400*5e3eaea3SApple OSS Distributions 		/* Disable signal handler that spawns child processes */
401*5e3eaea3SApple OSS Distributions 		if (ds_signal != NULL) {
402*5e3eaea3SApple OSS Distributions 		        dispatch_source_cancel(ds_signal);
403*5e3eaea3SApple OSS Distributions 		}
404*5e3eaea3SApple OSS Distributions 	});
405*5e3eaea3SApple OSS Distributions 
406*5e3eaea3SApple OSS Distributions 	pthread_mutex_lock(&test_mtx);
407*5e3eaea3SApple OSS Distributions 	T_LOG("Number of processes spawned: %d", num_children);
408*5e3eaea3SApple OSS Distributions 	T_LOG("Killing child processes...");
409*5e3eaea3SApple OSS Distributions 
410*5e3eaea3SApple OSS Distributions 	/* Kill all the child processes that were spawned */
411*5e3eaea3SApple OSS Distributions 	for (i = 0; i < num_children; i++) {
412*5e3eaea3SApple OSS Distributions 		pid_t pid = child_pids[i];
413*5e3eaea3SApple OSS Distributions 		int status = 0;
414*5e3eaea3SApple OSS Distributions 
415*5e3eaea3SApple OSS Distributions 		/*
416*5e3eaea3SApple OSS Distributions 		 * Kill and wait for each child to exit
417*5e3eaea3SApple OSS Distributions 		 * Without this we were seeing hw_lock_bit timeouts in BATS.
418*5e3eaea3SApple OSS Distributions 		 */
419*5e3eaea3SApple OSS Distributions 		kill(pid, SIGKILL);
420*5e3eaea3SApple OSS Distributions 		pthread_mutex_unlock(&test_mtx);
421*5e3eaea3SApple OSS Distributions 		if (waitpid(pid, &status, 0) < 0) {
422*5e3eaea3SApple OSS Distributions 			T_LOG("waitpid returned status %d", status);
423*5e3eaea3SApple OSS Distributions 		}
424*5e3eaea3SApple OSS Distributions 		pthread_mutex_lock(&test_mtx);
425*5e3eaea3SApple OSS Distributions 	}
426*5e3eaea3SApple OSS Distributions 	usleep(500 * 1000);
427*5e3eaea3SApple OSS Distributions 
428*5e3eaea3SApple OSS Distributions 	/* Force zone_gc before starting test for another zone or exiting */
429*5e3eaea3SApple OSS Distributions 	mach_zone_force_gc(mach_host_self());
430*5e3eaea3SApple OSS Distributions 
431*5e3eaea3SApple OSS Distributions 	/* End ktrace session */
432*5e3eaea3SApple OSS Distributions 	if (session != NULL) {
433*5e3eaea3SApple OSS Distributions 		ktrace_end(session, 1);
434*5e3eaea3SApple OSS Distributions 	}
435*5e3eaea3SApple OSS Distributions 
436*5e3eaea3SApple OSS Distributions 	if (current_test.num_zones > 0) {
437*5e3eaea3SApple OSS Distributions 		T_LOG("Relevant zone info at the end of the test:");
438*5e3eaea3SApple OSS Distributions 		for (i = 0; i < current_test.num_zones; i++) {
439*5e3eaea3SApple OSS Distributions 			print_zone_info(&(current_test.zone_names[i]), &(zone_info_array[i]));
440*5e3eaea3SApple OSS Distributions 		}
441*5e3eaea3SApple OSS Distributions 	}
442*5e3eaea3SApple OSS Distributions }
443*5e3eaea3SApple OSS Distributions 
444*5e3eaea3SApple OSS Distributions static void
setup_ktrace_session(void)445*5e3eaea3SApple OSS Distributions setup_ktrace_session(void)
446*5e3eaea3SApple OSS Distributions {
447*5e3eaea3SApple OSS Distributions 	int ret = 0;
448*5e3eaea3SApple OSS Distributions 
449*5e3eaea3SApple OSS Distributions 	T_LOG("Setting up ktrace session...");
450*5e3eaea3SApple OSS Distributions 	session = ktrace_session_create();
451*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(session, "ktrace_session_create");
452*5e3eaea3SApple OSS Distributions 
453*5e3eaea3SApple OSS Distributions 	ktrace_set_interactive(session);
454*5e3eaea3SApple OSS Distributions 
455*5e3eaea3SApple OSS Distributions 	ktrace_set_dropped_events_handler(session, ^{
456*5e3eaea3SApple OSS Distributions 		T_FAIL("Dropped ktrace events; might have missed an expected jetsam event. Terminating early.");
457*5e3eaea3SApple OSS Distributions 	});
458*5e3eaea3SApple OSS Distributions 
459*5e3eaea3SApple OSS Distributions 	ktrace_set_completion_handler(session, ^{
460*5e3eaea3SApple OSS Distributions 		ktrace_session_destroy(session);
461*5e3eaea3SApple OSS Distributions 		T_END;
462*5e3eaea3SApple OSS Distributions 	});
463*5e3eaea3SApple OSS Distributions 
464*5e3eaea3SApple OSS Distributions 	/* Listen for memorystatus_do_kill trace events */
465*5e3eaea3SApple OSS Distributions 	ret = ktrace_events_single(session, (BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)), ^(ktrace_event_t event) {
466*5e3eaea3SApple OSS Distributions 		int i;
467*5e3eaea3SApple OSS Distributions 		bool received_jetsam_event = false;
468*5e3eaea3SApple OSS Distributions 
469*5e3eaea3SApple OSS Distributions 		/*
470*5e3eaea3SApple OSS Distributions 		 * libktrace does not support DBG_FUNC_START/END in the event filter. It simply ignores it.
471*5e3eaea3SApple OSS Distributions 		 * So we need to explicitly check for the end event (a successful jetsam kill) here,
472*5e3eaea3SApple OSS Distributions 		 * instead of passing in ((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_START).
473*5e3eaea3SApple OSS Distributions 		 */
474*5e3eaea3SApple OSS Distributions 		if (!(event->debugid & DBG_FUNC_START)) {
475*5e3eaea3SApple OSS Distributions 		        return;
476*5e3eaea3SApple OSS Distributions 		}
477*5e3eaea3SApple OSS Distributions 
478*5e3eaea3SApple OSS Distributions 		/* Check for zone-map-exhaustion jetsam. */
479*5e3eaea3SApple OSS Distributions 		if (event->arg2 == kMemorystatusKilledZoneMapExhaustion) {
480*5e3eaea3SApple OSS Distributions 		        begin_test_teardown();
481*5e3eaea3SApple OSS Distributions 		        T_LOG("[memorystatus_do_kill] jetsam reason: zone-map-exhaustion, pid: %d\n\n", (int)event->arg1);
482*5e3eaea3SApple OSS Distributions 		        if (current_test.test_index == VME_ZONE_TEST || current_test.test_index == VM_OBJECTS_ZONE_TEST) {
483*5e3eaea3SApple OSS Distributions 		                /*
484*5e3eaea3SApple OSS Distributions 		                 * For the VM map entries zone we try to kill the leaking process.
485*5e3eaea3SApple OSS Distributions 		                 * Verify that we jetsammed one of the processes we spawned.
486*5e3eaea3SApple OSS Distributions 		                 *
487*5e3eaea3SApple OSS Distributions 		                 * For the vm objects zone we pick the leaking process via the VM map entries
488*5e3eaea3SApple OSS Distributions 		                 * zone, if the number of vm objects and VM map entries are comparable.
489*5e3eaea3SApple OSS Distributions 		                 * The test simulates this scenario, we should see a targeted jetsam for the
490*5e3eaea3SApple OSS Distributions 		                 * vm objects zone too.
491*5e3eaea3SApple OSS Distributions 		                 */
492*5e3eaea3SApple OSS Distributions 		                pthread_mutex_lock(&test_mtx);
493*5e3eaea3SApple OSS Distributions 		                for (i = 0; i < num_children; i++) {
494*5e3eaea3SApple OSS Distributions 		                        if (child_pids[i] == (pid_t)event->arg1) {
495*5e3eaea3SApple OSS Distributions 		                                received_jetsam_event = true;
496*5e3eaea3SApple OSS Distributions 		                                T_LOG("Received jetsam event for a child");
497*5e3eaea3SApple OSS Distributions 		                                break;
498*5e3eaea3SApple OSS Distributions 					}
499*5e3eaea3SApple OSS Distributions 				}
500*5e3eaea3SApple OSS Distributions 		                pthread_mutex_unlock(&test_mtx);
501*5e3eaea3SApple OSS Distributions 		                /*
502*5e3eaea3SApple OSS Distributions 		                 * If we didn't see a targeted jetsam, verify that the largest zone actually
503*5e3eaea3SApple OSS Distributions 		                 * fulfilled the criteria for generic jetsams.
504*5e3eaea3SApple OSS Distributions 		                 */
505*5e3eaea3SApple OSS Distributions 		                if (!received_jetsam_event && verify_generic_jetsam_criteria()) {
506*5e3eaea3SApple OSS Distributions 		                        received_jetsam_event = true;
507*5e3eaea3SApple OSS Distributions 		                        T_LOG("Did not receive jetsam event for a child, but generic jetsam criteria holds");
508*5e3eaea3SApple OSS Distributions 				}
509*5e3eaea3SApple OSS Distributions 			} else {
510*5e3eaea3SApple OSS Distributions 		                received_jetsam_event = true;
511*5e3eaea3SApple OSS Distributions 		                T_LOG("Received generic jetsam event");
512*5e3eaea3SApple OSS Distributions 			}
513*5e3eaea3SApple OSS Distributions 
514*5e3eaea3SApple OSS Distributions 		        T_QUIET; T_ASSERT_TRUE(received_jetsam_event, "Jetsam event not as expected");
515*5e3eaea3SApple OSS Distributions 		} else {
516*5e3eaea3SApple OSS Distributions 		        /*
517*5e3eaea3SApple OSS Distributions 		         * The test relies on the children being able to send a signal to the parent, to continue spawning new processes
518*5e3eaea3SApple OSS Distributions 		         * that leak more zone memory. If a child is jetsammed for some other reason, the parent can get stuck waiting for
519*5e3eaea3SApple OSS Distributions 		         * a signal from the child, never being able to make progress (We spawn only a single process at a time to rate-limit
520*5e3eaea3SApple OSS Distributions 		         * the zone memory bloat.). If this happens, the test eventually times out. So if a child is jetsammed for some
521*5e3eaea3SApple OSS Distributions 		         * reason other than zone-map-exhaustion, end the test early.
522*5e3eaea3SApple OSS Distributions 		         *
523*5e3eaea3SApple OSS Distributions 		         * This typically happens when we end up triggering vm-pageshortage jetsams before zone-map-exhaustion jetsams.
524*5e3eaea3SApple OSS Distributions 		         * Lowering the zone_map_jetsam_limit if the zone map size was initially low should help with this too.
525*5e3eaea3SApple OSS Distributions 		         * See sysctlbyname("kern.zone_map_jetsam_limit"...) in run_test() below.
526*5e3eaea3SApple OSS Distributions 		         */
527*5e3eaea3SApple OSS Distributions 		        pthread_mutex_lock(&test_mtx);
528*5e3eaea3SApple OSS Distributions 		        for (i = 0; i < num_children; i++) {
529*5e3eaea3SApple OSS Distributions 		                if (child_pids[i] == (pid_t)event->arg1) {
530*5e3eaea3SApple OSS Distributions 		                        begin_test_teardown();
531*5e3eaea3SApple OSS Distributions 		                        T_PASS("Child pid %d was jetsammed due to reason %d. Terminating early.",
532*5e3eaea3SApple OSS Distributions 		                        (int)event->arg1, (int)event->arg2);
533*5e3eaea3SApple OSS Distributions 				}
534*5e3eaea3SApple OSS Distributions 			}
535*5e3eaea3SApple OSS Distributions 		        pthread_mutex_unlock(&test_mtx);
536*5e3eaea3SApple OSS Distributions 		}
537*5e3eaea3SApple OSS Distributions 	});
538*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ret, "ktrace_events_single");
539*5e3eaea3SApple OSS Distributions 
540*5e3eaea3SApple OSS Distributions 	ret = ktrace_start(session, dispatch_get_main_queue());
541*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ret, "ktrace_start");
542*5e3eaea3SApple OSS Distributions }
543*5e3eaea3SApple OSS Distributions 
544*5e3eaea3SApple OSS Distributions static void
query_zone_map_size(uint64_t * current,uint64_t * total)545*5e3eaea3SApple OSS Distributions query_zone_map_size(uint64_t *current, uint64_t *total)
546*5e3eaea3SApple OSS Distributions {
547*5e3eaea3SApple OSS Distributions 	int ret;
548*5e3eaea3SApple OSS Distributions 	uint64_t zstats[2];
549*5e3eaea3SApple OSS Distributions 	size_t zstats_size = sizeof(zstats);
550*5e3eaea3SApple OSS Distributions 
551*5e3eaea3SApple OSS Distributions 	ret = sysctlbyname("kern.zone_map_size_and_capacity", &zstats, &zstats_size, NULL, 0);
552*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl kern.zone_map_size_and_capacity failed");
553*5e3eaea3SApple OSS Distributions 
554*5e3eaea3SApple OSS Distributions 	T_LOG("Zone map capacity: %-30lldZone map size: %lld [%lld%% full]", zstats[1], zstats[0], (zstats[0] * 100) / zstats[1]);
555*5e3eaea3SApple OSS Distributions 
556*5e3eaea3SApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
557*5e3eaea3SApple OSS Distributions 	int memstat_level;
558*5e3eaea3SApple OSS Distributions 	size_t memstat_level_size = sizeof(memstat_level);
559*5e3eaea3SApple OSS Distributions 	ret = sysctlbyname("kern.memorystatus_level", &memstat_level, &memstat_level_size, NULL, 0);
560*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl kern.memorystatus_level failed");
561*5e3eaea3SApple OSS Distributions 
562*5e3eaea3SApple OSS Distributions 	T_LOG("kern.memorystatus_level = %d%%", memstat_level);
563*5e3eaea3SApple OSS Distributions #endif
564*5e3eaea3SApple OSS Distributions 	if (current) {
565*5e3eaea3SApple OSS Distributions 		*current = zstats[0];
566*5e3eaea3SApple OSS Distributions 	}
567*5e3eaea3SApple OSS Distributions 	if (total) {
568*5e3eaea3SApple OSS Distributions 		*total = zstats[1];
569*5e3eaea3SApple OSS Distributions 	}
570*5e3eaea3SApple OSS Distributions }
571*5e3eaea3SApple OSS Distributions 
572*5e3eaea3SApple OSS Distributions static void
spawn_child_process(void)573*5e3eaea3SApple OSS Distributions spawn_child_process(void)
574*5e3eaea3SApple OSS Distributions {
575*5e3eaea3SApple OSS Distributions 	pid_t pid = -1;
576*5e3eaea3SApple OSS Distributions 	char helper_func[50];
577*5e3eaea3SApple OSS Distributions 	char *launch_tool_args[4];
578*5e3eaea3SApple OSS Distributions 
579*5e3eaea3SApple OSS Distributions 	pthread_mutex_lock(&test_mtx);
580*5e3eaea3SApple OSS Distributions 	if (!test_ending) {
581*5e3eaea3SApple OSS Distributions 		if (num_children == MAX_CHILD_PROCS) {
582*5e3eaea3SApple OSS Distributions 			pthread_mutex_unlock(&test_mtx);
583*5e3eaea3SApple OSS Distributions 			T_ASSERT_FAIL("Spawned too many children. Aborting test");
584*5e3eaea3SApple OSS Distributions 			/* not reached */
585*5e3eaea3SApple OSS Distributions 		}
586*5e3eaea3SApple OSS Distributions 
587*5e3eaea3SApple OSS Distributions 		strlcpy(helper_func, current_test.helper_func, sizeof(helper_func));
588*5e3eaea3SApple OSS Distributions 		launch_tool_args[0] = testpath;
589*5e3eaea3SApple OSS Distributions 		launch_tool_args[1] = "-n";
590*5e3eaea3SApple OSS Distributions 		launch_tool_args[2] = helper_func;
591*5e3eaea3SApple OSS Distributions 		launch_tool_args[3] = NULL;
592*5e3eaea3SApple OSS Distributions 
593*5e3eaea3SApple OSS Distributions 		/* Spawn the child process */
594*5e3eaea3SApple OSS Distributions 		int rc = dt_launch_tool(&pid, launch_tool_args, false, NULL, NULL);
595*5e3eaea3SApple OSS Distributions 		if (rc != 0) {
596*5e3eaea3SApple OSS Distributions 			T_LOG("dt_launch tool returned %d with error code %d", rc, errno);
597*5e3eaea3SApple OSS Distributions 		}
598*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "dt_launch_tool");
599*5e3eaea3SApple OSS Distributions 
600*5e3eaea3SApple OSS Distributions 		child_pids[num_children++] = pid;
601*5e3eaea3SApple OSS Distributions 	}
602*5e3eaea3SApple OSS Distributions 	pthread_mutex_unlock(&test_mtx);
603*5e3eaea3SApple OSS Distributions }
604*5e3eaea3SApple OSS Distributions 
605*5e3eaea3SApple OSS Distributions static void
run_test(void)606*5e3eaea3SApple OSS Distributions run_test(void)
607*5e3eaea3SApple OSS Distributions {
608*5e3eaea3SApple OSS Distributions 	uint64_t mem;
609*5e3eaea3SApple OSS Distributions 	uint32_t testpath_buf_size, pages;
610*5e3eaea3SApple OSS Distributions 	int ret, pgsz, old_limit, new_limit = 0;
611*5e3eaea3SApple OSS Distributions 	size_t sysctl_size;
612*5e3eaea3SApple OSS Distributions 	uint64_t zone_cur, zone_tot, zone_target;
613*5e3eaea3SApple OSS Distributions 
614*5e3eaea3SApple OSS Distributions 	T_ATEND(cleanup_and_end_test);
615*5e3eaea3SApple OSS Distributions 	T_SETUPBEGIN;
616*5e3eaea3SApple OSS Distributions 
617*5e3eaea3SApple OSS Distributions 	main_start = time(NULL);
618*5e3eaea3SApple OSS Distributions 
619*5e3eaea3SApple OSS Distributions 	testpath_buf_size = sizeof(testpath);
620*5e3eaea3SApple OSS Distributions 	ret = _NSGetExecutablePath(testpath, &testpath_buf_size);
621*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ret, "_NSGetExecutablePath");
622*5e3eaea3SApple OSS Distributions 	T_LOG("Executable path: %s", testpath);
623*5e3eaea3SApple OSS Distributions 
624*5e3eaea3SApple OSS Distributions 	sysctl_size = sizeof(mem);
625*5e3eaea3SApple OSS Distributions 	ret = sysctlbyname("hw.memsize", &mem, &sysctl_size, NULL, 0);
626*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl hw.memsize failed");
627*5e3eaea3SApple OSS Distributions 	T_LOG("hw.memsize: %llu", mem);
628*5e3eaea3SApple OSS Distributions 
629*5e3eaea3SApple OSS Distributions 	sysctl_size = sizeof(pgsz);
630*5e3eaea3SApple OSS Distributions 	ret = sysctlbyname("vm.pagesize", &pgsz, &sysctl_size, NULL, 0);
631*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl vm.pagesize failed");
632*5e3eaea3SApple OSS Distributions 	T_LOG("vm.pagesize: %d", pgsz);
633*5e3eaea3SApple OSS Distributions 
634*5e3eaea3SApple OSS Distributions 	sysctl_size = sizeof(pages);
635*5e3eaea3SApple OSS Distributions 	ret = sysctlbyname("vm.pages", &pages, &sysctl_size, NULL, 0);
636*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl vm.pages failed");
637*5e3eaea3SApple OSS Distributions 	T_LOG("vm.pages: %d", pages);
638*5e3eaea3SApple OSS Distributions 
639*5e3eaea3SApple OSS Distributions 	sysctl_size = sizeof(old_limit);
640*5e3eaea3SApple OSS Distributions 	ret = sysctlbyname("kern.zone_map_jetsam_limit", &old_limit, &sysctl_size, NULL, 0);
641*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl kern.zone_map_jetsam_limit failed");
642*5e3eaea3SApple OSS Distributions 	T_LOG("kern.zone_map_jetsam_limit: %d", old_limit);
643*5e3eaea3SApple OSS Distributions 
644*5e3eaea3SApple OSS Distributions 	/*
645*5e3eaea3SApple OSS Distributions 	 * In order to start jetsamming "quickly",
646*5e3eaea3SApple OSS Distributions 	 * set up the limit to be about 2x of what the current usage is.
647*5e3eaea3SApple OSS Distributions 	 */
648*5e3eaea3SApple OSS Distributions 	query_zone_map_size(&zone_cur, &zone_tot);
649*5e3eaea3SApple OSS Distributions 	zone_target = zone_cur * 2;
650*5e3eaea3SApple OSS Distributions 
651*5e3eaea3SApple OSS Distributions 	new_limit = (int)howmany(zone_target * 100, zone_tot);
652*5e3eaea3SApple OSS Distributions 
653*5e3eaea3SApple OSS Distributions 	if (new_limit < old_limit) {
654*5e3eaea3SApple OSS Distributions 		/*
655*5e3eaea3SApple OSS Distributions 		 * We should be fine messing with the zone_map_jetsam_limit here, i.e. outside of T_META_SYSCTL_INT.
656*5e3eaea3SApple OSS Distributions 		 * When the test ends, T_META_SYSCTL_INT will restore the zone_map_jetsam_limit to what it was
657*5e3eaea3SApple OSS Distributions 		 * before the test anyway.
658*5e3eaea3SApple OSS Distributions 		 */
659*5e3eaea3SApple OSS Distributions 		ret = sysctlbyname("kern.zone_map_jetsam_limit", NULL, NULL, &new_limit, sizeof(new_limit));
660*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl kern.zone_map_jetsam_limit failed");
661*5e3eaea3SApple OSS Distributions 		T_LOG("kern.zone_map_jetsam_limit set to %d%%", new_limit);
662*5e3eaea3SApple OSS Distributions 	}
663*5e3eaea3SApple OSS Distributions 
664*5e3eaea3SApple OSS Distributions 	zone_info_array = (mach_zone_info_array_t) calloc((unsigned long)current_test.num_zones, sizeof *zone_info_array);
665*5e3eaea3SApple OSS Distributions 
666*5e3eaea3SApple OSS Distributions 	/*
667*5e3eaea3SApple OSS Distributions 	 * If the timeout specified by T_META_TIMEOUT is hit, the atend handler does not get called.
668*5e3eaea3SApple OSS Distributions 	 * So we're queueing a dispatch block to fire after TIMEOUT_SECS seconds, so we can exit cleanly.
669*5e3eaea3SApple OSS Distributions 	 */
670*5e3eaea3SApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, TIMEOUT_SECS * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
671*5e3eaea3SApple OSS Distributions 		T_ASSERT_FAIL("Timed out after %d seconds", TIMEOUT_SECS);
672*5e3eaea3SApple OSS Distributions 	});
673*5e3eaea3SApple OSS Distributions 
674*5e3eaea3SApple OSS Distributions 	/*
675*5e3eaea3SApple OSS Distributions 	 * Create a dispatch source for the signal SIGUSR1. When a child is done allocating zone memory, it
676*5e3eaea3SApple OSS Distributions 	 * sends SIGUSR1 to the parent. Only then does the parent spawn another child. This prevents us from
677*5e3eaea3SApple OSS Distributions 	 * spawning many children at once and creating a lot of memory pressure.
678*5e3eaea3SApple OSS Distributions 	 */
679*5e3eaea3SApple OSS Distributions 	signal(SIGUSR1, SIG_IGN);
680*5e3eaea3SApple OSS Distributions 	dq_spawn = dispatch_queue_create("spawn_queue", DISPATCH_QUEUE_SERIAL);
681*5e3eaea3SApple OSS Distributions 	ds_signal = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dq_spawn);
682*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(ds_signal, "dispatch_source_create: signal");
683*5e3eaea3SApple OSS Distributions 
684*5e3eaea3SApple OSS Distributions 	dispatch_source_set_event_handler(ds_signal, ^{
685*5e3eaea3SApple OSS Distributions 		uint64_t cur, tot;
686*5e3eaea3SApple OSS Distributions 
687*5e3eaea3SApple OSS Distributions 		query_zone_map_size(&cur, &tot);
688*5e3eaea3SApple OSS Distributions 
689*5e3eaea3SApple OSS Distributions 		if (cur + cur / 20 >= zone_target) {
690*5e3eaea3SApple OSS Distributions 		        /*
691*5e3eaea3SApple OSS Distributions 		         * Slow down allocation pace when nearing target.
692*5e3eaea3SApple OSS Distributions 		         */
693*5e3eaea3SApple OSS Distributions 		        sleep(1);
694*5e3eaea3SApple OSS Distributions 		}
695*5e3eaea3SApple OSS Distributions 		spawn_child_process();
696*5e3eaea3SApple OSS Distributions 	});
697*5e3eaea3SApple OSS Distributions 	dispatch_activate(ds_signal);
698*5e3eaea3SApple OSS Distributions 
699*5e3eaea3SApple OSS Distributions 	/* Timer to query jetsam-relevant zone info every second. Print it every 5 seconds. */
700*5e3eaea3SApple OSS Distributions 	ds_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_queue_create("timer_queue", NULL));
701*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(ds_timer, "dispatch_source_create: timer");
702*5e3eaea3SApple OSS Distributions 	dispatch_source_set_timer(ds_timer, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0);
703*5e3eaea3SApple OSS Distributions 
704*5e3eaea3SApple OSS Distributions 	dispatch_source_set_event_handler(ds_timer, ^{
705*5e3eaea3SApple OSS Distributions 		query_zone_info();
706*5e3eaea3SApple OSS Distributions 	});
707*5e3eaea3SApple OSS Distributions 	dispatch_activate(ds_timer);
708*5e3eaea3SApple OSS Distributions 
709*5e3eaea3SApple OSS Distributions 	/* Set up a ktrace session to listen for jetsam events */
710*5e3eaea3SApple OSS Distributions 	setup_ktrace_session();
711*5e3eaea3SApple OSS Distributions 
712*5e3eaea3SApple OSS Distributions 	T_SETUPEND;
713*5e3eaea3SApple OSS Distributions 
714*5e3eaea3SApple OSS Distributions 	/* Spawn the first child process */
715*5e3eaea3SApple OSS Distributions 	T_LOG("Spawning child processes to allocate zone memory...\n\n");
716*5e3eaea3SApple OSS Distributions 	spawn_child_process();
717*5e3eaea3SApple OSS Distributions 
718*5e3eaea3SApple OSS Distributions 	dispatch_main();
719*5e3eaea3SApple OSS Distributions }
720*5e3eaea3SApple OSS Distributions 
721*5e3eaea3SApple OSS Distributions static void
move_to_idle_band(void)722*5e3eaea3SApple OSS Distributions move_to_idle_band(void)
723*5e3eaea3SApple OSS Distributions {
724*5e3eaea3SApple OSS Distributions 	memorystatus_priority_properties_t props;
725*5e3eaea3SApple OSS Distributions 
726*5e3eaea3SApple OSS Distributions 	/*
727*5e3eaea3SApple OSS Distributions 	 * We want to move the processes we spawn into the idle band, so that jetsam can target them first.
728*5e3eaea3SApple OSS Distributions 	 * This prevents other important BATS tasks from getting killed, specially in LTE where we have very few
729*5e3eaea3SApple OSS Distributions 	 * processes running.
730*5e3eaea3SApple OSS Distributions 	 *
731*5e3eaea3SApple OSS Distributions 	 * This is only needed for tests which (are likely to) lead us down the generic jetsam path.
732*5e3eaea3SApple OSS Distributions 	 */
733*5e3eaea3SApple OSS Distributions 	props.priority = JETSAM_PRIORITY_IDLE;
734*5e3eaea3SApple OSS Distributions 	props.user_data = 0;
735*5e3eaea3SApple OSS Distributions 
736*5e3eaea3SApple OSS Distributions 	if (memorystatus_control(MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES, getpid(), 0, &props, sizeof(props))) {
737*5e3eaea3SApple OSS Distributions 		printf("memorystatus call to change jetsam priority failed\n");
738*5e3eaea3SApple OSS Distributions 		exit(-1);
739*5e3eaea3SApple OSS Distributions 	}
740*5e3eaea3SApple OSS Distributions }
741*5e3eaea3SApple OSS Distributions 
742*5e3eaea3SApple OSS Distributions T_HELPER_DECL(allocate_vm_regions, "allocates VM regions")
743*5e3eaea3SApple OSS Distributions {
744*5e3eaea3SApple OSS Distributions 	move_to_idle_band();
745*5e3eaea3SApple OSS Distributions 	allocate_vm_stuff(REGIONS);
746*5e3eaea3SApple OSS Distributions }
747*5e3eaea3SApple OSS Distributions 
748*5e3eaea3SApple OSS Distributions T_HELPER_DECL(allocate_vm_objects, "allocates VM objects and VM regions")
749*5e3eaea3SApple OSS Distributions {
750*5e3eaea3SApple OSS Distributions 	move_to_idle_band();
751*5e3eaea3SApple OSS Distributions 	allocate_vm_stuff(OBJECTS);
752*5e3eaea3SApple OSS Distributions }
753*5e3eaea3SApple OSS Distributions 
754*5e3eaea3SApple OSS Distributions T_HELPER_DECL(allocate_from_generic_zone, "allocates from a generic zone")
755*5e3eaea3SApple OSS Distributions {
756*5e3eaea3SApple OSS Distributions 	move_to_idle_band();
757*5e3eaea3SApple OSS Distributions 	allocate_from_generic_zone();
758*5e3eaea3SApple OSS Distributions }
759*5e3eaea3SApple OSS Distributions 
760*5e3eaea3SApple OSS Distributions /*
761*5e3eaea3SApple OSS Distributions  * T_META_SYSCTL_INT(ZONEMAP_JETSAM_LIMIT_SYSCTL) changes the zone_map_jetsam_limit to a
762*5e3eaea3SApple OSS Distributions  * lower value, so that the test can complete faster.
763*5e3eaea3SApple OSS Distributions  * The test allocates zone memory pretty aggressively which can cause the system to panic
764*5e3eaea3SApple OSS Distributions  * if the jetsam limit is quite high; a lower value keeps us from panicking.
765*5e3eaea3SApple OSS Distributions  */
766*5e3eaea3SApple OSS Distributions T_DECL( memorystatus_vme_zone_test,
767*5e3eaea3SApple OSS Distributions     "allocates elements from the VM map entries zone, verifies zone-map-exhaustion jetsams",
768*5e3eaea3SApple OSS Distributions     T_META_ASROOT(true),
769*5e3eaea3SApple OSS Distributions     T_META_TIMEOUT(1800),
770*5e3eaea3SApple OSS Distributions /*		T_META_LTEPHASE(LTE_POSTINIT),
771*5e3eaea3SApple OSS Distributions  */
772*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_NE("kern.kasan.available", 1),
773*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1),
774*5e3eaea3SApple OSS Distributions     T_META_SYSCTL_INT(ZONEMAP_JETSAM_LIMIT_SYSCTL))
775*5e3eaea3SApple OSS Distributions {
776*5e3eaea3SApple OSS Distributions 	current_test = (test_config_struct) {
777*5e3eaea3SApple OSS Distributions 		.test_index = VME_ZONE_TEST,
778*5e3eaea3SApple OSS Distributions 		.helper_func = VME_ZONE_TEST_OPT,
779*5e3eaea3SApple OSS Distributions 		.num_zones = 1,
780*5e3eaea3SApple OSS Distributions 		.zone_names = (mach_zone_name_t[]){
781*5e3eaea3SApple OSS Distributions 			{ .mzn_name = VME_ZONE }
782*5e3eaea3SApple OSS Distributions 		}
783*5e3eaea3SApple OSS Distributions 	};
784*5e3eaea3SApple OSS Distributions 	run_test();
785*5e3eaea3SApple OSS Distributions }
786*5e3eaea3SApple OSS Distributions 
787*5e3eaea3SApple OSS Distributions T_DECL( memorystatus_vm_objects_zone_test,
788*5e3eaea3SApple OSS Distributions     "allocates elements from the VM objects and the VM map entries zones, verifies zone-map-exhaustion jetsams",
789*5e3eaea3SApple OSS Distributions     T_META_ASROOT(true),
790*5e3eaea3SApple OSS Distributions     T_META_TIMEOUT(1800),
791*5e3eaea3SApple OSS Distributions /*		T_META_LTEPHASE(LTE_POSTINIT),
792*5e3eaea3SApple OSS Distributions  */
793*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_NE("kern.kasan.available", 1),
794*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1),
795*5e3eaea3SApple OSS Distributions     T_META_SYSCTL_INT(ZONEMAP_JETSAM_LIMIT_SYSCTL))
796*5e3eaea3SApple OSS Distributions {
797*5e3eaea3SApple OSS Distributions 	current_test = (test_config_struct) {
798*5e3eaea3SApple OSS Distributions 		.test_index = VM_OBJECTS_ZONE_TEST,
799*5e3eaea3SApple OSS Distributions 		.helper_func = VM_OBJECTS_ZONE_TEST_OPT,
800*5e3eaea3SApple OSS Distributions 		.num_zones = 2,
801*5e3eaea3SApple OSS Distributions 		.zone_names = (mach_zone_name_t[]){
802*5e3eaea3SApple OSS Distributions 			{ .mzn_name = VME_ZONE },
803*5e3eaea3SApple OSS Distributions 			{ .mzn_name = VMOBJECTS_ZONE}
804*5e3eaea3SApple OSS Distributions 		}
805*5e3eaea3SApple OSS Distributions 	};
806*5e3eaea3SApple OSS Distributions 	run_test();
807*5e3eaea3SApple OSS Distributions }
808*5e3eaea3SApple OSS Distributions 
809*5e3eaea3SApple OSS Distributions T_DECL( memorystatus_generic_zone_test,
810*5e3eaea3SApple OSS Distributions     "allocates elements from a zone that doesn't have an optimized jetsam path, verifies zone-map-exhaustion jetsams",
811*5e3eaea3SApple OSS Distributions     T_META_ASROOT(true),
812*5e3eaea3SApple OSS Distributions     T_META_TIMEOUT(1800),
813*5e3eaea3SApple OSS Distributions /*		T_META_LTEPHASE(LTE_POSTINIT),
814*5e3eaea3SApple OSS Distributions  */
815*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_NE("kern.kasan.available", 1),
816*5e3eaea3SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1),
817*5e3eaea3SApple OSS Distributions     T_META_SYSCTL_INT(ZONEMAP_JETSAM_LIMIT_SYSCTL))
818*5e3eaea3SApple OSS Distributions {
819*5e3eaea3SApple OSS Distributions 	current_test = (test_config_struct) {
820*5e3eaea3SApple OSS Distributions 		.test_index = GENERIC_ZONE_TEST,
821*5e3eaea3SApple OSS Distributions 		.helper_func = GENERIC_ZONE_TEST_OPT,
822*5e3eaea3SApple OSS Distributions 		.num_zones = 0,
823*5e3eaea3SApple OSS Distributions 		.zone_names = NULL
824*5e3eaea3SApple OSS Distributions 	};
825*5e3eaea3SApple OSS Distributions 	run_test();
826*5e3eaea3SApple OSS Distributions }
827