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/
28 * Lustre is a trademark of Sun Microsystems, Inc.
30 * lustre/tests/reads.c
34 * Author: Nikita Danilov <nikita@clusterfs.com>
37 #define _XOPEN_SOURCE 500 /* for pread(2) */
47 #include <sys/types.h>
50 static void usage(void)
52 printf("reads: read random or stride chunks of a file.\n");
54 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");
58 BSIZE_DEFAULT = 16 * 4096
61 #define LOG(level, ...) \
63 if ((level) <= verbosity) \
64 fprintf(stderr, __VA_ARGS__); \
84 int main(int argc, char **argv)
86 int verbosity = LOG_CRIT;
93 unsigned int seed = 0;
94 unsigned long iterations = 0;
95 unsigned long timelimit = 24 * 3600;
96 unsigned long stride_length = 0;
97 unsigned long stride_offset = 0;
101 unsigned long nblocks;
105 struct timeval start;
114 opt = getopt(argc, argv, "f:s:b:va:hCS:n:t:l:o:w:");
119 LOG(LOG_CRIT, "Unable to parse command line.\n");
127 fname = strdup(optarg);
130 size = strtol(optarg, &term, 0);
131 if (term == optarg) {
132 fprintf (stderr, "Can't parse size %s\n", optarg);
138 bsize = strtol(optarg, &term, 0);
139 if (term == optarg) {
140 fprintf (stderr, "Can't parse bsize %s\n", optarg);
146 ad = (int)strtol(optarg, &term, 0);
147 if (term == optarg) {
148 fprintf (stderr, "Can't parse ad %s\n", optarg);
157 seed = strtol(optarg, &term, 0);
158 if (term == optarg) {
159 fprintf (stderr, "Can't parse seed %s\n", optarg);
165 iterations = strtol(optarg, &term, 0);
166 if (term == optarg) {
167 fprintf (stderr, "Can't parse seed %s\n", optarg);
175 timelimit = strtol(optarg, &term, 0);
176 if (term == optarg) {
177 fprintf (stderr, "Can't parse seed %s\n", optarg);
183 stride_length = strtol(optarg, &term, 0);
184 if (term == optarg) {
185 fprintf (stderr, "Can't parse seed %s\n", optarg);
191 stride_offset = strtol(optarg, &term, 0);
192 if (term == optarg) {
193 fprintf (stderr, "Can't parse seed %s\n", optarg);
199 width = (int)strtol(optarg, &term, 0);
200 if (term == optarg) {
201 fprintf (stderr, "Can't parse seed %s\n", optarg);
209 if (fname == NULL || size == 0 || bsize == 0 || ad <= 0) {
215 nblocks = size / bsize;
218 LOG(LOG_CRIT, "malloc(%lu) failure: %s\n", (long)bsize,
223 fd = open(fname, (preclean ? O_RDWR : O_RDONLY) | O_CREAT, 0700);
225 LOG(LOG_CRIT, "malloc(\"%s\") failure: %s\n", fname,
233 LOG(LOG_INFO, "precleaning");
234 for (i = 0, towrite = size; towrite > 0; towrite -= ret) {
235 count = bsize < towrite ? bsize : towrite;
236 memset(buf, bsize, seed + i++);
237 ret = write(fd, buf, count);
239 LOG(LOG_CRIT, "write() failure: %s\n",
248 gettimeofday(&start, NULL);
249 timelimit += start.tv_sec;
250 for (i = 0; !iterations || i < iterations; i ++) {
251 unsigned long block_nr;
255 block_nr = (unsigned long)(i*stride_length +
256 stride_offset) % nblocks;
258 block_nr = (unsigned long)((double)nblocks*rand()/
261 LOG(LOG_INFO, "\n%9lu: ", i);
262 LOG(LOG_INFO, "%7lu ", block_nr);
263 for (j = 0; j < ad; j++) {
264 ret = pread(fd, buf, bsize, (block_nr + j) * bsize);
267 "pread(...%zi, %li) got: %zi, %s\n", bsize,
268 block_nr * bsize, ret, strerror(errno));
273 gettimeofday(&stop, NULL);
274 if (stop.tv_sec > timelimit)
278 usecs = (stop.tv_sec - start.tv_sec) * 1000000. +
279 stop.tv_usec - start.tv_usec;
280 printf("\n%fs, %gMB/s\n", usecs / 1000000.,
281 (double)bsize * ad * i / usecs);