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