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/resource.h>
35 #include <sys/socket.h>
52 static struct timespec ts_start, ts_end;
53 static int alarm_timeout;
54 static volatile int alarm_fired;
56 #define timespecsub(vvp, uvp) \
58 (vvp)->tv_sec -= (uvp)->tv_sec; \
59 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
60 if ((vvp)->tv_nsec < 0) { \
62 (vvp)->tv_nsec += 1000000000; \
67 alarm_handler(int signum)
80 signal(SIGALRM, alarm_handler);
83 error = clock_gettime(CLOCK_REALTIME, &ts_start);
92 error = clock_gettime(CLOCK_REALTIME, &ts_end);
97 test_getuid(uintmax_t num, uintmax_t int_arg, const char *path)
102 * Thread-local data should require no locking if system
106 for (i = 0; i < num; i++) {
116 test_getppid(uintmax_t num, uintmax_t int_arg, const char *path)
121 * This is process-local, but can change, so will require a
125 for (i = 0; i < num; i++) {
135 test_clock_gettime(uintmax_t num, uintmax_t int_arg, const char *path)
141 for (i = 0; i < num; i++) {
144 (void)clock_gettime(CLOCK_REALTIME, &ts);
151 test_gettimeofday(uintmax_t num, uintmax_t int_arg, const char *path)
157 for (i = 0; i < num; i++) {
160 (void)gettimeofday(&tv, NULL);
167 test_getpriority(uintmax_t num, uintmax_t int_arg, const char *path)
172 for (i = 0; i < num; i++) {
175 (void)getpriority(PRIO_PROCESS, 0);
183 test_pipe(uintmax_t num, uintmax_t int_arg, const char *path)
188 * pipe creation is expensive, as it will allocate a new file
189 * descriptor, allocate a new pipe, hook it all up, and return.
190 * Destroying is also expensive, as we now have to free up
191 * the file descriptors and return the pipe.
194 err(-1, "test_pipe: pipe");
198 for (i = 0; i < num; i++) {
202 err(-1, "test_pipe: pipe");
211 test_socket_stream(uintmax_t num, uintmax_t int_arg, const char *path)
216 so = socket(int_arg, SOCK_STREAM, 0);
218 err(-1, "test_socket_stream: socket");
221 for (i = 0; i < num; i++) {
224 so = socket(int_arg, SOCK_STREAM, 0);
226 err(-1, "test_socket_stream: socket");
234 test_socket_dgram(uintmax_t num, uintmax_t int_arg, const char *path)
239 so = socket(int_arg, SOCK_DGRAM, 0);
241 err(-1, "test_socket_dgram: socket");
244 for (i = 0; i < num; i++) {
247 so = socket(int_arg, SOCK_DGRAM, 0);
249 err(-1, "test_socket_dgram: socket");
257 test_socketpair_stream(uintmax_t num, uintmax_t int_arg, const char *path)
262 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, so) == -1)
263 err(-1, "test_socketpair_stream: socketpair");
267 for (i = 0; i < num; i++) {
270 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, so) == -1)
271 err(-1, "test_socketpair_stream: socketpair");
280 test_socketpair_dgram(uintmax_t num, uintmax_t int_arg, const char *path)
285 if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, so) == -1)
286 err(-1, "test_socketpair_dgram: socketpair");
290 for (i = 0; i < num; i++) {
293 if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, so) == -1)
294 err(-1, "test_socketpair_dgram: socketpair");
303 test_create_unlink(uintmax_t num, uintmax_t int_arg, const char *path)
309 fd = open(path, O_RDWR | O_CREAT, 0600);
311 err(-1, "test_create_unlink: create: %s", path);
313 if (unlink(path) < 0)
314 err(-1, "test_create_unlink: unlink: %s", path);
316 for (i = 0; i < num; i++) {
319 fd = open(path, O_RDWR | O_CREAT, 0600);
321 err(-1, "test_create_unlink: create: %s", path);
323 if (unlink(path) < 0)
324 err(-1, "test_create_unlink: unlink: %s", path);
331 test_open_close(uintmax_t num, uintmax_t int_arg, const char *path)
336 fd = open(path, O_RDONLY);
338 err(-1, "test_open_close: %s", path);
342 for (i = 0; i < num; i++) {
345 fd = open(path, O_RDONLY);
347 err(-1, "test_open_close: %s", path);
355 test_read(uintmax_t num, uintmax_t int_arg, const char *path)
361 fd = open(path, O_RDONLY);
363 err(-1, "test_open_read: %s", path);
364 (void)pread(fd, buf, int_arg, 0);
367 for (i = 0; i < num; i++) {
370 (void)pread(fd, buf, int_arg, 0);
378 test_open_read_close(uintmax_t num, uintmax_t int_arg, const char *path)
384 fd = open(path, O_RDONLY);
386 err(-1, "test_open_read_close: %s", path);
387 (void)read(fd, buf, int_arg);
391 for (i = 0; i < num; i++) {
394 fd = open(path, O_RDONLY);
396 err(-1, "test_open_read_close: %s", path);
397 (void)read(fd, buf, int_arg);
405 test_dup(uintmax_t num, uintmax_t int_arg, const char *path)
409 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
411 err(-1, "test_dup: shm_open");
416 for (i = 0; i < num; i++) {
429 test_shmfd(uintmax_t num, uintmax_t int_arg, const char *path)
434 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
436 err(-1, "test_shmfd: shm_open");
439 for (i = 0; i < num; i++) {
442 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
444 err(-1, "test_shmfd: shm_open");
452 test_fstat_shmfd(uintmax_t num, uintmax_t int_arg, const char *path)
458 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
460 err(-1, "test_fstat_shmfd: shm_open");
461 if (fstat(shmfd, &sb) < 0)
462 err(-1, "test_fstat_shmfd: fstat");
464 for (i = 0; i < num; i++) {
467 (void)fstat(shmfd, &sb);
475 test_fork(uintmax_t num, uintmax_t int_arg, const char *path)
482 err(-1, "test_fork: fork");
485 if (waitpid(pid, NULL, 0) < 0)
486 err(-1, "test_fork: waitpid");
488 for (i = 0; i < num; i++) {
493 err(-1, "test_fork: fork");
496 if (waitpid(pid, NULL, 0) < 0)
497 err(-1, "test_fork: waitpid");
504 test_vfork(uintmax_t num, uintmax_t int_arg, const char *path)
511 err(-1, "test_vfork: vfork");
514 if (waitpid(pid, NULL, 0) < 0)
515 err(-1, "test_vfork: waitpid");
517 for (i = 0; i < num; i++) {
522 err(-1, "test_vfork: vfork");
525 if (waitpid(pid, NULL, 0) < 0)
526 err(-1, "test_vfork: waitpid");
532 #define USR_BIN_TRUE "/usr/bin/true"
533 static char *execve_args[] = { USR_BIN_TRUE, NULL};
534 extern char **environ;
537 test_fork_exec(uintmax_t num, uintmax_t int_arg, const char *path)
544 err(-1, "test_fork_exec: fork");
546 (void)execve(USR_BIN_TRUE, execve_args, environ);
549 if (waitpid(pid, NULL, 0) < 0)
550 err(-1, "test_fork: waitpid");
552 for (i = 0; i < num; i++) {
557 err(-1, "test_fork_exec: fork");
559 (void)execve(USR_BIN_TRUE, execve_args, environ);
560 err(-1, "test_fork_exec: execve");
562 if (waitpid(pid, NULL, 0) < 0)
563 err(-1, "test_fork_exec: waitpid");
570 test_vfork_exec(uintmax_t num, uintmax_t int_arg, const char *path)
577 err(-1, "test_vfork_exec: vfork");
579 (void)execve(USR_BIN_TRUE, execve_args, environ);
580 err(-1, "test_vfork_exec: execve");
582 if (waitpid(pid, NULL, 0) < 0)
583 err(-1, "test_vfork_exec: waitpid");
585 for (i = 0; i < num; i++) {
590 err(-1, "test_vfork_exec: vfork");
592 (void)execve(USR_BIN_TRUE, execve_args, environ);
595 if (waitpid(pid, NULL, 0) < 0)
596 err(-1, "test_vfork_exec: waitpid");
603 test_chroot(uintmax_t num, uintmax_t int_arg, const char *path)
608 err(-1, "test_chroot: chroot");
610 for (i = 0; i < num; i++) {
614 err(-1, "test_chroot: chroot");
621 test_setuid(uintmax_t num, uintmax_t int_arg, const char *path)
628 err(-1, "test_setuid: setuid");
630 for (i = 0; i < num; i++) {
634 err(-1, "test_setuid: setuid");
642 uintmax_t (*t_func)(uintmax_t, uintmax_t, const char *);
647 #define FLAG_PATH 0x00000001
649 static const struct test tests[] = {
650 { "getuid", test_getuid },
651 { "getppid", test_getppid },
652 { "clock_gettime", test_clock_gettime },
653 { "gettimeofday", test_gettimeofday },
654 { "getpriority", test_getpriority },
655 { "pipe", test_pipe },
656 { "socket_local_stream", test_socket_stream, .t_int = PF_LOCAL },
657 { "socket_local_dgram", test_socket_dgram, .t_int = PF_LOCAL },
658 { "socketpair_stream", test_socketpair_stream },
659 { "socketpair_dgram", test_socketpair_dgram },
660 { "socket_tcp", test_socket_stream, .t_int = PF_INET },
661 { "socket_udp", test_socket_dgram, .t_int = PF_INET },
662 { "create_unlink", test_create_unlink, .t_flags = FLAG_PATH },
663 { "open_close", test_open_close, .t_flags = FLAG_PATH },
664 { "open_read_close_1", test_open_read_close, .t_flags = FLAG_PATH,
666 { "open_read_close_10", test_open_read_close, .t_flags = FLAG_PATH,
668 { "open_read_close_100", test_open_read_close, .t_flags = FLAG_PATH,
670 { "open_read_close_1000", test_open_read_close, .t_flags = FLAG_PATH,
672 { "open_read_close_10000", test_open_read_close,
673 .t_flags = FLAG_PATH, .t_int = 10000 },
674 { "open_read_close_100000", test_open_read_close,
675 .t_flags = FLAG_PATH, .t_int = 100000 },
676 { "open_read_close_1000000", test_open_read_close,
677 .t_flags = FLAG_PATH, .t_int = 1000000 },
678 { "read_1", test_read, .t_flags = FLAG_PATH, .t_int = 1 },
679 { "read_10", test_read, .t_flags = FLAG_PATH, .t_int = 10 },
680 { "read_100", test_read, .t_flags = FLAG_PATH, .t_int = 100 },
681 { "read_1000", test_read, .t_flags = FLAG_PATH, .t_int = 1000 },
682 { "read_10000", test_read, .t_flags = FLAG_PATH, .t_int = 10000 },
683 { "read_100000", test_read, .t_flags = FLAG_PATH, .t_int = 100000 },
684 { "read_1000000", test_read, .t_flags = FLAG_PATH, .t_int = 1000000 },
686 { "shmfd", test_shmfd },
687 { "fstat_shmfd", test_fstat_shmfd },
688 { "fork", test_fork },
689 { "vfork", test_vfork },
690 { "fork_exec", test_fork_exec },
691 { "vfork_exec", test_vfork_exec },
692 { "chroot", test_chroot },
693 { "setuid", test_setuid },
695 static const int tests_count = sizeof(tests) / sizeof(tests[0]);
702 fprintf(stderr, "syscall_timing [-i iterations] [-l loops] "
703 "[-p path] [-s seconds] test\n");
704 for (i = 0; i < tests_count; i++)
705 fprintf(stderr, " %s\n", tests[i].t_name);
710 main(int argc, char *argv[])
712 struct timespec ts_res;
713 const struct test *the_test;
715 char *tmp_dir, *tmp_path;
718 int ch, fd, error, i, j, k, rv;
719 uintmax_t iterations, loops;
726 while ((ch = getopt(argc, argv, "i:l:p:s:")) != -1) {
729 ll = strtol(optarg, &endp, 10);
730 if (*endp != 0 || ll < 1 || ll > 100000)
736 ll = strtol(optarg, &endp, 10);
737 if (*endp != 0 || ll < 1 || ll > 100000)
747 ll = strtol(optarg, &endp, 10);
748 if (*endp != 0 || ll < 1 || ll > 60*60)
761 if (iterations < 1 && alarm_timeout < 1)
764 iterations = UINT64_MAX;
772 * Validate test list and that, if a path is required, it is
775 for (j = 0; j < argc; j++) {
777 for (i = 0; i < tests_count; i++) {
778 if (strcmp(argv[j], tests[i].t_name) == 0)
779 the_test = &tests[i];
781 if (the_test == NULL)
783 if ((the_test->t_flags & FLAG_PATH) && (path == NULL)) {
784 tmp_dir = strdup("/tmp/syscall_timing.XXXXXXXX");
787 tmp_dir = mkdtemp(tmp_dir);
790 rv = asprintf(&tmp_path, "%s/testfile", tmp_dir);
796 error = clock_getres(CLOCK_REALTIME, &ts_res);
798 printf("Clock resolution: %ju.%09ju\n", (uintmax_t)ts_res.tv_sec,
799 (uintmax_t)ts_res.tv_nsec);
800 printf("test\tloop\ttime\titerations\tperiteration\n");
802 for (j = 0; j < argc; j++) {
803 uintmax_t calls, nsecsperit;
806 for (i = 0; i < tests_count; i++) {
807 if (strcmp(argv[j], tests[i].t_name) == 0)
808 the_test = &tests[i];
811 if (tmp_path != NULL) {
812 fd = open(tmp_path, O_WRONLY | O_CREAT, 0700);
814 err(1, "cannot open %s", tmp_path);
815 error = ftruncate(fd, 1000000);
825 * Run one warmup, then do the real thing (loops) times.
827 the_test->t_func(iterations, the_test->t_int, path);
829 for (k = 0; k < loops; k++) {
830 calls = the_test->t_func(iterations, the_test->t_int,
832 timespecsub(&ts_end, &ts_start);
833 printf("%s\t%d\t", the_test->t_name, k);
834 printf("%ju.%09ju\t%ju\t", (uintmax_t)ts_end.tv_sec,
835 (uintmax_t)ts_end.tv_nsec, calls);
838 * Note. This assumes that each iteration takes less than
839 * a second, and that our total nanoseconds doesn't exceed
840 * the room in our arithmetic unit. Fine for system calls,
841 * but not for long things.
843 nsecsperit = ts_end.tv_sec * 1000000000;
844 nsecsperit += ts_end.tv_nsec;
846 printf("0.%09ju\n", (uintmax_t)nsecsperit);
850 if (tmp_path != NULL) {
851 error = unlink(tmp_path);
852 if (error != 0 && errno != ENOENT)
853 warn("cannot unlink %s", tmp_path);
854 error = rmdir(tmp_dir);
856 warn("cannot rmdir %s", tmp_dir);