tracex2_user.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <linux/bpf.h>
  7. #include <string.h>
  8. #include <sys/resource.h>
  9. #include <bpf/bpf.h>
  10. #include "bpf_load.h"
  11. #include "bpf_util.h"
  12. #define MAX_INDEX 64
  13. #define MAX_STARS 38
  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. struct rlimit r = {1024*1024, RLIM_INFINITY};
  101. char filename[256];
  102. long key, next_key, value;
  103. FILE *f;
  104. int i;
  105. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  106. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  107. perror("setrlimit(RLIMIT_MEMLOCK)");
  108. return 1;
  109. }
  110. signal(SIGINT, int_exit);
  111. signal(SIGTERM, int_exit);
  112. /* start 'ping' in the background to have some kfree_skb events */
  113. f = popen("ping -c5 localhost", "r");
  114. (void) f;
  115. /* start 'dd' in the background to have plenty of 'write' syscalls */
  116. f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
  117. (void) f;
  118. if (load_bpf_file(filename)) {
  119. printf("%s", bpf_log_buf);
  120. return 1;
  121. }
  122. for (i = 0; i < 5; i++) {
  123. key = 0;
  124. while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
  125. bpf_map_lookup_elem(map_fd[0], &next_key, &value);
  126. printf("location 0x%lx count %ld\n", next_key, value);
  127. key = next_key;
  128. }
  129. if (key)
  130. printf("\n");
  131. sleep(1);
  132. }
  133. print_hist(map_fd[1]);
  134. return 0;
  135. }