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