2 * Copyright (c) 2003-2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
39 #define timespecsub(vvp, uvp) \
41 (vvp)->tv_sec -= (uvp)->tv_sec; \
42 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
43 if ((vvp)->tv_nsec < 0) { \
45 (vvp)->tv_nsec += 1000000000; \
55 * Thread-local data should require no locking if system
58 for (i = 0; i < num; i++)
68 * This is process-local, but can change, so will require a
71 for (i = 0; i < num; i++)
76 test_clock_gettime(int num)
81 for (i = 0; i < num; i++) {
82 if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
83 perror("clock_gettime");
95 * pipe creation is expensive, as it will allocate a new file
96 * descriptor, allocate a new pipe, hook it all up, and return.
97 * Destroying is also expensive, as we now have to free up
98 * the file descriptors and return the pipe.
100 for (i = 0; i < num; i++) {
102 if (pipe(fd) == -1) {
118 * Sockets are also expensive, but unlike pipes, currently
121 for (i = 0; i < num; i++) {
122 so = socket(PF_LOCAL, SOCK_STREAM, 0);
135 fprintf(stderr, "syscall_timing [iterations] [test]\n");
136 fprintf(stderr, "supported tests: getuid getppid clock_gettime "
142 main(int argc, char *argv[])
144 struct timespec ts_start, ts_end, ts_res;
149 count = atoi(argv[1]);
151 assert(clock_getres(CLOCK_REALTIME, &ts_res) == 0);
152 printf("Clock resolution: %d.%09lu\n", ts_res.tv_sec, ts_res.tv_nsec);
154 if (strcmp(argv[2], "getuid") == 0) {
155 assert(clock_gettime(CLOCK_REALTIME, &ts_start) == 0);
157 assert(clock_gettime(CLOCK_REALTIME, &ts_end) == 0);
158 } else if (strcmp(argv[2], "getppid") == 0) {
159 assert(clock_gettime(CLOCK_REALTIME, &ts_start) == 0);
161 assert(clock_gettime(CLOCK_REALTIME, &ts_end) == 0);
162 } else if (strcmp(argv[2], "clock_gettime") == 0) {
163 assert(clock_gettime(CLOCK_REALTIME, &ts_start) == 0);
164 test_clock_gettime(count);
165 assert(clock_gettime(CLOCK_REALTIME, &ts_end) == 0);
166 } else if (strcmp(argv[2], "pipe") == 0) {
167 assert(clock_gettime(CLOCK_REALTIME, &ts_start) == 0);
169 assert(clock_gettime(CLOCK_REALTIME, &ts_end) == 0);
170 } else if (strcmp(argv[2], "socket") == 0) {
171 assert(clock_gettime(CLOCK_REALTIME, &ts_start) == 0);
173 assert(clock_gettime(CLOCK_REALTIME, &ts_end) == 0);
177 timespecsub(&ts_end, &ts_start);
179 printf("test: %s\n", argv[2]);
181 printf("%d.%09lu for %d iterations\n", ts_end.tv_sec,
182 ts_end.tv_nsec, count);
185 * Note. This assumes that each iteration takes less than
186 * a second, and that our total nanoseconds doesn't exceed
187 * the room in our arithmetic unit. Fine for system calls,
188 * but not for long things.
190 ts_end.tv_sec *= 1000000000 / count;
191 printf("0.%09lu per/iteration\n",
192 ts_end.tv_sec + ts_end.tv_nsec / count);