Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / tests / fhbench.c
1 /*
2 * Copyright (c) 2000 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <unistd.h>
42 #include <limits.h>
43 #include <sys/mount.h>
44
45 #ifdef HAVE_SYS_IOCCOM_H
46 #include <sys/ioccom.h>
47 #endif
48
49 #include <fcntl.h>
50
51 #include <err.h>
52 #include <agetarg.h>
53
54 #include <atypes.h>
55 #include <kafs.h>
56
57
58 struct fhb_handle {
59 char data[512];
60 };
61
62 static int help_flag;
63 static int num_files;
64 static int write_file = 0;
65 static int num_runs = 3;
66
67 static struct agetargs args[] = {
68 {"num", 'n', aarg_integer, &num_files, "number of files"},
69 {"write", 'w', aarg_integer, &write_file, "write num kb"},
70 {"runs", 'r', aarg_integer, &num_runs, "number of runs"},
71 {"help", 0, aarg_flag, &help_flag, NULL, NULL},
72 {NULL, 0, aarg_end, NULL, NULL, NULL}
73 };
74
75
76 static void
77 fhb_fhget(char *filename, struct fhb_handle *handle)
78 {
79 int ret = 0;
80 #if defined(HAVE_GETFH) && defined(HAVE_FHOPEN)
81 {
82 fhandle_t fh;
83
84 ret = getfh(filename, &fh);
85 if (ret)
86 err(1, "getfh");
87 memcpy(handle, &fh, sizeof(fh));
88 }
89 #endif
90 {
91 struct ViceIoctl vice_ioctl;
92
93 vice_ioctl.in = NULL;
94 vice_ioctl.in_size = 0;
95
96 vice_ioctl.out = (caddr_t) handle;
97 vice_ioctl.out_size = sizeof(*handle);
98
99 ret = pioctl(filename, VIOC_FHGET, &vice_ioctl, 0);
100 if (ret)
101 errx(1, "k_pioctl");
102 }
103 }
104
105
106 static int
107 fhb_fhopen(struct fhb_handle *handle, int flags)
108 {
109 int ret;
110 #if defined(HAVE_GETFH) && defined(HAVE_FHOPEN)
111 {
112 fhandle_t fh;
113
114 memcpy(&fh, handle, sizeof(fh));
115 ret = fhopen(&fh, flags);
116 if (ret >= 0)
117 return ret;
118 }
119 #endif
120
121 #ifdef KERBEROS /* really KAFS */
122 {
123 struct ViceIoctl vice_ioctl;
124
125 vice_ioctl.in = (caddr_t) handle;
126 vice_ioctl.in_size = sizeof(*handle);
127
128 vice_ioctl.out = NULL;
129 vice_ioctl.out_size = 0;
130
131 ret = k_pioctl(NULL, VIOC_FHOPEN, &vice_ioctl, flags);
132 if (ret >= 0)
133 return ret;
134 }
135 #endif
136 errx(1, "fhopen/k_pioctl");
137 }
138
139 static void
140 nop_call(void)
141 {
142 #ifdef KERBEROS /* really KAFS */
143 {
144 struct ViceIoctl vice_ioctl;
145 char c[8];
146 int ret;
147
148 vice_ioctl.in = (caddr_t) & c;
149 vice_ioctl.in_size = sizeof(c);
150
151 vice_ioctl.out = NULL;
152 vice_ioctl.out_size = 0;
153
154 ret = k_pioctl(NULL, VIOC_XFSDEBUG, &vice_ioctl, 0);
155 if (ret < 0)
156 err(1, "k_pioctl");
157 }
158 #else
159 {
160 static first = 1;
161 if (first) {
162 warnx("can't test this");
163 first = 0;
164 }
165 }
166 #endif
167 }
168
169 static void
170 create_file(int num, struct fhb_handle *handle)
171 {
172 int fd;
173 char filename[1024];
174
175 snprintf(filename, sizeof(filename), "file-%d", num);
176
177 fd = open(filename, O_CREAT | O_EXCL | O_RDWR, 0666);
178 if (fd < 0)
179 err(1, "open");
180
181 close(fd);
182
183 fhb_fhget(filename, handle);
184 }
185
186 char databuf[1024];
187
188 static void
189 write_to_file(int fd, int num)
190 {
191 int ret;
192 while (num > 0) {
193 ret = write(fd, databuf, sizeof(databuf));
194 if (ret != sizeof(databuf))
195 err(1, "write");
196 num--;
197 }
198 }
199
200 static void
201 fhopen_file(int num, struct fhb_handle *handle)
202 {
203 int fd;
204
205 fd = fhb_fhopen(handle, O_RDWR);
206 if (fd < 0)
207 err(1, "open");
208
209 if (write_file)
210 write_to_file(fd, write_file);
211 close(fd);
212 }
213
214 static void
215 open_file(int num)
216 {
217 int fd;
218 char filename[1024];
219
220 snprintf(filename, sizeof(filename), "file-%d", num);
221
222 fd = open(filename, O_RDWR, 0666);
223 if (fd < 0)
224 err(1, "open");
225
226 if (write_file)
227 write_to_file(fd, write_file);
228
229 close(fd);
230 }
231
232 static void
233 unlink_file(int num)
234 {
235 int ret;
236 char filename[1024];
237
238 snprintf(filename, sizeof(filename), "file-%d", num);
239
240 ret = unlink(filename);
241 if (ret < 0)
242 err(1, "unlink");
243 }
244
245 struct timeval time1, time2;
246
247 static void
248 starttesting(char *msg)
249 {
250 printf("testing %s...\n", msg);
251 fflush(stdout);
252 gettimeofday(&time1, NULL);
253 }
254
255 static void
256 endtesting(void)
257 {
258 gettimeofday(&time2, NULL);
259 timevalsub(&time2, &time1);
260 printf("timing: %ld.%06ld\n", (long)time2.tv_sec, (long)time2.tv_usec);
261 }
262
263 static void
264 usage(int exit_val)
265 {
266 aarg_printusage(args, NULL, "number of files", AARG_GNUSTYLE);
267 exit(exit_val);
268 }
269
270 static void
271 open_bench(int i, struct fhb_handle *handles)
272 {
273 printf("====== test run %d\n" "==================\n", i);
274
275 starttesting("fhopening files");
276 for (i = 0; i < num_files; i++)
277 fhopen_file(i, &handles[i]);
278 endtesting();
279
280 starttesting("opening files");
281 for (i = 0; i < num_files; i++)
282 open_file(i);
283 endtesting();
284 }
285
286 int
287 main(int argc, char **argv)
288 {
289 int optind = 0;
290 int i;
291 struct fhb_handle *handles;
292
293
294 if (agetarg(args, argc, argv, &optind, AARG_GNUSTYLE))
295 usage(1);
296
297 if (help_flag)
298 usage(0);
299
300 if (num_files <= 0)
301 usage(1);
302
303 if (write_file < 0)
304 usage(1);
305
306 #ifdef KERBEROS
307 if (!k_hasafs())
308 #endif
309 errx(1, "no afs kernel module");
310
311 handles = emalloc(num_files * sizeof(*handles));
312
313 starttesting("creating files");
314 for (i = 0; i < num_files; i++)
315 create_file(i, &handles[i]);
316 endtesting();
317
318 for (i = 0; i < num_runs; i++)
319 open_bench(i, handles);
320
321 printf("==================\n");
322 starttesting("unlink files");
323 for (i = 0; i < num_files; i++)
324 unlink_file(i);
325 endtesting();
326
327 printf("==================\n");
328 starttesting("nop call");
329 for (i = 0; i < num_files; i++)
330 nop_call();
331 endtesting();
332
333 return 0;
334 }