-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsync-tester.c
154 lines (130 loc) · 2.95 KB
/
fsync-tester.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
/*
* fsync-tester.c
*
* Written by Theodore Ts'o, 3/21/09. Updated by Chris Mason to include
* the random writer thread
*
* This file may be redistributed under the terms of the GNU Public
* License, version 2.
*/
#define _FILE_OFFSET_BITS 64
#define _XOPEN_SOURCE 500
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#define SIZE (32768*32)
static char bigbuf[4096];
static float timeval_subtract(struct timeval *tv1, struct timeval *tv2)
{
return ((tv1->tv_sec - tv2->tv_sec) +
((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
}
static void random_io(int fd, loff_t total)
{
loff_t cur = 0;
int ret;
/* just some constant so our runs are always the same */
srand(4096);
while(1) {
/*
* we want a random offset into the file,
* but rand only returns max in. So we make
* it a random block number instead, and multiply
* by 4096.
*/
cur = rand();
cur = (cur * 4096) % (total - 4096);
/* align our offset to 4k */
cur = cur / 4096;
cur = cur * 4096;
ret = pwrite(fd, bigbuf, 4096, cur);
if (ret < 4096) {
fprintf(stderr, "short write ret %d cur %llu\n",
ret, (unsigned long long)cur);
exit(1);
}
}
}
int main(int argc, char **argv)
{
int fd;
struct timeval tv, tv2, start;
char buf[SIZE];
pid_t pid;
loff_t total = ((loff_t)256) * 1024 * 1024;
loff_t cur = 0;
int rand_fd;
int ret;
int i;
int status;
struct stat st;
memset(bigbuf, 0, 4096);
rand_fd = open("fsync-tester.rnd-file", O_WRONLY|O_CREAT);
if (rand_fd < 0) {
perror("open");
exit(1);
}
ret = fstat(rand_fd, &st);
if (ret < 0) {
perror("fstat");
exit(1);
}
if (st.st_size < total) {
printf("setting up random write file\n");
while(cur < total) {
ret = write(rand_fd, bigbuf, 4096);
if (ret <= 0) {
fprintf(stderr, "short write\n");
exit(1);
}
cur += ret;
}
printf("done setting up random write file\n");
}
fd = open("fsync-tester.tst-file", O_WRONLY|O_CREAT);
if (fd < 0) {
perror("open");
exit(1);
}
memset(buf, 'a', SIZE);
pid = fork();
if (pid == 0) {
printf("starting random io!\n");
random_io(rand_fd, total);
exit(0);
}
close(rand_fd);
gettimeofday(&start, NULL);
printf("starting fsync run\n");
for(i = 0; i < 60; i++) {
float pwrite_time;
gettimeofday(&tv2, NULL);
pwrite(fd, buf, SIZE, 0);
gettimeofday(&tv, NULL);
pwrite_time = timeval_subtract(&tv, &tv2);
ret = fsync(fd);
gettimeofday(&tv2, NULL);
printf("write time: %5.4fs fsync time: %5.4fs\n", pwrite_time,
timeval_subtract(&tv2, &tv));
if (ret) {
printf("last fsync failed! with return code %i", ret);
return r;
}
if (timeval_subtract(&tv2, &start) > 60)
break;
sleep(1);
}
printf("run done %d fsyncs total, killing random writer\n", i + 1);
fflush(stdout);
kill(pid, SIGTERM);
wait(&status);
return 0;
}