]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/perf_index/test_fault_helper.c
1 #include "test_fault_helper.h"
7 #include <TargetConditionals.h>
9 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
10 #define MEMSIZE (1L<<28)
12 #define MEMSIZE (1L<<30)
15 static char* memblock
;
21 int pgsz
= getpagesize();
24 memblock
= (char *)mmap(NULL
, MEMSIZE
, PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
, -1, 0);
25 VERIFY(memblock
!= MAP_FAILED
, "mmap failed");
27 /* make sure memory is paged */
28 for (ptr
= memblock
; ptr
< memblock
+ MEMSIZE
; ptr
+= pgsz
) {
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");
36 retval
= mprotect(memblock
, MEMSIZE
, PROT_READ
| PROT_WRITE
);
37 VERIFY(retval
== 0, "mprotect failed");
39 return PERFINDEX_SUCCESS
;
43 test_fault_helper(int thread_id
, int num_threads
, long long length
, testtype_t testtype
)
46 int pgsz
= getpagesize();
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
;
54 if (thread_id
< num_pages
% num_threads
) {
55 region_start
+= thread_id
;
58 region_start
+= num_pages
% num_threads
;
63 region_end
= region_start
+ region_len
;
65 long long left
= length
;
68 for (ptr
= memblock
+ region_start
; ptr
< memblock
+ region_end
; ptr
+= pgsz
) {
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");
92 return PERFINDEX_SUCCESS
;