-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbench.c
More file actions
104 lines (100 loc) · 2.28 KB
/
fbench.c
File metadata and controls
104 lines (100 loc) · 2.28 KB
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
/* compile with: gcc syncbench.c -o syncbench -lrt -Wall -Wextra -Werror
run as:
./syncbench filename 1024 100 'd' # to test fdatasync()
./syncbench filename 1024 100 's' # to test fsync()
to perform 100 repetitions of writing 1024-byte chunks.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int mono_time(double *result)
{
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now)) {
return -1;
}
*result = (double)now.tv_sec + ((double)now.tv_nsec * 1e-9);
return 0;
}
int main(int argc, char **argv)
{
double t0, t1;
if (argc != 5) {
puts("Usage: syncbench file write_size write_qty (d|s)");
return 1;
}
if (strlen(argv[4]) != 1) {
puts("You're a bad person.");
return 1;
}
int d;
if (argv[4][0] == 'd') {
d = 1;
} else if (argv[4][0] == 's') {
d = 0;
} else {
puts("That last parameter should have been either 'd' or 's'.");
return 1;
}
int write_size = atoi(argv[2]);
int no_of_writes = atoi(argv[3]);
if ((write_size <= 0) || (no_of_writes <= 0)) {
puts("You want me to write no bytes?");
return 1;
}
int fd = open(argv[1], O_CREAT | O_WRONLY | O_EXCL);
if (fd == -1) {
if (errno == EEXIST) {
printf("Refusing to nuke existing file %s.\n", argv[1]);
} else {
printf("Can't open %s for write.\n", argv[1]);
}
return 1;
}
char *bytes = malloc(write_size);
if (bytes == NULL) {
puts("malloc() failed, FML.");
return 1;
}
int i;
for (i = 0; i < write_size; i++) {
bytes[i] = '0' + (i % 10);
}
if (mono_time(&t0)) {
puts("can't consult a clock.");
return 1;
}
for (i = 0; i < no_of_writes; i++) {
ssize_t r = write(fd, bytes, write_size);
if (r != write_size) {
puts("write() failed.");
return 1;
}
if (d) {
if (fsync(fd)) {
puts("fdatasync() failed.");
return 1;
}
} else {
if (fsync(fd)) {
puts("fsync() failed.");
return 1;
}
}
}
if (mono_time(&t1)) {
puts("really, can't consult the clock?");
return 1;
}
char *c = d ? "fdatasync" : "fsync";
printf("Performed %d %s()s in %f seconds.\n", no_of_writes, c, t1 - t0);
double rate = no_of_writes / (t1 - t0);
printf("That's %f calls/second, or %f seconds/call.\n", rate, 1 / rate);
return 0;
}