Whamcloud - gitweb
LU-10030 utils: add lfs tool to change/list project of file
[fs/lustre-release.git] / lustre / tests / smalliomany.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <time.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <getopt.h>
46
47 static void usage(char *prog)
48 {
49         printf("usage: %s {-w|-a|-r} filenamefmt count seconds\n"
50                "-w : write mode\n"
51                "-a : append\n"
52                "-r : read mode\n", prog);
53         exit(EXIT_FAILURE);
54 }
55
56 int main(int argc, char **argv)
57 {
58         int do_read = 0, do_append = 0;
59         char *base, *endp;
60         long int start, last;
61         long end = ~0UL >> 1, count = ~0UL >> 1;
62         int c, i, fd, rc = 0, len, mode = 0;
63         long nbytes = 0;
64         char buf[4096];
65
66         while ((c = getopt(argc, argv, "war")) != -1) {
67                 switch (c) {
68                 case 'w':
69                         mode = O_RDWR;
70                         break;
71                 case 'a':
72                         do_append = 1;
73                         mode = O_RDWR | O_APPEND;
74                         break;
75                 case 'r':
76                         do_read = 1;
77                         mode = O_RDONLY;
78                         break;
79                 case '?':
80                         printf("Unknown option '%c'\n", optopt);
81                         usage(argv[0]);
82                 }
83         }
84
85         if (optind + 3 != argc) {
86                 fprintf(stderr,
87                         "missing filenamebase, total_files, or seconds\n");
88                 usage(argv[0]);
89         }
90
91         base = argv[optind];
92         if (strlen(base) > 4080) {
93                 fprintf(stderr, "filenamebase too long\n");
94                 exit(1);
95         }
96
97         count = strtoul(argv[optind + 1], NULL, 0);
98
99         end = strtoul(argv[optind + 2], &endp, 0);
100         if (end <= 0 && *endp != '\0') {
101                 fprintf(stderr, "%s: error: bad number of seconds '%s'\n",
102                         argv[0], argv[optind + 2]);
103                 exit(2);
104         }
105
106         srand(42);
107
108         start = last = time(0);
109         end += start;
110
111         for (i = 0; i < count && time(0) < end; i++) {
112                 char filename[4096];
113
114                 snprintf(filename, sizeof(filename), "%s%d", base, i);
115
116                 fd = open(filename, mode, 0666);
117                 if (fd < 0) {
118                         fprintf(stderr, "fail to open %s\n", filename);
119                         rc = errno;
120                         break;
121                 }
122
123                 len = random() % 4096;
124
125                 if (do_read == 0) {
126                         c = write(fd, buf, len);
127                         if (c != len) {
128                                 fprintf(stderr, "fail to write %s, len %d,"
129                                         " written %d\n", filename, len, c);
130                                 rc = errno;
131                                 break;
132                         }
133                 } else {
134                         c = read(fd, buf, len);
135                 }
136                 nbytes += c;
137
138                 if (close(fd) < 0) {
139                         fprintf(stderr, "can't close %s\n", filename);
140                         rc = errno;
141                         break;
142                 }
143
144                 if (i && (i % 10000) == 0) {
145                         printf(" - %ld bytes (time %ld total %ld last %ld)"
146                                "\n", nbytes, time(0), time(0) - start,
147                                time(0) - last);
148                         last = time(0);
149                 }
150         }
151         printf("total: %s %ld bytes in %ld seconds: %.2f bytes/second\n",
152                do_read ? "read" : do_append ? "append" : "write", nbytes,
153                time(0) - start, ((double)nbytes / (time(0) - start)));
154
155         return rc;
156 }