xref: /xnu-8792.41.9/tests/perf_vmfault.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions #include <unistd.h>
2*5c2921b0SApple OSS Distributions #include <stdlib.h>
3*5c2921b0SApple OSS Distributions #include <sys/mman.h>
4*5c2921b0SApple OSS Distributions #include <sys/sysctl.h>
5*5c2921b0SApple OSS Distributions #include <mach/mach.h>
6*5c2921b0SApple OSS Distributions #include <mach/vm_map.h>
7*5c2921b0SApple OSS Distributions #include <darwintest.h>
8*5c2921b0SApple OSS Distributions #include <TargetConditionals.h>
9*5c2921b0SApple OSS Distributions #include <perfcheck_keys.h>
10*5c2921b0SApple OSS Distributions 
11*5c2921b0SApple OSS Distributions #include "benchmark/helpers.h"
12*5c2921b0SApple OSS Distributions #include "test_utils.h"
13*5c2921b0SApple OSS Distributions 
14*5c2921b0SApple OSS Distributions T_GLOBAL_META(
15*5c2921b0SApple OSS Distributions 	T_META_NAMESPACE("xnu.vm.perf"),
16*5c2921b0SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
17*5c2921b0SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"),
18*5c2921b0SApple OSS Distributions 	T_META_CHECK_LEAKS(false),
19*5c2921b0SApple OSS Distributions 	T_META_TAG_PERF
20*5c2921b0SApple OSS Distributions 	);
21*5c2921b0SApple OSS Distributions 
22*5c2921b0SApple OSS Distributions #ifdef DT_IOSMARK
23*5c2921b0SApple OSS Distributions #define MEMSIZE                 (1UL<<29)       /* 512 MB */
24*5c2921b0SApple OSS Distributions #else
25*5c2921b0SApple OSS Distributions #define MEMSIZE                 (1UL<<27)       /* 128 MB */
26*5c2921b0SApple OSS Distributions #endif
27*5c2921b0SApple OSS Distributions 
28*5c2921b0SApple OSS Distributions #define VM_TAG1                 100
29*5c2921b0SApple OSS Distributions #define VM_TAG2                 101
30*5c2921b0SApple OSS Distributions 
31*5c2921b0SApple OSS Distributions enum {
32*5c2921b0SApple OSS Distributions 	SOFT_FAULT,
33*5c2921b0SApple OSS Distributions 	ZERO_FILL,
34*5c2921b0SApple OSS Distributions 	NUM_FAULT_TYPES
35*5c2921b0SApple OSS Distributions };
36*5c2921b0SApple OSS Distributions 
37*5c2921b0SApple OSS Distributions enum {
38*5c2921b0SApple OSS Distributions 	VARIANT_DEFAULT = 1,
39*5c2921b0SApple OSS Distributions 	VARIANT_SINGLE_REGION,
40*5c2921b0SApple OSS Distributions 	VARIANT_MULTIPLE_REGIONS,
41*5c2921b0SApple OSS Distributions 	NUM_MAPPING_VARIANTS
42*5c2921b0SApple OSS Distributions };
43*5c2921b0SApple OSS Distributions 
44*5c2921b0SApple OSS Distributions static char *variant_str[] = {
45*5c2921b0SApple OSS Distributions 	"none",
46*5c2921b0SApple OSS Distributions 	"default",
47*5c2921b0SApple OSS Distributions 	"single-region",
48*5c2921b0SApple OSS Distributions 	"multiple-regions"
49*5c2921b0SApple OSS Distributions };
50*5c2921b0SApple OSS Distributions 
51*5c2921b0SApple OSS Distributions 
52*5c2921b0SApple OSS Distributions typedef struct {
53*5c2921b0SApple OSS Distributions 	char *region_addr;
54*5c2921b0SApple OSS Distributions 	char *shared_region_addr;
55*5c2921b0SApple OSS Distributions 	size_t region_len;
56*5c2921b0SApple OSS Distributions } memregion_config;
57*5c2921b0SApple OSS Distributions 
58*5c2921b0SApple OSS Distributions static memregion_config *memregion_config_per_thread;
59*5c2921b0SApple OSS Distributions 
60*5c2921b0SApple OSS Distributions static size_t pgsize;
61*5c2921b0SApple OSS Distributions static int num_threads;
62*5c2921b0SApple OSS Distributions static int ready_thread_count;
63*5c2921b0SApple OSS Distributions static int finished_thread_count;
64*5c2921b0SApple OSS Distributions static dt_stat_time_t runtime;
65*5c2921b0SApple OSS Distributions static pthread_cond_t start_cvar;
66*5c2921b0SApple OSS Distributions static pthread_cond_t threads_ready_cvar;
67*5c2921b0SApple OSS Distributions static pthread_cond_t threads_finished_cvar;
68*5c2921b0SApple OSS Distributions static pthread_mutex_t ready_thread_count_lock;
69*5c2921b0SApple OSS Distributions static pthread_mutex_t finished_thread_count_lock;
70*5c2921b0SApple OSS Distributions 
71*5c2921b0SApple OSS Distributions static void map_mem_regions_default(int fault_type, size_t memsize);
72*5c2921b0SApple OSS Distributions static void map_mem_regions_single(int fault_type, size_t memsize);
73*5c2921b0SApple OSS Distributions static void map_mem_regions_multiple(int fault_type, size_t memsize);
74*5c2921b0SApple OSS Distributions static void map_mem_regions(int fault_type, int mapping_variant, size_t memsize);
75*5c2921b0SApple OSS Distributions static void unmap_mem_regions(int mapping_variant, size_t memsize);
76*5c2921b0SApple OSS Distributions static void setup_per_thread_regions(char *memblock, char *memblock_share, int fault_type, size_t memsize);
77*5c2921b0SApple OSS Distributions static void fault_pages(int thread_id);
78*5c2921b0SApple OSS Distributions static void execute_threads(void);
79*5c2921b0SApple OSS Distributions static void *thread_setup(void *arg);
80*5c2921b0SApple OSS Distributions static void run_test(int fault_type, int mapping_variant, size_t memsize);
81*5c2921b0SApple OSS Distributions static void setup_and_run_test(int test, int threads);
82*5c2921b0SApple OSS Distributions 
83*5c2921b0SApple OSS Distributions /* Allocates memory using the default mmap behavior. Each VM region created is capped at 128 MB. */
84*5c2921b0SApple OSS Distributions static void
map_mem_regions_default(int fault_type,size_t memsize)85*5c2921b0SApple OSS Distributions map_mem_regions_default(int fault_type, size_t memsize)
86*5c2921b0SApple OSS Distributions {
87*5c2921b0SApple OSS Distributions 	volatile char val;
88*5c2921b0SApple OSS Distributions 	vm_prot_t curprot, maxprot;
89*5c2921b0SApple OSS Distributions 	char *ptr, *memblock, *memblock_share = NULL;
90*5c2921b0SApple OSS Distributions 
91*5c2921b0SApple OSS Distributions 	memblock = (char *)mmap(NULL, memsize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
92*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_NE((void *)memblock, MAP_FAILED, "mmap");
93*5c2921b0SApple OSS Distributions 
94*5c2921b0SApple OSS Distributions 	if (fault_type == SOFT_FAULT) {
95*5c2921b0SApple OSS Distributions 		/* Fault in all the pages of the original region. */
96*5c2921b0SApple OSS Distributions 		for (ptr = memblock; ptr < memblock + memsize; ptr += pgsize) {
97*5c2921b0SApple OSS Distributions 			val = *ptr;
98*5c2921b0SApple OSS Distributions 		}
99*5c2921b0SApple OSS Distributions 		/* Remap the region so that subsequent accesses result in read soft faults. */
100*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(vm_remap(mach_task_self(), (vm_address_t *)&memblock_share,
101*5c2921b0SApple OSS Distributions 		    memsize, 0, VM_FLAGS_ANYWHERE, mach_task_self(), (vm_address_t)memblock, FALSE,
102*5c2921b0SApple OSS Distributions 		    &curprot, &maxprot, VM_INHERIT_DEFAULT), "vm_remap");
103*5c2921b0SApple OSS Distributions 	}
104*5c2921b0SApple OSS Distributions 	setup_per_thread_regions(memblock, memblock_share, fault_type, memsize);
105*5c2921b0SApple OSS Distributions }
106*5c2921b0SApple OSS Distributions 
107*5c2921b0SApple OSS Distributions /* Creates a single VM region by mapping in a named memory entry. */
108*5c2921b0SApple OSS Distributions static void
map_mem_regions_single(int fault_type,size_t memsize)109*5c2921b0SApple OSS Distributions map_mem_regions_single(int fault_type, size_t memsize)
110*5c2921b0SApple OSS Distributions {
111*5c2921b0SApple OSS Distributions 	volatile char val;
112*5c2921b0SApple OSS Distributions 	vm_prot_t curprot, maxprot;
113*5c2921b0SApple OSS Distributions 	char *ptr, *memblock = NULL, *memblock_share = NULL;
114*5c2921b0SApple OSS Distributions 	vm_size_t size = memsize;
115*5c2921b0SApple OSS Distributions 	vm_offset_t addr1 = 0;
116*5c2921b0SApple OSS Distributions 	mach_port_t mem_handle = MACH_PORT_NULL;
117*5c2921b0SApple OSS Distributions 
118*5c2921b0SApple OSS Distributions 	/* Allocate a region and fault in all the pages. */
119*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(vm_allocate(mach_task_self(), &addr1, size, VM_FLAGS_ANYWHERE), "vm_allocate");
120*5c2921b0SApple OSS Distributions 	for (ptr = (char *)addr1; ptr < (char *)addr1 + memsize; ptr += pgsize) {
121*5c2921b0SApple OSS Distributions 		val = *ptr;
122*5c2921b0SApple OSS Distributions 	}
123*5c2921b0SApple OSS Distributions 
124*5c2921b0SApple OSS Distributions 	/* Create a named memory entry from the region allocated above, and de-allocate said region. */
125*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(mach_make_memory_entry(mach_task_self(), &size, addr1, VM_PROT_ALL | MAP_MEM_NAMED_CREATE,
126*5c2921b0SApple OSS Distributions 	    &mem_handle, MACH_PORT_NULL), "mach_make_memory_entry");
127*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(vm_deallocate(mach_task_self(), addr1, size), "vm_deallocate");
128*5c2921b0SApple OSS Distributions 
129*5c2921b0SApple OSS Distributions 	/* Map in the named entry and deallocate it. */
130*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(vm_map(mach_task_self(), (vm_address_t *)&memblock, size, 0, VM_FLAGS_ANYWHERE, mem_handle, 0,
131*5c2921b0SApple OSS Distributions 	    FALSE, VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_NONE), "vm_map");
132*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(mach_port_deallocate(mach_task_self(), mem_handle), "mach_port_deallocate");
133*5c2921b0SApple OSS Distributions 
134*5c2921b0SApple OSS Distributions 	if (fault_type == SOFT_FAULT) {
135*5c2921b0SApple OSS Distributions 		/* Fault in all the pages of the original region. */
136*5c2921b0SApple OSS Distributions 		for (ptr = memblock; ptr < memblock + memsize; ptr += pgsize) {
137*5c2921b0SApple OSS Distributions 			val = *ptr;
138*5c2921b0SApple OSS Distributions 		}
139*5c2921b0SApple OSS Distributions 		/* Remap the region so that subsequent accesses result in read soft faults. */
140*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(vm_remap(mach_task_self(), (vm_address_t *)&memblock_share,
141*5c2921b0SApple OSS Distributions 		    memsize, 0, VM_FLAGS_ANYWHERE, mach_task_self(), (vm_address_t)memblock, FALSE,
142*5c2921b0SApple OSS Distributions 		    &curprot, &maxprot, VM_INHERIT_DEFAULT), "vm_remap");
143*5c2921b0SApple OSS Distributions 	}
144*5c2921b0SApple OSS Distributions 	setup_per_thread_regions(memblock, memblock_share, fault_type, memsize);
145*5c2921b0SApple OSS Distributions }
146*5c2921b0SApple OSS Distributions 
147*5c2921b0SApple OSS Distributions /* Allocates a separate VM region for each thread. */
148*5c2921b0SApple OSS Distributions static void
map_mem_regions_multiple(int fault_type,size_t memsize)149*5c2921b0SApple OSS Distributions map_mem_regions_multiple(int fault_type, size_t memsize)
150*5c2921b0SApple OSS Distributions {
151*5c2921b0SApple OSS Distributions 	int i;
152*5c2921b0SApple OSS Distributions 	size_t region_len, num_pages;
153*5c2921b0SApple OSS Distributions 	volatile char val;
154*5c2921b0SApple OSS Distributions 	char *ptr, *memblock, *memblock_share;
155*5c2921b0SApple OSS Distributions 	vm_prot_t curprot, maxprot;
156*5c2921b0SApple OSS Distributions 
157*5c2921b0SApple OSS Distributions 	num_pages = memsize / pgsize;
158*5c2921b0SApple OSS Distributions 
159*5c2921b0SApple OSS Distributions 	for (i = 0; i < num_threads; i++) {
160*5c2921b0SApple OSS Distributions 		memblock = NULL;
161*5c2921b0SApple OSS Distributions 
162*5c2921b0SApple OSS Distributions 		region_len = num_pages / (size_t)num_threads;
163*5c2921b0SApple OSS Distributions 		if ((size_t)i < num_pages % (size_t)num_threads) {
164*5c2921b0SApple OSS Distributions 			region_len++;
165*5c2921b0SApple OSS Distributions 		}
166*5c2921b0SApple OSS Distributions 		region_len *= pgsize;
167*5c2921b0SApple OSS Distributions 
168*5c2921b0SApple OSS Distributions 		int fd = VM_MAKE_TAG((i % 2)? VM_TAG1 : VM_TAG2);
169*5c2921b0SApple OSS Distributions 		memblock = (char *)mmap(NULL, region_len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, fd, 0);
170*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_NE((void *)memblock, MAP_FAILED, "mmap");
171*5c2921b0SApple OSS Distributions 		memregion_config_per_thread[i].region_addr = memblock;
172*5c2921b0SApple OSS Distributions 		memregion_config_per_thread[i].shared_region_addr = 0;
173*5c2921b0SApple OSS Distributions 		memregion_config_per_thread[i].region_len = region_len;
174*5c2921b0SApple OSS Distributions 
175*5c2921b0SApple OSS Distributions 		if (fault_type == SOFT_FAULT) {
176*5c2921b0SApple OSS Distributions 			/* Fault in all the pages of the original region. */
177*5c2921b0SApple OSS Distributions 			for (ptr = memblock; ptr < memblock + region_len; ptr += pgsize) {
178*5c2921b0SApple OSS Distributions 				val = *ptr;
179*5c2921b0SApple OSS Distributions 			}
180*5c2921b0SApple OSS Distributions 			memblock_share = NULL;
181*5c2921b0SApple OSS Distributions 			/* Remap the region so that subsequent accesses result in read soft faults. */
182*5c2921b0SApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_SUCCESS(vm_remap(mach_task_self(), (vm_address_t *)&memblock_share,
183*5c2921b0SApple OSS Distributions 			    region_len, 0, VM_FLAGS_ANYWHERE, mach_task_self(), (vm_address_t)memblock, FALSE,
184*5c2921b0SApple OSS Distributions 			    &curprot, &maxprot, VM_INHERIT_DEFAULT), "vm_remap");
185*5c2921b0SApple OSS Distributions 			memregion_config_per_thread[i].shared_region_addr = memblock_share;
186*5c2921b0SApple OSS Distributions 		}
187*5c2921b0SApple OSS Distributions 	}
188*5c2921b0SApple OSS Distributions }
189*5c2921b0SApple OSS Distributions 
190*5c2921b0SApple OSS Distributions static void
map_mem_regions(int fault_type,int mapping_variant,size_t memsize)191*5c2921b0SApple OSS Distributions map_mem_regions(int fault_type, int mapping_variant, size_t memsize)
192*5c2921b0SApple OSS Distributions {
193*5c2921b0SApple OSS Distributions 	memregion_config_per_thread = (memregion_config *)malloc(sizeof(*memregion_config_per_thread) * (size_t)num_threads);
194*5c2921b0SApple OSS Distributions 	switch (mapping_variant) {
195*5c2921b0SApple OSS Distributions 	case VARIANT_SINGLE_REGION:
196*5c2921b0SApple OSS Distributions 		map_mem_regions_single(fault_type, memsize);
197*5c2921b0SApple OSS Distributions 		break;
198*5c2921b0SApple OSS Distributions 	case VARIANT_MULTIPLE_REGIONS:
199*5c2921b0SApple OSS Distributions 		map_mem_regions_multiple(fault_type, memsize);
200*5c2921b0SApple OSS Distributions 		break;
201*5c2921b0SApple OSS Distributions 	case VARIANT_DEFAULT:
202*5c2921b0SApple OSS Distributions 	default:
203*5c2921b0SApple OSS Distributions 		map_mem_regions_default(fault_type, memsize);
204*5c2921b0SApple OSS Distributions 	}
205*5c2921b0SApple OSS Distributions }
206*5c2921b0SApple OSS Distributions 
207*5c2921b0SApple OSS Distributions static void
setup_per_thread_regions(char * memblock,char * memblock_share,int fault_type,size_t memsize)208*5c2921b0SApple OSS Distributions setup_per_thread_regions(char *memblock, char *memblock_share, int fault_type, size_t memsize)
209*5c2921b0SApple OSS Distributions {
210*5c2921b0SApple OSS Distributions 	int i;
211*5c2921b0SApple OSS Distributions 	size_t region_len, region_start, num_pages;
212*5c2921b0SApple OSS Distributions 
213*5c2921b0SApple OSS Distributions 	num_pages = memsize / pgsize;
214*5c2921b0SApple OSS Distributions 	for (i = 0; i < num_threads; i++) {
215*5c2921b0SApple OSS Distributions 		region_len = num_pages / (size_t)num_threads;
216*5c2921b0SApple OSS Distributions 		region_start = region_len * (size_t)i;
217*5c2921b0SApple OSS Distributions 
218*5c2921b0SApple OSS Distributions 		if ((size_t)i < num_pages % (size_t)num_threads) {
219*5c2921b0SApple OSS Distributions 			region_start += (size_t)i;
220*5c2921b0SApple OSS Distributions 			region_len++;
221*5c2921b0SApple OSS Distributions 		} else {
222*5c2921b0SApple OSS Distributions 			region_start += num_pages % (size_t)num_threads;
223*5c2921b0SApple OSS Distributions 		}
224*5c2921b0SApple OSS Distributions 
225*5c2921b0SApple OSS Distributions 		region_start *= pgsize;
226*5c2921b0SApple OSS Distributions 		region_len *= pgsize;
227*5c2921b0SApple OSS Distributions 
228*5c2921b0SApple OSS Distributions 		memregion_config_per_thread[i].region_addr = memblock + region_start;
229*5c2921b0SApple OSS Distributions 		memregion_config_per_thread[i].shared_region_addr = ((fault_type == SOFT_FAULT) ?
230*5c2921b0SApple OSS Distributions 		    memblock_share + region_start : 0);
231*5c2921b0SApple OSS Distributions 		memregion_config_per_thread[i].region_len = region_len;
232*5c2921b0SApple OSS Distributions 	}
233*5c2921b0SApple OSS Distributions }
234*5c2921b0SApple OSS Distributions 
235*5c2921b0SApple OSS Distributions static void
unmap_mem_regions(int mapping_variant,size_t memsize)236*5c2921b0SApple OSS Distributions unmap_mem_regions(int mapping_variant, size_t memsize)
237*5c2921b0SApple OSS Distributions {
238*5c2921b0SApple OSS Distributions 	if (mapping_variant == VARIANT_MULTIPLE_REGIONS) {
239*5c2921b0SApple OSS Distributions 		int i;
240*5c2921b0SApple OSS Distributions 		for (i = 0; i < num_threads; i++) {
241*5c2921b0SApple OSS Distributions 			if (memregion_config_per_thread[i].shared_region_addr != 0) {
242*5c2921b0SApple OSS Distributions 				T_QUIET; T_ASSERT_MACH_SUCCESS(munmap(memregion_config_per_thread[i].shared_region_addr,
243*5c2921b0SApple OSS Distributions 				    memregion_config_per_thread[i].region_len), "munmap");
244*5c2921b0SApple OSS Distributions 			}
245*5c2921b0SApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_SUCCESS(munmap(memregion_config_per_thread[i].region_addr,
246*5c2921b0SApple OSS Distributions 			    memregion_config_per_thread[i].region_len), "munmap");
247*5c2921b0SApple OSS Distributions 		}
248*5c2921b0SApple OSS Distributions 	} else {
249*5c2921b0SApple OSS Distributions 		if (memregion_config_per_thread[0].shared_region_addr != 0) {
250*5c2921b0SApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_SUCCESS(munmap(memregion_config_per_thread[0].shared_region_addr, memsize), "munmap");
251*5c2921b0SApple OSS Distributions 		}
252*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(munmap(memregion_config_per_thread[0].region_addr, memsize), "munmap");
253*5c2921b0SApple OSS Distributions 	}
254*5c2921b0SApple OSS Distributions }
255*5c2921b0SApple OSS Distributions 
256*5c2921b0SApple OSS Distributions static void
fault_pages(int thread_id)257*5c2921b0SApple OSS Distributions fault_pages(int thread_id)
258*5c2921b0SApple OSS Distributions {
259*5c2921b0SApple OSS Distributions 	char *ptr, *block;
260*5c2921b0SApple OSS Distributions 	volatile char val;
261*5c2921b0SApple OSS Distributions 
262*5c2921b0SApple OSS Distributions 	block = memregion_config_per_thread[thread_id].shared_region_addr ?
263*5c2921b0SApple OSS Distributions 	    memregion_config_per_thread[thread_id].shared_region_addr :
264*5c2921b0SApple OSS Distributions 	    memregion_config_per_thread[thread_id].region_addr;
265*5c2921b0SApple OSS Distributions 	for (ptr = block; ptr < block + memregion_config_per_thread[thread_id].region_len; ptr += pgsize) {
266*5c2921b0SApple OSS Distributions 		val = *ptr;
267*5c2921b0SApple OSS Distributions 	}
268*5c2921b0SApple OSS Distributions }
269*5c2921b0SApple OSS Distributions 
270*5c2921b0SApple OSS Distributions static void *
thread_setup(void * arg)271*5c2921b0SApple OSS Distributions thread_setup(void *arg)
272*5c2921b0SApple OSS Distributions {
273*5c2921b0SApple OSS Distributions 	int my_index = *((int *)arg);
274*5c2921b0SApple OSS Distributions 
275*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_lock(&ready_thread_count_lock), "pthread_mutex_lock");
276*5c2921b0SApple OSS Distributions 	ready_thread_count++;
277*5c2921b0SApple OSS Distributions 	if (ready_thread_count == num_threads) {
278*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_signal(&threads_ready_cvar), "pthread_cond_signal");
279*5c2921b0SApple OSS Distributions 	}
280*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_wait(&start_cvar, &ready_thread_count_lock), "pthread_cond_wait");
281*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_unlock(&ready_thread_count_lock), "pthread_mutex_unlock");
282*5c2921b0SApple OSS Distributions 
283*5c2921b0SApple OSS Distributions 	fault_pages(my_index);
284*5c2921b0SApple OSS Distributions 
285*5c2921b0SApple OSS Distributions 	/* Up the finished count */
286*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_lock(&finished_thread_count_lock), "pthread_mutex_lock");
287*5c2921b0SApple OSS Distributions 	finished_thread_count++;
288*5c2921b0SApple OSS Distributions 	if (finished_thread_count == num_threads) {
289*5c2921b0SApple OSS Distributions 		/* All the threads are done. Wake up the main thread */
290*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_signal(&threads_finished_cvar), "pthread_cond_signal");
291*5c2921b0SApple OSS Distributions 	}
292*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_unlock(&finished_thread_count_lock), "pthread_mutex_unlock");
293*5c2921b0SApple OSS Distributions 	return NULL;
294*5c2921b0SApple OSS Distributions }
295*5c2921b0SApple OSS Distributions 
296*5c2921b0SApple OSS Distributions static void
execute_threads(void)297*5c2921b0SApple OSS Distributions execute_threads(void)
298*5c2921b0SApple OSS Distributions {
299*5c2921b0SApple OSS Distributions 	int thread_index, thread_retval;
300*5c2921b0SApple OSS Distributions 	int *thread_indices;
301*5c2921b0SApple OSS Distributions 	void *thread_retval_ptr = &thread_retval;
302*5c2921b0SApple OSS Distributions 	pthread_t* threads;
303*5c2921b0SApple OSS Distributions 
304*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_init(&threads_ready_cvar, NULL), "pthread_cond_init");
305*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_init(&start_cvar, NULL), "pthread_cond_init");
306*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_init(&ready_thread_count_lock, NULL), "pthread_mutex_init");
307*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_init(&threads_finished_cvar, NULL), "pthread_cond_init");
308*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_init(&finished_thread_count_lock, NULL), "pthread_mutex_init");
309*5c2921b0SApple OSS Distributions 	ready_thread_count = 0;
310*5c2921b0SApple OSS Distributions 	finished_thread_count = 0;
311*5c2921b0SApple OSS Distributions 
312*5c2921b0SApple OSS Distributions 	threads = (pthread_t *)malloc(sizeof(*threads) * (size_t)num_threads);
313*5c2921b0SApple OSS Distributions 	thread_indices = (int *)malloc(sizeof(*thread_indices) * (size_t)num_threads);
314*5c2921b0SApple OSS Distributions 	for (thread_index = 0; thread_index < num_threads; thread_index++) {
315*5c2921b0SApple OSS Distributions 		thread_indices[thread_index] = thread_index;
316*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_create(&threads[thread_index], NULL,
317*5c2921b0SApple OSS Distributions 		    thread_setup, (void *)&thread_indices[thread_index]), "pthread_create");
318*5c2921b0SApple OSS Distributions 	}
319*5c2921b0SApple OSS Distributions 
320*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_lock(&ready_thread_count_lock), "pthread_mutex_lock");
321*5c2921b0SApple OSS Distributions 	while (ready_thread_count != num_threads) {
322*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_wait(&threads_ready_cvar, &ready_thread_count_lock),
323*5c2921b0SApple OSS Distributions 		    "pthread_cond_wait");
324*5c2921b0SApple OSS Distributions 	}
325*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_unlock(&ready_thread_count_lock), "pthread_mutex_unlock");
326*5c2921b0SApple OSS Distributions 
327*5c2921b0SApple OSS Distributions 	T_STAT_MEASURE(runtime) {
328*5c2921b0SApple OSS Distributions 		/* Ungate the threads */
329*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_broadcast(&start_cvar), "pthread_cond_broadcast");
330*5c2921b0SApple OSS Distributions 		/* Wait for the threads to finish */
331*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_mutex_lock(&finished_thread_count_lock), "pthread_mutex_lock");
332*5c2921b0SApple OSS Distributions 		while (finished_thread_count != num_threads) {
333*5c2921b0SApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_cond_wait(&threads_finished_cvar, &finished_thread_count_lock), "pthread_cond_wait");
334*5c2921b0SApple OSS Distributions 		}
335*5c2921b0SApple OSS Distributions 	};
336*5c2921b0SApple OSS Distributions 
337*5c2921b0SApple OSS Distributions 	/* Join the threads */
338*5c2921b0SApple OSS Distributions 	for (thread_index = 0; thread_index < num_threads; thread_index++) {
339*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_join(threads[thread_index], &thread_retval_ptr),
340*5c2921b0SApple OSS Distributions 		    "pthread_join");
341*5c2921b0SApple OSS Distributions 	}
342*5c2921b0SApple OSS Distributions 
343*5c2921b0SApple OSS Distributions 	free(threads);
344*5c2921b0SApple OSS Distributions 	free(thread_indices);
345*5c2921b0SApple OSS Distributions }
346*5c2921b0SApple OSS Distributions 
347*5c2921b0SApple OSS Distributions static void
run_test(int fault_type,int mapping_variant,size_t memsize)348*5c2921b0SApple OSS Distributions run_test(int fault_type, int mapping_variant, size_t memsize)
349*5c2921b0SApple OSS Distributions {
350*5c2921b0SApple OSS Distributions 	char metric_str[32];
351*5c2921b0SApple OSS Distributions 	size_t num_pages;
352*5c2921b0SApple OSS Distributions 	size_t sysctl_size = sizeof(pgsize);
353*5c2921b0SApple OSS Distributions 	int ret = sysctlbyname("vm.pagesize", &pgsize, &sysctl_size, NULL, 0);
354*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl vm.pagesize failed");
355*5c2921b0SApple OSS Distributions 
356*5c2921b0SApple OSS Distributions 	num_pages = memsize / pgsize;
357*5c2921b0SApple OSS Distributions 
358*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_LT(fault_type, NUM_FAULT_TYPES, "invalid test type");
359*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_LT(mapping_variant, NUM_MAPPING_VARIANTS, "invalid mapping variant");
360*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_GT(num_threads, 0, "num_threads <= 0");
361*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_GT((int)num_pages / num_threads, 0, "num_pages/num_threads <= 0");
362*5c2921b0SApple OSS Distributions 
363*5c2921b0SApple OSS Distributions 	T_LOG("No. of cpus:     %d", get_ncpu());
364*5c2921b0SApple OSS Distributions 	T_LOG("No. of threads:  %d", num_threads);
365*5c2921b0SApple OSS Distributions 	T_LOG("No. of pages:    %ld", num_pages);
366*5c2921b0SApple OSS Distributions 	T_LOG("Pagesize:        %ld", pgsize);
367*5c2921b0SApple OSS Distributions 	T_LOG("Allocation size: %ld MB", memsize / (1024 * 1024));
368*5c2921b0SApple OSS Distributions 	T_LOG("Mapping variant: %s", variant_str[mapping_variant]);
369*5c2921b0SApple OSS Distributions 
370*5c2921b0SApple OSS Distributions 	snprintf(metric_str, 32, "Runtime-%s", variant_str[mapping_variant]);
371*5c2921b0SApple OSS Distributions 	runtime = dt_stat_time_create(metric_str);
372*5c2921b0SApple OSS Distributions 
373*5c2921b0SApple OSS Distributions 	while (!dt_stat_stable(runtime)) {
374*5c2921b0SApple OSS Distributions 		map_mem_regions(fault_type, mapping_variant, memsize);
375*5c2921b0SApple OSS Distributions 		execute_threads();
376*5c2921b0SApple OSS Distributions 		unmap_mem_regions(mapping_variant, memsize);
377*5c2921b0SApple OSS Distributions 	}
378*5c2921b0SApple OSS Distributions 
379*5c2921b0SApple OSS Distributions 	dt_stat_finalize(runtime);
380*5c2921b0SApple OSS Distributions 	T_LOG("Throughput-%s (MB/s): %lf\n\n", variant_str[mapping_variant], (double)memsize / (1024 * 1024) / dt_stat_mean((dt_stat_t)runtime));
381*5c2921b0SApple OSS Distributions }
382*5c2921b0SApple OSS Distributions 
383*5c2921b0SApple OSS Distributions static void
setup_and_run_test(int fault_type,int threads)384*5c2921b0SApple OSS Distributions setup_and_run_test(int fault_type, int threads)
385*5c2921b0SApple OSS Distributions {
386*5c2921b0SApple OSS Distributions 	int i, mapping_variant;
387*5c2921b0SApple OSS Distributions 	size_t memsize;
388*5c2921b0SApple OSS Distributions 	char *e;
389*5c2921b0SApple OSS Distributions 
390*5c2921b0SApple OSS Distributions 	mapping_variant = VARIANT_DEFAULT;
391*5c2921b0SApple OSS Distributions 	memsize = MEMSIZE;
392*5c2921b0SApple OSS Distributions 	num_threads = threads;
393*5c2921b0SApple OSS Distributions 
394*5c2921b0SApple OSS Distributions 	if ((e = getenv("NTHREADS"))) {
395*5c2921b0SApple OSS Distributions 		if (threads == 1) {
396*5c2921b0SApple OSS Distributions 			T_SKIP("Custom environment variables specified. Skipping single threaded version.");
397*5c2921b0SApple OSS Distributions 		}
398*5c2921b0SApple OSS Distributions 		num_threads = (int)strtol(e, NULL, 0);
399*5c2921b0SApple OSS Distributions 	}
400*5c2921b0SApple OSS Distributions 
401*5c2921b0SApple OSS Distributions 	if ((e = getenv("MEMSIZEMB"))) {
402*5c2921b0SApple OSS Distributions 		memsize = (size_t)strtol(e, NULL, 0) * 1024 * 1024;
403*5c2921b0SApple OSS Distributions 	}
404*5c2921b0SApple OSS Distributions 
405*5c2921b0SApple OSS Distributions 	if ((e = getenv("VARIANT"))) {
406*5c2921b0SApple OSS Distributions 		mapping_variant = (int)strtol(e, NULL, 0);
407*5c2921b0SApple OSS Distributions 		run_test(fault_type, mapping_variant, memsize);
408*5c2921b0SApple OSS Distributions 	} else {
409*5c2921b0SApple OSS Distributions 		for (i = VARIANT_DEFAULT; i < NUM_MAPPING_VARIANTS; i++) {
410*5c2921b0SApple OSS Distributions 			run_test(fault_type, i, memsize);
411*5c2921b0SApple OSS Distributions 		}
412*5c2921b0SApple OSS Distributions 	}
413*5c2921b0SApple OSS Distributions 
414*5c2921b0SApple OSS Distributions 	T_END;
415*5c2921b0SApple OSS Distributions }
416*5c2921b0SApple OSS Distributions 
417*5c2921b0SApple OSS Distributions T_DECL(read_soft_fault,
418*5c2921b0SApple OSS Distributions     "Read soft faults (single thread)")
419*5c2921b0SApple OSS Distributions {
420*5c2921b0SApple OSS Distributions 	setup_and_run_test(SOFT_FAULT, 1);
421*5c2921b0SApple OSS Distributions }
422*5c2921b0SApple OSS Distributions 
423*5c2921b0SApple OSS Distributions T_DECL(read_soft_fault_multithreaded,
424*5c2921b0SApple OSS Distributions     "Read soft faults (multi-threaded)")
425*5c2921b0SApple OSS Distributions {
426*5c2921b0SApple OSS Distributions 	char *e;
427*5c2921b0SApple OSS Distributions 	int nthreads;
428*5c2921b0SApple OSS Distributions 
429*5c2921b0SApple OSS Distributions 	/* iOSMark passes in the no. of threads via an env. variable */
430*5c2921b0SApple OSS Distributions 	if ((e = getenv("DT_STAT_NTHREADS"))) {
431*5c2921b0SApple OSS Distributions 		nthreads = (int)strtol(e, NULL, 0);
432*5c2921b0SApple OSS Distributions 	} else {
433*5c2921b0SApple OSS Distributions 		nthreads = get_ncpu();
434*5c2921b0SApple OSS Distributions 		if (nthreads == 1) {
435*5c2921b0SApple OSS Distributions 			T_SKIP("Skipping multi-threaded test on single core device.");
436*5c2921b0SApple OSS Distributions 		}
437*5c2921b0SApple OSS Distributions 	}
438*5c2921b0SApple OSS Distributions 	setup_and_run_test(SOFT_FAULT, nthreads);
439*5c2921b0SApple OSS Distributions }
440*5c2921b0SApple OSS Distributions 
441*5c2921b0SApple OSS Distributions T_DECL(zero_fill_fault,
442*5c2921b0SApple OSS Distributions     "Zero fill faults (single thread)")
443*5c2921b0SApple OSS Distributions {
444*5c2921b0SApple OSS Distributions 	setup_and_run_test(ZERO_FILL, 1);
445*5c2921b0SApple OSS Distributions }
446*5c2921b0SApple OSS Distributions 
447*5c2921b0SApple OSS Distributions T_DECL(zero_fill_fault_multithreaded,
448*5c2921b0SApple OSS Distributions     "Zero fill faults (multi-threaded)",
449*5c2921b0SApple OSS Distributions     XNU_T_META_SOC_SPECIFIC)
450*5c2921b0SApple OSS Distributions {
451*5c2921b0SApple OSS Distributions 	char *e;
452*5c2921b0SApple OSS Distributions 	int nthreads;
453*5c2921b0SApple OSS Distributions 
454*5c2921b0SApple OSS Distributions 	/* iOSMark passes in the no. of threads via an env. variable */
455*5c2921b0SApple OSS Distributions 	if ((e = getenv("DT_STAT_NTHREADS"))) {
456*5c2921b0SApple OSS Distributions 		nthreads = (int)strtol(e, NULL, 0);
457*5c2921b0SApple OSS Distributions 	} else {
458*5c2921b0SApple OSS Distributions 		nthreads = get_ncpu();
459*5c2921b0SApple OSS Distributions 		if (nthreads == 1) {
460*5c2921b0SApple OSS Distributions 			T_SKIP("Skipping multi-threaded test on single core device.");
461*5c2921b0SApple OSS Distributions 		}
462*5c2921b0SApple OSS Distributions 	}
463*5c2921b0SApple OSS Distributions 	setup_and_run_test(ZERO_FILL, nthreads);
464*5c2921b0SApple OSS Distributions }
465