2 * Copyright (c) 2003-2004, 2010 Robert N. M. Watson
5 * Portions of this software were developed at the University of Cambridge
6 * Computer Laboratory with support from a grant from Google, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * 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 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
34 #include <sys/socket.h>
50 static struct timespec ts_start, ts_end;
51 static int alarm_timeout;
52 static volatile int alarm_fired;
54 #define timespecsub(vvp, uvp) \
56 (vvp)->tv_sec -= (uvp)->tv_sec; \
57 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
58 if ((vvp)->tv_nsec < 0) { \
60 (vvp)->tv_nsec += 1000000000; \
65 alarm_handler(int signum)
77 signal(SIGALRM, alarm_handler);
80 assert(clock_gettime(CLOCK_REALTIME, &ts_start) == 0);
87 assert(clock_gettime(CLOCK_REALTIME, &ts_end) == 0);
91 test_getuid(uintmax_t num, uintmax_t int_arg, const char *path)
96 * Thread-local data should require no locking if system
100 for (i = 0; i < num; i++) {
110 test_getppid(uintmax_t num, uintmax_t int_arg, const char *path)
115 * This is process-local, but can change, so will require a
119 for (i = 0; i < num; i++) {
129 test_clock_gettime(uintmax_t num, uintmax_t int_arg, const char *path)
135 for (i = 0; i < num; i++) {
138 (void)clock_gettime(CLOCK_REALTIME, &ts);
145 test_pipe(uintmax_t num, uintmax_t int_arg, const char *path)
150 * pipe creation is expensive, as it will allocate a new file
151 * descriptor, allocate a new pipe, hook it all up, and return.
152 * Destroying is also expensive, as we now have to free up
153 * the file descriptors and return the pipe.
156 err(-1, "test_pipe: pipe");
160 for (i = 0; i < num; i++) {
164 err(-1, "test_pipe: pipe");
173 test_socket_stream(uintmax_t num, uintmax_t int_arg, const char *path)
177 so = socket(int_arg, SOCK_STREAM, 0);
179 err(-1, "test_socket_stream: socket");
182 for (i = 0; i < num; i++) {
185 so = socket(int_arg, SOCK_STREAM, 0);
187 err(-1, "test_socket_stream: socket");
195 test_socket_dgram(uintmax_t num, uintmax_t int_arg, const char *path)
199 so = socket(int_arg, SOCK_DGRAM, 0);
201 err(-1, "test_socket_dgram: socket");
204 for (i = 0; i < num; i++) {
207 so = socket(int_arg, SOCK_DGRAM, 0);
209 err(-1, "test_socket_dgram: socket");
217 test_socketpair_stream(uintmax_t num, uintmax_t int_arg, const char *path)
222 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, so) == -1)
223 err(-1, "test_socketpair_stream: socketpair");
227 for (i = 0; i < num; i++) {
230 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, so) == -1)
231 err(-1, "test_socketpair_stream: socketpair");
240 test_socketpair_dgram(uintmax_t num, uintmax_t int_arg, const char *path)
245 if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, so) == -1)
246 err(-1, "test_socketpair_dgram: socketpair");
250 for (i = 0; i < num; i++) {
253 if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, so) == -1)
254 err(-1, "test_socketpair_dgram: socketpair");
263 test_create_unlink(uintmax_t num, uintmax_t int_arg, const char *path)
269 fd = open(path, O_RDWR | O_CREAT, 0600);
271 err(-1, "test_create_unlink: create: %s", path);
273 if (unlink(path) < 0)
274 err(-1, "test_create_unlink: unlink: %s", path);
276 for (i = 0; i < num; i++) {
279 fd = open(path, O_RDWR | O_CREAT, 0600);
281 err(-1, "test_create_unlink: create: %s", path);
283 if (unlink(path) < 0)
284 err(-1, "test_create_unlink: unlink: %s", path);
291 test_open_close(uintmax_t num, uintmax_t int_arg, const char *path)
296 fd = open(path, O_RDONLY);
298 err(-1, "test_open_close: %s", path);
302 for (i = 0; i < num; i++) {
305 fd = open(path, O_RDONLY);
307 err(-1, "test_open_close: %s", path);
315 test_read(uintmax_t num, uintmax_t int_arg, const char *path)
321 fd = open(path, O_RDONLY);
323 err(-1, "test_open_read: %s", path);
324 (void)pread(fd, buf, int_arg, 0);
327 for (i = 0; i < num; i++) {
330 (void)pread(fd, buf, int_arg, 0);
338 test_open_read_close(uintmax_t num, uintmax_t int_arg, const char *path)
344 fd = open(path, O_RDONLY);
346 err(-1, "test_open_read_close: %s", path);
347 (void)read(fd, buf, int_arg);
351 for (i = 0; i < num; i++) {
354 fd = open(path, O_RDONLY);
356 err(-1, "test_open_read_close: %s", path);
357 (void)read(fd, buf, int_arg);
365 test_dup(uintmax_t num, uintmax_t int_arg, const char *path)
369 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
371 err(-1, "test_dup: shm_open");
376 for (i = 0; i < num; i++) {
389 test_shmfd(uintmax_t num, uintmax_t int_arg, const char *path)
393 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
395 err(-1, "test_shmfd: shm_open");
398 for (i = 0; i < num; i++) {
401 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
403 err(-1, "test_shmfd: shm_open");
411 test_fstat_shmfd(uintmax_t num, uintmax_t int_arg, const char *path)
416 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
418 err(-1, "test_fstat_shmfd: shm_open");
419 if (fstat(shmfd, &sb) < 0)
420 err(-1, "test_fstat_shmfd: fstat");
422 for (i = 0; i < num; i++) {
425 (void)fstat(shmfd, &sb);
433 test_fork(uintmax_t num, uintmax_t int_arg, const char *path)
440 err(-1, "test_fork: fork");
443 if (waitpid(pid, NULL, 0) < 0)
444 err(-1, "test_fork: waitpid");
446 for (i = 0; i < num; i++) {
451 err(-1, "test_fork: fork");
454 if (waitpid(pid, NULL, 0) < 0)
455 err(-1, "test_fork: waitpid");
462 test_vfork(uintmax_t num, uintmax_t int_arg, const char *path)
469 err(-1, "test_vfork: vfork");
472 if (waitpid(pid, NULL, 0) < 0)
473 err(-1, "test_vfork: waitpid");
475 for (i = 0; i < num; i++) {
480 err(-1, "test_vfork: vfork");
483 if (waitpid(pid, NULL, 0) < 0)
484 err(-1, "test_vfork: waitpid");
490 #define USR_BIN_TRUE "/usr/bin/true"
491 static char *execve_args[] = { USR_BIN_TRUE, NULL};
492 extern char **environ;
495 test_fork_exec(uintmax_t num, uintmax_t int_arg, const char *path)
502 err(-1, "test_fork_exec: fork");
504 (void)execve(USR_BIN_TRUE, execve_args, environ);
507 if (waitpid(pid, NULL, 0) < 0)
508 err(-1, "test_fork: waitpid");
510 for (i = 0; i < num; i++) {
515 err(-1, "test_fork_exec: fork");
517 (void)execve(USR_BIN_TRUE, execve_args, environ);
518 err(-1, "test_fork_exec: execve");
520 if (waitpid(pid, NULL, 0) < 0)
521 err(-1, "test_fork_exec: waitpid");
528 test_vfork_exec(uintmax_t num, uintmax_t int_arg, const char *path)
535 err(-1, "test_vfork_exec: vfork");
537 (void)execve(USR_BIN_TRUE, execve_args, environ);
538 err(-1, "test_vfork_exec: execve");
540 if (waitpid(pid, NULL, 0) < 0)
541 err(-1, "test_vfork_exec: waitpid");
543 for (i = 0; i < num; i++) {
548 err(-1, "test_vfork_exec: vfork");
550 (void)execve(USR_BIN_TRUE, execve_args, environ);
553 if (waitpid(pid, NULL, 0) < 0)
554 err(-1, "test_vfork_exec: waitpid");
561 test_chroot(uintmax_t num, uintmax_t int_arg, const char *path)
566 err(-1, "test_chroot: chroot");
568 for (i = 0; i < num; i++) {
572 err(-1, "test_chroot: chroot");
579 test_setuid(uintmax_t num, uintmax_t int_arg, const char *path)
586 err(-1, "test_setuid: setuid");
588 for (i = 0; i < num; i++) {
592 err(-1, "test_setuid: setuid");
600 uintmax_t (*t_func)(uintmax_t, uintmax_t, const char *);
605 #define FLAG_PATH 0x00000001
607 static const struct test tests[] = {
608 { "getuid", test_getuid },
609 { "getppid", test_getppid },
610 { "clock_gettime", test_clock_gettime },
611 { "pipe", test_pipe },
612 { "socket_local_stream", test_socket_stream, .t_int = PF_LOCAL },
613 { "socket_local_dgram", test_socket_dgram, .t_int = PF_LOCAL },
614 { "socketpair_stream", test_socketpair_stream },
615 { "socketpair_dgram", test_socketpair_dgram },
616 { "socket_tcp", test_socket_stream, .t_int = PF_INET },
617 { "socket_udp", test_socket_dgram, .t_int = PF_INET },
618 { "create_unlink", test_create_unlink, .t_flags = FLAG_PATH },
619 { "open_close", test_open_close, .t_flags = FLAG_PATH },
620 { "open_read_close_1", test_open_read_close, .t_flags = FLAG_PATH,
622 { "open_read_close_10", test_open_read_close, .t_flags = FLAG_PATH,
624 { "open_read_close_100", test_open_read_close, .t_flags = FLAG_PATH,
626 { "open_read_close_1000", test_open_read_close, .t_flags = FLAG_PATH,
628 { "open_read_close_10000", test_open_read_close,
629 .t_flags = FLAG_PATH, .t_int = 10000 },
630 { "open_read_close_100000", test_open_read_close,
631 .t_flags = FLAG_PATH, .t_int = 100000 },
632 { "open_read_close_1000000", test_open_read_close,
633 .t_flags = FLAG_PATH, .t_int = 1000000 },
634 { "read_1", test_read, .t_flags = FLAG_PATH, .t_int = 1 },
635 { "read_10", test_read, .t_flags = FLAG_PATH, .t_int = 10 },
636 { "read_100", test_read, .t_flags = FLAG_PATH, .t_int = 100 },
637 { "read_1000", test_read, .t_flags = FLAG_PATH, .t_int = 1000 },
638 { "read_10000", test_read, .t_flags = FLAG_PATH, .t_int = 10000 },
639 { "read_100000", test_read, .t_flags = FLAG_PATH, .t_int = 100000 },
640 { "read_1000000", test_read, .t_flags = FLAG_PATH, .t_int = 1000000 },
642 { "shmfd", test_shmfd },
643 { "fstat_shmfd", test_fstat_shmfd },
644 { "fork", test_fork },
645 { "vfork", test_vfork },
646 { "fork_exec", test_fork_exec },
647 { "vfork_exec", test_vfork_exec },
648 { "chroot", test_chroot },
649 { "setuid", test_setuid },
651 static const int tests_count = sizeof(tests) / sizeof(tests[0]);
658 fprintf(stderr, "syscall_timing [-i iterations] [-l loops] "
659 "[-p path] [-s seconds] test\n");
660 for (i = 0; i < tests_count; i++)
661 fprintf(stderr, " %s\n", tests[i].t_name);
666 main(int argc, char *argv[])
668 struct timespec ts_res;
669 const struct test *the_test;
674 uintmax_t iterations, loops;
680 while ((ch = getopt(argc, argv, "i:l:p:s:")) != -1) {
683 ll = strtol(optarg, &endp, 10);
684 if (*endp != 0 || ll < 1 || ll > 100000)
690 ll = strtol(optarg, &endp, 10);
691 if (*endp != 0 || ll < 1 || ll > 100000)
701 ll = strtol(optarg, &endp, 10);
702 if (*endp != 0 || ll < 1 || ll > 60*60)
715 if (iterations < 1 && alarm_timeout < 1)
718 iterations = UINT64_MAX;
726 * Validate test list and that, if a path is required, it is
729 for (j = 0; j < argc; j++) {
731 for (i = 0; i < tests_count; i++) {
732 if (strcmp(argv[j], tests[i].t_name) == 0)
733 the_test = &tests[i];
735 if (the_test == NULL)
737 if ((the_test->t_flags & FLAG_PATH) && (path == NULL)) {
738 errx(-1, "%s requires -p", the_test->t_name);
742 assert(clock_getres(CLOCK_REALTIME, &ts_res) == 0);
743 printf("Clock resolution: %ju.%09ju\n", (uintmax_t)ts_res.tv_sec,
744 (uintmax_t)ts_res.tv_nsec);
745 printf("test\tloop\ttime\titerations\tperiteration\n");
747 for (j = 0; j < argc; j++) {
748 uintmax_t calls, nsecsperit;
751 for (i = 0; i < tests_count; i++) {
752 if (strcmp(argv[j], tests[i].t_name) == 0)
753 the_test = &tests[i];
757 * Run one warmup, then do the real thing (loops) times.
759 the_test->t_func(iterations, the_test->t_int, path);
761 for (k = 0; k < loops; k++) {
762 calls = the_test->t_func(iterations, the_test->t_int,
764 timespecsub(&ts_end, &ts_start);
765 printf("%s\t%d\t", the_test->t_name, k);
766 printf("%ju.%09ju\t%d\t", (uintmax_t)ts_end.tv_sec,
767 (uintmax_t)ts_end.tv_nsec, calls);
770 * Note. This assumes that each iteration takes less than
771 * a second, and that our total nanoseconds doesn't exceed
772 * the room in our arithmetic unit. Fine for system calls,
773 * but not for long things.
775 nsecsperit = ts_end.tv_sec * 1000000000;
776 nsecsperit += ts_end.tv_nsec;
778 printf("0.%09ju\n", (uintmax_t)nsecsperit);