-
Notifications
You must be signed in to change notification settings - Fork 49
/
systime.c
311 lines (250 loc) · 6.64 KB
/
systime.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <inttypes.h>
#ifndef _WIN32
#include <pthread.h>
#endif
#ifdef _WIN32
double getCpuTime(int type)
{
FILETIME crtime[1];
FILETIME xittime[1];
FILETIME systime[1];
FILETIME usrtime[1];
SYSTEMTIME timeconv[1];
double ans = 0;
memset (timeconv, 0, sizeof(SYSTEMTIME));
switch( type ) {
case 0:
GetSystemTimeAsFileTime (xittime);
FileTimeToSystemTime (xittime, timeconv);
ans = (double)timeconv->wDayOfWeek * 3600 * 24;
break;
case 1:
GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
FileTimeToSystemTime (usrtime, timeconv);
break;
case 2:
GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
FileTimeToSystemTime (systime, timeconv);
break;
}
ans += (double)timeconv->wHour * 3600;
ans += (double)timeconv->wMinute * 60;
ans += (double)timeconv->wSecond;
ans += (double)timeconv->wMilliseconds / 1000;
return ans;
}
#else
#include <time.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <sys/time.h>
double getCpuTime(int type)
{
struct rusage used[1];
struct timeval tv[1];
switch( type ) {
case 0:
gettimeofday(tv, NULL);
return (double)tv->tv_sec + (double)tv->tv_usec / 1000000;
case 1:
getrusage(RUSAGE_SELF, used);
return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
case 2:
getrusage(RUSAGE_SELF, used);
return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
}
return 0;
}
#endif
uint64_t myrandom(uint32_t *seed, uint64_t modulo) {
uint64_t ans = 0;
ans |= rand_r(seed) % 32768;
ans <<= 15;
ans |= rand_r(seed) % 32768;
if (modulo >> 30) {
ans <<= 15;
ans |= rand_r(seed) % 32768;
}
return ans % modulo;
}
int towerHeight(uint32_t *seed, uint32_t range) {
uint32_t value = myrandom(seed, range);
int height = 1;
while(range >>= 1)
if(value < range)
height++;
else
break;
return height;
}
typedef struct {
int fd, idx, cnt, upd;
char *base, *map, type;
uint32_t seed[1];
uint64_t size;
} ThreadArg;
#ifndef _WIN32
void *execthrd (void *vals) {
#else
uint32_t __stdcall execthrd (void *vals) {
#endif
ThreadArg *args = vals;
int height, i, j, k;
char *page;
off_t off;
page = malloc(262144);
for (i = 0; i < args->cnt; i++) {
off = myrandom(args->seed, args->size - 262144) & ~0xfffLL;
height = towerHeight(args->seed, 262144);
// simulate operation on interior node
if (i % args->upd) {
for (j = 0; j < height; j++)
args->base[off + myrandom(args->seed, 262144)] += 1;
continue;
}
// simulate leaf level disk operation
switch(args->type) {
case 'm':
madvise(args->map + off, 262144, MADV_WILLNEED);
for(k = 0; k < args->upd; k++) {
height = towerHeight(args->seed, 262144);
for(j = 0; j < height; j++) {
uint32_t x = myrandom(args->seed, 262144);
args->map[off + x] = args->base[off + x];
}
}
madvise(args->map + off, 262144, MADV_DONTNEED);
break;
case 'd':
j = pread (args->fd, page, 262144, off);
if (j < 262144) {
printf("pread failed, errno = %d offset = %" PRIx64 " len = %d\n", errno, off, j);
exit(1);
}
for(k = 0; k < args->upd; k++) {
height = towerHeight(args->seed, 262144);
for(j = 0; j < height; j++) {
uint32_t x = myrandom(args->seed, 262144);
page[x] = args->base[off+ x];
}
}
j = pwrite (args->fd, page, 262144, off);
if (j < 262144) {
printf("pwrite failed, errno = %d offset = %" PRIx64 " len = %d\n", errno, off, j);
exit(1);
}
continue;
}
}
return NULL;
}
char usage[] = "usage: %s type filename reps megs upd thrds\n"
" where type is:\n"
" m - use full file memory map\n"
" d - use disk read/writes for leaves\n"
" reps is the number of random pages to simulate\n"
" filename is the name of the test disk file\n"
" megs is the size of the test file in megabytes\n"
" upd is number of buffered leaf updates\n"
" thrds is the number of threads to fire\n\n";
int main (int argc, char **argv) {
uint64_t size = 1024LL * 1024LL, off;
int fd, scale, upd, nthrds;
int cnt, idx, err;
#ifndef _WIN32
pthread_t *threads;
#else
HANDLE *threads;
#endif
ThreadArg *args;
char page[262144];
char *base, *map;
double start[3];
float elapsed;
int height;
if (argc < 7) {
fprintf (stderr, usage, argv[0]);
exit(1);
}
fd = open (argv[2], O_CREAT | O_RDWR, 0666);
cnt = atoi(argv[3]);
scale = atoi(argv[4]);
upd = atoi(argv[5]);
nthrds = atoi(argv[6]);
// store interior nodes in first segment
off = 0;
size *= scale;
if (lseek(fd, 0L, 2) < size)
while(off < size)
pwrite (fd, page, 262144, off), off += 262144;
base = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
off = size;
// store leaf nodes in second segment
if (lseek(fd, 0L, 2) < 2 * size)
while (off < 2 * size)
pwrite (fd, page, 262144, off), off += 262144;
switch(argv[1][0]) {
case 'm':
map = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, size);
if (map == MAP_FAILED) {
printf("mmap failed, errno = %d\n", errno);
exit(1);
}
break;
case 'd':
break;
default:
printf("invalid simulation type: %c\n\n", argv[1][0]);
fprintf (stderr, usage, argv[0]);
exit(1);
}
start[0] = getCpuTime(0);
start[1] = getCpuTime(1);
start[2] = getCpuTime(2);
#ifndef _WIN32
threads = malloc (nthrds * sizeof(pthread_t));
#else
threads = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, nthrds * sizeof(HANDLE));
#endif
args = malloc (nthrds * sizeof(ThreadArg));
// fire off threads
for( idx = 0; idx < nthrds; idx++ ) {
args[idx].type = argv[1][0];
args[idx].seed[0] = idx;
args[idx].size = size;
args[idx].base = base;
args[idx].map = map;
args[idx].idx = idx;
args[idx].upd = upd;
args[idx].cnt = cnt;
args[idx].fd = fd;
#ifndef _WIN32
if( (err = pthread_create (threads + idx, NULL, execthrd, args + idx)) )
fprintf(stderr, "Error creating thread %d\n", err);
#else
threads[idx] = (HANDLE)_beginthreadex(NULL, 65536, execthrd, args + idx, 0, NULL);
#endif
}
// wait for termination
#ifndef _WIN32
for( idx = 0; idx < nthrds; idx++ )
pthread_join (threads[idx], NULL);
#else
WaitForMultipleObjects (nthrds, threads, TRUE, INFINITE);
for( idx = 0; idx < nthrds; idx++ )
CloseHandle(threads[idx]);
#endif
elapsed = getCpuTime(0) - start[0];
fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
elapsed = getCpuTime(1) - start[1];
fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
elapsed = getCpuTime(2) - start[2];
fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
return 0;
}