Whamcloud - gitweb
LU-11085 tests: Add performance test for ldlm_extent code
[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 <sys/ioctl.h>
43 #include <time.h>
44 #include <unistd.h>
45 #include <lustre/lustreapi.h>
46
47 #include <linux/lustre/lustre_user.h>
48 #include <lustre/lustreapi.h>
49
50 static void usage(const char *prog)
51 {
52         printf("usage: %s {-o [-k]|-m|-d|-l<tgt>} [-u[<unlinkfmt>]] "
53                "[-i mdt_index] [-t seconds] filenamefmt [[start] count]\n",
54                prog);
55         printf("\t-i\tMDT to create the directories on\n"
56                "\t-l\tlink files to existing <tgt> file\n"
57                "\t-m\tmknod regular files (don't create OST objects)\n"
58                "\t-o\topen+create files with path and printf format\n"
59                "\t-k\t    keep files open until all files are opened\n"
60                "\t-u\tunlink file/dir (with optional <unlinkfmt>)\n");
61         printf("\t-d\tuse directories instead of regular files\n"
62                "\t-t\tstop creating files after <seconds> have elapsed\n");
63         printf("\t-S\tthe file size\n"
64                "\t-U\tthe start User ID of the file\n"
65                "\t-G\tthe start Group ID of the file\n"
66                "\t-P\tthe start Project ID of the file\n");
67
68         exit(EXIT_FAILURE);
69 }
70
71 static char *get_file_name(const char *fmt, long n, int has_fmt_spec)
72 {
73         static char filename[4096];
74         int bytes;
75
76         bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n) :
77                 snprintf(filename, 4095, "%s%ld", fmt, n);
78         if (bytes >= 4095) {
79                 printf("file name too long\n");
80                 exit(EXIT_FAILURE);
81         }
82         return filename;
83 }
84
85 static double now(void)
86 {
87         struct timeval tv;
88
89         gettimeofday(&tv, NULL);
90         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
91 }
92
93 int main(int argc, char **argv)
94 {
95         bool do_open = false, do_keep = false, do_link = false;
96         bool do_unlink = false, do_mknod = false, do_mkdir = false;
97         bool do_setsize = false, do_chuid = false, do_chgid = false;
98         bool do_chprj = false;
99         bool do_rmdir = false;
100         int stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
101         int stripe_offset = -1, stripe_count = 1;
102         char *filename, *progname;
103         char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
104         char *endp = NULL;
105         double start, last_t, end;
106         long begin = 0, count = ~0UL >> 1;
107         int has_fmt_spec = 0, unlink_has_fmt_spec = 0;
108         long i, total, last_i = 0;
109         int c, last_fd = -1, stderr_fd;
110         unsigned int uid = 0, gid = 0, pid = 0;
111         int size = 0;
112         int rc = 0;
113
114         /* Handle the deprecated positional last argument "-seconds" */
115         if (argc > 1 && argv[argc - 1][0] == '-' &&
116             (end = strtol(argv[argc - 1] + 1, &endp, 0)) && *endp == '\0') {
117                 fprintf(stderr,
118                         "warning: '-runtime' deprecated, use '-t runtime' instead\n");
119                 argv[--argc] = NULL;
120         } else {
121                 /* Not '-number', let regular argument parsing handle it. */
122                 end = ~0U >> 1;
123         }
124
125         if ((endp = strrchr(argv[0], '/')) != NULL)
126                 progname = endp + 1;
127         else
128                 progname = argv[0];
129
130         while ((c = getopt(argc, argv, "i:dG:l:kmor::S:t:u::U:")) != -1) {
131                 switch (c) {
132                 case 'd':
133                         do_mkdir = true;
134                         break;
135                 case 'G':
136                         do_chgid = true;
137                         gid = strtoul(optarg, NULL, 0);
138                         break;
139                 case 'i':
140                         stripe_offset = strtoul(optarg, &endp, 0);
141                         if (*endp != '\0') {
142                                 fprintf(stderr, "invalid MDT index '%s'\n",
143                                         optarg);
144                                 return 1;
145                         }
146                         break;
147                 case 'k':
148                         do_keep = true;
149                         break;
150                 case 'l':
151                         do_link = true;
152                         tgt = optarg;
153                         break;
154                 case 'm':
155                         do_mknod = true;
156                         break;
157                 case 'o':
158                         do_open = true;
159                         break;
160                 case 'P':
161                         do_chprj = true;
162                         pid = strtoul(optarg, NULL, 0);
163                         break;
164                 case 'S':
165                         do_setsize = true;
166                         size = atoi(optarg);
167                         break;
168                 case 't':
169                         end = strtol(optarg, &endp, 0);
170                         if (end <= 0.0 || *endp != '\0')
171                                 usage(progname);
172                         break;
173                 case 'r':
174                 case 'u':
175                         do_unlink = true;
176                         fmt_unlink = optarg;
177                         break;
178                 case 'U':
179                         do_chuid = true;
180                         uid = strtoul(optarg, NULL, 0);
181                         break;
182                 case '?':
183                         fprintf(stderr, "Unknown option '%c'\n", optopt);
184                         usage(progname);
185                 }
186         }
187
188         if (!do_open && (do_setsize || do_chuid || do_chgid || do_chprj)) {
189                 fprintf(stderr, "error: -S, -U, -G, -P works only with -o\n");
190                 usage(progname);
191         }
192
193         if (do_open + do_mkdir + do_link + do_mknod > 1 ||
194             do_open + do_mkdir + do_link + do_mknod + do_unlink == 0) {
195                 fprintf(stderr, "error: only one of -o, -m, -l, -d\n");
196                 usage(progname);
197         }
198         if (do_mkdir && do_unlink)
199                 do_rmdir = true;
200
201         if (!do_open && do_keep) {
202                 fprintf(stderr, "error: can only use -k with -o\n");
203                 usage(progname);
204         }
205
206         switch (argc - optind) {
207         case 3:
208                 begin = strtol(argv[argc - 2], NULL, 0);
209         case 2:
210                 count = strtol(argv[argc - 1], NULL, 0);
211         case 1:
212                 fmt = argv[optind];
213                 break;
214         default:
215                 usage(progname);
216         }
217
218         has_fmt_spec = strchr(fmt, '%') != NULL;
219         if (fmt_unlink != NULL)
220                 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
221
222         for (i = 0, start = last_t = now(), end += start;
223              i < count && now() < end; i++, begin++) {
224                 double tmp;
225
226                 filename = get_file_name(fmt, begin, has_fmt_spec);
227                 if (do_open) {
228                         int fd;
229
230                         fd = open(filename, O_CREAT|O_RDWR, 0644);
231                         if (fd < 0) {
232                                 printf("open(%s) error: %s\n", filename,
233                                        strerror(errno));
234                                 rc = errno;
235                                 break;
236                         }
237                         if (do_setsize) {
238                                 rc = lseek(fd, (size - 6) < 0 ? 0 : size - 6,
239                                            SEEK_SET);
240                                 if (rc < 0) {
241                                         printf("lseek(%s, %d) error: %s\n",
242                                                filename, size, strerror(errno));
243                                         break;
244                                 }
245                                 rc = write(fd, "Lustre", 6);
246                                 if (rc < 0) {
247                                         printf("write(%s, %d) error: %s\n",
248                                                filename, 6, strerror(errno));
249                                         break;
250                                 }
251                         }
252
253                         if (do_chuid || do_chgid) {
254                                 rc = fchown(fd, do_chuid ? uid + i : -1,
255                                             do_chgid ? gid + i : -1);
256                                 if (rc < 0) {
257                                         printf("fchown(%s, %u, %u) error: %s\n",
258                                                filename, do_chuid ? uid : -1,
259                                                do_chgid ? gid : -1,
260                                                strerror(errno));
261                                         break;
262                                 }
263                         }
264
265                         if (do_chprj) {
266                                 struct fsxattr fsx;
267
268                                 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
269                                 if (rc < 0) {
270                                         printf("ioctl(%s) error: %s\n",
271                                                "FS_IOC_GETXATTR",
272                                                strerror(errno));
273                                         break;
274                                 }
275
276                                 fsx.fsx_projid = pid + i;
277                                 rc = ioctl(fd, FS_IOC_FSSETXATTR, &fsx);
278                                 if (rc < 0) {
279                                         printf("ioctl(%s, %d) error: %s\n",
280                                                "FS_IOC_SETXATTR", pid,
281                                                strerror(errno));
282                                         break;
283                                 }
284                         }
285
286                         if (!do_keep)
287                                 close(fd);
288                         else if (fd > last_fd)
289                                 last_fd = fd;
290                 } else if (do_link) {
291                         rc = link(tgt, filename);
292                         if (rc) {
293                                 printf("link(%s, %s) error: %s\n",
294                                        tgt, filename, strerror(errno));
295                                 rc = errno;
296                                 break;
297                         }
298                 } else if (do_mkdir) {
299                         if (stripe_offset != -1) {
300                                 rc = llapi_dir_create_pool(filename, 0755,
301                                                            stripe_offset,
302                                                            stripe_count,
303                                                            stripe_pattern,
304                                                            NULL);
305                                 if (rc) {
306                                         printf("llapi_dir_create_pool(%s) error: %s\n",
307                                                filename, strerror(-rc));
308                                         rc = errno;
309                                         break;
310                                 }
311                         } else {
312                                 rc = mkdir(filename, 0755);
313                                 if (rc) {
314                                         printf("mkdir(%s) error: %s\n",
315                                                filename, strerror(errno));
316                                         rc = errno;
317                                         break;
318                                 }
319                         }
320                 } else if (do_mknod) {
321                         rc = mknod(filename, S_IFREG | 0444, 0);
322                         if (rc) {
323                                 printf("mknod(%s) error: %s\n",
324                                        filename, strerror(errno));
325                                 rc = errno;
326                                 break;
327                         }
328                 }
329                 if (do_unlink) {
330                         if (fmt_unlink != NULL)
331                                 filename = get_file_name(fmt_unlink, begin,
332                                                          unlink_has_fmt_spec);
333
334                         rc = do_rmdir ? rmdir(filename) : unlink(filename);
335                         /* use rmdir if this is a directory */
336                         if (!do_rmdir && rc && errno == EISDIR) {
337                                 do_rmdir = true;
338                                 rc = rmdir(filename);
339                         }
340                         if (rc) {
341                                 printf("unlink(%s) error: %s\n",
342                                        filename, strerror(errno));
343                                 rc = errno;
344                                 break;
345                         }
346                 }
347
348                 tmp = now();
349                 if (tmp - last_t >= 10.0 ||
350                     (tmp - last_t > 2.0 && (i % 10000) == 0)) {
351                         printf(" - %s%s %ld (time %.2f total %.2f last %.2f)\n",
352                                do_open ? do_keep ? "open/keep" : "open/close" :
353                                         do_mkdir ? "mkdir" : do_link ? "link" :
354                                         do_mknod ? "create" : "",
355                                do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
356                                i, tmp, tmp - start,
357                                (i - last_i) / (tmp - last_t));
358                         last_t = tmp;
359                         last_i = i;
360                 }
361         }
362         last_t = now();
363         total = i;
364         printf("total: %ld %s%s in %.2f seconds: %.2f ops/second\n", total,
365                do_open ? do_keep ? "open/keep" : "open/close" :
366                         do_mkdir ? "mkdir" : do_link ? "link" :
367                                              do_mknod ? "create" : "",
368                do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
369                last_t - start, ((double)total / (last_t - start)));
370
371         if (!do_keep)
372                 return rc;
373
374         stderr_fd = fileno(stderr);
375         start = last_t;
376         /* Assume fd is allocated in order, doing extra closes is not harmful */
377         for (i = 0; i < total && last_fd > stderr_fd; i++, --last_fd) {
378                 close(last_fd);
379
380                 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
381                         double tmp = now();
382
383                         printf(" - closed %ld (time %.2f total %.2f last %.2f)\n",
384                                i, tmp, tmp - start,
385                                (i - last_i) / (tmp - last_t));
386                         last_t = tmp;
387                         last_i = i;
388                 }
389         }
390         last_t = now();
391
392         printf("total: %ld close in %.2f seconds: %.2f close/second\n",
393                total, last_t - start, ((double)total / (last_t - start)));
394         return rc;
395 }