i2c: qcom-geni: Provide support for ACPI
[linux/fpc-iii.git] / samples / bpf / test_overhead_user.c
blob9d6dcaa9db9206ebe6a5bb14fe98c7001a543b6d
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.
6 */
7 #define _GNU_SOURCE
8 #include <sched.h>
9 #include <errno.h>
10 #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 <bpf/bpf.h>
24 #include "bpf_load.h"
26 #define MAX_CNT 1000000
28 static __u64 time_get_ns(void)
30 struct timespec ts;
32 clock_gettime(CLOCK_MONOTONIC, &ts);
33 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
36 static void test_task_rename(int cpu)
38 __u64 start_time;
39 char buf[] = "test\n";
40 int i, fd;
42 fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
43 if (fd < 0) {
44 printf("couldn't open /proc\n");
45 exit(1);
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));
51 close(fd);
52 return;
55 printf("task_rename:%d: %lld events per sec\n",
56 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
57 close(fd);
60 static void test_urandom_read(int cpu)
62 __u64 start_time;
63 char buf[4];
64 int i, fd;
66 fd = open("/dev/urandom", O_RDONLY);
67 if (fd < 0) {
68 printf("couldn't open /dev/urandom\n");
69 exit(1);
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));
75 close(fd);
76 return;
79 printf("urandom_read:%d: %lld events per sec\n",
80 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
81 close(fd);
84 static void loop(int cpu, int flags)
86 cpu_set_t cpuset;
88 CPU_ZERO(&cpuset);
89 CPU_SET(cpu, &cpuset);
90 sched_setaffinity(0, sizeof(cpuset), &cpuset);
92 if (flags & 1)
93 test_task_rename(cpu);
94 if (flags & 2)
95 test_urandom_read(cpu);
98 static void run_perf_test(int tasks, int flags)
100 pid_t pid[tasks];
101 int i;
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);
113 for (i = 0; i < tasks; i++) {
114 int status;
116 assert(waitpid(pid[i], &status, 0) == pid[i]);
117 assert(status == 0);
121 static void unload_progs(void)
123 close(prog_fd[0]);
124 close(prog_fd[1]);
125 close(event_fd[0]);
126 close(event_fd[1]);
129 int main(int argc, char **argv)
131 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
132 char filename[256];
133 int num_cpu = 8;
134 int test_flags = ~0;
136 setrlimit(RLIMIT_MEMLOCK, &r);
138 if (argc > 1)
139 test_flags = atoi(argv[1]) ? : test_flags;
140 if (argc > 2)
141 num_cpu = atoi(argv[2]) ? : num_cpu;
143 if (test_flags & 0x3) {
144 printf("BASE\n");
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);
153 return 1;
155 printf("w/KPROBE\n");
156 run_perf_test(num_cpu, test_flags >> 2);
157 unload_progs();
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;
167 printf("w/TRACEPOINT\n");
168 run_perf_test(num_cpu, test_flags >> 4);
169 unload_progs();
172 if (test_flags & 0xC0) {
173 snprintf(filename, sizeof(filename),
174 "%s_raw_tp_kern.o", argv[0]);
175 if (load_bpf_file(filename)) {
176 printf("%s", bpf_log_buf);
177 return 1;
179 printf("w/RAW_TRACEPOINT\n");
180 run_perf_test(num_cpu, test_flags >> 6);
181 unload_progs();
184 return 0;