offwaketime_kern.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <uapi/linux/bpf.h>
  8. #include "bpf_helpers.h"
  9. #include <uapi/linux/ptrace.h>
  10. #include <uapi/linux/perf_event.h>
  11. #include <linux/version.h>
  12. #include <linux/sched.h>
  13. #define _(P) ({typeof(P) val; bpf_probe_read(&val, sizeof(val), &P); val;})
  14. #define MINBLOCK_US 1
  15. struct key_t {
  16. char waker[TASK_COMM_LEN];
  17. char target[TASK_COMM_LEN];
  18. u32 wret;
  19. u32 tret;
  20. };
  21. struct bpf_map_def SEC("maps") counts = {
  22. .type = BPF_MAP_TYPE_HASH,
  23. .key_size = sizeof(struct key_t),
  24. .value_size = sizeof(u64),
  25. .max_entries = 10000,
  26. };
  27. struct bpf_map_def SEC("maps") start = {
  28. .type = BPF_MAP_TYPE_HASH,
  29. .key_size = sizeof(u32),
  30. .value_size = sizeof(u64),
  31. .max_entries = 10000,
  32. };
  33. struct wokeby_t {
  34. char name[TASK_COMM_LEN];
  35. u32 ret;
  36. };
  37. struct bpf_map_def SEC("maps") wokeby = {
  38. .type = BPF_MAP_TYPE_HASH,
  39. .key_size = sizeof(u32),
  40. .value_size = sizeof(struct wokeby_t),
  41. .max_entries = 10000,
  42. };
  43. struct bpf_map_def SEC("maps") stackmap = {
  44. .type = BPF_MAP_TYPE_STACK_TRACE,
  45. .key_size = sizeof(u32),
  46. .value_size = PERF_MAX_STACK_DEPTH * sizeof(u64),
  47. .max_entries = 10000,
  48. };
  49. #define STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
  50. SEC("kprobe/try_to_wake_up")
  51. int waker(struct pt_regs *ctx)
  52. {
  53. struct task_struct *p = (void *) PT_REGS_PARM1(ctx);
  54. struct wokeby_t woke;
  55. u32 pid;
  56. pid = _(p->pid);
  57. bpf_get_current_comm(&woke.name, sizeof(woke.name));
  58. woke.ret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
  59. bpf_map_update_elem(&wokeby, &pid, &woke, BPF_ANY);
  60. return 0;
  61. }
  62. static inline int update_counts(void *ctx, u32 pid, u64 delta)
  63. {
  64. struct wokeby_t *woke;
  65. u64 zero = 0, *val;
  66. struct key_t key;
  67. __builtin_memset(&key.waker, 0, sizeof(key.waker));
  68. bpf_get_current_comm(&key.target, sizeof(key.target));
  69. key.tret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
  70. key.wret = 0;
  71. woke = bpf_map_lookup_elem(&wokeby, &pid);
  72. if (woke) {
  73. key.wret = woke->ret;
  74. __builtin_memcpy(&key.waker, woke->name, sizeof(key.waker));
  75. bpf_map_delete_elem(&wokeby, &pid);
  76. }
  77. val = bpf_map_lookup_elem(&counts, &key);
  78. if (!val) {
  79. bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
  80. val = bpf_map_lookup_elem(&counts, &key);
  81. if (!val)
  82. return 0;
  83. }
  84. (*val) += delta;
  85. return 0;
  86. }
  87. #if 1
  88. /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */
  89. struct sched_switch_args {
  90. unsigned long long pad;
  91. char prev_comm[16];
  92. int prev_pid;
  93. int prev_prio;
  94. long long prev_state;
  95. char next_comm[16];
  96. int next_pid;
  97. int next_prio;
  98. };
  99. SEC("tracepoint/sched/sched_switch")
  100. int oncpu(struct sched_switch_args *ctx)
  101. {
  102. /* record previous thread sleep time */
  103. u32 pid = ctx->prev_pid;
  104. #else
  105. SEC("kprobe/finish_task_switch")
  106. int oncpu(struct pt_regs *ctx)
  107. {
  108. struct task_struct *p = (void *) PT_REGS_PARM1(ctx);
  109. /* record previous thread sleep time */
  110. u32 pid = _(p->pid);
  111. #endif
  112. u64 delta, ts, *tsp;
  113. ts = bpf_ktime_get_ns();
  114. bpf_map_update_elem(&start, &pid, &ts, BPF_ANY);
  115. /* calculate current thread's delta time */
  116. pid = bpf_get_current_pid_tgid();
  117. tsp = bpf_map_lookup_elem(&start, &pid);
  118. if (!tsp)
  119. /* missed start or filtered */
  120. return 0;
  121. delta = bpf_ktime_get_ns() - *tsp;
  122. bpf_map_delete_elem(&start, &pid);
  123. delta = delta / 1000;
  124. if (delta < MINBLOCK_US)
  125. return 0;
  126. return update_counts(ctx, pid, delta);
  127. }
  128. char _license[] SEC("license") = "GPL";
  129. u32 _version SEC("version") = LINUX_VERSION_CODE;