-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathlat_pagefault.c
202 lines (176 loc) · 4.43 KB
/
lat_pagefault.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* lat_pagefault.c - time a page fault in
*
* Usage: lat_pagefault [-C] [-P <parallel>] [-W <warmup>] [-N <repetitions>] file
*
* Copyright (c) 2000 Carl Staelin.
* Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
* additional restriction that results may published only if
* (1) the benchmark is unmodified, and
* (2) the version in the sccsid below is included in the report.
* Support for this development by Sun Microsystems is gratefully acknowledged.
*/
char *id = "$Id$\n";
#include "bench.h"
#define CHK(x) if ((x) == -1) { perror("x"); exit(1); }
typedef struct _state {
int fd;
int size;
int npages;
int clone;
char* file;
char* where;
size_t* pages;
} state_t;
void initialize(iter_t iterations, void *cookie);
void cleanup(iter_t iterations, void *cookie);
void benchmark(iter_t iterations, void * cookie);
void benchmark_mmap(iter_t iterations, void * cookie);
int
main(int ac, char **av)
{
int parallel = 1;
int warmup = 0;
int repetitions = TRIES;
int c;
double t_mmap;
double t_combined;
struct stat st;
struct _state state;
char buf[2048];
char* usage = "[-C] [-P <parallel>] [-W <warmup>] [-N <repetitions>] file\n";
state.clone = 0;
while (( c = getopt(ac, av, "P:W:N:C")) != EOF) {
switch(c) {
case 'P':
parallel = atoi(optarg);
if (parallel <= 0) lmbench_usage(ac, av, usage);
break;
case 'W':
warmup = atoi(optarg);
break;
case 'N':
repetitions = atoi(optarg);
break;
case 'C':
state.clone = 1;
break;
default:
lmbench_usage(ac, av, usage);
break;
}
}
if (optind != ac - 1 ) {
lmbench_usage(ac, av, usage);
}
state.file = av[optind];
CHK(stat(state.file, &st));
state.npages = st.st_size / (size_t)getpagesize();
#ifdef MS_INVALIDATE
benchmp(initialize, benchmark_mmap, cleanup, 0, parallel,
warmup, repetitions, &state);
t_mmap = gettime() / (double)get_n();
benchmp(initialize, benchmark, cleanup, 0, parallel,
warmup, repetitions, &state);
t_combined = gettime() / (double)get_n();
settime(get_n() * (t_combined - t_mmap));
sprintf(buf, "Pagefaults on %s", state.file);
micro(buf, state.npages * get_n());
#endif
return(0);
}
void
initialize(iter_t iterations, void* cookie)
{
int i, npages, pagesize;
int *p;
unsigned int r;
struct stat sbuf;
state_t *state = (state_t *) cookie;
if (iterations) return;
if (state->clone) {
char buf[128];
char* s;
/* copy original file into a process-specific one */
sprintf(buf, "%d", (int)getpid());
s = (char*)malloc(strlen(state->file) + strlen(buf) + 1);
sprintf(s, "%s%d", state->file, (int)getpid());
if (cp(state->file, s, S_IREAD|S_IWRITE) < 0) {
perror("Could not copy file");
unlink(s);
exit(1);
}
state->file = s;
}
CHK(state->fd = open(state->file, 0));
if (state->clone) unlink(state->file);
CHK(fstat(state->fd, &sbuf));
srand(getpid());
pagesize = getpagesize();
state->size = sbuf.st_size;
state->size -= state->size % pagesize;
state->npages = state->size / pagesize;
state->pages = permutation(state->npages, pagesize);
if (state->size < 1024*1024) {
fprintf(stderr, "lat_pagefault: %s too small\n", state->file);
exit(1);
}
state->where = mmap(0, state->size,
PROT_READ, MAP_SHARED, state->fd, 0);
#ifdef MS_INVALIDATE
if (msync(state->where, state->size, MS_INVALIDATE) != 0) {
perror("msync");
exit(1);
}
#endif
}
void
cleanup(iter_t iterations, void* cookie)
{
state_t *state = (state_t *) cookie;
if (iterations) return;
munmap(state->where, state->size);
if (state->fd >= 0) close(state->fd);
free(state->pages);
}
void
benchmark(iter_t iterations, void* cookie)
{
int i;
int sum = 0;
state_t *state = (state_t *) cookie;
while (iterations-- > 0) {
for (i = 0; i < state->npages; ++i) {
sum += *(state->where + state->pages[i]);
}
munmap(state->where, state->size);
state->where = mmap(0, state->size,
PROT_READ, MAP_SHARED, state->fd, 0);
#ifdef MS_INVALIDATE
if (msync(state->where, state->size, MS_INVALIDATE) != 0) {
perror("msync");
exit(1);
}
#endif
}
use_int(sum);
}
void
benchmark_mmap(iter_t iterations, void* cookie)
{
int i;
int sum = 0;
state_t *state = (state_t *) cookie;
while (iterations-- > 0) {
munmap(state->where, state->size);
state->where = mmap(0, state->size,
PROT_READ, MAP_SHARED, state->fd, 0);
#ifdef MS_INVALIDATE
if (msync(state->where, state->size, MS_INVALIDATE) != 0) {
perror("msync");
exit(1);
}
#endif
}
use_int(sum);
}