Davidlohr Bueso | c2a0820 | 2019-03-08 10:17:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * syscall.c |
| 4 | * |
| 5 | * syscall: Benchmark for system call performance |
| 6 | */ |
| 7 | #include "../perf.h" |
| 8 | #include "../util/util.h" |
| 9 | #include <subcmd/parse-options.h> |
| 10 | #include "../builtin.h" |
| 11 | #include "bench.h" |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <sys/time.h> |
| 15 | #include <sys/syscall.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <unistd.h> |
| 18 | #include <stdlib.h> |
| 19 | |
| 20 | #define LOOPS_DEFAULT 10000000 |
| 21 | static int loops = LOOPS_DEFAULT; |
| 22 | |
| 23 | static const struct option options[] = { |
| 24 | OPT_INTEGER('l', "loop", &loops, "Specify number of loops"), |
| 25 | OPT_END() |
| 26 | }; |
| 27 | |
| 28 | static const char * const bench_syscall_usage[] = { |
| 29 | "perf bench syscall <options>", |
| 30 | NULL |
| 31 | }; |
| 32 | |
| 33 | int bench_syscall_basic(int argc, const char **argv) |
| 34 | { |
| 35 | struct timeval start, stop, diff; |
| 36 | unsigned long long result_usec = 0; |
| 37 | int i; |
| 38 | |
| 39 | argc = parse_options(argc, argv, options, bench_syscall_usage, 0); |
| 40 | |
| 41 | gettimeofday(&start, NULL); |
| 42 | |
| 43 | for (i = 0; i < loops; i++) |
| 44 | getppid(); |
| 45 | |
| 46 | gettimeofday(&stop, NULL); |
| 47 | timersub(&stop, &start, &diff); |
| 48 | |
| 49 | switch (bench_format) { |
| 50 | case BENCH_FORMAT_DEFAULT: |
| 51 | printf("# Executed %'d getppid() calls\n", loops); |
| 52 | |
| 53 | result_usec = diff.tv_sec * 1000000; |
| 54 | result_usec += diff.tv_usec; |
| 55 | |
| 56 | printf(" %14s: %lu.%03lu [sec]\n\n", "Total time", |
Pierre Gondois | ded2e51 | 2021-02-24 18:24:10 +0000 | [diff] [blame] | 57 | (unsigned long) diff.tv_sec, |
Davidlohr Bueso | c2a0820 | 2019-03-08 10:17:47 -0800 | [diff] [blame] | 58 | (unsigned long) (diff.tv_usec/1000)); |
| 59 | |
| 60 | printf(" %14lf usecs/op\n", |
| 61 | (double)result_usec / (double)loops); |
| 62 | printf(" %'14d ops/sec\n", |
| 63 | (int)((double)loops / |
| 64 | ((double)result_usec / (double)1000000))); |
| 65 | break; |
| 66 | |
| 67 | case BENCH_FORMAT_SIMPLE: |
| 68 | printf("%lu.%03lu\n", |
Pierre Gondois | ded2e51 | 2021-02-24 18:24:10 +0000 | [diff] [blame] | 69 | (unsigned long) diff.tv_sec, |
Davidlohr Bueso | c2a0820 | 2019-03-08 10:17:47 -0800 | [diff] [blame] | 70 | (unsigned long) (diff.tv_usec / 1000)); |
| 71 | break; |
| 72 | |
| 73 | default: |
| 74 | /* reaching here is something disaster */ |
| 75 | fprintf(stderr, "Unknown format:%d\n", bench_format); |
| 76 | exit(1); |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |