-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk-benchmark.c
361 lines (320 loc) · 10 KB
/
disk-benchmark.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <limits.h>
#define DEFAULT_BLOCK_SIZE 4 * 1024 * 1024 // 4M
#define DEFAULT_TIME_LIMIT 15 // 15s
#define S_TO_NS 1000000000 // Seconds to nanoseconds ratio
typedef unsigned int uint;
typedef unsigned long long int uint64;
typedef unsigned short int uint16;
typedef enum {
READ,
WRITE,
READ_WRITE,
WRITE_READ,
READ_WRITE_READ
} TEST_TYPE;
typedef struct {
uint count_op;
time_t time_partial;
time_t time_total;
uint64 bytes_partial;
uint64 bytes_total;
} TEST;
struct timespec time_now() {
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
return now;
}
int parse_block_size(char *a) {
int i;
int unit = 1;
for(i=0; a[i] != '\0'; i++) {
if(!isdigit(a[i])) {
switch(a[i]) {
case 'K':
unit = 1024;
break;
case 'M':
unit = 1024 * 1024;
break;
default:
fprintf(stderr, "Invalid format %s, only K and M allowed\n", a);
exit(EXIT_FAILURE);
}
a[i] = '\0';
break;
}
}
if(i < 1) {
fprintf(stderr, "Invalid format %s, only K and M allowed\n", a);
exit(EXIT_FAILURE);
}
return atoi(a) * unit;
}
void fill_buffer(char *buffer, size_t size) {
for(int i=0; i<size; i+=2)
buffer[i]=(uint16) rand() % USHRT_MAX;
}
char *format_unit(char *str, double val) {
char *unit = "B";
if(val > 1024) {
val /= 1024;
unit = "KB";
}
if(val > 1024) {
val /= 1024;
unit = "MB";
}
if(val > 1024) {
val /= 1024;
unit = "GB";
}
if(val > 1024) {
val /= 1024;
unit = "TB";
}
sprintf(str, "%4.2f%s", val, unit);
return str;
}
char *format_speed(char *str, uint64 bytes, time_t time) {
double speed = (double)bytes / ((double)time / S_TO_NS);
format_unit(str, speed);
strcat(str, "/s");
return str;
}
void print_partial(TEST *test, bool is_write) {
char s[20];
if(test->time_partial > S_TO_NS) { // 1 second
char *msg = (!is_write ? "Read" : "Write");
printf("%s %s\n", msg, format_speed(s, test->bytes_partial, test->time_partial));
test->bytes_partial = 0;
test->time_partial = 0;
}
}
void print_total(TEST *test, size_t block_size, bool is_write) {
char s1[20];
char s2[20];
char *type = (!is_write ? "Read" : "Write");
printf("Bytes %s: %s (%lluB) in %.2fs, %d blocks of %s\n",
type,
format_unit(s1, test->bytes_total),
test->bytes_total,
test->time_total / (double) S_TO_NS,
test->count_op,
format_unit(s2, block_size));
printf("%s speed: %s\n", type,
format_speed(s1, test->bytes_total, test->time_total));
}
ssize_t benchmark_operation(int fd, TEST *test, char *buffer, size_t block_size, bool is_write) {
struct timespec start, end;
ssize_t result;
start = time_now();
result = is_write ? // Is read or write operations
write(fd, buffer, block_size) :
read(fd, buffer, block_size);
end = time_now();
if(result < 0) {
perror("I/O operation error");
return result;
}
time_t time_elapsed = // Calculate time difference
(end.tv_sec - start.tv_sec) * S_TO_NS + // Convert to ns
(end.tv_nsec - start.tv_nsec);
// Update statistics
test->time_partial += time_elapsed;
test->time_total += time_elapsed;
test->bytes_partial += result;
test->bytes_total += result;
test->count_op++;
print_partial(test, is_write);
return result;
}
void benchmark_file(const char *file_name, TEST_TYPE test_type, size_t block_size, time_t time_limit, bool is_random) {
char *buffer = (char *)malloc(block_size * sizeof(char));
char *buffer_cmp = (char *)malloc(block_size * sizeof(char));
TEST t1 = {}, t2 = {}, t3 = {};
time_t time_total;
off_t file_size, result;
char str[20];
// Select flags for READ-ONLY or READ-WRITE
int open_mode =
test_type == READ_WRITE || test_type == READ_WRITE_READ ? O_RDWR :
test_type == WRITE_READ || test_type == WRITE ? O_RDWR | O_CREAT :
O_RDONLY; // default
// Open file
int fd = open(file_name, open_mode | /* O_DIRECT | */ O_SYNC, S_IRUSR | S_IWUSR);
if(fd < 0) {
fprintf(stderr, "Cannot open %s: %s (errno %d)\n", file_name, strerror(errno), errno);
exit(EXIT_FAILURE);
}
// Find file size
file_size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
printf("## %s test with block size %s, file %s...\n",
is_random ? "Random" : "Sequential",
format_unit(str, block_size), file_name);
do {
size_t buffer_size = block_size;
if(is_random) // Seek into a random location of the file
lseek(fd, ((double)rand()/RAND_MAX) * file_size - block_size, SEEK_SET);
switch (test_type) {
case READ_WRITE_READ:
case READ_WRITE:
// Read
result = benchmark_operation(fd, &t1, buffer, block_size, false);
if(result < 1) // check for errors
break;
lseek(fd, -result, SEEK_CUR); // Seek back to the initial position
buffer_size = result; // Write the same amount of data
case WRITE_READ:
case WRITE:
// Generate data for WRITE_READ and WRITE modes
if(test_type == WRITE_READ || test_type == WRITE)
fill_buffer(buffer, buffer_size);
// Write
result = benchmark_operation(fd, &t2, buffer, buffer_size, true);
if(result < 1) // check for errors
break;
// Do not read back with READ_WRITE and WRITE modes
if(test_type == READ_WRITE || test_type == WRITE)
break;
lseek(fd, -result, SEEK_CUR); // Seek back to the initial position
buffer_size = result; // Write the same amount of data
case READ:
result = benchmark_operation(fd, &t3, buffer_cmp, buffer_size, false);
// Compare between data written and data read back
if(test_type != READ && strncmp(buffer, buffer_cmp, block_size) != 0) {
fprintf(stderr, "Error: the data written differs from the data read back\n");
result = -1;
}
break;
}
time_total = (t1.time_total + t2.time_total + t3.time_total) / S_TO_NS;
} while(time_total < time_limit && result > 0); // Terminate test
// Close file
close(fd);
free(buffer);
free(buffer_cmp);
if(result == 0)
fprintf(stderr, "End-of-file reached, time limit not hit!\n");
else if(result < 0)
fprintf(stderr, "Test ended due to errors (errno %d)!\n", errno);
// Print results
switch (test_type) {
case READ_WRITE_READ:
case READ_WRITE:
puts("-- Results test Read before Write:");
print_total(&t1, block_size, false);
case WRITE_READ:
case WRITE:
puts("-- Results test Write:");
print_total(&t2, block_size, true);
if(test_type == READ_WRITE || test_type == WRITE)
break;
case READ:
puts("-- Results Read:");
print_total(&t3, block_size, false);
}
}
void help() {
puts("Usage of disk-benchmark:");
puts(" -r Read test.");
puts(" -w Write test. The data is randomly generated, this is a DESTRUCTIVE test");
puts(" -rw Read and write test. Reads first the data and writes it back.");
puts(" -wr Write and read test. Writes first the data and reads it back, i.e. checks if the data");
puts(" was written correctly. The data is randomly generated, this is a DESTRUCTIVE test.");
puts(" -rwr Read, write and read test. It verifies if the data was written correctly");
puts(" in a non destructive way. The data written is the data read.");
puts(" -h, --help Shows this help");
puts(" -s, --sequential Performs a sequential test");
puts(" -u, --random Performs a random test, a 4K block size will be used");
puts(" -b, --block-size Sets the block size for sequential test (K and M multiplier allowed)");
puts(" -t, --time-limit Duration of each test");
exit(EXIT_FAILURE);
}
int main(int argc, char **argv) {
TEST_TYPE test_type = READ;
char *file_name = NULL;
bool sequential_test = false;
bool random_test = false;
size_t block_size = DEFAULT_BLOCK_SIZE;
time_t time_limit = DEFAULT_TIME_LIMIT;
int i;
// initialize random seed
srand(time(NULL));
for(i=1; i<argc; i++) {
char *a = argv[i];
// Mode selection
if(strcmp("-r", a) == 0)
test_type = READ;
else if(strcmp("-w", a) == 0)
test_type = WRITE;
else if(strcmp("-rw", a) == 0)
test_type = READ_WRITE;
else if(strcmp("-wr", a) == 0)
test_type = WRITE_READ;
else if(strcmp("-rwr", a) == 0)
test_type = READ_WRITE_READ;
// Sequencial or random test
else if(strcmp("-s", a) == 0 || strcmp("--sequential", a) == 0)
sequential_test = true;
else if(strcmp("-u", a) == 0 || strcmp("--random", a) == 0)
random_test = true;
// Block size and time limit
else if(strcmp("-b", a) == 0 || strcmp("--block-size", a) == 0) {
i++;
if(i >= argc)
fprintf(stderr, "Block size not provided\n"), exit(EXIT_FAILURE);
block_size = parse_block_size(argv[i]);
}
else if(strcmp("-t", a) == 0 || strcmp("--time-limit", a) == 0) {
i++;
if(i >= argc)
fprintf(stderr, "Time limit not provided\n"), exit(EXIT_FAILURE);
time_limit = atoi(argv[i]);
}
else if(strcmp("-h", a) == 0 || strcmp("--help", a) == 0)
help();
else {
if(strncmp("-", a, 1)==0) { // Option not recognized
fprintf(stderr, "Option %s not recognized\n", a);
help();
}
file_name = a;
}
}
if(file_name == NULL) {
fputs("No filename provided\n", stderr);
help();
}
// Data loss checks
if(test_type != READ) {
char ans[20];
puts("The selected test will perform writing operation on the file.");
puts("Data LOSS can occur. Are you sure you want to continue? (yes/no)");
fgets(ans, 20, stdin);
if(strcmp("yes\n", ans) != 0)
exit(EXIT_FAILURE);
}
if(test_type == WRITE || test_type == WRITE_READ) {
char ans[20];
puts("Random data will be written over your existing data.");
puts("This action is NOT RECOVERABLE. Are you really sure you want to continue? (yes/no)");
fgets(ans, 20, stdin);
if(strcmp("yes\n", ans) != 0)
exit(EXIT_FAILURE);
}
if(sequential_test || sequential_test == false && random_test == false) // Default option
benchmark_file(file_name, test_type, block_size, time_limit, false);
if(random_test)
benchmark_file(file_name, test_type, 4 * 1024, time_limit, true); // 4K (i.e. 4k random)
exit(EXIT_SUCCESS);
}