]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
35e8e302 SR |
2 | /* |
3 | * trace context switch | |
4 | * | |
5 | * Copyright (C) 2007 Steven Rostedt <[email protected]> | |
6 | * | |
7 | */ | |
8 | #include <linux/module.h> | |
35e8e302 SR |
9 | #include <linux/kallsyms.h> |
10 | #include <linux/uaccess.h> | |
35e8e302 | 11 | #include <linux/ftrace.h> |
ad8d75ff | 12 | #include <trace/events/sched.h> |
35e8e302 SR |
13 | |
14 | #include "trace.h" | |
15 | ||
d914ba37 JF |
16 | #define RECORD_CMDLINE 1 |
17 | #define RECORD_TGID 2 | |
18 | ||
19 | static int sched_cmdline_ref; | |
20 | static int sched_tgid_ref; | |
efade6e7 | 21 | static DEFINE_MUTEX(sched_register_mutex); |
82e04af4 | 22 | |
e309b41d | 23 | static void |
c73464b1 PZ |
24 | probe_sched_switch(void *ignore, bool preempt, |
25 | struct task_struct *prev, struct task_struct *next) | |
35e8e302 | 26 | { |
d914ba37 JF |
27 | int flags; |
28 | ||
29 | flags = (RECORD_TGID * !!sched_tgid_ref) + | |
30 | (RECORD_CMDLINE * !!sched_cmdline_ref); | |
b07c3f19 | 31 | |
d914ba37 JF |
32 | if (!flags) |
33 | return; | |
34 | tracing_record_taskinfo_sched_switch(prev, next, flags); | |
35e8e302 SR |
35 | } |
36 | ||
4e655519 | 37 | static void |
fbd705a0 | 38 | probe_sched_wakeup(void *ignore, struct task_struct *wakee) |
57422797 | 39 | { |
d914ba37 JF |
40 | int flags; |
41 | ||
42 | flags = (RECORD_TGID * !!sched_tgid_ref) + | |
43 | (RECORD_CMDLINE * !!sched_cmdline_ref); | |
57422797 | 44 | |
d914ba37 JF |
45 | if (!flags) |
46 | return; | |
47 | tracing_record_taskinfo(current, flags); | |
57422797 IM |
48 | } |
49 | ||
5b82a1b0 MD |
50 | static int tracing_sched_register(void) |
51 | { | |
52 | int ret; | |
53 | ||
38516ab5 | 54 | ret = register_trace_sched_wakeup(probe_sched_wakeup, NULL); |
5b82a1b0 | 55 | if (ret) { |
b07c3f19 | 56 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
57 | " probe to kernel_sched_wakeup\n"); |
58 | return ret; | |
59 | } | |
60 | ||
38516ab5 | 61 | ret = register_trace_sched_wakeup_new(probe_sched_wakeup, NULL); |
5b82a1b0 | 62 | if (ret) { |
b07c3f19 | 63 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
64 | " probe to kernel_sched_wakeup_new\n"); |
65 | goto fail_deprobe; | |
66 | } | |
67 | ||
38516ab5 | 68 | ret = register_trace_sched_switch(probe_sched_switch, NULL); |
5b82a1b0 | 69 | if (ret) { |
b07c3f19 | 70 | pr_info("sched trace: Couldn't activate tracepoint" |
73d8b8bc | 71 | " probe to kernel_sched_switch\n"); |
5b82a1b0 MD |
72 | goto fail_deprobe_wake_new; |
73 | } | |
74 | ||
75 | return ret; | |
76 | fail_deprobe_wake_new: | |
38516ab5 | 77 | unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL); |
5b82a1b0 | 78 | fail_deprobe: |
38516ab5 | 79 | unregister_trace_sched_wakeup(probe_sched_wakeup, NULL); |
5b82a1b0 MD |
80 | return ret; |
81 | } | |
82 | ||
83 | static void tracing_sched_unregister(void) | |
84 | { | |
38516ab5 SR |
85 | unregister_trace_sched_switch(probe_sched_switch, NULL); |
86 | unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL); | |
87 | unregister_trace_sched_wakeup(probe_sched_wakeup, NULL); | |
5b82a1b0 MD |
88 | } |
89 | ||
d914ba37 | 90 | static void tracing_start_sched_switch(int ops) |
5b82a1b0 | 91 | { |
d914ba37 | 92 | bool sched_register = (!sched_cmdline_ref && !sched_tgid_ref); |
efade6e7 | 93 | mutex_lock(&sched_register_mutex); |
d914ba37 JF |
94 | |
95 | switch (ops) { | |
96 | case RECORD_CMDLINE: | |
97 | sched_cmdline_ref++; | |
98 | break; | |
99 | ||
100 | case RECORD_TGID: | |
101 | sched_tgid_ref++; | |
102 | break; | |
103 | } | |
104 | ||
105 | if (sched_register && (sched_cmdline_ref || sched_tgid_ref)) | |
5b82a1b0 | 106 | tracing_sched_register(); |
efade6e7 | 107 | mutex_unlock(&sched_register_mutex); |
5b82a1b0 MD |
108 | } |
109 | ||
d914ba37 | 110 | static void tracing_stop_sched_switch(int ops) |
5b82a1b0 | 111 | { |
efade6e7 | 112 | mutex_lock(&sched_register_mutex); |
d914ba37 JF |
113 | |
114 | switch (ops) { | |
115 | case RECORD_CMDLINE: | |
116 | sched_cmdline_ref--; | |
117 | break; | |
118 | ||
119 | case RECORD_TGID: | |
120 | sched_tgid_ref--; | |
121 | break; | |
122 | } | |
123 | ||
124 | if (!sched_cmdline_ref && !sched_tgid_ref) | |
5b82a1b0 | 125 | tracing_sched_unregister(); |
efade6e7 | 126 | mutex_unlock(&sched_register_mutex); |
5b82a1b0 MD |
127 | } |
128 | ||
41bc8144 SR |
129 | void tracing_start_cmdline_record(void) |
130 | { | |
d914ba37 | 131 | tracing_start_sched_switch(RECORD_CMDLINE); |
41bc8144 SR |
132 | } |
133 | ||
134 | void tracing_stop_cmdline_record(void) | |
135 | { | |
d914ba37 JF |
136 | tracing_stop_sched_switch(RECORD_CMDLINE); |
137 | } | |
138 | ||
139 | void tracing_start_tgid_record(void) | |
140 | { | |
141 | tracing_start_sched_switch(RECORD_TGID); | |
142 | } | |
143 | ||
144 | void tracing_stop_tgid_record(void) | |
145 | { | |
146 | tracing_stop_sched_switch(RECORD_TGID); | |
41bc8144 | 147 | } |