tracex3_user.c 3.7 KB

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