5 * syscall: Benchmark for system call performance
8 #include "../util/util.h"
9 #include <subcmd/parse-options.h>
10 #include "../builtin.h"
15 #include <sys/syscall.h>
16 #include <sys/types.h>
25 #define LOOPS_DEFAULT 10000000
26 static int loops
= LOOPS_DEFAULT
;
28 static const struct option options
[] = {
29 OPT_INTEGER('l', "loop", &loops
, "Specify number of loops"),
33 static const char * const bench_syscall_usage
[] = {
34 "perf bench syscall <options>",
38 static void test_fork(void)
43 fprintf(stderr
, "fork failed\n");
45 } else if (pid
== 0) {
48 if (waitpid(pid
, NULL
, 0) < 0) {
49 fprintf(stderr
, "waitpid failed\n");
55 static void test_execve(void)
57 const char *pathname
= "/bin/true";
58 char *const argv
[] = { (char *)pathname
, NULL
};
62 fprintf(stderr
, "fork failed\n");
64 } else if (pid
== 0) {
65 execve(pathname
, argv
, NULL
);
66 fprintf(stderr
, "execve /bin/true failed\n");
69 if (waitpid(pid
, NULL
, 0) < 0) {
70 fprintf(stderr
, "waitpid failed\n");
76 static int bench_syscall_common(int argc
, const char **argv
, int syscall
)
78 struct timeval start
, stop
, diff
;
79 unsigned long long result_usec
= 0;
80 const char *name
= NULL
;
83 argc
= parse_options(argc
, argv
, options
, bench_syscall_usage
, 0);
85 gettimeofday(&start
, NULL
);
87 for (i
= 0; i
< loops
; i
++) {
97 /* Only loop 10000 times to save time */
103 /* Only loop 10000 times to save time */
112 gettimeofday(&stop
, NULL
);
113 timersub(&stop
, &start
, &diff
);
132 switch (bench_format
) {
133 case BENCH_FORMAT_DEFAULT
:
134 printf("# Executed %'d %s calls\n", loops
, name
);
136 result_usec
= diff
.tv_sec
* 1000000;
137 result_usec
+= diff
.tv_usec
;
139 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
140 (unsigned long) diff
.tv_sec
,
141 (unsigned long) (diff
.tv_usec
/1000));
143 printf(" %14lf usecs/op\n",
144 (double)result_usec
/ (double)loops
);
145 printf(" %'14d ops/sec\n",
146 (int)((double)loops
/
147 ((double)result_usec
/ (double)1000000)));
150 case BENCH_FORMAT_SIMPLE
:
151 printf("%lu.%03lu\n",
152 (unsigned long) diff
.tv_sec
,
153 (unsigned long) (diff
.tv_usec
/ 1000));
157 /* reaching here is something disaster */
158 fprintf(stderr
, "Unknown format:%d\n", bench_format
);
166 int bench_syscall_basic(int argc
, const char **argv
)
168 return bench_syscall_common(argc
, argv
, __NR_getppid
);
171 int bench_syscall_getpgid(int argc
, const char **argv
)
173 return bench_syscall_common(argc
, argv
, __NR_getpgid
);
176 int bench_syscall_fork(int argc
, const char **argv
)
178 return bench_syscall_common(argc
, argv
, __NR_fork
);
181 int bench_syscall_execve(int argc
, const char **argv
)
183 return bench_syscall_common(argc
, argv
, __NR_execve
);