tracex2_user.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <signal.h>
  6. #include <string.h>
  7. #include <bpf/bpf.h>
  8. #include <bpf/libbpf.h>
  9. #include "bpf_util.h"
  10. #define MAX_INDEX 64
  11. #define MAX_STARS 38
  12. /* my_map, my_hist_map */
  13. static int map_fd[2];
  14. static void stars(char *str, long val, long max, int width)
  15. {
  16. int i;
  17. for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
  18. str[i] = '*';
  19. if (val > max)
  20. str[i - 1] = '+';
  21. str[i] = '\0';
  22. }
  23. struct task {
  24. char comm[16];
  25. __u64 pid_tgid;
  26. __u64 uid_gid;
  27. };
  28. struct hist_key {
  29. struct task t;
  30. __u32 index;
  31. };
  32. #define SIZE sizeof(struct task)
  33. static void print_hist_for_pid(int fd, void *task)
  34. {
  35. unsigned int nr_cpus = bpf_num_possible_cpus();
  36. struct hist_key key = {}, next_key;
  37. long values[nr_cpus];
  38. char starstr[MAX_STARS];
  39. long value;
  40. long data[MAX_INDEX] = {};
  41. int max_ind = -1;
  42. long max_value = 0;
  43. int i, ind;
  44. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  45. if (memcmp(&next_key, task, SIZE)) {
  46. key = next_key;
  47. continue;
  48. }
  49. bpf_map_lookup_elem(fd, &next_key, values);
  50. value = 0;
  51. for (i = 0; i < nr_cpus; i++)
  52. value += values[i];
  53. ind = next_key.index;
  54. data[ind] = value;
  55. if (value && ind > max_ind)
  56. max_ind = ind;
  57. if (value > max_value)
  58. max_value = value;
  59. key = next_key;
  60. }
  61. printf(" syscall write() stats\n");
  62. printf(" byte_size : count distribution\n");
  63. for (i = 1; i <= max_ind + 1; i++) {
  64. stars(starstr, data[i - 1], max_value, MAX_STARS);
  65. printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
  66. (1l << i) >> 1, (1l << i) - 1, data[i - 1],
  67. MAX_STARS, starstr);
  68. }
  69. }
  70. static void print_hist(int fd)
  71. {
  72. struct hist_key key = {}, next_key;
  73. static struct task tasks[1024];
  74. int task_cnt = 0;
  75. int i;
  76. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  77. int found = 0;
  78. for (i = 0; i < task_cnt; i++)
  79. if (memcmp(&tasks[i], &next_key, SIZE) == 0)
  80. found = 1;
  81. if (!found)
  82. memcpy(&tasks[task_cnt++], &next_key, SIZE);
  83. key = next_key;
  84. }
  85. for (i = 0; i < task_cnt; i++) {
  86. printf("\npid %d cmd %s uid %d\n",
  87. (__u32) tasks[i].pid_tgid,
  88. tasks[i].comm,
  89. (__u32) tasks[i].uid_gid);
  90. print_hist_for_pid(fd, &tasks[i]);
  91. }
  92. }
  93. static void int_exit(int sig)
  94. {
  95. print_hist(map_fd[1]);
  96. exit(0);
  97. }
  98. int main(int ac, char **argv)
  99. {
  100. long key, next_key, value;
  101. struct bpf_link *links[2];
  102. struct bpf_program *prog;
  103. struct bpf_object *obj;
  104. char filename[256];
  105. int i, j = 0;
  106. FILE *f;
  107. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  108. obj = bpf_object__open_file(filename, NULL);
  109. if (libbpf_get_error(obj)) {
  110. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  111. return 0;
  112. }
  113. /* load BPF program */
  114. if (bpf_object__load(obj)) {
  115. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  116. goto cleanup;
  117. }
  118. map_fd[0] = bpf_object__find_map_fd_by_name(obj, "my_map");
  119. map_fd[1] = bpf_object__find_map_fd_by_name(obj, "my_hist_map");
  120. if (map_fd[0] < 0 || map_fd[1] < 0) {
  121. fprintf(stderr, "ERROR: finding a map in obj file failed\n");
  122. goto cleanup;
  123. }
  124. signal(SIGINT, int_exit);
  125. signal(SIGTERM, int_exit);
  126. /* start 'ping' in the background to have some kfree_skb events */
  127. f = popen("ping -4 -c5 localhost", "r");
  128. (void) f;
  129. /* start 'dd' in the background to have plenty of 'write' syscalls */
  130. f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
  131. (void) f;
  132. bpf_object__for_each_program(prog, obj) {
  133. links[j] = bpf_program__attach(prog);
  134. if (libbpf_get_error(links[j])) {
  135. fprintf(stderr, "ERROR: bpf_program__attach failed\n");
  136. links[j] = NULL;
  137. goto cleanup;
  138. }
  139. j++;
  140. }
  141. for (i = 0; i < 5; i++) {
  142. key = 0;
  143. while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
  144. bpf_map_lookup_elem(map_fd[0], &next_key, &value);
  145. printf("location 0x%lx count %ld\n", next_key, value);
  146. key = next_key;
  147. }
  148. if (key)
  149. printf("\n");
  150. sleep(1);
  151. }
  152. print_hist(map_fd[1]);
  153. cleanup:
  154. for (j--; j >= 0; j--)
  155. bpf_link__destroy(links[j]);
  156. bpf_object__close(obj);
  157. return 0;
  158. }