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
|
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#define FASTWRITER_SYNCIO_ALIGN 512
#define SYNC_MODE
#define BUFSIZE 2097152
#ifdef SYNC_MODE
# define BLOCK_SIZE 2097152
#else /* SYNC_MODE */
# define BLOCK_SIZE 16384
#endif /* SYNC_MODE */
#define WRITE_INTERVAL 1
int main(int argc, char *argv[]) {
int err;
size_t SKIP = 1;
DIR *dir;
struct dirent *ent;
struct timeval start, tv;
size_t us;
size_t files = 0;
size_t total_size = 0;
size_t last_write = 0;
size_t skip;
size_t run;
ssize_t res;
size_t max_size = (size_t)-1;
char *buffer;//[BUFSIZE];
long double mcoef = 1000000. / (1024 * 1024);
int flags = O_RDONLY|O_NOATIME|O_LARGEFILE;
#ifdef SYNC_MODE
flags |= O_DIRECT;
#endif
posix_memalign((void**)&buffer, FASTWRITER_SYNCIO_ALIGN, BUFSIZE);
if (argc < 2) {
printf("Usage: %s <directory|device> [skip|size]\n", argv[0]);
exit(0);
}
if (strstr(argv[1], "/dev/")) {
if (argc > 2) {
max_size = atol(argv[2]);
max_size *= 1024 * 1024 * 1024;
}
int fd = open(argv[1], flags, 0);
if (fd < 0) {
printf("Unable to open device %s\n", argv[1]);
exit(1);
}
size_t size = BLOCK_SIZE;
gettimeofday(&start, NULL);
res = read(fd, buffer, size);
while (res > 0) {
if (res != size) {
printf("Incomplete read: %zu bytes read instead of %zu\n", res, size);
exit(-1);
}
total_size += res;
gettimeofday(&tv, NULL);
us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
if ((us - last_write) > WRITE_INTERVAL * 1000000) {
last_write = us;
printf("Reading: %s (%lu GB), Measured speed: %zu MB/s\n", argv[0], total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
}
if (total_size > max_size) {
printf("Reading: %s (%lu GB), Measured speed: %zu MB/s\n", argv[0], total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
break;
}
res = read(fd, buffer, size);
}
close(fd);
if (res < 0) {
printf("Read failed with errno %i\n", errno);
exit(-1);
}
free(buffer);
return 0;
}
chdir(argv[1]);
if (argc > 2) {
SKIP = atoi(argv[2]);
printf("Skip %zu\n", SKIP);
}
gettimeofday(&start, NULL);
for (run = 0; run < SKIP; run++) {
skip = 0;
dir = opendir(".");
while ((ent = readdir(dir))) {
struct stat st;
if (((skip++)%SKIP) != run) continue;
if (stat(ent->d_name, &st)) continue;
if (!S_ISREG(st.st_mode)) continue;
#ifdef F_MODE
FILE *f = fopen(ent->d_name, "r");
if (!f) continue;
#else
int fd = open(ent->d_name, flags, 0);
if (fd < 0) continue;
#endif
int size = st.st_blksize;
if (size > BUFSIZE) {
printf("Buffer too small\n");
exit(1);
}
#ifdef F_MODE
while (!feof(f)) {
err = fread(buffer, 1, size, f);
if (err < 0) break;
}
#else
# ifdef SYNC_MODE
if (size < BLOCK_SIZE) size = BLOCK_SIZE;
# endif
err = read(fd, buffer, size);
while (err > 0) {
err = read(fd, buffer, size);
}
#endif
if (err < 0) {
printf("Read failed\n");
exit(1);
}
#ifdef F_MODE
fclose(f);
#else
close(fd);
#endif
total_size += st.st_size;
files++;
gettimeofday(&tv, NULL);
us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
printf("Reading: %s (%lu MB), Read: %lu files (%lu GB), Measured speed: %zu MB/s\n", ent->d_name, st.st_size/1024/1024, files, total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
}
closedir(dir);
}
free(buffer);
}
|