| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
#include <linux/ftrace.h> |
| 3 |
#include <linux/tracepoint.h> |
| 4 |
#include <linux/kernel.h> |
| 5 |
#include <linux/module.h> |
| 6 |
#include <linux/init.h> |
| 7 |
#include <linux/rv.h> |
| 8 |
#include <rv/instrumentation.h> |
| 9 |
|
| 10 |
#define MODULE_NAME "sssw" |
| 11 |
|
| 12 |
#include <trace/events/sched.h> |
| 13 |
#include <trace/events/signal.h> |
| 14 |
#include <rv_trace.h> |
| 15 |
#include <monitors/sched/sched.h> |
| 16 |
|
| 17 |
#define RV_MON_TYPE RV_MON_PER_TASK |
| 18 |
#include "sssw.h" |
| 19 |
#include <rv/da_monitor.h> |
| 20 |
|
| 21 |
static void handle_sched_set_state(void *data, struct task_struct *tsk, int state) |
| 22 |
{ |
| 23 |
if (state == TASK_RUNNING) |
| 24 |
da_handle_start_event(tsk, sched_set_state_runnable_sssw); |
| 25 |
else |
| 26 |
da_handle_event(tsk, sched_set_state_sleepable_sssw); |
| 27 |
} |
| 28 |
|
| 29 |
static void handle_sched_switch(void *data, bool preempt, |
| 30 |
struct task_struct *prev, |
| 31 |
struct task_struct *next, |
| 32 |
unsigned int prev_state) |
| 33 |
{ |
| 34 |
if (preempt) |
| 35 |
da_handle_event(prev, sched_switch_preempt_sssw); |
| 36 |
else if (prev_state == TASK_RUNNING) |
| 37 |
da_handle_event(prev, sched_switch_yield_sssw); |
| 38 |
else if (prev_state == TASK_RTLOCK_WAIT) |
| 39 |
/* special case of sleeping task with racy conditions */ |
| 40 |
da_handle_event(prev, sched_switch_blocking_sssw); |
| 41 |
else |
| 42 |
da_handle_event(prev, sched_switch_suspend_sssw); |
| 43 |
da_handle_event(next, sched_switch_in_sssw); |
| 44 |
} |
| 45 |
|
| 46 |
static void handle_sched_wakeup(void *data, struct task_struct *p) |
| 47 |
{ |
| 48 |
/* |
| 49 |
* Wakeup can also lead to signal_wakeup although the system is |
| 50 |
* actually runnable. The monitor can safely start with this event. |
| 51 |
*/ |
| 52 |
da_handle_start_event(p, sched_wakeup_sssw); |
| 53 |
} |
| 54 |
|
| 55 |
static void handle_signal_deliver(void *data, int sig, |
| 56 |
struct kernel_siginfo *info, |
| 57 |
struct k_sigaction *ka) |
| 58 |
{ |
| 59 |
da_handle_event(current, signal_deliver_sssw); |
| 60 |
} |
| 61 |
|
| 62 |
static int enable_sssw(void) |
| 63 |
{ |
| 64 |
int retval; |
| 65 |
|
| 66 |
retval = da_monitor_init(); |
| 67 |
if (retval) |
| 68 |
return retval; |
| 69 |
|
| 70 |
rv_attach_trace_probe("sssw", sched_set_state_tp, handle_sched_set_state); |
| 71 |
rv_attach_trace_probe("sssw", sched_switch, handle_sched_switch); |
| 72 |
rv_attach_trace_probe("sssw", sched_wakeup, handle_sched_wakeup); |
| 73 |
rv_attach_trace_probe("sssw", signal_deliver, handle_signal_deliver); |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static void disable_sssw(void) |
| 79 |
{ |
| 80 |
rv_this.enabled = 0; |
| 81 |
|
| 82 |
rv_detach_trace_probe("sssw", sched_set_state_tp, handle_sched_set_state); |
| 83 |
rv_detach_trace_probe("sssw", sched_switch, handle_sched_switch); |
| 84 |
rv_detach_trace_probe("sssw", sched_wakeup, handle_sched_wakeup); |
| 85 |
rv_detach_trace_probe("sssw", signal_deliver, handle_signal_deliver); |
| 86 |
|
| 87 |
da_monitor_destroy(); |
| 88 |
} |
| 89 |
|
| 90 |
static struct rv_monitor rv_this = { |
| 91 |
.name = "sssw", |
| 92 |
.description = "set state sleep and wakeup.", |
| 93 |
.enable = enable_sssw, |
| 94 |
.disable = disable_sssw, |
| 95 |
.reset = da_monitor_reset_all, |
| 96 |
.enabled = 0, |
| 97 |
}; |
| 98 |
|
| 99 |
static int __init register_sssw(void) |
| 100 |
{ |
| 101 |
return rv_register_monitor(&rv_this, &rv_sched); |
| 102 |
} |
| 103 |
|
| 104 |
static void __exit unregister_sssw(void) |
| 105 |
{ |
| 106 |
rv_unregister_monitor(&rv_this); |
| 107 |
} |
| 108 |
|
| 109 |
module_init(register_sssw); |
| 110 |
module_exit(unregister_sssw); |
| 111 |
|
| 112 |
MODULE_LICENSE("GPL"); |
| 113 |
MODULE_AUTHOR("Gabriele Monaco <[email protected]>"); |
| 114 |
MODULE_DESCRIPTION("sssw: set state sleep and wakeup."); |