WIP FPC-III support
[linux/fpc-iii.git] / kernel / trace / trace_sched_switch.c
blobe304196d7c2853ae0056c06c78d74819468265ca
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * trace context switch
5 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
7 */
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
12 #include <trace/events/sched.h>
14 #include "trace.h"
16 #define RECORD_CMDLINE 1
17 #define RECORD_TGID 2
19 static int sched_cmdline_ref;
20 static int sched_tgid_ref;
21 static DEFINE_MUTEX(sched_register_mutex);
23 static void
24 probe_sched_switch(void *ignore, bool preempt,
25 struct task_struct *prev, struct task_struct *next)
27 int flags;
29 flags = (RECORD_TGID * !!sched_tgid_ref) +
30 (RECORD_CMDLINE * !!sched_cmdline_ref);
32 if (!flags)
33 return;
34 tracing_record_taskinfo_sched_switch(prev, next, flags);
37 static void
38 probe_sched_wakeup(void *ignore, struct task_struct *wakee)
40 int flags;
42 flags = (RECORD_TGID * !!sched_tgid_ref) +
43 (RECORD_CMDLINE * !!sched_cmdline_ref);
45 if (!flags)
46 return;
47 tracing_record_taskinfo(current, flags);
50 static int tracing_sched_register(void)
52 int ret;
54 ret = register_trace_sched_wakeup(probe_sched_wakeup, NULL);
55 if (ret) {
56 pr_info("wakeup trace: Couldn't activate tracepoint"
57 " probe to kernel_sched_wakeup\n");
58 return ret;
61 ret = register_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
62 if (ret) {
63 pr_info("wakeup trace: Couldn't activate tracepoint"
64 " probe to kernel_sched_wakeup_new\n");
65 goto fail_deprobe;
68 ret = register_trace_sched_switch(probe_sched_switch, NULL);
69 if (ret) {
70 pr_info("sched trace: Couldn't activate tracepoint"
71 " probe to kernel_sched_switch\n");
72 goto fail_deprobe_wake_new;
75 return ret;
76 fail_deprobe_wake_new:
77 unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
78 fail_deprobe:
79 unregister_trace_sched_wakeup(probe_sched_wakeup, NULL);
80 return ret;
83 static void tracing_sched_unregister(void)
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);
90 static void tracing_start_sched_switch(int ops)
92 bool sched_register;
94 mutex_lock(&sched_register_mutex);
95 sched_register = (!sched_cmdline_ref && !sched_tgid_ref);
97 switch (ops) {
98 case RECORD_CMDLINE:
99 sched_cmdline_ref++;
100 break;
102 case RECORD_TGID:
103 sched_tgid_ref++;
104 break;
107 if (sched_register && (sched_cmdline_ref || sched_tgid_ref))
108 tracing_sched_register();
109 mutex_unlock(&sched_register_mutex);
112 static void tracing_stop_sched_switch(int ops)
114 mutex_lock(&sched_register_mutex);
116 switch (ops) {
117 case RECORD_CMDLINE:
118 sched_cmdline_ref--;
119 break;
121 case RECORD_TGID:
122 sched_tgid_ref--;
123 break;
126 if (!sched_cmdline_ref && !sched_tgid_ref)
127 tracing_sched_unregister();
128 mutex_unlock(&sched_register_mutex);
131 void tracing_start_cmdline_record(void)
133 tracing_start_sched_switch(RECORD_CMDLINE);
136 void tracing_stop_cmdline_record(void)
138 tracing_stop_sched_switch(RECORD_CMDLINE);
141 void tracing_start_tgid_record(void)
143 tracing_start_sched_switch(RECORD_TGID);
146 void tracing_stop_tgid_record(void)
148 tracing_stop_sched_switch(RECORD_TGID);