]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/perf_index/stress_fault.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / tools / tests / perf_index / stress_fault.c
1 #include "perf_index.h"
2 #include <sys/mman.h>
3 #include <TargetConditionals.h>
4
5 #if TARGET_OS_EMBEDDED
6 #define MEMSIZE (1L<<28)
7 #else
8 #define MEMSIZE (1L<<30)
9 #endif
10
11 typedef enum {
12 TESTZFOD,
13 TESTFAULT
14 } testtype_t;
15
16 const stress_test_t fault_test = {"fault", &stress_fault_init, &stress_fault, &stress_general_cleanup, &no_validate};
17 const stress_test_t zfod_test = {"zfod", &stress_fault_init, &stress_zfod, &stress_general_cleanup, &no_validate};
18
19 static char *memblock;
20
21 DECL_INIT(stress_fault_init) {
22 int pgsz = getpagesize();
23 memblock = (char *)mmap(NULL, MEMSIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
24 char *ptr;
25 /* make sure memory is paged */
26 for(ptr = memblock; ptr<memblock+MEMSIZE; ptr+= pgsz) {
27 *ptr = 1;
28 }
29 /* set to read only, then back to read write so it faults on first write */
30 mprotect(memblock, MEMSIZE, PROT_READ);
31 mprotect(memblock, MEMSIZE, PROT_READ | PROT_WRITE);
32 }
33
34 void stress_fault_helper(int thread_id, int num_threads, long long length, testtype_t testtype) {
35 char *ptr;
36 int pgsz = getpagesize();
37
38 long long num_pages = MEMSIZE / pgsz;
39 long long region_len = num_pages/num_threads;
40 long long region_start = region_len * thread_id;
41 long long region_end;
42
43 if(thread_id < num_pages % num_threads) {
44 region_start += thread_id;
45 region_len++;
46 }
47 else {
48 region_start += num_pages % num_threads;
49 }
50
51 region_start *= pgsz;
52 region_len *= pgsz;
53 region_end = region_start + region_len;
54
55 long long left = length;
56
57 while(1) {
58 for(ptr = memblock+region_start; ptr<memblock+region_end; ptr+= pgsz) {
59 *ptr = 1;
60 left--;
61 if(left==0)
62 break;
63 }
64
65 if(left==0)
66 break;
67
68 if(testtype == TESTFAULT) {
69 assert(mprotect(memblock+region_start, region_len, PROT_READ) == 0);
70 assert(mprotect(memblock+region_start, region_len, PROT_READ | PROT_WRITE) == 0);
71 }
72 else if(testtype == TESTZFOD) {
73 assert(munmap(memblock+region_start, region_len) == 0);
74 assert(mmap(memblock+region_start, region_len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0)!=0);
75 }
76 }
77 }
78
79 DECL_TEST(stress_fault) {
80 stress_fault_helper(thread_id, num_threads, length, TESTFAULT);
81 }
82
83 DECL_TEST(stress_zfod) {
84 stress_fault_helper(thread_id, num_threads, length, TESTZFOD);
85 }