tracex2_user.c 3.0 KB

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