1 #include <stdio.h>
2 #include <unistd.h>
3 #include <linux/bpf.h>
4 #include <asm/unistd.h>
5 #include <fcntl.h>
6 #include <string.h>
7
8
sys_bpf(enum bpf_cmd cmd,union bpf_attr * attr,unsigned int size)9 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
10 unsigned int size)
11 {
12 return syscall(__NR_bpf, cmd, attr, size);
13 }
ptr_to_u64(const void * ptr)14 static inline __u64 ptr_to_u64(const void *ptr)
15 {
16 return (__u64) (unsigned long) ptr;
17 }
18
19
get_root_fd()20 int get_root_fd() {
21 union bpf_attr attr_pin;
22 int fd, rc;
23 memset(&attr_pin, 0, sizeof(attr_pin));
24 attr_pin.pathname = ptr_to_u64("/sys/fs/bpf/ksysread_stats");
25 fd = sys_bpf(BPF_OBJ_GET, &attr_pin, sizeof(attr_pin));
26 return fd;
27 }
28
29
main(int argc,char * argv[])30 int main(int argc, char *argv[]) {
31 unsigned int vip, ip;
32 int i, rc, fd;
33 union bpf_attr attr_elem;
34 unsigned int key;
35 unsigned long long value, b=1;
36 int root_fd = get_root_fd();
37 if (root_fd < 0) {
38 perror("fail to open map\n");
39 return -1;
40 }
41 memset(&attr_elem, 0, sizeof(attr_elem));
42 attr_elem.flags = BPF_ANY;
43 attr_elem.map_fd = root_fd;
44 attr_elem.key = ptr_to_u64(&key);
45 attr_elem.value = ptr_to_u64(&value);
46 for (key=0; key<32; key++) {
47 rc = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr_elem, sizeof(attr_elem));
48 if (rc!=0) {
49 printf("fail to get read stats %d\n", key);
50 return 0;
51 }
52 // if (value==0) continue;
53 if (b>=1024*1024) printf("%6lldMB: %-9lld\n", b/1024/1024, value);
54 else if (b>=1024) printf("%6lldKB: %-9lld\n", b/1024, value);
55 else printf("%7lldB: %-9lld\n", b, value);
56 b<<=1;
57 }
58 return 0;
59 }
60