Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[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  * Copyright (c) 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <getopt.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <time.h>
43 #include <unistd.h>
44
45 static void usage(const char *prog)
46 {
47         printf("usage: %s {-o [-k]|-m|-d|-l<tgt>} [-u[<unlinkfmt>]] "
48                "[-t seconds] filenamefmt [[start] count]\n", prog);
49         printf("\t-l\tlink files to existing <tgt> file\n"
50                "\t-m\tmknod regular files (don't create OST objects)\n"
51                "\t-o\topen+create files with path and printf format\n"
52                "\t-k\t    keep files open until all files are opened\n"
53                "\t-u\tunlink file/dir (with optional <unlinkfmt>)\n");
54         printf("\t-d\tuse directories instead of regular files\n"
55                "\t-t\tstop creating files after <seconds> have elapsed\n");
56
57         exit(EXIT_FAILURE);
58 }
59
60 static char *get_file_name(const char *fmt, long n, int has_fmt_spec)
61 {
62         static char filename[4096];
63         int bytes;
64
65         bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n) :
66                 snprintf(filename, 4095, "%s%ld", fmt, n);
67         if (bytes >= 4095) {
68                 printf("file name too long\n");
69                 exit(EXIT_FAILURE);
70         }
71         return filename;
72 }
73
74 double now(void)
75 {
76         struct timeval tv;
77         gettimeofday(&tv, NULL);
78         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
79 }
80
81 int main(int argc, char ** argv)
82 {
83         bool do_open = false, do_keep = false, do_link = false;
84         bool do_unlink = false, do_mknod = false, do_mkdir = false;
85         char *filename, *progname;
86         char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
87         char *endp = NULL;
88         double start, last_t, end;
89         long begin = 0, count = ~0UL >> 1;
90         int has_fmt_spec = 0, unlink_has_fmt_spec = 0;
91         long i, total, last_i = 0;
92         int c, last_fd = -1, stderr_fd;
93         int rc = 0;
94
95         /* Handle the deprecated positional last argument "-seconds" */
96         if (argc > 1 && argv[argc - 1][0] == '-' &&
97             (end = strtol(argv[argc - 1] + 1, &endp, 0)) && *endp == '\0') {
98                 fprintf(stderr, "warning: '-runtime' deprecated, "
99                         "use '-t runtime' instead\n");
100                 argv[--argc] = NULL;
101         } else {
102                 /* Not '-number', let regular argument parsing handle it. */
103                 end = ~0U >> 1;
104         }
105
106         if ((endp = strrchr(argv[0], '/')) != NULL)
107                 progname = endp + 1;
108         else
109                 progname = argv[0];
110
111         while ((c = getopt(argc, argv, "dl:kmor::t:u::")) != -1) {
112                 switch (c) {
113                 case 'd':
114                         do_mkdir = true;
115                         break;
116                 case 'k':
117                         do_keep = true;
118                         break;
119                 case 'l':
120                         do_link = true;
121                         tgt = optarg;
122                         break;
123                 case 'm':
124                         do_mknod = true;
125                         break;
126                 case 'o':
127                         do_open = true;
128                         break;
129                 case 't':
130                         end = strtol(optarg, &endp, 0);
131                         if (end <= 0.0 || *endp != '\0')
132                                 usage(progname);
133                         break;
134                 case 'r':
135                 case 'u':
136                         do_unlink = true;
137                         fmt_unlink = optarg;
138                         break;
139                 case '?':
140                         fprintf(stderr, "Unknown option '%c'\n", optopt);
141                         usage(progname);
142                 }
143         }
144
145         if (do_open + do_mkdir + do_link + do_mknod > 1 ||
146             do_open + do_mkdir + do_link + do_mknod + do_unlink == 0) {
147                 fprintf(stderr, "error: only one of -o, -m, -l, -d\n");
148                 usage(progname);
149         }
150
151         if (!do_open && do_keep) {
152                 fprintf(stderr, "error: can only use -k with -o\n");
153                 usage(progname);
154         }
155
156         switch (argc - optind) {
157         case 3:
158                 begin = strtol(argv[argc - 2], NULL, 0);
159         case 2:
160                 count = strtol(argv[argc - 1], NULL, 0);
161         case 1:
162                 fmt = argv[optind];
163                 break;
164         default:
165                 usage(progname);
166         }
167
168         has_fmt_spec = strchr(fmt, '%') != NULL;
169         if (fmt_unlink != NULL)
170                 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
171
172         for (i = 0, start = last_t = now(), end += start;
173              i < count && now() < end; i++, begin++) {
174                 filename = get_file_name(fmt, begin, has_fmt_spec);
175                 if (do_open) {
176                         int fd = open(filename, O_CREAT|O_RDWR, 0644);
177                         if (fd < 0) {
178                                 printf("open(%s) error: %s\n", filename,
179                                        strerror(errno));
180                                 rc = errno;
181                                 break;
182                         }
183                         if (!do_keep)
184                                 close(fd);
185                         else if (fd > last_fd)
186                                 last_fd = fd;
187                 } else if (do_link) {
188                         rc = link(tgt, filename);
189                         if (rc) {
190                                 printf("link(%s, %s) error: %s\n",
191                                        tgt, filename, strerror(errno));
192                                 rc = errno;
193                                 break;
194                         }
195                 } else if (do_mkdir) {
196                         rc = mkdir(filename, 0755);
197                         if (rc) {
198                                 printf("mkdir(%s) error: %s\n",
199                                        filename, strerror(errno));
200                                 rc = errno;
201                                 break;
202                         }
203                 } else if (do_mknod) {
204                         rc = mknod(filename, S_IFREG | 0444, 0);
205                         if (rc) {
206                                 printf("mknod(%s) error: %s\n",
207                                        filename, strerror(errno));
208                                 rc = errno;
209                                 break;
210                         }
211                 }
212                 if (do_unlink) {
213                         if (fmt_unlink != NULL)
214                                 filename = get_file_name(fmt_unlink, begin,
215                                                          unlink_has_fmt_spec);
216
217                         rc = do_mkdir ? rmdir(filename) : unlink(filename);
218                         if (rc) {
219                                 printf("unlink(%s) error: %s\n",
220                                        filename, strerror(errno));
221                                 rc = errno;
222                                 break;
223                         }
224                 }
225
226                 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
227                         double tmp = now();
228
229                         printf(" - %s%s %ld (time %.2f total %.2f last %.2f)"
230                                "\n",
231                                do_open ? do_keep ? "open/keep" : "open/close" :
232                                         do_mkdir ? "mkdir" : do_link ? "link" :
233                                         do_mknod ? "create" : "",
234                                do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
235                                i, tmp, tmp - start,
236                                (i - last_i) / (tmp - last_t));
237                         last_t = tmp;
238                         last_i = i;
239                 }
240         }
241         last_t = now();
242         total = i;
243         printf("total: %ld %s%s in %.2f seconds: %.2f ops/second\n", total,
244                do_open ? do_keep ? "open/keep" : "open/close" :
245                         do_mkdir ? "mkdir" : do_link ? "link" :
246                                              do_mknod ? "create" : "",
247                do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
248                last_t - start, ((double)total / (last_t - start)));
249
250         if (!do_keep)
251                 return rc;
252
253         stderr_fd = fileno(stderr);
254         start = last_t;
255         /* Assume fd is allocated in order, doing extra closes is not harmful */
256         for (i = 0; i < total && last_fd > stderr_fd; i++, --last_fd) {
257                 close(last_fd);
258
259                 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
260                         double tmp = now();
261
262                         printf(" - closed %ld (time %.2f total %.2f last %.2f)"
263                                "\n", i, tmp, tmp - start,
264                                (i - last_i) / (tmp - last_t));
265                         last_t = tmp;
266                         last_i = i;
267                 }
268         }
269         last_t = now();
270
271         printf("total: %ld close in %.2f seconds: %.2f close/second\n",
272                total, last_t - start, ((double)total / (last_t - start)));
273         return rc;
274 }