-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlargefile.c
170 lines (138 loc) · 3.49 KB
/
largefile.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
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
/* Measure creating a large file and overwriting that file */
#define WSIZE (16 * 4096)
#define FILESIZE 50 * 1024 * 1024
#define NAMESIZE 100
static char name[NAMESIZE];
static char buf[WSIZE];
static char *prog;
static char *dir;
void printstats(int reset)
{
int fd;
int r;
sprintf(name, "%s/stats", dir);
if((fd = open(name, O_RDONLY)) < 0) {
return;
}
bzero(buf, WSIZE);
if ((r = read(fd, buf, WSIZE)) < 0) {
perror("read");
exit(1);
}
if (!reset) fprintf(stdout, "=== FS Stats ===\n%s========\n", buf);
if ((r = close(fd)) < 0) {
perror("close");
}
}
int makefile()
{
int i;
int r;
int fd;
int n = FILESIZE/WSIZE;
sprintf(name, "%s/d/f", dir);
if((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) {
printf("%s: create %s failed %s\n", prog, name, strerror(errno));
exit(1);
}
sprintf(buf, "%s/stats", dir);
for (i = 0; i < n; i++) {
if (write(fd, buf, WSIZE) != WSIZE) {
printf("%s: write %s failed %s\n", prog, name, strerror(errno));
exit(1);
}
}
if (fsync(fd) < 0) {
printf("%s: fsync %s failed %s\n", prog, name, strerror(errno));
exit(1);
}
lseek(fd, SEEK_SET, 0);
write(fd, buf, WSIZE);
close(fd);
fd = open(".", O_DIRECTORY | O_RDONLY);
if (fd < 0) {
perror("open dir");
exit(-1);
}
if (fsync(fd) < 0) {
perror("fsync");
exit(-1);
}
}
int writefile()
{
int i;
int r;
int fd;
int n = FILESIZE/WSIZE;
sprintf(name, "%s/d/f", dir);
if((fd = open(name, O_RDWR, S_IRWXU)) < 0) {
printf("%s: open %s failed %s\n", prog, name, strerror(errno));
exit(1);
}
sprintf(buf, "%s/stats", dir);
for (i = 0; i < n; i++) {
if (write(fd, buf, WSIZE) != WSIZE) {
printf("%s: write %s failed %s\n", prog, name, strerror(errno));
exit(1);
}
if (((i + 1) * WSIZE) % (10 * 1024 * 1024) == 0) {
if (fsync(fd) < 0) {
printf("%s: fsync %s failed %s\n", prog, name, strerror(errno));
exit(1);
}
}
}
if ((i * WSIZE) % (10 * 1024 * 1024) != 0 && fsync(fd) < 0) {
printf("%s: fsync %s failed %s\n", prog, name, strerror(errno));
exit(1);
}
close(fd);
}
int main(int argc, char *argv[])
{
long time;
struct timeval before;
struct timeval after;
float tput;
if (argc != 2) {
printf("Usage: %s basedir\n", argv[0]);
exit(-1);
}
prog = argv[0];
dir = argv[1];
sprintf(name, "%s/d", dir);
if (mkdir(name, S_IRWXU) < 0) {
printf("%s: create %s failed %s\n", prog, name, strerror(errno));
exit(1);
}
printstats(1);
gettimeofday ( &before, NULL );
makefile();
gettimeofday ( &after, NULL );
time = (after.tv_sec - before.tv_sec) * 1000000 +
(after.tv_usec - before.tv_usec);
tput = ((float) (FILESIZE/1024) / (time / 1000000.0));
float mf_tput = tput;
//printf("makefile %d MB %ld usec throughput %5.1f KB/s\n", FILESIZE/(1024*1024), time, tput);
printstats(0);
gettimeofday ( &before, NULL );
writefile();
gettimeofday ( &after, NULL );
time = (after.tv_sec - before.tv_sec) * 1000000 +
(after.tv_usec - before.tv_usec);
tput = ((float) (FILESIZE/1024) / (time / 1000000.0));
float wf_tput = tput;
//printf("writefile %d MB %ld usec throughput %5.1f KB/s\n", FILESIZE/(1024*1024), time, tput);
printf("%5.1f, %5.1f\n", mf_tput, wf_tput);
printstats(0);
}