blob: 7dad9a3168e1e5a05dc96aea65ed45f457a3a08b [file] [log] [blame]
Alexei Starovoitove3edfde2016-04-06 18:43:31 -07001/* Copyright (c) 2016 Facebook
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#define _GNU_SOURCE
8#include <sched.h>
Taeung Songfbfd7532018-07-04 22:36:38 +09009#include <errno.h>
Alexei Starovoitove3edfde2016-04-06 18:43:31 -070010#include <stdio.h>
11#include <sys/types.h>
12#include <asm/unistd.h>
13#include <fcntl.h>
14#include <unistd.h>
15#include <assert.h>
16#include <sys/wait.h>
17#include <stdlib.h>
18#include <signal.h>
19#include <linux/bpf.h>
20#include <string.h>
21#include <time.h>
22#include <sys/resource.h>
23#include "libbpf.h"
24#include "bpf_load.h"
25
26#define MAX_CNT 1000000
27
28static __u64 time_get_ns(void)
29{
30 struct timespec ts;
31
32 clock_gettime(CLOCK_MONOTONIC, &ts);
33 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
34}
35
36static void test_task_rename(int cpu)
37{
38 __u64 start_time;
39 char buf[] = "test\n";
40 int i, fd;
41
42 fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
43 if (fd < 0) {
44 printf("couldn't open /proc\n");
45 exit(1);
46 }
47 start_time = time_get_ns();
Taeung Songfbfd7532018-07-04 22:36:38 +090048 for (i = 0; i < MAX_CNT; i++) {
49 if (write(fd, buf, sizeof(buf)) < 0) {
50 printf("task rename failed: %s\n", strerror(errno));
51 close(fd);
52 return;
53 }
54 }
Alexei Starovoitove3edfde2016-04-06 18:43:31 -070055 printf("task_rename:%d: %lld events per sec\n",
56 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
57 close(fd);
58}
59
60static void test_urandom_read(int cpu)
61{
62 __u64 start_time;
63 char buf[4];
64 int i, fd;
65
66 fd = open("/dev/urandom", O_RDONLY);
67 if (fd < 0) {
68 printf("couldn't open /dev/urandom\n");
69 exit(1);
70 }
71 start_time = time_get_ns();
Taeung Songfbfd7532018-07-04 22:36:38 +090072 for (i = 0; i < MAX_CNT; i++) {
73 if (read(fd, buf, sizeof(buf)) < 0) {
74 printf("failed to read from /dev/urandom: %s\n", strerror(errno));
75 close(fd);
76 return;
77 }
78 }
Alexei Starovoitove3edfde2016-04-06 18:43:31 -070079 printf("urandom_read:%d: %lld events per sec\n",
80 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
81 close(fd);
82}
83
84static void loop(int cpu, int flags)
85{
86 cpu_set_t cpuset;
87
88 CPU_ZERO(&cpuset);
89 CPU_SET(cpu, &cpuset);
90 sched_setaffinity(0, sizeof(cpuset), &cpuset);
91
92 if (flags & 1)
93 test_task_rename(cpu);
94 if (flags & 2)
95 test_urandom_read(cpu);
96}
97
98static void run_perf_test(int tasks, int flags)
99{
100 pid_t pid[tasks];
101 int i;
102
103 for (i = 0; i < tasks; i++) {
104 pid[i] = fork();
105 if (pid[i] == 0) {
106 loop(i, flags);
107 exit(0);
108 } else if (pid[i] == -1) {
109 printf("couldn't spawn #%d process\n", i);
110 exit(1);
111 }
112 }
113 for (i = 0; i < tasks; i++) {
114 int status;
115
116 assert(waitpid(pid[i], &status, 0) == pid[i]);
117 assert(status == 0);
118 }
119}
120
121static void unload_progs(void)
122{
123 close(prog_fd[0]);
124 close(prog_fd[1]);
125 close(event_fd[0]);
126 close(event_fd[1]);
127}
128
129int main(int argc, char **argv)
130{
131 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
132 char filename[256];
133 int num_cpu = 8;
134 int test_flags = ~0;
135
136 setrlimit(RLIMIT_MEMLOCK, &r);
137
138 if (argc > 1)
139 test_flags = atoi(argv[1]) ? : test_flags;
140 if (argc > 2)
141 num_cpu = atoi(argv[2]) ? : num_cpu;
142
143 if (test_flags & 0x3) {
144 printf("BASE\n");
145 run_perf_test(num_cpu, test_flags);
146 }
147
148 if (test_flags & 0xC) {
149 snprintf(filename, sizeof(filename),
150 "%s_kprobe_kern.o", argv[0]);
151 if (load_bpf_file(filename)) {
152 printf("%s", bpf_log_buf);
153 return 1;
154 }
155 printf("w/KPROBE\n");
156 run_perf_test(num_cpu, test_flags >> 2);
157 unload_progs();
158 }
159
160 if (test_flags & 0x30) {
161 snprintf(filename, sizeof(filename),
162 "%s_tp_kern.o", argv[0]);
163 if (load_bpf_file(filename)) {
164 printf("%s", bpf_log_buf);
165 return 1;
166 }
167 printf("w/TRACEPOINT\n");
168 run_perf_test(num_cpu, test_flags >> 4);
169 unload_progs();
170 }
171
172 return 0;
173}