tracex2_user.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <linux/bpf.h>
  6. #include <string.h>
  7. #include <sys/resource.h>
  8. #include "libbpf.h"
  9. #include "bpf_load.h"
  10. #include "bpf_util.h"
  11. #define MAX_INDEX 64
  12. #define MAX_STARS 38
  13. static void stars(char *str, long val, long max, int width)
  14. {
  15. int i;
  16. for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
  17. str[i] = '*';
  18. if (val > max)
  19. str[i - 1] = '+';
  20. str[i] = '\0';
  21. }
  22. struct task {
  23. char comm[16];
  24. __u64 pid_tgid;
  25. __u64 uid_gid;
  26. };
  27. struct hist_key {
  28. struct task t;
  29. __u32 index;
  30. };
  31. #define SIZE sizeof(struct task)
  32. static void print_hist_for_pid(int fd, void *task)
  33. {
  34. unsigned int nr_cpus = bpf_num_possible_cpus();
  35. struct hist_key key = {}, next_key;
  36. long values[nr_cpus];
  37. char starstr[MAX_STARS];
  38. long value;
  39. long data[MAX_INDEX] = {};
  40. int max_ind = -1;
  41. long max_value = 0;
  42. int i, ind;
  43. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  44. if (memcmp(&next_key, task, SIZE)) {
  45. key = next_key;
  46. continue;
  47. }
  48. bpf_map_lookup_elem(fd, &next_key, values);
  49. value = 0;
  50. for (i = 0; i < nr_cpus; i++)
  51. value += values[i];
  52. ind = next_key.index;
  53. data[ind] = value;
  54. if (value && ind > max_ind)
  55. max_ind = ind;
  56. if (value > max_value)
  57. max_value = value;
  58. key = next_key;
  59. }
  60. printf(" syscall write() stats\n");
  61. printf(" byte_size : count distribution\n");
  62. for (i = 1; i <= max_ind + 1; i++) {
  63. stars(starstr, data[i - 1], max_value, MAX_STARS);
  64. printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
  65. (1l << i) >> 1, (1l << i) - 1, data[i - 1],
  66. MAX_STARS, starstr);
  67. }
  68. }
  69. static void print_hist(int fd)
  70. {
  71. struct hist_key key = {}, next_key;
  72. static struct task tasks[1024];
  73. int task_cnt = 0;
  74. int i;
  75. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  76. int found = 0;
  77. for (i = 0; i < task_cnt; i++)
  78. if (memcmp(&tasks[i], &next_key, SIZE) == 0)
  79. found = 1;
  80. if (!found)
  81. memcpy(&tasks[task_cnt++], &next_key, SIZE);
  82. key = next_key;
  83. }
  84. for (i = 0; i < task_cnt; i++) {
  85. printf("\npid %d cmd %s uid %d\n",
  86. (__u32) tasks[i].pid_tgid,
  87. tasks[i].comm,
  88. (__u32) tasks[i].uid_gid);
  89. print_hist_for_pid(fd, &tasks[i]);
  90. }
  91. }
  92. static void int_exit(int sig)
  93. {
  94. print_hist(map_fd[1]);
  95. exit(0);
  96. }
  97. int main(int ac, char **argv)
  98. {
  99. struct rlimit r = {1024*1024, RLIM_INFINITY};
  100. char filename[256];
  101. long key, next_key, value;
  102. FILE *f;
  103. int i;
  104. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  105. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  106. perror("setrlimit(RLIMIT_MEMLOCK)");
  107. return 1;
  108. }
  109. signal(SIGINT, int_exit);
  110. signal(SIGTERM, int_exit);
  111. /* start 'ping' in the background to have some kfree_skb events */
  112. f = popen("ping -c5 localhost", "r");
  113. (void) f;
  114. /* start 'dd' in the background to have plenty of 'write' syscalls */
  115. f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
  116. (void) f;
  117. if (load_bpf_file(filename)) {
  118. printf("%s", bpf_log_buf);
  119. return 1;
  120. }
  121. for (i = 0; i < 5; i++) {
  122. key = 0;
  123. while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
  124. bpf_map_lookup_elem(map_fd[0], &next_key, &value);
  125. printf("location 0x%lx count %ld\n", next_key, value);
  126. key = next_key;
  127. }
  128. if (key)
  129. printf("\n");
  130. sleep(1);
  131. }
  132. print_hist(map_fd[1]);
  133. return 0;
  134. }