2 * Copyright (c) 2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <sys/types.h>
20 #include <sys/param.h>
32 #include <attr/attributes.h>
34 typedef unsigned int uint_t;
37 * Loop over directory sizes:
39 * touch n files in each directory
40 * write y bytes to all files in each directory
41 * set DMF attribute on x files in each directory
42 * Change directory sizes by multiplication or addition.
43 * Allow control of starting & stopping sizes, name length, target directory.
44 * Print size and wallclock time (ms per file).
45 * Output can be used to make graphs (gnuplot)
49 static uint_t dirchars;
50 static char *directory;
51 static uint_t firstsize;
52 static uint_t lastsize;
53 static uint_t minchars;
57 static uint_t pfxchars;
62 static int mkfile(char *, char *);
63 static void filename(int, int, char *);
64 static int hexchars(uint_t);
65 static uint_t nextsize(uint_t);
66 static double now(void);
67 static void usage(void);
70 * Maximum size allowed, this is pretty nuts.
71 * The largest one we've ever built has been about 2 million.
73 #define MAX_DIR_SIZE (16 * 1024 * 1024)
74 #define DFL_FIRST_SIZE 1
75 #define DFL_LAST_SIZE (1024 * 1024)
76 #define MAX_DIR_COUNT 1024
77 #define MIN_DIR_COUNT 1
80 #define DMFATTRNAME "SGI_DMI_DMFATTR"
83 main(int argc, char **argv)
89 char name[NAME_MAX + 1];
90 char attr[DMFATTRLEN];
93 while ((c = getopt(argc, argv, "a:b:c:d:f:l:m:n:s:")) != -1) {
96 addval = (uint_t)atoi(optarg);
99 bsize = (size_t)atol(optarg);
102 nchars = (uint_t)atoi(optarg);
108 firstsize = (uint_t)atoi(optarg);
111 lastsize = (uint_t)atoi(optarg);
114 mulval = atof(optarg);
117 ndirs = (uint_t)atoi(optarg);
120 fsize = (off64_t)atol(optarg);
128 if (!addval && !mulval)
130 else if ((addval && mulval) || mulval < 0.0) {
136 buffer = memalign(getpagesize(), bsize);
137 memset(buffer, 0xfeed, bsize);
138 memset(attr, 0xaaaaaaa, sizeof(attr));
143 if (mkdir(directory, 0777) < 0 && errno != EEXIST) {
147 if (chdir(directory) < 0) {
153 firstsize = DFL_FIRST_SIZE;
154 else if (firstsize > MAX_DIR_SIZE)
155 firstsize = MAX_DIR_SIZE;
157 lastsize = DFL_LAST_SIZE;
158 else if (lastsize > MAX_DIR_SIZE)
159 lastsize = MAX_DIR_SIZE;
160 if (lastsize < firstsize)
161 lastsize = firstsize;
162 minchars = hexchars(lastsize - 1);
163 if (nchars < minchars)
165 else if (nchars >= NAME_MAX + 1)
167 if (ndirs > MAX_DIR_COUNT)
168 ndirs = MAX_DIR_COUNT;
169 if (ndirs < MIN_DIR_COUNT)
170 ndirs = MIN_DIR_COUNT;
171 dirchars = hexchars(ndirs);
172 pfxchars = nchars - minchars;
174 memset(&name[dirchars + 1], 'a', pfxchars);
177 for (j = 0; j < ndirs; j++) {
178 filename(0, j, name);
179 name[dirchars] = '\0';
182 for (i = 0; i < cursize; i++) {
183 filename((i + j) % cursize, j, name);
184 close(mkfile(name, attr));
186 printf("%d %.3f\n", cursize,
187 (now() - stime) * 1.0e3 / (cursize * ndirs));
188 cursize = nextsize(cursize);
194 mkfile(char *name, char *attr)
197 ssize_t wrote, wsize;
198 off64_t bytes = fsize;
200 if ((fd = open(name, O_WRONLY | O_CREAT | O_EXCL | O_DIRECT, 0666)) < 0) {
204 if (attr_setf(fd, DMFATTRNAME, attr, DMFATTRLEN, ATTR_ROOT) < 0) {
209 wsize = (bsize < bytes) ? bsize : bytes;
210 if ((wrote = write(fd, buffer, wsize)) < 0) {
220 filename(int idx, int dir, char *name)
222 static char hexc[16] = "0123456789abcdef";
225 for (i = dirchars - 1; i >= 0; i--)
226 *name++ = hexc[(dir >> (4 * i)) & 0xf];
228 name += pfxchars; /* skip pfx a's */
229 for (i = minchars - 1; i >= 0; i--)
230 *name++ = hexc[(idx >> (4 * i)) & 0xf];
235 hexchars(uint_t maxval)
239 if (maxval < 16 * 16)
241 if (maxval < 16 * 16 * 16)
243 if (maxval < 16 * 16 * 16 * 16)
245 if (maxval < 16 * 16 * 16 * 16 * 16)
247 if (maxval < 16 * 16 * 16 * 16 * 16 * 16)
249 if (maxval < 16 * 16 * 16 * 16 * 16 * 16 * 16)
255 nextsize(uint_t cursize)
264 if (n > (double)lastsize + 0.5)
265 return lastsize + 1; /* i.e. out of bounds */
266 else if ((uint_t)n == cursize)
277 gettimeofday(&tv, NULL);
278 return (double)tv.tv_sec + 1.0e-6 * (double)tv.tv_usec;
285 "usage: dirperf [-d dir] [-a addstep | -m mulstep] [-f first] "
286 "[-l last] [-c nchars] [-n ndirs] [-s size]\n");