1 // SPDX-License-Identifier: GPL-2.0
3 * OS Noise Tracer: computes the OS Noise suffered by a running thread.
4 * Timerlat Tracer: measures the wakeup latency of a timer triggered IRQ and thread.
6 * Based on "hwlat_detector" tracer by:
7 * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
8 * Copyright (C) 2013-2016 Steven Rostedt, Red Hat, Inc. <srostedt@redhat.com>
9 * With feedback from Clark Williams <williams@redhat.com>
11 * And also based on the rtsl tracer presented on:
12 * DE OLIVEIRA, Daniel Bristot, et al. Demystifying the real-time linux
13 * scheduling latency. In: 32nd Euromicro Conference on Real-Time Systems
14 * (ECRTS 2020). Schloss Dagstuhl-Leibniz-Zentrum fur Informatik, 2020.
16 * Copyright (C) 2021 Daniel Bristot de Oliveira, Red Hat, Inc. <bristot@redhat.com>
19 #include <linux/kthread.h>
20 #include <linux/tracefs.h>
21 #include <linux/uaccess.h>
22 #include <linux/cpumask.h>
23 #include <linux/delay.h>
24 #include <linux/sched/clock.h>
25 #include <uapi/linux/sched/types.h>
26 #include <linux/sched.h>
29 #ifdef CONFIG_X86_LOCAL_APIC
30 #include <asm/trace/irq_vectors.h>
31 #undef TRACE_INCLUDE_PATH
32 #undef TRACE_INCLUDE_FILE
33 #endif /* CONFIG_X86_LOCAL_APIC */
35 #include <trace/events/irq.h>
36 #include <trace/events/sched.h>
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/osnoise.h>
44 #define BANNER "osnoise: "
45 #define DEFAULT_SAMPLE_PERIOD 1000000 /* 1s */
46 #define DEFAULT_SAMPLE_RUNTIME 1000000 /* 1s */
48 #define DEFAULT_TIMERLAT_PERIOD 1000 /* 1ms */
49 #define DEFAULT_TIMERLAT_PRIO 95 /* FIFO 95 */
52 * osnoise/options entries.
54 enum osnoise_options_index {
63 static const char * const osnoise_options_str[OSN_MAX] = {
67 "OSNOISE_PREEMPT_DISABLE",
68 "OSNOISE_IRQ_DISABLE" };
70 #define OSN_DEFAULT_OPTIONS 0x2
71 static unsigned long osnoise_options = OSN_DEFAULT_OPTIONS;
74 * trace_array of the enabled osnoise/timerlat instances.
76 struct osnoise_instance {
77 struct list_head list;
78 struct trace_array *tr;
81 static struct list_head osnoise_instances;
83 static bool osnoise_has_registered_instances(void)
85 return !!list_first_or_null_rcu(&osnoise_instances,
86 struct osnoise_instance,
91 * osnoise_instance_registered - check if a tr is already registered
93 static int osnoise_instance_registered(struct trace_array *tr)
95 struct osnoise_instance *inst;
99 list_for_each_entry_rcu(inst, &osnoise_instances, list) {
109 * osnoise_register_instance - register a new trace instance
111 * Register a trace_array *tr in the list of instances running
112 * osnoise/timerlat tracers.
114 static int osnoise_register_instance(struct trace_array *tr)
116 struct osnoise_instance *inst;
119 * register/unregister serialization is provided by trace's
122 lockdep_assert_held(&trace_types_lock);
124 inst = kmalloc(sizeof(*inst), GFP_KERNEL);
128 INIT_LIST_HEAD_RCU(&inst->list);
130 list_add_tail_rcu(&inst->list, &osnoise_instances);
136 * osnoise_unregister_instance - unregister a registered trace instance
138 * Remove the trace_array *tr from the list of instances running
139 * osnoise/timerlat tracers.
141 static void osnoise_unregister_instance(struct trace_array *tr)
143 struct osnoise_instance *inst;
147 * register/unregister serialization is provided by trace's
150 lockdep_assert_held(&trace_types_lock);
152 list_for_each_entry_rcu(inst, &osnoise_instances, list) {
153 if (inst->tr == tr) {
154 list_del_rcu(&inst->list);
183 #define IRQ_CONTEXT 0
184 #define THREAD_CONTEXT 1
186 * sofirq runtime info.
195 * thread runtime info.
204 * Runtime information: this structure saves the runtime information used by
205 * one sampling thread.
207 struct osnoise_variables {
208 struct task_struct *kthread;
213 struct osn_softirq softirq;
214 struct osn_thread thread;
219 * Per-cpu runtime information.
221 DEFINE_PER_CPU(struct osnoise_variables, per_cpu_osnoise_var);
224 * this_cpu_osn_var - Return the per-cpu osnoise_variables on its relative CPU
226 static inline struct osnoise_variables *this_cpu_osn_var(void)
228 return this_cpu_ptr(&per_cpu_osnoise_var);
231 #ifdef CONFIG_TIMERLAT_TRACER
233 * Runtime information for the timer mode.
235 struct timerlat_variables {
236 struct task_struct *kthread;
237 struct hrtimer timer;
244 DEFINE_PER_CPU(struct timerlat_variables, per_cpu_timerlat_var);
247 * this_cpu_tmr_var - Return the per-cpu timerlat_variables on its relative CPU
249 static inline struct timerlat_variables *this_cpu_tmr_var(void)
251 return this_cpu_ptr(&per_cpu_timerlat_var);
255 * tlat_var_reset - Reset the values of the given timerlat_variables
257 static inline void tlat_var_reset(void)
259 struct timerlat_variables *tlat_var;
262 * So far, all the values are initialized as 0, so
263 * zeroing the structure is perfect.
265 for_each_cpu(cpu, cpu_online_mask) {
266 tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu);
267 memset(tlat_var, 0, sizeof(*tlat_var));
270 #else /* CONFIG_TIMERLAT_TRACER */
271 #define tlat_var_reset() do {} while (0)
272 #endif /* CONFIG_TIMERLAT_TRACER */
275 * osn_var_reset - Reset the values of the given osnoise_variables
277 static inline void osn_var_reset(void)
279 struct osnoise_variables *osn_var;
283 * So far, all the values are initialized as 0, so
284 * zeroing the structure is perfect.
286 for_each_cpu(cpu, cpu_online_mask) {
287 osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
288 memset(osn_var, 0, sizeof(*osn_var));
293 * osn_var_reset_all - Reset the value of all per-cpu osnoise_variables
295 static inline void osn_var_reset_all(void)
302 * Tells NMIs to call back to the osnoise tracer to record timestamps.
304 bool trace_osnoise_callback_enabled;
307 * osnoise sample structure definition. Used to store the statistics of a
310 struct osnoise_sample {
311 u64 runtime; /* runtime */
312 u64 noise; /* noise */
313 u64 max_sample; /* max single noise sample */
314 int hw_count; /* # HW (incl. hypervisor) interference */
315 int nmi_count; /* # NMIs during this sample */
316 int irq_count; /* # IRQs during this sample */
317 int softirq_count; /* # softirqs during this sample */
318 int thread_count; /* # threads during this sample */
321 #ifdef CONFIG_TIMERLAT_TRACER
323 * timerlat sample structure definition. Used to store the statistics of
326 struct timerlat_sample {
327 u64 timer_latency; /* timer_latency */
328 unsigned int seqnum; /* unique sequence */
329 int context; /* timer context */
334 * Protect the interface.
336 struct mutex interface_lock;
341 static struct osnoise_data {
342 u64 sample_period; /* total sampling period */
343 u64 sample_runtime; /* active sampling portion of period */
344 u64 stop_tracing; /* stop trace in the internal operation (loop/irq) */
345 u64 stop_tracing_total; /* stop trace in the final operation (report/thread) */
346 #ifdef CONFIG_TIMERLAT_TRACER
347 u64 timerlat_period; /* timerlat period */
348 u64 print_stack; /* print IRQ stack if total > */
349 int timerlat_tracer; /* timerlat tracer */
351 bool tainted; /* infor users and developers about a problem */
353 .sample_period = DEFAULT_SAMPLE_PERIOD,
354 .sample_runtime = DEFAULT_SAMPLE_RUNTIME,
356 .stop_tracing_total = 0,
357 #ifdef CONFIG_TIMERLAT_TRACER
359 .timerlat_period = DEFAULT_TIMERLAT_PERIOD,
360 .timerlat_tracer = 0,
364 #ifdef CONFIG_TIMERLAT_TRACER
365 static inline bool timerlat_enabled(void)
367 return osnoise_data.timerlat_tracer;
370 static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var)
372 struct timerlat_variables *tlat_var = this_cpu_tmr_var();
374 * If the timerlat is enabled, but the irq handler did
375 * not run yet enabling timerlat_tracer, do not trace.
377 if (!tlat_var->tracing_thread) {
378 osn_var->softirq.arrival_time = 0;
379 osn_var->softirq.delta_start = 0;
385 static inline int timerlat_thread_exit(struct osnoise_variables *osn_var)
387 struct timerlat_variables *tlat_var = this_cpu_tmr_var();
389 * If the timerlat is enabled, but the irq handler did
390 * not run yet enabling timerlat_tracer, do not trace.
392 if (!tlat_var->tracing_thread) {
393 osn_var->thread.delta_start = 0;
394 osn_var->thread.arrival_time = 0;
399 #else /* CONFIG_TIMERLAT_TRACER */
400 static inline bool timerlat_enabled(void)
405 static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var)
409 static inline int timerlat_thread_exit(struct osnoise_variables *osn_var)
415 #ifdef CONFIG_PREEMPT_RT
417 * Print the osnoise header info.
419 static void print_osnoise_headers(struct seq_file *s)
421 if (osnoise_data.tainted)
422 seq_puts(s, "# osnoise is tainted!\n");
424 seq_puts(s, "# _-------=> irqs-off\n");
425 seq_puts(s, "# / _------=> need-resched\n");
426 seq_puts(s, "# | / _-----=> need-resched-lazy\n");
427 seq_puts(s, "# || / _----=> hardirq/softirq\n");
428 seq_puts(s, "# ||| / _---=> preempt-depth\n");
429 seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n");
430 seq_puts(s, "# ||||| / _-=> migrate-disable\n");
432 seq_puts(s, "# |||||| / ");
433 seq_puts(s, " MAX\n");
435 seq_puts(s, "# ||||| / ");
436 seq_puts(s, " SINGLE Interference counters:\n");
438 seq_puts(s, "# ||||||| RUNTIME ");
439 seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n");
441 seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP IN US ");
442 seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n");
444 seq_puts(s, "# | | | ||||||| | | ");
445 seq_puts(s, " | | | | | | | |\n");
447 #else /* CONFIG_PREEMPT_RT */
448 static void print_osnoise_headers(struct seq_file *s)
450 if (osnoise_data.tainted)
451 seq_puts(s, "# osnoise is tainted!\n");
453 seq_puts(s, "# _-----=> irqs-off\n");
454 seq_puts(s, "# / _----=> need-resched\n");
455 seq_puts(s, "# | / _---=> hardirq/softirq\n");
456 seq_puts(s, "# || / _--=> preempt-depth\n");
457 seq_puts(s, "# ||| / _-=> migrate-disable ");
458 seq_puts(s, " MAX\n");
459 seq_puts(s, "# |||| / delay ");
460 seq_puts(s, " SINGLE Interference counters:\n");
462 seq_puts(s, "# ||||| RUNTIME ");
463 seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n");
465 seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP IN US ");
466 seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n");
468 seq_puts(s, "# | | | ||||| | | ");
469 seq_puts(s, " | | | | | | | |\n");
471 #endif /* CONFIG_PREEMPT_RT */
474 * osnoise_taint - report an osnoise error.
476 #define osnoise_taint(msg) ({ \
477 struct osnoise_instance *inst; \
478 struct trace_buffer *buffer; \
481 list_for_each_entry_rcu(inst, &osnoise_instances, list) { \
482 buffer = inst->tr->array_buffer.buffer; \
483 trace_array_printk_buf(buffer, _THIS_IP_, msg); \
486 osnoise_data.tainted = true; \
490 * Record an osnoise_sample into the tracer buffer.
493 __trace_osnoise_sample(struct osnoise_sample *sample, struct trace_buffer *buffer)
495 struct trace_event_call *call = &event_osnoise;
496 struct ring_buffer_event *event;
497 struct osnoise_entry *entry;
499 event = trace_buffer_lock_reserve(buffer, TRACE_OSNOISE, sizeof(*entry),
503 entry = ring_buffer_event_data(event);
504 entry->runtime = sample->runtime;
505 entry->noise = sample->noise;
506 entry->max_sample = sample->max_sample;
507 entry->hw_count = sample->hw_count;
508 entry->nmi_count = sample->nmi_count;
509 entry->irq_count = sample->irq_count;
510 entry->softirq_count = sample->softirq_count;
511 entry->thread_count = sample->thread_count;
513 if (!call_filter_check_discard(call, entry, buffer, event))
514 trace_buffer_unlock_commit_nostack(buffer, event);
518 * Record an osnoise_sample on all osnoise instances.
520 static void trace_osnoise_sample(struct osnoise_sample *sample)
522 struct osnoise_instance *inst;
523 struct trace_buffer *buffer;
526 list_for_each_entry_rcu(inst, &osnoise_instances, list) {
527 buffer = inst->tr->array_buffer.buffer;
528 __trace_osnoise_sample(sample, buffer);
533 #ifdef CONFIG_TIMERLAT_TRACER
535 * Print the timerlat header info.
537 #ifdef CONFIG_PREEMPT_RT
538 static void print_timerlat_headers(struct seq_file *s)
540 seq_puts(s, "# _-------=> irqs-off\n");
541 seq_puts(s, "# / _------=> need-resched\n");
542 seq_puts(s, "# | / _-----=> need-resched-lazy\n");
543 seq_puts(s, "# || / _----=> hardirq/softirq\n");
544 seq_puts(s, "# ||| / _---=> preempt-depth\n");
545 seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n");
546 seq_puts(s, "# ||||| / _-=> migrate-disable\n");
547 seq_puts(s, "# |||||| /\n");
548 seq_puts(s, "# ||||||| ACTIVATION\n");
549 seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP ID ");
550 seq_puts(s, " CONTEXT LATENCY\n");
551 seq_puts(s, "# | | | ||||||| | | ");
552 seq_puts(s, " | |\n");
554 #else /* CONFIG_PREEMPT_RT */
555 static void print_timerlat_headers(struct seq_file *s)
557 seq_puts(s, "# _-----=> irqs-off\n");
558 seq_puts(s, "# / _----=> need-resched\n");
559 seq_puts(s, "# | / _---=> hardirq/softirq\n");
560 seq_puts(s, "# || / _--=> preempt-depth\n");
561 seq_puts(s, "# ||| / _-=> migrate-disable\n");
562 seq_puts(s, "# |||| / delay\n");
563 seq_puts(s, "# ||||| ACTIVATION\n");
564 seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP ID ");
565 seq_puts(s, " CONTEXT LATENCY\n");
566 seq_puts(s, "# | | | ||||| | | ");
567 seq_puts(s, " | |\n");
569 #endif /* CONFIG_PREEMPT_RT */
572 __trace_timerlat_sample(struct timerlat_sample *sample, struct trace_buffer *buffer)
574 struct trace_event_call *call = &event_osnoise;
575 struct ring_buffer_event *event;
576 struct timerlat_entry *entry;
578 event = trace_buffer_lock_reserve(buffer, TRACE_TIMERLAT, sizeof(*entry),
582 entry = ring_buffer_event_data(event);
583 entry->seqnum = sample->seqnum;
584 entry->context = sample->context;
585 entry->timer_latency = sample->timer_latency;
587 if (!call_filter_check_discard(call, entry, buffer, event))
588 trace_buffer_unlock_commit_nostack(buffer, event);
592 * Record an timerlat_sample into the tracer buffer.
594 static void trace_timerlat_sample(struct timerlat_sample *sample)
596 struct osnoise_instance *inst;
597 struct trace_buffer *buffer;
600 list_for_each_entry_rcu(inst, &osnoise_instances, list) {
601 buffer = inst->tr->array_buffer.buffer;
602 __trace_timerlat_sample(sample, buffer);
607 #ifdef CONFIG_STACKTRACE
609 #define MAX_CALLS 256
612 * Stack trace will take place only at IRQ level, so, no need
613 * to control nesting here.
618 unsigned long calls[MAX_CALLS];
621 static DEFINE_PER_CPU(struct trace_stack, trace_stack);
624 * timerlat_save_stack - save a stack trace without printing
626 * Save the current stack trace without printing. The
627 * stack will be printed later, after the end of the measurement.
629 static void timerlat_save_stack(int skip)
631 unsigned int size, nr_entries;
632 struct trace_stack *fstack;
634 fstack = this_cpu_ptr(&trace_stack);
636 size = ARRAY_SIZE(fstack->calls);
638 nr_entries = stack_trace_save(fstack->calls, size, skip);
640 fstack->stack_size = nr_entries * sizeof(unsigned long);
641 fstack->nr_entries = nr_entries;
648 __timerlat_dump_stack(struct trace_buffer *buffer, struct trace_stack *fstack, unsigned int size)
650 struct trace_event_call *call = &event_osnoise;
651 struct ring_buffer_event *event;
652 struct stack_entry *entry;
654 event = trace_buffer_lock_reserve(buffer, TRACE_STACK, sizeof(*entry) + size,
659 entry = ring_buffer_event_data(event);
661 memcpy(&entry->caller, fstack->calls, size);
662 entry->size = fstack->nr_entries;
664 if (!call_filter_check_discard(call, entry, buffer, event))
665 trace_buffer_unlock_commit_nostack(buffer, event);
669 * timerlat_dump_stack - dump a stack trace previously saved
671 static void timerlat_dump_stack(u64 latency)
673 struct osnoise_instance *inst;
674 struct trace_buffer *buffer;
675 struct trace_stack *fstack;
679 * trace only if latency > print_stack config, if enabled.
681 if (!osnoise_data.print_stack || osnoise_data.print_stack > latency)
684 preempt_disable_notrace();
685 fstack = this_cpu_ptr(&trace_stack);
686 size = fstack->stack_size;
689 list_for_each_entry_rcu(inst, &osnoise_instances, list) {
690 buffer = inst->tr->array_buffer.buffer;
691 __timerlat_dump_stack(buffer, fstack, size);
695 preempt_enable_notrace();
697 #else /* CONFIG_STACKTRACE */
698 #define timerlat_dump_stack(u64 latency) do {} while (0)
699 #define timerlat_save_stack(a) do {} while (0)
700 #endif /* CONFIG_STACKTRACE */
701 #endif /* CONFIG_TIMERLAT_TRACER */
704 * Macros to encapsulate the time capturing infrastructure.
706 #define time_get() trace_clock_local()
707 #define time_to_us(x) div_u64(x, 1000)
708 #define time_sub(a, b) ((a) - (b))
711 * cond_move_irq_delta_start - Forward the delta_start of a running IRQ
713 * If an IRQ is preempted by an NMI, its delta_start is pushed forward
714 * to discount the NMI interference.
716 * See get_int_safe_duration().
719 cond_move_irq_delta_start(struct osnoise_variables *osn_var, u64 duration)
721 if (osn_var->irq.delta_start)
722 osn_var->irq.delta_start += duration;
725 #ifndef CONFIG_PREEMPT_RT
727 * cond_move_softirq_delta_start - Forward the delta_start of a running softirq.
729 * If a softirq is preempted by an IRQ or NMI, its delta_start is pushed
730 * forward to discount the interference.
732 * See get_int_safe_duration().
735 cond_move_softirq_delta_start(struct osnoise_variables *osn_var, u64 duration)
737 if (osn_var->softirq.delta_start)
738 osn_var->softirq.delta_start += duration;
740 #else /* CONFIG_PREEMPT_RT */
741 #define cond_move_softirq_delta_start(osn_var, duration) do {} while (0)
745 * cond_move_thread_delta_start - Forward the delta_start of a running thread
747 * If a noisy thread is preempted by an softirq, IRQ or NMI, its delta_start
748 * is pushed forward to discount the interference.
750 * See get_int_safe_duration().
753 cond_move_thread_delta_start(struct osnoise_variables *osn_var, u64 duration)
755 if (osn_var->thread.delta_start)
756 osn_var->thread.delta_start += duration;
760 * get_int_safe_duration - Get the duration of a window
762 * The irq, softirq and thread varaibles need to have its duration without
763 * the interference from higher priority interrupts. Instead of keeping a
764 * variable to discount the interrupt interference from these variables, the
765 * starting time of these variables are pushed forward with the interrupt's
766 * duration. In this way, a single variable is used to:
768 * - Know if a given window is being measured.
769 * - Account its duration.
770 * - Discount the interference.
772 * To avoid getting inconsistent values, e.g.,:
776 * delta_start -= int duration;
778 * duration = now - delta_start;
780 * result: negative duration if the variable duration before the
781 * interrupt was smaller than the interrupt execution.
783 * A counter of interrupts is used. If the counter increased, try
784 * to capture an interference safe duration.
787 get_int_safe_duration(struct osnoise_variables *osn_var, u64 *delta_start)
789 u64 int_counter, now;
793 int_counter = local_read(&osn_var->int_counter);
794 /* synchronize with interrupts */
798 duration = (now - *delta_start);
800 /* synchronize with interrupts */
802 } while (int_counter != local_read(&osn_var->int_counter));
805 * This is an evidence of race conditions that cause
806 * a value to be "discounted" too much.
809 osnoise_taint("Negative duration!\n");
818 * set_int_safe_time - Save the current time on *time, aware of interference
820 * Get the time, taking into consideration a possible interference from
821 * higher priority interrupts.
823 * See get_int_safe_duration() for an explanation.
826 set_int_safe_time(struct osnoise_variables *osn_var, u64 *time)
831 int_counter = local_read(&osn_var->int_counter);
832 /* synchronize with interrupts */
837 /* synchronize with interrupts */
839 } while (int_counter != local_read(&osn_var->int_counter));
844 #ifdef CONFIG_TIMERLAT_TRACER
846 * copy_int_safe_time - Copy *src into *desc aware of interference
849 copy_int_safe_time(struct osnoise_variables *osn_var, u64 *dst, u64 *src)
854 int_counter = local_read(&osn_var->int_counter);
855 /* synchronize with interrupts */
860 /* synchronize with interrupts */
862 } while (int_counter != local_read(&osn_var->int_counter));
866 #endif /* CONFIG_TIMERLAT_TRACER */
869 * trace_osnoise_callback - NMI entry/exit callback
871 * This function is called at the entry and exit NMI code. The bool enter
872 * distinguishes between either case. This function is used to note a NMI
873 * occurrence, compute the noise caused by the NMI, and to remove the noise
874 * it is potentially causing on other interference variables.
876 void trace_osnoise_callback(bool enter)
878 struct osnoise_variables *osn_var = this_cpu_osn_var();
881 if (!osn_var->sampling)
885 * Currently trace_clock_local() calls sched_clock() and the
886 * generic version is not NMI safe.
888 if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) {
890 osn_var->nmi.delta_start = time_get();
891 local_inc(&osn_var->int_counter);
893 duration = time_get() - osn_var->nmi.delta_start;
895 trace_nmi_noise(osn_var->nmi.delta_start, duration);
897 cond_move_irq_delta_start(osn_var, duration);
898 cond_move_softirq_delta_start(osn_var, duration);
899 cond_move_thread_delta_start(osn_var, duration);
904 osn_var->nmi.count++;
908 * osnoise_trace_irq_entry - Note the starting of an IRQ
910 * Save the starting time of an IRQ. As IRQs are non-preemptive to other IRQs,
911 * it is safe to use a single variable (ons_var->irq) to save the statistics.
912 * The arrival_time is used to report... the arrival time. The delta_start
913 * is used to compute the duration at the IRQ exit handler. See
914 * cond_move_irq_delta_start().
916 void osnoise_trace_irq_entry(int id)
918 struct osnoise_variables *osn_var = this_cpu_osn_var();
920 if (!osn_var->sampling)
923 * This value will be used in the report, but not to compute
924 * the execution time, so it is safe to get it unsafe.
926 osn_var->irq.arrival_time = time_get();
927 set_int_safe_time(osn_var, &osn_var->irq.delta_start);
928 osn_var->irq.count++;
930 local_inc(&osn_var->int_counter);
934 * osnoise_irq_exit - Note the end of an IRQ, sava data and trace
936 * Computes the duration of the IRQ noise, and trace it. Also discounts the
937 * interference from other sources of noise could be currently being accounted.
939 void osnoise_trace_irq_exit(int id, const char *desc)
941 struct osnoise_variables *osn_var = this_cpu_osn_var();
944 if (!osn_var->sampling)
947 duration = get_int_safe_duration(osn_var, &osn_var->irq.delta_start);
948 trace_irq_noise(id, desc, osn_var->irq.arrival_time, duration);
949 osn_var->irq.arrival_time = 0;
950 cond_move_softirq_delta_start(osn_var, duration);
951 cond_move_thread_delta_start(osn_var, duration);
955 * trace_irqentry_callback - Callback to the irq:irq_entry traceevent
957 * Used to note the starting of an IRQ occurece.
959 static void trace_irqentry_callback(void *data, int irq,
960 struct irqaction *action)
962 osnoise_trace_irq_entry(irq);
966 * trace_irqexit_callback - Callback to the irq:irq_exit traceevent
968 * Used to note the end of an IRQ occurece.
970 static void trace_irqexit_callback(void *data, int irq,
971 struct irqaction *action, int ret)
973 osnoise_trace_irq_exit(irq, action->name);
977 * arch specific register function.
979 int __weak osnoise_arch_register(void)
985 * arch specific unregister function.
987 void __weak osnoise_arch_unregister(void)
993 * hook_irq_events - Hook IRQ handling events
995 * This function hooks the IRQ related callbacks to the respective trace
998 static int hook_irq_events(void)
1002 ret = register_trace_irq_handler_entry(trace_irqentry_callback, NULL);
1006 ret = register_trace_irq_handler_exit(trace_irqexit_callback, NULL);
1008 goto out_unregister_entry;
1010 ret = osnoise_arch_register();
1017 unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL);
1018 out_unregister_entry:
1019 unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL);
1025 * unhook_irq_events - Unhook IRQ handling events
1027 * This function unhooks the IRQ related callbacks to the respective trace
1030 static void unhook_irq_events(void)
1032 osnoise_arch_unregister();
1033 unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL);
1034 unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL);
1037 #ifndef CONFIG_PREEMPT_RT
1039 * trace_softirq_entry_callback - Note the starting of a softirq
1041 * Save the starting time of a softirq. As softirqs are non-preemptive to
1042 * other softirqs, it is safe to use a single variable (ons_var->softirq)
1043 * to save the statistics. The arrival_time is used to report... the
1044 * arrival time. The delta_start is used to compute the duration at the
1045 * softirq exit handler. See cond_move_softirq_delta_start().
1047 static void trace_softirq_entry_callback(void *data, unsigned int vec_nr)
1049 struct osnoise_variables *osn_var = this_cpu_osn_var();
1051 if (!osn_var->sampling)
1054 * This value will be used in the report, but not to compute
1055 * the execution time, so it is safe to get it unsafe.
1057 osn_var->softirq.arrival_time = time_get();
1058 set_int_safe_time(osn_var, &osn_var->softirq.delta_start);
1059 osn_var->softirq.count++;
1061 local_inc(&osn_var->int_counter);
1065 * trace_softirq_exit_callback - Note the end of an softirq
1067 * Computes the duration of the softirq noise, and trace it. Also discounts the
1068 * interference from other sources of noise could be currently being accounted.
1070 static void trace_softirq_exit_callback(void *data, unsigned int vec_nr)
1072 struct osnoise_variables *osn_var = this_cpu_osn_var();
1075 if (!osn_var->sampling)
1078 if (unlikely(timerlat_enabled()))
1079 if (!timerlat_softirq_exit(osn_var))
1082 duration = get_int_safe_duration(osn_var, &osn_var->softirq.delta_start);
1083 trace_softirq_noise(vec_nr, osn_var->softirq.arrival_time, duration);
1084 cond_move_thread_delta_start(osn_var, duration);
1085 osn_var->softirq.arrival_time = 0;
1089 * hook_softirq_events - Hook softirq handling events
1091 * This function hooks the softirq related callbacks to the respective trace
1094 static int hook_softirq_events(void)
1098 ret = register_trace_softirq_entry(trace_softirq_entry_callback, NULL);
1102 ret = register_trace_softirq_exit(trace_softirq_exit_callback, NULL);
1104 goto out_unreg_entry;
1109 unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL);
1115 * unhook_softirq_events - Unhook softirq handling events
1117 * This function hooks the softirq related callbacks to the respective trace
1120 static void unhook_softirq_events(void)
1122 unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL);
1123 unregister_trace_softirq_exit(trace_softirq_exit_callback, NULL);
1125 #else /* CONFIG_PREEMPT_RT */
1127 * softirq are threads on the PREEMPT_RT mode.
1129 static int hook_softirq_events(void)
1133 static void unhook_softirq_events(void)
1139 * thread_entry - Record the starting of a thread noise window
1141 * It saves the context switch time for a noisy thread, and increments
1142 * the interference counters.
1145 thread_entry(struct osnoise_variables *osn_var, struct task_struct *t)
1147 if (!osn_var->sampling)
1150 * The arrival time will be used in the report, but not to compute
1151 * the execution time, so it is safe to get it unsafe.
1153 osn_var->thread.arrival_time = time_get();
1155 set_int_safe_time(osn_var, &osn_var->thread.delta_start);
1157 osn_var->thread.count++;
1158 local_inc(&osn_var->int_counter);
1162 * thread_exit - Report the end of a thread noise window
1164 * It computes the total noise from a thread, tracing if needed.
1167 thread_exit(struct osnoise_variables *osn_var, struct task_struct *t)
1171 if (!osn_var->sampling)
1174 if (unlikely(timerlat_enabled()))
1175 if (!timerlat_thread_exit(osn_var))
1178 duration = get_int_safe_duration(osn_var, &osn_var->thread.delta_start);
1180 trace_thread_noise(t, osn_var->thread.arrival_time, duration);
1182 osn_var->thread.arrival_time = 0;
1186 * trace_sched_switch - sched:sched_switch trace event handler
1188 * This function is hooked to the sched:sched_switch trace event, and it is
1189 * used to record the beginning and to report the end of a thread noise window.
1192 trace_sched_switch_callback(void *data, bool preempt,
1193 struct task_struct *p,
1194 struct task_struct *n,
1195 unsigned int prev_state)
1197 struct osnoise_variables *osn_var = this_cpu_osn_var();
1198 int workload = test_bit(OSN_WORKLOAD, &osnoise_options);
1200 if ((p->pid != osn_var->pid) || !workload)
1201 thread_exit(osn_var, p);
1203 if ((n->pid != osn_var->pid) || !workload)
1204 thread_entry(osn_var, n);
1208 * hook_thread_events - Hook the insturmentation for thread noise
1210 * Hook the osnoise tracer callbacks to handle the noise from other
1211 * threads on the necessary kernel events.
1213 static int hook_thread_events(void)
1217 ret = register_trace_sched_switch(trace_sched_switch_callback, NULL);
1225 * unhook_thread_events - *nhook the insturmentation for thread noise
1227 * Unook the osnoise tracer callbacks to handle the noise from other
1228 * threads on the necessary kernel events.
1230 static void unhook_thread_events(void)
1232 unregister_trace_sched_switch(trace_sched_switch_callback, NULL);
1236 * save_osn_sample_stats - Save the osnoise_sample statistics
1238 * Save the osnoise_sample statistics before the sampling phase. These
1239 * values will be used later to compute the diff betwneen the statistics
1240 * before and after the osnoise sampling.
1243 save_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s)
1245 s->nmi_count = osn_var->nmi.count;
1246 s->irq_count = osn_var->irq.count;
1247 s->softirq_count = osn_var->softirq.count;
1248 s->thread_count = osn_var->thread.count;
1252 * diff_osn_sample_stats - Compute the osnoise_sample statistics
1254 * After a sample period, compute the difference on the osnoise_sample
1255 * statistics. The struct osnoise_sample *s contains the statistics saved via
1256 * save_osn_sample_stats() before the osnoise sampling.
1259 diff_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s)
1261 s->nmi_count = osn_var->nmi.count - s->nmi_count;
1262 s->irq_count = osn_var->irq.count - s->irq_count;
1263 s->softirq_count = osn_var->softirq.count - s->softirq_count;
1264 s->thread_count = osn_var->thread.count - s->thread_count;
1268 * osnoise_stop_tracing - Stop tracing and the tracer.
1270 static __always_inline void osnoise_stop_tracing(void)
1272 struct osnoise_instance *inst;
1273 struct trace_array *tr;
1276 list_for_each_entry_rcu(inst, &osnoise_instances, list) {
1278 trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_,
1279 "stop tracing hit on cpu %d\n", smp_processor_id());
1281 if (test_bit(OSN_PANIC_ON_STOP, &osnoise_options))
1282 panic("tracer hit stop condition on CPU %d\n", smp_processor_id());
1284 tracer_tracing_off(tr);
1290 * notify_new_max_latency - Notify a new max latency via fsnotify interface.
1292 static void notify_new_max_latency(u64 latency)
1294 struct osnoise_instance *inst;
1295 struct trace_array *tr;
1298 list_for_each_entry_rcu(inst, &osnoise_instances, list) {
1300 if (tr->max_latency < latency) {
1301 tr->max_latency = latency;
1302 latency_fsnotify(tr);
1309 * run_osnoise - Sample the time and look for osnoise
1311 * Used to capture the time, looking for potential osnoise latency repeatedly.
1312 * Different from hwlat_detector, it is called with preemption and interrupts
1313 * enabled. This allows irqs, softirqs and threads to run, interfering on the
1314 * osnoise sampling thread, as they would do with a regular thread.
1316 static int run_osnoise(void)
1318 bool disable_irq = test_bit(OSN_IRQ_DISABLE, &osnoise_options);
1319 struct osnoise_variables *osn_var = this_cpu_osn_var();
1320 u64 start, sample, last_sample;
1321 u64 last_int_count, int_count;
1322 s64 noise = 0, max_noise = 0;
1323 s64 total, last_total = 0;
1324 struct osnoise_sample s;
1325 bool disable_preemption;
1326 unsigned int threshold;
1327 u64 runtime, stop_in;
1333 * Disabling preemption is only required if IRQs are enabled,
1334 * and the options is set on.
1336 disable_preemption = !disable_irq && test_bit(OSN_PREEMPT_DISABLE, &osnoise_options);
1339 * Considers the current thread as the workload.
1341 osn_var->pid = current->pid;
1344 * Save the current stats for the diff
1346 save_osn_sample_stats(osn_var, &s);
1349 * if threshold is 0, use the default value of 5 us.
1351 threshold = tracing_thresh ? : 5000;
1354 * Apply PREEMPT and IRQ disabled options.
1357 local_irq_disable();
1359 if (disable_preemption)
1363 * Make sure NMIs see sampling first
1365 osn_var->sampling = true;
1369 * Transform the *_us config to nanoseconds to avoid the
1370 * division on the main loop.
1372 runtime = osnoise_data.sample_runtime * NSEC_PER_USEC;
1373 stop_in = osnoise_data.stop_tracing * NSEC_PER_USEC;
1383 last_int_count = set_int_safe_time(osn_var, &last_sample);
1389 int_count = set_int_safe_time(osn_var, &sample);
1391 noise = time_sub(sample, last_sample);
1394 * This shouldn't happen.
1397 osnoise_taint("negative noise!");
1404 total = time_sub(sample, start);
1407 * Check for possible overflows.
1409 if (total < last_total) {
1410 osnoise_taint("total overflow!");
1416 if (noise >= threshold) {
1417 int interference = int_count - last_int_count;
1419 if (noise > max_noise)
1427 trace_sample_threshold(last_sample, noise, interference);
1429 if (osnoise_data.stop_tracing)
1430 if (noise > stop_in)
1431 osnoise_stop_tracing();
1435 * In some cases, notably when running on a nohz_full CPU with
1436 * a stopped tick PREEMPT_RCU has no way to account for QSs.
1437 * This will eventually cause unwarranted noise as PREEMPT_RCU
1438 * will force preemption as the means of ending the current
1439 * grace period. We avoid this problem by calling
1440 * rcu_momentary_dyntick_idle(), which performs a zero duration
1441 * EQS allowing PREEMPT_RCU to end the current grace period.
1442 * This call shouldn't be wrapped inside an RCU critical
1445 * Note that in non PREEMPT_RCU kernels QSs are handled through
1448 if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
1450 local_irq_disable();
1452 rcu_momentary_dyntick_idle();
1459 * For the non-preemptive kernel config: let threads runs, if
1460 * they so wish, unless set not do to so.
1462 if (!disable_irq && !disable_preemption)
1465 last_sample = sample;
1466 last_int_count = int_count;
1468 } while (total < runtime && !kthread_should_stop());
1471 * Finish the above in the view for interrupts.
1475 osn_var->sampling = false;
1478 * Make sure sampling data is no longer updated.
1483 * Return to the preemptive state.
1485 if (disable_preemption)
1494 s.noise = time_to_us(sum_noise);
1495 s.runtime = time_to_us(total);
1496 s.max_sample = time_to_us(max_noise);
1497 s.hw_count = hw_count;
1499 /* Save interference stats info */
1500 diff_osn_sample_stats(osn_var, &s);
1502 trace_osnoise_sample(&s);
1504 notify_new_max_latency(max_noise);
1506 if (osnoise_data.stop_tracing_total)
1507 if (s.noise > osnoise_data.stop_tracing_total)
1508 osnoise_stop_tracing();
1515 static struct cpumask osnoise_cpumask;
1516 static struct cpumask save_cpumask;
1519 * osnoise_sleep - sleep until the next period
1521 static void osnoise_sleep(void)
1526 mutex_lock(&interface_lock);
1527 interval = osnoise_data.sample_period - osnoise_data.sample_runtime;
1528 mutex_unlock(&interface_lock);
1531 * differently from hwlat_detector, the osnoise tracer can run
1532 * without a pause because preemption is on.
1535 /* Let synchronize_rcu_tasks() make progress */
1536 cond_resched_tasks_rcu_qs();
1540 wake_time = ktime_add_us(ktime_get(), interval);
1541 __set_current_state(TASK_INTERRUPTIBLE);
1543 while (schedule_hrtimeout_range(&wake_time, 0, HRTIMER_MODE_ABS)) {
1544 if (kthread_should_stop())
1550 * osnoise_main - The osnoise detection kernel thread
1552 * Calls run_osnoise() function to measure the osnoise for the configured runtime,
1555 static int osnoise_main(void *data)
1558 while (!kthread_should_stop()) {
1566 #ifdef CONFIG_TIMERLAT_TRACER
1568 * timerlat_irq - hrtimer handler for timerlat.
1570 static enum hrtimer_restart timerlat_irq(struct hrtimer *timer)
1572 struct osnoise_variables *osn_var = this_cpu_osn_var();
1573 struct timerlat_variables *tlat;
1574 struct timerlat_sample s;
1579 * I am not sure if the timer was armed for this CPU. So, get
1580 * the timerlat struct from the timer itself, not from this
1583 tlat = container_of(timer, struct timerlat_variables, timer);
1585 now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
1588 * Enable the osnoise: events for thread an softirq.
1590 tlat->tracing_thread = true;
1592 osn_var->thread.arrival_time = time_get();
1595 * A hardirq is running: the timer IRQ. It is for sure preempting
1596 * a thread, and potentially preempting a softirq.
1598 * At this point, it is not interesting to know the duration of the
1599 * preempted thread (and maybe softirq), but how much time they will
1600 * delay the beginning of the execution of the timer thread.
1602 * To get the correct (net) delay added by the softirq, its delta_start
1603 * is set as the IRQ one. In this way, at the return of the IRQ, the delta
1604 * start of the sofitrq will be zeroed, accounting then only the time
1607 * The thread follows the same principle. However, if a softirq is
1608 * running, the thread needs to receive the softirq delta_start. The
1609 * reason being is that the softirq will be the last to be unfolded,
1610 * resseting the thread delay to zero.
1612 * The PREEMPT_RT is a special case, though. As softirqs run as threads
1613 * on RT, moving the thread is enough.
1615 if (!IS_ENABLED(CONFIG_PREEMPT_RT) && osn_var->softirq.delta_start) {
1616 copy_int_safe_time(osn_var, &osn_var->thread.delta_start,
1617 &osn_var->softirq.delta_start);
1619 copy_int_safe_time(osn_var, &osn_var->softirq.delta_start,
1620 &osn_var->irq.delta_start);
1622 copy_int_safe_time(osn_var, &osn_var->thread.delta_start,
1623 &osn_var->irq.delta_start);
1627 * Compute the current time with the expected time.
1629 diff = now - tlat->abs_period;
1632 s.seqnum = tlat->count;
1633 s.timer_latency = diff;
1634 s.context = IRQ_CONTEXT;
1636 trace_timerlat_sample(&s);
1638 if (osnoise_data.stop_tracing) {
1639 if (time_to_us(diff) >= osnoise_data.stop_tracing) {
1642 * At this point, if stop_tracing is set and <= print_stack,
1643 * print_stack is set and would be printed in the thread handler.
1645 * Thus, print the stack trace as it is helpful to define the
1646 * root cause of an IRQ latency.
1648 if (osnoise_data.stop_tracing <= osnoise_data.print_stack) {
1649 timerlat_save_stack(0);
1650 timerlat_dump_stack(time_to_us(diff));
1653 osnoise_stop_tracing();
1654 notify_new_max_latency(diff);
1656 return HRTIMER_NORESTART;
1660 wake_up_process(tlat->kthread);
1662 if (osnoise_data.print_stack)
1663 timerlat_save_stack(0);
1665 return HRTIMER_NORESTART;
1669 * wait_next_period - Wait for the next period for timerlat
1671 static int wait_next_period(struct timerlat_variables *tlat)
1673 ktime_t next_abs_period, now;
1674 u64 rel_period = osnoise_data.timerlat_period * 1000;
1676 now = hrtimer_cb_get_time(&tlat->timer);
1677 next_abs_period = ns_to_ktime(tlat->abs_period + rel_period);
1680 * Save the next abs_period.
1682 tlat->abs_period = (u64) ktime_to_ns(next_abs_period);
1685 * If the new abs_period is in the past, skip the activation.
1687 while (ktime_compare(now, next_abs_period) > 0) {
1688 next_abs_period = ns_to_ktime(tlat->abs_period + rel_period);
1689 tlat->abs_period = (u64) ktime_to_ns(next_abs_period);
1692 set_current_state(TASK_INTERRUPTIBLE);
1694 hrtimer_start(&tlat->timer, next_abs_period, HRTIMER_MODE_ABS_PINNED_HARD);
1700 * timerlat_main- Timerlat main
1702 static int timerlat_main(void *data)
1704 struct osnoise_variables *osn_var = this_cpu_osn_var();
1705 struct timerlat_variables *tlat = this_cpu_tmr_var();
1706 struct timerlat_sample s;
1707 struct sched_param sp;
1711 * Make the thread RT, that is how cyclictest is usually used.
1713 sp.sched_priority = DEFAULT_TIMERLAT_PRIO;
1714 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
1717 tlat->tracing_thread = false;
1719 hrtimer_init(&tlat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
1720 tlat->timer.function = timerlat_irq;
1721 tlat->kthread = current;
1722 osn_var->pid = current->pid;
1724 * Anotate the arrival time.
1726 tlat->abs_period = hrtimer_cb_get_time(&tlat->timer);
1728 wait_next_period(tlat);
1730 osn_var->sampling = 1;
1732 while (!kthread_should_stop()) {
1733 now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
1734 diff = now - tlat->abs_period;
1736 s.seqnum = tlat->count;
1737 s.timer_latency = diff;
1738 s.context = THREAD_CONTEXT;
1740 trace_timerlat_sample(&s);
1742 timerlat_dump_stack(time_to_us(diff));
1744 tlat->tracing_thread = false;
1745 if (osnoise_data.stop_tracing_total)
1746 if (time_to_us(diff) >= osnoise_data.stop_tracing_total)
1747 osnoise_stop_tracing();
1749 wait_next_period(tlat);
1752 hrtimer_cancel(&tlat->timer);
1755 #else /* CONFIG_TIMERLAT_TRACER */
1756 static int timerlat_main(void *data)
1760 #endif /* CONFIG_TIMERLAT_TRACER */
1763 * stop_kthread - stop a workload thread
1765 static void stop_kthread(unsigned int cpu)
1767 struct task_struct *kthread;
1769 kthread = per_cpu(per_cpu_osnoise_var, cpu).kthread;
1771 kthread_stop(kthread);
1772 per_cpu(per_cpu_osnoise_var, cpu).kthread = NULL;
1774 if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
1775 per_cpu(per_cpu_osnoise_var, cpu).sampling = false;
1783 * stop_per_cpu_kthread - Stop per-cpu threads
1785 * Stop the osnoise sampling htread. Use this on unload and at system
1788 static void stop_per_cpu_kthreads(void)
1794 for_each_online_cpu(cpu)
1801 * start_kthread - Start a workload tread
1803 static int start_kthread(unsigned int cpu)
1805 struct task_struct *kthread;
1806 void *main = osnoise_main;
1809 if (timerlat_enabled()) {
1810 snprintf(comm, 24, "timerlat/%d", cpu);
1811 main = timerlat_main;
1813 /* if no workload, just return */
1814 if (!test_bit(OSN_WORKLOAD, &osnoise_options)) {
1815 per_cpu(per_cpu_osnoise_var, cpu).sampling = true;
1820 snprintf(comm, 24, "osnoise/%d", cpu);
1823 kthread = kthread_run_on_cpu(main, NULL, cpu, comm);
1825 if (IS_ERR(kthread)) {
1826 pr_err(BANNER "could not start sampling thread\n");
1827 stop_per_cpu_kthreads();
1831 per_cpu(per_cpu_osnoise_var, cpu).kthread = kthread;
1837 * start_per_cpu_kthread - Kick off per-cpu osnoise sampling kthreads
1839 * This starts the kernel thread that will look for osnoise on many
1842 static int start_per_cpu_kthreads(void)
1844 struct cpumask *current_mask = &save_cpumask;
1850 * Run only on online CPUs in which osnoise is allowed to run.
1852 cpumask_and(current_mask, cpu_online_mask, &osnoise_cpumask);
1854 for_each_possible_cpu(cpu)
1855 per_cpu(per_cpu_osnoise_var, cpu).kthread = NULL;
1857 for_each_cpu(cpu, current_mask) {
1858 retval = start_kthread(cpu);
1861 stop_per_cpu_kthreads();
1871 #ifdef CONFIG_HOTPLUG_CPU
1872 static void osnoise_hotplug_workfn(struct work_struct *dummy)
1874 unsigned int cpu = smp_processor_id();
1876 mutex_lock(&trace_types_lock);
1878 if (!osnoise_has_registered_instances())
1879 goto out_unlock_trace;
1881 mutex_lock(&interface_lock);
1884 if (!cpumask_test_cpu(cpu, &osnoise_cpumask))
1891 mutex_unlock(&interface_lock);
1893 mutex_unlock(&trace_types_lock);
1896 static DECLARE_WORK(osnoise_hotplug_work, osnoise_hotplug_workfn);
1899 * osnoise_cpu_init - CPU hotplug online callback function
1901 static int osnoise_cpu_init(unsigned int cpu)
1903 schedule_work_on(cpu, &osnoise_hotplug_work);
1908 * osnoise_cpu_die - CPU hotplug offline callback function
1910 static int osnoise_cpu_die(unsigned int cpu)
1916 static void osnoise_init_hotplug_support(void)
1920 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "trace/osnoise:online",
1921 osnoise_cpu_init, osnoise_cpu_die);
1923 pr_warn(BANNER "Error to init cpu hotplug support\n");
1927 #else /* CONFIG_HOTPLUG_CPU */
1928 static void osnoise_init_hotplug_support(void)
1932 #endif /* CONFIG_HOTPLUG_CPU */
1935 * seq file functions for the osnoise/options file.
1937 static void *s_options_start(struct seq_file *s, loff_t *pos)
1941 mutex_lock(&interface_lock);
1943 if (option >= OSN_MAX)
1949 static void *s_options_next(struct seq_file *s, void *v, loff_t *pos)
1951 int option = ++(*pos);
1953 if (option >= OSN_MAX)
1959 static int s_options_show(struct seq_file *s, void *v)
1964 if (option == OSN_DEFAULTS) {
1965 if (osnoise_options == OSN_DEFAULT_OPTIONS)
1966 seq_printf(s, "%s", osnoise_options_str[option]);
1968 seq_printf(s, "NO_%s", osnoise_options_str[option]);
1972 if (test_bit(option, &osnoise_options))
1973 seq_printf(s, "%s", osnoise_options_str[option]);
1975 seq_printf(s, "NO_%s", osnoise_options_str[option]);
1978 if (option != OSN_MAX)
1984 static void s_options_stop(struct seq_file *s, void *v)
1987 mutex_unlock(&interface_lock);
1990 static const struct seq_operations osnoise_options_seq_ops = {
1991 .start = s_options_start,
1992 .next = s_options_next,
1993 .show = s_options_show,
1994 .stop = s_options_stop
1997 static int osnoise_options_open(struct inode *inode, struct file *file)
1999 return seq_open(file, &osnoise_options_seq_ops);
2003 * osnoise_options_write - Write function for "options" entry
2004 * @filp: The active open file structure
2005 * @ubuf: The user buffer that contains the value to write
2006 * @cnt: The maximum number of bytes to write to "file"
2007 * @ppos: The current position in @file
2009 * Writing the option name sets the option, writing the "NO_"
2010 * prefix in front of the option name disables it.
2012 * Writing "DEFAULTS" resets the option values to the default ones.
2014 static ssize_t osnoise_options_write(struct file *filp, const char __user *ubuf,
2015 size_t cnt, loff_t *ppos)
2017 int running, option, enable, retval;
2018 char buf[256], *option_str;
2023 if (copy_from_user(buf, ubuf, cnt))
2028 if (strncmp(buf, "NO_", 3)) {
2029 option_str = strstrip(buf);
2032 option_str = strstrip(&buf[3]);
2036 option = match_string(osnoise_options_str, OSN_MAX, option_str);
2041 * trace_types_lock is taken to avoid concurrency on start/stop.
2043 mutex_lock(&trace_types_lock);
2044 running = osnoise_has_registered_instances();
2046 stop_per_cpu_kthreads();
2048 mutex_lock(&interface_lock);
2050 * avoid CPU hotplug operations that might read options.
2057 if (option == OSN_DEFAULTS)
2058 osnoise_options = OSN_DEFAULT_OPTIONS;
2060 set_bit(option, &osnoise_options);
2062 if (option == OSN_DEFAULTS)
2065 clear_bit(option, &osnoise_options);
2069 mutex_unlock(&interface_lock);
2072 start_per_cpu_kthreads();
2073 mutex_unlock(&trace_types_lock);
2079 * osnoise_cpus_read - Read function for reading the "cpus" file
2080 * @filp: The active open file structure
2081 * @ubuf: The userspace provided buffer to read value into
2082 * @cnt: The maximum number of bytes to read
2083 * @ppos: The current "file" position
2085 * Prints the "cpus" output into the user-provided buffer.
2088 osnoise_cpus_read(struct file *filp, char __user *ubuf, size_t count,
2094 mutex_lock(&interface_lock);
2096 len = snprintf(NULL, 0, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)) + 1;
2097 mask_str = kmalloc(len, GFP_KERNEL);
2103 len = snprintf(mask_str, len, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask));
2109 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
2114 mutex_unlock(&interface_lock);
2120 * osnoise_cpus_write - Write function for "cpus" entry
2121 * @filp: The active open file structure
2122 * @ubuf: The user buffer that contains the value to write
2123 * @cnt: The maximum number of bytes to write to "file"
2124 * @ppos: The current position in @file
2126 * This function provides a write implementation for the "cpus"
2127 * interface to the osnoise trace. By default, it lists all CPUs,
2128 * in this way, allowing osnoise threads to run on any online CPU
2129 * of the system. It serves to restrict the execution of osnoise to the
2130 * set of CPUs writing via this interface. Why not use "tracing_cpumask"?
2131 * Because the user might be interested in tracing what is running on
2132 * other CPUs. For instance, one might run osnoise in one HT CPU
2133 * while observing what is running on the sibling HT CPU.
2136 osnoise_cpus_write(struct file *filp, const char __user *ubuf, size_t count,
2139 cpumask_var_t osnoise_cpumask_new;
2146 if (copy_from_user(buf, ubuf, count))
2149 if (!zalloc_cpumask_var(&osnoise_cpumask_new, GFP_KERNEL))
2152 err = cpulist_parse(buf, osnoise_cpumask_new);
2157 * trace_types_lock is taken to avoid concurrency on start/stop.
2159 mutex_lock(&trace_types_lock);
2160 running = osnoise_has_registered_instances();
2162 stop_per_cpu_kthreads();
2164 mutex_lock(&interface_lock);
2166 * osnoise_cpumask is read by CPU hotplug operations.
2170 cpumask_copy(&osnoise_cpumask, osnoise_cpumask_new);
2173 mutex_unlock(&interface_lock);
2176 start_per_cpu_kthreads();
2177 mutex_unlock(&trace_types_lock);
2179 free_cpumask_var(osnoise_cpumask_new);
2183 free_cpumask_var(osnoise_cpumask_new);
2189 * osnoise/runtime_us: cannot be greater than the period.
2191 static struct trace_min_max_param osnoise_runtime = {
2192 .lock = &interface_lock,
2193 .val = &osnoise_data.sample_runtime,
2194 .max = &osnoise_data.sample_period,
2199 * osnoise/period_us: cannot be smaller than the runtime.
2201 static struct trace_min_max_param osnoise_period = {
2202 .lock = &interface_lock,
2203 .val = &osnoise_data.sample_period,
2205 .min = &osnoise_data.sample_runtime,
2209 * osnoise/stop_tracing_us: no limit.
2211 static struct trace_min_max_param osnoise_stop_tracing_in = {
2212 .lock = &interface_lock,
2213 .val = &osnoise_data.stop_tracing,
2219 * osnoise/stop_tracing_total_us: no limit.
2221 static struct trace_min_max_param osnoise_stop_tracing_total = {
2222 .lock = &interface_lock,
2223 .val = &osnoise_data.stop_tracing_total,
2228 #ifdef CONFIG_TIMERLAT_TRACER
2230 * osnoise/print_stack: print the stacktrace of the IRQ handler if the total
2231 * latency is higher than val.
2233 static struct trace_min_max_param osnoise_print_stack = {
2234 .lock = &interface_lock,
2235 .val = &osnoise_data.print_stack,
2241 * osnoise/timerlat_period: min 100 us, max 1 s
2243 u64 timerlat_min_period = 100;
2244 u64 timerlat_max_period = 1000000;
2245 static struct trace_min_max_param timerlat_period = {
2246 .lock = &interface_lock,
2247 .val = &osnoise_data.timerlat_period,
2248 .max = &timerlat_max_period,
2249 .min = &timerlat_min_period,
2253 static const struct file_operations cpus_fops = {
2254 .open = tracing_open_generic,
2255 .read = osnoise_cpus_read,
2256 .write = osnoise_cpus_write,
2257 .llseek = generic_file_llseek,
2260 static const struct file_operations osnoise_options_fops = {
2261 .open = osnoise_options_open,
2263 .llseek = seq_lseek,
2264 .release = seq_release,
2265 .write = osnoise_options_write
2268 #ifdef CONFIG_TIMERLAT_TRACER
2269 #ifdef CONFIG_STACKTRACE
2270 static int init_timerlat_stack_tracefs(struct dentry *top_dir)
2274 tmp = tracefs_create_file("print_stack", TRACE_MODE_WRITE, top_dir,
2275 &osnoise_print_stack, &trace_min_max_fops);
2281 #else /* CONFIG_STACKTRACE */
2282 static int init_timerlat_stack_tracefs(struct dentry *top_dir)
2286 #endif /* CONFIG_STACKTRACE */
2289 * init_timerlat_tracefs - A function to initialize the timerlat interface files
2291 static int init_timerlat_tracefs(struct dentry *top_dir)
2295 tmp = tracefs_create_file("timerlat_period_us", TRACE_MODE_WRITE, top_dir,
2296 &timerlat_period, &trace_min_max_fops);
2300 return init_timerlat_stack_tracefs(top_dir);
2302 #else /* CONFIG_TIMERLAT_TRACER */
2303 static int init_timerlat_tracefs(struct dentry *top_dir)
2307 #endif /* CONFIG_TIMERLAT_TRACER */
2310 * init_tracefs - A function to initialize the tracefs interface files
2312 * This function creates entries in tracefs for "osnoise" and "timerlat".
2313 * It creates these directories in the tracing directory, and within that
2314 * directory the use can change and view the configs.
2316 static int init_tracefs(void)
2318 struct dentry *top_dir;
2322 ret = tracing_init_dentry();
2326 top_dir = tracefs_create_dir("osnoise", NULL);
2330 tmp = tracefs_create_file("period_us", TRACE_MODE_WRITE, top_dir,
2331 &osnoise_period, &trace_min_max_fops);
2335 tmp = tracefs_create_file("runtime_us", TRACE_MODE_WRITE, top_dir,
2336 &osnoise_runtime, &trace_min_max_fops);
2340 tmp = tracefs_create_file("stop_tracing_us", TRACE_MODE_WRITE, top_dir,
2341 &osnoise_stop_tracing_in, &trace_min_max_fops);
2345 tmp = tracefs_create_file("stop_tracing_total_us", TRACE_MODE_WRITE, top_dir,
2346 &osnoise_stop_tracing_total, &trace_min_max_fops);
2350 tmp = trace_create_file("cpus", TRACE_MODE_WRITE, top_dir, NULL, &cpus_fops);
2354 tmp = trace_create_file("options", TRACE_MODE_WRITE, top_dir, NULL,
2355 &osnoise_options_fops);
2359 ret = init_timerlat_tracefs(top_dir);
2366 tracefs_remove(top_dir);
2370 static int osnoise_hook_events(void)
2375 * Trace is already hooked, we are re-enabling from
2378 if (trace_osnoise_callback_enabled)
2381 retval = hook_irq_events();
2385 retval = hook_softirq_events();
2387 goto out_unhook_irq;
2389 retval = hook_thread_events();
2396 unhook_softirq_events();
2398 unhook_irq_events();
2402 static void osnoise_unhook_events(void)
2404 unhook_thread_events();
2405 unhook_softirq_events();
2406 unhook_irq_events();
2410 * osnoise_workload_start - start the workload and hook to events
2412 static int osnoise_workload_start(void)
2417 * Instances need to be registered after calling workload
2418 * start. Hence, if there is already an instance, the
2419 * workload was already registered. Otherwise, this
2420 * code is on the way to register the first instance,
2421 * and the workload will start.
2423 if (osnoise_has_registered_instances())
2426 osn_var_reset_all();
2428 retval = osnoise_hook_events();
2433 * Make sure that ftrace_nmi_enter/exit() see reset values
2434 * before enabling trace_osnoise_callback_enabled.
2437 trace_osnoise_callback_enabled = true;
2439 retval = start_per_cpu_kthreads();
2441 trace_osnoise_callback_enabled = false;
2443 * Make sure that ftrace_nmi_enter/exit() see
2444 * trace_osnoise_callback_enabled as false before continuing.
2448 osnoise_unhook_events();
2456 * osnoise_workload_stop - stop the workload and unhook the events
2458 static void osnoise_workload_stop(void)
2461 * Instances need to be unregistered before calling
2462 * stop. Hence, if there is a registered instance, more
2463 * than one instance is running, and the workload will not
2464 * yet stop. Otherwise, this code is on the way to disable
2465 * the last instance, and the workload can stop.
2467 if (osnoise_has_registered_instances())
2471 * If callbacks were already disabled in a previous stop
2472 * call, there is no need to disable then again.
2474 * For instance, this happens when tracing is stopped via:
2475 * echo 0 > tracing_on
2476 * echo nop > current_tracer.
2478 if (!trace_osnoise_callback_enabled)
2481 trace_osnoise_callback_enabled = false;
2483 * Make sure that ftrace_nmi_enter/exit() see
2484 * trace_osnoise_callback_enabled as false before continuing.
2488 stop_per_cpu_kthreads();
2490 osnoise_unhook_events();
2493 static void osnoise_tracer_start(struct trace_array *tr)
2498 * If the instance is already registered, there is no need to
2499 * register it again.
2501 if (osnoise_instance_registered(tr))
2504 retval = osnoise_workload_start();
2506 pr_err(BANNER "Error starting osnoise tracer\n");
2508 osnoise_register_instance(tr);
2511 static void osnoise_tracer_stop(struct trace_array *tr)
2513 osnoise_unregister_instance(tr);
2514 osnoise_workload_stop();
2517 static int osnoise_tracer_init(struct trace_array *tr)
2520 * Only allow osnoise tracer if timerlat tracer is not running
2523 if (timerlat_enabled())
2526 tr->max_latency = 0;
2528 osnoise_tracer_start(tr);
2532 static void osnoise_tracer_reset(struct trace_array *tr)
2534 osnoise_tracer_stop(tr);
2537 static struct tracer osnoise_tracer __read_mostly = {
2539 .init = osnoise_tracer_init,
2540 .reset = osnoise_tracer_reset,
2541 .start = osnoise_tracer_start,
2542 .stop = osnoise_tracer_stop,
2543 .print_header = print_osnoise_headers,
2544 .allow_instances = true,
2547 #ifdef CONFIG_TIMERLAT_TRACER
2548 static void timerlat_tracer_start(struct trace_array *tr)
2553 * If the instance is already registered, there is no need to
2554 * register it again.
2556 if (osnoise_instance_registered(tr))
2559 retval = osnoise_workload_start();
2561 pr_err(BANNER "Error starting timerlat tracer\n");
2563 osnoise_register_instance(tr);
2568 static void timerlat_tracer_stop(struct trace_array *tr)
2572 osnoise_unregister_instance(tr);
2575 * Instruct the threads to stop only if this is the last instance.
2577 if (!osnoise_has_registered_instances()) {
2578 for_each_online_cpu(cpu)
2579 per_cpu(per_cpu_osnoise_var, cpu).sampling = 0;
2582 osnoise_workload_stop();
2585 static int timerlat_tracer_init(struct trace_array *tr)
2588 * Only allow timerlat tracer if osnoise tracer is not running already.
2590 if (osnoise_has_registered_instances() && !osnoise_data.timerlat_tracer)
2594 * If this is the first instance, set timerlat_tracer to block
2595 * osnoise tracer start.
2597 if (!osnoise_has_registered_instances())
2598 osnoise_data.timerlat_tracer = 1;
2600 tr->max_latency = 0;
2601 timerlat_tracer_start(tr);
2606 static void timerlat_tracer_reset(struct trace_array *tr)
2608 timerlat_tracer_stop(tr);
2611 * If this is the last instance, reset timerlat_tracer allowing
2612 * osnoise to be started.
2614 if (!osnoise_has_registered_instances())
2615 osnoise_data.timerlat_tracer = 0;
2618 static struct tracer timerlat_tracer __read_mostly = {
2620 .init = timerlat_tracer_init,
2621 .reset = timerlat_tracer_reset,
2622 .start = timerlat_tracer_start,
2623 .stop = timerlat_tracer_stop,
2624 .print_header = print_timerlat_headers,
2625 .allow_instances = true,
2628 __init static int init_timerlat_tracer(void)
2630 return register_tracer(&timerlat_tracer);
2632 #else /* CONFIG_TIMERLAT_TRACER */
2633 __init static int init_timerlat_tracer(void)
2637 #endif /* CONFIG_TIMERLAT_TRACER */
2639 __init static int init_osnoise_tracer(void)
2643 mutex_init(&interface_lock);
2645 cpumask_copy(&osnoise_cpumask, cpu_all_mask);
2647 ret = register_tracer(&osnoise_tracer);
2649 pr_err(BANNER "Error registering osnoise!\n");
2653 ret = init_timerlat_tracer();
2655 pr_err(BANNER "Error registering timerlat!\n");
2659 osnoise_init_hotplug_support();
2661 INIT_LIST_HEAD_RCU(&osnoise_instances);
2667 late_initcall(init_osnoise_tracer);