offwaketime_kern.c 3.8 KB

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