-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathscaleread.c
224 lines (195 loc) · 4.63 KB
/
scaleread.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
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2003-2004 Silicon Graphics, Inc.
* All Rights Reserved.
*/
/*
* Test scaling of multiple processes opening/reading
* a number of small files simultaneously.
* - create <f> files
* - fork <n> processes
* - wait for all processes ready
* - start all proceses at the same time
* - each processes opens , read, closes each file
* - option to resync each process at each file
*
* test [-c cpus] [-b bytes] [-f files] [-v] [-s] [-S]
* OR
* test -i [-b bytes] [-f files]
*/
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void do_initfiles(void);
void slave(int);
#define VPRINT(x...) do { if(verbose) fprintf(x);} while(0)
#define perrorx(s) do {perror(s); exit(1);} while (0)
long bytes=8192;
int cpus=1;
int init=0;
int strided=0;
int files=1;
int blksize=512;
int syncstep=0;
int verbose=0;
typedef struct {
volatile long go;
long fill[15];
volatile long rdy[512];
} share_t;
share_t *sharep;
int
runon(int cpu)
{
#ifdef sys_sched_setaffinity
unsigned long mask[8];
if (cpu < 0 || cpu >= 512)
return -1;
memset(mask, 0, sizeof(mask));
mask[cpu/64] |= 1UL<<(cpu&63);
if (syscall(sys_sched_setaffinity, 0, sizeof(mask), mask))
return -1;
#endif
return 0;
}
long
scaled_atol(char *p)
{
long val;
char *pe;
val = strtol(p, &pe, 0);
if (*pe == 'K' || *pe == 'k')
val *= 1024L;
else if (*pe == 'M' || *pe == 'm')
val *= 1024L*1024L;
else if (*pe == 'G' || *pe == 'g')
val *= 1024L*1024L*1024L;
else if (*pe == 'p' || *pe == 'P')
val *= getpagesize();
return val;
}
int
main(int argc, char** argv) {
int shmid;
static char optstr[] = "c:b:f:sSivH";
int notdone, stat, i, j, c, er=0;
opterr=1;
while ((c = getopt(argc, argv, optstr)) != EOF)
switch (c) {
case 'c':
cpus = atoi(optarg);
break;
case 'b':
bytes = scaled_atol(optarg);
break;
case 'f':
files = atoi(optarg);
break;
case 'i':
init++;
break;
case 's':
syncstep++;
break;
case 'S':
strided++;
break;
case 'v':
verbose++;
break;
case '?':
er = 1;
break;
}
if (er) {
printf("usage: %s %s\n", argv[0], optstr);
exit(1);
}
if ((shmid = shmget(IPC_PRIVATE, sizeof (share_t), IPC_CREAT|SHM_R|SHM_W)) == -1)
perrorx("shmget failed");
sharep = (share_t*)shmat(shmid, (void*)0, SHM_R|SHM_W);
memset(sharep, -1, sizeof (share_t));
if (init) {
do_initfiles();
exit(0);
}
for (i=0; i<cpus; i++) {
if (fork() == 0)
slave(i);
}
for (i=0; i<files; i++) {
VPRINT(stderr, "%d:", i);
notdone = cpus;
do {
for (j=0; j<cpus; j++) {
if (sharep->rdy[j] == i) {
sharep->rdy[j] = -1;
VPRINT(stderr, " %d", j);
notdone--;
}
}
} while (notdone);
VPRINT(stderr, "\n");
sharep->go = i;
if (!syncstep)
break;
}
VPRINT(stderr, "\n");
while (wait(&stat)> 0)
VPRINT(stderr, ".");
VPRINT(stderr, "\n");
exit(0);
}
void
slave(int id)
{
int i, fd, byte;
char *buf, filename[32];
runon (id+1);
buf = malloc(blksize);
bzero(buf, blksize);
for (i=0; i<files; i++) {
if (!i || syncstep) {
sharep->rdy[id] = i;
while(sharep->go != i);
}
sprintf(filename, "/tmp/tst.%d", (strided ? ((i + id) % files) : i));
if ((fd = open (filename, O_RDONLY)) < 0) {
perrorx(filename);
}
for (byte=0; byte<bytes; byte+=blksize) {
if (read (fd, buf, blksize) != blksize)
perrorx("read of file failed");
}
close(fd);
}
exit(0);
}
void
do_initfiles(void)
{
int i, fd, byte;
char *buf, filename[32];
buf = malloc(blksize);
bzero(buf, blksize);
for (i=0; i<files; i++) {
sprintf(filename, "/tmp/tst.%d", i);
unlink(filename);
if ((fd = open (filename, O_RDWR|O_CREAT, 0644)) < 0)
perrorx(filename);
for (byte=0; byte<bytes; byte+=blksize) {
if (write (fd, buf, blksize) != blksize)
perrorx("write of file failed");
}
close(fd);
}
sync();
}