4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
29 * lustre/tests/reads.c
33 * Author: Nikita Danilov <nikita@clusterfs.com>
36 #define _XOPEN_SOURCE 500 /* for pread(2) */
46 #include <sys/types.h>
49 static void usage(void)
51 printf("reads: read random or stride chunks of a file.\n");
53 printf("reads -f <filename> -s <filesize> -b <buffersize> -a <adjacent reads> [-v] [-h] [-C] [-l <stride_length> ] [ -o <stride_offset> ] [-S <seed>] [-n <iterations>][-w <width>] [-t <timelimit>]\n");
57 BSIZE_DEFAULT = 16 * 4096
60 #define LOG(level, ...) \
62 if ((level) <= verbosity) \
63 fprintf(stderr, __VA_ARGS__); \
83 int main(int argc, char **argv)
85 int verbosity = LOG_CRIT;
92 unsigned int seed = 0;
93 unsigned long iterations = 0;
94 unsigned long timelimit = 24 * 3600;
95 unsigned long stride_length = 0;
96 unsigned long stride_offset = 0;
100 unsigned long nblocks;
104 struct timeval start;
113 opt = getopt(argc, argv, "f:s:b:va:hCS:n:t:l:o:w:");
118 LOG(LOG_CRIT, "Unable to parse command line.\n");
126 fname = strdup(optarg);
129 size = strtol(optarg, &term, 0);
130 if (term == optarg) {
131 fprintf (stderr, "Can't parse size %s\n", optarg);
137 bsize = strtol(optarg, &term, 0);
138 if (term == optarg) {
139 fprintf (stderr, "Can't parse bsize %s\n", optarg);
145 ad = (int)strtol(optarg, &term, 0);
146 if (term == optarg) {
147 fprintf (stderr, "Can't parse ad %s\n", optarg);
156 seed = strtol(optarg, &term, 0);
157 if (term == optarg) {
158 fprintf (stderr, "Can't parse seed %s\n", optarg);
164 iterations = strtol(optarg, &term, 0);
165 if (term == optarg) {
166 fprintf (stderr, "Can't parse seed %s\n", optarg);
174 timelimit = strtol(optarg, &term, 0);
175 if (term == optarg) {
176 fprintf (stderr, "Can't parse seed %s\n", optarg);
182 stride_length = strtol(optarg, &term, 0);
183 if (term == optarg) {
184 fprintf (stderr, "Can't parse seed %s\n", optarg);
190 stride_offset = strtol(optarg, &term, 0);
191 if (term == optarg) {
192 fprintf (stderr, "Can't parse seed %s\n", optarg);
198 width = (int)strtol(optarg, &term, 0);
199 if (term == optarg) {
200 fprintf (stderr, "Can't parse seed %s\n", optarg);
208 if (fname == NULL || size == 0 || bsize == 0 || ad <= 0) {
214 nblocks = size / bsize;
217 LOG(LOG_CRIT, "malloc(%lu) failure: %s\n", (long)bsize,
222 fd = open(fname, (preclean ? O_RDWR : O_RDONLY) | O_CREAT, 0700);
224 LOG(LOG_CRIT, "malloc(\"%s\") failure: %s\n", fname,
232 LOG(LOG_INFO, "precleaning");
233 for (i = 0, towrite = size; towrite > 0; towrite -= ret) {
234 count = bsize < towrite ? bsize : towrite;
235 memset(buf, bsize, seed + i++);
236 ret = write(fd, buf, count);
238 LOG(LOG_CRIT, "write() failure: %s\n",
247 gettimeofday(&start, NULL);
248 timelimit += start.tv_sec;
249 for (i = 0; !iterations || i < iterations; i ++) {
250 unsigned long block_nr;
254 block_nr = (unsigned long)(i*stride_length +
255 stride_offset) % nblocks;
257 block_nr = (unsigned long)((double)nblocks*rand()/
260 LOG(LOG_INFO, "\n%9lu: ", i);
261 LOG(LOG_INFO, "%7lu ", block_nr);
262 for (j = 0; j < ad; j++) {
263 ret = pread(fd, buf, bsize, (block_nr + j) * bsize);
266 "pread(...%zi, %li) got: %zi, %s\n", bsize,
267 block_nr * bsize, ret, strerror(errno));
272 gettimeofday(&stop, NULL);
273 if (stop.tv_sec > timelimit)
277 usecs = (stop.tv_sec - start.tv_sec) * 1000000. +
278 stop.tv_usec - start.tv_usec;
279 printf("\n%fs, %gMB/s\n", usecs / 1000000.,
280 (double)bsize * ad * i / usecs);