15 static void stars(char *str, long val, long max, int width)
19 for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
37 #define SIZE sizeof(struct task)
39 static void print_hist_for_pid(int fd, void *task)
41 unsigned int nr_cpus = bpf_num_possible_cpus();
42 struct hist_key key = {}, next_key;
44 char starstr[MAX_STARS];
46 long data[MAX_INDEX] = {};
51 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
52 if (memcmp(&next_key, task, SIZE)) {
56 bpf_map_lookup_elem(fd, &next_key, values);
58 for (i = 0; i < nr_cpus; i++)
62 if (value && ind > max_ind)
64 if (value > max_value)
69 printf(" syscall write() stats\n");
70 printf(" byte_size : count distribution\n");
71 for (i = 1; i <= max_ind + 1; i++) {
72 stars(starstr, data[i - 1], max_value, MAX_STARS);
73 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
74 (1l << i) >> 1, (1l << i) - 1, data[i - 1],
79 static void print_hist(int fd)
81 struct hist_key key = {}, next_key;
82 static struct task tasks[1024];
86 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
89 for (i = 0; i < task_cnt; i++)
90 if (memcmp(&tasks[i], &next_key, SIZE) == 0)
93 memcpy(&tasks[task_cnt++], &next_key, SIZE);
97 for (i = 0; i < task_cnt; i++) {
98 printf("\npid %d cmd %s uid %d\n",
99 (__u32) tasks[i].pid_tgid,
101 (__u32) tasks[i].uid_gid);
102 print_hist_for_pid(fd, &tasks[i]);
107 static void int_exit(int sig)
109 print_hist(map_fd[1]);
113 int main(int ac, char **argv)
116 long key, next_key, value;
120 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
122 signal(SIGINT, int_exit);
124 /* start 'ping' in the background to have some kfree_skb events */
125 f = popen("ping -c5 localhost", "r");
128 /* start 'dd' in the background to have plenty of 'write' syscalls */
129 f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
132 if (load_bpf_file(filename)) {
133 printf("%s", bpf_log_buf);
137 for (i = 0; i < 5; i++) {
139 while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
140 bpf_map_lookup_elem(map_fd[0], &next_key, &value);
141 printf("location 0x%lx count %ld\n", next_key, value);
148 print_hist(map_fd[1]);