]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - samples/bpf/tracex2_user.c
Merge remote-tracking branch 'regulator/fix/max77802' into regulator-linus
[mirror_ubuntu-artful-kernel.git] / samples / bpf / tracex2_user.c
CommitLineData
d822a192
AS
1#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <signal.h>
5#include <linux/bpf.h>
ffeedafb 6#include <string.h>
55de1703 7#include <sys/resource.h>
e00c7b21 8
d822a192
AS
9#include "libbpf.h"
10#include "bpf_load.h"
e00c7b21 11#include "bpf_util.h"
d822a192
AS
12
13#define MAX_INDEX 64
14#define MAX_STARS 38
15
16static void stars(char *str, long val, long max, int width)
17{
18 int i;
19
20 for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
21 str[i] = '*';
22 if (val > max)
23 str[i - 1] = '+';
24 str[i] = '\0';
25}
26
ffeedafb
AS
27struct task {
28 char comm[16];
29 __u64 pid_tgid;
30 __u64 uid_gid;
31};
32
33struct hist_key {
34 struct task t;
35 __u32 index;
36};
37
38#define SIZE sizeof(struct task)
39
40static void print_hist_for_pid(int fd, void *task)
d822a192 41{
e00c7b21 42 unsigned int nr_cpus = bpf_num_possible_cpus();
ffeedafb 43 struct hist_key key = {}, next_key;
3059303f 44 long values[nr_cpus];
ffeedafb 45 char starstr[MAX_STARS];
d822a192
AS
46 long value;
47 long data[MAX_INDEX] = {};
d822a192
AS
48 int max_ind = -1;
49 long max_value = 0;
ffeedafb 50 int i, ind;
d822a192 51
d40fc181 52 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
ffeedafb
AS
53 if (memcmp(&next_key, task, SIZE)) {
54 key = next_key;
55 continue;
56 }
d40fc181 57 bpf_map_lookup_elem(fd, &next_key, values);
3059303f
AS
58 value = 0;
59 for (i = 0; i < nr_cpus; i++)
60 value += values[i];
ffeedafb
AS
61 ind = next_key.index;
62 data[ind] = value;
63 if (value && ind > max_ind)
64 max_ind = ind;
d822a192
AS
65 if (value > max_value)
66 max_value = value;
ffeedafb 67 key = next_key;
d822a192
AS
68 }
69
70 printf(" syscall write() stats\n");
71 printf(" byte_size : count distribution\n");
72 for (i = 1; i <= max_ind + 1; i++) {
73 stars(starstr, data[i - 1], max_value, MAX_STARS);
74 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
75 (1l << i) >> 1, (1l << i) - 1, data[i - 1],
76 MAX_STARS, starstr);
77 }
78}
ffeedafb
AS
79
80static void print_hist(int fd)
81{
82 struct hist_key key = {}, next_key;
83 static struct task tasks[1024];
84 int task_cnt = 0;
85 int i;
86
d40fc181 87 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
ffeedafb
AS
88 int found = 0;
89
90 for (i = 0; i < task_cnt; i++)
91 if (memcmp(&tasks[i], &next_key, SIZE) == 0)
92 found = 1;
93 if (!found)
94 memcpy(&tasks[task_cnt++], &next_key, SIZE);
95 key = next_key;
96 }
97
98 for (i = 0; i < task_cnt; i++) {
99 printf("\npid %d cmd %s uid %d\n",
100 (__u32) tasks[i].pid_tgid,
101 tasks[i].comm,
102 (__u32) tasks[i].uid_gid);
103 print_hist_for_pid(fd, &tasks[i]);
104 }
105
106}
107
d822a192
AS
108static void int_exit(int sig)
109{
110 print_hist(map_fd[1]);
111 exit(0);
112}
113
114int main(int ac, char **argv)
115{
55de1703 116 struct rlimit r = {1024*1024, RLIM_INFINITY};
d822a192
AS
117 char filename[256];
118 long key, next_key, value;
119 FILE *f;
120 int i;
121
122 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
123
55de1703
JDB
124 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
125 perror("setrlimit(RLIMIT_MEMLOCK)");
126 return 1;
127 }
128
d822a192 129 signal(SIGINT, int_exit);
ad990dbe 130 signal(SIGTERM, int_exit);
d822a192
AS
131
132 /* start 'ping' in the background to have some kfree_skb events */
133 f = popen("ping -c5 localhost", "r");
134 (void) f;
135
136 /* start 'dd' in the background to have plenty of 'write' syscalls */
137 f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
138 (void) f;
139
140 if (load_bpf_file(filename)) {
141 printf("%s", bpf_log_buf);
142 return 1;
143 }
144
145 for (i = 0; i < 5; i++) {
146 key = 0;
d40fc181
JS
147 while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
148 bpf_map_lookup_elem(map_fd[0], &next_key, &value);
d822a192
AS
149 printf("location 0x%lx count %ld\n", next_key, value);
150 key = next_key;
151 }
152 if (key)
153 printf("\n");
154 sleep(1);
155 }
156 print_hist(map_fd[1]);
157
158 return 0;
159}