forked from AltraMayor/f3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f3read.c
338 lines (288 loc) · 8.3 KB
/
f3read.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
#define _POSIX_C_SOURCE 200112L
#define _XOPEN_SOURCE 600
#include <assert.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <sys/time.h>
#include <argp.h>
#include "utils.h"
#include "version.h"
/* Argp's global variables. */
const char *argp_program_version = "F3 Read " F3_STR_VERSION;
/* Arguments. */
static char adoc[] = "<PATH>";
static char doc[] = "F3 Read -- validate .h2w files to test "
"the real capacity of the drive";
static struct argp_option options[] = {
{"start-at", 's', "NUM", 0,
"First NUM.h2w file to be read", 1},
{"end-at", 'e', "NUM", 0,
"Last NUM.h2w file to be read", 0},
{"show-progress", 'p', "NUM", 0,
"Show progress if NUM is not zero", 0},
{ 0 }
};
struct args {
long start_at;
long end_at;
int show_progress;
const char *dev_path;
};
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct args *args = state->input;
long l;
switch (key) {
case 's':
l = arg_to_long(state, arg);
if (l <= 0)
argp_error(state,
"NUM must be greater than zero");
args->start_at = l - 1;
break;
case 'e':
l = arg_to_long(state, arg);
if (l <= 0)
argp_error(state,
"NUM must be greater than zero");
args->end_at = l - 1;
break;
case 'p':
args->show_progress = !!arg_to_long(state, arg);
break;
case ARGP_KEY_INIT:
args->dev_path = NULL;
break;
case ARGP_KEY_ARG:
if (args->dev_path)
argp_error(state,
"Wrong number of arguments; only one is allowed");
args->dev_path = arg;
break;
case ARGP_KEY_END:
if (!args->dev_path)
argp_error(state,
"The disk path was not specified");
if (args->start_at > args->end_at)
argp_error(state,
"Option --start-at must be less or equal to option --end-at");
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {options, parse_opt, adoc, doc, NULL, NULL, NULL};
static inline void update_dt(struct timeval *dt, const struct timeval *t1,
const struct timeval *t2)
{
dt->tv_sec += t2->tv_sec - t1->tv_sec;
dt->tv_usec += t2->tv_usec - t1->tv_usec;
if (dt->tv_usec >= 1000000) {
dt->tv_sec++;
dt->tv_usec -= 1000000;
}
}
#define TOLERANCE 2
#define PRINT_STATUS(s) printf("%s%7" PRIu64 "/%9" PRIu64 "/%7" PRIu64 "/%7" \
PRIu64, (s), *ptr_ok, *ptr_corrupted, *ptr_changed, *ptr_overwritten)
#define BLANK " "
#define CLEAR ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" \
"\b\b\b\b\b\b\b\b\b\b\b\b\b")
static void validate_file(const char *path, int number,
uint64_t *ptr_ok, uint64_t *ptr_corrupted, uint64_t *ptr_changed,
uint64_t *ptr_overwritten, uint64_t *ptr_size, int *ptr_read_all,
struct timeval *ptr_dt, int progress)
{
char *full_fn;
const char *filename;
const int num_int64 = SECTOR_SIZE >> 3;
uint64_t sector[num_int64];
FILE *f;
int fd;
size_t sectors_read;
uint64_t expected_offset;
int final_errno;
struct timeval t1, t2;
/* Progress time. */
struct timeval pt1;
*ptr_ok = *ptr_corrupted = *ptr_changed = *ptr_overwritten = 0;
full_fn = full_fn_from_number(&filename, path, number);
assert(full_fn);
printf("Validating file %s ... %s", filename, progress ? BLANK : "");
fflush(stdout);
#ifdef __CYGWIN__
/* We don't need write access, but some kernels require that
* the file descriptor passed to fdatasync(2) to be writable.
*/
f = fopen(full_fn, "rb+");
#else
f = fopen(full_fn, "rb");
#endif
if (!f)
err(errno, "Can't open file %s", full_fn);
fd = fileno(f);
assert(fd >= 0);
/* If the kernel follows our advice, f3read won't ever read from cache
* even when testing small memory cards without a remount, and
* we should have a better reading-speed measurement.
*/
assert(!fdatasync(fd));
assert(!posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED));
/* Obtain initial time. */
assert(!gettimeofday(&t1, NULL));
pt1 = t1;
pt1.tv_sec -= 1000;
/* Help the kernel to help us. */
assert(!posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL));
sectors_read = fread(sector, SECTOR_SIZE, 1, f);
final_errno = errno;
expected_offset = (uint64_t)number * GIGABYTES;
while (sectors_read > 0) {
uint64_t rn;
int error_count, i;
assert(sectors_read == 1);
rn = sector[0];
error_count = 0;
for (i = 1; error_count <= TOLERANCE && i < num_int64; i++) {
rn = random_number(rn);
if (rn != sector[i])
error_count++;
}
if (expected_offset == sector[0]) {
if (error_count == 0)
(*ptr_ok)++;
else if (error_count <= TOLERANCE)
(*ptr_changed)++;
else
(*ptr_corrupted)++;
} else if (error_count <= TOLERANCE)
(*ptr_overwritten)++;
else
(*ptr_corrupted)++;
sectors_read = fread(sector, SECTOR_SIZE, 1, f);
final_errno = errno;
expected_offset += SECTOR_SIZE;
if (progress) {
struct timeval pt2;
assert(!gettimeofday(&pt2, NULL));
/* Avoid often printouts. */
if (delay_ms(&pt1, &pt2) >= 200) {
PRINT_STATUS(CLEAR);
fflush(stdout);
pt1 = pt2;
}
}
}
assert(!gettimeofday(&t2, NULL));
update_dt(ptr_dt, &t1, &t2);
*ptr_read_all = feof(f);
*ptr_size = ftell(f);
PRINT_STATUS(progress ? CLEAR : "");
if (!*ptr_read_all) {
assert(ferror(f));
printf(" - NOT fully read due to \"%s\"",
strerror(final_errno));
}
printf("\n");
fclose(f);
free(full_fn);
}
static void report(const char *prefix, uint64_t i)
{
double f = (double) (i * SECTOR_SIZE);
const char *unit = adjust_unit(&f);
printf("%s %.2f %s (%" PRIu64 " sectors)\n", prefix, f, unit, i);
}
static inline double dt_to_s(struct timeval *dt)
{
double ret = (double)dt->tv_sec + ((double)dt->tv_usec / 1000000.);
assert(ret >= 0);
return ret > 0 ? ret : 1;
}
static void iterate_files(const char *path, const long *files,
long start_at, long end_at, int progress)
{
uint64_t tot_ok, tot_corrupted, tot_changed, tot_overwritten, tot_size;
struct timeval tot_dt = { .tv_sec = 0, .tv_usec = 0 };
double read_speed;
const char *unit;
int and_read_all = 1;
int or_missing_file = 0;
long number = start_at;
UNUSED(end_at);
tot_ok = tot_corrupted = tot_changed = tot_overwritten = tot_size = 0;
printf(" SECTORS "
" ok/corrupted/changed/overwritten\n");
while (*files >= 0) {
uint64_t sec_ok, sec_corrupted, sec_changed,
sec_overwritten, file_size;
int read_all;
or_missing_file = or_missing_file || (*files != number);
for (; number < *files; number++) {
char *full_fn;
const char *filename;
full_fn = full_fn_from_number(&filename, "", number);
assert(full_fn);
printf("Missing file %s\n", filename);
free(full_fn);
}
number++;
validate_file(path, *files, &sec_ok, &sec_corrupted,
&sec_changed, &sec_overwritten,
&file_size, &read_all, &tot_dt, progress);
tot_ok += sec_ok;
tot_corrupted += sec_corrupted;
tot_changed += sec_changed;
tot_overwritten += sec_overwritten;
tot_size += file_size;
and_read_all = and_read_all && read_all;
files++;
}
assert(tot_size / SECTOR_SIZE ==
(tot_ok + tot_corrupted + tot_changed + tot_overwritten));
/* Notice that not reporting `missing' files after the last file
* in @files is important since @end_at could be very large.
*/
report("\n Data OK:", tot_ok);
report("Data LOST:", tot_corrupted + tot_changed + tot_overwritten);
report("\t Corrupted:", tot_corrupted);
report("\tSlightly changed:", tot_changed);
report("\t Overwritten:", tot_overwritten);
if (or_missing_file)
printf("WARNING: Not all F3 files in the range %li to %li are available\n",
start_at + 1, number);
if (!and_read_all)
printf("WARNING: Not all data was read due to I/O error(s)\n");
/* Reading speed. */
read_speed = (double)tot_size / dt_to_s(&tot_dt);
unit = adjust_unit(&read_speed);
printf("Average reading speed: %.2f %s/s\n", read_speed, unit);
}
int main(int argc, char **argv)
{
const long *files;
struct args args = {
/* Defaults. */
.start_at = 0,
.end_at = LONG_MAX - 1,
/* If stdout isn't a terminal, supress progress. */
.show_progress = isatty(STDOUT_FILENO),
};
/* Read parameters. */
argp_parse(&argp, argc, argv, 0, NULL, &args);
print_header(stdout, "read");
files = ls_my_files(args.dev_path, args.start_at, args.end_at);
iterate_files(args.dev_path, files, args.start_at, args.end_at,
args.show_progress);
free((void *)files);
return 0;
}