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