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/param.h>
31 #include <attr/attributes.h>
33 typedef unsigned int uint_t;
36 * Loop over directory sizes:
38 * touch n files in each directory
39 * write y bytes to all files in each directory
40 * set DMF attribute on x files in each directory
41 * Change directory sizes by multiplication or addition.
42 * Allow control of starting & stopping sizes, name length, target directory.
43 * Print size and wallclock time (ms per file).
44 * Output can be used to make graphs (gnuplot)
48 static uint_t dirchars;
49 static char *directory;
50 static uint_t firstsize;
51 static uint_t lastsize;
52 static uint_t minchars;
56 static uint_t pfxchars;
61 static int mkfile(char *, char *);
62 static void filename(int, int, char *);
63 static int hexchars(uint_t);
64 static uint_t nextsize(uint_t);
65 static double now(void);
66 static void usage(void);
69 * Maximum size allowed, this is pretty nuts.
70 * The largest one we've ever built has been about 2 million.
72 #define MAX_DIR_SIZE (16 * 1024 * 1024)
73 #define DFL_FIRST_SIZE 1
74 #define DFL_LAST_SIZE (1024 * 1024)
75 #define MAX_DIR_COUNT 1024
76 #define MIN_DIR_COUNT 1
79 #define DMFATTRNAME "SGI_DMI_DMFATTR"
82 main(int argc, char **argv)
88 char name[NAME_MAX + 1];
89 char attr[DMFATTRLEN];
92 while ((c = getopt(argc, argv, "a:b:c:d:f:l:m:n:s:")) != -1) {
95 addval = (uint_t)atoi(optarg);
98 bsize = (size_t)atol(optarg);
101 nchars = (uint_t)atoi(optarg);
107 firstsize = (uint_t)atoi(optarg);
110 lastsize = (uint_t)atoi(optarg);
113 mulval = atof(optarg);
116 ndirs = (uint_t)atoi(optarg);
119 fsize = (off64_t)atol(optarg);
127 if (!addval && !mulval)
129 else if ((addval && mulval) || mulval < 0.0) {
135 buffer = memalign(getpagesize(), bsize);
136 memset(buffer, 0xfeed, bsize);
137 memset(attr, 0xaaaaaaa, sizeof(attr));
142 if (mkdir(directory, 0777) < 0 && errno != EEXIST) {
146 if (chdir(directory) < 0) {
152 firstsize = DFL_FIRST_SIZE;
153 else if (firstsize > MAX_DIR_SIZE)
154 firstsize = MAX_DIR_SIZE;
156 lastsize = DFL_LAST_SIZE;
157 else if (lastsize > MAX_DIR_SIZE)
158 lastsize = MAX_DIR_SIZE;
159 if (lastsize < firstsize)
160 lastsize = firstsize;
161 minchars = hexchars(lastsize - 1);
162 if (nchars < minchars)
164 else if (nchars >= NAME_MAX + 1)
166 if (ndirs > MAX_DIR_COUNT)
167 ndirs = MAX_DIR_COUNT;
168 if (ndirs < MIN_DIR_COUNT)
169 ndirs = MIN_DIR_COUNT;
170 dirchars = hexchars(ndirs);
171 pfxchars = nchars - minchars;
173 memset(&name[dirchars + 1], 'a', pfxchars);
176 for (j = 0; j < ndirs; j++) {
177 filename(0, j, name);
178 name[dirchars] = '\0';
181 for (i = 0; i < cursize; i++) {
182 filename((i + j) % cursize, j, name);
183 close(mkfile(name, attr));
185 printf("%d %.3f\n", cursize,
186 (now() - stime) * 1.0e3 / (cursize * ndirs));
187 cursize = nextsize(cursize);
193 mkfile(char *name, char *attr)
196 ssize_t wrote, wsize;
197 off64_t bytes = fsize;
199 if ((fd = open(name, O_WRONLY | O_CREAT | O_EXCL | O_DIRECT, 0666)) < 0) {
203 if (attr_setf(fd, DMFATTRNAME, attr, DMFATTRLEN, ATTR_ROOT) < 0) {
208 wsize = (bsize < bytes) ? bsize : bytes;
209 if ((wrote = write(fd, buffer, wsize)) < 0) {
219 filename(int idx, int dir, char *name)
221 static char hexc[16] = "0123456789abcdef";
224 for (i = dirchars - 1; i >= 0; i--)
225 *name++ = hexc[(dir >> (4 * i)) & 0xf];
227 name += pfxchars; /* skip pfx a's */
228 for (i = minchars - 1; i >= 0; i--)
229 *name++ = hexc[(idx >> (4 * i)) & 0xf];
234 hexchars(uint_t maxval)
238 if (maxval < 16 * 16)
240 if (maxval < 16 * 16 * 16)
242 if (maxval < 16 * 16 * 16 * 16)
244 if (maxval < 16 * 16 * 16 * 16 * 16)
246 if (maxval < 16 * 16 * 16 * 16 * 16 * 16)
248 if (maxval < 16 * 16 * 16 * 16 * 16 * 16 * 16)
254 nextsize(uint_t cursize)
263 if (n > (double)lastsize + 0.5)
264 return lastsize + 1; /* i.e. out of bounds */
265 else if ((uint_t)n == cursize)
276 gettimeofday(&tv, NULL);
277 return (double)tv.tv_sec + 1.0e-6 * (double)tv.tv_usec;
284 "usage: dirperf [-d dir] [-a addstep | -m mulstep] [-f first] "
285 "[-l last] [-c nchars] [-n ndirs] [-s size]\n");