~martin-decky/helenos/rcu

« back to all changes in this revision

Viewing changes to uspace/app/testread/testread.c

  • Committer: Martin Sucha
  • Date: 2011-03-23 19:45:46 UTC
  • mto: (720.2.80 ext2-merge)
  • mto: This revision was merged to the branch mainline in revision 1004.
  • Revision ID: sucha14@st.fmph.uniba.sk-20110323194546-sjykxzofnqglbuxd
Add tools to test reading of files from a filesystem

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <devmap.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
        
 
78
        if (argc < 2) {
 
79
                printf(NAME ": Error, argument missing.\n");
 
80
                syntax_print();
 
81
                return 1;
 
82
        }
 
83
        
 
84
        // Skip program name
 
85
        --argc; ++argv;
 
86
        
 
87
        if (argc != 1) {
 
88
                printf(NAME ": Error, unexpected argument.\n");
 
89
                syntax_print();
 
90
                return 1;
 
91
        }
 
92
        
 
93
        file_name = *argv;
 
94
        
 
95
        buf = calloc(BUFELEMS, sizeof(uint64_t));
 
96
        if (buf == NULL) {
 
97
                printf("Failed allocating buffer\n");
 
98
                return 1;
 
99
        }
 
100
        
 
101
        file = fopen(file_name, "r");
 
102
        if (file == NULL) {
 
103
                printf("Failed opening file\n");
 
104
                return 1;
 
105
        }
 
106
        
 
107
        expected = 0;
 
108
        offset = 0;
 
109
        next_mark = 0;
 
110
        last_mark = 0;
 
111
        struct timeval prev_time;
 
112
        struct timeval start_time;
 
113
        int rc;
 
114
        rc = gettimeofday(&start_time, NULL);
 
115
        if (rc != EOK) {
 
116
                printf("gettimeofday failed\n");
 
117
                fclose(file);
 
118
                free(buf);
 
119
                return 1;
 
120
        }
 
121
        prev_time = start_time;
 
122
        
 
123
        while (!feof(file)) {
 
124
                size_t elems = fread(buf, sizeof(uint64_t), BUFELEMS, file);
 
125
                if (ferror(file)) {
 
126
                        printf("Failed reading file\n");
 
127
                        fclose(file);
 
128
                        free(buf);
 
129
                        return 1;
 
130
                }
 
131
                
 
132
                for (i = 0; i < elems; i++) {
 
133
                        if (uint64_t_le2host(buf[i]) != expected) {
 
134
                                printf("Unexpected value at offset %" PRIuOFF64 "\n", offset);
 
135
                                fclose(file);
 
136
                                free(buf);
 
137
                                return 2;
 
138
                        }
 
139
                        expected++;
 
140
                        offset += sizeof(uint64_t);
 
141
                }
 
142
                
 
143
                if (offset >= next_mark) {
 
144
                        struct timeval cur_time;
 
145
                        rc = gettimeofday(&cur_time, NULL);
 
146
                        if (rc != EOK) {
 
147
                                printf("gettimeofday failed\n");
 
148
                                fclose(file);
 
149
                                free(buf);
 
150
                                return 1;
 
151
                        }
 
152
                        uint32_t last_run = cur_time.tv_sec - prev_time.tv_sec;
 
153
                        uint32_t total_time = cur_time.tv_sec - start_time.tv_sec;
 
154
                        if (last_run > 0 && total_time > 0) {
 
155
                                printf("%" PRIuOFF64 "M - time: %u s, "
 
156
                                    "cur: %"PRIuOFF64 " B/s, avg: %" PRIuOFF64 " B/s\n",
 
157
                                    offset / MBYTE, last_run,
 
158
                                    (offset-last_mark)/last_run,
 
159
                                    offset/total_time);
 
160
                                prev_time = cur_time;
 
161
                                last_mark = offset;
 
162
                        }
 
163
                        next_mark += MBYTE;
 
164
                }
 
165
        }
 
166
        
 
167
        fclose(file);
 
168
        free(buf);
 
169
 
 
170
        return 0;
 
171
}
 
172
 
 
173
 
 
174
static void syntax_print(void)
 
175
{
 
176
        printf("syntax: testread <filename>\n");
 
177
}
 
178
 
 
179
/**
 
180
 * @}
 
181
 */