summaryrefslogtreecommitdiff
path: root/tools/perf/bench/syscall.c
blob: fe79f7f3091e1e5c7ea89e930bb4f049516ad3a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 *
 * syscall.c
 *
 * syscall: Benchmark for system call performance
 */
#include "../perf.h"
#include "../util/util.h"
#include <subcmd/parse-options.h>
#include "../builtin.h"
#include "bench.h"

#include <stdio.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

#define LOOPS_DEFAULT 10000000
static	int loops = LOOPS_DEFAULT;

static const struct option options[] = {
	OPT_INTEGER('l', "loop",	&loops,		"Specify number of loops"),
	OPT_END()
};

static const char * const bench_syscall_usage[] = {
	"perf bench syscall <options>",
	NULL
};

static void test_execve(void)
{
	const char *pathname = "/bin/true";
	char *const argv[] = { (char *)pathname, NULL };
	pid_t pid = fork();

	if (pid < 0) {
		fprintf(stderr, "fork failed\n");
		exit(1);
	} else if (pid == 0) {
		execve(pathname, argv, NULL);
		fprintf(stderr, "execve /bin/true failed\n");
		exit(1);
	} else {
		if (waitpid(pid, NULL, 0) < 0) {
			fprintf(stderr, "waitpid failed\n");
			exit(1);
		}
	}
}

static int bench_syscall_common(int argc, const char **argv, int syscall)
{
	struct timeval start, stop, diff;
	unsigned long long result_usec = 0;
	const char *name = NULL;
	int i;

	argc = parse_options(argc, argv, options, bench_syscall_usage, 0);

	gettimeofday(&start, NULL);

	for (i = 0; i < loops; i++) {
		switch (syscall) {
		case __NR_getppid:
			getppid();
			break;
		case __NR_getpgid:
			getpgid(0);
			break;
		case __NR_execve:
			test_execve();
			/* Only loop 10000 times to save time */
			if (i == 10000)
				loops = 10000;
			break;
		default:
			break;
		}
	}

	gettimeofday(&stop, NULL);
	timersub(&stop, &start, &diff);

	switch (syscall) {
	case __NR_getppid:
		name = "getppid()";
		break;
	case __NR_getpgid:
		name = "getpgid()";
		break;
	case __NR_execve:
		name = "execve()";
		break;
	default:
		break;
	}

	switch (bench_format) {
	case BENCH_FORMAT_DEFAULT:
		printf("# Executed %'d %s calls\n", loops, name);

		result_usec = diff.tv_sec * 1000000;
		result_usec += diff.tv_usec;

		printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
		       (unsigned long) diff.tv_sec,
		       (unsigned long) (diff.tv_usec/1000));

		printf(" %14lf usecs/op\n",
		       (double)result_usec / (double)loops);
		printf(" %'14d ops/sec\n",
		       (int)((double)loops /
			     ((double)result_usec / (double)1000000)));
		break;

	case BENCH_FORMAT_SIMPLE:
		printf("%lu.%03lu\n",
		       (unsigned long) diff.tv_sec,
		       (unsigned long) (diff.tv_usec / 1000));
		break;

	default:
		/* reaching here is something disaster */
		fprintf(stderr, "Unknown format:%d\n", bench_format);
		exit(1);
		break;
	}

	return 0;
}

int bench_syscall_basic(int argc, const char **argv)
{
	return bench_syscall_common(argc, argv, __NR_getppid);
}

int bench_syscall_getpgid(int argc, const char **argv)
{
	return bench_syscall_common(argc, argv, __NR_getpgid);
}

int bench_syscall_execve(int argc, const char **argv)
{
	return bench_syscall_common(argc, argv, __NR_execve);
}