-
Notifications
You must be signed in to change notification settings - Fork 0
/
rab.c
370 lines (327 loc) · 9.13 KB
/
rab.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Reimplemented Andrew Benchmark (RAB)
*
* This benchmark is not the Modified Andrew Benchmark (MAB), but
* purports to exercise the same parts of the kernel. Scalability is
* the goal. Measuring transactions is cool too.
*
* Operating Systems & Architecture Group
* University of Texas at Austin - Department of Computer Sciences
* Copyright 2007-2009. All Rights Reserved.
* See LICENSE file for license terms.
*
* Primarily written by Alex Benn and Don Porter.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#ifdef TX
#include "tx.h"
#else
#define xbegin(a, b)
#define xend()
#endif
#define MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
struct timeval s;
int n_source_files;
void start_phase() {
printf("starting phase...\n");
gettimeofday(&s, NULL);
#ifdef BIG_TX
xbegin(TX_LIVE_DANGEROUSLY, NULL);
#endif
}
double end_phase() {
time_t ds;
time_t du;
struct timeval f;
struct rusage ru;
#ifdef PRE_XEND_TIME
gettimeofday(&f, NULL);
#endif
#ifdef BIG_TX
xend();
#endif
#ifndef PRE_XEND_TIME
gettimeofday(&f, NULL);
#endif
ds = f.tv_sec - s.tv_sec;
du = f.tv_usec - s.tv_usec;
printf("ending phase...\n");
return (ds) + ((double)du/1000000.0);
}
#if defined TX && !defined BIG_TX
#define lil_xbegin() xbegin(TX_LIVE_DANGEROUSLY, NULL)
#define lil_xend() xend()
#else
#define lil_xbegin()
#define lil_xend()
#endif
#define PATH_LEN 64
void make_files(int copy_filesize) {
int i, j;
char path[PATH_LEN];
char rand_buf[copy_filesize];
snprintf(path, PATH_LEN, "srcdir");
mkdir(path, S_IRWXU);
for (i=0; i<n_source_files; i++) {
int fd, err;
ssize_t n_written, n_left;
for (j=0; j<copy_filesize; j++) {
long int rnd = random();
rand_buf[j] = rnd%('~' - ' ') + ' ';
}
snprintf(path, PATH_LEN, "srcdir/f%d", i);
fd = open(path, O_CREAT | O_WRONLY, MODE);
if (fd < 0) {
perror("create files\n");
exit(-100);
}
n_left = copy_filesize;
do {
n_written = write(fd, rand_buf, n_left);
if (n_written < 0) {
perror("write init files\n");
exit(-100);
}
} while (n_left -= n_written);
do {
err = close(fd);
if (err < 0 && errno != -EINTR) {
perror("close init files\n");
exit(-100);
}
} while (err < 0 && errno == -EINTR);
}
}
/* each thread has its own directory called d0, d1, etc. */
void prepare(int n_threads, int copy_filesize) {
int i;
char path[PATH_LEN];
make_files(copy_filesize);
for (i = 0; i<n_threads; i++) {
snprintf(path, PATH_LEN, "d%d", i);
mkdir(path, S_IRWXU);
}
}
void cleanup() {
system("rm -rf d* srcdir");
}
#define P_PATH_LEN 256
/* phase 1: do a bunch of mkdirs */
void phase_1(int n_iters, int id) {
char path[P_PATH_LEN];
int i;
for (i=0; i<n_iters; i++) {
snprintf(path, P_PATH_LEN, "d%d/sd%d", id, i);
mkdir(path, S_IRWXU);
}
}
#define P2_SOURCE_DIR "srcdir"
/* const char *copy_targets[] = { */
/* "arbitrator.c", "bres.s", "cmr.c", "cvtalto.c", "cvtfont.c", */
/* "cvtv1v2.c", "display.h", "DrawString.c", "filelist", "font.c", */
/* "font.h", "fontmanip.c", "fontmanip.h", "fontnaming.c", */
/* "font.v0.h", "font.v1.h", "framebuf.h", "gacha12.c", "graphicops.c", */
/* "gxfind.c", "keymap.h", "logo.c", "makefile", "MakeLoadable.c", */
/* "menu.c", "menu.h", "mkfont.c", "mksail7.c", "mouse.c", "mousedd.c", */
/* "profile.c", "ProgramName.c", "putenv.c", "RasterFile.h", */
/* "rasterop.c", "sail7.c", "sun1bw.c", "sun1color.c", "sunbitmap.c", */
/* "suncolor.c", "test.c", "timetest.c", "towindow.c", "UserDrawS.c", */
/* "usergraphics.c", "usergraphics.h", "util.h", "vec.c", "window.c", */
/* "window.h", "windowops.c", "wm.c", */
/* "include/assert.h", "include/colorbuf.h", "include/ctype.h", */
/* "include/errno.h", "include/fcntl.h", "include/netdb.h", */
/* "include/sgtty.h", "include/signal.h", "include/stdio.h", "include/time.h", */
/* "include/netinet/in.h", "include/sys/dir.h", "include/sys/ioctl.h", */
/* "include/sys/socket.h", "include/sys/stat.h", "include/sys/ttychars.h", */
/* "include/sys/ttydev.h", "include/sys/types.h", "include/sys/wait.h" */
/* }; */
/* const int n_copy_targets = 71; */
#define BUF_SIZE 4*1048576
/* phase 2: copy a bunch of files from srcdir */
void phase_2(int n_iters, int id) {
char srcpath[P_PATH_LEN];
char dstpath[P_PATH_LEN];
char buf[BUF_SIZE];
int dstfds[n_iters];
int i, j, err, bytes_left, nchars;
int fdin;
for(j=0; j<n_source_files; j++) {
snprintf(srcpath, P_PATH_LEN, "%s/f%d", P2_SOURCE_DIR, j);
fdin = open(srcpath, O_RDONLY, MODE);
if (fdin < 0){
fprintf(stderr, "err opening %s\n",srcpath);
exit(-100);
}
for (i=0; i<n_iters; i++) {
snprintf(dstpath, P_PATH_LEN, "d%d/sd%d/c%d", id, i, j);
dstfds[i] = open(dstpath, O_CREAT | O_WRONLY, MODE);
if (dstfds[i] < 0) {
fprintf(stderr, "err opening %s\n",dstpath);
exit(-100);
}
}
while(nchars = read(fdin, buf, BUF_SIZE)) {
for (i=0; i<n_iters; i++) {
bytes_left = nchars;
do {
char *buf_left = buf;
err = write(dstfds[i], buf_left, bytes_left);
if (err < 0) {
fprintf(stderr, "err writing %s", srcpath);
exit(-100);
}
bytes_left -= err;
buf_left += err;
} while (bytes_left > 0);
}
}
for (i=0; i<n_iters; i++) {
err = close(dstfds[i]);
if (err < 0) {
perror("err doing close");
exit(-100);
}
}
err = close(fdin);
if (err < 0) {
perror("err doing close");
exit(-100);
}
}
}
/* phase 3: du on the resulting file tree
*/
void phase_3(int n_iters, int id) {
char dustr[P_PATH_LEN];
snprintf(dustr, P_PATH_LEN, "du d%d > /dev/null", id);
system(dustr);
}
/* phase 4: grep and sum on the resulting file tree
*/
void phase_4(int n_iters, int id) {
char grepstr[P_PATH_LEN];
int i;
snprintf(grepstr, P_PATH_LEN, "grep -r xoxoxoxo d%d > /dev/null", id);
system(grepstr);
for (i=0; i<n_iters; i++) {
snprintf(grepstr, P_PATH_LEN, "cd d%d; echo \"sum sd%d/* &> /dev/null\" | bash",id, i);
system(grepstr);
}
}
#define N_PROCESSORS 4 /* bad bad bad bad bad hack */
/* phase: infrastructure for spawning multiple parallel threads
* executing a function
* phase_func's arguments are the number of iterations and a unique
* id used to identify which subdirectory to perform ops in
*/
double phase(int n_iters, int n_threads, void (*phase_func)(int, int)) {
int i;
double duration = 0.0;
cpu_set_t mask;
CPU_ZERO(&mask);
start_phase();
for (i=0; i<n_threads; i++) {
int pid = fork();
if (pid == 0) {
CPU_SET(i%N_PROCESSORS, &mask);
sched_setaffinity(0, sizeof(cpu_set_t), &mask);
lil_xbegin();
phase_func(n_iters, i);
lil_xend();
exit(0);
} else if (pid < 0) {
perror("fork");
exit(-100);
}
}
for (i=0; i < n_threads; i++) {
int stat, retval;
do {
retval = waitpid(-1, &stat, __WALL);
if (retval == -1) {
perror("waitpid");
exit(-100);
}
} while (!WIFEXITED(stat) && !WIFSIGNALED(stat));
}
duration = end_phase();
return duration;
}
int flush_caches()
{
int fd, r, i;
sync();
sync();
sync();
fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
if(!fd){
printf("Failed to open drop_caches: %d\n", errno);
exit(1);
}
r = write(fd, "3", 1);
if(r != 1){
printf("Failed to write drop_caches: %d\n", errno);
exit(1);
}
close(fd);
}
int main (int argc, char * argv[])
{
int n_iters;
int n_threads = 1;
int n_bytes_to_copy = 1024;
double duration;
if (argc < 2) {
printf("Usage: %s <num iterations> [<num threads, default 1>, [<n files to copy, default 100>, [<n bytes per file, default 1k>]]]\n", argv[0]);
exit(-100);
}
n_source_files = 100;
n_iters = atoi(argv[1]);
if (argc >= 3) {
n_threads = atoi(argv[2]);
if (argc >= 4) {
n_source_files = atoi(argv[3]);
if (argc >= 5) {
n_bytes_to_copy = atoi(argv[4]);
}
}
}
flush_caches();
printf("n subdirs=%d, n input files=%d, size of input file=%d bytes, \n"
"n copies=%d, n threads=%d\n",
n_iters*40, n_source_files, n_bytes_to_copy, n_iters, n_threads);
prepare(n_threads, n_bytes_to_copy); /* 100 files, 1k long */
/* PHASE 1 */
printf("Phase 1: mkdir\n");
duration = phase(n_iters * 40, n_threads, &phase_1);
/* 40 is to make phase 1 comparable in size to other phases */
printf("duration: %g\n", duration);
/* PHASE 2 */
printf("Phase 2: copy\n");
duration = phase(n_iters, n_threads, &phase_2);
printf("duration: %g\n", duration);
/* PHASE 3 */
printf("Phase 3: du\n");
duration = phase(n_iters, n_threads, &phase_3);
printf("duration: %g\n", duration);
/* PHASE 4 */
/* This phase would do something linear in the size of the files.
* Discussed possibilities include grep and sum.
*/
printf("Phase 4: grep and sum\n");
duration = phase(n_iters, n_threads, &phase_4);
printf("duration: %g\n", duration);
cleanup();
return 0;
}