[6b3e7f7f] | 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>
|
---|
[8d2dd7f2] | 51 | #include <stddef.h>
|
---|
| 52 | #include <stdint.h>
|
---|
[6b3e7f7f] | 53 | #include <mem.h>
|
---|
[15f3c3f] | 54 | #include <loc.h>
|
---|
[6b3e7f7f] | 55 | #include <byteorder.h>
|
---|
| 56 | #include <inttypes.h>
|
---|
| 57 | #include <errno.h>
|
---|
| 58 | #include <time.h>
|
---|
[23c8acd9] | 59 | #include <offset.h>
|
---|
[6b3e7f7f] | 60 |
|
---|
| 61 | #define NAME "testread"
|
---|
| 62 | #define BUFELEMS 1024
|
---|
| 63 | #define MBYTE (1024*1024)
|
---|
| 64 |
|
---|
| 65 | static void syntax_print(void);
|
---|
| 66 |
|
---|
| 67 | int main(int argc, char **argv)
|
---|
| 68 | {
|
---|
| 69 |
|
---|
| 70 | FILE *file;
|
---|
| 71 | char *file_name;
|
---|
| 72 | uint64_t *buf;
|
---|
| 73 | uint64_t expected;
|
---|
| 74 | aoff64_t offset;
|
---|
| 75 | aoff64_t next_mark;
|
---|
| 76 | aoff64_t last_mark;
|
---|
| 77 | unsigned int i;
|
---|
[fca78d4] | 78 | bool check_enabled = true;
|
---|
| 79 | bool progress = true;
|
---|
[6b3e7f7f] | 80 |
|
---|
| 81 | if (argc < 2) {
|
---|
| 82 | printf(NAME ": Error, argument missing.\n");
|
---|
| 83 | syntax_print();
|
---|
| 84 | return 1;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[1a86c50] | 87 | /* Skip program name */
|
---|
[6b3e7f7f] | 88 | --argc; ++argv;
|
---|
| 89 |
|
---|
[fca78d4] | 90 | if (argc > 0 && str_cmp(*argv, "--no-check") == 0) {
|
---|
| 91 | check_enabled = false;
|
---|
| 92 | --argc; ++argv;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | if (argc > 0 && str_cmp(*argv, "--no-progress") == 0) {
|
---|
| 96 | progress = false;
|
---|
| 97 | --argc; ++argv;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[6b3e7f7f] | 100 | if (argc != 1) {
|
---|
| 101 | printf(NAME ": Error, unexpected argument.\n");
|
---|
| 102 | syntax_print();
|
---|
| 103 | return 1;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | file_name = *argv;
|
---|
| 107 |
|
---|
| 108 | buf = calloc(BUFELEMS, sizeof(uint64_t));
|
---|
| 109 | if (buf == NULL) {
|
---|
| 110 | printf("Failed allocating buffer\n");
|
---|
| 111 | return 1;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | file = fopen(file_name, "r");
|
---|
| 115 | if (file == NULL) {
|
---|
| 116 | printf("Failed opening file\n");
|
---|
| 117 | return 1;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | expected = 0;
|
---|
| 121 | offset = 0;
|
---|
| 122 | next_mark = 0;
|
---|
| 123 | last_mark = 0;
|
---|
| 124 | struct timeval prev_time;
|
---|
| 125 | struct timeval start_time;
|
---|
[1ab8539] | 126 | gettimeofday(&start_time, NULL);
|
---|
[6b3e7f7f] | 127 | prev_time = start_time;
|
---|
| 128 |
|
---|
| 129 | while (!feof(file)) {
|
---|
| 130 | size_t elems = fread(buf, sizeof(uint64_t), BUFELEMS, file);
|
---|
| 131 | if (ferror(file)) {
|
---|
| 132 | printf("Failed reading file\n");
|
---|
| 133 | fclose(file);
|
---|
| 134 | free(buf);
|
---|
| 135 | return 1;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | for (i = 0; i < elems; i++) {
|
---|
[fca78d4] | 139 | if (check_enabled && uint64_t_le2host(buf[i]) != expected) {
|
---|
[6b3e7f7f] | 140 | printf("Unexpected value at offset %" PRIuOFF64 "\n", offset);
|
---|
| 141 | fclose(file);
|
---|
| 142 | free(buf);
|
---|
| 143 | return 2;
|
---|
| 144 | }
|
---|
| 145 | expected++;
|
---|
| 146 | offset += sizeof(uint64_t);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[fca78d4] | 149 | if (progress && offset >= next_mark) {
|
---|
[6b3e7f7f] | 150 | struct timeval cur_time;
|
---|
[1ab8539] | 151 | gettimeofday(&cur_time, NULL);
|
---|
| 152 |
|
---|
[6b3e7f7f] | 153 | uint32_t last_run = cur_time.tv_sec - prev_time.tv_sec;
|
---|
| 154 | uint32_t total_time = cur_time.tv_sec - start_time.tv_sec;
|
---|
| 155 | if (last_run > 0 && total_time > 0) {
|
---|
| 156 | printf("%" PRIuOFF64 "M - time: %u s, "
|
---|
| 157 | "cur: %"PRIuOFF64 " B/s, avg: %" PRIuOFF64 " B/s\n",
|
---|
| 158 | offset / MBYTE, last_run,
|
---|
| 159 | (offset-last_mark)/last_run,
|
---|
| 160 | offset/total_time);
|
---|
| 161 | prev_time = cur_time;
|
---|
| 162 | last_mark = offset;
|
---|
| 163 | }
|
---|
| 164 | next_mark += MBYTE;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[fca78d4] | 168 | struct timeval final_time;
|
---|
[1ab8539] | 169 | gettimeofday(&final_time, NULL);
|
---|
[fca78d4] | 170 |
|
---|
| 171 | uint32_t total_run_time = final_time.tv_sec - start_time.tv_sec;
|
---|
| 172 | if (total_run_time > 0) {
|
---|
| 173 | printf("total bytes: %" PRIuOFF64
|
---|
| 174 | ", total time: %u s, avg speed: %" PRIuOFF64 " B/s\n",
|
---|
| 175 | offset,
|
---|
| 176 | total_run_time,
|
---|
| 177 | offset/total_run_time);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[6b3e7f7f] | 180 | fclose(file);
|
---|
| 181 | free(buf);
|
---|
| 182 |
|
---|
| 183 | return 0;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 | static void syntax_print(void)
|
---|
| 188 | {
|
---|
| 189 | printf("syntax: testread <filename>\n");
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /**
|
---|
| 193 | * @}
|
---|
| 194 | */
|
---|