tracex3_user.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "libbpf.h"
  15. #include "bpf_load.h"
  16. #include "bpf_util.h"
  17. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
  18. #define SLOTS 100
  19. static void clear_stats(int fd)
  20. {
  21. unsigned int nr_cpus = bpf_num_possible_cpus();
  22. __u64 values[nr_cpus];
  23. __u32 key;
  24. memset(values, 0, sizeof(values));
  25. for (key = 0; key < SLOTS; key++)
  26. bpf_map_update_elem(fd, &key, values, BPF_ANY);
  27. }
  28. const char *color[] = {
  29. "\033[48;5;255m",
  30. "\033[48;5;252m",
  31. "\033[48;5;250m",
  32. "\033[48;5;248m",
  33. "\033[48;5;246m",
  34. "\033[48;5;244m",
  35. "\033[48;5;242m",
  36. "\033[48;5;240m",
  37. "\033[48;5;238m",
  38. "\033[48;5;236m",
  39. "\033[48;5;234m",
  40. "\033[48;5;232m",
  41. };
  42. const int num_colors = ARRAY_SIZE(color);
  43. const char nocolor[] = "\033[00m";
  44. const char *sym[] = {
  45. " ",
  46. " ",
  47. ".",
  48. ".",
  49. "*",
  50. "*",
  51. "o",
  52. "o",
  53. "O",
  54. "O",
  55. "#",
  56. "#",
  57. };
  58. bool full_range = false;
  59. bool text_only = false;
  60. static void print_banner(void)
  61. {
  62. if (full_range)
  63. printf("|1ns |10ns |100ns |1us |10us |100us"
  64. " |1ms |10ms |100ms |1s |10s\n");
  65. else
  66. printf("|1us |10us |100us |1ms |10ms "
  67. "|100ms |1s |10s\n");
  68. }
  69. static void print_hist(int fd)
  70. {
  71. unsigned int nr_cpus = bpf_num_possible_cpus();
  72. __u64 total_events = 0;
  73. long values[nr_cpus];
  74. __u64 max_cnt = 0;
  75. __u64 cnt[SLOTS];
  76. __u64 value;
  77. __u32 key;
  78. int i;
  79. for (key = 0; key < SLOTS; key++) {
  80. bpf_map_lookup_elem(fd, &key, values);
  81. value = 0;
  82. for (i = 0; i < nr_cpus; i++)
  83. value += values[i];
  84. cnt[key] = value;
  85. total_events += value;
  86. if (value > max_cnt)
  87. max_cnt = value;
  88. }
  89. clear_stats(fd);
  90. for (key = full_range ? 0 : 29; key < SLOTS; key++) {
  91. int c = num_colors * cnt[key] / (max_cnt + 1);
  92. if (text_only)
  93. printf("%s", sym[c]);
  94. else
  95. printf("%s %s", color[c], nocolor);
  96. }
  97. printf(" # %lld\n", total_events);
  98. }
  99. int main(int ac, char **argv)
  100. {
  101. char filename[256];
  102. int i;
  103. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  104. if (load_bpf_file(filename)) {
  105. printf("%s", bpf_log_buf);
  106. return 1;
  107. }
  108. for (i = 1; i < ac; i++) {
  109. if (strcmp(argv[i], "-a") == 0) {
  110. full_range = true;
  111. } else if (strcmp(argv[i], "-t") == 0) {
  112. text_only = true;
  113. } else if (strcmp(argv[i], "-h") == 0) {
  114. printf("Usage:\n"
  115. " -a display wider latency range\n"
  116. " -t text only\n");
  117. return 1;
  118. }
  119. }
  120. printf(" heatmap of IO latency\n");
  121. if (text_only)
  122. printf(" %s", sym[num_colors - 1]);
  123. else
  124. printf(" %s %s", color[num_colors - 1], nocolor);
  125. printf(" - many events with this latency\n");
  126. if (text_only)
  127. printf(" %s", sym[0]);
  128. else
  129. printf(" %s %s", color[0], nocolor);
  130. printf(" - few events\n");
  131. for (i = 0; ; i++) {
  132. if (i % 20 == 0)
  133. print_banner();
  134. print_hist(map_fd[1]);
  135. sleep(2);
  136. }
  137. return 0;
  138. }