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)
78 signal(SIGALRM, alarm_handler);
81 error = clock_gettime(CLOCK_REALTIME, &ts_start);
90 error = clock_gettime(CLOCK_REALTIME, &ts_end);
95 test_getuid(uintmax_t num, uintmax_t int_arg, const char *path)
100 * Thread-local data should require no locking if system
104 for (i = 0; i < num; i++) {
114 test_getppid(uintmax_t num, uintmax_t int_arg, const char *path)
119 * This is process-local, but can change, so will require a
123 for (i = 0; i < num; i++) {
133 test_clock_gettime(uintmax_t num, uintmax_t int_arg, const char *path)
139 for (i = 0; i < num; i++) {
142 (void)clock_gettime(CLOCK_REALTIME, &ts);
149 test_gettimeofday(uintmax_t num, uintmax_t int_arg, const char *path)
155 for (i = 0; i < num; i++) {
158 (void)gettimeofday(&tv, NULL);
165 test_pipe(uintmax_t num, uintmax_t int_arg, const char *path)
170 * pipe creation is expensive, as it will allocate a new file
171 * descriptor, allocate a new pipe, hook it all up, and return.
172 * Destroying is also expensive, as we now have to free up
173 * the file descriptors and return the pipe.
176 err(-1, "test_pipe: pipe");
180 for (i = 0; i < num; i++) {
184 err(-1, "test_pipe: pipe");
193 test_socket_stream(uintmax_t num, uintmax_t int_arg, const char *path)
198 so = socket(int_arg, SOCK_STREAM, 0);
200 err(-1, "test_socket_stream: socket");
203 for (i = 0; i < num; i++) {
206 so = socket(int_arg, SOCK_STREAM, 0);
208 err(-1, "test_socket_stream: socket");
216 test_socket_dgram(uintmax_t num, uintmax_t int_arg, const char *path)
221 so = socket(int_arg, SOCK_DGRAM, 0);
223 err(-1, "test_socket_dgram: socket");
226 for (i = 0; i < num; i++) {
229 so = socket(int_arg, SOCK_DGRAM, 0);
231 err(-1, "test_socket_dgram: socket");
239 test_socketpair_stream(uintmax_t num, uintmax_t int_arg, const char *path)
244 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, so) == -1)
245 err(-1, "test_socketpair_stream: socketpair");
249 for (i = 0; i < num; i++) {
252 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, so) == -1)
253 err(-1, "test_socketpair_stream: socketpair");
262 test_socketpair_dgram(uintmax_t num, uintmax_t int_arg, const char *path)
267 if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, so) == -1)
268 err(-1, "test_socketpair_dgram: socketpair");
272 for (i = 0; i < num; i++) {
275 if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, so) == -1)
276 err(-1, "test_socketpair_dgram: socketpair");
285 test_create_unlink(uintmax_t num, uintmax_t int_arg, const char *path)
291 fd = open(path, O_RDWR | O_CREAT, 0600);
293 err(-1, "test_create_unlink: create: %s", path);
295 if (unlink(path) < 0)
296 err(-1, "test_create_unlink: unlink: %s", path);
298 for (i = 0; i < num; i++) {
301 fd = open(path, O_RDWR | O_CREAT, 0600);
303 err(-1, "test_create_unlink: create: %s", path);
305 if (unlink(path) < 0)
306 err(-1, "test_create_unlink: unlink: %s", path);
313 test_open_close(uintmax_t num, uintmax_t int_arg, const char *path)
318 fd = open(path, O_RDONLY);
320 err(-1, "test_open_close: %s", path);
324 for (i = 0; i < num; i++) {
327 fd = open(path, O_RDONLY);
329 err(-1, "test_open_close: %s", path);
337 test_read(uintmax_t num, uintmax_t int_arg, const char *path)
343 fd = open(path, O_RDONLY);
345 err(-1, "test_open_read: %s", path);
346 (void)pread(fd, buf, int_arg, 0);
349 for (i = 0; i < num; i++) {
352 (void)pread(fd, buf, int_arg, 0);
360 test_open_read_close(uintmax_t num, uintmax_t int_arg, const char *path)
366 fd = open(path, O_RDONLY);
368 err(-1, "test_open_read_close: %s", path);
369 (void)read(fd, buf, int_arg);
373 for (i = 0; i < num; i++) {
376 fd = open(path, O_RDONLY);
378 err(-1, "test_open_read_close: %s", path);
379 (void)read(fd, buf, int_arg);
387 test_dup(uintmax_t num, uintmax_t int_arg, const char *path)
391 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
393 err(-1, "test_dup: shm_open");
398 for (i = 0; i < num; i++) {
411 test_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_shmfd: shm_open");
421 for (i = 0; i < num; i++) {
424 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
426 err(-1, "test_shmfd: shm_open");
434 test_fstat_shmfd(uintmax_t num, uintmax_t int_arg, const char *path)
440 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
442 err(-1, "test_fstat_shmfd: shm_open");
443 if (fstat(shmfd, &sb) < 0)
444 err(-1, "test_fstat_shmfd: fstat");
446 for (i = 0; i < num; i++) {
449 (void)fstat(shmfd, &sb);
457 test_fork(uintmax_t num, uintmax_t int_arg, const char *path)
464 err(-1, "test_fork: fork");
467 if (waitpid(pid, NULL, 0) < 0)
468 err(-1, "test_fork: waitpid");
470 for (i = 0; i < num; i++) {
475 err(-1, "test_fork: fork");
478 if (waitpid(pid, NULL, 0) < 0)
479 err(-1, "test_fork: waitpid");
486 test_vfork(uintmax_t num, uintmax_t int_arg, const char *path)
493 err(-1, "test_vfork: vfork");
496 if (waitpid(pid, NULL, 0) < 0)
497 err(-1, "test_vfork: waitpid");
499 for (i = 0; i < num; i++) {
504 err(-1, "test_vfork: vfork");
507 if (waitpid(pid, NULL, 0) < 0)
508 err(-1, "test_vfork: waitpid");
514 #define USR_BIN_TRUE "/usr/bin/true"
515 static char *execve_args[] = { USR_BIN_TRUE, NULL};
516 extern char **environ;
519 test_fork_exec(uintmax_t num, uintmax_t int_arg, const char *path)
526 err(-1, "test_fork_exec: fork");
528 (void)execve(USR_BIN_TRUE, execve_args, environ);
531 if (waitpid(pid, NULL, 0) < 0)
532 err(-1, "test_fork: waitpid");
534 for (i = 0; i < num; i++) {
539 err(-1, "test_fork_exec: fork");
541 (void)execve(USR_BIN_TRUE, execve_args, environ);
542 err(-1, "test_fork_exec: execve");
544 if (waitpid(pid, NULL, 0) < 0)
545 err(-1, "test_fork_exec: waitpid");
552 test_vfork_exec(uintmax_t num, uintmax_t int_arg, const char *path)
559 err(-1, "test_vfork_exec: vfork");
561 (void)execve(USR_BIN_TRUE, execve_args, environ);
562 err(-1, "test_vfork_exec: execve");
564 if (waitpid(pid, NULL, 0) < 0)
565 err(-1, "test_vfork_exec: waitpid");
567 for (i = 0; i < num; i++) {
572 err(-1, "test_vfork_exec: vfork");
574 (void)execve(USR_BIN_TRUE, execve_args, environ);
577 if (waitpid(pid, NULL, 0) < 0)
578 err(-1, "test_vfork_exec: waitpid");
585 test_chroot(uintmax_t num, uintmax_t int_arg, const char *path)
590 err(-1, "test_chroot: chroot");
592 for (i = 0; i < num; i++) {
596 err(-1, "test_chroot: chroot");
603 test_setuid(uintmax_t num, uintmax_t int_arg, const char *path)
610 err(-1, "test_setuid: setuid");
612 for (i = 0; i < num; i++) {
616 err(-1, "test_setuid: setuid");
624 uintmax_t (*t_func)(uintmax_t, uintmax_t, const char *);
629 #define FLAG_PATH 0x00000001
631 static const struct test tests[] = {
632 { "getuid", test_getuid },
633 { "getppid", test_getppid },
634 { "clock_gettime", test_clock_gettime },
635 { "gettimeofday", test_gettimeofday },
636 { "pipe", test_pipe },
637 { "socket_local_stream", test_socket_stream, .t_int = PF_LOCAL },
638 { "socket_local_dgram", test_socket_dgram, .t_int = PF_LOCAL },
639 { "socketpair_stream", test_socketpair_stream },
640 { "socketpair_dgram", test_socketpair_dgram },
641 { "socket_tcp", test_socket_stream, .t_int = PF_INET },
642 { "socket_udp", test_socket_dgram, .t_int = PF_INET },
643 { "create_unlink", test_create_unlink, .t_flags = FLAG_PATH },
644 { "open_close", test_open_close, .t_flags = FLAG_PATH },
645 { "open_read_close_1", test_open_read_close, .t_flags = FLAG_PATH,
647 { "open_read_close_10", test_open_read_close, .t_flags = FLAG_PATH,
649 { "open_read_close_100", test_open_read_close, .t_flags = FLAG_PATH,
651 { "open_read_close_1000", test_open_read_close, .t_flags = FLAG_PATH,
653 { "open_read_close_10000", test_open_read_close,
654 .t_flags = FLAG_PATH, .t_int = 10000 },
655 { "open_read_close_100000", test_open_read_close,
656 .t_flags = FLAG_PATH, .t_int = 100000 },
657 { "open_read_close_1000000", test_open_read_close,
658 .t_flags = FLAG_PATH, .t_int = 1000000 },
659 { "read_1", test_read, .t_flags = FLAG_PATH, .t_int = 1 },
660 { "read_10", test_read, .t_flags = FLAG_PATH, .t_int = 10 },
661 { "read_100", test_read, .t_flags = FLAG_PATH, .t_int = 100 },
662 { "read_1000", test_read, .t_flags = FLAG_PATH, .t_int = 1000 },
663 { "read_10000", test_read, .t_flags = FLAG_PATH, .t_int = 10000 },
664 { "read_100000", test_read, .t_flags = FLAG_PATH, .t_int = 100000 },
665 { "read_1000000", test_read, .t_flags = FLAG_PATH, .t_int = 1000000 },
667 { "shmfd", test_shmfd },
668 { "fstat_shmfd", test_fstat_shmfd },
669 { "fork", test_fork },
670 { "vfork", test_vfork },
671 { "fork_exec", test_fork_exec },
672 { "vfork_exec", test_vfork_exec },
673 { "chroot", test_chroot },
674 { "setuid", test_setuid },
676 static const int tests_count = sizeof(tests) / sizeof(tests[0]);
683 fprintf(stderr, "syscall_timing [-i iterations] [-l loops] "
684 "[-p path] [-s seconds] test\n");
685 for (i = 0; i < tests_count; i++)
686 fprintf(stderr, " %s\n", tests[i].t_name);
691 main(int argc, char *argv[])
693 struct timespec ts_res;
694 const struct test *the_test;
698 int ch, error, i, j, k;
699 uintmax_t iterations, loops;
705 while ((ch = getopt(argc, argv, "i:l:p:s:")) != -1) {
708 ll = strtol(optarg, &endp, 10);
709 if (*endp != 0 || ll < 1 || ll > 100000)
715 ll = strtol(optarg, &endp, 10);
716 if (*endp != 0 || ll < 1 || ll > 100000)
726 ll = strtol(optarg, &endp, 10);
727 if (*endp != 0 || ll < 1 || ll > 60*60)
740 if (iterations < 1 && alarm_timeout < 1)
743 iterations = UINT64_MAX;
751 * Validate test list and that, if a path is required, it is
754 for (j = 0; j < argc; j++) {
756 for (i = 0; i < tests_count; i++) {
757 if (strcmp(argv[j], tests[i].t_name) == 0)
758 the_test = &tests[i];
760 if (the_test == NULL)
762 if ((the_test->t_flags & FLAG_PATH) && (path == NULL)) {
763 errx(-1, "%s requires -p", the_test->t_name);
767 error = clock_getres(CLOCK_REALTIME, &ts_res);
769 printf("Clock resolution: %ju.%09ju\n", (uintmax_t)ts_res.tv_sec,
770 (uintmax_t)ts_res.tv_nsec);
771 printf("test\tloop\ttime\titerations\tperiteration\n");
773 for (j = 0; j < argc; j++) {
774 uintmax_t calls, nsecsperit;
777 for (i = 0; i < tests_count; i++) {
778 if (strcmp(argv[j], tests[i].t_name) == 0)
779 the_test = &tests[i];
783 * Run one warmup, then do the real thing (loops) times.
785 the_test->t_func(iterations, the_test->t_int, path);
787 for (k = 0; k < loops; k++) {
788 calls = the_test->t_func(iterations, the_test->t_int,
790 timespecsub(&ts_end, &ts_start);
791 printf("%s\t%d\t", the_test->t_name, k);
792 printf("%ju.%09ju\t%ju\t", (uintmax_t)ts_end.tv_sec,
793 (uintmax_t)ts_end.tv_nsec, calls);
796 * Note. This assumes that each iteration takes less than
797 * a second, and that our total nanoseconds doesn't exceed
798 * the room in our arithmetic unit. Fine for system calls,
799 * but not for long things.
801 nsecsperit = ts_end.tv_sec * 1000000000;
802 nsecsperit += ts_end.tv_nsec;
804 printf("0.%09ju\n", (uintmax_t)nsecsperit);