*** empty log message ***
[arla.git] / tests / fhbench.c
blobd73c976e30ba2ca3adccb1a062141788b10d9949
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.
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.
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.
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.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
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>
45 #ifdef HAVE_SYS_IOCCOM_H
46 #include <sys/ioccom.h>
47 #endif
49 #include <fcntl.h>
51 #include <err.h>
52 #include <roken.h>
53 #include <agetarg.h>
55 #include <atypes.h>
56 #include <roken.h>
57 #include <arla-pioctl.h>
58 #ifdef KERBEROS
59 #include <kafs.h>
60 #endif
62 RCSID("$Id$");
64 struct fhb_handle {
65 char data[512];
68 static int help_flag;
69 static int num_files;
70 static int write_file = 0;
71 static int num_runs = 3;
73 static struct agetargs args[] = {
74 {"num", 'n', aarg_integer, &num_files, "number of files"},
75 {"write", 'w', aarg_integer, &write_file, "write num kb"},
76 {"runs", 'r', aarg_integer, &num_runs, "number of runs"},
77 {"help", 0, aarg_flag, &help_flag, NULL, NULL},
78 {NULL, 0, aarg_end, NULL, NULL, NULL}
82 static void
83 fhb_fhget (char *filename, struct fhb_handle *handle)
85 int ret = 0;
86 #if defined(HAVE_GETFH) && defined(HAVE_FHOPEN)
88 fhandle_t fh;
90 ret = getfh (filename, &fh);
91 if (ret)
92 err (1, "getfh");
93 memcpy (handle, &fh, sizeof(fh));
95 #endif
96 #ifdef KERBEROS
98 struct arlaViceIoctl vice_ioctl;
100 vice_ioctl.in = NULL;
101 vice_ioctl.in_size = 0;
103 vice_ioctl.out = (caddr_t)handle;
104 vice_ioctl.out_size = sizeof(*handle);
106 ret = k_pioctl (filename, ARLA_VIOC_FHGET, (void*)&vice_ioctl, 0);
107 if (ret)
108 errx (1, "k_pioctl");
110 #endif
114 static int
115 fhb_fhopen (struct fhb_handle *handle, int flags)
117 int ret;
118 #if defined(HAVE_GETFH) && defined(HAVE_FHOPEN)
120 fhandle_t fh;
122 memcpy (&fh, handle, sizeof(fh));
123 ret = fhopen (&fh, flags);
124 if (ret >= 0)
125 return ret;
127 #endif
129 #ifdef KERBEROS /* really KAFS */
131 struct arlaViceIoctl vice_ioctl;
133 vice_ioctl.in = (caddr_t)handle;
134 vice_ioctl.in_size = sizeof(*handle);
136 vice_ioctl.out = NULL;
137 vice_ioctl.out_size = 0;
139 ret = k_pioctl (NULL, ARLA_VIOC_FHOPEN, (void*)&vice_ioctl, flags);
140 if (ret >= 0)
141 return ret;
143 #endif
144 errx (1, "fhopen/k_pioctl");
147 static void
148 nop_call (void)
150 #ifdef KERBEROS /* really KAFS */
152 struct arlaViceIoctl vice_ioctl;
153 char c[8];
154 int ret;
156 vice_ioctl.in = (caddr_t)&c;
157 vice_ioctl.in_size = sizeof(c);
159 vice_ioctl.out = NULL;
160 vice_ioctl.out_size = 0;
162 ret = k_pioctl (NULL, ARLA_VIOC_NNPFSDEBUG, (void*)&vice_ioctl, 0);
163 if (ret < 0)
164 err (1, "k_pioctl");
166 #else
168 static first = 1;
169 if (first) {
170 warnx ("can't test this");
171 first = 0;
174 #endif
177 static void
178 create_file (int num, struct fhb_handle *handle)
180 int fd;
181 char filename[1024];
183 snprintf (filename, sizeof(filename), "file-%d", num);
185 fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0666);
186 if (fd < 0)
187 err (1, "open");
189 close (fd);
191 fhb_fhget(filename, handle);
194 char databuf[1024];
196 static void
197 write_to_file (int fd, int num)
199 int ret;
200 while (num > 0) {
201 ret = write (fd, databuf, sizeof(databuf));
202 if (ret != sizeof(databuf))
203 err (1, "write");
204 num--;
208 static void
209 fhopen_file (int num, struct fhb_handle *handle)
211 int fd;
213 fd = fhb_fhopen(handle, O_RDWR);
214 if (fd < 0)
215 err (1, "open");
217 if (write_file)
218 write_to_file(fd, write_file);
219 close(fd);
222 static void
223 open_file (int num)
225 int fd;
226 char filename[1024];
228 snprintf (filename, sizeof(filename), "file-%d", num);
230 fd = open (filename, O_RDWR, 0666);
231 if (fd < 0)
232 err (1, "open");
234 if (write_file)
235 write_to_file(fd, write_file);
237 close (fd);
240 static void
241 unlink_file (int num)
243 int ret;
244 char filename[1024];
246 snprintf (filename, sizeof(filename), "file-%d", num);
248 ret = unlink(filename);
249 if (ret < 0)
250 err (1, "unlink");
254 * Make `t1' consistent.
257 static void
258 tvfix(struct timeval *t1)
260 if (t1->tv_usec < 0) {
261 t1->tv_sec--;
262 t1->tv_usec += 1000000;
264 if (t1->tv_usec >= 1000000) {
265 t1->tv_sec++;
266 t1->tv_usec -= 1000000;
271 * t1 -= t2
274 static void
275 tvsub(struct timeval *t1, const struct timeval *t2)
277 t1->tv_sec -= t2->tv_sec;
278 t1->tv_usec -= t2->tv_usec;
279 tvfix(t1);
283 struct timeval time1, time2;
285 static void
286 starttesting(char *msg)
288 printf("testing %s...\n", msg);
289 fflush (stdout);
290 gettimeofday(&time1, NULL);
293 static void
294 endtesting(void)
296 gettimeofday(&time2, NULL);
297 tvsub(&time2, &time1);
298 printf("timing: %ld.%06ld\n", (long)time2.tv_sec, (long)time2.tv_usec);
301 static void
302 usage (int exit_val)
304 aarg_printusage (args, NULL, "number of files", AARG_GNUSTYLE);
305 exit (exit_val);
308 static void
309 open_bench (int i, struct fhb_handle *handles)
311 printf ("====== test run %d\n"
312 "==================\n",
315 starttesting ("fhopening files");
316 for (i = 0; i < num_files; i++)
317 fhopen_file (i, &handles[i]);
318 endtesting ();
320 starttesting ("opening files");
321 for (i = 0; i < num_files; i++)
322 open_file (i);
323 endtesting ();
327 main (int argc, char **argv)
329 int optind = 0;
330 int i;
331 struct fhb_handle *handles;
333 setprogname (argv[0]);
335 if (agetarg (args, argc, argv, &optind, AARG_GNUSTYLE))
336 usage (1);
338 if (help_flag)
339 usage (0);
341 if (num_files <= 0)
342 usage (1);
344 if (write_file < 0)
345 usage (1);
347 #ifdef KERBEROS
348 if (!k_hasafs())
349 #endif
350 errx (1, "no afs kernel module");
352 handles = emalloc (num_files * sizeof(*handles));
354 starttesting ("creating files");
355 for (i = 0; i < num_files; i++)
356 create_file (i, &handles[i]);
357 endtesting ();
359 for (i = 0 ; i < num_runs; i++)
360 open_bench (i, handles);
362 printf ( "==================\n");
363 starttesting ("unlink files");
364 for (i = 0; i < num_files; i++)
365 unlink_file (i);
366 endtesting ();
368 printf ( "==================\n");
369 starttesting ("nop call");
370 for (i = 0; i < num_files; i++)
371 nop_call ();
372 endtesting ();
374 return 0;