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