1bce29ac9SDaniel Bristot de Oliveira // SPDX-License-Identifier: GPL-2.0
2bce29ac9SDaniel Bristot de Oliveira /*
3bce29ac9SDaniel Bristot de Oliveira * OS Noise Tracer: computes the OS Noise suffered by a running thread.
4a955d7eaSDaniel Bristot de Oliveira * Timerlat Tracer: measures the wakeup latency of a timer triggered IRQ and thread.
5bce29ac9SDaniel Bristot de Oliveira *
6bce29ac9SDaniel Bristot de Oliveira * Based on "hwlat_detector" tracer by:
7bce29ac9SDaniel Bristot de Oliveira * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
8bce29ac9SDaniel Bristot de Oliveira * Copyright (C) 2013-2016 Steven Rostedt, Red Hat, Inc. <srostedt@redhat.com>
9bce29ac9SDaniel Bristot de Oliveira * With feedback from Clark Williams <williams@redhat.com>
10bce29ac9SDaniel Bristot de Oliveira *
11bce29ac9SDaniel Bristot de Oliveira * And also based on the rtsl tracer presented on:
12bce29ac9SDaniel Bristot de Oliveira * DE OLIVEIRA, Daniel Bristot, et al. Demystifying the real-time linux
13bce29ac9SDaniel Bristot de Oliveira * scheduling latency. In: 32nd Euromicro Conference on Real-Time Systems
14bce29ac9SDaniel Bristot de Oliveira * (ECRTS 2020). Schloss Dagstuhl-Leibniz-Zentrum fur Informatik, 2020.
15bce29ac9SDaniel Bristot de Oliveira *
16bce29ac9SDaniel Bristot de Oliveira * Copyright (C) 2021 Daniel Bristot de Oliveira, Red Hat, Inc. <bristot@redhat.com>
17bce29ac9SDaniel Bristot de Oliveira */
18bce29ac9SDaniel Bristot de Oliveira
19bce29ac9SDaniel Bristot de Oliveira #include <linux/kthread.h>
20bce29ac9SDaniel Bristot de Oliveira #include <linux/tracefs.h>
21bce29ac9SDaniel Bristot de Oliveira #include <linux/uaccess.h>
22bce29ac9SDaniel Bristot de Oliveira #include <linux/cpumask.h>
23bce29ac9SDaniel Bristot de Oliveira #include <linux/delay.h>
24bce29ac9SDaniel Bristot de Oliveira #include <linux/sched/clock.h>
25a955d7eaSDaniel Bristot de Oliveira #include <uapi/linux/sched/types.h>
26bce29ac9SDaniel Bristot de Oliveira #include <linux/sched.h>
27bce29ac9SDaniel Bristot de Oliveira #include "trace.h"
28bce29ac9SDaniel Bristot de Oliveira
29bce29ac9SDaniel Bristot de Oliveira #ifdef CONFIG_X86_LOCAL_APIC
30bce29ac9SDaniel Bristot de Oliveira #include <asm/trace/irq_vectors.h>
31bce29ac9SDaniel Bristot de Oliveira #undef TRACE_INCLUDE_PATH
32bce29ac9SDaniel Bristot de Oliveira #undef TRACE_INCLUDE_FILE
33bce29ac9SDaniel Bristot de Oliveira #endif /* CONFIG_X86_LOCAL_APIC */
34bce29ac9SDaniel Bristot de Oliveira
35bce29ac9SDaniel Bristot de Oliveira #include <trace/events/irq.h>
36bce29ac9SDaniel Bristot de Oliveira #include <trace/events/sched.h>
37bce29ac9SDaniel Bristot de Oliveira
38bce29ac9SDaniel Bristot de Oliveira #define CREATE_TRACE_POINTS
39bce29ac9SDaniel Bristot de Oliveira #include <trace/events/osnoise.h>
40bce29ac9SDaniel Bristot de Oliveira
41bce29ac9SDaniel Bristot de Oliveira /*
42bce29ac9SDaniel Bristot de Oliveira * Default values.
43bce29ac9SDaniel Bristot de Oliveira */
44bce29ac9SDaniel Bristot de Oliveira #define BANNER "osnoise: "
45bce29ac9SDaniel Bristot de Oliveira #define DEFAULT_SAMPLE_PERIOD 1000000 /* 1s */
46bce29ac9SDaniel Bristot de Oliveira #define DEFAULT_SAMPLE_RUNTIME 1000000 /* 1s */
47bce29ac9SDaniel Bristot de Oliveira
48a955d7eaSDaniel Bristot de Oliveira #define DEFAULT_TIMERLAT_PERIOD 1000 /* 1ms */
49a955d7eaSDaniel Bristot de Oliveira #define DEFAULT_TIMERLAT_PRIO 95 /* FIFO 95 */
50a955d7eaSDaniel Bristot de Oliveira
51bce29ac9SDaniel Bristot de Oliveira /*
52b179d48bSDaniel Bristot de Oliveira * osnoise/options entries.
53b179d48bSDaniel Bristot de Oliveira */
54b179d48bSDaniel Bristot de Oliveira enum osnoise_options_index {
55b179d48bSDaniel Bristot de Oliveira OSN_DEFAULTS = 0,
5630838fcdSDaniel Bristot de Oliveira OSN_WORKLOAD,
571603dda4SDaniel Bristot de Oliveira OSN_PANIC_ON_STOP,
58b5dce200SDaniel Bristot de Oliveira OSN_PREEMPT_DISABLE,
59b5dce200SDaniel Bristot de Oliveira OSN_IRQ_DISABLE,
60b179d48bSDaniel Bristot de Oliveira OSN_MAX
61b179d48bSDaniel Bristot de Oliveira };
62b179d48bSDaniel Bristot de Oliveira
63b5dce200SDaniel Bristot de Oliveira static const char * const osnoise_options_str[OSN_MAX] = {
64b5dce200SDaniel Bristot de Oliveira "DEFAULTS",
65b5dce200SDaniel Bristot de Oliveira "OSNOISE_WORKLOAD",
66b5dce200SDaniel Bristot de Oliveira "PANIC_ON_STOP",
67b5dce200SDaniel Bristot de Oliveira "OSNOISE_PREEMPT_DISABLE",
68b5dce200SDaniel Bristot de Oliveira "OSNOISE_IRQ_DISABLE" };
69b179d48bSDaniel Bristot de Oliveira
7030838fcdSDaniel Bristot de Oliveira #define OSN_DEFAULT_OPTIONS 0x2
71ec370890SDaniel Bristot de Oliveira static unsigned long osnoise_options = OSN_DEFAULT_OPTIONS;
72b179d48bSDaniel Bristot de Oliveira
73b179d48bSDaniel Bristot de Oliveira /*
74dae18134SDaniel Bristot de Oliveira * trace_array of the enabled osnoise/timerlat instances.
75dae18134SDaniel Bristot de Oliveira */
76dae18134SDaniel Bristot de Oliveira struct osnoise_instance {
77dae18134SDaniel Bristot de Oliveira struct list_head list;
78dae18134SDaniel Bristot de Oliveira struct trace_array *tr;
79dae18134SDaniel Bristot de Oliveira };
80d7458bc0SDaniel Bristot de Oliveira
81d7458bc0SDaniel Bristot de Oliveira static struct list_head osnoise_instances;
82dae18134SDaniel Bristot de Oliveira
osnoise_has_registered_instances(void)83dae18134SDaniel Bristot de Oliveira static bool osnoise_has_registered_instances(void)
84dae18134SDaniel Bristot de Oliveira {
85dae18134SDaniel Bristot de Oliveira return !!list_first_or_null_rcu(&osnoise_instances,
86dae18134SDaniel Bristot de Oliveira struct osnoise_instance,
87dae18134SDaniel Bristot de Oliveira list);
88dae18134SDaniel Bristot de Oliveira }
89dae18134SDaniel Bristot de Oliveira
90dae18134SDaniel Bristot de Oliveira /*
912fac8d64SDaniel Bristot de Oliveira * osnoise_instance_registered - check if a tr is already registered
922fac8d64SDaniel Bristot de Oliveira */
osnoise_instance_registered(struct trace_array * tr)932fac8d64SDaniel Bristot de Oliveira static int osnoise_instance_registered(struct trace_array *tr)
942fac8d64SDaniel Bristot de Oliveira {
952fac8d64SDaniel Bristot de Oliveira struct osnoise_instance *inst;
962fac8d64SDaniel Bristot de Oliveira int found = 0;
972fac8d64SDaniel Bristot de Oliveira
982fac8d64SDaniel Bristot de Oliveira rcu_read_lock();
992fac8d64SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) {
1002fac8d64SDaniel Bristot de Oliveira if (inst->tr == tr)
1012fac8d64SDaniel Bristot de Oliveira found = 1;
1022fac8d64SDaniel Bristot de Oliveira }
1032fac8d64SDaniel Bristot de Oliveira rcu_read_unlock();
1042fac8d64SDaniel Bristot de Oliveira
1052fac8d64SDaniel Bristot de Oliveira return found;
1062fac8d64SDaniel Bristot de Oliveira }
1072fac8d64SDaniel Bristot de Oliveira
1082fac8d64SDaniel Bristot de Oliveira /*
109dae18134SDaniel Bristot de Oliveira * osnoise_register_instance - register a new trace instance
110dae18134SDaniel Bristot de Oliveira *
111dae18134SDaniel Bristot de Oliveira * Register a trace_array *tr in the list of instances running
112dae18134SDaniel Bristot de Oliveira * osnoise/timerlat tracers.
113dae18134SDaniel Bristot de Oliveira */
osnoise_register_instance(struct trace_array * tr)114dae18134SDaniel Bristot de Oliveira static int osnoise_register_instance(struct trace_array *tr)
115dae18134SDaniel Bristot de Oliveira {
116dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst;
117dae18134SDaniel Bristot de Oliveira
118dae18134SDaniel Bristot de Oliveira /*
119dae18134SDaniel Bristot de Oliveira * register/unregister serialization is provided by trace's
120dae18134SDaniel Bristot de Oliveira * trace_types_lock.
121dae18134SDaniel Bristot de Oliveira */
122dae18134SDaniel Bristot de Oliveira lockdep_assert_held(&trace_types_lock);
123dae18134SDaniel Bristot de Oliveira
124dae18134SDaniel Bristot de Oliveira inst = kmalloc(sizeof(*inst), GFP_KERNEL);
125dae18134SDaniel Bristot de Oliveira if (!inst)
126dae18134SDaniel Bristot de Oliveira return -ENOMEM;
127dae18134SDaniel Bristot de Oliveira
128dae18134SDaniel Bristot de Oliveira INIT_LIST_HEAD_RCU(&inst->list);
129dae18134SDaniel Bristot de Oliveira inst->tr = tr;
130dae18134SDaniel Bristot de Oliveira list_add_tail_rcu(&inst->list, &osnoise_instances);
131dae18134SDaniel Bristot de Oliveira
132dae18134SDaniel Bristot de Oliveira return 0;
133dae18134SDaniel Bristot de Oliveira }
134dae18134SDaniel Bristot de Oliveira
135dae18134SDaniel Bristot de Oliveira /*
136dae18134SDaniel Bristot de Oliveira * osnoise_unregister_instance - unregister a registered trace instance
137dae18134SDaniel Bristot de Oliveira *
138dae18134SDaniel Bristot de Oliveira * Remove the trace_array *tr from the list of instances running
139dae18134SDaniel Bristot de Oliveira * osnoise/timerlat tracers.
140dae18134SDaniel Bristot de Oliveira */
osnoise_unregister_instance(struct trace_array * tr)141dae18134SDaniel Bristot de Oliveira static void osnoise_unregister_instance(struct trace_array *tr)
142dae18134SDaniel Bristot de Oliveira {
143dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst;
144dae18134SDaniel Bristot de Oliveira int found = 0;
145dae18134SDaniel Bristot de Oliveira
146dae18134SDaniel Bristot de Oliveira /*
147dae18134SDaniel Bristot de Oliveira * register/unregister serialization is provided by trace's
148dae18134SDaniel Bristot de Oliveira * trace_types_lock.
149dae18134SDaniel Bristot de Oliveira */
150685b64e4SChuang Wang list_for_each_entry_rcu(inst, &osnoise_instances, list,
151685b64e4SChuang Wang lockdep_is_held(&trace_types_lock)) {
152dae18134SDaniel Bristot de Oliveira if (inst->tr == tr) {
153dae18134SDaniel Bristot de Oliveira list_del_rcu(&inst->list);
154dae18134SDaniel Bristot de Oliveira found = 1;
155dae18134SDaniel Bristot de Oliveira break;
156dae18134SDaniel Bristot de Oliveira }
157dae18134SDaniel Bristot de Oliveira }
158dae18134SDaniel Bristot de Oliveira
159dae18134SDaniel Bristot de Oliveira if (!found)
160dae18134SDaniel Bristot de Oliveira return;
161dae18134SDaniel Bristot de Oliveira
162cae16f2cSUladzislau Rezki (Sony) kvfree_rcu_mightsleep(inst);
163dae18134SDaniel Bristot de Oliveira }
164dae18134SDaniel Bristot de Oliveira
165dae18134SDaniel Bristot de Oliveira /*
166bce29ac9SDaniel Bristot de Oliveira * NMI runtime info.
167bce29ac9SDaniel Bristot de Oliveira */
168bce29ac9SDaniel Bristot de Oliveira struct osn_nmi {
169bce29ac9SDaniel Bristot de Oliveira u64 count;
170bce29ac9SDaniel Bristot de Oliveira u64 delta_start;
171bce29ac9SDaniel Bristot de Oliveira };
172bce29ac9SDaniel Bristot de Oliveira
173bce29ac9SDaniel Bristot de Oliveira /*
174bce29ac9SDaniel Bristot de Oliveira * IRQ runtime info.
175bce29ac9SDaniel Bristot de Oliveira */
176bce29ac9SDaniel Bristot de Oliveira struct osn_irq {
177bce29ac9SDaniel Bristot de Oliveira u64 count;
178bce29ac9SDaniel Bristot de Oliveira u64 arrival_time;
179bce29ac9SDaniel Bristot de Oliveira u64 delta_start;
180bce29ac9SDaniel Bristot de Oliveira };
181bce29ac9SDaniel Bristot de Oliveira
182a955d7eaSDaniel Bristot de Oliveira #define IRQ_CONTEXT 0
183a955d7eaSDaniel Bristot de Oliveira #define THREAD_CONTEXT 1
184e88ed227SDaniel Bristot de Oliveira #define THREAD_URET 2
185bce29ac9SDaniel Bristot de Oliveira /*
186bce29ac9SDaniel Bristot de Oliveira * sofirq runtime info.
187bce29ac9SDaniel Bristot de Oliveira */
188bce29ac9SDaniel Bristot de Oliveira struct osn_softirq {
189bce29ac9SDaniel Bristot de Oliveira u64 count;
190bce29ac9SDaniel Bristot de Oliveira u64 arrival_time;
191bce29ac9SDaniel Bristot de Oliveira u64 delta_start;
192bce29ac9SDaniel Bristot de Oliveira };
193bce29ac9SDaniel Bristot de Oliveira
194bce29ac9SDaniel Bristot de Oliveira /*
195bce29ac9SDaniel Bristot de Oliveira * thread runtime info.
196bce29ac9SDaniel Bristot de Oliveira */
197bce29ac9SDaniel Bristot de Oliveira struct osn_thread {
198bce29ac9SDaniel Bristot de Oliveira u64 count;
199bce29ac9SDaniel Bristot de Oliveira u64 arrival_time;
200bce29ac9SDaniel Bristot de Oliveira u64 delta_start;
201bce29ac9SDaniel Bristot de Oliveira };
202bce29ac9SDaniel Bristot de Oliveira
203bce29ac9SDaniel Bristot de Oliveira /*
204bce29ac9SDaniel Bristot de Oliveira * Runtime information: this structure saves the runtime information used by
205bce29ac9SDaniel Bristot de Oliveira * one sampling thread.
206bce29ac9SDaniel Bristot de Oliveira */
207bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables {
208bce29ac9SDaniel Bristot de Oliveira struct task_struct *kthread;
209bce29ac9SDaniel Bristot de Oliveira bool sampling;
210bce29ac9SDaniel Bristot de Oliveira pid_t pid;
211bce29ac9SDaniel Bristot de Oliveira struct osn_nmi nmi;
212bce29ac9SDaniel Bristot de Oliveira struct osn_irq irq;
213bce29ac9SDaniel Bristot de Oliveira struct osn_softirq softirq;
214bce29ac9SDaniel Bristot de Oliveira struct osn_thread thread;
215bce29ac9SDaniel Bristot de Oliveira local_t int_counter;
216bce29ac9SDaniel Bristot de Oliveira };
217bce29ac9SDaniel Bristot de Oliveira
218bce29ac9SDaniel Bristot de Oliveira /*
219bce29ac9SDaniel Bristot de Oliveira * Per-cpu runtime information.
220bce29ac9SDaniel Bristot de Oliveira */
2217a025e06STom Rix static DEFINE_PER_CPU(struct osnoise_variables, per_cpu_osnoise_var);
222bce29ac9SDaniel Bristot de Oliveira
223bce29ac9SDaniel Bristot de Oliveira /*
224bce29ac9SDaniel Bristot de Oliveira * this_cpu_osn_var - Return the per-cpu osnoise_variables on its relative CPU
225bce29ac9SDaniel Bristot de Oliveira */
this_cpu_osn_var(void)226bce29ac9SDaniel Bristot de Oliveira static inline struct osnoise_variables *this_cpu_osn_var(void)
227bce29ac9SDaniel Bristot de Oliveira {
228bce29ac9SDaniel Bristot de Oliveira return this_cpu_ptr(&per_cpu_osnoise_var);
229bce29ac9SDaniel Bristot de Oliveira }
230bce29ac9SDaniel Bristot de Oliveira
231a8632ef4SSteven Rostedt /*
232a8632ef4SSteven Rostedt * Protect the interface.
233a8632ef4SSteven Rostedt */
234a8632ef4SSteven Rostedt static struct mutex interface_lock;
235a8632ef4SSteven Rostedt
236a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
237bce29ac9SDaniel Bristot de Oliveira /*
238a955d7eaSDaniel Bristot de Oliveira * Runtime information for the timer mode.
239bce29ac9SDaniel Bristot de Oliveira */
240a955d7eaSDaniel Bristot de Oliveira struct timerlat_variables {
241a955d7eaSDaniel Bristot de Oliveira struct task_struct *kthread;
242a955d7eaSDaniel Bristot de Oliveira struct hrtimer timer;
243a955d7eaSDaniel Bristot de Oliveira u64 rel_period;
244a955d7eaSDaniel Bristot de Oliveira u64 abs_period;
245a955d7eaSDaniel Bristot de Oliveira bool tracing_thread;
246a955d7eaSDaniel Bristot de Oliveira u64 count;
247e88ed227SDaniel Bristot de Oliveira bool uthread_migrate;
248a955d7eaSDaniel Bristot de Oliveira };
249a955d7eaSDaniel Bristot de Oliveira
2507a025e06STom Rix static DEFINE_PER_CPU(struct timerlat_variables, per_cpu_timerlat_var);
251a955d7eaSDaniel Bristot de Oliveira
252a955d7eaSDaniel Bristot de Oliveira /*
253a955d7eaSDaniel Bristot de Oliveira * this_cpu_tmr_var - Return the per-cpu timerlat_variables on its relative CPU
254a955d7eaSDaniel Bristot de Oliveira */
this_cpu_tmr_var(void)255a955d7eaSDaniel Bristot de Oliveira static inline struct timerlat_variables *this_cpu_tmr_var(void)
256bce29ac9SDaniel Bristot de Oliveira {
257a955d7eaSDaniel Bristot de Oliveira return this_cpu_ptr(&per_cpu_timerlat_var);
258a955d7eaSDaniel Bristot de Oliveira }
259a955d7eaSDaniel Bristot de Oliveira
260a955d7eaSDaniel Bristot de Oliveira /*
261a955d7eaSDaniel Bristot de Oliveira * tlat_var_reset - Reset the values of the given timerlat_variables
262a955d7eaSDaniel Bristot de Oliveira */
tlat_var_reset(void)263a955d7eaSDaniel Bristot de Oliveira static inline void tlat_var_reset(void)
264a955d7eaSDaniel Bristot de Oliveira {
265a955d7eaSDaniel Bristot de Oliveira struct timerlat_variables *tlat_var;
266a955d7eaSDaniel Bristot de Oliveira int cpu;
2678c72f0b2SSteven Rostedt
2688c72f0b2SSteven Rostedt /* Synchronize with the timerlat interfaces */
2698c72f0b2SSteven Rostedt mutex_lock(&interface_lock);
270bce29ac9SDaniel Bristot de Oliveira /*
271bce29ac9SDaniel Bristot de Oliveira * So far, all the values are initialized as 0, so
272bce29ac9SDaniel Bristot de Oliveira * zeroing the structure is perfect.
273bce29ac9SDaniel Bristot de Oliveira */
274a955d7eaSDaniel Bristot de Oliveira for_each_cpu(cpu, cpu_online_mask) {
275a955d7eaSDaniel Bristot de Oliveira tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu);
2768c72f0b2SSteven Rostedt if (tlat_var->kthread)
2778c72f0b2SSteven Rostedt hrtimer_cancel(&tlat_var->timer);
278a955d7eaSDaniel Bristot de Oliveira memset(tlat_var, 0, sizeof(*tlat_var));
279a955d7eaSDaniel Bristot de Oliveira }
2808c72f0b2SSteven Rostedt mutex_unlock(&interface_lock);
281a955d7eaSDaniel Bristot de Oliveira }
282a955d7eaSDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */
283a955d7eaSDaniel Bristot de Oliveira #define tlat_var_reset() do {} while (0)
284a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */
285a955d7eaSDaniel Bristot de Oliveira
286a955d7eaSDaniel Bristot de Oliveira /*
287a955d7eaSDaniel Bristot de Oliveira * osn_var_reset - Reset the values of the given osnoise_variables
288a955d7eaSDaniel Bristot de Oliveira */
osn_var_reset(void)289a955d7eaSDaniel Bristot de Oliveira static inline void osn_var_reset(void)
290a955d7eaSDaniel Bristot de Oliveira {
291a955d7eaSDaniel Bristot de Oliveira struct osnoise_variables *osn_var;
292a955d7eaSDaniel Bristot de Oliveira int cpu;
293a955d7eaSDaniel Bristot de Oliveira
294a955d7eaSDaniel Bristot de Oliveira /*
295a955d7eaSDaniel Bristot de Oliveira * So far, all the values are initialized as 0, so
296a955d7eaSDaniel Bristot de Oliveira * zeroing the structure is perfect.
297a955d7eaSDaniel Bristot de Oliveira */
298a955d7eaSDaniel Bristot de Oliveira for_each_cpu(cpu, cpu_online_mask) {
299a955d7eaSDaniel Bristot de Oliveira osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
300bce29ac9SDaniel Bristot de Oliveira memset(osn_var, 0, sizeof(*osn_var));
301bce29ac9SDaniel Bristot de Oliveira }
302a955d7eaSDaniel Bristot de Oliveira }
303bce29ac9SDaniel Bristot de Oliveira
304bce29ac9SDaniel Bristot de Oliveira /*
305bce29ac9SDaniel Bristot de Oliveira * osn_var_reset_all - Reset the value of all per-cpu osnoise_variables
306bce29ac9SDaniel Bristot de Oliveira */
osn_var_reset_all(void)307bce29ac9SDaniel Bristot de Oliveira static inline void osn_var_reset_all(void)
308bce29ac9SDaniel Bristot de Oliveira {
309a955d7eaSDaniel Bristot de Oliveira osn_var_reset();
310a955d7eaSDaniel Bristot de Oliveira tlat_var_reset();
311bce29ac9SDaniel Bristot de Oliveira }
312bce29ac9SDaniel Bristot de Oliveira
313bce29ac9SDaniel Bristot de Oliveira /*
314bce29ac9SDaniel Bristot de Oliveira * Tells NMIs to call back to the osnoise tracer to record timestamps.
315bce29ac9SDaniel Bristot de Oliveira */
316bce29ac9SDaniel Bristot de Oliveira bool trace_osnoise_callback_enabled;
317bce29ac9SDaniel Bristot de Oliveira
318bce29ac9SDaniel Bristot de Oliveira /*
319bce29ac9SDaniel Bristot de Oliveira * osnoise sample structure definition. Used to store the statistics of a
320bce29ac9SDaniel Bristot de Oliveira * sample run.
321bce29ac9SDaniel Bristot de Oliveira */
322bce29ac9SDaniel Bristot de Oliveira struct osnoise_sample {
323bce29ac9SDaniel Bristot de Oliveira u64 runtime; /* runtime */
324bce29ac9SDaniel Bristot de Oliveira u64 noise; /* noise */
325bce29ac9SDaniel Bristot de Oliveira u64 max_sample; /* max single noise sample */
326bce29ac9SDaniel Bristot de Oliveira int hw_count; /* # HW (incl. hypervisor) interference */
327bce29ac9SDaniel Bristot de Oliveira int nmi_count; /* # NMIs during this sample */
328bce29ac9SDaniel Bristot de Oliveira int irq_count; /* # IRQs during this sample */
329bce29ac9SDaniel Bristot de Oliveira int softirq_count; /* # softirqs during this sample */
330bce29ac9SDaniel Bristot de Oliveira int thread_count; /* # threads during this sample */
331bce29ac9SDaniel Bristot de Oliveira };
332bce29ac9SDaniel Bristot de Oliveira
333a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
334a955d7eaSDaniel Bristot de Oliveira /*
335a955d7eaSDaniel Bristot de Oliveira * timerlat sample structure definition. Used to store the statistics of
336a955d7eaSDaniel Bristot de Oliveira * a sample run.
337a955d7eaSDaniel Bristot de Oliveira */
338a955d7eaSDaniel Bristot de Oliveira struct timerlat_sample {
339a955d7eaSDaniel Bristot de Oliveira u64 timer_latency; /* timer_latency */
340a955d7eaSDaniel Bristot de Oliveira unsigned int seqnum; /* unique sequence */
341a955d7eaSDaniel Bristot de Oliveira int context; /* timer context */
342a955d7eaSDaniel Bristot de Oliveira };
343a955d7eaSDaniel Bristot de Oliveira #endif
344a955d7eaSDaniel Bristot de Oliveira
345bce29ac9SDaniel Bristot de Oliveira /*
346bce29ac9SDaniel Bristot de Oliveira * Tracer data.
347bce29ac9SDaniel Bristot de Oliveira */
348bce29ac9SDaniel Bristot de Oliveira static struct osnoise_data {
349bce29ac9SDaniel Bristot de Oliveira u64 sample_period; /* total sampling period */
350bce29ac9SDaniel Bristot de Oliveira u64 sample_runtime; /* active sampling portion of period */
351a955d7eaSDaniel Bristot de Oliveira u64 stop_tracing; /* stop trace in the internal operation (loop/irq) */
352a955d7eaSDaniel Bristot de Oliveira u64 stop_tracing_total; /* stop trace in the final operation (report/thread) */
353a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
354a955d7eaSDaniel Bristot de Oliveira u64 timerlat_period; /* timerlat period */
355a955d7eaSDaniel Bristot de Oliveira u64 print_stack; /* print IRQ stack if total > */
356a955d7eaSDaniel Bristot de Oliveira int timerlat_tracer; /* timerlat tracer */
357a955d7eaSDaniel Bristot de Oliveira #endif
358bce29ac9SDaniel Bristot de Oliveira bool tainted; /* infor users and developers about a problem */
359bce29ac9SDaniel Bristot de Oliveira } osnoise_data = {
360bce29ac9SDaniel Bristot de Oliveira .sample_period = DEFAULT_SAMPLE_PERIOD,
361bce29ac9SDaniel Bristot de Oliveira .sample_runtime = DEFAULT_SAMPLE_RUNTIME,
362bce29ac9SDaniel Bristot de Oliveira .stop_tracing = 0,
363bce29ac9SDaniel Bristot de Oliveira .stop_tracing_total = 0,
364a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
365a955d7eaSDaniel Bristot de Oliveira .print_stack = 0,
366a955d7eaSDaniel Bristot de Oliveira .timerlat_period = DEFAULT_TIMERLAT_PERIOD,
367a955d7eaSDaniel Bristot de Oliveira .timerlat_tracer = 0,
368a955d7eaSDaniel Bristot de Oliveira #endif
369bce29ac9SDaniel Bristot de Oliveira };
370bce29ac9SDaniel Bristot de Oliveira
371ccb67544SDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
timerlat_enabled(void)372ccb67544SDaniel Bristot de Oliveira static inline bool timerlat_enabled(void)
373ccb67544SDaniel Bristot de Oliveira {
374ccb67544SDaniel Bristot de Oliveira return osnoise_data.timerlat_tracer;
375ccb67544SDaniel Bristot de Oliveira }
376ccb67544SDaniel Bristot de Oliveira
timerlat_softirq_exit(struct osnoise_variables * osn_var)377ccb67544SDaniel Bristot de Oliveira static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var)
378ccb67544SDaniel Bristot de Oliveira {
379ccb67544SDaniel Bristot de Oliveira struct timerlat_variables *tlat_var = this_cpu_tmr_var();
380ccb67544SDaniel Bristot de Oliveira /*
381ccb67544SDaniel Bristot de Oliveira * If the timerlat is enabled, but the irq handler did
382ccb67544SDaniel Bristot de Oliveira * not run yet enabling timerlat_tracer, do not trace.
383ccb67544SDaniel Bristot de Oliveira */
384ccb67544SDaniel Bristot de Oliveira if (!tlat_var->tracing_thread) {
385ccb67544SDaniel Bristot de Oliveira osn_var->softirq.arrival_time = 0;
386ccb67544SDaniel Bristot de Oliveira osn_var->softirq.delta_start = 0;
387ccb67544SDaniel Bristot de Oliveira return 0;
388ccb67544SDaniel Bristot de Oliveira }
389ccb67544SDaniel Bristot de Oliveira return 1;
390ccb67544SDaniel Bristot de Oliveira }
391ccb67544SDaniel Bristot de Oliveira
timerlat_thread_exit(struct osnoise_variables * osn_var)392ccb67544SDaniel Bristot de Oliveira static inline int timerlat_thread_exit(struct osnoise_variables *osn_var)
393ccb67544SDaniel Bristot de Oliveira {
394ccb67544SDaniel Bristot de Oliveira struct timerlat_variables *tlat_var = this_cpu_tmr_var();
395ccb67544SDaniel Bristot de Oliveira /*
396ccb67544SDaniel Bristot de Oliveira * If the timerlat is enabled, but the irq handler did
397ccb67544SDaniel Bristot de Oliveira * not run yet enabling timerlat_tracer, do not trace.
398ccb67544SDaniel Bristot de Oliveira */
399ccb67544SDaniel Bristot de Oliveira if (!tlat_var->tracing_thread) {
400ccb67544SDaniel Bristot de Oliveira osn_var->thread.delta_start = 0;
401ccb67544SDaniel Bristot de Oliveira osn_var->thread.arrival_time = 0;
402ccb67544SDaniel Bristot de Oliveira return 0;
403ccb67544SDaniel Bristot de Oliveira }
404ccb67544SDaniel Bristot de Oliveira return 1;
405ccb67544SDaniel Bristot de Oliveira }
406ccb67544SDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */
timerlat_enabled(void)407ccb67544SDaniel Bristot de Oliveira static inline bool timerlat_enabled(void)
408ccb67544SDaniel Bristot de Oliveira {
409ccb67544SDaniel Bristot de Oliveira return false;
410ccb67544SDaniel Bristot de Oliveira }
411ccb67544SDaniel Bristot de Oliveira
timerlat_softirq_exit(struct osnoise_variables * osn_var)412ccb67544SDaniel Bristot de Oliveira static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var)
413ccb67544SDaniel Bristot de Oliveira {
414ccb67544SDaniel Bristot de Oliveira return 1;
415ccb67544SDaniel Bristot de Oliveira }
timerlat_thread_exit(struct osnoise_variables * osn_var)416ccb67544SDaniel Bristot de Oliveira static inline int timerlat_thread_exit(struct osnoise_variables *osn_var)
417ccb67544SDaniel Bristot de Oliveira {
418ccb67544SDaniel Bristot de Oliveira return 1;
419ccb67544SDaniel Bristot de Oliveira }
420ccb67544SDaniel Bristot de Oliveira #endif
421ccb67544SDaniel Bristot de Oliveira
422d03721a6SDaniel Bristot de Oliveira #ifdef CONFIG_PREEMPT_RT
423bce29ac9SDaniel Bristot de Oliveira /*
424bce29ac9SDaniel Bristot de Oliveira * Print the osnoise header info.
425bce29ac9SDaniel Bristot de Oliveira */
print_osnoise_headers(struct seq_file * s)426bce29ac9SDaniel Bristot de Oliveira static void print_osnoise_headers(struct seq_file *s)
427bce29ac9SDaniel Bristot de Oliveira {
428bce29ac9SDaniel Bristot de Oliveira if (osnoise_data.tainted)
429bce29ac9SDaniel Bristot de Oliveira seq_puts(s, "# osnoise is tainted!\n");
430bce29ac9SDaniel Bristot de Oliveira
431d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# _-------=> irqs-off\n");
432d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# / _------=> need-resched\n");
433d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# | / _-----=> need-resched-lazy\n");
434d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# || / _----=> hardirq/softirq\n");
435d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# ||| / _---=> preempt-depth\n");
436d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n");
437d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# ||||| / _-=> migrate-disable\n");
438d03721a6SDaniel Bristot de Oliveira
439d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# |||||| / ");
440d03721a6SDaniel Bristot de Oliveira seq_puts(s, " MAX\n");
441d03721a6SDaniel Bristot de Oliveira
442d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# ||||| / ");
443d03721a6SDaniel Bristot de Oliveira seq_puts(s, " SINGLE Interference counters:\n");
444d03721a6SDaniel Bristot de Oliveira
445d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# ||||||| RUNTIME ");
446d03721a6SDaniel Bristot de Oliveira seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n");
447d03721a6SDaniel Bristot de Oliveira
448d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP IN US ");
449d03721a6SDaniel Bristot de Oliveira seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n");
450d03721a6SDaniel Bristot de Oliveira
451d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# | | | ||||||| | | ");
452d03721a6SDaniel Bristot de Oliveira seq_puts(s, " | | | | | | | |\n");
453d03721a6SDaniel Bristot de Oliveira }
454d03721a6SDaniel Bristot de Oliveira #else /* CONFIG_PREEMPT_RT */
print_osnoise_headers(struct seq_file * s)455d03721a6SDaniel Bristot de Oliveira static void print_osnoise_headers(struct seq_file *s)
456d03721a6SDaniel Bristot de Oliveira {
457d03721a6SDaniel Bristot de Oliveira if (osnoise_data.tainted)
458d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# osnoise is tainted!\n");
459d03721a6SDaniel Bristot de Oliveira
460bce29ac9SDaniel Bristot de Oliveira seq_puts(s, "# _-----=> irqs-off\n");
461bce29ac9SDaniel Bristot de Oliveira seq_puts(s, "# / _----=> need-resched\n");
462bce29ac9SDaniel Bristot de Oliveira seq_puts(s, "# | / _---=> hardirq/softirq\n");
463e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# || / _--=> preempt-depth\n");
464e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# ||| / _-=> migrate-disable ");
465bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " MAX\n");
466e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# |||| / delay ");
467bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " SINGLE Interference counters:\n");
468bce29ac9SDaniel Bristot de Oliveira
469e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# ||||| RUNTIME ");
470bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n");
471bce29ac9SDaniel Bristot de Oliveira
472e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP IN US ");
473bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n");
474bce29ac9SDaniel Bristot de Oliveira
475e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# | | | ||||| | | ");
476bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " | | | | | | | |\n");
477bce29ac9SDaniel Bristot de Oliveira }
478d03721a6SDaniel Bristot de Oliveira #endif /* CONFIG_PREEMPT_RT */
479bce29ac9SDaniel Bristot de Oliveira
480bce29ac9SDaniel Bristot de Oliveira /*
481bce29ac9SDaniel Bristot de Oliveira * osnoise_taint - report an osnoise error.
482bce29ac9SDaniel Bristot de Oliveira */
483bce29ac9SDaniel Bristot de Oliveira #define osnoise_taint(msg) ({ \
484dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst; \
485dae18134SDaniel Bristot de Oliveira struct trace_buffer *buffer; \
486bce29ac9SDaniel Bristot de Oliveira \
487dae18134SDaniel Bristot de Oliveira rcu_read_lock(); \
488dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) { \
489dae18134SDaniel Bristot de Oliveira buffer = inst->tr->array_buffer.buffer; \
490dae18134SDaniel Bristot de Oliveira trace_array_printk_buf(buffer, _THIS_IP_, msg); \
491dae18134SDaniel Bristot de Oliveira } \
492dae18134SDaniel Bristot de Oliveira rcu_read_unlock(); \
493bce29ac9SDaniel Bristot de Oliveira osnoise_data.tainted = true; \
494bce29ac9SDaniel Bristot de Oliveira })
495bce29ac9SDaniel Bristot de Oliveira
496bce29ac9SDaniel Bristot de Oliveira /*
497bce29ac9SDaniel Bristot de Oliveira * Record an osnoise_sample into the tracer buffer.
498bce29ac9SDaniel Bristot de Oliveira */
499dae18134SDaniel Bristot de Oliveira static void
__trace_osnoise_sample(struct osnoise_sample * sample,struct trace_buffer * buffer)500dae18134SDaniel Bristot de Oliveira __trace_osnoise_sample(struct osnoise_sample *sample, struct trace_buffer *buffer)
501bce29ac9SDaniel Bristot de Oliveira {
502bce29ac9SDaniel Bristot de Oliveira struct trace_event_call *call = &event_osnoise;
503bce29ac9SDaniel Bristot de Oliveira struct ring_buffer_event *event;
504bce29ac9SDaniel Bristot de Oliveira struct osnoise_entry *entry;
505bce29ac9SDaniel Bristot de Oliveira
506bce29ac9SDaniel Bristot de Oliveira event = trace_buffer_lock_reserve(buffer, TRACE_OSNOISE, sizeof(*entry),
507bce29ac9SDaniel Bristot de Oliveira tracing_gen_ctx());
508bce29ac9SDaniel Bristot de Oliveira if (!event)
509bce29ac9SDaniel Bristot de Oliveira return;
510bce29ac9SDaniel Bristot de Oliveira entry = ring_buffer_event_data(event);
511bce29ac9SDaniel Bristot de Oliveira entry->runtime = sample->runtime;
512bce29ac9SDaniel Bristot de Oliveira entry->noise = sample->noise;
513bce29ac9SDaniel Bristot de Oliveira entry->max_sample = sample->max_sample;
514bce29ac9SDaniel Bristot de Oliveira entry->hw_count = sample->hw_count;
515bce29ac9SDaniel Bristot de Oliveira entry->nmi_count = sample->nmi_count;
516bce29ac9SDaniel Bristot de Oliveira entry->irq_count = sample->irq_count;
517bce29ac9SDaniel Bristot de Oliveira entry->softirq_count = sample->softirq_count;
518bce29ac9SDaniel Bristot de Oliveira entry->thread_count = sample->thread_count;
519bce29ac9SDaniel Bristot de Oliveira
520bce29ac9SDaniel Bristot de Oliveira if (!call_filter_check_discard(call, entry, buffer, event))
521bce29ac9SDaniel Bristot de Oliveira trace_buffer_unlock_commit_nostack(buffer, event);
522bce29ac9SDaniel Bristot de Oliveira }
523bce29ac9SDaniel Bristot de Oliveira
524dae18134SDaniel Bristot de Oliveira /*
525dae18134SDaniel Bristot de Oliveira * Record an osnoise_sample on all osnoise instances.
526dae18134SDaniel Bristot de Oliveira */
trace_osnoise_sample(struct osnoise_sample * sample)527dae18134SDaniel Bristot de Oliveira static void trace_osnoise_sample(struct osnoise_sample *sample)
528dae18134SDaniel Bristot de Oliveira {
529dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst;
530dae18134SDaniel Bristot de Oliveira struct trace_buffer *buffer;
531dae18134SDaniel Bristot de Oliveira
532dae18134SDaniel Bristot de Oliveira rcu_read_lock();
533dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) {
534dae18134SDaniel Bristot de Oliveira buffer = inst->tr->array_buffer.buffer;
535dae18134SDaniel Bristot de Oliveira __trace_osnoise_sample(sample, buffer);
536dae18134SDaniel Bristot de Oliveira }
537dae18134SDaniel Bristot de Oliveira rcu_read_unlock();
538dae18134SDaniel Bristot de Oliveira }
539dae18134SDaniel Bristot de Oliveira
540a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
541a955d7eaSDaniel Bristot de Oliveira /*
542a955d7eaSDaniel Bristot de Oliveira * Print the timerlat header info.
543a955d7eaSDaniel Bristot de Oliveira */
544e1c4ad4aSDaniel Bristot de Oliveira #ifdef CONFIG_PREEMPT_RT
print_timerlat_headers(struct seq_file * s)545e1c4ad4aSDaniel Bristot de Oliveira static void print_timerlat_headers(struct seq_file *s)
546e1c4ad4aSDaniel Bristot de Oliveira {
547e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# _-------=> irqs-off\n");
548e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# / _------=> need-resched\n");
549e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# | / _-----=> need-resched-lazy\n");
550e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# || / _----=> hardirq/softirq\n");
551e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# ||| / _---=> preempt-depth\n");
552e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n");
553e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# ||||| / _-=> migrate-disable\n");
554e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# |||||| /\n");
555e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# ||||||| ACTIVATION\n");
556e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP ID ");
557e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, " CONTEXT LATENCY\n");
558e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# | | | ||||||| | | ");
559e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, " | |\n");
560e1c4ad4aSDaniel Bristot de Oliveira }
561e1c4ad4aSDaniel Bristot de Oliveira #else /* CONFIG_PREEMPT_RT */
print_timerlat_headers(struct seq_file * s)562a955d7eaSDaniel Bristot de Oliveira static void print_timerlat_headers(struct seq_file *s)
563a955d7eaSDaniel Bristot de Oliveira {
564a955d7eaSDaniel Bristot de Oliveira seq_puts(s, "# _-----=> irqs-off\n");
565a955d7eaSDaniel Bristot de Oliveira seq_puts(s, "# / _----=> need-resched\n");
566a955d7eaSDaniel Bristot de Oliveira seq_puts(s, "# | / _---=> hardirq/softirq\n");
567a955d7eaSDaniel Bristot de Oliveira seq_puts(s, "# || / _--=> preempt-depth\n");
568aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# ||| / _-=> migrate-disable\n");
569aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# |||| / delay\n");
570aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# ||||| ACTIVATION\n");
571aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP ID ");
572a955d7eaSDaniel Bristot de Oliveira seq_puts(s, " CONTEXT LATENCY\n");
573aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# | | | ||||| | | ");
574a955d7eaSDaniel Bristot de Oliveira seq_puts(s, " | |\n");
575a955d7eaSDaniel Bristot de Oliveira }
576e1c4ad4aSDaniel Bristot de Oliveira #endif /* CONFIG_PREEMPT_RT */
577a955d7eaSDaniel Bristot de Oliveira
578dae18134SDaniel Bristot de Oliveira static void
__trace_timerlat_sample(struct timerlat_sample * sample,struct trace_buffer * buffer)579dae18134SDaniel Bristot de Oliveira __trace_timerlat_sample(struct timerlat_sample *sample, struct trace_buffer *buffer)
580a955d7eaSDaniel Bristot de Oliveira {
581a955d7eaSDaniel Bristot de Oliveira struct trace_event_call *call = &event_osnoise;
582a955d7eaSDaniel Bristot de Oliveira struct ring_buffer_event *event;
583a955d7eaSDaniel Bristot de Oliveira struct timerlat_entry *entry;
584a955d7eaSDaniel Bristot de Oliveira
585a955d7eaSDaniel Bristot de Oliveira event = trace_buffer_lock_reserve(buffer, TRACE_TIMERLAT, sizeof(*entry),
586a955d7eaSDaniel Bristot de Oliveira tracing_gen_ctx());
587a955d7eaSDaniel Bristot de Oliveira if (!event)
588a955d7eaSDaniel Bristot de Oliveira return;
589a955d7eaSDaniel Bristot de Oliveira entry = ring_buffer_event_data(event);
590a955d7eaSDaniel Bristot de Oliveira entry->seqnum = sample->seqnum;
591a955d7eaSDaniel Bristot de Oliveira entry->context = sample->context;
592a955d7eaSDaniel Bristot de Oliveira entry->timer_latency = sample->timer_latency;
593a955d7eaSDaniel Bristot de Oliveira
594a955d7eaSDaniel Bristot de Oliveira if (!call_filter_check_discard(call, entry, buffer, event))
595a955d7eaSDaniel Bristot de Oliveira trace_buffer_unlock_commit_nostack(buffer, event);
596a955d7eaSDaniel Bristot de Oliveira }
597a955d7eaSDaniel Bristot de Oliveira
598dae18134SDaniel Bristot de Oliveira /*
599dae18134SDaniel Bristot de Oliveira * Record an timerlat_sample into the tracer buffer.
600dae18134SDaniel Bristot de Oliveira */
trace_timerlat_sample(struct timerlat_sample * sample)601dae18134SDaniel Bristot de Oliveira static void trace_timerlat_sample(struct timerlat_sample *sample)
602dae18134SDaniel Bristot de Oliveira {
603dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst;
604dae18134SDaniel Bristot de Oliveira struct trace_buffer *buffer;
605dae18134SDaniel Bristot de Oliveira
606dae18134SDaniel Bristot de Oliveira rcu_read_lock();
607dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) {
608dae18134SDaniel Bristot de Oliveira buffer = inst->tr->array_buffer.buffer;
609dae18134SDaniel Bristot de Oliveira __trace_timerlat_sample(sample, buffer);
610dae18134SDaniel Bristot de Oliveira }
611dae18134SDaniel Bristot de Oliveira rcu_read_unlock();
612dae18134SDaniel Bristot de Oliveira }
613dae18134SDaniel Bristot de Oliveira
614a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_STACKTRACE
615a955d7eaSDaniel Bristot de Oliveira
616a955d7eaSDaniel Bristot de Oliveira #define MAX_CALLS 256
617a955d7eaSDaniel Bristot de Oliveira
618a955d7eaSDaniel Bristot de Oliveira /*
619a955d7eaSDaniel Bristot de Oliveira * Stack trace will take place only at IRQ level, so, no need
620a955d7eaSDaniel Bristot de Oliveira * to control nesting here.
621a955d7eaSDaniel Bristot de Oliveira */
622a955d7eaSDaniel Bristot de Oliveira struct trace_stack {
623a955d7eaSDaniel Bristot de Oliveira int stack_size;
624a955d7eaSDaniel Bristot de Oliveira int nr_entries;
625a955d7eaSDaniel Bristot de Oliveira unsigned long calls[MAX_CALLS];
626a955d7eaSDaniel Bristot de Oliveira };
627a955d7eaSDaniel Bristot de Oliveira
628a955d7eaSDaniel Bristot de Oliveira static DEFINE_PER_CPU(struct trace_stack, trace_stack);
629a955d7eaSDaniel Bristot de Oliveira
630a955d7eaSDaniel Bristot de Oliveira /*
631a955d7eaSDaniel Bristot de Oliveira * timerlat_save_stack - save a stack trace without printing
632a955d7eaSDaniel Bristot de Oliveira *
633a955d7eaSDaniel Bristot de Oliveira * Save the current stack trace without printing. The
634a955d7eaSDaniel Bristot de Oliveira * stack will be printed later, after the end of the measurement.
635a955d7eaSDaniel Bristot de Oliveira */
timerlat_save_stack(int skip)636a955d7eaSDaniel Bristot de Oliveira static void timerlat_save_stack(int skip)
637a955d7eaSDaniel Bristot de Oliveira {
638a955d7eaSDaniel Bristot de Oliveira unsigned int size, nr_entries;
639a955d7eaSDaniel Bristot de Oliveira struct trace_stack *fstack;
640a955d7eaSDaniel Bristot de Oliveira
641a955d7eaSDaniel Bristot de Oliveira fstack = this_cpu_ptr(&trace_stack);
642a955d7eaSDaniel Bristot de Oliveira
643a955d7eaSDaniel Bristot de Oliveira size = ARRAY_SIZE(fstack->calls);
644a955d7eaSDaniel Bristot de Oliveira
645a955d7eaSDaniel Bristot de Oliveira nr_entries = stack_trace_save(fstack->calls, size, skip);
646a955d7eaSDaniel Bristot de Oliveira
647a955d7eaSDaniel Bristot de Oliveira fstack->stack_size = nr_entries * sizeof(unsigned long);
648a955d7eaSDaniel Bristot de Oliveira fstack->nr_entries = nr_entries;
649a955d7eaSDaniel Bristot de Oliveira
650a955d7eaSDaniel Bristot de Oliveira return;
651a955d7eaSDaniel Bristot de Oliveira
652a955d7eaSDaniel Bristot de Oliveira }
653dae18134SDaniel Bristot de Oliveira
654dae18134SDaniel Bristot de Oliveira static void
__timerlat_dump_stack(struct trace_buffer * buffer,struct trace_stack * fstack,unsigned int size)655dae18134SDaniel Bristot de Oliveira __timerlat_dump_stack(struct trace_buffer *buffer, struct trace_stack *fstack, unsigned int size)
656a955d7eaSDaniel Bristot de Oliveira {
657a955d7eaSDaniel Bristot de Oliveira struct trace_event_call *call = &event_osnoise;
658a955d7eaSDaniel Bristot de Oliveira struct ring_buffer_event *event;
659a955d7eaSDaniel Bristot de Oliveira struct stack_entry *entry;
660a955d7eaSDaniel Bristot de Oliveira
661a955d7eaSDaniel Bristot de Oliveira event = trace_buffer_lock_reserve(buffer, TRACE_STACK, sizeof(*entry) + size,
662a955d7eaSDaniel Bristot de Oliveira tracing_gen_ctx());
663a955d7eaSDaniel Bristot de Oliveira if (!event)
664dae18134SDaniel Bristot de Oliveira return;
665a955d7eaSDaniel Bristot de Oliveira
666a955d7eaSDaniel Bristot de Oliveira entry = ring_buffer_event_data(event);
667a955d7eaSDaniel Bristot de Oliveira
668a955d7eaSDaniel Bristot de Oliveira memcpy(&entry->caller, fstack->calls, size);
669a955d7eaSDaniel Bristot de Oliveira entry->size = fstack->nr_entries;
670a955d7eaSDaniel Bristot de Oliveira
671a955d7eaSDaniel Bristot de Oliveira if (!call_filter_check_discard(call, entry, buffer, event))
672a955d7eaSDaniel Bristot de Oliveira trace_buffer_unlock_commit_nostack(buffer, event);
673dae18134SDaniel Bristot de Oliveira }
674a955d7eaSDaniel Bristot de Oliveira
675dae18134SDaniel Bristot de Oliveira /*
676dae18134SDaniel Bristot de Oliveira * timerlat_dump_stack - dump a stack trace previously saved
677dae18134SDaniel Bristot de Oliveira */
timerlat_dump_stack(u64 latency)678b14f4568SDaniel Bristot de Oliveira static void timerlat_dump_stack(u64 latency)
679dae18134SDaniel Bristot de Oliveira {
680dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst;
681dae18134SDaniel Bristot de Oliveira struct trace_buffer *buffer;
682dae18134SDaniel Bristot de Oliveira struct trace_stack *fstack;
683dae18134SDaniel Bristot de Oliveira unsigned int size;
684dae18134SDaniel Bristot de Oliveira
685b14f4568SDaniel Bristot de Oliveira /*
686b14f4568SDaniel Bristot de Oliveira * trace only if latency > print_stack config, if enabled.
687b14f4568SDaniel Bristot de Oliveira */
688b14f4568SDaniel Bristot de Oliveira if (!osnoise_data.print_stack || osnoise_data.print_stack > latency)
689b14f4568SDaniel Bristot de Oliveira return;
690b14f4568SDaniel Bristot de Oliveira
691dae18134SDaniel Bristot de Oliveira preempt_disable_notrace();
692dae18134SDaniel Bristot de Oliveira fstack = this_cpu_ptr(&trace_stack);
693dae18134SDaniel Bristot de Oliveira size = fstack->stack_size;
694dae18134SDaniel Bristot de Oliveira
695dae18134SDaniel Bristot de Oliveira rcu_read_lock();
696dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) {
697dae18134SDaniel Bristot de Oliveira buffer = inst->tr->array_buffer.buffer;
698dae18134SDaniel Bristot de Oliveira __timerlat_dump_stack(buffer, fstack, size);
699dae18134SDaniel Bristot de Oliveira
700dae18134SDaniel Bristot de Oliveira }
701dae18134SDaniel Bristot de Oliveira rcu_read_unlock();
702a955d7eaSDaniel Bristot de Oliveira preempt_enable_notrace();
703a955d7eaSDaniel Bristot de Oliveira }
704b14f4568SDaniel Bristot de Oliveira #else /* CONFIG_STACKTRACE */
705b14f4568SDaniel Bristot de Oliveira #define timerlat_dump_stack(u64 latency) do {} while (0)
706a955d7eaSDaniel Bristot de Oliveira #define timerlat_save_stack(a) do {} while (0)
707a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_STACKTRACE */
708a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */
709a955d7eaSDaniel Bristot de Oliveira
710bce29ac9SDaniel Bristot de Oliveira /*
711bce29ac9SDaniel Bristot de Oliveira * Macros to encapsulate the time capturing infrastructure.
712bce29ac9SDaniel Bristot de Oliveira */
713bce29ac9SDaniel Bristot de Oliveira #define time_get() trace_clock_local()
714bce29ac9SDaniel Bristot de Oliveira #define time_to_us(x) div_u64(x, 1000)
715bce29ac9SDaniel Bristot de Oliveira #define time_sub(a, b) ((a) - (b))
716bce29ac9SDaniel Bristot de Oliveira
717bce29ac9SDaniel Bristot de Oliveira /*
718bce29ac9SDaniel Bristot de Oliveira * cond_move_irq_delta_start - Forward the delta_start of a running IRQ
719bce29ac9SDaniel Bristot de Oliveira *
720bce29ac9SDaniel Bristot de Oliveira * If an IRQ is preempted by an NMI, its delta_start is pushed forward
721bce29ac9SDaniel Bristot de Oliveira * to discount the NMI interference.
722bce29ac9SDaniel Bristot de Oliveira *
723bce29ac9SDaniel Bristot de Oliveira * See get_int_safe_duration().
724bce29ac9SDaniel Bristot de Oliveira */
725bce29ac9SDaniel Bristot de Oliveira static inline void
cond_move_irq_delta_start(struct osnoise_variables * osn_var,u64 duration)726bce29ac9SDaniel Bristot de Oliveira cond_move_irq_delta_start(struct osnoise_variables *osn_var, u64 duration)
727bce29ac9SDaniel Bristot de Oliveira {
728bce29ac9SDaniel Bristot de Oliveira if (osn_var->irq.delta_start)
729bce29ac9SDaniel Bristot de Oliveira osn_var->irq.delta_start += duration;
730bce29ac9SDaniel Bristot de Oliveira }
731bce29ac9SDaniel Bristot de Oliveira
732bce29ac9SDaniel Bristot de Oliveira #ifndef CONFIG_PREEMPT_RT
733bce29ac9SDaniel Bristot de Oliveira /*
734bce29ac9SDaniel Bristot de Oliveira * cond_move_softirq_delta_start - Forward the delta_start of a running softirq.
735bce29ac9SDaniel Bristot de Oliveira *
736bce29ac9SDaniel Bristot de Oliveira * If a softirq is preempted by an IRQ or NMI, its delta_start is pushed
737bce29ac9SDaniel Bristot de Oliveira * forward to discount the interference.
738bce29ac9SDaniel Bristot de Oliveira *
739bce29ac9SDaniel Bristot de Oliveira * See get_int_safe_duration().
740bce29ac9SDaniel Bristot de Oliveira */
741bce29ac9SDaniel Bristot de Oliveira static inline void
cond_move_softirq_delta_start(struct osnoise_variables * osn_var,u64 duration)742bce29ac9SDaniel Bristot de Oliveira cond_move_softirq_delta_start(struct osnoise_variables *osn_var, u64 duration)
743bce29ac9SDaniel Bristot de Oliveira {
744bce29ac9SDaniel Bristot de Oliveira if (osn_var->softirq.delta_start)
745bce29ac9SDaniel Bristot de Oliveira osn_var->softirq.delta_start += duration;
746bce29ac9SDaniel Bristot de Oliveira }
747bce29ac9SDaniel Bristot de Oliveira #else /* CONFIG_PREEMPT_RT */
748bce29ac9SDaniel Bristot de Oliveira #define cond_move_softirq_delta_start(osn_var, duration) do {} while (0)
749bce29ac9SDaniel Bristot de Oliveira #endif
750bce29ac9SDaniel Bristot de Oliveira
751bce29ac9SDaniel Bristot de Oliveira /*
752bce29ac9SDaniel Bristot de Oliveira * cond_move_thread_delta_start - Forward the delta_start of a running thread
753bce29ac9SDaniel Bristot de Oliveira *
754bce29ac9SDaniel Bristot de Oliveira * If a noisy thread is preempted by an softirq, IRQ or NMI, its delta_start
755bce29ac9SDaniel Bristot de Oliveira * is pushed forward to discount the interference.
756bce29ac9SDaniel Bristot de Oliveira *
757bce29ac9SDaniel Bristot de Oliveira * See get_int_safe_duration().
758bce29ac9SDaniel Bristot de Oliveira */
759bce29ac9SDaniel Bristot de Oliveira static inline void
cond_move_thread_delta_start(struct osnoise_variables * osn_var,u64 duration)760bce29ac9SDaniel Bristot de Oliveira cond_move_thread_delta_start(struct osnoise_variables *osn_var, u64 duration)
761bce29ac9SDaniel Bristot de Oliveira {
762bce29ac9SDaniel Bristot de Oliveira if (osn_var->thread.delta_start)
763bce29ac9SDaniel Bristot de Oliveira osn_var->thread.delta_start += duration;
764bce29ac9SDaniel Bristot de Oliveira }
765bce29ac9SDaniel Bristot de Oliveira
766bce29ac9SDaniel Bristot de Oliveira /*
767bce29ac9SDaniel Bristot de Oliveira * get_int_safe_duration - Get the duration of a window
768bce29ac9SDaniel Bristot de Oliveira *
769bce29ac9SDaniel Bristot de Oliveira * The irq, softirq and thread varaibles need to have its duration without
770bce29ac9SDaniel Bristot de Oliveira * the interference from higher priority interrupts. Instead of keeping a
771bce29ac9SDaniel Bristot de Oliveira * variable to discount the interrupt interference from these variables, the
772bce29ac9SDaniel Bristot de Oliveira * starting time of these variables are pushed forward with the interrupt's
773bce29ac9SDaniel Bristot de Oliveira * duration. In this way, a single variable is used to:
774bce29ac9SDaniel Bristot de Oliveira *
775bce29ac9SDaniel Bristot de Oliveira * - Know if a given window is being measured.
776bce29ac9SDaniel Bristot de Oliveira * - Account its duration.
777bce29ac9SDaniel Bristot de Oliveira * - Discount the interference.
778bce29ac9SDaniel Bristot de Oliveira *
779bce29ac9SDaniel Bristot de Oliveira * To avoid getting inconsistent values, e.g.,:
780bce29ac9SDaniel Bristot de Oliveira *
781bce29ac9SDaniel Bristot de Oliveira * now = time_get()
782bce29ac9SDaniel Bristot de Oliveira * ---> interrupt!
783bce29ac9SDaniel Bristot de Oliveira * delta_start -= int duration;
784bce29ac9SDaniel Bristot de Oliveira * <---
785bce29ac9SDaniel Bristot de Oliveira * duration = now - delta_start;
786bce29ac9SDaniel Bristot de Oliveira *
787bce29ac9SDaniel Bristot de Oliveira * result: negative duration if the variable duration before the
788bce29ac9SDaniel Bristot de Oliveira * interrupt was smaller than the interrupt execution.
789bce29ac9SDaniel Bristot de Oliveira *
790bce29ac9SDaniel Bristot de Oliveira * A counter of interrupts is used. If the counter increased, try
791bce29ac9SDaniel Bristot de Oliveira * to capture an interference safe duration.
792bce29ac9SDaniel Bristot de Oliveira */
793bce29ac9SDaniel Bristot de Oliveira static inline s64
get_int_safe_duration(struct osnoise_variables * osn_var,u64 * delta_start)794bce29ac9SDaniel Bristot de Oliveira get_int_safe_duration(struct osnoise_variables *osn_var, u64 *delta_start)
795bce29ac9SDaniel Bristot de Oliveira {
796bce29ac9SDaniel Bristot de Oliveira u64 int_counter, now;
797bce29ac9SDaniel Bristot de Oliveira s64 duration;
798bce29ac9SDaniel Bristot de Oliveira
799bce29ac9SDaniel Bristot de Oliveira do {
800bce29ac9SDaniel Bristot de Oliveira int_counter = local_read(&osn_var->int_counter);
801bce29ac9SDaniel Bristot de Oliveira /* synchronize with interrupts */
802bce29ac9SDaniel Bristot de Oliveira barrier();
803bce29ac9SDaniel Bristot de Oliveira
804bce29ac9SDaniel Bristot de Oliveira now = time_get();
805bce29ac9SDaniel Bristot de Oliveira duration = (now - *delta_start);
806bce29ac9SDaniel Bristot de Oliveira
807bce29ac9SDaniel Bristot de Oliveira /* synchronize with interrupts */
808bce29ac9SDaniel Bristot de Oliveira barrier();
809bce29ac9SDaniel Bristot de Oliveira } while (int_counter != local_read(&osn_var->int_counter));
810bce29ac9SDaniel Bristot de Oliveira
811bce29ac9SDaniel Bristot de Oliveira /*
812bce29ac9SDaniel Bristot de Oliveira * This is an evidence of race conditions that cause
813bce29ac9SDaniel Bristot de Oliveira * a value to be "discounted" too much.
814bce29ac9SDaniel Bristot de Oliveira */
815bce29ac9SDaniel Bristot de Oliveira if (duration < 0)
816bce29ac9SDaniel Bristot de Oliveira osnoise_taint("Negative duration!\n");
817bce29ac9SDaniel Bristot de Oliveira
818bce29ac9SDaniel Bristot de Oliveira *delta_start = 0;
819bce29ac9SDaniel Bristot de Oliveira
820bce29ac9SDaniel Bristot de Oliveira return duration;
821bce29ac9SDaniel Bristot de Oliveira }
822bce29ac9SDaniel Bristot de Oliveira
823bce29ac9SDaniel Bristot de Oliveira /*
824bce29ac9SDaniel Bristot de Oliveira *
825bce29ac9SDaniel Bristot de Oliveira * set_int_safe_time - Save the current time on *time, aware of interference
826bce29ac9SDaniel Bristot de Oliveira *
827bce29ac9SDaniel Bristot de Oliveira * Get the time, taking into consideration a possible interference from
828bce29ac9SDaniel Bristot de Oliveira * higher priority interrupts.
829bce29ac9SDaniel Bristot de Oliveira *
830bce29ac9SDaniel Bristot de Oliveira * See get_int_safe_duration() for an explanation.
831bce29ac9SDaniel Bristot de Oliveira */
832bce29ac9SDaniel Bristot de Oliveira static u64
set_int_safe_time(struct osnoise_variables * osn_var,u64 * time)833bce29ac9SDaniel Bristot de Oliveira set_int_safe_time(struct osnoise_variables *osn_var, u64 *time)
834bce29ac9SDaniel Bristot de Oliveira {
835bce29ac9SDaniel Bristot de Oliveira u64 int_counter;
836bce29ac9SDaniel Bristot de Oliveira
837bce29ac9SDaniel Bristot de Oliveira do {
838bce29ac9SDaniel Bristot de Oliveira int_counter = local_read(&osn_var->int_counter);
839bce29ac9SDaniel Bristot de Oliveira /* synchronize with interrupts */
840bce29ac9SDaniel Bristot de Oliveira barrier();
841bce29ac9SDaniel Bristot de Oliveira
842bce29ac9SDaniel Bristot de Oliveira *time = time_get();
843bce29ac9SDaniel Bristot de Oliveira
844bce29ac9SDaniel Bristot de Oliveira /* synchronize with interrupts */
845bce29ac9SDaniel Bristot de Oliveira barrier();
846bce29ac9SDaniel Bristot de Oliveira } while (int_counter != local_read(&osn_var->int_counter));
847bce29ac9SDaniel Bristot de Oliveira
848bce29ac9SDaniel Bristot de Oliveira return int_counter;
849bce29ac9SDaniel Bristot de Oliveira }
850bce29ac9SDaniel Bristot de Oliveira
851a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
852a955d7eaSDaniel Bristot de Oliveira /*
853a955d7eaSDaniel Bristot de Oliveira * copy_int_safe_time - Copy *src into *desc aware of interference
854a955d7eaSDaniel Bristot de Oliveira */
855a955d7eaSDaniel Bristot de Oliveira static u64
copy_int_safe_time(struct osnoise_variables * osn_var,u64 * dst,u64 * src)856a955d7eaSDaniel Bristot de Oliveira copy_int_safe_time(struct osnoise_variables *osn_var, u64 *dst, u64 *src)
857a955d7eaSDaniel Bristot de Oliveira {
858a955d7eaSDaniel Bristot de Oliveira u64 int_counter;
859a955d7eaSDaniel Bristot de Oliveira
860a955d7eaSDaniel Bristot de Oliveira do {
861a955d7eaSDaniel Bristot de Oliveira int_counter = local_read(&osn_var->int_counter);
862a955d7eaSDaniel Bristot de Oliveira /* synchronize with interrupts */
863a955d7eaSDaniel Bristot de Oliveira barrier();
864a955d7eaSDaniel Bristot de Oliveira
865a955d7eaSDaniel Bristot de Oliveira *dst = *src;
866a955d7eaSDaniel Bristot de Oliveira
867a955d7eaSDaniel Bristot de Oliveira /* synchronize with interrupts */
868a955d7eaSDaniel Bristot de Oliveira barrier();
869a955d7eaSDaniel Bristot de Oliveira } while (int_counter != local_read(&osn_var->int_counter));
870a955d7eaSDaniel Bristot de Oliveira
871a955d7eaSDaniel Bristot de Oliveira return int_counter;
872a955d7eaSDaniel Bristot de Oliveira }
873a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */
874a955d7eaSDaniel Bristot de Oliveira
875bce29ac9SDaniel Bristot de Oliveira /*
876bce29ac9SDaniel Bristot de Oliveira * trace_osnoise_callback - NMI entry/exit callback
877bce29ac9SDaniel Bristot de Oliveira *
878bce29ac9SDaniel Bristot de Oliveira * This function is called at the entry and exit NMI code. The bool enter
879bce29ac9SDaniel Bristot de Oliveira * distinguishes between either case. This function is used to note a NMI
880bce29ac9SDaniel Bristot de Oliveira * occurrence, compute the noise caused by the NMI, and to remove the noise
881bce29ac9SDaniel Bristot de Oliveira * it is potentially causing on other interference variables.
882bce29ac9SDaniel Bristot de Oliveira */
trace_osnoise_callback(bool enter)883bce29ac9SDaniel Bristot de Oliveira void trace_osnoise_callback(bool enter)
884bce29ac9SDaniel Bristot de Oliveira {
885bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var();
886bce29ac9SDaniel Bristot de Oliveira u64 duration;
887bce29ac9SDaniel Bristot de Oliveira
888bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling)
889bce29ac9SDaniel Bristot de Oliveira return;
890bce29ac9SDaniel Bristot de Oliveira
891bce29ac9SDaniel Bristot de Oliveira /*
892bce29ac9SDaniel Bristot de Oliveira * Currently trace_clock_local() calls sched_clock() and the
893bce29ac9SDaniel Bristot de Oliveira * generic version is not NMI safe.
894bce29ac9SDaniel Bristot de Oliveira */
895bce29ac9SDaniel Bristot de Oliveira if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) {
896bce29ac9SDaniel Bristot de Oliveira if (enter) {
897bce29ac9SDaniel Bristot de Oliveira osn_var->nmi.delta_start = time_get();
898bce29ac9SDaniel Bristot de Oliveira local_inc(&osn_var->int_counter);
899bce29ac9SDaniel Bristot de Oliveira } else {
900bce29ac9SDaniel Bristot de Oliveira duration = time_get() - osn_var->nmi.delta_start;
901bce29ac9SDaniel Bristot de Oliveira
902bce29ac9SDaniel Bristot de Oliveira trace_nmi_noise(osn_var->nmi.delta_start, duration);
903bce29ac9SDaniel Bristot de Oliveira
904bce29ac9SDaniel Bristot de Oliveira cond_move_irq_delta_start(osn_var, duration);
905bce29ac9SDaniel Bristot de Oliveira cond_move_softirq_delta_start(osn_var, duration);
906bce29ac9SDaniel Bristot de Oliveira cond_move_thread_delta_start(osn_var, duration);
907bce29ac9SDaniel Bristot de Oliveira }
908bce29ac9SDaniel Bristot de Oliveira }
909bce29ac9SDaniel Bristot de Oliveira
910bce29ac9SDaniel Bristot de Oliveira if (enter)
911bce29ac9SDaniel Bristot de Oliveira osn_var->nmi.count++;
912bce29ac9SDaniel Bristot de Oliveira }
913bce29ac9SDaniel Bristot de Oliveira
914bce29ac9SDaniel Bristot de Oliveira /*
915bce29ac9SDaniel Bristot de Oliveira * osnoise_trace_irq_entry - Note the starting of an IRQ
916bce29ac9SDaniel Bristot de Oliveira *
917bce29ac9SDaniel Bristot de Oliveira * Save the starting time of an IRQ. As IRQs are non-preemptive to other IRQs,
918bce29ac9SDaniel Bristot de Oliveira * it is safe to use a single variable (ons_var->irq) to save the statistics.
919bce29ac9SDaniel Bristot de Oliveira * The arrival_time is used to report... the arrival time. The delta_start
920bce29ac9SDaniel Bristot de Oliveira * is used to compute the duration at the IRQ exit handler. See
921bce29ac9SDaniel Bristot de Oliveira * cond_move_irq_delta_start().
922bce29ac9SDaniel Bristot de Oliveira */
osnoise_trace_irq_entry(int id)923bce29ac9SDaniel Bristot de Oliveira void osnoise_trace_irq_entry(int id)
924bce29ac9SDaniel Bristot de Oliveira {
925bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var();
926bce29ac9SDaniel Bristot de Oliveira
927bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling)
928bce29ac9SDaniel Bristot de Oliveira return;
929bce29ac9SDaniel Bristot de Oliveira /*
930bce29ac9SDaniel Bristot de Oliveira * This value will be used in the report, but not to compute
931bce29ac9SDaniel Bristot de Oliveira * the execution time, so it is safe to get it unsafe.
932bce29ac9SDaniel Bristot de Oliveira */
933bce29ac9SDaniel Bristot de Oliveira osn_var->irq.arrival_time = time_get();
934bce29ac9SDaniel Bristot de Oliveira set_int_safe_time(osn_var, &osn_var->irq.delta_start);
935bce29ac9SDaniel Bristot de Oliveira osn_var->irq.count++;
936bce29ac9SDaniel Bristot de Oliveira
937bce29ac9SDaniel Bristot de Oliveira local_inc(&osn_var->int_counter);
938bce29ac9SDaniel Bristot de Oliveira }
939bce29ac9SDaniel Bristot de Oliveira
940bce29ac9SDaniel Bristot de Oliveira /*
941bce29ac9SDaniel Bristot de Oliveira * osnoise_irq_exit - Note the end of an IRQ, sava data and trace
942bce29ac9SDaniel Bristot de Oliveira *
943bce29ac9SDaniel Bristot de Oliveira * Computes the duration of the IRQ noise, and trace it. Also discounts the
944bce29ac9SDaniel Bristot de Oliveira * interference from other sources of noise could be currently being accounted.
945bce29ac9SDaniel Bristot de Oliveira */
osnoise_trace_irq_exit(int id,const char * desc)946bce29ac9SDaniel Bristot de Oliveira void osnoise_trace_irq_exit(int id, const char *desc)
947bce29ac9SDaniel Bristot de Oliveira {
948bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var();
949022632f6SDaniel Bristot de Oliveira s64 duration;
950bce29ac9SDaniel Bristot de Oliveira
951bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling)
952bce29ac9SDaniel Bristot de Oliveira return;
953bce29ac9SDaniel Bristot de Oliveira
954bce29ac9SDaniel Bristot de Oliveira duration = get_int_safe_duration(osn_var, &osn_var->irq.delta_start);
955bce29ac9SDaniel Bristot de Oliveira trace_irq_noise(id, desc, osn_var->irq.arrival_time, duration);
956bce29ac9SDaniel Bristot de Oliveira osn_var->irq.arrival_time = 0;
957bce29ac9SDaniel Bristot de Oliveira cond_move_softirq_delta_start(osn_var, duration);
958bce29ac9SDaniel Bristot de Oliveira cond_move_thread_delta_start(osn_var, duration);
959bce29ac9SDaniel Bristot de Oliveira }
960bce29ac9SDaniel Bristot de Oliveira
961bce29ac9SDaniel Bristot de Oliveira /*
962bce29ac9SDaniel Bristot de Oliveira * trace_irqentry_callback - Callback to the irq:irq_entry traceevent
963bce29ac9SDaniel Bristot de Oliveira *
964bce29ac9SDaniel Bristot de Oliveira * Used to note the starting of an IRQ occurece.
965bce29ac9SDaniel Bristot de Oliveira */
trace_irqentry_callback(void * data,int irq,struct irqaction * action)966bce29ac9SDaniel Bristot de Oliveira static void trace_irqentry_callback(void *data, int irq,
967bce29ac9SDaniel Bristot de Oliveira struct irqaction *action)
968bce29ac9SDaniel Bristot de Oliveira {
969bce29ac9SDaniel Bristot de Oliveira osnoise_trace_irq_entry(irq);
970bce29ac9SDaniel Bristot de Oliveira }
971bce29ac9SDaniel Bristot de Oliveira
972bce29ac9SDaniel Bristot de Oliveira /*
973bce29ac9SDaniel Bristot de Oliveira * trace_irqexit_callback - Callback to the irq:irq_exit traceevent
974bce29ac9SDaniel Bristot de Oliveira *
975bce29ac9SDaniel Bristot de Oliveira * Used to note the end of an IRQ occurece.
976bce29ac9SDaniel Bristot de Oliveira */
trace_irqexit_callback(void * data,int irq,struct irqaction * action,int ret)977bce29ac9SDaniel Bristot de Oliveira static void trace_irqexit_callback(void *data, int irq,
978bce29ac9SDaniel Bristot de Oliveira struct irqaction *action, int ret)
979bce29ac9SDaniel Bristot de Oliveira {
980bce29ac9SDaniel Bristot de Oliveira osnoise_trace_irq_exit(irq, action->name);
981bce29ac9SDaniel Bristot de Oliveira }
982bce29ac9SDaniel Bristot de Oliveira
983bce29ac9SDaniel Bristot de Oliveira /*
984bce29ac9SDaniel Bristot de Oliveira * arch specific register function.
985bce29ac9SDaniel Bristot de Oliveira */
osnoise_arch_register(void)986bce29ac9SDaniel Bristot de Oliveira int __weak osnoise_arch_register(void)
987bce29ac9SDaniel Bristot de Oliveira {
988bce29ac9SDaniel Bristot de Oliveira return 0;
989bce29ac9SDaniel Bristot de Oliveira }
990bce29ac9SDaniel Bristot de Oliveira
991bce29ac9SDaniel Bristot de Oliveira /*
992bce29ac9SDaniel Bristot de Oliveira * arch specific unregister function.
993bce29ac9SDaniel Bristot de Oliveira */
osnoise_arch_unregister(void)994bce29ac9SDaniel Bristot de Oliveira void __weak osnoise_arch_unregister(void)
995bce29ac9SDaniel Bristot de Oliveira {
996bce29ac9SDaniel Bristot de Oliveira return;
997bce29ac9SDaniel Bristot de Oliveira }
998bce29ac9SDaniel Bristot de Oliveira
999bce29ac9SDaniel Bristot de Oliveira /*
1000bce29ac9SDaniel Bristot de Oliveira * hook_irq_events - Hook IRQ handling events
1001bce29ac9SDaniel Bristot de Oliveira *
1002bce29ac9SDaniel Bristot de Oliveira * This function hooks the IRQ related callbacks to the respective trace
1003bce29ac9SDaniel Bristot de Oliveira * events.
1004bce29ac9SDaniel Bristot de Oliveira */
hook_irq_events(void)1005f7d9f637SDaniel Bristot de Oliveira static int hook_irq_events(void)
1006bce29ac9SDaniel Bristot de Oliveira {
1007bce29ac9SDaniel Bristot de Oliveira int ret;
1008bce29ac9SDaniel Bristot de Oliveira
1009bce29ac9SDaniel Bristot de Oliveira ret = register_trace_irq_handler_entry(trace_irqentry_callback, NULL);
1010bce29ac9SDaniel Bristot de Oliveira if (ret)
1011bce29ac9SDaniel Bristot de Oliveira goto out_err;
1012bce29ac9SDaniel Bristot de Oliveira
1013bce29ac9SDaniel Bristot de Oliveira ret = register_trace_irq_handler_exit(trace_irqexit_callback, NULL);
1014bce29ac9SDaniel Bristot de Oliveira if (ret)
1015bce29ac9SDaniel Bristot de Oliveira goto out_unregister_entry;
1016bce29ac9SDaniel Bristot de Oliveira
1017bce29ac9SDaniel Bristot de Oliveira ret = osnoise_arch_register();
1018bce29ac9SDaniel Bristot de Oliveira if (ret)
1019bce29ac9SDaniel Bristot de Oliveira goto out_irq_exit;
1020bce29ac9SDaniel Bristot de Oliveira
1021bce29ac9SDaniel Bristot de Oliveira return 0;
1022bce29ac9SDaniel Bristot de Oliveira
1023bce29ac9SDaniel Bristot de Oliveira out_irq_exit:
1024bce29ac9SDaniel Bristot de Oliveira unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL);
1025bce29ac9SDaniel Bristot de Oliveira out_unregister_entry:
1026bce29ac9SDaniel Bristot de Oliveira unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL);
1027bce29ac9SDaniel Bristot de Oliveira out_err:
1028bce29ac9SDaniel Bristot de Oliveira return -EINVAL;
1029bce29ac9SDaniel Bristot de Oliveira }
1030bce29ac9SDaniel Bristot de Oliveira
1031bce29ac9SDaniel Bristot de Oliveira /*
1032bce29ac9SDaniel Bristot de Oliveira * unhook_irq_events - Unhook IRQ handling events
1033bce29ac9SDaniel Bristot de Oliveira *
1034bce29ac9SDaniel Bristot de Oliveira * This function unhooks the IRQ related callbacks to the respective trace
1035bce29ac9SDaniel Bristot de Oliveira * events.
1036bce29ac9SDaniel Bristot de Oliveira */
unhook_irq_events(void)1037f7d9f637SDaniel Bristot de Oliveira static void unhook_irq_events(void)
1038bce29ac9SDaniel Bristot de Oliveira {
1039bce29ac9SDaniel Bristot de Oliveira osnoise_arch_unregister();
1040bce29ac9SDaniel Bristot de Oliveira unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL);
1041bce29ac9SDaniel Bristot de Oliveira unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL);
1042bce29ac9SDaniel Bristot de Oliveira }
1043bce29ac9SDaniel Bristot de Oliveira
1044bce29ac9SDaniel Bristot de Oliveira #ifndef CONFIG_PREEMPT_RT
1045bce29ac9SDaniel Bristot de Oliveira /*
1046bce29ac9SDaniel Bristot de Oliveira * trace_softirq_entry_callback - Note the starting of a softirq
1047bce29ac9SDaniel Bristot de Oliveira *
1048bce29ac9SDaniel Bristot de Oliveira * Save the starting time of a softirq. As softirqs are non-preemptive to
1049bce29ac9SDaniel Bristot de Oliveira * other softirqs, it is safe to use a single variable (ons_var->softirq)
1050bce29ac9SDaniel Bristot de Oliveira * to save the statistics. The arrival_time is used to report... the
1051bce29ac9SDaniel Bristot de Oliveira * arrival time. The delta_start is used to compute the duration at the
1052bce29ac9SDaniel Bristot de Oliveira * softirq exit handler. See cond_move_softirq_delta_start().
1053bce29ac9SDaniel Bristot de Oliveira */
trace_softirq_entry_callback(void * data,unsigned int vec_nr)1054f7d9f637SDaniel Bristot de Oliveira static void trace_softirq_entry_callback(void *data, unsigned int vec_nr)
1055bce29ac9SDaniel Bristot de Oliveira {
1056bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var();
1057bce29ac9SDaniel Bristot de Oliveira
1058bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling)
1059bce29ac9SDaniel Bristot de Oliveira return;
1060bce29ac9SDaniel Bristot de Oliveira /*
1061bce29ac9SDaniel Bristot de Oliveira * This value will be used in the report, but not to compute
1062bce29ac9SDaniel Bristot de Oliveira * the execution time, so it is safe to get it unsafe.
1063bce29ac9SDaniel Bristot de Oliveira */
1064bce29ac9SDaniel Bristot de Oliveira osn_var->softirq.arrival_time = time_get();
1065bce29ac9SDaniel Bristot de Oliveira set_int_safe_time(osn_var, &osn_var->softirq.delta_start);
1066bce29ac9SDaniel Bristot de Oliveira osn_var->softirq.count++;
1067bce29ac9SDaniel Bristot de Oliveira
1068bce29ac9SDaniel Bristot de Oliveira local_inc(&osn_var->int_counter);
1069bce29ac9SDaniel Bristot de Oliveira }
1070bce29ac9SDaniel Bristot de Oliveira
1071bce29ac9SDaniel Bristot de Oliveira /*
1072bce29ac9SDaniel Bristot de Oliveira * trace_softirq_exit_callback - Note the end of an softirq
1073bce29ac9SDaniel Bristot de Oliveira *
1074bce29ac9SDaniel Bristot de Oliveira * Computes the duration of the softirq noise, and trace it. Also discounts the
1075bce29ac9SDaniel Bristot de Oliveira * interference from other sources of noise could be currently being accounted.
1076bce29ac9SDaniel Bristot de Oliveira */
trace_softirq_exit_callback(void * data,unsigned int vec_nr)1077f7d9f637SDaniel Bristot de Oliveira static void trace_softirq_exit_callback(void *data, unsigned int vec_nr)
1078bce29ac9SDaniel Bristot de Oliveira {
1079bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var();
1080022632f6SDaniel Bristot de Oliveira s64 duration;
1081bce29ac9SDaniel Bristot de Oliveira
1082bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling)
1083bce29ac9SDaniel Bristot de Oliveira return;
1084bce29ac9SDaniel Bristot de Oliveira
1085ccb67544SDaniel Bristot de Oliveira if (unlikely(timerlat_enabled()))
1086ccb67544SDaniel Bristot de Oliveira if (!timerlat_softirq_exit(osn_var))
1087a955d7eaSDaniel Bristot de Oliveira return;
1088a955d7eaSDaniel Bristot de Oliveira
1089bce29ac9SDaniel Bristot de Oliveira duration = get_int_safe_duration(osn_var, &osn_var->softirq.delta_start);
1090bce29ac9SDaniel Bristot de Oliveira trace_softirq_noise(vec_nr, osn_var->softirq.arrival_time, duration);
1091bce29ac9SDaniel Bristot de Oliveira cond_move_thread_delta_start(osn_var, duration);
1092bce29ac9SDaniel Bristot de Oliveira osn_var->softirq.arrival_time = 0;
1093bce29ac9SDaniel Bristot de Oliveira }
1094bce29ac9SDaniel Bristot de Oliveira
1095bce29ac9SDaniel Bristot de Oliveira /*
1096bce29ac9SDaniel Bristot de Oliveira * hook_softirq_events - Hook softirq handling events
1097bce29ac9SDaniel Bristot de Oliveira *
1098bce29ac9SDaniel Bristot de Oliveira * This function hooks the softirq related callbacks to the respective trace
1099bce29ac9SDaniel Bristot de Oliveira * events.
1100bce29ac9SDaniel Bristot de Oliveira */
hook_softirq_events(void)1101bce29ac9SDaniel Bristot de Oliveira static int hook_softirq_events(void)
1102bce29ac9SDaniel Bristot de Oliveira {
1103bce29ac9SDaniel Bristot de Oliveira int ret;
1104bce29ac9SDaniel Bristot de Oliveira
1105bce29ac9SDaniel Bristot de Oliveira ret = register_trace_softirq_entry(trace_softirq_entry_callback, NULL);
1106bce29ac9SDaniel Bristot de Oliveira if (ret)
1107bce29ac9SDaniel Bristot de Oliveira goto out_err;
1108bce29ac9SDaniel Bristot de Oliveira
1109bce29ac9SDaniel Bristot de Oliveira ret = register_trace_softirq_exit(trace_softirq_exit_callback, NULL);
1110bce29ac9SDaniel Bristot de Oliveira if (ret)
1111bce29ac9SDaniel Bristot de Oliveira goto out_unreg_entry;
1112bce29ac9SDaniel Bristot de Oliveira
1113bce29ac9SDaniel Bristot de Oliveira return 0;
1114bce29ac9SDaniel Bristot de Oliveira
1115bce29ac9SDaniel Bristot de Oliveira out_unreg_entry:
1116bce29ac9SDaniel Bristot de Oliveira unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL);
1117bce29ac9SDaniel Bristot de Oliveira out_err:
1118bce29ac9SDaniel Bristot de Oliveira return -EINVAL;
1119bce29ac9SDaniel Bristot de Oliveira }
1120bce29ac9SDaniel Bristot de Oliveira
1121bce29ac9SDaniel Bristot de Oliveira /*
1122bce29ac9SDaniel Bristot de Oliveira * unhook_softirq_events - Unhook softirq handling events
1123bce29ac9SDaniel Bristot de Oliveira *
1124bce29ac9SDaniel Bristot de Oliveira * This function hooks the softirq related callbacks to the respective trace
1125bce29ac9SDaniel Bristot de Oliveira * events.
1126bce29ac9SDaniel Bristot de Oliveira */
unhook_softirq_events(void)1127bce29ac9SDaniel Bristot de Oliveira static void unhook_softirq_events(void)
1128bce29ac9SDaniel Bristot de Oliveira {
1129bce29ac9SDaniel Bristot de Oliveira unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL);
1130bce29ac9SDaniel Bristot de Oliveira unregister_trace_softirq_exit(trace_softirq_exit_callback, NULL);
1131bce29ac9SDaniel Bristot de Oliveira }
1132bce29ac9SDaniel Bristot de Oliveira #else /* CONFIG_PREEMPT_RT */
1133bce29ac9SDaniel Bristot de Oliveira /*
1134bce29ac9SDaniel Bristot de Oliveira * softirq are threads on the PREEMPT_RT mode.
1135bce29ac9SDaniel Bristot de Oliveira */
hook_softirq_events(void)1136bce29ac9SDaniel Bristot de Oliveira static int hook_softirq_events(void)
1137bce29ac9SDaniel Bristot de Oliveira {
1138bce29ac9SDaniel Bristot de Oliveira return 0;
1139bce29ac9SDaniel Bristot de Oliveira }
unhook_softirq_events(void)1140bce29ac9SDaniel Bristot de Oliveira static void unhook_softirq_events(void)
1141bce29ac9SDaniel Bristot de Oliveira {
1142bce29ac9SDaniel Bristot de Oliveira }
1143bce29ac9SDaniel Bristot de Oliveira #endif
1144bce29ac9SDaniel Bristot de Oliveira
1145bce29ac9SDaniel Bristot de Oliveira /*
1146bce29ac9SDaniel Bristot de Oliveira * thread_entry - Record the starting of a thread noise window
1147bce29ac9SDaniel Bristot de Oliveira *
1148bce29ac9SDaniel Bristot de Oliveira * It saves the context switch time for a noisy thread, and increments
1149bce29ac9SDaniel Bristot de Oliveira * the interference counters.
1150bce29ac9SDaniel Bristot de Oliveira */
1151bce29ac9SDaniel Bristot de Oliveira static void
thread_entry(struct osnoise_variables * osn_var,struct task_struct * t)1152bce29ac9SDaniel Bristot de Oliveira thread_entry(struct osnoise_variables *osn_var, struct task_struct *t)
1153bce29ac9SDaniel Bristot de Oliveira {
1154bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling)
1155bce29ac9SDaniel Bristot de Oliveira return;
1156bce29ac9SDaniel Bristot de Oliveira /*
1157bce29ac9SDaniel Bristot de Oliveira * The arrival time will be used in the report, but not to compute
1158bce29ac9SDaniel Bristot de Oliveira * the execution time, so it is safe to get it unsafe.
1159bce29ac9SDaniel Bristot de Oliveira */
1160bce29ac9SDaniel Bristot de Oliveira osn_var->thread.arrival_time = time_get();
1161bce29ac9SDaniel Bristot de Oliveira
1162bce29ac9SDaniel Bristot de Oliveira set_int_safe_time(osn_var, &osn_var->thread.delta_start);
1163bce29ac9SDaniel Bristot de Oliveira
1164bce29ac9SDaniel Bristot de Oliveira osn_var->thread.count++;
1165bce29ac9SDaniel Bristot de Oliveira local_inc(&osn_var->int_counter);
1166bce29ac9SDaniel Bristot de Oliveira }
1167bce29ac9SDaniel Bristot de Oliveira
1168bce29ac9SDaniel Bristot de Oliveira /*
1169bce29ac9SDaniel Bristot de Oliveira * thread_exit - Report the end of a thread noise window
1170bce29ac9SDaniel Bristot de Oliveira *
1171bce29ac9SDaniel Bristot de Oliveira * It computes the total noise from a thread, tracing if needed.
1172bce29ac9SDaniel Bristot de Oliveira */
1173bce29ac9SDaniel Bristot de Oliveira static void
thread_exit(struct osnoise_variables * osn_var,struct task_struct * t)1174bce29ac9SDaniel Bristot de Oliveira thread_exit(struct osnoise_variables *osn_var, struct task_struct *t)
1175bce29ac9SDaniel Bristot de Oliveira {
1176022632f6SDaniel Bristot de Oliveira s64 duration;
1177bce29ac9SDaniel Bristot de Oliveira
1178bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling)
1179bce29ac9SDaniel Bristot de Oliveira return;
1180bce29ac9SDaniel Bristot de Oliveira
1181ccb67544SDaniel Bristot de Oliveira if (unlikely(timerlat_enabled()))
1182ccb67544SDaniel Bristot de Oliveira if (!timerlat_thread_exit(osn_var))
1183a955d7eaSDaniel Bristot de Oliveira return;
1184a955d7eaSDaniel Bristot de Oliveira
1185bce29ac9SDaniel Bristot de Oliveira duration = get_int_safe_duration(osn_var, &osn_var->thread.delta_start);
1186bce29ac9SDaniel Bristot de Oliveira
1187bce29ac9SDaniel Bristot de Oliveira trace_thread_noise(t, osn_var->thread.arrival_time, duration);
1188bce29ac9SDaniel Bristot de Oliveira
1189bce29ac9SDaniel Bristot de Oliveira osn_var->thread.arrival_time = 0;
1190bce29ac9SDaniel Bristot de Oliveira }
1191bce29ac9SDaniel Bristot de Oliveira
1192e88ed227SDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
1193e88ed227SDaniel Bristot de Oliveira /*
1194e88ed227SDaniel Bristot de Oliveira * osnoise_stop_exception - Stop tracing and the tracer.
1195e88ed227SDaniel Bristot de Oliveira */
osnoise_stop_exception(char * msg,int cpu)1196e88ed227SDaniel Bristot de Oliveira static __always_inline void osnoise_stop_exception(char *msg, int cpu)
1197e88ed227SDaniel Bristot de Oliveira {
1198e88ed227SDaniel Bristot de Oliveira struct osnoise_instance *inst;
1199e88ed227SDaniel Bristot de Oliveira struct trace_array *tr;
1200e88ed227SDaniel Bristot de Oliveira
1201e88ed227SDaniel Bristot de Oliveira rcu_read_lock();
1202e88ed227SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) {
1203e88ed227SDaniel Bristot de Oliveira tr = inst->tr;
1204e88ed227SDaniel Bristot de Oliveira trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_,
1205e88ed227SDaniel Bristot de Oliveira "stop tracing hit on cpu %d due to exception: %s\n",
1206e88ed227SDaniel Bristot de Oliveira smp_processor_id(),
1207e88ed227SDaniel Bristot de Oliveira msg);
1208e88ed227SDaniel Bristot de Oliveira
1209e88ed227SDaniel Bristot de Oliveira if (test_bit(OSN_PANIC_ON_STOP, &osnoise_options))
1210e88ed227SDaniel Bristot de Oliveira panic("tracer hit on cpu %d due to exception: %s\n",
1211e88ed227SDaniel Bristot de Oliveira smp_processor_id(),
1212e88ed227SDaniel Bristot de Oliveira msg);
1213e88ed227SDaniel Bristot de Oliveira
1214e88ed227SDaniel Bristot de Oliveira tracer_tracing_off(tr);
1215e88ed227SDaniel Bristot de Oliveira }
1216e88ed227SDaniel Bristot de Oliveira rcu_read_unlock();
1217e88ed227SDaniel Bristot de Oliveira }
1218e88ed227SDaniel Bristot de Oliveira
1219e88ed227SDaniel Bristot de Oliveira /*
1220e88ed227SDaniel Bristot de Oliveira * trace_sched_migrate_callback - sched:sched_migrate_task trace event handler
1221e88ed227SDaniel Bristot de Oliveira *
1222e88ed227SDaniel Bristot de Oliveira * his function is hooked to the sched:sched_migrate_task trace event, and monitors
1223e88ed227SDaniel Bristot de Oliveira * timerlat user-space thread migration.
1224e88ed227SDaniel Bristot de Oliveira */
trace_sched_migrate_callback(void * data,struct task_struct * p,int dest_cpu)1225e88ed227SDaniel Bristot de Oliveira static void trace_sched_migrate_callback(void *data, struct task_struct *p, int dest_cpu)
1226e88ed227SDaniel Bristot de Oliveira {
1227e88ed227SDaniel Bristot de Oliveira struct osnoise_variables *osn_var;
1228e88ed227SDaniel Bristot de Oliveira long cpu = task_cpu(p);
1229e88ed227SDaniel Bristot de Oliveira
1230e88ed227SDaniel Bristot de Oliveira osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
1231e88ed227SDaniel Bristot de Oliveira if (osn_var->pid == p->pid && dest_cpu != cpu) {
1232e88ed227SDaniel Bristot de Oliveira per_cpu_ptr(&per_cpu_timerlat_var, cpu)->uthread_migrate = 1;
1233e88ed227SDaniel Bristot de Oliveira osnoise_taint("timerlat user-thread migrated\n");
1234e88ed227SDaniel Bristot de Oliveira osnoise_stop_exception("timerlat user-thread migrated", cpu);
1235e88ed227SDaniel Bristot de Oliveira }
1236e88ed227SDaniel Bristot de Oliveira }
1237e88ed227SDaniel Bristot de Oliveira
register_migration_monitor(void)1238e88ed227SDaniel Bristot de Oliveira static int register_migration_monitor(void)
1239e88ed227SDaniel Bristot de Oliveira {
1240e88ed227SDaniel Bristot de Oliveira int ret = 0;
1241e88ed227SDaniel Bristot de Oliveira
1242e88ed227SDaniel Bristot de Oliveira /*
1243e88ed227SDaniel Bristot de Oliveira * Timerlat thread migration check is only required when running timerlat in user-space.
1244e88ed227SDaniel Bristot de Oliveira * Thus, enable callback only if timerlat is set with no workload.
1245e88ed227SDaniel Bristot de Oliveira */
1246e88ed227SDaniel Bristot de Oliveira if (timerlat_enabled() && !test_bit(OSN_WORKLOAD, &osnoise_options))
1247e88ed227SDaniel Bristot de Oliveira ret = register_trace_sched_migrate_task(trace_sched_migrate_callback, NULL);
1248e88ed227SDaniel Bristot de Oliveira
1249e88ed227SDaniel Bristot de Oliveira return ret;
1250e88ed227SDaniel Bristot de Oliveira }
1251e88ed227SDaniel Bristot de Oliveira
unregister_migration_monitor(void)1252e88ed227SDaniel Bristot de Oliveira static void unregister_migration_monitor(void)
1253e88ed227SDaniel Bristot de Oliveira {
1254e88ed227SDaniel Bristot de Oliveira if (timerlat_enabled() && !test_bit(OSN_WORKLOAD, &osnoise_options))
1255e88ed227SDaniel Bristot de Oliveira unregister_trace_sched_migrate_task(trace_sched_migrate_callback, NULL);
1256e88ed227SDaniel Bristot de Oliveira }
1257e88ed227SDaniel Bristot de Oliveira #else
register_migration_monitor(void)1258e88ed227SDaniel Bristot de Oliveira static int register_migration_monitor(void)
1259e88ed227SDaniel Bristot de Oliveira {
1260e88ed227SDaniel Bristot de Oliveira return 0;
1261e88ed227SDaniel Bristot de Oliveira }
unregister_migration_monitor(void)1262e88ed227SDaniel Bristot de Oliveira static void unregister_migration_monitor(void) {}
1263e88ed227SDaniel Bristot de Oliveira #endif
1264bce29ac9SDaniel Bristot de Oliveira /*
1265bce29ac9SDaniel Bristot de Oliveira * trace_sched_switch - sched:sched_switch trace event handler
1266bce29ac9SDaniel Bristot de Oliveira *
1267bce29ac9SDaniel Bristot de Oliveira * This function is hooked to the sched:sched_switch trace event, and it is
1268bce29ac9SDaniel Bristot de Oliveira * used to record the beginning and to report the end of a thread noise window.
1269bce29ac9SDaniel Bristot de Oliveira */
1270f7d9f637SDaniel Bristot de Oliveira static void
trace_sched_switch_callback(void * data,bool preempt,struct task_struct * p,struct task_struct * n,unsigned int prev_state)1271fa2c3254SValentin Schneider trace_sched_switch_callback(void *data, bool preempt,
1272fa2c3254SValentin Schneider struct task_struct *p,
12739c2136beSDelyan Kratunov struct task_struct *n,
12749c2136beSDelyan Kratunov unsigned int prev_state)
1275bce29ac9SDaniel Bristot de Oliveira {
1276bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var();
127730838fcdSDaniel Bristot de Oliveira int workload = test_bit(OSN_WORKLOAD, &osnoise_options);
1278bce29ac9SDaniel Bristot de Oliveira
127930838fcdSDaniel Bristot de Oliveira if ((p->pid != osn_var->pid) || !workload)
1280bce29ac9SDaniel Bristot de Oliveira thread_exit(osn_var, p);
1281bce29ac9SDaniel Bristot de Oliveira
128230838fcdSDaniel Bristot de Oliveira if ((n->pid != osn_var->pid) || !workload)
1283bce29ac9SDaniel Bristot de Oliveira thread_entry(osn_var, n);
1284bce29ac9SDaniel Bristot de Oliveira }
1285bce29ac9SDaniel Bristot de Oliveira
1286bce29ac9SDaniel Bristot de Oliveira /*
1287e88ed227SDaniel Bristot de Oliveira * hook_thread_events - Hook the instrumentation for thread noise
1288bce29ac9SDaniel Bristot de Oliveira *
1289bce29ac9SDaniel Bristot de Oliveira * Hook the osnoise tracer callbacks to handle the noise from other
1290bce29ac9SDaniel Bristot de Oliveira * threads on the necessary kernel events.
1291bce29ac9SDaniel Bristot de Oliveira */
hook_thread_events(void)1292f7d9f637SDaniel Bristot de Oliveira static int hook_thread_events(void)
1293bce29ac9SDaniel Bristot de Oliveira {
1294bce29ac9SDaniel Bristot de Oliveira int ret;
1295bce29ac9SDaniel Bristot de Oliveira
1296bce29ac9SDaniel Bristot de Oliveira ret = register_trace_sched_switch(trace_sched_switch_callback, NULL);
1297bce29ac9SDaniel Bristot de Oliveira if (ret)
1298bce29ac9SDaniel Bristot de Oliveira return -EINVAL;
1299bce29ac9SDaniel Bristot de Oliveira
1300e88ed227SDaniel Bristot de Oliveira ret = register_migration_monitor();
1301e88ed227SDaniel Bristot de Oliveira if (ret)
1302e88ed227SDaniel Bristot de Oliveira goto out_unreg;
1303e88ed227SDaniel Bristot de Oliveira
1304bce29ac9SDaniel Bristot de Oliveira return 0;
1305e88ed227SDaniel Bristot de Oliveira
1306e88ed227SDaniel Bristot de Oliveira out_unreg:
1307e88ed227SDaniel Bristot de Oliveira unregister_trace_sched_switch(trace_sched_switch_callback, NULL);
1308e88ed227SDaniel Bristot de Oliveira return -EINVAL;
1309bce29ac9SDaniel Bristot de Oliveira }
1310bce29ac9SDaniel Bristot de Oliveira
1311bce29ac9SDaniel Bristot de Oliveira /*
1312e88ed227SDaniel Bristot de Oliveira * unhook_thread_events - unhook the instrumentation for thread noise
1313bce29ac9SDaniel Bristot de Oliveira *
1314bce29ac9SDaniel Bristot de Oliveira * Unook the osnoise tracer callbacks to handle the noise from other
1315bce29ac9SDaniel Bristot de Oliveira * threads on the necessary kernel events.
1316bce29ac9SDaniel Bristot de Oliveira */
unhook_thread_events(void)1317f7d9f637SDaniel Bristot de Oliveira static void unhook_thread_events(void)
1318bce29ac9SDaniel Bristot de Oliveira {
1319bce29ac9SDaniel Bristot de Oliveira unregister_trace_sched_switch(trace_sched_switch_callback, NULL);
1320e88ed227SDaniel Bristot de Oliveira unregister_migration_monitor();
1321bce29ac9SDaniel Bristot de Oliveira }
1322bce29ac9SDaniel Bristot de Oliveira
1323bce29ac9SDaniel Bristot de Oliveira /*
1324bce29ac9SDaniel Bristot de Oliveira * save_osn_sample_stats - Save the osnoise_sample statistics
1325bce29ac9SDaniel Bristot de Oliveira *
1326bce29ac9SDaniel Bristot de Oliveira * Save the osnoise_sample statistics before the sampling phase. These
1327bce29ac9SDaniel Bristot de Oliveira * values will be used later to compute the diff betwneen the statistics
1328bce29ac9SDaniel Bristot de Oliveira * before and after the osnoise sampling.
1329bce29ac9SDaniel Bristot de Oliveira */
1330f7d9f637SDaniel Bristot de Oliveira static void
save_osn_sample_stats(struct osnoise_variables * osn_var,struct osnoise_sample * s)1331f7d9f637SDaniel Bristot de Oliveira save_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s)
1332bce29ac9SDaniel Bristot de Oliveira {
1333bce29ac9SDaniel Bristot de Oliveira s->nmi_count = osn_var->nmi.count;
1334bce29ac9SDaniel Bristot de Oliveira s->irq_count = osn_var->irq.count;
1335bce29ac9SDaniel Bristot de Oliveira s->softirq_count = osn_var->softirq.count;
1336bce29ac9SDaniel Bristot de Oliveira s->thread_count = osn_var->thread.count;
1337bce29ac9SDaniel Bristot de Oliveira }
1338bce29ac9SDaniel Bristot de Oliveira
1339bce29ac9SDaniel Bristot de Oliveira /*
1340bce29ac9SDaniel Bristot de Oliveira * diff_osn_sample_stats - Compute the osnoise_sample statistics
1341bce29ac9SDaniel Bristot de Oliveira *
1342bce29ac9SDaniel Bristot de Oliveira * After a sample period, compute the difference on the osnoise_sample
1343bce29ac9SDaniel Bristot de Oliveira * statistics. The struct osnoise_sample *s contains the statistics saved via
1344bce29ac9SDaniel Bristot de Oliveira * save_osn_sample_stats() before the osnoise sampling.
1345bce29ac9SDaniel Bristot de Oliveira */
1346f7d9f637SDaniel Bristot de Oliveira static void
diff_osn_sample_stats(struct osnoise_variables * osn_var,struct osnoise_sample * s)1347f7d9f637SDaniel Bristot de Oliveira diff_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s)
1348bce29ac9SDaniel Bristot de Oliveira {
1349bce29ac9SDaniel Bristot de Oliveira s->nmi_count = osn_var->nmi.count - s->nmi_count;
1350bce29ac9SDaniel Bristot de Oliveira s->irq_count = osn_var->irq.count - s->irq_count;
1351bce29ac9SDaniel Bristot de Oliveira s->softirq_count = osn_var->softirq.count - s->softirq_count;
1352bce29ac9SDaniel Bristot de Oliveira s->thread_count = osn_var->thread.count - s->thread_count;
1353bce29ac9SDaniel Bristot de Oliveira }
1354bce29ac9SDaniel Bristot de Oliveira
1355bce29ac9SDaniel Bristot de Oliveira /*
1356bce29ac9SDaniel Bristot de Oliveira * osnoise_stop_tracing - Stop tracing and the tracer.
1357bce29ac9SDaniel Bristot de Oliveira */
osnoise_stop_tracing(void)13580e05ba49SDaniel Bristot de Oliveira static __always_inline void osnoise_stop_tracing(void)
1359bce29ac9SDaniel Bristot de Oliveira {
1360dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst;
1361dae18134SDaniel Bristot de Oliveira struct trace_array *tr;
13620e05ba49SDaniel Bristot de Oliveira
1363dae18134SDaniel Bristot de Oliveira rcu_read_lock();
1364dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) {
1365dae18134SDaniel Bristot de Oliveira tr = inst->tr;
13660e05ba49SDaniel Bristot de Oliveira trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_,
13670e05ba49SDaniel Bristot de Oliveira "stop tracing hit on cpu %d\n", smp_processor_id());
13680e05ba49SDaniel Bristot de Oliveira
13691603dda4SDaniel Bristot de Oliveira if (test_bit(OSN_PANIC_ON_STOP, &osnoise_options))
13701603dda4SDaniel Bristot de Oliveira panic("tracer hit stop condition on CPU %d\n", smp_processor_id());
13711603dda4SDaniel Bristot de Oliveira
1372bce29ac9SDaniel Bristot de Oliveira tracer_tracing_off(tr);
1373bce29ac9SDaniel Bristot de Oliveira }
1374dae18134SDaniel Bristot de Oliveira rcu_read_unlock();
1375dae18134SDaniel Bristot de Oliveira }
1376dae18134SDaniel Bristot de Oliveira
1377dae18134SDaniel Bristot de Oliveira /*
1378cb7ca871SDaniel Bristot de Oliveira * osnoise_has_tracing_on - Check if there is at least one instance on
1379cb7ca871SDaniel Bristot de Oliveira */
osnoise_has_tracing_on(void)1380cb7ca871SDaniel Bristot de Oliveira static __always_inline int osnoise_has_tracing_on(void)
1381cb7ca871SDaniel Bristot de Oliveira {
1382cb7ca871SDaniel Bristot de Oliveira struct osnoise_instance *inst;
1383cb7ca871SDaniel Bristot de Oliveira int trace_is_on = 0;
1384cb7ca871SDaniel Bristot de Oliveira
1385cb7ca871SDaniel Bristot de Oliveira rcu_read_lock();
1386cb7ca871SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list)
1387cb7ca871SDaniel Bristot de Oliveira trace_is_on += tracer_tracing_is_on(inst->tr);
1388cb7ca871SDaniel Bristot de Oliveira rcu_read_unlock();
1389cb7ca871SDaniel Bristot de Oliveira
1390cb7ca871SDaniel Bristot de Oliveira return trace_is_on;
1391cb7ca871SDaniel Bristot de Oliveira }
1392cb7ca871SDaniel Bristot de Oliveira
1393cb7ca871SDaniel Bristot de Oliveira /*
1394dae18134SDaniel Bristot de Oliveira * notify_new_max_latency - Notify a new max latency via fsnotify interface.
1395dae18134SDaniel Bristot de Oliveira */
notify_new_max_latency(u64 latency)1396dae18134SDaniel Bristot de Oliveira static void notify_new_max_latency(u64 latency)
1397dae18134SDaniel Bristot de Oliveira {
1398dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst;
1399dae18134SDaniel Bristot de Oliveira struct trace_array *tr;
1400dae18134SDaniel Bristot de Oliveira
1401dae18134SDaniel Bristot de Oliveira rcu_read_lock();
1402dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) {
1403dae18134SDaniel Bristot de Oliveira tr = inst->tr;
1404d3cba7f0SDaniel Bristot de Oliveira if (tracer_tracing_is_on(tr) && tr->max_latency < latency) {
1405dae18134SDaniel Bristot de Oliveira tr->max_latency = latency;
1406dae18134SDaniel Bristot de Oliveira latency_fsnotify(tr);
1407dae18134SDaniel Bristot de Oliveira }
1408dae18134SDaniel Bristot de Oliveira }
1409dae18134SDaniel Bristot de Oliveira rcu_read_unlock();
1410dae18134SDaniel Bristot de Oliveira }
1411bce29ac9SDaniel Bristot de Oliveira
1412bce29ac9SDaniel Bristot de Oliveira /*
1413bce29ac9SDaniel Bristot de Oliveira * run_osnoise - Sample the time and look for osnoise
1414bce29ac9SDaniel Bristot de Oliveira *
1415bce29ac9SDaniel Bristot de Oliveira * Used to capture the time, looking for potential osnoise latency repeatedly.
1416bce29ac9SDaniel Bristot de Oliveira * Different from hwlat_detector, it is called with preemption and interrupts
1417bce29ac9SDaniel Bristot de Oliveira * enabled. This allows irqs, softirqs and threads to run, interfering on the
1418bce29ac9SDaniel Bristot de Oliveira * osnoise sampling thread, as they would do with a regular thread.
1419bce29ac9SDaniel Bristot de Oliveira */
run_osnoise(void)1420bce29ac9SDaniel Bristot de Oliveira static int run_osnoise(void)
1421bce29ac9SDaniel Bristot de Oliveira {
1422b5dce200SDaniel Bristot de Oliveira bool disable_irq = test_bit(OSN_IRQ_DISABLE, &osnoise_options);
1423bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var();
1424bce29ac9SDaniel Bristot de Oliveira u64 start, sample, last_sample;
1425bce29ac9SDaniel Bristot de Oliveira u64 last_int_count, int_count;
142619c3eaa7SDaniel Bristot de Oliveira s64 noise = 0, max_noise = 0;
1427bce29ac9SDaniel Bristot de Oliveira s64 total, last_total = 0;
1428bce29ac9SDaniel Bristot de Oliveira struct osnoise_sample s;
1429b5dce200SDaniel Bristot de Oliveira bool disable_preemption;
1430bce29ac9SDaniel Bristot de Oliveira unsigned int threshold;
1431bce29ac9SDaniel Bristot de Oliveira u64 runtime, stop_in;
143219c3eaa7SDaniel Bristot de Oliveira u64 sum_noise = 0;
143319c3eaa7SDaniel Bristot de Oliveira int hw_count = 0;
1434bce29ac9SDaniel Bristot de Oliveira int ret = -1;
1435bce29ac9SDaniel Bristot de Oliveira
1436bce29ac9SDaniel Bristot de Oliveira /*
1437b5dce200SDaniel Bristot de Oliveira * Disabling preemption is only required if IRQs are enabled,
1438b5dce200SDaniel Bristot de Oliveira * and the options is set on.
1439b5dce200SDaniel Bristot de Oliveira */
1440b5dce200SDaniel Bristot de Oliveira disable_preemption = !disable_irq && test_bit(OSN_PREEMPT_DISABLE, &osnoise_options);
1441b5dce200SDaniel Bristot de Oliveira
1442b5dce200SDaniel Bristot de Oliveira /*
1443bce29ac9SDaniel Bristot de Oliveira * Considers the current thread as the workload.
1444bce29ac9SDaniel Bristot de Oliveira */
1445bce29ac9SDaniel Bristot de Oliveira osn_var->pid = current->pid;
1446bce29ac9SDaniel Bristot de Oliveira
1447bce29ac9SDaniel Bristot de Oliveira /*
1448bce29ac9SDaniel Bristot de Oliveira * Save the current stats for the diff
1449bce29ac9SDaniel Bristot de Oliveira */
1450bce29ac9SDaniel Bristot de Oliveira save_osn_sample_stats(osn_var, &s);
1451bce29ac9SDaniel Bristot de Oliveira
1452bce29ac9SDaniel Bristot de Oliveira /*
1453bce29ac9SDaniel Bristot de Oliveira * if threshold is 0, use the default value of 5 us.
1454bce29ac9SDaniel Bristot de Oliveira */
1455bce29ac9SDaniel Bristot de Oliveira threshold = tracing_thresh ? : 5000;
1456bce29ac9SDaniel Bristot de Oliveira
1457bce29ac9SDaniel Bristot de Oliveira /*
1458b5dce200SDaniel Bristot de Oliveira * Apply PREEMPT and IRQ disabled options.
1459b5dce200SDaniel Bristot de Oliveira */
1460b5dce200SDaniel Bristot de Oliveira if (disable_irq)
1461b5dce200SDaniel Bristot de Oliveira local_irq_disable();
1462b5dce200SDaniel Bristot de Oliveira
1463b5dce200SDaniel Bristot de Oliveira if (disable_preemption)
1464b5dce200SDaniel Bristot de Oliveira preempt_disable();
1465b5dce200SDaniel Bristot de Oliveira
1466b5dce200SDaniel Bristot de Oliveira /*
1467bce29ac9SDaniel Bristot de Oliveira * Make sure NMIs see sampling first
1468bce29ac9SDaniel Bristot de Oliveira */
1469bce29ac9SDaniel Bristot de Oliveira osn_var->sampling = true;
1470bce29ac9SDaniel Bristot de Oliveira barrier();
1471bce29ac9SDaniel Bristot de Oliveira
1472bce29ac9SDaniel Bristot de Oliveira /*
1473bce29ac9SDaniel Bristot de Oliveira * Transform the *_us config to nanoseconds to avoid the
1474bce29ac9SDaniel Bristot de Oliveira * division on the main loop.
1475bce29ac9SDaniel Bristot de Oliveira */
1476bce29ac9SDaniel Bristot de Oliveira runtime = osnoise_data.sample_runtime * NSEC_PER_USEC;
1477bce29ac9SDaniel Bristot de Oliveira stop_in = osnoise_data.stop_tracing * NSEC_PER_USEC;
1478bce29ac9SDaniel Bristot de Oliveira
1479bce29ac9SDaniel Bristot de Oliveira /*
1480bce29ac9SDaniel Bristot de Oliveira * Start timestemp
1481bce29ac9SDaniel Bristot de Oliveira */
1482bce29ac9SDaniel Bristot de Oliveira start = time_get();
1483bce29ac9SDaniel Bristot de Oliveira
1484bce29ac9SDaniel Bristot de Oliveira /*
1485bce29ac9SDaniel Bristot de Oliveira * "previous" loop.
1486bce29ac9SDaniel Bristot de Oliveira */
1487bce29ac9SDaniel Bristot de Oliveira last_int_count = set_int_safe_time(osn_var, &last_sample);
1488bce29ac9SDaniel Bristot de Oliveira
1489bce29ac9SDaniel Bristot de Oliveira do {
1490bce29ac9SDaniel Bristot de Oliveira /*
1491bce29ac9SDaniel Bristot de Oliveira * Get sample!
1492bce29ac9SDaniel Bristot de Oliveira */
1493bce29ac9SDaniel Bristot de Oliveira int_count = set_int_safe_time(osn_var, &sample);
1494bce29ac9SDaniel Bristot de Oliveira
1495bce29ac9SDaniel Bristot de Oliveira noise = time_sub(sample, last_sample);
1496bce29ac9SDaniel Bristot de Oliveira
1497bce29ac9SDaniel Bristot de Oliveira /*
1498bce29ac9SDaniel Bristot de Oliveira * This shouldn't happen.
1499bce29ac9SDaniel Bristot de Oliveira */
1500bce29ac9SDaniel Bristot de Oliveira if (noise < 0) {
1501bce29ac9SDaniel Bristot de Oliveira osnoise_taint("negative noise!");
1502bce29ac9SDaniel Bristot de Oliveira goto out;
1503bce29ac9SDaniel Bristot de Oliveira }
1504bce29ac9SDaniel Bristot de Oliveira
1505bce29ac9SDaniel Bristot de Oliveira /*
1506bce29ac9SDaniel Bristot de Oliveira * Sample runtime.
1507bce29ac9SDaniel Bristot de Oliveira */
1508bce29ac9SDaniel Bristot de Oliveira total = time_sub(sample, start);
1509bce29ac9SDaniel Bristot de Oliveira
1510bce29ac9SDaniel Bristot de Oliveira /*
1511bce29ac9SDaniel Bristot de Oliveira * Check for possible overflows.
1512bce29ac9SDaniel Bristot de Oliveira */
1513bce29ac9SDaniel Bristot de Oliveira if (total < last_total) {
1514bce29ac9SDaniel Bristot de Oliveira osnoise_taint("total overflow!");
1515bce29ac9SDaniel Bristot de Oliveira break;
1516bce29ac9SDaniel Bristot de Oliveira }
1517bce29ac9SDaniel Bristot de Oliveira
1518bce29ac9SDaniel Bristot de Oliveira last_total = total;
1519bce29ac9SDaniel Bristot de Oliveira
1520bce29ac9SDaniel Bristot de Oliveira if (noise >= threshold) {
1521bce29ac9SDaniel Bristot de Oliveira int interference = int_count - last_int_count;
1522bce29ac9SDaniel Bristot de Oliveira
1523bce29ac9SDaniel Bristot de Oliveira if (noise > max_noise)
1524bce29ac9SDaniel Bristot de Oliveira max_noise = noise;
1525bce29ac9SDaniel Bristot de Oliveira
1526bce29ac9SDaniel Bristot de Oliveira if (!interference)
1527bce29ac9SDaniel Bristot de Oliveira hw_count++;
1528bce29ac9SDaniel Bristot de Oliveira
1529bce29ac9SDaniel Bristot de Oliveira sum_noise += noise;
1530bce29ac9SDaniel Bristot de Oliveira
1531bce29ac9SDaniel Bristot de Oliveira trace_sample_threshold(last_sample, noise, interference);
1532bce29ac9SDaniel Bristot de Oliveira
1533bce29ac9SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing)
1534bce29ac9SDaniel Bristot de Oliveira if (noise > stop_in)
1535bce29ac9SDaniel Bristot de Oliveira osnoise_stop_tracing();
1536bce29ac9SDaniel Bristot de Oliveira }
1537bce29ac9SDaniel Bristot de Oliveira
1538bce29ac9SDaniel Bristot de Oliveira /*
1539caf4c86bSNicolas Saenz Julienne * In some cases, notably when running on a nohz_full CPU with
1540caf4c86bSNicolas Saenz Julienne * a stopped tick PREEMPT_RCU has no way to account for QSs.
1541caf4c86bSNicolas Saenz Julienne * This will eventually cause unwarranted noise as PREEMPT_RCU
1542caf4c86bSNicolas Saenz Julienne * will force preemption as the means of ending the current
1543caf4c86bSNicolas Saenz Julienne * grace period. We avoid this problem by calling
1544caf4c86bSNicolas Saenz Julienne * rcu_momentary_dyntick_idle(), which performs a zero duration
1545caf4c86bSNicolas Saenz Julienne * EQS allowing PREEMPT_RCU to end the current grace period.
1546caf4c86bSNicolas Saenz Julienne * This call shouldn't be wrapped inside an RCU critical
1547caf4c86bSNicolas Saenz Julienne * section.
1548caf4c86bSNicolas Saenz Julienne *
1549caf4c86bSNicolas Saenz Julienne * Note that in non PREEMPT_RCU kernels QSs are handled through
1550caf4c86bSNicolas Saenz Julienne * cond_resched()
1551caf4c86bSNicolas Saenz Julienne */
1552caf4c86bSNicolas Saenz Julienne if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
1553b5dce200SDaniel Bristot de Oliveira if (!disable_irq)
1554caf4c86bSNicolas Saenz Julienne local_irq_disable();
1555b5dce200SDaniel Bristot de Oliveira
1556caf4c86bSNicolas Saenz Julienne rcu_momentary_dyntick_idle();
1557b5dce200SDaniel Bristot de Oliveira
1558b5dce200SDaniel Bristot de Oliveira if (!disable_irq)
1559caf4c86bSNicolas Saenz Julienne local_irq_enable();
1560caf4c86bSNicolas Saenz Julienne }
1561caf4c86bSNicolas Saenz Julienne
1562caf4c86bSNicolas Saenz Julienne /*
1563bce29ac9SDaniel Bristot de Oliveira * For the non-preemptive kernel config: let threads runs, if
1564b5dce200SDaniel Bristot de Oliveira * they so wish, unless set not do to so.
1565bce29ac9SDaniel Bristot de Oliveira */
1566b5dce200SDaniel Bristot de Oliveira if (!disable_irq && !disable_preemption)
1567bce29ac9SDaniel Bristot de Oliveira cond_resched();
1568bce29ac9SDaniel Bristot de Oliveira
1569bce29ac9SDaniel Bristot de Oliveira last_sample = sample;
1570bce29ac9SDaniel Bristot de Oliveira last_int_count = int_count;
1571bce29ac9SDaniel Bristot de Oliveira
1572bce29ac9SDaniel Bristot de Oliveira } while (total < runtime && !kthread_should_stop());
1573bce29ac9SDaniel Bristot de Oliveira
1574bce29ac9SDaniel Bristot de Oliveira /*
1575bce29ac9SDaniel Bristot de Oliveira * Finish the above in the view for interrupts.
1576bce29ac9SDaniel Bristot de Oliveira */
1577bce29ac9SDaniel Bristot de Oliveira barrier();
1578bce29ac9SDaniel Bristot de Oliveira
1579bce29ac9SDaniel Bristot de Oliveira osn_var->sampling = false;
1580bce29ac9SDaniel Bristot de Oliveira
1581bce29ac9SDaniel Bristot de Oliveira /*
1582bce29ac9SDaniel Bristot de Oliveira * Make sure sampling data is no longer updated.
1583bce29ac9SDaniel Bristot de Oliveira */
1584bce29ac9SDaniel Bristot de Oliveira barrier();
1585bce29ac9SDaniel Bristot de Oliveira
1586bce29ac9SDaniel Bristot de Oliveira /*
1587b5dce200SDaniel Bristot de Oliveira * Return to the preemptive state.
1588b5dce200SDaniel Bristot de Oliveira */
1589b5dce200SDaniel Bristot de Oliveira if (disable_preemption)
1590b5dce200SDaniel Bristot de Oliveira preempt_enable();
1591b5dce200SDaniel Bristot de Oliveira
1592b5dce200SDaniel Bristot de Oliveira if (disable_irq)
1593b5dce200SDaniel Bristot de Oliveira local_irq_enable();
1594b5dce200SDaniel Bristot de Oliveira
1595b5dce200SDaniel Bristot de Oliveira /*
1596bce29ac9SDaniel Bristot de Oliveira * Save noise info.
1597bce29ac9SDaniel Bristot de Oliveira */
1598bce29ac9SDaniel Bristot de Oliveira s.noise = time_to_us(sum_noise);
1599bce29ac9SDaniel Bristot de Oliveira s.runtime = time_to_us(total);
1600bce29ac9SDaniel Bristot de Oliveira s.max_sample = time_to_us(max_noise);
1601bce29ac9SDaniel Bristot de Oliveira s.hw_count = hw_count;
1602bce29ac9SDaniel Bristot de Oliveira
1603bce29ac9SDaniel Bristot de Oliveira /* Save interference stats info */
1604bce29ac9SDaniel Bristot de Oliveira diff_osn_sample_stats(osn_var, &s);
1605bce29ac9SDaniel Bristot de Oliveira
1606bce29ac9SDaniel Bristot de Oliveira trace_osnoise_sample(&s);
1607bce29ac9SDaniel Bristot de Oliveira
1608dae18134SDaniel Bristot de Oliveira notify_new_max_latency(max_noise);
1609bce29ac9SDaniel Bristot de Oliveira
1610bce29ac9SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing_total)
1611bce29ac9SDaniel Bristot de Oliveira if (s.noise > osnoise_data.stop_tracing_total)
1612bce29ac9SDaniel Bristot de Oliveira osnoise_stop_tracing();
1613bce29ac9SDaniel Bristot de Oliveira
1614bce29ac9SDaniel Bristot de Oliveira return 0;
1615bce29ac9SDaniel Bristot de Oliveira out:
1616bce29ac9SDaniel Bristot de Oliveira return ret;
1617bce29ac9SDaniel Bristot de Oliveira }
1618bce29ac9SDaniel Bristot de Oliveira
1619bce29ac9SDaniel Bristot de Oliveira static struct cpumask osnoise_cpumask;
1620bce29ac9SDaniel Bristot de Oliveira static struct cpumask save_cpumask;
16217a5f0182SSteven Rostedt static struct cpumask kthread_cpumask;
1622bce29ac9SDaniel Bristot de Oliveira
1623bce29ac9SDaniel Bristot de Oliveira /*
1624dd990352SDaniel Bristot de Oliveira * osnoise_sleep - sleep until the next period
1625dd990352SDaniel Bristot de Oliveira */
osnoise_sleep(bool skip_period)1626cb7ca871SDaniel Bristot de Oliveira static void osnoise_sleep(bool skip_period)
1627dd990352SDaniel Bristot de Oliveira {
1628dd990352SDaniel Bristot de Oliveira u64 interval;
1629dd990352SDaniel Bristot de Oliveira ktime_t wake_time;
1630dd990352SDaniel Bristot de Oliveira
1631dd990352SDaniel Bristot de Oliveira mutex_lock(&interface_lock);
1632cb7ca871SDaniel Bristot de Oliveira if (skip_period)
1633cb7ca871SDaniel Bristot de Oliveira interval = osnoise_data.sample_period;
1634cb7ca871SDaniel Bristot de Oliveira else
1635dd990352SDaniel Bristot de Oliveira interval = osnoise_data.sample_period - osnoise_data.sample_runtime;
1636dd990352SDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
1637dd990352SDaniel Bristot de Oliveira
1638dd990352SDaniel Bristot de Oliveira /*
1639dd990352SDaniel Bristot de Oliveira * differently from hwlat_detector, the osnoise tracer can run
1640dd990352SDaniel Bristot de Oliveira * without a pause because preemption is on.
1641dd990352SDaniel Bristot de Oliveira */
1642dd990352SDaniel Bristot de Oliveira if (!interval) {
1643dd990352SDaniel Bristot de Oliveira /* Let synchronize_rcu_tasks() make progress */
1644dd990352SDaniel Bristot de Oliveira cond_resched_tasks_rcu_qs();
1645dd990352SDaniel Bristot de Oliveira return;
1646dd990352SDaniel Bristot de Oliveira }
1647dd990352SDaniel Bristot de Oliveira
1648dd990352SDaniel Bristot de Oliveira wake_time = ktime_add_us(ktime_get(), interval);
1649dd990352SDaniel Bristot de Oliveira __set_current_state(TASK_INTERRUPTIBLE);
1650dd990352SDaniel Bristot de Oliveira
1651b18c58afSDavidlohr Bueso while (schedule_hrtimeout(&wake_time, HRTIMER_MODE_ABS)) {
1652dd990352SDaniel Bristot de Oliveira if (kthread_should_stop())
1653dd990352SDaniel Bristot de Oliveira break;
1654dd990352SDaniel Bristot de Oliveira }
1655dd990352SDaniel Bristot de Oliveira }
1656dd990352SDaniel Bristot de Oliveira
1657dd990352SDaniel Bristot de Oliveira /*
16584998e7fdSDaniel Bristot de Oliveira * osnoise_migration_pending - checks if the task needs to migrate
16594998e7fdSDaniel Bristot de Oliveira *
16604998e7fdSDaniel Bristot de Oliveira * osnoise/timerlat threads are per-cpu. If there is a pending request to
16614998e7fdSDaniel Bristot de Oliveira * migrate the thread away from the current CPU, something bad has happened.
16624998e7fdSDaniel Bristot de Oliveira * Play the good citizen and leave.
16634998e7fdSDaniel Bristot de Oliveira *
16644998e7fdSDaniel Bristot de Oliveira * Returns 0 if it is safe to continue, 1 otherwise.
16654998e7fdSDaniel Bristot de Oliveira */
osnoise_migration_pending(void)16664998e7fdSDaniel Bristot de Oliveira static inline int osnoise_migration_pending(void)
16674998e7fdSDaniel Bristot de Oliveira {
16684998e7fdSDaniel Bristot de Oliveira if (!current->migration_pending)
16694998e7fdSDaniel Bristot de Oliveira return 0;
16704998e7fdSDaniel Bristot de Oliveira
16714998e7fdSDaniel Bristot de Oliveira /*
16724998e7fdSDaniel Bristot de Oliveira * If migration is pending, there is a task waiting for the
16734998e7fdSDaniel Bristot de Oliveira * tracer to enable migration. The tracer does not allow migration,
16744998e7fdSDaniel Bristot de Oliveira * thus: taint and leave to unblock the blocked thread.
16754998e7fdSDaniel Bristot de Oliveira */
16764998e7fdSDaniel Bristot de Oliveira osnoise_taint("migration requested to osnoise threads, leaving.");
16774998e7fdSDaniel Bristot de Oliveira
16784998e7fdSDaniel Bristot de Oliveira /*
16794998e7fdSDaniel Bristot de Oliveira * Unset this thread from the threads managed by the interface.
16804998e7fdSDaniel Bristot de Oliveira * The tracers are responsible for cleaning their env before
16814998e7fdSDaniel Bristot de Oliveira * exiting.
16824998e7fdSDaniel Bristot de Oliveira */
16834998e7fdSDaniel Bristot de Oliveira mutex_lock(&interface_lock);
16844998e7fdSDaniel Bristot de Oliveira this_cpu_osn_var()->kthread = NULL;
16857a5f0182SSteven Rostedt cpumask_clear_cpu(smp_processor_id(), &kthread_cpumask);
16864998e7fdSDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
16874998e7fdSDaniel Bristot de Oliveira
16884998e7fdSDaniel Bristot de Oliveira return 1;
16894998e7fdSDaniel Bristot de Oliveira }
16904998e7fdSDaniel Bristot de Oliveira
16914998e7fdSDaniel Bristot de Oliveira /*
1692bce29ac9SDaniel Bristot de Oliveira * osnoise_main - The osnoise detection kernel thread
1693bce29ac9SDaniel Bristot de Oliveira *
1694bce29ac9SDaniel Bristot de Oliveira * Calls run_osnoise() function to measure the osnoise for the configured runtime,
1695bce29ac9SDaniel Bristot de Oliveira * every period.
1696bce29ac9SDaniel Bristot de Oliveira */
osnoise_main(void * data)1697bce29ac9SDaniel Bristot de Oliveira static int osnoise_main(void *data)
1698bce29ac9SDaniel Bristot de Oliveira {
16994998e7fdSDaniel Bristot de Oliveira unsigned long flags;
17004998e7fdSDaniel Bristot de Oliveira
17014998e7fdSDaniel Bristot de Oliveira /*
17024998e7fdSDaniel Bristot de Oliveira * This thread was created pinned to the CPU using PF_NO_SETAFFINITY.
17034998e7fdSDaniel Bristot de Oliveira * The problem is that cgroup does not allow PF_NO_SETAFFINITY thread.
17044998e7fdSDaniel Bristot de Oliveira *
17054998e7fdSDaniel Bristot de Oliveira * To work around this limitation, disable migration and remove the
17064998e7fdSDaniel Bristot de Oliveira * flag.
17074998e7fdSDaniel Bristot de Oliveira */
17084998e7fdSDaniel Bristot de Oliveira migrate_disable();
17094998e7fdSDaniel Bristot de Oliveira raw_spin_lock_irqsave(¤t->pi_lock, flags);
17104998e7fdSDaniel Bristot de Oliveira current->flags &= ~(PF_NO_SETAFFINITY);
17114998e7fdSDaniel Bristot de Oliveira raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
1712bce29ac9SDaniel Bristot de Oliveira
1713bce29ac9SDaniel Bristot de Oliveira while (!kthread_should_stop()) {
17144998e7fdSDaniel Bristot de Oliveira if (osnoise_migration_pending())
17154998e7fdSDaniel Bristot de Oliveira break;
17164998e7fdSDaniel Bristot de Oliveira
1717cb7ca871SDaniel Bristot de Oliveira /* skip a period if tracing is off on all instances */
1718cb7ca871SDaniel Bristot de Oliveira if (!osnoise_has_tracing_on()) {
1719cb7ca871SDaniel Bristot de Oliveira osnoise_sleep(true);
1720cb7ca871SDaniel Bristot de Oliveira continue;
1721cb7ca871SDaniel Bristot de Oliveira }
1722cb7ca871SDaniel Bristot de Oliveira
1723bce29ac9SDaniel Bristot de Oliveira run_osnoise();
1724cb7ca871SDaniel Bristot de Oliveira osnoise_sleep(false);
1725bce29ac9SDaniel Bristot de Oliveira }
1726bce29ac9SDaniel Bristot de Oliveira
17274998e7fdSDaniel Bristot de Oliveira migrate_enable();
1728bce29ac9SDaniel Bristot de Oliveira return 0;
1729bce29ac9SDaniel Bristot de Oliveira }
1730bce29ac9SDaniel Bristot de Oliveira
1731a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
1732a955d7eaSDaniel Bristot de Oliveira /*
1733a955d7eaSDaniel Bristot de Oliveira * timerlat_irq - hrtimer handler for timerlat.
1734a955d7eaSDaniel Bristot de Oliveira */
timerlat_irq(struct hrtimer * timer)1735a955d7eaSDaniel Bristot de Oliveira static enum hrtimer_restart timerlat_irq(struct hrtimer *timer)
1736a955d7eaSDaniel Bristot de Oliveira {
1737a955d7eaSDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var();
1738a955d7eaSDaniel Bristot de Oliveira struct timerlat_variables *tlat;
1739a955d7eaSDaniel Bristot de Oliveira struct timerlat_sample s;
1740a955d7eaSDaniel Bristot de Oliveira u64 now;
1741a955d7eaSDaniel Bristot de Oliveira u64 diff;
1742a955d7eaSDaniel Bristot de Oliveira
1743a955d7eaSDaniel Bristot de Oliveira /*
1744a955d7eaSDaniel Bristot de Oliveira * I am not sure if the timer was armed for this CPU. So, get
1745a955d7eaSDaniel Bristot de Oliveira * the timerlat struct from the timer itself, not from this
1746a955d7eaSDaniel Bristot de Oliveira * CPU.
1747a955d7eaSDaniel Bristot de Oliveira */
1748a955d7eaSDaniel Bristot de Oliveira tlat = container_of(timer, struct timerlat_variables, timer);
1749a955d7eaSDaniel Bristot de Oliveira
1750a955d7eaSDaniel Bristot de Oliveira now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
1751a955d7eaSDaniel Bristot de Oliveira
1752a955d7eaSDaniel Bristot de Oliveira /*
1753a955d7eaSDaniel Bristot de Oliveira * Enable the osnoise: events for thread an softirq.
1754a955d7eaSDaniel Bristot de Oliveira */
1755a955d7eaSDaniel Bristot de Oliveira tlat->tracing_thread = true;
1756a955d7eaSDaniel Bristot de Oliveira
1757a955d7eaSDaniel Bristot de Oliveira osn_var->thread.arrival_time = time_get();
1758a955d7eaSDaniel Bristot de Oliveira
1759a955d7eaSDaniel Bristot de Oliveira /*
1760a955d7eaSDaniel Bristot de Oliveira * A hardirq is running: the timer IRQ. It is for sure preempting
1761a955d7eaSDaniel Bristot de Oliveira * a thread, and potentially preempting a softirq.
1762a955d7eaSDaniel Bristot de Oliveira *
1763a955d7eaSDaniel Bristot de Oliveira * At this point, it is not interesting to know the duration of the
1764a955d7eaSDaniel Bristot de Oliveira * preempted thread (and maybe softirq), but how much time they will
1765a955d7eaSDaniel Bristot de Oliveira * delay the beginning of the execution of the timer thread.
1766a955d7eaSDaniel Bristot de Oliveira *
1767a955d7eaSDaniel Bristot de Oliveira * To get the correct (net) delay added by the softirq, its delta_start
1768a955d7eaSDaniel Bristot de Oliveira * is set as the IRQ one. In this way, at the return of the IRQ, the delta
1769a955d7eaSDaniel Bristot de Oliveira * start of the sofitrq will be zeroed, accounting then only the time
1770a955d7eaSDaniel Bristot de Oliveira * after that.
1771a955d7eaSDaniel Bristot de Oliveira *
1772a955d7eaSDaniel Bristot de Oliveira * The thread follows the same principle. However, if a softirq is
1773a955d7eaSDaniel Bristot de Oliveira * running, the thread needs to receive the softirq delta_start. The
1774a955d7eaSDaniel Bristot de Oliveira * reason being is that the softirq will be the last to be unfolded,
1775a955d7eaSDaniel Bristot de Oliveira * resseting the thread delay to zero.
177601e181c7SDaniel Bristot de Oliveira *
177701e181c7SDaniel Bristot de Oliveira * The PREEMPT_RT is a special case, though. As softirqs run as threads
177801e181c7SDaniel Bristot de Oliveira * on RT, moving the thread is enough.
1779a955d7eaSDaniel Bristot de Oliveira */
178001e181c7SDaniel Bristot de Oliveira if (!IS_ENABLED(CONFIG_PREEMPT_RT) && osn_var->softirq.delta_start) {
1781a955d7eaSDaniel Bristot de Oliveira copy_int_safe_time(osn_var, &osn_var->thread.delta_start,
1782a955d7eaSDaniel Bristot de Oliveira &osn_var->softirq.delta_start);
1783a955d7eaSDaniel Bristot de Oliveira
1784a955d7eaSDaniel Bristot de Oliveira copy_int_safe_time(osn_var, &osn_var->softirq.delta_start,
1785a955d7eaSDaniel Bristot de Oliveira &osn_var->irq.delta_start);
1786a955d7eaSDaniel Bristot de Oliveira } else {
1787a955d7eaSDaniel Bristot de Oliveira copy_int_safe_time(osn_var, &osn_var->thread.delta_start,
1788a955d7eaSDaniel Bristot de Oliveira &osn_var->irq.delta_start);
1789a955d7eaSDaniel Bristot de Oliveira }
1790a955d7eaSDaniel Bristot de Oliveira
1791a955d7eaSDaniel Bristot de Oliveira /*
1792a955d7eaSDaniel Bristot de Oliveira * Compute the current time with the expected time.
1793a955d7eaSDaniel Bristot de Oliveira */
1794a955d7eaSDaniel Bristot de Oliveira diff = now - tlat->abs_period;
1795a955d7eaSDaniel Bristot de Oliveira
1796a955d7eaSDaniel Bristot de Oliveira tlat->count++;
1797a955d7eaSDaniel Bristot de Oliveira s.seqnum = tlat->count;
1798a955d7eaSDaniel Bristot de Oliveira s.timer_latency = diff;
1799a955d7eaSDaniel Bristot de Oliveira s.context = IRQ_CONTEXT;
1800a955d7eaSDaniel Bristot de Oliveira
1801a955d7eaSDaniel Bristot de Oliveira trace_timerlat_sample(&s);
1802a955d7eaSDaniel Bristot de Oliveira
1803aa748949SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing) {
1804aa748949SDaniel Bristot de Oliveira if (time_to_us(diff) >= osnoise_data.stop_tracing) {
18054dd2aea2SDaniel Bristot de Oliveira
18064dd2aea2SDaniel Bristot de Oliveira /*
18074dd2aea2SDaniel Bristot de Oliveira * At this point, if stop_tracing is set and <= print_stack,
18084dd2aea2SDaniel Bristot de Oliveira * print_stack is set and would be printed in the thread handler.
18094dd2aea2SDaniel Bristot de Oliveira *
18104dd2aea2SDaniel Bristot de Oliveira * Thus, print the stack trace as it is helpful to define the
18114dd2aea2SDaniel Bristot de Oliveira * root cause of an IRQ latency.
18124dd2aea2SDaniel Bristot de Oliveira */
18134dd2aea2SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing <= osnoise_data.print_stack) {
18144dd2aea2SDaniel Bristot de Oliveira timerlat_save_stack(0);
18154dd2aea2SDaniel Bristot de Oliveira timerlat_dump_stack(time_to_us(diff));
18164dd2aea2SDaniel Bristot de Oliveira }
18174dd2aea2SDaniel Bristot de Oliveira
1818a955d7eaSDaniel Bristot de Oliveira osnoise_stop_tracing();
1819a955d7eaSDaniel Bristot de Oliveira notify_new_max_latency(diff);
1820a955d7eaSDaniel Bristot de Oliveira
1821632478a0SDaniel Bristot de Oliveira wake_up_process(tlat->kthread);
1822632478a0SDaniel Bristot de Oliveira
18239c556e5aSDaniel Bristot de Oliveira return HRTIMER_NORESTART;
1824aa748949SDaniel Bristot de Oliveira }
1825aa748949SDaniel Bristot de Oliveira }
1826a955d7eaSDaniel Bristot de Oliveira
1827a955d7eaSDaniel Bristot de Oliveira wake_up_process(tlat->kthread);
1828a955d7eaSDaniel Bristot de Oliveira
1829a955d7eaSDaniel Bristot de Oliveira if (osnoise_data.print_stack)
1830a955d7eaSDaniel Bristot de Oliveira timerlat_save_stack(0);
1831a955d7eaSDaniel Bristot de Oliveira
1832a955d7eaSDaniel Bristot de Oliveira return HRTIMER_NORESTART;
1833a955d7eaSDaniel Bristot de Oliveira }
1834a955d7eaSDaniel Bristot de Oliveira
1835a955d7eaSDaniel Bristot de Oliveira /*
1836a955d7eaSDaniel Bristot de Oliveira * wait_next_period - Wait for the next period for timerlat
1837a955d7eaSDaniel Bristot de Oliveira */
wait_next_period(struct timerlat_variables * tlat)1838a955d7eaSDaniel Bristot de Oliveira static int wait_next_period(struct timerlat_variables *tlat)
1839a955d7eaSDaniel Bristot de Oliveira {
1840a955d7eaSDaniel Bristot de Oliveira ktime_t next_abs_period, now;
1841a955d7eaSDaniel Bristot de Oliveira u64 rel_period = osnoise_data.timerlat_period * 1000;
1842a955d7eaSDaniel Bristot de Oliveira
1843a955d7eaSDaniel Bristot de Oliveira now = hrtimer_cb_get_time(&tlat->timer);
1844a955d7eaSDaniel Bristot de Oliveira next_abs_period = ns_to_ktime(tlat->abs_period + rel_period);
1845a955d7eaSDaniel Bristot de Oliveira
1846a955d7eaSDaniel Bristot de Oliveira /*
1847a955d7eaSDaniel Bristot de Oliveira * Save the next abs_period.
1848a955d7eaSDaniel Bristot de Oliveira */
1849a955d7eaSDaniel Bristot de Oliveira tlat->abs_period = (u64) ktime_to_ns(next_abs_period);
1850a955d7eaSDaniel Bristot de Oliveira
1851a955d7eaSDaniel Bristot de Oliveira /*
1852a955d7eaSDaniel Bristot de Oliveira * If the new abs_period is in the past, skip the activation.
1853a955d7eaSDaniel Bristot de Oliveira */
1854a955d7eaSDaniel Bristot de Oliveira while (ktime_compare(now, next_abs_period) > 0) {
1855a955d7eaSDaniel Bristot de Oliveira next_abs_period = ns_to_ktime(tlat->abs_period + rel_period);
1856a955d7eaSDaniel Bristot de Oliveira tlat->abs_period = (u64) ktime_to_ns(next_abs_period);
1857a955d7eaSDaniel Bristot de Oliveira }
1858a955d7eaSDaniel Bristot de Oliveira
1859a955d7eaSDaniel Bristot de Oliveira set_current_state(TASK_INTERRUPTIBLE);
1860a955d7eaSDaniel Bristot de Oliveira
1861a955d7eaSDaniel Bristot de Oliveira hrtimer_start(&tlat->timer, next_abs_period, HRTIMER_MODE_ABS_PINNED_HARD);
1862a955d7eaSDaniel Bristot de Oliveira schedule();
1863a955d7eaSDaniel Bristot de Oliveira return 1;
1864a955d7eaSDaniel Bristot de Oliveira }
1865a955d7eaSDaniel Bristot de Oliveira
1866a955d7eaSDaniel Bristot de Oliveira /*
1867a955d7eaSDaniel Bristot de Oliveira * timerlat_main- Timerlat main
1868a955d7eaSDaniel Bristot de Oliveira */
timerlat_main(void * data)1869a955d7eaSDaniel Bristot de Oliveira static int timerlat_main(void *data)
1870a955d7eaSDaniel Bristot de Oliveira {
1871a955d7eaSDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var();
1872a955d7eaSDaniel Bristot de Oliveira struct timerlat_variables *tlat = this_cpu_tmr_var();
1873a955d7eaSDaniel Bristot de Oliveira struct timerlat_sample s;
1874a955d7eaSDaniel Bristot de Oliveira struct sched_param sp;
18754998e7fdSDaniel Bristot de Oliveira unsigned long flags;
1876a955d7eaSDaniel Bristot de Oliveira u64 now, diff;
1877a955d7eaSDaniel Bristot de Oliveira
1878a955d7eaSDaniel Bristot de Oliveira /*
1879a955d7eaSDaniel Bristot de Oliveira * Make the thread RT, that is how cyclictest is usually used.
1880a955d7eaSDaniel Bristot de Oliveira */
1881a955d7eaSDaniel Bristot de Oliveira sp.sched_priority = DEFAULT_TIMERLAT_PRIO;
1882a955d7eaSDaniel Bristot de Oliveira sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
1883a955d7eaSDaniel Bristot de Oliveira
18844998e7fdSDaniel Bristot de Oliveira /*
18854998e7fdSDaniel Bristot de Oliveira * This thread was created pinned to the CPU using PF_NO_SETAFFINITY.
18864998e7fdSDaniel Bristot de Oliveira * The problem is that cgroup does not allow PF_NO_SETAFFINITY thread.
18874998e7fdSDaniel Bristot de Oliveira *
18884998e7fdSDaniel Bristot de Oliveira * To work around this limitation, disable migration and remove the
18894998e7fdSDaniel Bristot de Oliveira * flag.
18904998e7fdSDaniel Bristot de Oliveira */
18914998e7fdSDaniel Bristot de Oliveira migrate_disable();
18924998e7fdSDaniel Bristot de Oliveira raw_spin_lock_irqsave(¤t->pi_lock, flags);
18934998e7fdSDaniel Bristot de Oliveira current->flags &= ~(PF_NO_SETAFFINITY);
18944998e7fdSDaniel Bristot de Oliveira raw_spin_unlock_irqrestore(¤t->pi_lock, flags);
18954998e7fdSDaniel Bristot de Oliveira
1896a955d7eaSDaniel Bristot de Oliveira tlat->count = 0;
1897a955d7eaSDaniel Bristot de Oliveira tlat->tracing_thread = false;
1898a955d7eaSDaniel Bristot de Oliveira
1899a955d7eaSDaniel Bristot de Oliveira hrtimer_init(&tlat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
1900a955d7eaSDaniel Bristot de Oliveira tlat->timer.function = timerlat_irq;
1901a955d7eaSDaniel Bristot de Oliveira tlat->kthread = current;
1902a955d7eaSDaniel Bristot de Oliveira osn_var->pid = current->pid;
1903a955d7eaSDaniel Bristot de Oliveira /*
1904a955d7eaSDaniel Bristot de Oliveira * Anotate the arrival time.
1905a955d7eaSDaniel Bristot de Oliveira */
1906a955d7eaSDaniel Bristot de Oliveira tlat->abs_period = hrtimer_cb_get_time(&tlat->timer);
1907a955d7eaSDaniel Bristot de Oliveira
1908a955d7eaSDaniel Bristot de Oliveira wait_next_period(tlat);
1909a955d7eaSDaniel Bristot de Oliveira
1910a955d7eaSDaniel Bristot de Oliveira osn_var->sampling = 1;
1911a955d7eaSDaniel Bristot de Oliveira
1912a955d7eaSDaniel Bristot de Oliveira while (!kthread_should_stop()) {
19134998e7fdSDaniel Bristot de Oliveira
1914a955d7eaSDaniel Bristot de Oliveira now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
1915a955d7eaSDaniel Bristot de Oliveira diff = now - tlat->abs_period;
1916a955d7eaSDaniel Bristot de Oliveira
1917a955d7eaSDaniel Bristot de Oliveira s.seqnum = tlat->count;
1918a955d7eaSDaniel Bristot de Oliveira s.timer_latency = diff;
1919a955d7eaSDaniel Bristot de Oliveira s.context = THREAD_CONTEXT;
1920a955d7eaSDaniel Bristot de Oliveira
1921a955d7eaSDaniel Bristot de Oliveira trace_timerlat_sample(&s);
1922a955d7eaSDaniel Bristot de Oliveira
1923b9f451a9SDaniel Bristot de Oliveira notify_new_max_latency(diff);
1924b9f451a9SDaniel Bristot de Oliveira
1925b14f4568SDaniel Bristot de Oliveira timerlat_dump_stack(time_to_us(diff));
1926a955d7eaSDaniel Bristot de Oliveira
1927a955d7eaSDaniel Bristot de Oliveira tlat->tracing_thread = false;
1928a955d7eaSDaniel Bristot de Oliveira if (osnoise_data.stop_tracing_total)
1929a955d7eaSDaniel Bristot de Oliveira if (time_to_us(diff) >= osnoise_data.stop_tracing_total)
1930a955d7eaSDaniel Bristot de Oliveira osnoise_stop_tracing();
1931a955d7eaSDaniel Bristot de Oliveira
19324998e7fdSDaniel Bristot de Oliveira if (osnoise_migration_pending())
19334998e7fdSDaniel Bristot de Oliveira break;
19344998e7fdSDaniel Bristot de Oliveira
1935a955d7eaSDaniel Bristot de Oliveira wait_next_period(tlat);
1936a955d7eaSDaniel Bristot de Oliveira }
1937a955d7eaSDaniel Bristot de Oliveira
1938a955d7eaSDaniel Bristot de Oliveira hrtimer_cancel(&tlat->timer);
19394998e7fdSDaniel Bristot de Oliveira migrate_enable();
1940a955d7eaSDaniel Bristot de Oliveira return 0;
1941a955d7eaSDaniel Bristot de Oliveira }
1942ccb67544SDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */
timerlat_main(void * data)1943ccb67544SDaniel Bristot de Oliveira static int timerlat_main(void *data)
1944ccb67544SDaniel Bristot de Oliveira {
1945ccb67544SDaniel Bristot de Oliveira return 0;
1946ccb67544SDaniel Bristot de Oliveira }
1947a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */
1948a955d7eaSDaniel Bristot de Oliveira
1949bce29ac9SDaniel Bristot de Oliveira /*
1950c8895e27SDaniel Bristot de Oliveira * stop_kthread - stop a workload thread
1951c8895e27SDaniel Bristot de Oliveira */
stop_kthread(unsigned int cpu)1952c8895e27SDaniel Bristot de Oliveira static void stop_kthread(unsigned int cpu)
1953c8895e27SDaniel Bristot de Oliveira {
1954c8895e27SDaniel Bristot de Oliveira struct task_struct *kthread;
1955c8895e27SDaniel Bristot de Oliveira
1956a4a05cefSWei Li kthread = xchg_relaxed(&(per_cpu(per_cpu_osnoise_var, cpu).kthread), NULL);
195730838fcdSDaniel Bristot de Oliveira if (kthread) {
19587a5f0182SSteven Rostedt if (cpumask_test_and_clear_cpu(cpu, &kthread_cpumask) &&
19597a5f0182SSteven Rostedt !WARN_ON(!test_bit(OSN_WORKLOAD, &osnoise_options))) {
1960c8895e27SDaniel Bristot de Oliveira kthread_stop(kthread);
19617a5f0182SSteven Rostedt } else if (!WARN_ON(test_bit(OSN_WORKLOAD, &osnoise_options))) {
1962e88ed227SDaniel Bristot de Oliveira /*
1963e88ed227SDaniel Bristot de Oliveira * This is a user thread waiting on the timerlat_fd. We need
1964e88ed227SDaniel Bristot de Oliveira * to close all users, and the best way to guarantee this is
1965e88ed227SDaniel Bristot de Oliveira * by killing the thread. NOTE: this is a purpose specific file.
1966e88ed227SDaniel Bristot de Oliveira */
1967e88ed227SDaniel Bristot de Oliveira kill_pid(kthread->thread_pid, SIGKILL, 1);
1968e88ed227SDaniel Bristot de Oliveira put_task_struct(kthread);
1969e88ed227SDaniel Bristot de Oliveira }
197030838fcdSDaniel Bristot de Oliveira } else {
1971e88ed227SDaniel Bristot de Oliveira /* if no workload, just return */
197230838fcdSDaniel Bristot de Oliveira if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
1973e88ed227SDaniel Bristot de Oliveira /*
1974e88ed227SDaniel Bristot de Oliveira * This is set in the osnoise tracer case.
1975e88ed227SDaniel Bristot de Oliveira */
197630838fcdSDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).sampling = false;
197730838fcdSDaniel Bristot de Oliveira barrier();
197830838fcdSDaniel Bristot de Oliveira }
197930838fcdSDaniel Bristot de Oliveira }
1980c8895e27SDaniel Bristot de Oliveira }
1981c8895e27SDaniel Bristot de Oliveira
1982c8895e27SDaniel Bristot de Oliveira /*
1983c8895e27SDaniel Bristot de Oliveira * stop_per_cpu_kthread - Stop per-cpu threads
1984bce29ac9SDaniel Bristot de Oliveira *
1985bce29ac9SDaniel Bristot de Oliveira * Stop the osnoise sampling htread. Use this on unload and at system
1986bce29ac9SDaniel Bristot de Oliveira * shutdown.
1987bce29ac9SDaniel Bristot de Oliveira */
stop_per_cpu_kthreads(void)1988bce29ac9SDaniel Bristot de Oliveira static void stop_per_cpu_kthreads(void)
1989bce29ac9SDaniel Bristot de Oliveira {
1990bce29ac9SDaniel Bristot de Oliveira int cpu;
1991bce29ac9SDaniel Bristot de Oliveira
1992a4a05cefSWei Li cpus_read_lock();
1993a4a05cefSWei Li
1994a4a05cefSWei Li for_each_online_cpu(cpu)
1995c8895e27SDaniel Bristot de Oliveira stop_kthread(cpu);
1996a4a05cefSWei Li
1997a4a05cefSWei Li cpus_read_unlock();
1998c8895e27SDaniel Bristot de Oliveira }
1999c8895e27SDaniel Bristot de Oliveira
2000c8895e27SDaniel Bristot de Oliveira /*
2001c8895e27SDaniel Bristot de Oliveira * start_kthread - Start a workload tread
2002c8895e27SDaniel Bristot de Oliveira */
start_kthread(unsigned int cpu)2003c8895e27SDaniel Bristot de Oliveira static int start_kthread(unsigned int cpu)
2004c8895e27SDaniel Bristot de Oliveira {
2005c8895e27SDaniel Bristot de Oliveira struct task_struct *kthread;
2006c8895e27SDaniel Bristot de Oliveira void *main = osnoise_main;
2007c8895e27SDaniel Bristot de Oliveira char comm[24];
2008c8895e27SDaniel Bristot de Oliveira
2009*8b0f0a26SWei Li /* Do not start a new thread if it is already running */
2010*8b0f0a26SWei Li if (per_cpu(per_cpu_osnoise_var, cpu).kthread)
2011*8b0f0a26SWei Li return 0;
2012*8b0f0a26SWei Li
2013ccb67544SDaniel Bristot de Oliveira if (timerlat_enabled()) {
2014a955d7eaSDaniel Bristot de Oliveira snprintf(comm, 24, "timerlat/%d", cpu);
2015a955d7eaSDaniel Bristot de Oliveira main = timerlat_main;
2016a955d7eaSDaniel Bristot de Oliveira } else {
201730838fcdSDaniel Bristot de Oliveira /* if no workload, just return */
201830838fcdSDaniel Bristot de Oliveira if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
201930838fcdSDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).sampling = true;
202030838fcdSDaniel Bristot de Oliveira barrier();
202130838fcdSDaniel Bristot de Oliveira return 0;
202230838fcdSDaniel Bristot de Oliveira }
2023bce29ac9SDaniel Bristot de Oliveira snprintf(comm, 24, "osnoise/%d", cpu);
2024a955d7eaSDaniel Bristot de Oliveira }
2025ccb67544SDaniel Bristot de Oliveira
202611e4e352SCai Huoqing kthread = kthread_run_on_cpu(main, NULL, cpu, comm);
2027bce29ac9SDaniel Bristot de Oliveira
2028bce29ac9SDaniel Bristot de Oliveira if (IS_ERR(kthread)) {
2029bce29ac9SDaniel Bristot de Oliveira pr_err(BANNER "could not start sampling thread\n");
2030bce29ac9SDaniel Bristot de Oliveira stop_per_cpu_kthreads();
2031bce29ac9SDaniel Bristot de Oliveira return -ENOMEM;
2032bce29ac9SDaniel Bristot de Oliveira }
2033bce29ac9SDaniel Bristot de Oliveira
2034bce29ac9SDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).kthread = kthread;
20357a5f0182SSteven Rostedt cpumask_set_cpu(cpu, &kthread_cpumask);
2036bce29ac9SDaniel Bristot de Oliveira
2037bce29ac9SDaniel Bristot de Oliveira return 0;
2038bce29ac9SDaniel Bristot de Oliveira }
2039bce29ac9SDaniel Bristot de Oliveira
2040bce29ac9SDaniel Bristot de Oliveira /*
2041c8895e27SDaniel Bristot de Oliveira * start_per_cpu_kthread - Kick off per-cpu osnoise sampling kthreads
2042c8895e27SDaniel Bristot de Oliveira *
2043c8895e27SDaniel Bristot de Oliveira * This starts the kernel thread that will look for osnoise on many
2044c8895e27SDaniel Bristot de Oliveira * cpus.
2045c8895e27SDaniel Bristot de Oliveira */
start_per_cpu_kthreads(void)204615ca4bdbSDaniel Bristot de Oliveira static int start_per_cpu_kthreads(void)
2047c8895e27SDaniel Bristot de Oliveira {
2048c8895e27SDaniel Bristot de Oliveira struct cpumask *current_mask = &save_cpumask;
20494b6b08f2SQiang.Zhang int retval = 0;
2050c8895e27SDaniel Bristot de Oliveira int cpu;
2051c8895e27SDaniel Bristot de Oliveira
2052e88ed227SDaniel Bristot de Oliveira if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
2053e88ed227SDaniel Bristot de Oliveira if (timerlat_enabled())
2054e88ed227SDaniel Bristot de Oliveira return 0;
2055e88ed227SDaniel Bristot de Oliveira }
2056e88ed227SDaniel Bristot de Oliveira
205799c37d1aSSebastian Andrzej Siewior cpus_read_lock();
2058c8895e27SDaniel Bristot de Oliveira /*
205966df27f1SDaniel Bristot de Oliveira * Run only on online CPUs in which osnoise is allowed to run.
2060c8895e27SDaniel Bristot de Oliveira */
206166df27f1SDaniel Bristot de Oliveira cpumask_and(current_mask, cpu_online_mask, &osnoise_cpumask);
2062c8895e27SDaniel Bristot de Oliveira
20637a5f0182SSteven Rostedt for_each_possible_cpu(cpu) {
20647a5f0182SSteven Rostedt if (cpumask_test_and_clear_cpu(cpu, &kthread_cpumask)) {
20657a5f0182SSteven Rostedt struct task_struct *kthread;
20667a5f0182SSteven Rostedt
2067*8b0f0a26SWei Li kthread = xchg_relaxed(&(per_cpu(per_cpu_osnoise_var, cpu).kthread), NULL);
20687a5f0182SSteven Rostedt if (!WARN_ON(!kthread))
20697a5f0182SSteven Rostedt kthread_stop(kthread);
20707a5f0182SSteven Rostedt }
20717a5f0182SSteven Rostedt }
2072c8895e27SDaniel Bristot de Oliveira
2073c8895e27SDaniel Bristot de Oliveira for_each_cpu(cpu, current_mask) {
2074c8895e27SDaniel Bristot de Oliveira retval = start_kthread(cpu);
2075c8895e27SDaniel Bristot de Oliveira if (retval) {
207699ee9317SNico Pache cpus_read_unlock();
2077c8895e27SDaniel Bristot de Oliveira stop_per_cpu_kthreads();
207899ee9317SNico Pache return retval;
2079c8895e27SDaniel Bristot de Oliveira }
2080c8895e27SDaniel Bristot de Oliveira }
2081c8895e27SDaniel Bristot de Oliveira
208299c37d1aSSebastian Andrzej Siewior cpus_read_unlock();
2083c8895e27SDaniel Bristot de Oliveira
20844b6b08f2SQiang.Zhang return retval;
2085c8895e27SDaniel Bristot de Oliveira }
2086c8895e27SDaniel Bristot de Oliveira
2087c8895e27SDaniel Bristot de Oliveira #ifdef CONFIG_HOTPLUG_CPU
osnoise_hotplug_workfn(struct work_struct * dummy)2088c8895e27SDaniel Bristot de Oliveira static void osnoise_hotplug_workfn(struct work_struct *dummy)
2089c8895e27SDaniel Bristot de Oliveira {
2090c8895e27SDaniel Bristot de Oliveira unsigned int cpu = smp_processor_id();
2091c8895e27SDaniel Bristot de Oliveira
2092c8895e27SDaniel Bristot de Oliveira mutex_lock(&trace_types_lock);
2093c8895e27SDaniel Bristot de Oliveira
2094dae18134SDaniel Bristot de Oliveira if (!osnoise_has_registered_instances())
2095c8895e27SDaniel Bristot de Oliveira goto out_unlock_trace;
2096c8895e27SDaniel Bristot de Oliveira
2097c8895e27SDaniel Bristot de Oliveira mutex_lock(&interface_lock);
209899c37d1aSSebastian Andrzej Siewior cpus_read_lock();
2099c8895e27SDaniel Bristot de Oliveira
2100a6e98490SWei Li if (!cpu_online(cpu))
2101a6e98490SWei Li goto out_unlock;
2102c8895e27SDaniel Bristot de Oliveira if (!cpumask_test_cpu(cpu, &osnoise_cpumask))
2103c8895e27SDaniel Bristot de Oliveira goto out_unlock;
2104c8895e27SDaniel Bristot de Oliveira
2105c8895e27SDaniel Bristot de Oliveira start_kthread(cpu);
2106c8895e27SDaniel Bristot de Oliveira
2107c8895e27SDaniel Bristot de Oliveira out_unlock:
210899c37d1aSSebastian Andrzej Siewior cpus_read_unlock();
2109c8895e27SDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2110c8895e27SDaniel Bristot de Oliveira out_unlock_trace:
2111c8895e27SDaniel Bristot de Oliveira mutex_unlock(&trace_types_lock);
2112c8895e27SDaniel Bristot de Oliveira }
2113c8895e27SDaniel Bristot de Oliveira
2114c8895e27SDaniel Bristot de Oliveira static DECLARE_WORK(osnoise_hotplug_work, osnoise_hotplug_workfn);
2115c8895e27SDaniel Bristot de Oliveira
2116c8895e27SDaniel Bristot de Oliveira /*
2117c8895e27SDaniel Bristot de Oliveira * osnoise_cpu_init - CPU hotplug online callback function
2118c8895e27SDaniel Bristot de Oliveira */
osnoise_cpu_init(unsigned int cpu)2119c8895e27SDaniel Bristot de Oliveira static int osnoise_cpu_init(unsigned int cpu)
2120c8895e27SDaniel Bristot de Oliveira {
2121c8895e27SDaniel Bristot de Oliveira schedule_work_on(cpu, &osnoise_hotplug_work);
2122c8895e27SDaniel Bristot de Oliveira return 0;
2123c8895e27SDaniel Bristot de Oliveira }
2124c8895e27SDaniel Bristot de Oliveira
2125c8895e27SDaniel Bristot de Oliveira /*
2126c8895e27SDaniel Bristot de Oliveira * osnoise_cpu_die - CPU hotplug offline callback function
2127c8895e27SDaniel Bristot de Oliveira */
osnoise_cpu_die(unsigned int cpu)2128c8895e27SDaniel Bristot de Oliveira static int osnoise_cpu_die(unsigned int cpu)
2129c8895e27SDaniel Bristot de Oliveira {
2130c8895e27SDaniel Bristot de Oliveira stop_kthread(cpu);
2131c8895e27SDaniel Bristot de Oliveira return 0;
2132c8895e27SDaniel Bristot de Oliveira }
2133c8895e27SDaniel Bristot de Oliveira
osnoise_init_hotplug_support(void)2134c8895e27SDaniel Bristot de Oliveira static void osnoise_init_hotplug_support(void)
2135c8895e27SDaniel Bristot de Oliveira {
2136c8895e27SDaniel Bristot de Oliveira int ret;
2137c8895e27SDaniel Bristot de Oliveira
2138c8895e27SDaniel Bristot de Oliveira ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "trace/osnoise:online",
2139c8895e27SDaniel Bristot de Oliveira osnoise_cpu_init, osnoise_cpu_die);
2140c8895e27SDaniel Bristot de Oliveira if (ret < 0)
2141c8895e27SDaniel Bristot de Oliveira pr_warn(BANNER "Error to init cpu hotplug support\n");
2142c8895e27SDaniel Bristot de Oliveira
2143c8895e27SDaniel Bristot de Oliveira return;
2144c8895e27SDaniel Bristot de Oliveira }
2145c8895e27SDaniel Bristot de Oliveira #else /* CONFIG_HOTPLUG_CPU */
osnoise_init_hotplug_support(void)2146c8895e27SDaniel Bristot de Oliveira static void osnoise_init_hotplug_support(void)
2147c8895e27SDaniel Bristot de Oliveira {
2148498627b4SDaniel Bristot de Oliveira return;
2149c8895e27SDaniel Bristot de Oliveira }
2150c8895e27SDaniel Bristot de Oliveira #endif /* CONFIG_HOTPLUG_CPU */
2151c8895e27SDaniel Bristot de Oliveira
2152c8895e27SDaniel Bristot de Oliveira /*
2153b179d48bSDaniel Bristot de Oliveira * seq file functions for the osnoise/options file.
2154b179d48bSDaniel Bristot de Oliveira */
s_options_start(struct seq_file * s,loff_t * pos)2155b179d48bSDaniel Bristot de Oliveira static void *s_options_start(struct seq_file *s, loff_t *pos)
2156b179d48bSDaniel Bristot de Oliveira {
2157b179d48bSDaniel Bristot de Oliveira int option = *pos;
2158b179d48bSDaniel Bristot de Oliveira
2159b179d48bSDaniel Bristot de Oliveira mutex_lock(&interface_lock);
2160b179d48bSDaniel Bristot de Oliveira
2161b179d48bSDaniel Bristot de Oliveira if (option >= OSN_MAX)
2162b179d48bSDaniel Bristot de Oliveira return NULL;
2163b179d48bSDaniel Bristot de Oliveira
2164b179d48bSDaniel Bristot de Oliveira return pos;
2165b179d48bSDaniel Bristot de Oliveira }
2166b179d48bSDaniel Bristot de Oliveira
s_options_next(struct seq_file * s,void * v,loff_t * pos)2167b179d48bSDaniel Bristot de Oliveira static void *s_options_next(struct seq_file *s, void *v, loff_t *pos)
2168b179d48bSDaniel Bristot de Oliveira {
2169b179d48bSDaniel Bristot de Oliveira int option = ++(*pos);
2170b179d48bSDaniel Bristot de Oliveira
2171b179d48bSDaniel Bristot de Oliveira if (option >= OSN_MAX)
2172b179d48bSDaniel Bristot de Oliveira return NULL;
2173b179d48bSDaniel Bristot de Oliveira
2174b179d48bSDaniel Bristot de Oliveira return pos;
2175b179d48bSDaniel Bristot de Oliveira }
2176b179d48bSDaniel Bristot de Oliveira
s_options_show(struct seq_file * s,void * v)2177b179d48bSDaniel Bristot de Oliveira static int s_options_show(struct seq_file *s, void *v)
2178b179d48bSDaniel Bristot de Oliveira {
2179b179d48bSDaniel Bristot de Oliveira loff_t *pos = v;
2180b179d48bSDaniel Bristot de Oliveira int option = *pos;
2181b179d48bSDaniel Bristot de Oliveira
2182b179d48bSDaniel Bristot de Oliveira if (option == OSN_DEFAULTS) {
2183b179d48bSDaniel Bristot de Oliveira if (osnoise_options == OSN_DEFAULT_OPTIONS)
2184b179d48bSDaniel Bristot de Oliveira seq_printf(s, "%s", osnoise_options_str[option]);
2185b179d48bSDaniel Bristot de Oliveira else
2186b179d48bSDaniel Bristot de Oliveira seq_printf(s, "NO_%s", osnoise_options_str[option]);
2187b179d48bSDaniel Bristot de Oliveira goto out;
2188b179d48bSDaniel Bristot de Oliveira }
2189b179d48bSDaniel Bristot de Oliveira
2190b179d48bSDaniel Bristot de Oliveira if (test_bit(option, &osnoise_options))
2191b179d48bSDaniel Bristot de Oliveira seq_printf(s, "%s", osnoise_options_str[option]);
2192b179d48bSDaniel Bristot de Oliveira else
2193b179d48bSDaniel Bristot de Oliveira seq_printf(s, "NO_%s", osnoise_options_str[option]);
2194b179d48bSDaniel Bristot de Oliveira
2195b179d48bSDaniel Bristot de Oliveira out:
2196b179d48bSDaniel Bristot de Oliveira if (option != OSN_MAX)
2197b179d48bSDaniel Bristot de Oliveira seq_puts(s, " ");
2198b179d48bSDaniel Bristot de Oliveira
2199b179d48bSDaniel Bristot de Oliveira return 0;
2200b179d48bSDaniel Bristot de Oliveira }
2201b179d48bSDaniel Bristot de Oliveira
s_options_stop(struct seq_file * s,void * v)2202b179d48bSDaniel Bristot de Oliveira static void s_options_stop(struct seq_file *s, void *v)
2203b179d48bSDaniel Bristot de Oliveira {
2204b179d48bSDaniel Bristot de Oliveira seq_puts(s, "\n");
2205b179d48bSDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2206b179d48bSDaniel Bristot de Oliveira }
2207b179d48bSDaniel Bristot de Oliveira
2208b179d48bSDaniel Bristot de Oliveira static const struct seq_operations osnoise_options_seq_ops = {
2209b179d48bSDaniel Bristot de Oliveira .start = s_options_start,
2210b179d48bSDaniel Bristot de Oliveira .next = s_options_next,
2211b179d48bSDaniel Bristot de Oliveira .show = s_options_show,
2212b179d48bSDaniel Bristot de Oliveira .stop = s_options_stop
2213b179d48bSDaniel Bristot de Oliveira };
2214b179d48bSDaniel Bristot de Oliveira
osnoise_options_open(struct inode * inode,struct file * file)2215b179d48bSDaniel Bristot de Oliveira static int osnoise_options_open(struct inode *inode, struct file *file)
2216b179d48bSDaniel Bristot de Oliveira {
2217b179d48bSDaniel Bristot de Oliveira return seq_open(file, &osnoise_options_seq_ops);
2218b179d48bSDaniel Bristot de Oliveira };
2219b179d48bSDaniel Bristot de Oliveira
2220b179d48bSDaniel Bristot de Oliveira /**
2221b179d48bSDaniel Bristot de Oliveira * osnoise_options_write - Write function for "options" entry
2222b179d48bSDaniel Bristot de Oliveira * @filp: The active open file structure
2223b179d48bSDaniel Bristot de Oliveira * @ubuf: The user buffer that contains the value to write
2224b179d48bSDaniel Bristot de Oliveira * @cnt: The maximum number of bytes to write to "file"
2225b179d48bSDaniel Bristot de Oliveira * @ppos: The current position in @file
2226b179d48bSDaniel Bristot de Oliveira *
2227b179d48bSDaniel Bristot de Oliveira * Writing the option name sets the option, writing the "NO_"
2228b179d48bSDaniel Bristot de Oliveira * prefix in front of the option name disables it.
2229b179d48bSDaniel Bristot de Oliveira *
2230b179d48bSDaniel Bristot de Oliveira * Writing "DEFAULTS" resets the option values to the default ones.
2231b179d48bSDaniel Bristot de Oliveira */
osnoise_options_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)2232b179d48bSDaniel Bristot de Oliveira static ssize_t osnoise_options_write(struct file *filp, const char __user *ubuf,
2233b179d48bSDaniel Bristot de Oliveira size_t cnt, loff_t *ppos)
2234b179d48bSDaniel Bristot de Oliveira {
2235b179d48bSDaniel Bristot de Oliveira int running, option, enable, retval;
2236b179d48bSDaniel Bristot de Oliveira char buf[256], *option_str;
2237b179d48bSDaniel Bristot de Oliveira
2238b179d48bSDaniel Bristot de Oliveira if (cnt >= 256)
2239b179d48bSDaniel Bristot de Oliveira return -EINVAL;
2240b179d48bSDaniel Bristot de Oliveira
2241b179d48bSDaniel Bristot de Oliveira if (copy_from_user(buf, ubuf, cnt))
2242b179d48bSDaniel Bristot de Oliveira return -EFAULT;
2243b179d48bSDaniel Bristot de Oliveira
2244b179d48bSDaniel Bristot de Oliveira buf[cnt] = 0;
2245b179d48bSDaniel Bristot de Oliveira
2246b179d48bSDaniel Bristot de Oliveira if (strncmp(buf, "NO_", 3)) {
2247b179d48bSDaniel Bristot de Oliveira option_str = strstrip(buf);
2248b179d48bSDaniel Bristot de Oliveira enable = true;
2249b179d48bSDaniel Bristot de Oliveira } else {
2250b179d48bSDaniel Bristot de Oliveira option_str = strstrip(&buf[3]);
2251b179d48bSDaniel Bristot de Oliveira enable = false;
2252b179d48bSDaniel Bristot de Oliveira }
2253b179d48bSDaniel Bristot de Oliveira
2254b179d48bSDaniel Bristot de Oliveira option = match_string(osnoise_options_str, OSN_MAX, option_str);
2255b179d48bSDaniel Bristot de Oliveira if (option < 0)
2256b179d48bSDaniel Bristot de Oliveira return -EINVAL;
2257b179d48bSDaniel Bristot de Oliveira
2258b179d48bSDaniel Bristot de Oliveira /*
2259b179d48bSDaniel Bristot de Oliveira * trace_types_lock is taken to avoid concurrency on start/stop.
2260b179d48bSDaniel Bristot de Oliveira */
2261b179d48bSDaniel Bristot de Oliveira mutex_lock(&trace_types_lock);
2262b179d48bSDaniel Bristot de Oliveira running = osnoise_has_registered_instances();
2263b179d48bSDaniel Bristot de Oliveira if (running)
2264b179d48bSDaniel Bristot de Oliveira stop_per_cpu_kthreads();
2265b179d48bSDaniel Bristot de Oliveira
2266b179d48bSDaniel Bristot de Oliveira mutex_lock(&interface_lock);
2267b179d48bSDaniel Bristot de Oliveira /*
2268b179d48bSDaniel Bristot de Oliveira * avoid CPU hotplug operations that might read options.
2269b179d48bSDaniel Bristot de Oliveira */
2270b179d48bSDaniel Bristot de Oliveira cpus_read_lock();
2271b179d48bSDaniel Bristot de Oliveira
2272b179d48bSDaniel Bristot de Oliveira retval = cnt;
2273b179d48bSDaniel Bristot de Oliveira
2274b179d48bSDaniel Bristot de Oliveira if (enable) {
2275b179d48bSDaniel Bristot de Oliveira if (option == OSN_DEFAULTS)
2276b179d48bSDaniel Bristot de Oliveira osnoise_options = OSN_DEFAULT_OPTIONS;
2277b179d48bSDaniel Bristot de Oliveira else
2278b179d48bSDaniel Bristot de Oliveira set_bit(option, &osnoise_options);
2279b179d48bSDaniel Bristot de Oliveira } else {
2280b179d48bSDaniel Bristot de Oliveira if (option == OSN_DEFAULTS)
2281b179d48bSDaniel Bristot de Oliveira retval = -EINVAL;
2282b179d48bSDaniel Bristot de Oliveira else
2283b179d48bSDaniel Bristot de Oliveira clear_bit(option, &osnoise_options);
2284b179d48bSDaniel Bristot de Oliveira }
2285b179d48bSDaniel Bristot de Oliveira
2286b179d48bSDaniel Bristot de Oliveira cpus_read_unlock();
2287b179d48bSDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2288b179d48bSDaniel Bristot de Oliveira
2289b179d48bSDaniel Bristot de Oliveira if (running)
2290b179d48bSDaniel Bristot de Oliveira start_per_cpu_kthreads();
2291b179d48bSDaniel Bristot de Oliveira mutex_unlock(&trace_types_lock);
2292b179d48bSDaniel Bristot de Oliveira
2293b179d48bSDaniel Bristot de Oliveira return retval;
2294b179d48bSDaniel Bristot de Oliveira }
2295b179d48bSDaniel Bristot de Oliveira
2296b179d48bSDaniel Bristot de Oliveira /*
2297bce29ac9SDaniel Bristot de Oliveira * osnoise_cpus_read - Read function for reading the "cpus" file
2298bce29ac9SDaniel Bristot de Oliveira * @filp: The active open file structure
2299bce29ac9SDaniel Bristot de Oliveira * @ubuf: The userspace provided buffer to read value into
2300bce29ac9SDaniel Bristot de Oliveira * @cnt: The maximum number of bytes to read
2301bce29ac9SDaniel Bristot de Oliveira * @ppos: The current "file" position
2302bce29ac9SDaniel Bristot de Oliveira *
2303bce29ac9SDaniel Bristot de Oliveira * Prints the "cpus" output into the user-provided buffer.
2304bce29ac9SDaniel Bristot de Oliveira */
2305bce29ac9SDaniel Bristot de Oliveira static ssize_t
osnoise_cpus_read(struct file * filp,char __user * ubuf,size_t count,loff_t * ppos)2306bce29ac9SDaniel Bristot de Oliveira osnoise_cpus_read(struct file *filp, char __user *ubuf, size_t count,
2307bce29ac9SDaniel Bristot de Oliveira loff_t *ppos)
2308bce29ac9SDaniel Bristot de Oliveira {
2309bce29ac9SDaniel Bristot de Oliveira char *mask_str;
2310bce29ac9SDaniel Bristot de Oliveira int len;
2311bce29ac9SDaniel Bristot de Oliveira
2312bce29ac9SDaniel Bristot de Oliveira mutex_lock(&interface_lock);
2313bce29ac9SDaniel Bristot de Oliveira
2314bce29ac9SDaniel Bristot de Oliveira len = snprintf(NULL, 0, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)) + 1;
2315bce29ac9SDaniel Bristot de Oliveira mask_str = kmalloc(len, GFP_KERNEL);
2316bce29ac9SDaniel Bristot de Oliveira if (!mask_str) {
2317bce29ac9SDaniel Bristot de Oliveira count = -ENOMEM;
2318bce29ac9SDaniel Bristot de Oliveira goto out_unlock;
2319bce29ac9SDaniel Bristot de Oliveira }
2320bce29ac9SDaniel Bristot de Oliveira
2321bce29ac9SDaniel Bristot de Oliveira len = snprintf(mask_str, len, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask));
2322bce29ac9SDaniel Bristot de Oliveira if (len >= count) {
2323bce29ac9SDaniel Bristot de Oliveira count = -EINVAL;
2324bce29ac9SDaniel Bristot de Oliveira goto out_free;
2325bce29ac9SDaniel Bristot de Oliveira }
2326bce29ac9SDaniel Bristot de Oliveira
2327bce29ac9SDaniel Bristot de Oliveira count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
2328bce29ac9SDaniel Bristot de Oliveira
2329bce29ac9SDaniel Bristot de Oliveira out_free:
2330bce29ac9SDaniel Bristot de Oliveira kfree(mask_str);
2331bce29ac9SDaniel Bristot de Oliveira out_unlock:
2332bce29ac9SDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2333bce29ac9SDaniel Bristot de Oliveira
2334bce29ac9SDaniel Bristot de Oliveira return count;
2335bce29ac9SDaniel Bristot de Oliveira }
2336bce29ac9SDaniel Bristot de Oliveira
2337bce29ac9SDaniel Bristot de Oliveira /*
2338bce29ac9SDaniel Bristot de Oliveira * osnoise_cpus_write - Write function for "cpus" entry
2339bce29ac9SDaniel Bristot de Oliveira * @filp: The active open file structure
2340bce29ac9SDaniel Bristot de Oliveira * @ubuf: The user buffer that contains the value to write
2341bce29ac9SDaniel Bristot de Oliveira * @cnt: The maximum number of bytes to write to "file"
2342bce29ac9SDaniel Bristot de Oliveira * @ppos: The current position in @file
2343bce29ac9SDaniel Bristot de Oliveira *
2344bce29ac9SDaniel Bristot de Oliveira * This function provides a write implementation for the "cpus"
2345bce29ac9SDaniel Bristot de Oliveira * interface to the osnoise trace. By default, it lists all CPUs,
2346bce29ac9SDaniel Bristot de Oliveira * in this way, allowing osnoise threads to run on any online CPU
2347bce29ac9SDaniel Bristot de Oliveira * of the system. It serves to restrict the execution of osnoise to the
234866df27f1SDaniel Bristot de Oliveira * set of CPUs writing via this interface. Why not use "tracing_cpumask"?
234966df27f1SDaniel Bristot de Oliveira * Because the user might be interested in tracing what is running on
235066df27f1SDaniel Bristot de Oliveira * other CPUs. For instance, one might run osnoise in one HT CPU
235166df27f1SDaniel Bristot de Oliveira * while observing what is running on the sibling HT CPU.
2352bce29ac9SDaniel Bristot de Oliveira */
2353bce29ac9SDaniel Bristot de Oliveira static ssize_t
osnoise_cpus_write(struct file * filp,const char __user * ubuf,size_t count,loff_t * ppos)2354bce29ac9SDaniel Bristot de Oliveira osnoise_cpus_write(struct file *filp, const char __user *ubuf, size_t count,
2355bce29ac9SDaniel Bristot de Oliveira loff_t *ppos)
2356bce29ac9SDaniel Bristot de Oliveira {
2357bce29ac9SDaniel Bristot de Oliveira cpumask_var_t osnoise_cpumask_new;
2358bce29ac9SDaniel Bristot de Oliveira int running, err;
2359bce29ac9SDaniel Bristot de Oliveira char buf[256];
2360bce29ac9SDaniel Bristot de Oliveira
2361bce29ac9SDaniel Bristot de Oliveira if (count >= 256)
2362bce29ac9SDaniel Bristot de Oliveira return -EINVAL;
2363bce29ac9SDaniel Bristot de Oliveira
2364bce29ac9SDaniel Bristot de Oliveira if (copy_from_user(buf, ubuf, count))
2365bce29ac9SDaniel Bristot de Oliveira return -EFAULT;
2366bce29ac9SDaniel Bristot de Oliveira
2367bce29ac9SDaniel Bristot de Oliveira if (!zalloc_cpumask_var(&osnoise_cpumask_new, GFP_KERNEL))
2368bce29ac9SDaniel Bristot de Oliveira return -ENOMEM;
2369bce29ac9SDaniel Bristot de Oliveira
2370bce29ac9SDaniel Bristot de Oliveira err = cpulist_parse(buf, osnoise_cpumask_new);
2371bce29ac9SDaniel Bristot de Oliveira if (err)
2372bce29ac9SDaniel Bristot de Oliveira goto err_free;
2373bce29ac9SDaniel Bristot de Oliveira
2374bce29ac9SDaniel Bristot de Oliveira /*
2375dae18134SDaniel Bristot de Oliveira * trace_types_lock is taken to avoid concurrency on start/stop.
2376bce29ac9SDaniel Bristot de Oliveira */
2377bce29ac9SDaniel Bristot de Oliveira mutex_lock(&trace_types_lock);
2378dae18134SDaniel Bristot de Oliveira running = osnoise_has_registered_instances();
2379bce29ac9SDaniel Bristot de Oliveira if (running)
23802bd1bdf0SDaniel Bristot de Oliveira stop_per_cpu_kthreads();
2381bce29ac9SDaniel Bristot de Oliveira
2382bce29ac9SDaniel Bristot de Oliveira mutex_lock(&interface_lock);
2383c8895e27SDaniel Bristot de Oliveira /*
2384c8895e27SDaniel Bristot de Oliveira * osnoise_cpumask is read by CPU hotplug operations.
2385c8895e27SDaniel Bristot de Oliveira */
238699c37d1aSSebastian Andrzej Siewior cpus_read_lock();
2387c8895e27SDaniel Bristot de Oliveira
2388bce29ac9SDaniel Bristot de Oliveira cpumask_copy(&osnoise_cpumask, osnoise_cpumask_new);
2389c8895e27SDaniel Bristot de Oliveira
239099c37d1aSSebastian Andrzej Siewior cpus_read_unlock();
2391bce29ac9SDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2392bce29ac9SDaniel Bristot de Oliveira
2393bce29ac9SDaniel Bristot de Oliveira if (running)
23942bd1bdf0SDaniel Bristot de Oliveira start_per_cpu_kthreads();
2395bce29ac9SDaniel Bristot de Oliveira mutex_unlock(&trace_types_lock);
2396bce29ac9SDaniel Bristot de Oliveira
2397bce29ac9SDaniel Bristot de Oliveira free_cpumask_var(osnoise_cpumask_new);
2398bce29ac9SDaniel Bristot de Oliveira return count;
2399bce29ac9SDaniel Bristot de Oliveira
2400bce29ac9SDaniel Bristot de Oliveira err_free:
2401bce29ac9SDaniel Bristot de Oliveira free_cpumask_var(osnoise_cpumask_new);
2402bce29ac9SDaniel Bristot de Oliveira
2403bce29ac9SDaniel Bristot de Oliveira return err;
2404bce29ac9SDaniel Bristot de Oliveira }
2405bce29ac9SDaniel Bristot de Oliveira
2406e88ed227SDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
timerlat_fd_open(struct inode * inode,struct file * file)2407e88ed227SDaniel Bristot de Oliveira static int timerlat_fd_open(struct inode *inode, struct file *file)
2408e88ed227SDaniel Bristot de Oliveira {
2409e88ed227SDaniel Bristot de Oliveira struct osnoise_variables *osn_var;
2410e88ed227SDaniel Bristot de Oliveira struct timerlat_variables *tlat;
2411e88ed227SDaniel Bristot de Oliveira long cpu = (long) inode->i_cdev;
2412e88ed227SDaniel Bristot de Oliveira
2413e88ed227SDaniel Bristot de Oliveira mutex_lock(&interface_lock);
2414e88ed227SDaniel Bristot de Oliveira
2415e88ed227SDaniel Bristot de Oliveira /*
2416e88ed227SDaniel Bristot de Oliveira * This file is accessible only if timerlat is enabled, and
2417e88ed227SDaniel Bristot de Oliveira * NO_OSNOISE_WORKLOAD is set.
2418e88ed227SDaniel Bristot de Oliveira */
2419e88ed227SDaniel Bristot de Oliveira if (!timerlat_enabled() || test_bit(OSN_WORKLOAD, &osnoise_options)) {
2420e88ed227SDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2421e88ed227SDaniel Bristot de Oliveira return -EINVAL;
2422e88ed227SDaniel Bristot de Oliveira }
2423e88ed227SDaniel Bristot de Oliveira
2424e88ed227SDaniel Bristot de Oliveira migrate_disable();
2425e88ed227SDaniel Bristot de Oliveira
2426e88ed227SDaniel Bristot de Oliveira osn_var = this_cpu_osn_var();
2427e88ed227SDaniel Bristot de Oliveira
2428e88ed227SDaniel Bristot de Oliveira /*
2429e88ed227SDaniel Bristot de Oliveira * The osn_var->pid holds the single access to this file.
2430e88ed227SDaniel Bristot de Oliveira */
2431e88ed227SDaniel Bristot de Oliveira if (osn_var->pid) {
2432e88ed227SDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2433e88ed227SDaniel Bristot de Oliveira migrate_enable();
2434e88ed227SDaniel Bristot de Oliveira return -EBUSY;
2435e88ed227SDaniel Bristot de Oliveira }
2436e88ed227SDaniel Bristot de Oliveira
2437e88ed227SDaniel Bristot de Oliveira /*
2438e88ed227SDaniel Bristot de Oliveira * timerlat tracer is a per-cpu tracer. Check if the user-space too
2439e88ed227SDaniel Bristot de Oliveira * is pinned to a single CPU. The tracer laters monitor if the task
2440e88ed227SDaniel Bristot de Oliveira * migrates and then disables tracer if it does. However, it is
2441e88ed227SDaniel Bristot de Oliveira * worth doing this basic acceptance test to avoid obviusly wrong
2442e88ed227SDaniel Bristot de Oliveira * setup.
2443e88ed227SDaniel Bristot de Oliveira */
2444e88ed227SDaniel Bristot de Oliveira if (current->nr_cpus_allowed > 1 || cpu != smp_processor_id()) {
2445e88ed227SDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2446e88ed227SDaniel Bristot de Oliveira migrate_enable();
2447e88ed227SDaniel Bristot de Oliveira return -EPERM;
2448e88ed227SDaniel Bristot de Oliveira }
2449e88ed227SDaniel Bristot de Oliveira
2450e88ed227SDaniel Bristot de Oliveira /*
2451e88ed227SDaniel Bristot de Oliveira * From now on, it is good to go.
2452e88ed227SDaniel Bristot de Oliveira */
2453e88ed227SDaniel Bristot de Oliveira file->private_data = inode->i_cdev;
2454e88ed227SDaniel Bristot de Oliveira
2455e88ed227SDaniel Bristot de Oliveira get_task_struct(current);
2456e88ed227SDaniel Bristot de Oliveira
2457e88ed227SDaniel Bristot de Oliveira osn_var->kthread = current;
2458e88ed227SDaniel Bristot de Oliveira osn_var->pid = current->pid;
2459e88ed227SDaniel Bristot de Oliveira
2460e88ed227SDaniel Bristot de Oliveira /*
2461e88ed227SDaniel Bristot de Oliveira * Setup is done.
2462e88ed227SDaniel Bristot de Oliveira */
2463e88ed227SDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2464e88ed227SDaniel Bristot de Oliveira
2465e88ed227SDaniel Bristot de Oliveira tlat = this_cpu_tmr_var();
2466e88ed227SDaniel Bristot de Oliveira tlat->count = 0;
2467e88ed227SDaniel Bristot de Oliveira
24685f703935SDaniel Bristot de Oliveira hrtimer_init(&tlat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
24695f703935SDaniel Bristot de Oliveira tlat->timer.function = timerlat_irq;
24705f703935SDaniel Bristot de Oliveira
2471e88ed227SDaniel Bristot de Oliveira migrate_enable();
2472e88ed227SDaniel Bristot de Oliveira return 0;
2473e88ed227SDaniel Bristot de Oliveira };
2474e88ed227SDaniel Bristot de Oliveira
2475e88ed227SDaniel Bristot de Oliveira /*
2476e88ed227SDaniel Bristot de Oliveira * timerlat_fd_read - Read function for "timerlat_fd" file
2477e88ed227SDaniel Bristot de Oliveira * @file: The active open file structure
2478e88ed227SDaniel Bristot de Oliveira * @ubuf: The userspace provided buffer to read value into
2479e88ed227SDaniel Bristot de Oliveira * @cnt: The maximum number of bytes to read
2480e88ed227SDaniel Bristot de Oliveira * @ppos: The current "file" position
2481e88ed227SDaniel Bristot de Oliveira *
2482e88ed227SDaniel Bristot de Oliveira * Prints 1 on timerlat, the number of interferences on osnoise, -1 on error.
2483e88ed227SDaniel Bristot de Oliveira */
2484e88ed227SDaniel Bristot de Oliveira static ssize_t
timerlat_fd_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)2485e88ed227SDaniel Bristot de Oliveira timerlat_fd_read(struct file *file, char __user *ubuf, size_t count,
2486e88ed227SDaniel Bristot de Oliveira loff_t *ppos)
2487e88ed227SDaniel Bristot de Oliveira {
2488e88ed227SDaniel Bristot de Oliveira long cpu = (long) file->private_data;
2489e88ed227SDaniel Bristot de Oliveira struct osnoise_variables *osn_var;
2490e88ed227SDaniel Bristot de Oliveira struct timerlat_variables *tlat;
2491e88ed227SDaniel Bristot de Oliveira struct timerlat_sample s;
2492e88ed227SDaniel Bristot de Oliveira s64 diff;
2493e88ed227SDaniel Bristot de Oliveira u64 now;
2494e88ed227SDaniel Bristot de Oliveira
2495e88ed227SDaniel Bristot de Oliveira migrate_disable();
2496e88ed227SDaniel Bristot de Oliveira
2497e88ed227SDaniel Bristot de Oliveira tlat = this_cpu_tmr_var();
2498e88ed227SDaniel Bristot de Oliveira
2499e88ed227SDaniel Bristot de Oliveira /*
2500e88ed227SDaniel Bristot de Oliveira * While in user-space, the thread is migratable. There is nothing
2501e88ed227SDaniel Bristot de Oliveira * we can do about it.
2502e88ed227SDaniel Bristot de Oliveira * So, if the thread is running on another CPU, stop the machinery.
2503e88ed227SDaniel Bristot de Oliveira */
2504e88ed227SDaniel Bristot de Oliveira if (cpu == smp_processor_id()) {
2505e88ed227SDaniel Bristot de Oliveira if (tlat->uthread_migrate) {
2506e88ed227SDaniel Bristot de Oliveira migrate_enable();
2507e88ed227SDaniel Bristot de Oliveira return -EINVAL;
2508e88ed227SDaniel Bristot de Oliveira }
2509e88ed227SDaniel Bristot de Oliveira } else {
2510e88ed227SDaniel Bristot de Oliveira per_cpu_ptr(&per_cpu_timerlat_var, cpu)->uthread_migrate = 1;
2511e88ed227SDaniel Bristot de Oliveira osnoise_taint("timerlat user thread migrate\n");
2512e88ed227SDaniel Bristot de Oliveira osnoise_stop_tracing();
2513e88ed227SDaniel Bristot de Oliveira migrate_enable();
2514e88ed227SDaniel Bristot de Oliveira return -EINVAL;
2515e88ed227SDaniel Bristot de Oliveira }
2516e88ed227SDaniel Bristot de Oliveira
2517e88ed227SDaniel Bristot de Oliveira osn_var = this_cpu_osn_var();
2518e88ed227SDaniel Bristot de Oliveira
2519e88ed227SDaniel Bristot de Oliveira /*
2520e88ed227SDaniel Bristot de Oliveira * The timerlat in user-space runs in a different order:
2521e88ed227SDaniel Bristot de Oliveira * the read() starts from the execution of the previous occurrence,
2522e88ed227SDaniel Bristot de Oliveira * sleeping for the next occurrence.
2523e88ed227SDaniel Bristot de Oliveira *
2524e88ed227SDaniel Bristot de Oliveira * So, skip if we are entering on read() before the first wakeup
2525e88ed227SDaniel Bristot de Oliveira * from timerlat IRQ:
2526e88ed227SDaniel Bristot de Oliveira */
2527e88ed227SDaniel Bristot de Oliveira if (likely(osn_var->sampling)) {
2528e88ed227SDaniel Bristot de Oliveira now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
2529e88ed227SDaniel Bristot de Oliveira diff = now - tlat->abs_period;
2530e88ed227SDaniel Bristot de Oliveira
2531e88ed227SDaniel Bristot de Oliveira /*
2532e88ed227SDaniel Bristot de Oliveira * it was not a timer firing, but some other signal?
2533e88ed227SDaniel Bristot de Oliveira */
2534e88ed227SDaniel Bristot de Oliveira if (diff < 0)
2535e88ed227SDaniel Bristot de Oliveira goto out;
2536e88ed227SDaniel Bristot de Oliveira
2537e88ed227SDaniel Bristot de Oliveira s.seqnum = tlat->count;
2538e88ed227SDaniel Bristot de Oliveira s.timer_latency = diff;
2539e88ed227SDaniel Bristot de Oliveira s.context = THREAD_URET;
2540e88ed227SDaniel Bristot de Oliveira
2541e88ed227SDaniel Bristot de Oliveira trace_timerlat_sample(&s);
2542e88ed227SDaniel Bristot de Oliveira
2543e88ed227SDaniel Bristot de Oliveira notify_new_max_latency(diff);
2544e88ed227SDaniel Bristot de Oliveira
2545e88ed227SDaniel Bristot de Oliveira tlat->tracing_thread = false;
2546e88ed227SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing_total)
2547e88ed227SDaniel Bristot de Oliveira if (time_to_us(diff) >= osnoise_data.stop_tracing_total)
2548e88ed227SDaniel Bristot de Oliveira osnoise_stop_tracing();
2549e88ed227SDaniel Bristot de Oliveira } else {
2550e88ed227SDaniel Bristot de Oliveira tlat->tracing_thread = false;
2551e88ed227SDaniel Bristot de Oliveira tlat->kthread = current;
2552e88ed227SDaniel Bristot de Oliveira
2553e88ed227SDaniel Bristot de Oliveira /* Annotate now to drift new period */
2554e88ed227SDaniel Bristot de Oliveira tlat->abs_period = hrtimer_cb_get_time(&tlat->timer);
2555e88ed227SDaniel Bristot de Oliveira
2556e88ed227SDaniel Bristot de Oliveira osn_var->sampling = 1;
2557e88ed227SDaniel Bristot de Oliveira }
2558e88ed227SDaniel Bristot de Oliveira
2559e88ed227SDaniel Bristot de Oliveira /* wait for the next period */
2560e88ed227SDaniel Bristot de Oliveira wait_next_period(tlat);
2561e88ed227SDaniel Bristot de Oliveira
2562e88ed227SDaniel Bristot de Oliveira /* This is the wakeup from this cycle */
2563e88ed227SDaniel Bristot de Oliveira now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
2564e88ed227SDaniel Bristot de Oliveira diff = now - tlat->abs_period;
2565e88ed227SDaniel Bristot de Oliveira
2566e88ed227SDaniel Bristot de Oliveira /*
2567e88ed227SDaniel Bristot de Oliveira * it was not a timer firing, but some other signal?
2568e88ed227SDaniel Bristot de Oliveira */
2569e88ed227SDaniel Bristot de Oliveira if (diff < 0)
2570e88ed227SDaniel Bristot de Oliveira goto out;
2571e88ed227SDaniel Bristot de Oliveira
2572e88ed227SDaniel Bristot de Oliveira s.seqnum = tlat->count;
2573e88ed227SDaniel Bristot de Oliveira s.timer_latency = diff;
2574e88ed227SDaniel Bristot de Oliveira s.context = THREAD_CONTEXT;
2575e88ed227SDaniel Bristot de Oliveira
2576e88ed227SDaniel Bristot de Oliveira trace_timerlat_sample(&s);
2577e88ed227SDaniel Bristot de Oliveira
2578e88ed227SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing_total) {
2579e88ed227SDaniel Bristot de Oliveira if (time_to_us(diff) >= osnoise_data.stop_tracing_total) {
2580e88ed227SDaniel Bristot de Oliveira timerlat_dump_stack(time_to_us(diff));
2581e88ed227SDaniel Bristot de Oliveira notify_new_max_latency(diff);
2582e88ed227SDaniel Bristot de Oliveira osnoise_stop_tracing();
2583e88ed227SDaniel Bristot de Oliveira }
2584e88ed227SDaniel Bristot de Oliveira }
2585e88ed227SDaniel Bristot de Oliveira
2586e88ed227SDaniel Bristot de Oliveira out:
2587e88ed227SDaniel Bristot de Oliveira migrate_enable();
2588e88ed227SDaniel Bristot de Oliveira return 0;
2589e88ed227SDaniel Bristot de Oliveira }
2590e88ed227SDaniel Bristot de Oliveira
timerlat_fd_release(struct inode * inode,struct file * file)2591e88ed227SDaniel Bristot de Oliveira static int timerlat_fd_release(struct inode *inode, struct file *file)
2592e88ed227SDaniel Bristot de Oliveira {
2593e88ed227SDaniel Bristot de Oliveira struct osnoise_variables *osn_var;
2594e88ed227SDaniel Bristot de Oliveira struct timerlat_variables *tlat_var;
2595e88ed227SDaniel Bristot de Oliveira long cpu = (long) file->private_data;
2596e88ed227SDaniel Bristot de Oliveira
2597e88ed227SDaniel Bristot de Oliveira migrate_disable();
2598e88ed227SDaniel Bristot de Oliveira mutex_lock(&interface_lock);
2599e88ed227SDaniel Bristot de Oliveira
2600e88ed227SDaniel Bristot de Oliveira osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
2601e88ed227SDaniel Bristot de Oliveira tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu);
2602e88ed227SDaniel Bristot de Oliveira
26038c72f0b2SSteven Rostedt if (tlat_var->kthread)
2604e88ed227SDaniel Bristot de Oliveira hrtimer_cancel(&tlat_var->timer);
2605e88ed227SDaniel Bristot de Oliveira memset(tlat_var, 0, sizeof(*tlat_var));
2606e88ed227SDaniel Bristot de Oliveira
2607e88ed227SDaniel Bristot de Oliveira osn_var->sampling = 0;
2608e88ed227SDaniel Bristot de Oliveira osn_var->pid = 0;
2609e88ed227SDaniel Bristot de Oliveira
2610e88ed227SDaniel Bristot de Oliveira /*
2611e88ed227SDaniel Bristot de Oliveira * We are leaving, not being stopped... see stop_kthread();
2612e88ed227SDaniel Bristot de Oliveira */
2613e88ed227SDaniel Bristot de Oliveira if (osn_var->kthread) {
2614e88ed227SDaniel Bristot de Oliveira put_task_struct(osn_var->kthread);
2615e88ed227SDaniel Bristot de Oliveira osn_var->kthread = NULL;
2616e88ed227SDaniel Bristot de Oliveira }
2617e88ed227SDaniel Bristot de Oliveira
2618e88ed227SDaniel Bristot de Oliveira mutex_unlock(&interface_lock);
2619e88ed227SDaniel Bristot de Oliveira migrate_enable();
2620e88ed227SDaniel Bristot de Oliveira return 0;
2621e88ed227SDaniel Bristot de Oliveira }
2622e88ed227SDaniel Bristot de Oliveira #endif
2623e88ed227SDaniel Bristot de Oliveira
2624bce29ac9SDaniel Bristot de Oliveira /*
2625bce29ac9SDaniel Bristot de Oliveira * osnoise/runtime_us: cannot be greater than the period.
2626bce29ac9SDaniel Bristot de Oliveira */
2627bce29ac9SDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_runtime = {
2628bce29ac9SDaniel Bristot de Oliveira .lock = &interface_lock,
2629bce29ac9SDaniel Bristot de Oliveira .val = &osnoise_data.sample_runtime,
2630bce29ac9SDaniel Bristot de Oliveira .max = &osnoise_data.sample_period,
2631bce29ac9SDaniel Bristot de Oliveira .min = NULL,
2632bce29ac9SDaniel Bristot de Oliveira };
2633bce29ac9SDaniel Bristot de Oliveira
2634bce29ac9SDaniel Bristot de Oliveira /*
2635bce29ac9SDaniel Bristot de Oliveira * osnoise/period_us: cannot be smaller than the runtime.
2636bce29ac9SDaniel Bristot de Oliveira */
2637bce29ac9SDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_period = {
2638bce29ac9SDaniel Bristot de Oliveira .lock = &interface_lock,
2639bce29ac9SDaniel Bristot de Oliveira .val = &osnoise_data.sample_period,
2640bce29ac9SDaniel Bristot de Oliveira .max = NULL,
2641bce29ac9SDaniel Bristot de Oliveira .min = &osnoise_data.sample_runtime,
2642bce29ac9SDaniel Bristot de Oliveira };
2643bce29ac9SDaniel Bristot de Oliveira
2644bce29ac9SDaniel Bristot de Oliveira /*
2645bce29ac9SDaniel Bristot de Oliveira * osnoise/stop_tracing_us: no limit.
2646bce29ac9SDaniel Bristot de Oliveira */
2647bce29ac9SDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_stop_tracing_in = {
2648bce29ac9SDaniel Bristot de Oliveira .lock = &interface_lock,
2649bce29ac9SDaniel Bristot de Oliveira .val = &osnoise_data.stop_tracing,
2650bce29ac9SDaniel Bristot de Oliveira .max = NULL,
2651bce29ac9SDaniel Bristot de Oliveira .min = NULL,
2652bce29ac9SDaniel Bristot de Oliveira };
2653bce29ac9SDaniel Bristot de Oliveira
2654bce29ac9SDaniel Bristot de Oliveira /*
2655bce29ac9SDaniel Bristot de Oliveira * osnoise/stop_tracing_total_us: no limit.
2656bce29ac9SDaniel Bristot de Oliveira */
2657bce29ac9SDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_stop_tracing_total = {
2658bce29ac9SDaniel Bristot de Oliveira .lock = &interface_lock,
2659bce29ac9SDaniel Bristot de Oliveira .val = &osnoise_data.stop_tracing_total,
2660bce29ac9SDaniel Bristot de Oliveira .max = NULL,
2661bce29ac9SDaniel Bristot de Oliveira .min = NULL,
2662bce29ac9SDaniel Bristot de Oliveira };
2663bce29ac9SDaniel Bristot de Oliveira
2664a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
2665a955d7eaSDaniel Bristot de Oliveira /*
2666a955d7eaSDaniel Bristot de Oliveira * osnoise/print_stack: print the stacktrace of the IRQ handler if the total
2667a955d7eaSDaniel Bristot de Oliveira * latency is higher than val.
2668a955d7eaSDaniel Bristot de Oliveira */
2669a955d7eaSDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_print_stack = {
2670a955d7eaSDaniel Bristot de Oliveira .lock = &interface_lock,
2671a955d7eaSDaniel Bristot de Oliveira .val = &osnoise_data.print_stack,
2672a955d7eaSDaniel Bristot de Oliveira .max = NULL,
2673a955d7eaSDaniel Bristot de Oliveira .min = NULL,
2674a955d7eaSDaniel Bristot de Oliveira };
2675a955d7eaSDaniel Bristot de Oliveira
2676a955d7eaSDaniel Bristot de Oliveira /*
2677a955d7eaSDaniel Bristot de Oliveira * osnoise/timerlat_period: min 100 us, max 1 s
2678a955d7eaSDaniel Bristot de Oliveira */
26797a025e06STom Rix static u64 timerlat_min_period = 100;
26807a025e06STom Rix static u64 timerlat_max_period = 1000000;
2681a955d7eaSDaniel Bristot de Oliveira static struct trace_min_max_param timerlat_period = {
2682a955d7eaSDaniel Bristot de Oliveira .lock = &interface_lock,
2683a955d7eaSDaniel Bristot de Oliveira .val = &osnoise_data.timerlat_period,
2684a955d7eaSDaniel Bristot de Oliveira .max = &timerlat_max_period,
2685a955d7eaSDaniel Bristot de Oliveira .min = &timerlat_min_period,
2686a955d7eaSDaniel Bristot de Oliveira };
2687e88ed227SDaniel Bristot de Oliveira
2688e88ed227SDaniel Bristot de Oliveira static const struct file_operations timerlat_fd_fops = {
2689e88ed227SDaniel Bristot de Oliveira .open = timerlat_fd_open,
2690e88ed227SDaniel Bristot de Oliveira .read = timerlat_fd_read,
2691e88ed227SDaniel Bristot de Oliveira .release = timerlat_fd_release,
2692e88ed227SDaniel Bristot de Oliveira .llseek = generic_file_llseek,
2693e88ed227SDaniel Bristot de Oliveira };
2694a955d7eaSDaniel Bristot de Oliveira #endif
2695a955d7eaSDaniel Bristot de Oliveira
2696bce29ac9SDaniel Bristot de Oliveira static const struct file_operations cpus_fops = {
2697bce29ac9SDaniel Bristot de Oliveira .open = tracing_open_generic,
2698bce29ac9SDaniel Bristot de Oliveira .read = osnoise_cpus_read,
2699bce29ac9SDaniel Bristot de Oliveira .write = osnoise_cpus_write,
2700bce29ac9SDaniel Bristot de Oliveira .llseek = generic_file_llseek,
2701bce29ac9SDaniel Bristot de Oliveira };
2702bce29ac9SDaniel Bristot de Oliveira
2703b179d48bSDaniel Bristot de Oliveira static const struct file_operations osnoise_options_fops = {
2704b179d48bSDaniel Bristot de Oliveira .open = osnoise_options_open,
2705b179d48bSDaniel Bristot de Oliveira .read = seq_read,
2706b179d48bSDaniel Bristot de Oliveira .llseek = seq_lseek,
2707b179d48bSDaniel Bristot de Oliveira .release = seq_release,
2708b179d48bSDaniel Bristot de Oliveira .write = osnoise_options_write
2709b179d48bSDaniel Bristot de Oliveira };
2710b179d48bSDaniel Bristot de Oliveira
2711ccb67544SDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
2712b14f4568SDaniel Bristot de Oliveira #ifdef CONFIG_STACKTRACE
init_timerlat_stack_tracefs(struct dentry * top_dir)2713b14f4568SDaniel Bristot de Oliveira static int init_timerlat_stack_tracefs(struct dentry *top_dir)
2714b14f4568SDaniel Bristot de Oliveira {
2715b14f4568SDaniel Bristot de Oliveira struct dentry *tmp;
2716b14f4568SDaniel Bristot de Oliveira
2717b14f4568SDaniel Bristot de Oliveira tmp = tracefs_create_file("print_stack", TRACE_MODE_WRITE, top_dir,
2718b14f4568SDaniel Bristot de Oliveira &osnoise_print_stack, &trace_min_max_fops);
2719b14f4568SDaniel Bristot de Oliveira if (!tmp)
2720b14f4568SDaniel Bristot de Oliveira return -ENOMEM;
2721b14f4568SDaniel Bristot de Oliveira
2722b14f4568SDaniel Bristot de Oliveira return 0;
2723b14f4568SDaniel Bristot de Oliveira }
2724b14f4568SDaniel Bristot de Oliveira #else /* CONFIG_STACKTRACE */
init_timerlat_stack_tracefs(struct dentry * top_dir)2725b14f4568SDaniel Bristot de Oliveira static int init_timerlat_stack_tracefs(struct dentry *top_dir)
2726b14f4568SDaniel Bristot de Oliveira {
2727b14f4568SDaniel Bristot de Oliveira return 0;
2728b14f4568SDaniel Bristot de Oliveira }
2729b14f4568SDaniel Bristot de Oliveira #endif /* CONFIG_STACKTRACE */
2730b14f4568SDaniel Bristot de Oliveira
osnoise_create_cpu_timerlat_fd(struct dentry * top_dir)2731e88ed227SDaniel Bristot de Oliveira static int osnoise_create_cpu_timerlat_fd(struct dentry *top_dir)
2732e88ed227SDaniel Bristot de Oliveira {
2733e88ed227SDaniel Bristot de Oliveira struct dentry *timerlat_fd;
2734e88ed227SDaniel Bristot de Oliveira struct dentry *per_cpu;
2735e88ed227SDaniel Bristot de Oliveira struct dentry *cpu_dir;
2736e88ed227SDaniel Bristot de Oliveira char cpu_str[30]; /* see trace.c: tracing_init_tracefs_percpu() */
2737e88ed227SDaniel Bristot de Oliveira long cpu;
2738e88ed227SDaniel Bristot de Oliveira
2739e88ed227SDaniel Bristot de Oliveira /*
2740e88ed227SDaniel Bristot de Oliveira * Why not using tracing instance per_cpu/ dir?
2741e88ed227SDaniel Bristot de Oliveira *
2742e88ed227SDaniel Bristot de Oliveira * Because osnoise/timerlat have a single workload, having
2743e88ed227SDaniel Bristot de Oliveira * multiple files like these are wast of memory.
2744e88ed227SDaniel Bristot de Oliveira */
2745e88ed227SDaniel Bristot de Oliveira per_cpu = tracefs_create_dir("per_cpu", top_dir);
2746e88ed227SDaniel Bristot de Oliveira if (!per_cpu)
2747e88ed227SDaniel Bristot de Oliveira return -ENOMEM;
2748e88ed227SDaniel Bristot de Oliveira
2749e88ed227SDaniel Bristot de Oliveira for_each_possible_cpu(cpu) {
2750e88ed227SDaniel Bristot de Oliveira snprintf(cpu_str, 30, "cpu%ld", cpu);
2751e88ed227SDaniel Bristot de Oliveira cpu_dir = tracefs_create_dir(cpu_str, per_cpu);
2752e88ed227SDaniel Bristot de Oliveira if (!cpu_dir)
2753e88ed227SDaniel Bristot de Oliveira goto out_clean;
2754e88ed227SDaniel Bristot de Oliveira
2755e88ed227SDaniel Bristot de Oliveira timerlat_fd = trace_create_file("timerlat_fd", TRACE_MODE_READ,
2756e88ed227SDaniel Bristot de Oliveira cpu_dir, NULL, &timerlat_fd_fops);
2757e88ed227SDaniel Bristot de Oliveira if (!timerlat_fd)
2758e88ed227SDaniel Bristot de Oliveira goto out_clean;
2759e88ed227SDaniel Bristot de Oliveira
2760e88ed227SDaniel Bristot de Oliveira /* Record the CPU */
2761e88ed227SDaniel Bristot de Oliveira d_inode(timerlat_fd)->i_cdev = (void *)(cpu);
2762e88ed227SDaniel Bristot de Oliveira }
2763e88ed227SDaniel Bristot de Oliveira
2764e88ed227SDaniel Bristot de Oliveira return 0;
2765e88ed227SDaniel Bristot de Oliveira
2766e88ed227SDaniel Bristot de Oliveira out_clean:
2767e88ed227SDaniel Bristot de Oliveira tracefs_remove(per_cpu);
2768e88ed227SDaniel Bristot de Oliveira return -ENOMEM;
2769e88ed227SDaniel Bristot de Oliveira }
2770e88ed227SDaniel Bristot de Oliveira
2771ccb67544SDaniel Bristot de Oliveira /*
2772ccb67544SDaniel Bristot de Oliveira * init_timerlat_tracefs - A function to initialize the timerlat interface files
2773ccb67544SDaniel Bristot de Oliveira */
init_timerlat_tracefs(struct dentry * top_dir)2774ccb67544SDaniel Bristot de Oliveira static int init_timerlat_tracefs(struct dentry *top_dir)
2775ccb67544SDaniel Bristot de Oliveira {
2776ccb67544SDaniel Bristot de Oliveira struct dentry *tmp;
2777e88ed227SDaniel Bristot de Oliveira int retval;
2778ccb67544SDaniel Bristot de Oliveira
2779ccb67544SDaniel Bristot de Oliveira tmp = tracefs_create_file("timerlat_period_us", TRACE_MODE_WRITE, top_dir,
2780ccb67544SDaniel Bristot de Oliveira &timerlat_period, &trace_min_max_fops);
2781ccb67544SDaniel Bristot de Oliveira if (!tmp)
2782ccb67544SDaniel Bristot de Oliveira return -ENOMEM;
2783ccb67544SDaniel Bristot de Oliveira
2784e88ed227SDaniel Bristot de Oliveira retval = osnoise_create_cpu_timerlat_fd(top_dir);
2785e88ed227SDaniel Bristot de Oliveira if (retval)
2786e88ed227SDaniel Bristot de Oliveira return retval;
2787e88ed227SDaniel Bristot de Oliveira
2788b14f4568SDaniel Bristot de Oliveira return init_timerlat_stack_tracefs(top_dir);
2789ccb67544SDaniel Bristot de Oliveira }
2790ccb67544SDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */
init_timerlat_tracefs(struct dentry * top_dir)2791ccb67544SDaniel Bristot de Oliveira static int init_timerlat_tracefs(struct dentry *top_dir)
2792ccb67544SDaniel Bristot de Oliveira {
2793ccb67544SDaniel Bristot de Oliveira return 0;
2794ccb67544SDaniel Bristot de Oliveira }
2795ccb67544SDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */
2796ccb67544SDaniel Bristot de Oliveira
2797bce29ac9SDaniel Bristot de Oliveira /*
2798bce29ac9SDaniel Bristot de Oliveira * init_tracefs - A function to initialize the tracefs interface files
2799bce29ac9SDaniel Bristot de Oliveira *
2800a955d7eaSDaniel Bristot de Oliveira * This function creates entries in tracefs for "osnoise" and "timerlat".
2801a955d7eaSDaniel Bristot de Oliveira * It creates these directories in the tracing directory, and within that
2802a955d7eaSDaniel Bristot de Oliveira * directory the use can change and view the configs.
2803bce29ac9SDaniel Bristot de Oliveira */
init_tracefs(void)2804bce29ac9SDaniel Bristot de Oliveira static int init_tracefs(void)
2805bce29ac9SDaniel Bristot de Oliveira {
2806bce29ac9SDaniel Bristot de Oliveira struct dentry *top_dir;
2807bce29ac9SDaniel Bristot de Oliveira struct dentry *tmp;
2808bce29ac9SDaniel Bristot de Oliveira int ret;
2809bce29ac9SDaniel Bristot de Oliveira
2810bce29ac9SDaniel Bristot de Oliveira ret = tracing_init_dentry();
2811bce29ac9SDaniel Bristot de Oliveira if (ret)
2812bce29ac9SDaniel Bristot de Oliveira return -ENOMEM;
2813bce29ac9SDaniel Bristot de Oliveira
2814bce29ac9SDaniel Bristot de Oliveira top_dir = tracefs_create_dir("osnoise", NULL);
2815bce29ac9SDaniel Bristot de Oliveira if (!top_dir)
2816a955d7eaSDaniel Bristot de Oliveira return 0;
2817bce29ac9SDaniel Bristot de Oliveira
281821ccc9cdSSteven Rostedt (VMware) tmp = tracefs_create_file("period_us", TRACE_MODE_WRITE, top_dir,
2819bce29ac9SDaniel Bristot de Oliveira &osnoise_period, &trace_min_max_fops);
2820bce29ac9SDaniel Bristot de Oliveira if (!tmp)
2821bce29ac9SDaniel Bristot de Oliveira goto err;
2822bce29ac9SDaniel Bristot de Oliveira
282321ccc9cdSSteven Rostedt (VMware) tmp = tracefs_create_file("runtime_us", TRACE_MODE_WRITE, top_dir,
2824bce29ac9SDaniel Bristot de Oliveira &osnoise_runtime, &trace_min_max_fops);
2825bce29ac9SDaniel Bristot de Oliveira if (!tmp)
2826bce29ac9SDaniel Bristot de Oliveira goto err;
2827bce29ac9SDaniel Bristot de Oliveira
282821ccc9cdSSteven Rostedt (VMware) tmp = tracefs_create_file("stop_tracing_us", TRACE_MODE_WRITE, top_dir,
2829bce29ac9SDaniel Bristot de Oliveira &osnoise_stop_tracing_in, &trace_min_max_fops);
2830bce29ac9SDaniel Bristot de Oliveira if (!tmp)
2831bce29ac9SDaniel Bristot de Oliveira goto err;
2832bce29ac9SDaniel Bristot de Oliveira
283321ccc9cdSSteven Rostedt (VMware) tmp = tracefs_create_file("stop_tracing_total_us", TRACE_MODE_WRITE, top_dir,
2834bce29ac9SDaniel Bristot de Oliveira &osnoise_stop_tracing_total, &trace_min_max_fops);
2835bce29ac9SDaniel Bristot de Oliveira if (!tmp)
2836bce29ac9SDaniel Bristot de Oliveira goto err;
2837bce29ac9SDaniel Bristot de Oliveira
283821ccc9cdSSteven Rostedt (VMware) tmp = trace_create_file("cpus", TRACE_MODE_WRITE, top_dir, NULL, &cpus_fops);
2839bce29ac9SDaniel Bristot de Oliveira if (!tmp)
2840bce29ac9SDaniel Bristot de Oliveira goto err;
2841a955d7eaSDaniel Bristot de Oliveira
2842b179d48bSDaniel Bristot de Oliveira tmp = trace_create_file("options", TRACE_MODE_WRITE, top_dir, NULL,
2843b179d48bSDaniel Bristot de Oliveira &osnoise_options_fops);
2844b179d48bSDaniel Bristot de Oliveira if (!tmp)
2845b179d48bSDaniel Bristot de Oliveira goto err;
2846b179d48bSDaniel Bristot de Oliveira
2847ccb67544SDaniel Bristot de Oliveira ret = init_timerlat_tracefs(top_dir);
2848ccb67544SDaniel Bristot de Oliveira if (ret)
2849a955d7eaSDaniel Bristot de Oliveira goto err;
2850bce29ac9SDaniel Bristot de Oliveira
2851bce29ac9SDaniel Bristot de Oliveira return 0;
2852bce29ac9SDaniel Bristot de Oliveira
2853bce29ac9SDaniel Bristot de Oliveira err:
2854bce29ac9SDaniel Bristot de Oliveira tracefs_remove(top_dir);
2855bce29ac9SDaniel Bristot de Oliveira return -ENOMEM;
2856bce29ac9SDaniel Bristot de Oliveira }
2857bce29ac9SDaniel Bristot de Oliveira
osnoise_hook_events(void)2858bce29ac9SDaniel Bristot de Oliveira static int osnoise_hook_events(void)
2859bce29ac9SDaniel Bristot de Oliveira {
2860bce29ac9SDaniel Bristot de Oliveira int retval;
2861bce29ac9SDaniel Bristot de Oliveira
2862bce29ac9SDaniel Bristot de Oliveira /*
2863bce29ac9SDaniel Bristot de Oliveira * Trace is already hooked, we are re-enabling from
2864bce29ac9SDaniel Bristot de Oliveira * a stop_tracing_*.
2865bce29ac9SDaniel Bristot de Oliveira */
2866bce29ac9SDaniel Bristot de Oliveira if (trace_osnoise_callback_enabled)
2867bce29ac9SDaniel Bristot de Oliveira return 0;
2868bce29ac9SDaniel Bristot de Oliveira
2869bce29ac9SDaniel Bristot de Oliveira retval = hook_irq_events();
2870bce29ac9SDaniel Bristot de Oliveira if (retval)
2871bce29ac9SDaniel Bristot de Oliveira return -EINVAL;
2872bce29ac9SDaniel Bristot de Oliveira
2873bce29ac9SDaniel Bristot de Oliveira retval = hook_softirq_events();
2874bce29ac9SDaniel Bristot de Oliveira if (retval)
2875bce29ac9SDaniel Bristot de Oliveira goto out_unhook_irq;
2876bce29ac9SDaniel Bristot de Oliveira
2877bce29ac9SDaniel Bristot de Oliveira retval = hook_thread_events();
2878bce29ac9SDaniel Bristot de Oliveira /*
2879bce29ac9SDaniel Bristot de Oliveira * All fine!
2880bce29ac9SDaniel Bristot de Oliveira */
2881bce29ac9SDaniel Bristot de Oliveira if (!retval)
2882bce29ac9SDaniel Bristot de Oliveira return 0;
2883bce29ac9SDaniel Bristot de Oliveira
2884bce29ac9SDaniel Bristot de Oliveira unhook_softirq_events();
2885bce29ac9SDaniel Bristot de Oliveira out_unhook_irq:
2886bce29ac9SDaniel Bristot de Oliveira unhook_irq_events();
2887bce29ac9SDaniel Bristot de Oliveira return -EINVAL;
2888bce29ac9SDaniel Bristot de Oliveira }
2889bce29ac9SDaniel Bristot de Oliveira
osnoise_unhook_events(void)28900878355bSNikita Yushchenko static void osnoise_unhook_events(void)
28910878355bSNikita Yushchenko {
28920878355bSNikita Yushchenko unhook_thread_events();
28930878355bSNikita Yushchenko unhook_softirq_events();
28940878355bSNikita Yushchenko unhook_irq_events();
28950878355bSNikita Yushchenko }
28960878355bSNikita Yushchenko
289715ca4bdbSDaniel Bristot de Oliveira /*
289815ca4bdbSDaniel Bristot de Oliveira * osnoise_workload_start - start the workload and hook to events
289915ca4bdbSDaniel Bristot de Oliveira */
osnoise_workload_start(void)290015ca4bdbSDaniel Bristot de Oliveira static int osnoise_workload_start(void)
2901bce29ac9SDaniel Bristot de Oliveira {
2902bce29ac9SDaniel Bristot de Oliveira int retval;
2903bce29ac9SDaniel Bristot de Oliveira
29042fac8d64SDaniel Bristot de Oliveira /*
29052fac8d64SDaniel Bristot de Oliveira * Instances need to be registered after calling workload
29062fac8d64SDaniel Bristot de Oliveira * start. Hence, if there is already an instance, the
29072fac8d64SDaniel Bristot de Oliveira * workload was already registered. Otherwise, this
29082fac8d64SDaniel Bristot de Oliveira * code is on the way to register the first instance,
29092fac8d64SDaniel Bristot de Oliveira * and the workload will start.
29102fac8d64SDaniel Bristot de Oliveira */
29112fac8d64SDaniel Bristot de Oliveira if (osnoise_has_registered_instances())
29122fac8d64SDaniel Bristot de Oliveira return 0;
29132fac8d64SDaniel Bristot de Oliveira
2914bce29ac9SDaniel Bristot de Oliveira osn_var_reset_all();
2915bce29ac9SDaniel Bristot de Oliveira
2916bce29ac9SDaniel Bristot de Oliveira retval = osnoise_hook_events();
2917bce29ac9SDaniel Bristot de Oliveira if (retval)
2918a955d7eaSDaniel Bristot de Oliveira return retval;
2919c3b6343cSDaniel Bristot de Oliveira
2920bce29ac9SDaniel Bristot de Oliveira /*
2921c3b6343cSDaniel Bristot de Oliveira * Make sure that ftrace_nmi_enter/exit() see reset values
2922c3b6343cSDaniel Bristot de Oliveira * before enabling trace_osnoise_callback_enabled.
2923bce29ac9SDaniel Bristot de Oliveira */
2924bce29ac9SDaniel Bristot de Oliveira barrier();
2925bce29ac9SDaniel Bristot de Oliveira trace_osnoise_callback_enabled = true;
2926bce29ac9SDaniel Bristot de Oliveira
292715ca4bdbSDaniel Bristot de Oliveira retval = start_per_cpu_kthreads();
2928a955d7eaSDaniel Bristot de Oliveira if (retval) {
29290878355bSNikita Yushchenko trace_osnoise_callback_enabled = false;
29300878355bSNikita Yushchenko /*
29310878355bSNikita Yushchenko * Make sure that ftrace_nmi_enter/exit() see
29320878355bSNikita Yushchenko * trace_osnoise_callback_enabled as false before continuing.
29330878355bSNikita Yushchenko */
29340878355bSNikita Yushchenko barrier();
29350878355bSNikita Yushchenko
29360878355bSNikita Yushchenko osnoise_unhook_events();
2937a955d7eaSDaniel Bristot de Oliveira return retval;
2938a955d7eaSDaniel Bristot de Oliveira }
2939a955d7eaSDaniel Bristot de Oliveira
2940a955d7eaSDaniel Bristot de Oliveira return 0;
2941a955d7eaSDaniel Bristot de Oliveira }
2942a955d7eaSDaniel Bristot de Oliveira
294315ca4bdbSDaniel Bristot de Oliveira /*
294415ca4bdbSDaniel Bristot de Oliveira * osnoise_workload_stop - stop the workload and unhook the events
294515ca4bdbSDaniel Bristot de Oliveira */
osnoise_workload_stop(void)294615ca4bdbSDaniel Bristot de Oliveira static void osnoise_workload_stop(void)
2947bce29ac9SDaniel Bristot de Oliveira {
29482fac8d64SDaniel Bristot de Oliveira /*
29492fac8d64SDaniel Bristot de Oliveira * Instances need to be unregistered before calling
29502fac8d64SDaniel Bristot de Oliveira * stop. Hence, if there is a registered instance, more
29512fac8d64SDaniel Bristot de Oliveira * than one instance is running, and the workload will not
29522fac8d64SDaniel Bristot de Oliveira * yet stop. Otherwise, this code is on the way to disable
29532fac8d64SDaniel Bristot de Oliveira * the last instance, and the workload can stop.
29542fac8d64SDaniel Bristot de Oliveira */
2955dae18134SDaniel Bristot de Oliveira if (osnoise_has_registered_instances())
2956bce29ac9SDaniel Bristot de Oliveira return;
2957bce29ac9SDaniel Bristot de Oliveira
2958f0cfe17bSDaniel Bristot de Oliveira /*
2959f0cfe17bSDaniel Bristot de Oliveira * If callbacks were already disabled in a previous stop
2960f0cfe17bSDaniel Bristot de Oliveira * call, there is no need to disable then again.
2961f0cfe17bSDaniel Bristot de Oliveira *
2962f0cfe17bSDaniel Bristot de Oliveira * For instance, this happens when tracing is stopped via:
2963f0cfe17bSDaniel Bristot de Oliveira * echo 0 > tracing_on
2964f0cfe17bSDaniel Bristot de Oliveira * echo nop > current_tracer.
2965f0cfe17bSDaniel Bristot de Oliveira */
2966f0cfe17bSDaniel Bristot de Oliveira if (!trace_osnoise_callback_enabled)
2967f0cfe17bSDaniel Bristot de Oliveira return;
2968f0cfe17bSDaniel Bristot de Oliveira
2969bce29ac9SDaniel Bristot de Oliveira trace_osnoise_callback_enabled = false;
2970c3b6343cSDaniel Bristot de Oliveira /*
2971c3b6343cSDaniel Bristot de Oliveira * Make sure that ftrace_nmi_enter/exit() see
2972c3b6343cSDaniel Bristot de Oliveira * trace_osnoise_callback_enabled as false before continuing.
2973c3b6343cSDaniel Bristot de Oliveira */
2974bce29ac9SDaniel Bristot de Oliveira barrier();
2975bce29ac9SDaniel Bristot de Oliveira
2976bce29ac9SDaniel Bristot de Oliveira stop_per_cpu_kthreads();
2977bce29ac9SDaniel Bristot de Oliveira
29780878355bSNikita Yushchenko osnoise_unhook_events();
2979bce29ac9SDaniel Bristot de Oliveira }
2980bce29ac9SDaniel Bristot de Oliveira
osnoise_tracer_start(struct trace_array * tr)298115ca4bdbSDaniel Bristot de Oliveira static void osnoise_tracer_start(struct trace_array *tr)
298215ca4bdbSDaniel Bristot de Oliveira {
298315ca4bdbSDaniel Bristot de Oliveira int retval;
298415ca4bdbSDaniel Bristot de Oliveira
29852fac8d64SDaniel Bristot de Oliveira /*
29862fac8d64SDaniel Bristot de Oliveira * If the instance is already registered, there is no need to
29872fac8d64SDaniel Bristot de Oliveira * register it again.
29882fac8d64SDaniel Bristot de Oliveira */
29892fac8d64SDaniel Bristot de Oliveira if (osnoise_instance_registered(tr))
299015ca4bdbSDaniel Bristot de Oliveira return;
299115ca4bdbSDaniel Bristot de Oliveira
299215ca4bdbSDaniel Bristot de Oliveira retval = osnoise_workload_start();
299315ca4bdbSDaniel Bristot de Oliveira if (retval)
299415ca4bdbSDaniel Bristot de Oliveira pr_err(BANNER "Error starting osnoise tracer\n");
299515ca4bdbSDaniel Bristot de Oliveira
2996dae18134SDaniel Bristot de Oliveira osnoise_register_instance(tr);
299715ca4bdbSDaniel Bristot de Oliveira }
299815ca4bdbSDaniel Bristot de Oliveira
osnoise_tracer_stop(struct trace_array * tr)299915ca4bdbSDaniel Bristot de Oliveira static void osnoise_tracer_stop(struct trace_array *tr)
300015ca4bdbSDaniel Bristot de Oliveira {
3001dae18134SDaniel Bristot de Oliveira osnoise_unregister_instance(tr);
300215ca4bdbSDaniel Bristot de Oliveira osnoise_workload_stop();
300315ca4bdbSDaniel Bristot de Oliveira }
300415ca4bdbSDaniel Bristot de Oliveira
osnoise_tracer_init(struct trace_array * tr)3005bce29ac9SDaniel Bristot de Oliveira static int osnoise_tracer_init(struct trace_array *tr)
3006bce29ac9SDaniel Bristot de Oliveira {
30072fac8d64SDaniel Bristot de Oliveira /*
30082fac8d64SDaniel Bristot de Oliveira * Only allow osnoise tracer if timerlat tracer is not running
30092fac8d64SDaniel Bristot de Oliveira * already.
30102fac8d64SDaniel Bristot de Oliveira */
30112fac8d64SDaniel Bristot de Oliveira if (timerlat_enabled())
3012bce29ac9SDaniel Bristot de Oliveira return -EBUSY;
3013bce29ac9SDaniel Bristot de Oliveira
3014bce29ac9SDaniel Bristot de Oliveira tr->max_latency = 0;
3015bce29ac9SDaniel Bristot de Oliveira
3016bce29ac9SDaniel Bristot de Oliveira osnoise_tracer_start(tr);
3017bce29ac9SDaniel Bristot de Oliveira return 0;
3018bce29ac9SDaniel Bristot de Oliveira }
3019bce29ac9SDaniel Bristot de Oliveira
osnoise_tracer_reset(struct trace_array * tr)3020bce29ac9SDaniel Bristot de Oliveira static void osnoise_tracer_reset(struct trace_array *tr)
3021bce29ac9SDaniel Bristot de Oliveira {
3022bce29ac9SDaniel Bristot de Oliveira osnoise_tracer_stop(tr);
3023bce29ac9SDaniel Bristot de Oliveira }
3024bce29ac9SDaniel Bristot de Oliveira
3025bce29ac9SDaniel Bristot de Oliveira static struct tracer osnoise_tracer __read_mostly = {
3026bce29ac9SDaniel Bristot de Oliveira .name = "osnoise",
3027bce29ac9SDaniel Bristot de Oliveira .init = osnoise_tracer_init,
3028bce29ac9SDaniel Bristot de Oliveira .reset = osnoise_tracer_reset,
3029bce29ac9SDaniel Bristot de Oliveira .start = osnoise_tracer_start,
3030bce29ac9SDaniel Bristot de Oliveira .stop = osnoise_tracer_stop,
3031bce29ac9SDaniel Bristot de Oliveira .print_header = print_osnoise_headers,
3032bce29ac9SDaniel Bristot de Oliveira .allow_instances = true,
3033bce29ac9SDaniel Bristot de Oliveira };
3034bce29ac9SDaniel Bristot de Oliveira
3035a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER
timerlat_tracer_start(struct trace_array * tr)3036a955d7eaSDaniel Bristot de Oliveira static void timerlat_tracer_start(struct trace_array *tr)
3037a955d7eaSDaniel Bristot de Oliveira {
3038a955d7eaSDaniel Bristot de Oliveira int retval;
3039a955d7eaSDaniel Bristot de Oliveira
30402fac8d64SDaniel Bristot de Oliveira /*
30412fac8d64SDaniel Bristot de Oliveira * If the instance is already registered, there is no need to
30422fac8d64SDaniel Bristot de Oliveira * register it again.
30432fac8d64SDaniel Bristot de Oliveira */
30442fac8d64SDaniel Bristot de Oliveira if (osnoise_instance_registered(tr))
3045a955d7eaSDaniel Bristot de Oliveira return;
3046a955d7eaSDaniel Bristot de Oliveira
304715ca4bdbSDaniel Bristot de Oliveira retval = osnoise_workload_start();
3048a955d7eaSDaniel Bristot de Oliveira if (retval)
30492fac8d64SDaniel Bristot de Oliveira pr_err(BANNER "Error starting timerlat tracer\n");
3050a955d7eaSDaniel Bristot de Oliveira
3051dae18134SDaniel Bristot de Oliveira osnoise_register_instance(tr);
3052dae18134SDaniel Bristot de Oliveira
3053a955d7eaSDaniel Bristot de Oliveira return;
3054a955d7eaSDaniel Bristot de Oliveira }
3055a955d7eaSDaniel Bristot de Oliveira
timerlat_tracer_stop(struct trace_array * tr)3056a955d7eaSDaniel Bristot de Oliveira static void timerlat_tracer_stop(struct trace_array *tr)
3057a955d7eaSDaniel Bristot de Oliveira {
3058a955d7eaSDaniel Bristot de Oliveira int cpu;
3059a955d7eaSDaniel Bristot de Oliveira
30602fac8d64SDaniel Bristot de Oliveira osnoise_unregister_instance(tr);
3061a955d7eaSDaniel Bristot de Oliveira
30622fac8d64SDaniel Bristot de Oliveira /*
30632fac8d64SDaniel Bristot de Oliveira * Instruct the threads to stop only if this is the last instance.
30642fac8d64SDaniel Bristot de Oliveira */
30652fac8d64SDaniel Bristot de Oliveira if (!osnoise_has_registered_instances()) {
3066a955d7eaSDaniel Bristot de Oliveira for_each_online_cpu(cpu)
3067a955d7eaSDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).sampling = 0;
30682fac8d64SDaniel Bristot de Oliveira }
3069a955d7eaSDaniel Bristot de Oliveira
30702fac8d64SDaniel Bristot de Oliveira osnoise_workload_stop();
3071a955d7eaSDaniel Bristot de Oliveira }
3072a955d7eaSDaniel Bristot de Oliveira
timerlat_tracer_init(struct trace_array * tr)3073a955d7eaSDaniel Bristot de Oliveira static int timerlat_tracer_init(struct trace_array *tr)
3074a955d7eaSDaniel Bristot de Oliveira {
30752fac8d64SDaniel Bristot de Oliveira /*
30762fac8d64SDaniel Bristot de Oliveira * Only allow timerlat tracer if osnoise tracer is not running already.
30772fac8d64SDaniel Bristot de Oliveira */
30782fac8d64SDaniel Bristot de Oliveira if (osnoise_has_registered_instances() && !osnoise_data.timerlat_tracer)
3079a955d7eaSDaniel Bristot de Oliveira return -EBUSY;
3080a955d7eaSDaniel Bristot de Oliveira
30812fac8d64SDaniel Bristot de Oliveira /*
30822fac8d64SDaniel Bristot de Oliveira * If this is the first instance, set timerlat_tracer to block
30832fac8d64SDaniel Bristot de Oliveira * osnoise tracer start.
30842fac8d64SDaniel Bristot de Oliveira */
30852fac8d64SDaniel Bristot de Oliveira if (!osnoise_has_registered_instances())
30862fac8d64SDaniel Bristot de Oliveira osnoise_data.timerlat_tracer = 1;
3087a955d7eaSDaniel Bristot de Oliveira
30882fac8d64SDaniel Bristot de Oliveira tr->max_latency = 0;
3089a955d7eaSDaniel Bristot de Oliveira timerlat_tracer_start(tr);
3090a955d7eaSDaniel Bristot de Oliveira
3091a955d7eaSDaniel Bristot de Oliveira return 0;
3092a955d7eaSDaniel Bristot de Oliveira }
3093a955d7eaSDaniel Bristot de Oliveira
timerlat_tracer_reset(struct trace_array * tr)3094a955d7eaSDaniel Bristot de Oliveira static void timerlat_tracer_reset(struct trace_array *tr)
3095a955d7eaSDaniel Bristot de Oliveira {
3096a955d7eaSDaniel Bristot de Oliveira timerlat_tracer_stop(tr);
30972fac8d64SDaniel Bristot de Oliveira
30982fac8d64SDaniel Bristot de Oliveira /*
30992fac8d64SDaniel Bristot de Oliveira * If this is the last instance, reset timerlat_tracer allowing
31002fac8d64SDaniel Bristot de Oliveira * osnoise to be started.
31012fac8d64SDaniel Bristot de Oliveira */
31022fac8d64SDaniel Bristot de Oliveira if (!osnoise_has_registered_instances())
31032fac8d64SDaniel Bristot de Oliveira osnoise_data.timerlat_tracer = 0;
3104a955d7eaSDaniel Bristot de Oliveira }
3105a955d7eaSDaniel Bristot de Oliveira
3106a955d7eaSDaniel Bristot de Oliveira static struct tracer timerlat_tracer __read_mostly = {
3107a955d7eaSDaniel Bristot de Oliveira .name = "timerlat",
3108a955d7eaSDaniel Bristot de Oliveira .init = timerlat_tracer_init,
3109a955d7eaSDaniel Bristot de Oliveira .reset = timerlat_tracer_reset,
3110a955d7eaSDaniel Bristot de Oliveira .start = timerlat_tracer_start,
3111a955d7eaSDaniel Bristot de Oliveira .stop = timerlat_tracer_stop,
3112a955d7eaSDaniel Bristot de Oliveira .print_header = print_timerlat_headers,
3113a955d7eaSDaniel Bristot de Oliveira .allow_instances = true,
3114a955d7eaSDaniel Bristot de Oliveira };
3115ccb67544SDaniel Bristot de Oliveira
init_timerlat_tracer(void)3116ccb67544SDaniel Bristot de Oliveira __init static int init_timerlat_tracer(void)
3117ccb67544SDaniel Bristot de Oliveira {
3118ccb67544SDaniel Bristot de Oliveira return register_tracer(&timerlat_tracer);
3119ccb67544SDaniel Bristot de Oliveira }
3120ccb67544SDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */
init_timerlat_tracer(void)3121ccb67544SDaniel Bristot de Oliveira __init static int init_timerlat_tracer(void)
3122ccb67544SDaniel Bristot de Oliveira {
3123ccb67544SDaniel Bristot de Oliveira return 0;
3124ccb67544SDaniel Bristot de Oliveira }
3125a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */
3126a955d7eaSDaniel Bristot de Oliveira
init_osnoise_tracer(void)3127bce29ac9SDaniel Bristot de Oliveira __init static int init_osnoise_tracer(void)
3128bce29ac9SDaniel Bristot de Oliveira {
3129bce29ac9SDaniel Bristot de Oliveira int ret;
3130bce29ac9SDaniel Bristot de Oliveira
3131bce29ac9SDaniel Bristot de Oliveira mutex_init(&interface_lock);
3132bce29ac9SDaniel Bristot de Oliveira
3133bce29ac9SDaniel Bristot de Oliveira cpumask_copy(&osnoise_cpumask, cpu_all_mask);
3134bce29ac9SDaniel Bristot de Oliveira
3135bce29ac9SDaniel Bristot de Oliveira ret = register_tracer(&osnoise_tracer);
3136a955d7eaSDaniel Bristot de Oliveira if (ret) {
3137a955d7eaSDaniel Bristot de Oliveira pr_err(BANNER "Error registering osnoise!\n");
3138bce29ac9SDaniel Bristot de Oliveira return ret;
3139a955d7eaSDaniel Bristot de Oliveira }
3140a955d7eaSDaniel Bristot de Oliveira
3141ccb67544SDaniel Bristot de Oliveira ret = init_timerlat_tracer();
3142a955d7eaSDaniel Bristot de Oliveira if (ret) {
3143ccb67544SDaniel Bristot de Oliveira pr_err(BANNER "Error registering timerlat!\n");
3144a955d7eaSDaniel Bristot de Oliveira return ret;
3145a955d7eaSDaniel Bristot de Oliveira }
3146ccb67544SDaniel Bristot de Oliveira
3147c8895e27SDaniel Bristot de Oliveira osnoise_init_hotplug_support();
3148bce29ac9SDaniel Bristot de Oliveira
3149dae18134SDaniel Bristot de Oliveira INIT_LIST_HEAD_RCU(&osnoise_instances);
3150dae18134SDaniel Bristot de Oliveira
3151bce29ac9SDaniel Bristot de Oliveira init_tracefs();
3152bce29ac9SDaniel Bristot de Oliveira
3153bce29ac9SDaniel Bristot de Oliveira return 0;
3154bce29ac9SDaniel Bristot de Oliveira }
3155bce29ac9SDaniel Bristot de Oliveira late_initcall(init_osnoise_tracer);
3156