1 /* Copyright (c) 2016 Facebook
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.
10 #include <sys/types.h>
11 #include <asm/unistd.h>
18 #include <linux/bpf.h>
21 #include <sys/resource.h>
25 #define MAX_CNT 1000000
27 static __u64 time_get_ns(void)
31 clock_gettime(CLOCK_MONOTONIC, &ts);
32 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
35 static void test_task_rename(int cpu)
38 char buf[] = "test\n";
41 fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
43 printf("couldn't open /proc\n");
46 start_time = time_get_ns();
47 for (i = 0; i < MAX_CNT; i++)
48 write(fd, buf, sizeof(buf));
49 printf("task_rename:%d: %lld events per sec\n",
50 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
54 static void test_urandom_read(int cpu)
60 fd = open("/dev/urandom", O_RDONLY);
62 printf("couldn't open /dev/urandom\n");
65 start_time = time_get_ns();
66 for (i = 0; i < MAX_CNT; i++)
67 read(fd, buf, sizeof(buf));
68 printf("urandom_read:%d: %lld events per sec\n",
69 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
73 static void loop(int cpu, int flags)
78 CPU_SET(cpu, &cpuset);
79 sched_setaffinity(0, sizeof(cpuset), &cpuset);
82 test_task_rename(cpu);
84 test_urandom_read(cpu);
87 static void run_perf_test(int tasks, int flags)
92 for (i = 0; i < tasks; i++) {
97 } else if (pid[i] == -1) {
98 printf("couldn't spawn #%d process\n", i);
102 for (i = 0; i < tasks; i++) {
105 assert(waitpid(pid[i], &status, 0) == pid[i]);
110 static void unload_progs(void)
118 int main(int argc, char **argv)
120 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
125 setrlimit(RLIMIT_MEMLOCK, &r);
128 test_flags = atoi(argv[1]) ? : test_flags;
130 num_cpu = atoi(argv[2]) ? : num_cpu;
132 if (test_flags & 0x3) {
134 run_perf_test(num_cpu, test_flags);
137 if (test_flags & 0xC) {
138 snprintf(filename, sizeof(filename),
139 "%s_kprobe_kern.o", argv[0]);
140 if (load_bpf_file(filename)) {
141 printf("%s", bpf_log_buf);
144 printf("w/KPROBE\n");
145 run_perf_test(num_cpu, test_flags >> 2);
149 if (test_flags & 0x30) {
150 snprintf(filename, sizeof(filename),
151 "%s_tp_kern.o", argv[0]);
152 if (load_bpf_file(filename)) {
153 printf("%s", bpf_log_buf);
156 printf("w/TRACEPOINT\n");
157 run_perf_test(num_cpu, test_flags >> 4);
161 if (test_flags & 0xC0) {
162 snprintf(filename, sizeof(filename),
163 "%s_raw_tp_kern.o", argv[0]);
164 if (load_bpf_file(filename)) {
165 printf("%s", bpf_log_buf);
168 printf("w/RAW_TRACEPOINT\n");
169 run_perf_test(num_cpu, test_flags >> 6);