tracex3_user.c 2.9 KB

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