]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/perf_index/stress_fault.c
1 #include "perf_index.h"
3 #include <TargetConditionals.h>
6 #define MEMSIZE (1L<<28)
8 #define MEMSIZE (1L<<30)
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
};
19 static char *memblock
;
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);
25 /* make sure memory is paged */
26 for(ptr
= memblock
; ptr
<memblock
+MEMSIZE
; ptr
+= pgsz
) {
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
);
34 void stress_fault_helper(int thread_id
, int num_threads
, long long length
, testtype_t testtype
) {
36 int pgsz
= getpagesize();
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
;
43 if(thread_id
< num_pages
% num_threads
) {
44 region_start
+= thread_id
;
48 region_start
+= num_pages
% num_threads
;
53 region_end
= region_start
+ region_len
;
55 long long left
= length
;
58 for(ptr
= memblock
+region_start
; ptr
<memblock
+region_end
; ptr
+= pgsz
) {
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);
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);
79 DECL_TEST(stress_fault
) {
80 stress_fault_helper(thread_id
, num_threads
, length
, TESTFAULT
);
83 DECL_TEST(stress_zfod
) {
84 stress_fault_helper(thread_id
, num_threads
, length
, TESTZFOD
);