tracex2_user.c 4.2 KB

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