aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/tests/bpf-script-example.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/tests/bpf-script-example.c')
-rw-r--r--tools/perf/tests/bpf-script-example.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c
new file mode 100644
index 000000000000..410a70b93b93
--- /dev/null
+++ b/tools/perf/tests/bpf-script-example.c
@@ -0,0 +1,44 @@
1#ifndef LINUX_VERSION_CODE
2# error Need LINUX_VERSION_CODE
3# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
4#endif
5#define BPF_ANY 0
6#define BPF_MAP_TYPE_ARRAY 2
7#define BPF_FUNC_map_lookup_elem 1
8#define BPF_FUNC_map_update_elem 2
9
10static void *(*bpf_map_lookup_elem)(void *map, void *key) =
11 (void *) BPF_FUNC_map_lookup_elem;
12static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
13 (void *) BPF_FUNC_map_update_elem;
14
15struct bpf_map_def {
16 unsigned int type;
17 unsigned int key_size;
18 unsigned int value_size;
19 unsigned int max_entries;
20};
21
22#define SEC(NAME) __attribute__((section(NAME), used))
23struct bpf_map_def SEC("maps") flip_table = {
24 .type = BPF_MAP_TYPE_ARRAY,
25 .key_size = sizeof(int),
26 .value_size = sizeof(int),
27 .max_entries = 1,
28};
29
30SEC("func=sys_epoll_pwait")
31int bpf_func__sys_epoll_pwait(void *ctx)
32{
33 int ind =0;
34 int *flag = bpf_map_lookup_elem(&flip_table, &ind);
35 int new_flag;
36 if (!flag)
37 return 0;
38 /* flip flag and store back */
39 new_flag = !*flag;
40 bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
41 return new_flag;
42}
43char _license[] SEC("license") = "GPL";
44int _version SEC("version") = LINUX_VERSION_CODE;