1 | /*
|
---|
2 | * Copyright (c) 2011 Martin Sucha
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup test
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file testread.c
|
---|
35 | * This program checks if the file contains a pattern of increasing
|
---|
36 | * 64 bit unsigned integers (that may overflow at some point)
|
---|
37 | * stored in little endian byte order.
|
---|
38 | * This is to verify that the filesystem reads
|
---|
39 | * the files correctly.
|
---|
40 | * If the file does not contain specified pattern, it stops
|
---|
41 | * at the point where the file does not match and prints
|
---|
42 | * the byte offset at which the error occured.
|
---|
43 | * While checking the file, the program displays how many megabytes
|
---|
44 | * it has already read, how long it took to read the last megabyte
|
---|
45 | * and current and average read speed.
|
---|
46 | *
|
---|
47 | */
|
---|
48 |
|
---|
49 | #include <stdio.h>
|
---|
50 | #include <stdlib.h>
|
---|
51 | #include <mem.h>
|
---|
52 | #include <loc.h>
|
---|
53 | #include <byteorder.h>
|
---|
54 | #include <sys/types.h>
|
---|
55 | #include <sys/typefmt.h>
|
---|
56 | #include <inttypes.h>
|
---|
57 | #include <errno.h>
|
---|
58 | #include <time.h>
|
---|
59 |
|
---|
60 | #define NAME "testread"
|
---|
61 | #define BUFELEMS 1024
|
---|
62 | #define MBYTE (1024*1024)
|
---|
63 |
|
---|
64 | static void syntax_print(void);
|
---|
65 |
|
---|
66 | int main(int argc, char **argv)
|
---|
67 | {
|
---|
68 |
|
---|
69 | FILE *file;
|
---|
70 | char *file_name;
|
---|
71 | uint64_t *buf;
|
---|
72 | uint64_t expected;
|
---|
73 | aoff64_t offset;
|
---|
74 | aoff64_t next_mark;
|
---|
75 | aoff64_t last_mark;
|
---|
76 | unsigned int i;
|
---|
77 | bool check_enabled = true;
|
---|
78 | bool progress = true;
|
---|
79 |
|
---|
80 | if (argc < 2) {
|
---|
81 | printf(NAME ": Error, argument missing.\n");
|
---|
82 | syntax_print();
|
---|
83 | return 1;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* Skip program name */
|
---|
87 | --argc; ++argv;
|
---|
88 |
|
---|
89 | if (argc > 0 && str_cmp(*argv, "--no-check") == 0) {
|
---|
90 | check_enabled = false;
|
---|
91 | --argc; ++argv;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (argc > 0 && str_cmp(*argv, "--no-progress") == 0) {
|
---|
95 | progress = false;
|
---|
96 | --argc; ++argv;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (argc != 1) {
|
---|
100 | printf(NAME ": Error, unexpected argument.\n");
|
---|
101 | syntax_print();
|
---|
102 | return 1;
|
---|
103 | }
|
---|
104 |
|
---|
105 | file_name = *argv;
|
---|
106 |
|
---|
107 | buf = calloc(BUFELEMS, sizeof(uint64_t));
|
---|
108 | if (buf == NULL) {
|
---|
109 | printf("Failed allocating buffer\n");
|
---|
110 | return 1;
|
---|
111 | }
|
---|
112 |
|
---|
113 | file = fopen(file_name, "r");
|
---|
114 | if (file == NULL) {
|
---|
115 | printf("Failed opening file\n");
|
---|
116 | return 1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | expected = 0;
|
---|
120 | offset = 0;
|
---|
121 | next_mark = 0;
|
---|
122 | last_mark = 0;
|
---|
123 | struct timeval prev_time;
|
---|
124 | struct timeval start_time;
|
---|
125 | int rc;
|
---|
126 | rc = gettimeofday(&start_time, NULL);
|
---|
127 | if (rc != EOK) {
|
---|
128 | printf("gettimeofday failed\n");
|
---|
129 | fclose(file);
|
---|
130 | free(buf);
|
---|
131 | return 1;
|
---|
132 | }
|
---|
133 | prev_time = start_time;
|
---|
134 |
|
---|
135 | while (!feof(file)) {
|
---|
136 | size_t elems = fread(buf, sizeof(uint64_t), BUFELEMS, file);
|
---|
137 | if (ferror(file)) {
|
---|
138 | printf("Failed reading file\n");
|
---|
139 | fclose(file);
|
---|
140 | free(buf);
|
---|
141 | return 1;
|
---|
142 | }
|
---|
143 |
|
---|
144 | for (i = 0; i < elems; i++) {
|
---|
145 | if (check_enabled && uint64_t_le2host(buf[i]) != expected) {
|
---|
146 | printf("Unexpected value at offset %" PRIuOFF64 "\n", offset);
|
---|
147 | fclose(file);
|
---|
148 | free(buf);
|
---|
149 | return 2;
|
---|
150 | }
|
---|
151 | expected++;
|
---|
152 | offset += sizeof(uint64_t);
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (progress && offset >= next_mark) {
|
---|
156 | struct timeval cur_time;
|
---|
157 | rc = gettimeofday(&cur_time, NULL);
|
---|
158 | if (rc != EOK) {
|
---|
159 | printf("gettimeofday failed\n");
|
---|
160 | fclose(file);
|
---|
161 | free(buf);
|
---|
162 | return 1;
|
---|
163 | }
|
---|
164 | uint32_t last_run = cur_time.tv_sec - prev_time.tv_sec;
|
---|
165 | uint32_t total_time = cur_time.tv_sec - start_time.tv_sec;
|
---|
166 | if (last_run > 0 && total_time > 0) {
|
---|
167 | printf("%" PRIuOFF64 "M - time: %u s, "
|
---|
168 | "cur: %"PRIuOFF64 " B/s, avg: %" PRIuOFF64 " B/s\n",
|
---|
169 | offset / MBYTE, last_run,
|
---|
170 | (offset-last_mark)/last_run,
|
---|
171 | offset/total_time);
|
---|
172 | prev_time = cur_time;
|
---|
173 | last_mark = offset;
|
---|
174 | }
|
---|
175 | next_mark += MBYTE;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | struct timeval final_time;
|
---|
180 | rc = gettimeofday(&final_time, NULL);
|
---|
181 | if (rc != EOK) {
|
---|
182 | printf("gettimeofday failed\n");
|
---|
183 | fclose(file);
|
---|
184 | free(buf);
|
---|
185 | return 1;
|
---|
186 | }
|
---|
187 |
|
---|
188 | uint32_t total_run_time = final_time.tv_sec - start_time.tv_sec;
|
---|
189 | if (total_run_time > 0) {
|
---|
190 | printf("total bytes: %" PRIuOFF64
|
---|
191 | ", total time: %u s, avg speed: %" PRIuOFF64 " B/s\n",
|
---|
192 | offset,
|
---|
193 | total_run_time,
|
---|
194 | offset/total_run_time);
|
---|
195 | }
|
---|
196 |
|
---|
197 | fclose(file);
|
---|
198 | free(buf);
|
---|
199 |
|
---|
200 | return 0;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | static void syntax_print(void)
|
---|
205 | {
|
---|
206 | printf("syntax: testread <filename>\n");
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * @}
|
---|
211 | */
|
---|