Whamcloud - gitweb
LU-15519 quota: fallocate does not increase projectid usage
[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         bool do_rmdir = false;
86         char *filename, *progname;
87         char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
88         char *endp = NULL;
89         double start, last_t, end;
90         long begin = 0, count = ~0UL >> 1;
91         int has_fmt_spec = 0, unlink_has_fmt_spec = 0;
92         long i, total, last_i = 0;
93         int c, last_fd = -1, stderr_fd;
94         int rc = 0;
95
96         /* Handle the deprecated positional last argument "-seconds" */
97         if (argc > 1 && argv[argc - 1][0] == '-' &&
98             (end = strtol(argv[argc - 1] + 1, &endp, 0)) && *endp == '\0') {
99                 fprintf(stderr, "warning: '-runtime' deprecated, "
100                         "use '-t runtime' instead\n");
101                 argv[--argc] = NULL;
102         } else {
103                 /* Not '-number', let regular argument parsing handle it. */
104                 end = ~0U >> 1;
105         }
106
107         if ((endp = strrchr(argv[0], '/')) != NULL)
108                 progname = endp + 1;
109         else
110                 progname = argv[0];
111
112         while ((c = getopt(argc, argv, "dl:kmor::t:u::")) != -1) {
113                 switch (c) {
114                 case 'd':
115                         do_mkdir = true;
116                         break;
117                 case 'k':
118                         do_keep = true;
119                         break;
120                 case 'l':
121                         do_link = true;
122                         tgt = optarg;
123                         break;
124                 case 'm':
125                         do_mknod = true;
126                         break;
127                 case 'o':
128                         do_open = true;
129                         break;
130                 case 't':
131                         end = strtol(optarg, &endp, 0);
132                         if (end <= 0.0 || *endp != '\0')
133                                 usage(progname);
134                         break;
135                 case 'r':
136                 case 'u':
137                         do_unlink = true;
138                         fmt_unlink = optarg;
139                         break;
140                 case '?':
141                         fprintf(stderr, "Unknown option '%c'\n", optopt);
142                         usage(progname);
143                 }
144         }
145
146         if (do_open + do_mkdir + do_link + do_mknod > 1 ||
147             do_open + do_mkdir + do_link + do_mknod + do_unlink == 0) {
148                 fprintf(stderr, "error: only one of -o, -m, -l, -d\n");
149                 usage(progname);
150         }
151         if (do_mkdir && do_unlink)
152                 do_rmdir = true;
153
154         if (!do_open && do_keep) {
155                 fprintf(stderr, "error: can only use -k with -o\n");
156                 usage(progname);
157         }
158
159         switch (argc - optind) {
160         case 3:
161                 begin = strtol(argv[argc - 2], NULL, 0);
162         case 2:
163                 count = strtol(argv[argc - 1], NULL, 0);
164         case 1:
165                 fmt = argv[optind];
166                 break;
167         default:
168                 usage(progname);
169         }
170
171         has_fmt_spec = strchr(fmt, '%') != NULL;
172         if (fmt_unlink != NULL)
173                 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
174
175         for (i = 0, start = last_t = now(), end += start;
176              i < count && now() < end; i++, begin++) {
177                 double tmp;
178
179                 filename = get_file_name(fmt, begin, has_fmt_spec);
180                 if (do_open) {
181                         int fd = open(filename, O_CREAT|O_RDWR, 0644);
182                         if (fd < 0) {
183                                 printf("open(%s) error: %s\n", filename,
184                                        strerror(errno));
185                                 rc = errno;
186                                 break;
187                         }
188                         if (!do_keep)
189                                 close(fd);
190                         else if (fd > last_fd)
191                                 last_fd = fd;
192                 } else if (do_link) {
193                         rc = link(tgt, filename);
194                         if (rc) {
195                                 printf("link(%s, %s) error: %s\n",
196                                        tgt, filename, strerror(errno));
197                                 rc = errno;
198                                 break;
199                         }
200                 } else if (do_mkdir) {
201                         rc = mkdir(filename, 0755);
202                         if (rc) {
203                                 printf("mkdir(%s) error: %s\n",
204                                        filename, strerror(errno));
205                                 rc = errno;
206                                 break;
207                         }
208                 } else if (do_mknod) {
209                         rc = mknod(filename, S_IFREG | 0444, 0);
210                         if (rc) {
211                                 printf("mknod(%s) error: %s\n",
212                                        filename, strerror(errno));
213                                 rc = errno;
214                                 break;
215                         }
216                 }
217                 if (do_unlink) {
218                         if (fmt_unlink != NULL)
219                                 filename = get_file_name(fmt_unlink, begin,
220                                                          unlink_has_fmt_spec);
221
222                         rc = do_rmdir ? rmdir(filename) : unlink(filename);
223                         /* use rmdir if this is a directory */
224                         if (!do_rmdir && rc && errno == EISDIR) {
225                                 do_rmdir = true;
226                                 rc = rmdir(filename);
227                         }
228                         if (rc) {
229                                 printf("unlink(%s) error: %s\n",
230                                        filename, strerror(errno));
231                                 rc = errno;
232                                 break;
233                         }
234                 }
235
236                 tmp = now();
237                 if (tmp - last_t >= 10.0 ||
238                     (tmp - last_t > 2.0 && (i % 10000) == 0)) {
239                         printf(" - %s%s %ld (time %.2f total %.2f last %.2f)"
240                                "\n",
241                                do_open ? do_keep ? "open/keep" : "open/close" :
242                                         do_mkdir ? "mkdir" : do_link ? "link" :
243                                         do_mknod ? "create" : "",
244                                do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
245                                i, tmp, tmp - start,
246                                (i - last_i) / (tmp - last_t));
247                         last_t = tmp;
248                         last_i = i;
249                 }
250         }
251         last_t = now();
252         total = i;
253         printf("total: %ld %s%s in %.2f seconds: %.2f ops/second\n", total,
254                do_open ? do_keep ? "open/keep" : "open/close" :
255                         do_mkdir ? "mkdir" : do_link ? "link" :
256                                              do_mknod ? "create" : "",
257                do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
258                last_t - start, ((double)total / (last_t - start)));
259
260         if (!do_keep)
261                 return rc;
262
263         stderr_fd = fileno(stderr);
264         start = last_t;
265         /* Assume fd is allocated in order, doing extra closes is not harmful */
266         for (i = 0; i < total && last_fd > stderr_fd; i++, --last_fd) {
267                 close(last_fd);
268
269                 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
270                         double tmp = now();
271
272                         printf(" - closed %ld (time %.2f total %.2f last %.2f)"
273                                "\n", i, tmp, tmp - start,
274                                (i - last_i) / (tmp - last_t));
275                         last_t = tmp;
276                         last_i = i;
277                 }
278         }
279         last_t = now();
280
281         printf("total: %ld close in %.2f seconds: %.2f close/second\n",
282                total, last_t - start, ((double)total / (last_t - start)));
283         return rc;
284 }