blob: 2131f1648cf16dd5c49e0496fb9dcb9d62ed132e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Alexei Starovoitovd822a192015-03-25 12:49:24 -07002#include <stdio.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <signal.h>
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07006#include <string.h>
Daniel Borkmanne00c7b22016-11-26 01:28:09 +01007
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -07008#include <bpf/bpf.h>
Daniel T. Lee63841bc2020-05-16 13:06:05 +09009#include <bpf/libbpf.h>
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010010#include "bpf_util.h"
Alexei Starovoitovd822a192015-03-25 12:49:24 -070011
12#define MAX_INDEX 64
13#define MAX_STARS 38
14
Daniel T. Lee63841bc2020-05-16 13:06:05 +090015/* my_map, my_hist_map */
16static int map_fd[2];
17
Alexei Starovoitovd822a192015-03-25 12:49:24 -070018static void stars(char *str, long val, long max, int width)
19{
20 int i;
21
22 for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
23 str[i] = '*';
24 if (val > max)
25 str[i - 1] = '+';
26 str[i] = '\0';
27}
28
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070029struct task {
30 char comm[16];
31 __u64 pid_tgid;
32 __u64 uid_gid;
33};
34
35struct hist_key {
36 struct task t;
37 __u32 index;
38};
39
40#define SIZE sizeof(struct task)
41
42static void print_hist_for_pid(int fd, void *task)
Alexei Starovoitovd822a192015-03-25 12:49:24 -070043{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010044 unsigned int nr_cpus = bpf_num_possible_cpus();
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070045 struct hist_key key = {}, next_key;
Alexei Starovoitov30593032016-02-01 22:39:58 -080046 long values[nr_cpus];
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070047 char starstr[MAX_STARS];
Alexei Starovoitovd822a192015-03-25 12:49:24 -070048 long value;
49 long data[MAX_INDEX] = {};
Alexei Starovoitovd822a192015-03-25 12:49:24 -070050 int max_ind = -1;
51 long max_value = 0;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070052 int i, ind;
Alexei Starovoitovd822a192015-03-25 12:49:24 -070053
Joe Stringerd40fc182016-12-14 14:43:38 -080054 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070055 if (memcmp(&next_key, task, SIZE)) {
56 key = next_key;
57 continue;
58 }
Joe Stringerd40fc182016-12-14 14:43:38 -080059 bpf_map_lookup_elem(fd, &next_key, values);
Alexei Starovoitov30593032016-02-01 22:39:58 -080060 value = 0;
61 for (i = 0; i < nr_cpus; i++)
62 value += values[i];
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070063 ind = next_key.index;
64 data[ind] = value;
65 if (value && ind > max_ind)
66 max_ind = ind;
Alexei Starovoitovd822a192015-03-25 12:49:24 -070067 if (value > max_value)
68 max_value = value;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070069 key = next_key;
Alexei Starovoitovd822a192015-03-25 12:49:24 -070070 }
71
72 printf(" syscall write() stats\n");
73 printf(" byte_size : count distribution\n");
74 for (i = 1; i <= max_ind + 1; i++) {
75 stars(starstr, data[i - 1], max_value, MAX_STARS);
76 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
77 (1l << i) >> 1, (1l << i) - 1, data[i - 1],
78 MAX_STARS, starstr);
79 }
80}
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070081
82static void print_hist(int fd)
83{
84 struct hist_key key = {}, next_key;
85 static struct task tasks[1024];
86 int task_cnt = 0;
87 int i;
88
Joe Stringerd40fc182016-12-14 14:43:38 -080089 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070090 int found = 0;
91
92 for (i = 0; i < task_cnt; i++)
93 if (memcmp(&tasks[i], &next_key, SIZE) == 0)
94 found = 1;
95 if (!found)
96 memcpy(&tasks[task_cnt++], &next_key, SIZE);
97 key = next_key;
98 }
99
100 for (i = 0; i < task_cnt; i++) {
101 printf("\npid %d cmd %s uid %d\n",
102 (__u32) tasks[i].pid_tgid,
103 tasks[i].comm,
104 (__u32) tasks[i].uid_gid);
105 print_hist_for_pid(fd, &tasks[i]);
106 }
107
108}
109
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700110static void int_exit(int sig)
111{
112 print_hist(map_fd[1]);
113 exit(0);
114}
115
116int main(int ac, char **argv)
117{
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700118 long key, next_key, value;
Daniel T. Lee63841bc2020-05-16 13:06:05 +0900119 struct bpf_link *links[2];
120 struct bpf_program *prog;
121 struct bpf_object *obj;
122 char filename[256];
123 int i, j = 0;
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700124 FILE *f;
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700125
Daniel T. Leed4fffba2022-12-24 16:15:24 +0900126 snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
Daniel T. Lee63841bc2020-05-16 13:06:05 +0900127 obj = bpf_object__open_file(filename, NULL);
128 if (libbpf_get_error(obj)) {
129 fprintf(stderr, "ERROR: opening BPF object file failed\n");
130 return 0;
131 }
132
133 /* load BPF program */
134 if (bpf_object__load(obj)) {
135 fprintf(stderr, "ERROR: loading BPF object file failed\n");
136 goto cleanup;
137 }
138
139 map_fd[0] = bpf_object__find_map_fd_by_name(obj, "my_map");
140 map_fd[1] = bpf_object__find_map_fd_by_name(obj, "my_hist_map");
141 if (map_fd[0] < 0 || map_fd[1] < 0) {
142 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
143 goto cleanup;
144 }
145
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700146 signal(SIGINT, int_exit);
Andy Gospodarekad990db2017-05-11 15:52:30 -0400147 signal(SIGTERM, int_exit);
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700148
Rong Tao1baa7e32022-10-29 17:11:13 +0800149 /* start 'ping' in the background to have some kfree_skb_reason
150 * events */
Jakub Kicinski5c3cf872019-02-27 19:04:10 -0800151 f = popen("ping -4 -c5 localhost", "r");
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700152 (void) f;
153
154 /* start 'dd' in the background to have plenty of 'write' syscalls */
155 f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
156 (void) f;
157
Daniel T. Lee63841bc2020-05-16 13:06:05 +0900158 bpf_object__for_each_program(prog, obj) {
159 links[j] = bpf_program__attach(prog);
160 if (libbpf_get_error(links[j])) {
161 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
162 links[j] = NULL;
163 goto cleanup;
164 }
165 j++;
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700166 }
167
168 for (i = 0; i < 5; i++) {
169 key = 0;
Joe Stringerd40fc182016-12-14 14:43:38 -0800170 while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
171 bpf_map_lookup_elem(map_fd[0], &next_key, &value);
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700172 printf("location 0x%lx count %ld\n", next_key, value);
173 key = next_key;
174 }
175 if (key)
176 printf("\n");
177 sleep(1);
178 }
179 print_hist(map_fd[1]);
180
Daniel T. Lee63841bc2020-05-16 13:06:05 +0900181cleanup:
182 for (j--; j >= 0; j--)
183 bpf_link__destroy(links[j]);
184
185 bpf_object__close(obj);
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700186 return 0;
187}