]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - tools/perf/tests/bpf-script-example.c
Merge branch 'ras-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[karo-tx-linux.git] / tools / perf / tests / bpf-script-example.c
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
10 static void *(*bpf_map_lookup_elem)(void *map, void *key) =
11         (void *) BPF_FUNC_map_lookup_elem;
12 static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
13         (void *) BPF_FUNC_map_update_elem;
14
15 struct 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))
23 struct 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
30 SEC("func=sys_epoll_pwait")
31 int 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 }
43 char _license[] SEC("license") = "GPL";
44 int _version SEC("version") = LINUX_VERSION_CODE;