lathist_user.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  2. * Copyright (c) 2015 BMW Car IT GmbH
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <signal.h>
  12. #include <linux/bpf.h>
  13. #include "libbpf.h"
  14. #include "bpf_load.h"
  15. #define MAX_ENTRIES 20
  16. #define MAX_CPU 4
  17. #define MAX_STARS 40
  18. struct cpu_hist {
  19. long data[MAX_ENTRIES];
  20. long max;
  21. };
  22. static struct cpu_hist cpu_hist[MAX_CPU];
  23. static void stars(char *str, long val, long max, int width)
  24. {
  25. int i;
  26. for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
  27. str[i] = '*';
  28. if (val > max)
  29. str[i - 1] = '+';
  30. str[i] = '\0';
  31. }
  32. static void print_hist(void)
  33. {
  34. char starstr[MAX_STARS];
  35. struct cpu_hist *hist;
  36. int i, j;
  37. /* clear screen */
  38. printf("\033[2J");
  39. for (j = 0; j < MAX_CPU; j++) {
  40. hist = &cpu_hist[j];
  41. /* ignore CPUs without data (maybe offline?) */
  42. if (hist->max == 0)
  43. continue;
  44. printf("CPU %d\n", j);
  45. printf(" latency : count distribution\n");
  46. for (i = 1; i <= MAX_ENTRIES; i++) {
  47. stars(starstr, hist->data[i - 1], hist->max, MAX_STARS);
  48. printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
  49. (1l << i) >> 1, (1l << i) - 1,
  50. hist->data[i - 1], MAX_STARS, starstr);
  51. }
  52. }
  53. }
  54. static void get_data(int fd)
  55. {
  56. long key, value;
  57. int c, i;
  58. for (i = 0; i < MAX_CPU; i++)
  59. cpu_hist[i].max = 0;
  60. for (c = 0; c < MAX_CPU; c++) {
  61. for (i = 0; i < MAX_ENTRIES; i++) {
  62. key = c * MAX_ENTRIES + i;
  63. bpf_lookup_elem(fd, &key, &value);
  64. cpu_hist[c].data[i] = value;
  65. if (value > cpu_hist[c].max)
  66. cpu_hist[c].max = value;
  67. }
  68. }
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. char filename[256];
  73. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  74. if (load_bpf_file(filename)) {
  75. printf("%s", bpf_log_buf);
  76. return 1;
  77. }
  78. while (1) {
  79. get_data(map_fd[1]);
  80. print_hist();
  81. sleep(5);
  82. }
  83. return 0;
  84. }