tracex2_user.c 3.1 KB

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