offwaketime_user.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright (c) 2016 Facebook
  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 <unistd.h>
  9. #include <stdlib.h>
  10. #include <signal.h>
  11. #include <linux/bpf.h>
  12. #include <string.h>
  13. #include <linux/perf_event.h>
  14. #include <errno.h>
  15. #include <assert.h>
  16. #include <stdbool.h>
  17. #include <sys/resource.h>
  18. #include "libbpf.h"
  19. #include "bpf_load.h"
  20. #define PRINT_RAW_ADDR 0
  21. static void print_ksym(__u64 addr)
  22. {
  23. struct ksym *sym;
  24. if (!addr)
  25. return;
  26. sym = ksym_search(addr);
  27. if (PRINT_RAW_ADDR)
  28. printf("%s/%llx;", sym->name, addr);
  29. else
  30. printf("%s;", sym->name);
  31. }
  32. #define TASK_COMM_LEN 16
  33. struct key_t {
  34. char waker[TASK_COMM_LEN];
  35. char target[TASK_COMM_LEN];
  36. __u32 wret;
  37. __u32 tret;
  38. };
  39. static void print_stack(struct key_t *key, __u64 count)
  40. {
  41. __u64 ip[PERF_MAX_STACK_DEPTH] = {};
  42. static bool warned;
  43. int i;
  44. printf("%s;", key->target);
  45. if (bpf_lookup_elem(map_fd[3], &key->tret, ip) != 0) {
  46. printf("---;");
  47. } else {
  48. for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
  49. print_ksym(ip[i]);
  50. }
  51. printf("-;");
  52. if (bpf_lookup_elem(map_fd[3], &key->wret, ip) != 0) {
  53. printf("---;");
  54. } else {
  55. for (i = 0; i < PERF_MAX_STACK_DEPTH; i++)
  56. print_ksym(ip[i]);
  57. }
  58. printf(";%s %lld\n", key->waker, count);
  59. if ((key->tret == -EEXIST || key->wret == -EEXIST) && !warned) {
  60. printf("stackmap collisions seen. Consider increasing size\n");
  61. warned = true;
  62. } else if (((int)(key->tret) < 0 || (int)(key->wret) < 0)) {
  63. printf("err stackid %d %d\n", key->tret, key->wret);
  64. }
  65. }
  66. static void print_stacks(int fd)
  67. {
  68. struct key_t key = {}, next_key;
  69. __u64 value;
  70. while (bpf_get_next_key(fd, &key, &next_key) == 0) {
  71. bpf_lookup_elem(fd, &next_key, &value);
  72. print_stack(&next_key, value);
  73. key = next_key;
  74. }
  75. }
  76. static void int_exit(int sig)
  77. {
  78. print_stacks(map_fd[0]);
  79. exit(0);
  80. }
  81. int main(int argc, char **argv)
  82. {
  83. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  84. char filename[256];
  85. int delay = 1;
  86. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  87. setrlimit(RLIMIT_MEMLOCK, &r);
  88. signal(SIGINT, int_exit);
  89. if (load_kallsyms()) {
  90. printf("failed to process /proc/kallsyms\n");
  91. return 2;
  92. }
  93. if (load_bpf_file(filename)) {
  94. printf("%s", bpf_log_buf);
  95. return 1;
  96. }
  97. if (argc > 1)
  98. delay = atoi(argv[1]);
  99. sleep(delay);
  100. print_stacks(map_fd[0]);
  101. return 0;
  102. }