]>
Commit | Line | Data |
---|---|---|
1 | #include "test_fault_helper.h" | |
2 | #include "fail.h" | |
3 | #include <sys/mman.h> | |
4 | #include <stdlib.h> | |
5 | #include <unistd.h> | |
6 | #include <assert.h> | |
7 | #include <TargetConditionals.h> | |
8 | ||
9 | #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) | |
10 | #define MEMSIZE (1L<<28) | |
11 | #else | |
12 | #define MEMSIZE (1L<<30) | |
13 | #endif | |
14 | ||
15 | static char* memblock; | |
16 | ||
17 | int | |
18 | test_fault_setup() | |
19 | { | |
20 | char *ptr; | |
21 | int pgsz = getpagesize(); | |
22 | int retval; | |
23 | ||
24 | memblock = (char *)mmap(NULL, MEMSIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); | |
25 | VERIFY(memblock != MAP_FAILED, "mmap failed"); | |
26 | ||
27 | /* make sure memory is paged */ | |
28 | for (ptr = memblock; ptr < memblock + MEMSIZE; ptr += pgsz) { | |
29 | *ptr = 1; | |
30 | } | |
31 | ||
32 | /* set to read only, then back to read write so it faults on first write */ | |
33 | retval = mprotect(memblock, MEMSIZE, PROT_READ); | |
34 | VERIFY(retval == 0, "mprotect failed"); | |
35 | ||
36 | retval = mprotect(memblock, MEMSIZE, PROT_READ | PROT_WRITE); | |
37 | VERIFY(retval == 0, "mprotect failed"); | |
38 | ||
39 | return PERFINDEX_SUCCESS; | |
40 | } | |
41 | ||
42 | int | |
43 | test_fault_helper(int thread_id, int num_threads, long long length, testtype_t testtype) | |
44 | { | |
45 | char *ptr; | |
46 | int pgsz = getpagesize(); | |
47 | int retval; | |
48 | ||
49 | long long num_pages = MEMSIZE / pgsz; | |
50 | long long region_len = num_pages / num_threads; | |
51 | long long region_start = region_len * thread_id; | |
52 | long long region_end; | |
53 | ||
54 | if (thread_id < num_pages % num_threads) { | |
55 | region_start += thread_id; | |
56 | region_len++; | |
57 | } else { | |
58 | region_start += num_pages % num_threads; | |
59 | } | |
60 | ||
61 | region_start *= pgsz; | |
62 | region_len *= pgsz; | |
63 | region_end = region_start + region_len; | |
64 | ||
65 | long long left = length; | |
66 | ||
67 | while (1) { | |
68 | for (ptr = memblock + region_start; ptr < memblock + region_end; ptr += pgsz) { | |
69 | *ptr = 1; | |
70 | left--; | |
71 | if (left == 0) { | |
72 | break; | |
73 | } | |
74 | } | |
75 | ||
76 | if (left == 0) { | |
77 | break; | |
78 | } | |
79 | ||
80 | if (testtype == TESTFAULT) { | |
81 | retval = mprotect(memblock + region_start, region_len, PROT_READ) == 0; | |
82 | VERIFY(retval == 0, "mprotect failed"); | |
83 | retval = mprotect(memblock + region_start, region_len, PROT_READ | PROT_WRITE) == 0; | |
84 | VERIFY(retval == 0, "mprotect failed"); | |
85 | } else if (testtype == TESTZFOD) { | |
86 | retval = munmap(memblock + region_start, region_len) == 0; | |
87 | VERIFY(retval == 0, "munmap failed"); | |
88 | ptr = mmap(memblock + region_start, region_len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0); | |
89 | VERIFY(ptr != 0, "mmap failed"); | |
90 | } | |
91 | } | |
92 | return PERFINDEX_SUCCESS; | |
93 | } |