|
1 |
| - |
2 |
| -#define _GNU_SOURCE |
3 |
| -#include <stdio.h> |
4 |
| -#include <pthread.h> |
5 |
| -#include <sched.h> |
6 |
| -#include <sys/mman.h> |
7 |
| -#include <sys/types.h> |
8 |
| -#include <sys/wait.h> |
9 |
| -#include <stdlib.h> |
10 |
| -#include <string.h> |
11 |
| -#include <unistd.h> |
12 |
| -#include <fcntl.h> |
13 |
| -#include <sys/syscall.h> |
14 |
| -#include <signal.h> |
15 |
| - |
16 |
| -#define SYS_vrange 314 |
17 |
| - |
18 |
| -#define VRANGE_VOLATILE 0 /* unpin all pages so VM can discard them */ |
19 |
| -#define VRANGE_NOVOLATILE 1 /* pin all pages so VM can't discard them */ |
20 |
| - |
21 |
| -#define VRANGE_MODE_SHARED 0x1 /* discard all pages of the range */ |
22 |
| - |
23 |
| - |
24 |
| - |
25 |
| -#define VRANGE_MODE 0x1 |
26 |
| - |
27 |
| -static int vrange(unsigned long start, size_t length, int mode, int *purged) |
28 |
| -{ |
29 |
| - return syscall(SYS_vrange, start, length, mode, purged); |
30 |
| -} |
31 |
| - |
32 |
| - |
33 |
| -static int mvolatile(void *addr, size_t length) |
34 |
| -{ |
35 |
| - return vrange((long)addr, length, VRANGE_VOLATILE, 0); |
36 |
| -} |
37 |
| - |
38 |
| - |
39 |
| -static int mnovolatile(void *addr, size_t length, int* purged) |
40 |
| -{ |
41 |
| - return vrange((long)addr, length, VRANGE_NOVOLATILE, purged); |
42 |
| -} |
43 |
| - |
44 |
| - |
45 |
| -char* vaddr; |
46 |
| -#define PAGE_SIZE (4*1024) |
47 |
| -#define CHUNK (4*1024*4) |
48 |
| -#define CHUNKNUM 26 |
49 |
| -#define FULLSIZE (CHUNK*CHUNKNUM + 2*PAGE_SIZE) |
50 |
| - |
51 |
| -void generate_pressure(megs) |
52 |
| -{ |
53 |
| - pid_t child; |
54 |
| - int one_meg = 1024*1024; |
55 |
| - char *addr; |
56 |
| - int i, status; |
57 |
| - |
58 |
| - child = fork(); |
59 |
| - if (!child) { |
60 |
| - for (i=0; i < megs; i++) { |
61 |
| - addr = malloc(one_meg); |
62 |
| - bzero(addr, one_meg); |
63 |
| - } |
64 |
| - exit(0); |
65 |
| - } |
66 |
| - |
67 |
| - waitpid(child, &status, 0); |
68 |
| - return; |
69 |
| -} |
70 |
| - |
71 |
| -void sigaction_sigbusy(int signum, siginfo_t *info, void *ctxt) |
72 |
| -{ |
73 |
| - char *ptr; |
74 |
| - int ret; |
75 |
| - char x; |
76 |
| - long len; |
77 |
| - if (signum != SIGBUS) |
78 |
| - return; |
79 |
| - |
80 |
| - ptr = info->si_addr; |
81 |
| - |
82 |
| - mnovolatile(ptr, CHUNK, &ret); |
83 |
| - printf("Fixing up data\n"); |
84 |
| - len = (ptr - vaddr)/CHUNK; |
85 |
| - x = 'A' + len; |
86 |
| - memset(ptr, x, CHUNK); |
87 |
| - printf("%c\n", x); |
88 |
| - |
89 |
| -} |
90 |
| - |
91 |
| -void signal_handler_sigbusy(int signum) |
92 |
| -{ |
93 |
| - if (signum == SIGBUS) { |
94 |
| - printf("We received SIGBUSY\n"); |
95 |
| - } |
96 |
| -} |
97 |
| - |
98 |
| -void register_signal_handler() |
99 |
| -{ |
100 |
| - struct sigaction action; |
101 |
| - action.sa_sigaction = &sigaction_sigbusy; |
102 |
| - sigemptyset(&action.sa_mask); |
103 |
| - action.sa_flags = SA_SIGINFO; |
104 |
| - action.sa_restorer = NULL; |
105 |
| - sigaction(SIGBUS, &action, NULL); |
106 |
| -} |
107 |
| - |
108 |
| -int main(int argc, char *argv[]) |
109 |
| -{ |
110 |
| - int i, purged; |
111 |
| - char* file = NULL; |
112 |
| - int fd; |
113 |
| - int pressure = 0; |
114 |
| - int opt; |
115 |
| - |
116 |
| - //signal(SIGBUS, signal_handler_sigbusy |
117 |
| - //sigaction(SIGBUS, sigaction_sigbusy, NULL); |
118 |
| - register_signal_handler(); |
119 |
| - |
120 |
| - /* Process arguments */ |
121 |
| - while ((opt = getopt(argc, argv, "p:f:"))!=-1) { |
122 |
| - switch(opt) { |
123 |
| - case 'p': |
124 |
| - pressure = atoi(optarg); |
125 |
| - break; |
126 |
| - case 'f': |
127 |
| - file = optarg; |
128 |
| - break; |
129 |
| - default: |
130 |
| - printf("Usage: %s [-p <mempressure in megs>] [-f <filename>]\n", argv[0]); |
131 |
| - printf(" -p: Amount of memory pressure to generate\n"); |
132 |
| - printf(" -f: Use a file\n"); |
133 |
| - exit(-1); |
134 |
| - } |
135 |
| - } |
136 |
| - |
137 |
| - if (file) { |
138 |
| - file = argv[1]; |
139 |
| - fd = open(file, O_RDWR); |
140 |
| - vaddr = mmap(0, FULLSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
141 |
| - } else { |
142 |
| - vaddr = malloc(FULLSIZE); |
143 |
| - } |
144 |
| - |
145 |
| - purged = 0; |
146 |
| - vaddr += PAGE_SIZE-1; |
147 |
| - vaddr -= (long)vaddr % PAGE_SIZE; |
148 |
| - |
149 |
| - for(i=0; i < CHUNKNUM; i++) |
150 |
| - memset(vaddr + (i*CHUNK), 'A'+i, CHUNK); |
151 |
| - |
152 |
| - |
153 |
| - for(i=0; i < CHUNKNUM; ) { |
154 |
| - mvolatile(vaddr + (i*CHUNK), CHUNK); |
155 |
| - i+=2; |
156 |
| - } |
157 |
| - |
158 |
| -// for(i=0; i < CHUNKNUM; i++) |
159 |
| -// printf("%c\n", vaddr[i*CHUNK]); |
160 |
| - |
161 |
| - generate_pressure(pressure); |
162 |
| - |
163 |
| -// for(i=0; i < CHUNKNUM; i++) |
164 |
| -// printf("%c\n", vaddr[i*CHUNK]); |
165 |
| - |
166 |
| - /*for(i=0; i < CHUNKNUM; ) { |
167 |
| - int ret; |
168 |
| - ret = mnovolatile(vaddr + (i*CHUNK), CHUNK, &purged); |
169 |
| - i+=2; |
170 |
| - }*/ |
171 |
| - |
172 |
| - if (purged) |
173 |
| - printf("Data purged!\n"); |
174 |
| - for(i=0; i < CHUNKNUM; i++) |
175 |
| - printf("%c\n", vaddr[i*CHUNK]); |
176 |
| - |
177 |
| - |
178 |
| - |
179 |
| - return 0; |
180 |
| -} |
181 |
| -
|
| 1 | + |
| 2 | +#define _GNU_SOURCE |
| 3 | +#include <stdio.h> |
| 4 | +#include <pthread.h> |
| 5 | +#include <sched.h> |
| 6 | +#include <sys/mman.h> |
| 7 | +#include <sys/types.h> |
| 8 | +#include <sys/wait.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | +#include <unistd.h> |
| 12 | +#include <fcntl.h> |
| 13 | +#include <sys/syscall.h> |
| 14 | +#include <signal.h> |
| 15 | + |
| 16 | +#define SYS_vrange 314 |
| 17 | + |
| 18 | +#define VRANGE_VOLATILE 0 /* unpin all pages so VM can discard them */ |
| 19 | +#define VRANGE_NOVOLATILE 1 /* pin all pages so VM can't discard them */ |
| 20 | + |
| 21 | +#define VRANGE_MODE_SHARED 0x1 /* discard all pages of the range */ |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +#define VRANGE_MODE 0x1 |
| 26 | + |
| 27 | +static int vrange(unsigned long start, size_t length, int mode, int *purged) |
| 28 | +{ |
| 29 | + return syscall(SYS_vrange, start, length, mode, purged); |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +static int mvolatile(void *addr, size_t length) |
| 34 | +{ |
| 35 | + return vrange((long)addr, length, VRANGE_VOLATILE, 0); |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +static int mnovolatile(void *addr, size_t length, int* purged) |
| 40 | +{ |
| 41 | + return vrange((long)addr, length, VRANGE_NOVOLATILE, purged); |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +char* vaddr; |
| 46 | +#define PAGE_SIZE (4*1024) |
| 47 | +#define CHUNK (4*1024*4) |
| 48 | +#define CHUNKNUM 26 |
| 49 | +#define FULLSIZE (CHUNK*CHUNKNUM + 2*PAGE_SIZE) |
| 50 | + |
| 51 | +void generate_pressure(megs) |
| 52 | +{ |
| 53 | + pid_t child; |
| 54 | + int one_meg = 1024*1024; |
| 55 | + char *addr; |
| 56 | + int i, status; |
| 57 | + |
| 58 | + child = fork(); |
| 59 | + if (!child) { |
| 60 | + for (i=0; i < megs; i++) { |
| 61 | + addr = malloc(one_meg); |
| 62 | + bzero(addr, one_meg); |
| 63 | + } |
| 64 | + exit(0); |
| 65 | + } |
| 66 | + |
| 67 | + waitpid(child, &status, 0); |
| 68 | + return; |
| 69 | +} |
| 70 | + |
| 71 | +void sigaction_sigbusy(int signum, siginfo_t *info, void *ctxt) |
| 72 | +{ |
| 73 | + char *ptr; |
| 74 | + int ret; |
| 75 | + char x; |
| 76 | + long len; |
| 77 | + if (signum != SIGBUS) |
| 78 | + return; |
| 79 | + |
| 80 | + ptr = info->si_addr; |
| 81 | + |
| 82 | + mnovolatile(ptr, CHUNK, &ret); |
| 83 | + printf("Fixing up data\n"); |
| 84 | + len = (ptr - vaddr)/CHUNK; |
| 85 | + x = 'A' + len; |
| 86 | + memset(ptr, x, CHUNK); |
| 87 | + printf("%c\n", x); |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +void signal_handler_sigbusy(int signum) |
| 92 | +{ |
| 93 | + if (signum == SIGBUS) { |
| 94 | + printf("We received SIGBUSY\n"); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void register_signal_handler() |
| 99 | +{ |
| 100 | + struct sigaction action; |
| 101 | + action.sa_sigaction = &sigaction_sigbusy; |
| 102 | + sigemptyset(&action.sa_mask); |
| 103 | + action.sa_flags = SA_SIGINFO; |
| 104 | + action.sa_restorer = NULL; |
| 105 | + sigaction(SIGBUS, &action, NULL); |
| 106 | +} |
| 107 | + |
| 108 | +int main(int argc, char *argv[]) |
| 109 | +{ |
| 110 | + int i, purged; |
| 111 | + char* file = NULL; |
| 112 | + int fd; |
| 113 | + int pressure = 0; |
| 114 | + int opt; |
| 115 | + |
| 116 | + //signal(SIGBUS, signal_handler_sigbusy |
| 117 | + //sigaction(SIGBUS, sigaction_sigbusy, NULL); |
| 118 | + register_signal_handler(); |
| 119 | + |
| 120 | + /* Process arguments */ |
| 121 | + while ((opt = getopt(argc, argv, "p:f:"))!=-1) { |
| 122 | + switch(opt) { |
| 123 | + case 'p': |
| 124 | + pressure = atoi(optarg); |
| 125 | + break; |
| 126 | + case 'f': |
| 127 | + file = optarg; |
| 128 | + break; |
| 129 | + default: |
| 130 | + printf("Usage: %s [-p <mempressure in megs>] [-f <filename>]\n", argv[0]); |
| 131 | + printf(" -p: Amount of memory pressure to generate\n"); |
| 132 | + printf(" -f: Use a file\n"); |
| 133 | + exit(-1); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (file) { |
| 138 | + file = argv[1]; |
| 139 | + fd = open(file, O_RDWR); |
| 140 | + vaddr = mmap(0, FULLSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 141 | + } else { |
| 142 | + vaddr = malloc(FULLSIZE); |
| 143 | + } |
| 144 | + |
| 145 | + purged = 0; |
| 146 | + vaddr += PAGE_SIZE-1; |
| 147 | + vaddr -= (long)vaddr % PAGE_SIZE; |
| 148 | + |
| 149 | + for(i=0; i < CHUNKNUM; i++) |
| 150 | + memset(vaddr + (i*CHUNK), 'A'+i, CHUNK); |
| 151 | + |
| 152 | + |
| 153 | + for(i=0; i < CHUNKNUM; ) { |
| 154 | + mvolatile(vaddr + (i*CHUNK), CHUNK); |
| 155 | + i+=2; |
| 156 | + } |
| 157 | + |
| 158 | +// for(i=0; i < CHUNKNUM; i++) |
| 159 | +// printf("%c\n", vaddr[i*CHUNK]); |
| 160 | + |
| 161 | + generate_pressure(pressure); |
| 162 | + |
| 163 | +// for(i=0; i < CHUNKNUM; i++) |
| 164 | +// printf("%c\n", vaddr[i*CHUNK]); |
| 165 | + |
| 166 | + /*for(i=0; i < CHUNKNUM; ) { |
| 167 | + int ret; |
| 168 | + ret = mnovolatile(vaddr + (i*CHUNK), CHUNK, &purged); |
| 169 | + i+=2; |
| 170 | + }*/ |
| 171 | + |
| 172 | + if (purged) |
| 173 | + printf("Data purged!\n"); |
| 174 | + for(i=0; i < CHUNKNUM; i++) |
| 175 | + printf("%c\n", vaddr[i*CHUNK]); |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + return 0; |
| 180 | +} |
| 181 | + |
0 commit comments