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.
11 #include <sys/types.h>
12 #include <asm/unistd.h>
19 #include <linux/bpf.h>
22 #include <sys/resource.h>
26 #define MAX_CNT 1000000
28 static __u64 time_get_ns(void)
32 clock_gettime(CLOCK_MONOTONIC, &ts);
33 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
36 static void test_task_rename(int cpu)
39 char buf[] = "test\n";
42 fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
44 printf("couldn't open /proc\n");
47 start_time = time_get_ns();
48 for (i = 0; i < MAX_CNT; i++) {
49 if (write(fd, buf, sizeof(buf)) < 0) {
50 printf("task rename failed: %s\n", strerror(errno));
55 printf("task_rename:%d: %lld events per sec\n",
56 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
60 static void test_urandom_read(int cpu)
66 fd = open("/dev/urandom", O_RDONLY);
68 printf("couldn't open /dev/urandom\n");
71 start_time = time_get_ns();
72 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));
79 printf("urandom_read:%d: %lld events per sec\n",
80 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
84 static void loop(int cpu, int flags)
89 CPU_SET(cpu, &cpuset);
90 sched_setaffinity(0, sizeof(cpuset), &cpuset);
93 test_task_rename(cpu);
95 test_urandom_read(cpu);
98 static void run_perf_test(int tasks, int flags)
103 for (i = 0; i < tasks; i++) {
108 } else if (pid[i] == -1) {
109 printf("couldn't spawn #%d process\n", i);
113 for (i = 0; i < tasks; i++) {
116 assert(waitpid(pid[i], &status, 0) == pid[i]);
121 static void unload_progs(void)
129 int main(int argc, char **argv)
131 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
136 setrlimit(RLIMIT_MEMLOCK, &r);
139 test_flags = atoi(argv[1]) ? : test_flags;
141 num_cpu = atoi(argv[2]) ? : num_cpu;
143 if (test_flags & 0x3) {
145 run_perf_test(num_cpu, test_flags);
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);
155 printf("w/KPROBE\n");
156 run_perf_test(num_cpu, test_flags >> 2);
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);
167 printf("w/TRACEPOINT\n");
168 run_perf_test(num_cpu, test_flags >> 4);