lathist_user.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  3. * Copyright (c) 2015 BMW Car IT GmbH
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <signal.h>
  9. #include <bpf/libbpf.h>
  10. #include <bpf/bpf.h>
  11. #define MAX_ENTRIES 20
  12. #define MAX_CPU 4
  13. #define MAX_STARS 40
  14. struct cpu_hist {
  15. long data[MAX_ENTRIES];
  16. long max;
  17. };
  18. static struct cpu_hist cpu_hist[MAX_CPU];
  19. static void stars(char *str, long val, long max, int width)
  20. {
  21. int i;
  22. for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
  23. str[i] = '*';
  24. if (val > max)
  25. str[i - 1] = '+';
  26. str[i] = '\0';
  27. }
  28. static void print_hist(void)
  29. {
  30. char starstr[MAX_STARS];
  31. struct cpu_hist *hist;
  32. int i, j;
  33. /* clear screen */
  34. printf("\033[2J");
  35. for (j = 0; j < MAX_CPU; j++) {
  36. hist = &cpu_hist[j];
  37. /* ignore CPUs without data (maybe offline?) */
  38. if (hist->max == 0)
  39. continue;
  40. printf("CPU %d\n", j);
  41. printf(" latency : count distribution\n");
  42. for (i = 1; i <= MAX_ENTRIES; i++) {
  43. stars(starstr, hist->data[i - 1], hist->max, MAX_STARS);
  44. printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
  45. (1l << i) >> 1, (1l << i) - 1,
  46. hist->data[i - 1], MAX_STARS, starstr);
  47. }
  48. }
  49. }
  50. static void get_data(int fd)
  51. {
  52. long key, value;
  53. int c, i;
  54. for (i = 0; i < MAX_CPU; i++)
  55. cpu_hist[i].max = 0;
  56. for (c = 0; c < MAX_CPU; c++) {
  57. for (i = 0; i < MAX_ENTRIES; i++) {
  58. key = c * MAX_ENTRIES + i;
  59. bpf_map_lookup_elem(fd, &key, &value);
  60. cpu_hist[c].data[i] = value;
  61. if (value > cpu_hist[c].max)
  62. cpu_hist[c].max = value;
  63. }
  64. }
  65. }
  66. int main(int argc, char **argv)
  67. {
  68. struct bpf_link *links[2];
  69. struct bpf_program *prog;
  70. struct bpf_object *obj;
  71. char filename[256];
  72. int map_fd, i = 0;
  73. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  74. obj = bpf_object__open_file(filename, NULL);
  75. if (libbpf_get_error(obj)) {
  76. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  77. return 0;
  78. }
  79. /* load BPF program */
  80. if (bpf_object__load(obj)) {
  81. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  82. goto cleanup;
  83. }
  84. map_fd = bpf_object__find_map_fd_by_name(obj, "my_lat");
  85. if (map_fd < 0) {
  86. fprintf(stderr, "ERROR: finding a map in obj file failed\n");
  87. goto cleanup;
  88. }
  89. bpf_object__for_each_program(prog, obj) {
  90. links[i] = bpf_program__attach(prog);
  91. if (libbpf_get_error(links[i])) {
  92. fprintf(stderr, "ERROR: bpf_program__attach failed\n");
  93. links[i] = NULL;
  94. goto cleanup;
  95. }
  96. i++;
  97. }
  98. while (1) {
  99. get_data(map_fd);
  100. print_hist();
  101. sleep(5);
  102. }
  103. cleanup:
  104. for (i--; i >= 0; i--)
  105. bpf_link__destroy(links[i]);
  106. bpf_object__close(obj);
  107. return 0;
  108. }