Whamcloud - gitweb
LU-13128 osc: glimpse and lock cancel race
[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  * Copyright (c) 2017, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <time.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <getopt.h>
48
49 static void usage(char *prog)
50 {
51         printf("usage: %s {-w|-a|-r} filenamefmt count seconds\n"
52                "-w : write mode\n"
53                "-a : append\n"
54                "-r : read mode\n", prog);
55         exit(EXIT_FAILURE);
56 }
57
58 int main(int argc, char **argv)
59 {
60         int do_read = 0, do_append = 0;
61         char *base, *endp;
62         long int start, last;
63         long end = ~0UL >> 1, count = ~0UL >> 1;
64         int c, i, fd, rc = 0, len, mode = 0;
65         long nbytes = 0;
66         char buf[4096];
67
68         while ((c = getopt(argc, argv, "war")) != -1) {
69                 switch (c) {
70                 case 'w':
71                         mode = O_RDWR;
72                         break;
73                 case 'a':
74                         do_append = 1;
75                         mode = O_RDWR | O_APPEND;
76                         break;
77                 case 'r':
78                         do_read = 1;
79                         mode = O_RDONLY;
80                         break;
81                 case '?':
82                         printf("Unknown option '%c'\n", optopt);
83                         usage(argv[0]);
84                 }
85         }
86
87         if (optind + 3 != argc) {
88                 fprintf(stderr,
89                         "missing filenamebase, total_files, or seconds\n");
90                 usage(argv[0]);
91         }
92
93         base = argv[optind];
94         if (strlen(base) > 4080) {
95                 fprintf(stderr, "filenamebase too long\n");
96                 exit(1);
97         }
98
99         count = strtoul(argv[optind + 1], NULL, 0);
100
101         end = strtoul(argv[optind + 2], &endp, 0);
102         if (end <= 0 && *endp != '\0') {
103                 fprintf(stderr, "%s: error: bad number of seconds '%s'\n",
104                         argv[0], argv[optind + 2]);
105                 exit(2);
106         }
107
108         srand(42);
109
110         start = last = time(0);
111         end += start;
112
113         for (i = 0; i < count && time(0) < end; i++) {
114                 char filename[4096];
115
116                 snprintf(filename, sizeof(filename), "%s%d", base, i);
117
118                 fd = open(filename, mode, 0666);
119                 if (fd < 0) {
120                         fprintf(stderr, "fail to open %s\n", filename);
121                         rc = errno;
122                         break;
123                 }
124
125                 len = random() % 4096;
126
127                 if (do_read == 0) {
128                         c = write(fd, buf, len);
129                         if (c != len) {
130                                 fprintf(stderr, "fail to write %s, len %d,"
131                                         " written %d\n", filename, len, c);
132                                 rc = errno;
133                                 break;
134                         }
135                 } else {
136                         c = read(fd, buf, len);
137                 }
138                 nbytes += c;
139
140                 if (close(fd) < 0) {
141                         fprintf(stderr, "can't close %s\n", filename);
142                         rc = errno;
143                         break;
144                 }
145
146                 if (i && (i % 10000) == 0) {
147                         printf(" - %ld bytes (time %ld total %ld last %ld)"
148                                "\n", nbytes, time(0), time(0) - start,
149                                time(0) - last);
150                         last = time(0);
151                 }
152         }
153         printf("total: %s %ld bytes in %ld seconds: %.2f bytes/second\n",
154                do_read ? "read" : do_append ? "append" : "write", nbytes,
155                time(0) - start, ((double)nbytes / (time(0) - start)));
156
157         return rc;
158 }