-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfstones.c
339 lines (287 loc) · 8.12 KB
/
fstones.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
/*
*
*
* Paul Stephen Borile
*
*
* f s t o n e s . c : Disk performance evaluator
*
*/
static char *Version = "3.0";
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int offset;
int fd, fdsync;
struct stat buf;
int seekerrs = 0, readerrs = 0, writerrs = 0;
int nfiles = 0;
double seektime;
double servtime;
#define OPTS "wrWRf:d:"
static struct option long_options[] = {
{"write", no_argument, 0, 'w' },
{"read", no_argument, 0, 'r' },
{"rwrite", no_argument, 0, 'W' },
{"rread", no_argument, 0, 'R' },
{"dir", required_argument, 0, 'd' },
{"size", required_argument, 0, 's' },
{"blocksize", required_argument, 0, 'b' },
{"file", required_argument, 0, 'f' },
{0, 0, 0, 0 }
};
void usage(char *str);
int main(argc, argv)
int argc;
char **argv;
{
int option_index = 0;
char filename[512];
int dir_operations = 0;
int dir_opt = 0;
int rread_opt = 0;
int rwrite_opt = 0;
int read_opt = 0;
int write_opt = 0;
int size_in_GB = 10; // default 10GB
int blocksize_in_K = 1024; // default 1 MB
long long bytes;
long long size;
long int blocksize;
long int randsize;
long int syncsize;
long long rnum;
time_t start, stop;
char *block;
int opt;
int rc = 0;
char str[1024];
while ((opt = getopt_long(argc, argv, OPTS, long_options, &option_index)) != -1)
{
switch (opt)
{
case 'f':
strcpy(filename, optarg);
break;
case 'b':
dir_opt = 1;
blocksize_in_K = atoi(optarg);
break;
case 's':
size_in_GB = atoi(optarg);
break;
case 'd':
dir_operations = atoi(optarg);
break;
case 'R':
rread_opt = 1;
break;
case 'W':
rwrite_opt = 1;
break;
case 'r':
read_opt = 1;
break;
case 'w':
write_opt = 1;
break;
default: /* '?' */
HELP:
fprintf(stderr, "Usage: %s [--read] [--write] [--rread] [--rwrite] --size <in_GB> --blocksize <in_K> --file <filename> --dir <num_of_dir_operations>\n", argv[0]);
exit(EXIT_FAILURE);
}
}
blocksize = blocksize_in_K * 1024;
block = malloc(blocksize);
bytes = size_in_GB * 1024L * 1024L * 1024L; // total bytes for read/write ops
syncsize = size_in_GB * 1024 * 1024; // size for sync operations is 1/1024 of normal since O_SYNC write are slow
randsize = syncsize;
memset(block, 'F', blocksize);
printf("** fstones %s - (C) 1989-2022 Paul Stephen Borile\n", Version);
printf("Blocksize for read/write operations is %dK\n", blocksize_in_K);
if (write_opt)
{
printf("Phase 1 ** Sequential file creation \n");
printf(" Creating a %Ld GB file\n", bytes/1024/1024/1024);
time(&start);
if ((fd = open(filename, O_CREAT|O_RDWR, 0644)) == -1 )
{
perror(filename);
exit(0);
}
long long donebytes = 0;
int ops_done = 0;
while (donebytes <= bytes)
{
rc = write(fd, block, blocksize);
if (rc != blocksize)
{
printf("write error on block %d\n", ops_done);
writerrs++;
}
donebytes += blocksize;
ops_done++;
}
close(fd);
time(&stop);
if ((stop - start) == 0 )
{
printf("Test too short, aborting\n");
}
else
{
printf(" Elapsed time %ld - %LdM/sec\n", (stop-start),
((bytes/(stop-start))/1024/1024));
servtime = (double)((double)(stop-start)/(double)ops_done);
printf(" Average write service time %.9f ms.\n", servtime*1000);
}
}
if (read_opt)
{
printf("Phase 2 ** Sequential file reading \n");
time(&start);
if ((fd = open(filename, O_CREAT|O_RDWR, 0644)) == -1 )
{
perror(filename);
exit(0);
}
long long donebytes = 0;
int ops_done = 0;
while (donebytes <= bytes)
{
rc = read(fd, block, blocksize);
if (rc != blocksize)
{
printf("read error on block %d\n", ops_done);
writerrs++;
}
donebytes += blocksize;
ops_done++;
}
close(fd);
time(&stop);
if ((stop - start) == 0 )
{
printf("Test too short, aborting\n");
}
else
{
printf(" Elapsed time %ld - %LdM/sec\n", (stop-start),
((bytes/(stop-start))/1024/1024));
servtime = (double)((double)(stop-start)/(double)ops_done);
printf(" Average read service time %.9f ms.\n",
servtime*1000);
}
}
if (rread_opt)
{
printf("Phase 3 ** Random file read \n");
time(&start);
srand(start);
if ((fd = open(filename, O_RDONLY)) == -1 )
{
perror(filename);
exit(0);
}
for (long int i=0; i<randsize; i++) {
rnum = (rand() % ((bytes/blocksize)-1));
rc = lseek(fd, rnum, SEEK_SET);
if ( rc != rnum )
{
printf("Seek error on block %Ld\n", rnum);
seekerrs++;
}
rc = read(fd, block, blocksize);
if (rc != blocksize)
{
printf("read error on block %ld\n", i);
readerrs++;
}
}
close(fd);
time(&stop);
if ((stop - start) == 0 )
{
printf("Test too short, aborting\n");
}
else
{
printf(" Elapsed time %ld - %LdM/sec\n", (stop-start),
((bytes/(stop-start))/1024/1024));
seektime = (double)((double)(stop-start)/(double)randsize);
printf(" Average seek-read time %.9f ms.\n",
seektime * 1000);
}
}
if (rwrite_opt)
{
printf("Phase 4 ** Random file write \n");
time(&start);
srand(start);
if ((fd = open(filename, O_WRONLY)) == -1 )
{
perror(filename);
exit(0);
}
for (long int i=0; i<randsize; i++) {
rnum = (rand() % ((bytes/blocksize)-1));
rc = lseek(fd, rnum, SEEK_SET);
if ( rc != rnum )
{
printf("Seek error on block %Ld\n", rnum);
seekerrs++;
}
rc = write(fd, block, blocksize);
if (rc != blocksize)
{
printf("write error on block %ld\n", i);
writerrs++;
}
}
close(fd);
time(&stop);
if ((stop - start) == 0 )
{
printf("Test too short, aborting\n");
}
else
{
printf(" Elapsed time %ld - %LdM/sec\n", (stop-start),
((bytes/(stop-start))/1024/1024));
seektime = (double)((double)(stop-start)/(double)randsize);
printf(" Average seek-write time %.9f ms.\n", seektime * 1000);
}
}
if (dir_opt)
{
printf("Phase 5 ** Directory test \n");
time(&start);
stat(filename, &buf);
for (int i=0; i<dir_operations; i++) {
sprintf(str, "%s-%d", filename, i);
link(filename, str);
stat(str, &buf);
}
for (int i=0; i<dir_operations; i++) {
sprintf(str, "%s-%d", filename, i);
stat(str, &buf);
unlink(str);
}
time(&stop);
if ((stop - start) == 0 )
{
printf("Test too short, aborting\n");
}
else
{
printf(" Elapsed time %ld - index %ld/sec\n", (stop-start),
dir_operations/(stop-start) );
}
}
}