tracex2_user.c 2.9 KB

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