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);
182 test_pipe(uintmax_t num, uintmax_t int_arg, const char *path)
187 * pipe creation is expensive, as it will allocate a new file
188 * descriptor, allocate a new pipe, hook it all up, and return.
189 * Destroying is also expensive, as we now have to free up
190 * the file descriptors and return the pipe.
193 err(-1, "test_pipe: pipe");
197 for (i = 0; i < num; i++) {
201 err(-1, "test_pipe: pipe");
210 test_select(uintmax_t num, uintmax_t int_arg, const char *path)
212 fd_set readfds, writefds, exceptfds;
225 for (i = 0; i < num; i++) {
228 (void)select(0, &readfds, &writefds, &exceptfds, &tv);
235 test_socket_stream(uintmax_t num, uintmax_t int_arg, const char *path)
240 so = socket(int_arg, SOCK_STREAM, 0);
242 err(-1, "test_socket_stream: socket");
245 for (i = 0; i < num; i++) {
248 so = socket(int_arg, SOCK_STREAM, 0);
250 err(-1, "test_socket_stream: socket");
258 test_socket_dgram(uintmax_t num, uintmax_t int_arg, const char *path)
263 so = socket(int_arg, SOCK_DGRAM, 0);
265 err(-1, "test_socket_dgram: socket");
268 for (i = 0; i < num; i++) {
271 so = socket(int_arg, SOCK_DGRAM, 0);
273 err(-1, "test_socket_dgram: socket");
281 test_socketpair_stream(uintmax_t num, uintmax_t int_arg, const char *path)
286 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, so) == -1)
287 err(-1, "test_socketpair_stream: socketpair");
291 for (i = 0; i < num; i++) {
294 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, so) == -1)
295 err(-1, "test_socketpair_stream: socketpair");
304 test_socketpair_dgram(uintmax_t num, uintmax_t int_arg, const char *path)
309 if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, so) == -1)
310 err(-1, "test_socketpair_dgram: socketpair");
314 for (i = 0; i < num; i++) {
317 if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, so) == -1)
318 err(-1, "test_socketpair_dgram: socketpair");
327 test_create_unlink(uintmax_t num, uintmax_t int_arg, const char *path)
333 fd = open(path, O_RDWR | O_CREAT, 0600);
335 err(-1, "test_create_unlink: create: %s", path);
337 if (unlink(path) < 0)
338 err(-1, "test_create_unlink: unlink: %s", path);
340 for (i = 0; i < num; i++) {
343 fd = open(path, O_RDWR | O_CREAT, 0600);
345 err(-1, "test_create_unlink: create: %s", path);
347 if (unlink(path) < 0)
348 err(-1, "test_create_unlink: unlink: %s", path);
355 test_open_close(uintmax_t num, uintmax_t int_arg, const char *path)
360 fd = open(path, O_RDONLY);
362 err(-1, "test_open_close: %s", path);
366 for (i = 0; i < num; i++) {
369 fd = open(path, O_RDONLY);
371 err(-1, "test_open_close: %s", path);
379 test_read(uintmax_t num, uintmax_t int_arg, const char *path)
385 fd = open(path, O_RDONLY);
387 err(-1, "test_open_read: %s", path);
388 (void)pread(fd, buf, int_arg, 0);
391 for (i = 0; i < num; i++) {
394 (void)pread(fd, buf, int_arg, 0);
402 test_open_read_close(uintmax_t num, uintmax_t int_arg, const char *path)
408 fd = open(path, O_RDONLY);
410 err(-1, "test_open_read_close: %s", path);
411 (void)read(fd, buf, int_arg);
415 for (i = 0; i < num; i++) {
418 fd = open(path, O_RDONLY);
420 err(-1, "test_open_read_close: %s", path);
421 (void)read(fd, buf, int_arg);
429 test_dup(uintmax_t num, uintmax_t int_arg, const char *path)
433 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
435 err(-1, "test_dup: shm_open");
440 for (i = 0; i < num; i++) {
453 test_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_shmfd: shm_open");
463 for (i = 0; i < num; i++) {
466 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
468 err(-1, "test_shmfd: shm_open");
476 test_fstat_shmfd(uintmax_t num, uintmax_t int_arg, const char *path)
482 shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
484 err(-1, "test_fstat_shmfd: shm_open");
485 if (fstat(shmfd, &sb) < 0)
486 err(-1, "test_fstat_shmfd: fstat");
488 for (i = 0; i < num; i++) {
491 (void)fstat(shmfd, &sb);
499 test_fork(uintmax_t num, uintmax_t int_arg, const char *path)
506 err(-1, "test_fork: fork");
509 if (waitpid(pid, NULL, 0) < 0)
510 err(-1, "test_fork: waitpid");
512 for (i = 0; i < num; i++) {
517 err(-1, "test_fork: fork");
520 if (waitpid(pid, NULL, 0) < 0)
521 err(-1, "test_fork: waitpid");
528 test_vfork(uintmax_t num, uintmax_t int_arg, const char *path)
535 err(-1, "test_vfork: vfork");
538 if (waitpid(pid, NULL, 0) < 0)
539 err(-1, "test_vfork: waitpid");
541 for (i = 0; i < num; i++) {
546 err(-1, "test_vfork: vfork");
549 if (waitpid(pid, NULL, 0) < 0)
550 err(-1, "test_vfork: waitpid");
556 #define USR_BIN_TRUE "/usr/bin/true"
557 static char *execve_args[] = { USR_BIN_TRUE, NULL};
558 extern char **environ;
561 test_fork_exec(uintmax_t num, uintmax_t int_arg, const char *path)
568 err(-1, "test_fork_exec: fork");
570 (void)execve(USR_BIN_TRUE, execve_args, environ);
573 if (waitpid(pid, NULL, 0) < 0)
574 err(-1, "test_fork: waitpid");
576 for (i = 0; i < num; i++) {
581 err(-1, "test_fork_exec: fork");
583 (void)execve(USR_BIN_TRUE, execve_args, environ);
584 err(-1, "test_fork_exec: execve");
586 if (waitpid(pid, NULL, 0) < 0)
587 err(-1, "test_fork_exec: waitpid");
594 test_vfork_exec(uintmax_t num, uintmax_t int_arg, const char *path)
601 err(-1, "test_vfork_exec: vfork");
603 (void)execve(USR_BIN_TRUE, execve_args, environ);
604 err(-1, "test_vfork_exec: execve");
606 if (waitpid(pid, NULL, 0) < 0)
607 err(-1, "test_vfork_exec: waitpid");
609 for (i = 0; i < num; i++) {
614 err(-1, "test_vfork_exec: vfork");
616 (void)execve(USR_BIN_TRUE, execve_args, environ);
619 if (waitpid(pid, NULL, 0) < 0)
620 err(-1, "test_vfork_exec: waitpid");
627 test_chroot(uintmax_t num, uintmax_t int_arg, const char *path)
632 err(-1, "test_chroot: chroot");
634 for (i = 0; i < num; i++) {
638 err(-1, "test_chroot: chroot");
645 test_setuid(uintmax_t num, uintmax_t int_arg, const char *path)
652 err(-1, "test_setuid: setuid");
654 for (i = 0; i < num; i++) {
658 err(-1, "test_setuid: setuid");
666 uintmax_t (*t_func)(uintmax_t, uintmax_t, const char *);
671 #define FLAG_PATH 0x00000001
673 static const struct test tests[] = {
674 { "getuid", test_getuid },
675 { "getppid", test_getppid },
676 { "clock_gettime", test_clock_gettime },
677 { "gettimeofday", test_gettimeofday },
678 { "getpriority", test_getpriority },
679 { "pipe", test_pipe },
680 { "select", test_select },
681 { "socket_local_stream", test_socket_stream, .t_int = PF_LOCAL },
682 { "socket_local_dgram", test_socket_dgram, .t_int = PF_LOCAL },
683 { "socketpair_stream", test_socketpair_stream },
684 { "socketpair_dgram", test_socketpair_dgram },
685 { "socket_tcp", test_socket_stream, .t_int = PF_INET },
686 { "socket_udp", test_socket_dgram, .t_int = PF_INET },
687 { "create_unlink", test_create_unlink, .t_flags = FLAG_PATH },
688 { "open_close", test_open_close, .t_flags = FLAG_PATH },
689 { "open_read_close_1", test_open_read_close, .t_flags = FLAG_PATH,
691 { "open_read_close_10", test_open_read_close, .t_flags = FLAG_PATH,
693 { "open_read_close_100", test_open_read_close, .t_flags = FLAG_PATH,
695 { "open_read_close_1000", test_open_read_close, .t_flags = FLAG_PATH,
697 { "open_read_close_10000", test_open_read_close,
698 .t_flags = FLAG_PATH, .t_int = 10000 },
699 { "open_read_close_100000", test_open_read_close,
700 .t_flags = FLAG_PATH, .t_int = 100000 },
701 { "open_read_close_1000000", test_open_read_close,
702 .t_flags = FLAG_PATH, .t_int = 1000000 },
703 { "read_1", test_read, .t_flags = FLAG_PATH, .t_int = 1 },
704 { "read_10", test_read, .t_flags = FLAG_PATH, .t_int = 10 },
705 { "read_100", test_read, .t_flags = FLAG_PATH, .t_int = 100 },
706 { "read_1000", test_read, .t_flags = FLAG_PATH, .t_int = 1000 },
707 { "read_10000", test_read, .t_flags = FLAG_PATH, .t_int = 10000 },
708 { "read_100000", test_read, .t_flags = FLAG_PATH, .t_int = 100000 },
709 { "read_1000000", test_read, .t_flags = FLAG_PATH, .t_int = 1000000 },
711 { "shmfd", test_shmfd },
712 { "fstat_shmfd", test_fstat_shmfd },
713 { "fork", test_fork },
714 { "vfork", test_vfork },
715 { "fork_exec", test_fork_exec },
716 { "vfork_exec", test_vfork_exec },
717 { "chroot", test_chroot },
718 { "setuid", test_setuid },
720 static const int tests_count = sizeof(tests) / sizeof(tests[0]);
727 fprintf(stderr, "syscall_timing [-i iterations] [-l loops] "
728 "[-p path] [-s seconds] test\n");
729 for (i = 0; i < tests_count; i++)
730 fprintf(stderr, " %s\n", tests[i].t_name);
735 main(int argc, char *argv[])
737 struct timespec ts_res;
738 const struct test *the_test;
740 char *tmp_dir, *tmp_path;
743 int ch, fd, error, i, j, k, rv;
744 uintmax_t iterations, loops;
751 while ((ch = getopt(argc, argv, "i:l:p:s:")) != -1) {
754 ll = strtol(optarg, &endp, 10);
755 if (*endp != 0 || ll < 1 || ll > 100000)
761 ll = strtol(optarg, &endp, 10);
762 if (*endp != 0 || ll < 1 || ll > 100000)
772 ll = strtol(optarg, &endp, 10);
773 if (*endp != 0 || ll < 1 || ll > 60*60)
786 if (iterations < 1 && alarm_timeout < 1)
789 iterations = UINT64_MAX;
797 * Validate test list and that, if a path is required, it is
800 for (j = 0; j < argc; j++) {
802 for (i = 0; i < tests_count; i++) {
803 if (strcmp(argv[j], tests[i].t_name) == 0)
804 the_test = &tests[i];
806 if (the_test == NULL)
808 if ((the_test->t_flags & FLAG_PATH) && (path == NULL)) {
809 tmp_dir = strdup("/tmp/syscall_timing.XXXXXXXX");
812 tmp_dir = mkdtemp(tmp_dir);
815 rv = asprintf(&tmp_path, "%s/testfile", tmp_dir);
821 error = clock_getres(CLOCK_REALTIME, &ts_res);
823 printf("Clock resolution: %ju.%09ju\n", (uintmax_t)ts_res.tv_sec,
824 (uintmax_t)ts_res.tv_nsec);
825 printf("test\tloop\ttime\titerations\tperiteration\n");
827 for (j = 0; j < argc; j++) {
828 uintmax_t calls, nsecsperit;
831 for (i = 0; i < tests_count; i++) {
832 if (strcmp(argv[j], tests[i].t_name) == 0)
833 the_test = &tests[i];
836 if (tmp_path != NULL) {
837 fd = open(tmp_path, O_WRONLY | O_CREAT, 0700);
839 err(1, "cannot open %s", tmp_path);
840 error = ftruncate(fd, 1000000);
850 * Run one warmup, then do the real thing (loops) times.
852 the_test->t_func(iterations, the_test->t_int, path);
854 for (k = 0; k < loops; k++) {
855 calls = the_test->t_func(iterations, the_test->t_int,
857 timespecsub(&ts_end, &ts_start);
858 printf("%s\t%d\t", the_test->t_name, k);
859 printf("%ju.%09ju\t%ju\t", (uintmax_t)ts_end.tv_sec,
860 (uintmax_t)ts_end.tv_nsec, calls);
863 * Note. This assumes that each iteration takes less than
864 * a second, and that our total nanoseconds doesn't exceed
865 * the room in our arithmetic unit. Fine for system calls,
866 * but not for long things.
868 nsecsperit = ts_end.tv_sec * 1000000000;
869 nsecsperit += ts_end.tv_nsec;
871 printf("0.%09ju\n", (uintmax_t)nsecsperit);
875 if (tmp_path != NULL) {
876 error = unlink(tmp_path);
877 if (error != 0 && errno != ENOENT)
878 warn("cannot unlink %s", tmp_path);
879 error = rmdir(tmp_dir);
881 warn("cannot rmdir %s", tmp_dir);