tracex3_user.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <signal.h>
  10. #include <unistd.h>
  11. #include <stdbool.h>
  12. #include <string.h>
  13. #include <linux/bpf.h>
  14. #include <sys/resource.h>
  15. #include <bpf/bpf.h>
  16. #include "bpf_load.h"
  17. #include "bpf_util.h"
  18. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
  19. #define SLOTS 100
  20. static void clear_stats(int fd)
  21. {
  22. unsigned int nr_cpus = bpf_num_possible_cpus();
  23. __u64 values[nr_cpus];
  24. __u32 key;
  25. memset(values, 0, sizeof(values));
  26. for (key = 0; key < SLOTS; key++)
  27. bpf_map_update_elem(fd, &key, values, BPF_ANY);
  28. }
  29. const char *color[] = {
  30. "\033[48;5;255m",
  31. "\033[48;5;252m",
  32. "\033[48;5;250m",
  33. "\033[48;5;248m",
  34. "\033[48;5;246m",
  35. "\033[48;5;244m",
  36. "\033[48;5;242m",
  37. "\033[48;5;240m",
  38. "\033[48;5;238m",
  39. "\033[48;5;236m",
  40. "\033[48;5;234m",
  41. "\033[48;5;232m",
  42. };
  43. const int num_colors = ARRAY_SIZE(color);
  44. const char nocolor[] = "\033[00m";
  45. const char *sym[] = {
  46. " ",
  47. " ",
  48. ".",
  49. ".",
  50. "*",
  51. "*",
  52. "o",
  53. "o",
  54. "O",
  55. "O",
  56. "#",
  57. "#",
  58. };
  59. bool full_range = false;
  60. bool text_only = false;
  61. static void print_banner(void)
  62. {
  63. if (full_range)
  64. printf("|1ns |10ns |100ns |1us |10us |100us"
  65. " |1ms |10ms |100ms |1s |10s\n");
  66. else
  67. printf("|1us |10us |100us |1ms |10ms "
  68. "|100ms |1s |10s\n");
  69. }
  70. static void print_hist(int fd)
  71. {
  72. unsigned int nr_cpus = bpf_num_possible_cpus();
  73. __u64 total_events = 0;
  74. long values[nr_cpus];
  75. __u64 max_cnt = 0;
  76. __u64 cnt[SLOTS];
  77. __u64 value;
  78. __u32 key;
  79. int i;
  80. for (key = 0; key < SLOTS; key++) {
  81. bpf_map_lookup_elem(fd, &key, values);
  82. value = 0;
  83. for (i = 0; i < nr_cpus; i++)
  84. value += values[i];
  85. cnt[key] = value;
  86. total_events += value;
  87. if (value > max_cnt)
  88. max_cnt = value;
  89. }
  90. clear_stats(fd);
  91. for (key = full_range ? 0 : 29; key < SLOTS; key++) {
  92. int c = num_colors * cnt[key] / (max_cnt + 1);
  93. if (text_only)
  94. printf("%s", sym[c]);
  95. else
  96. printf("%s %s", color[c], nocolor);
  97. }
  98. printf(" # %lld\n", total_events);
  99. }
  100. int main(int ac, char **argv)
  101. {
  102. struct rlimit r = {1024*1024, RLIM_INFINITY};
  103. char filename[256];
  104. int i;
  105. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  106. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  107. perror("setrlimit(RLIMIT_MEMLOCK)");
  108. return 1;
  109. }
  110. if (load_bpf_file(filename)) {
  111. printf("%s", bpf_log_buf);
  112. return 1;
  113. }
  114. for (i = 1; i < ac; i++) {
  115. if (strcmp(argv[i], "-a") == 0) {
  116. full_range = true;
  117. } else if (strcmp(argv[i], "-t") == 0) {
  118. text_only = true;
  119. } else if (strcmp(argv[i], "-h") == 0) {
  120. printf("Usage:\n"
  121. " -a display wider latency range\n"
  122. " -t text only\n");
  123. return 1;
  124. }
  125. }
  126. printf(" heatmap of IO latency\n");
  127. if (text_only)
  128. printf(" %s", sym[num_colors - 1]);
  129. else
  130. printf(" %s %s", color[num_colors - 1], nocolor);
  131. printf(" - many events with this latency\n");
  132. if (text_only)
  133. printf(" %s", sym[0]);
  134. else
  135. printf(" %s %s", color[0], nocolor);
  136. printf(" - few events\n");
  137. for (i = 0; ; i++) {
  138. if (i % 20 == 0)
  139. print_banner();
  140. print_hist(map_fd[1]);
  141. sleep(2);
  142. }
  143. return 0;
  144. }