blob: 7321a3f253c991f88e96f2a53e6ffa3e7c5f1719 [file] [log] [blame]
Alexei Starovoitovd822a192015-03-25 12:49:24 -07001#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <signal.h>
5#include <linux/bpf.h>
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07006#include <string.h>
Jesper Dangaard Brouer55de1702017-05-02 14:31:50 +02007#include <sys/resource.h>
Daniel Borkmanne00c7b22016-11-26 01:28:09 +01008
Alexei Starovoitovd822a192015-03-25 12:49:24 -07009#include "libbpf.h"
10#include "bpf_load.h"
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010011#include "bpf_util.h"
Alexei Starovoitovd822a192015-03-25 12:49:24 -070012
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
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070027struct 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)
Alexei Starovoitovd822a192015-03-25 12:49:24 -070041{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010042 unsigned int nr_cpus = bpf_num_possible_cpus();
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070043 struct hist_key key = {}, next_key;
Alexei Starovoitov30593032016-02-01 22:39:58 -080044 long values[nr_cpus];
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070045 char starstr[MAX_STARS];
Alexei Starovoitovd822a192015-03-25 12:49:24 -070046 long value;
47 long data[MAX_INDEX] = {};
Alexei Starovoitovd822a192015-03-25 12:49:24 -070048 int max_ind = -1;
49 long max_value = 0;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070050 int i, ind;
Alexei Starovoitovd822a192015-03-25 12:49:24 -070051
Joe Stringerd40fc182016-12-14 14:43:38 -080052 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070053 if (memcmp(&next_key, task, SIZE)) {
54 key = next_key;
55 continue;
56 }
Joe Stringerd40fc182016-12-14 14:43:38 -080057 bpf_map_lookup_elem(fd, &next_key, values);
Alexei Starovoitov30593032016-02-01 22:39:58 -080058 value = 0;
59 for (i = 0; i < nr_cpus; i++)
60 value += values[i];
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070061 ind = next_key.index;
62 data[ind] = value;
63 if (value && ind > max_ind)
64 max_ind = ind;
Alexei Starovoitovd822a192015-03-25 12:49:24 -070065 if (value > max_value)
66 max_value = value;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070067 key = next_key;
Alexei Starovoitovd822a192015-03-25 12:49:24 -070068 }
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}
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070079
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
Joe Stringerd40fc182016-12-14 14:43:38 -080087 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070088 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
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700108static void int_exit(int sig)
109{
110 print_hist(map_fd[1]);
111 exit(0);
112}
113
114int main(int ac, char **argv)
115{
Jesper Dangaard Brouer55de1702017-05-02 14:31:50 +0200116 struct rlimit r = {1024*1024, RLIM_INFINITY};
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700117 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
Jesper Dangaard Brouer55de1702017-05-02 14:31:50 +0200124 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
125 perror("setrlimit(RLIMIT_MEMLOCK)");
126 return 1;
127 }
128
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700129 signal(SIGINT, int_exit);
Andy Gospodarekad990db2017-05-11 15:52:30 -0400130 signal(SIGTERM, int_exit);
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700131
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;
Joe Stringerd40fc182016-12-14 14:43:38 -0800147 while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
148 bpf_map_lookup_elem(map_fd[0], &next_key, &value);
Alexei Starovoitovd822a192015-03-25 12:49:24 -0700149 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}