tracex3_user.c 3.1 KB

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