tracex3_user.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include <sys/resource.h>
  11. #include <bpf/bpf.h>
  12. #include <bpf/libbpf.h>
  13. #include "bpf_util.h"
  14. #define SLOTS 100
  15. static void clear_stats(int fd)
  16. {
  17. unsigned int nr_cpus = bpf_num_possible_cpus();
  18. __u64 values[nr_cpus];
  19. __u32 key;
  20. memset(values, 0, sizeof(values));
  21. for (key = 0; key < SLOTS; key++)
  22. bpf_map_update_elem(fd, &key, values, BPF_ANY);
  23. }
  24. const char *color[] = {
  25. "\033[48;5;255m",
  26. "\033[48;5;252m",
  27. "\033[48;5;250m",
  28. "\033[48;5;248m",
  29. "\033[48;5;246m",
  30. "\033[48;5;244m",
  31. "\033[48;5;242m",
  32. "\033[48;5;240m",
  33. "\033[48;5;238m",
  34. "\033[48;5;236m",
  35. "\033[48;5;234m",
  36. "\033[48;5;232m",
  37. };
  38. const int num_colors = ARRAY_SIZE(color);
  39. const char nocolor[] = "\033[00m";
  40. const char *sym[] = {
  41. " ",
  42. " ",
  43. ".",
  44. ".",
  45. "*",
  46. "*",
  47. "o",
  48. "o",
  49. "O",
  50. "O",
  51. "#",
  52. "#",
  53. };
  54. bool full_range = false;
  55. bool text_only = false;
  56. static void print_banner(void)
  57. {
  58. if (full_range)
  59. printf("|1ns |10ns |100ns |1us |10us |100us"
  60. " |1ms |10ms |100ms |1s |10s\n");
  61. else
  62. printf("|1us |10us |100us |1ms |10ms "
  63. "|100ms |1s |10s\n");
  64. }
  65. static void print_hist(int fd)
  66. {
  67. unsigned int nr_cpus = bpf_num_possible_cpus();
  68. __u64 total_events = 0;
  69. long values[nr_cpus];
  70. __u64 max_cnt = 0;
  71. __u64 cnt[SLOTS];
  72. __u64 value;
  73. __u32 key;
  74. int i;
  75. for (key = 0; key < SLOTS; key++) {
  76. bpf_map_lookup_elem(fd, &key, values);
  77. value = 0;
  78. for (i = 0; i < nr_cpus; i++)
  79. value += values[i];
  80. cnt[key] = value;
  81. total_events += value;
  82. if (value > max_cnt)
  83. max_cnt = value;
  84. }
  85. clear_stats(fd);
  86. for (key = full_range ? 0 : 29; key < SLOTS; key++) {
  87. int c = num_colors * cnt[key] / (max_cnt + 1);
  88. if (text_only)
  89. printf("%s", sym[c]);
  90. else
  91. printf("%s %s", color[c], nocolor);
  92. }
  93. printf(" # %lld\n", total_events);
  94. }
  95. int main(int ac, char **argv)
  96. {
  97. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  98. struct bpf_link *links[2];
  99. struct bpf_program *prog;
  100. struct bpf_object *obj;
  101. char filename[256];
  102. int map_fd, i, j = 0;
  103. for (i = 1; i < ac; i++) {
  104. if (strcmp(argv[i], "-a") == 0) {
  105. full_range = true;
  106. } else if (strcmp(argv[i], "-t") == 0) {
  107. text_only = true;
  108. } else if (strcmp(argv[i], "-h") == 0) {
  109. printf("Usage:\n"
  110. " -a display wider latency range\n"
  111. " -t text only\n");
  112. return 1;
  113. }
  114. }
  115. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  116. perror("setrlimit(RLIMIT_MEMLOCK)");
  117. return 1;
  118. }
  119. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  120. obj = bpf_object__open_file(filename, NULL);
  121. if (libbpf_get_error(obj)) {
  122. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  123. return 0;
  124. }
  125. /* load BPF program */
  126. if (bpf_object__load(obj)) {
  127. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  128. goto cleanup;
  129. }
  130. map_fd = bpf_object__find_map_fd_by_name(obj, "lat_map");
  131. if (map_fd < 0) {
  132. fprintf(stderr, "ERROR: finding a map in obj file failed\n");
  133. goto cleanup;
  134. }
  135. bpf_object__for_each_program(prog, obj) {
  136. links[j] = bpf_program__attach(prog);
  137. if (libbpf_get_error(links[j])) {
  138. fprintf(stderr, "ERROR: bpf_program__attach failed\n");
  139. links[j] = NULL;
  140. goto cleanup;
  141. }
  142. j++;
  143. }
  144. printf(" heatmap of IO latency\n");
  145. if (text_only)
  146. printf(" %s", sym[num_colors - 1]);
  147. else
  148. printf(" %s %s", color[num_colors - 1], nocolor);
  149. printf(" - many events with this latency\n");
  150. if (text_only)
  151. printf(" %s", sym[0]);
  152. else
  153. printf(" %s %s", color[0], nocolor);
  154. printf(" - few events\n");
  155. for (i = 0; ; i++) {
  156. if (i % 20 == 0)
  157. print_banner();
  158. print_hist(map_fd);
  159. sleep(2);
  160. }
  161. cleanup:
  162. for (j--; j >= 0; j--)
  163. bpf_link__destroy(links[j]);
  164. bpf_object__close(obj);
  165. return 0;
  166. }