Whamcloud - gitweb
LU-8726 osd-ldiskfs: bypass read for benchmarking
[fs/lustre-release.git] / lustre / tests / createmany.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <time.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <getopt.h>
42
43 static void usage(char *prog)
44 {
45         printf("usage: %s {-o|-m|-d|-l<tgt>} [-r altpath ] filenamefmt count\n", prog);
46         printf("       %s {-o|-m|-d|-l<tgt>} [-r altpath ] filenamefmt ] -seconds\n", prog);
47         printf("       %s {-o|-m|-d|-l<tgt>} [-r altpath ] filenamefmt start count\n", prog);
48         exit(EXIT_FAILURE);
49 }
50
51 static char *get_file_name(const char *fmt, long n, int has_fmt_spec)
52 {
53         static char filename[4096];
54         int bytes;
55
56         bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n) :
57                 snprintf(filename, 4095, "%s%ld", fmt, n);
58         if (bytes >= 4095) {
59                 printf("file name too long\n");
60                 exit(EXIT_FAILURE);
61         }
62         return filename;
63 }
64
65 double now(void)
66 {
67         struct timeval tv;
68         gettimeofday(&tv, NULL);
69         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
70 }
71
72 int main(int argc, char ** argv)
73 {
74         long i;
75         int rc = 0, do_open = 0, do_link = 0, do_mkdir = 0;
76         int do_unlink = 0, do_mknod = 0;
77         char *filename;
78         char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
79         double start, last;
80         long begin = 0, end = ~0UL >> 1, count = ~0UL >> 1;
81         int c, has_fmt_spec = 0, unlink_has_fmt_spec = 0;
82
83         /* Handle the last argument in form of "-seconds" */
84         if (argc > 1 && argv[argc - 1][0] == '-') {
85                 char *endp;
86
87                 argc--;
88                 end = strtol(argv[argc] + 1, &endp, 0);
89                 if (end <= 0 || *endp != '\0')
90                         usage(argv[0]);
91                 end = end + time(NULL);
92         }
93
94         while ((c = getopt(argc, argv, "omdl:r:")) != -1) {
95                 switch(c) {
96                 case 'o':
97                         do_open++;
98                         break;
99                 case 'm':
100                         do_mknod++;
101                         break;
102                 case 'd':
103                         do_mkdir++;
104                         break;
105                 case 'l':
106                         do_link++;
107                         tgt = optarg;
108                         break;
109                 case 'r':
110                         do_unlink++;
111                         fmt_unlink = optarg;
112                         break;
113                 case '?':
114                         printf("Unknown option '%c'\n", optopt);
115                         usage(argv[0]);
116                 }
117         }
118
119         if (do_open + do_mkdir + do_link + do_mknod != 1 ||
120             do_unlink > 1)
121                 usage(argv[0]);
122
123         switch (argc - optind) {
124         case 3:
125                 begin = strtol(argv[argc - 2], NULL, 0);
126         case 2:
127                 count = strtol(argv[argc - 1], NULL, 0);
128                 if (end != ~0UL >> 1)
129                         usage(argv[0]);
130         case 1:
131                 fmt = argv[optind];
132                 break;
133         default:
134                 usage(argv[0]);
135         }
136
137         start = last = now();
138
139         has_fmt_spec = strchr(fmt, '%') != NULL;
140         if (do_unlink)
141                 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
142
143         for (i = 0; i < count && time(NULL) < end; i++, begin++) {
144                 filename = get_file_name(fmt, begin, has_fmt_spec);
145                 if (do_open) {
146                         int fd = open(filename, O_CREAT|O_RDWR, 0644);
147                         if (fd < 0) {
148                                 printf("open(%s) error: %s\n", filename,
149                                        strerror(errno));
150                                 rc = errno;
151                                 break;
152                         }
153                         close(fd);
154                 } else if (do_link) {
155                         rc = link(tgt, filename);
156                         if (rc) {
157                                 printf("link(%s, %s) error: %s\n",
158                                        tgt, filename, strerror(errno));
159                                 rc = errno;
160                                 break;
161                         }
162                 } else if (do_mkdir) {
163                         rc = mkdir(filename, 0755);
164                         if (rc) {
165                                 printf("mkdir(%s) error: %s\n",
166                                        filename, strerror(errno));
167                                 rc = errno;
168                                 break;
169                         }
170                 } else {
171                         rc = mknod(filename, S_IFREG| 0444, 0);
172                         if (rc) {
173                                 printf("mknod(%s) error: %s\n",
174                                        filename, strerror(errno));
175                                 rc = errno;
176                                 break;
177                         }
178                 }
179                 if (do_unlink) {
180                         filename = get_file_name(fmt_unlink, begin,
181                                       unlink_has_fmt_spec);
182                         rc = do_mkdir ? rmdir(filename) : unlink(filename);
183                         if (rc) {
184                                 printf("unlink(%s) error: %s\n",
185                                        filename, strerror(errno));
186                                 rc = errno;
187                                 break;
188                         }
189                 }
190
191                 if (i && (i % 10000) == 0) {
192                         printf(" - created %ld (time %.2f total %.2f last %.2f)"
193                                "\n", i, now(), now() - start, now() - last);
194                         last = now();
195                 }
196         }
197         printf("total: %ld creates%s in %.2f seconds: %.2f creates/second\n", i,
198                do_unlink ? "/deletions" : "",
199                now() - start, ((double)i / (now() - start)));
200
201         return rc;
202 }