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 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 */ 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 */ 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 */ 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 184bce29ac9SDaniel Bristot de Oliveira /* 185bce29ac9SDaniel Bristot de Oliveira * sofirq runtime info. 186bce29ac9SDaniel Bristot de Oliveira */ 187bce29ac9SDaniel Bristot de Oliveira struct osn_softirq { 188bce29ac9SDaniel Bristot de Oliveira u64 count; 189bce29ac9SDaniel Bristot de Oliveira u64 arrival_time; 190bce29ac9SDaniel Bristot de Oliveira u64 delta_start; 191bce29ac9SDaniel Bristot de Oliveira }; 192bce29ac9SDaniel Bristot de Oliveira 193bce29ac9SDaniel Bristot de Oliveira /* 194bce29ac9SDaniel Bristot de Oliveira * thread runtime info. 195bce29ac9SDaniel Bristot de Oliveira */ 196bce29ac9SDaniel Bristot de Oliveira struct osn_thread { 197bce29ac9SDaniel Bristot de Oliveira u64 count; 198bce29ac9SDaniel Bristot de Oliveira u64 arrival_time; 199bce29ac9SDaniel Bristot de Oliveira u64 delta_start; 200bce29ac9SDaniel Bristot de Oliveira }; 201bce29ac9SDaniel Bristot de Oliveira 202bce29ac9SDaniel Bristot de Oliveira /* 203bce29ac9SDaniel Bristot de Oliveira * Runtime information: this structure saves the runtime information used by 204bce29ac9SDaniel Bristot de Oliveira * one sampling thread. 205bce29ac9SDaniel Bristot de Oliveira */ 206bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables { 207bce29ac9SDaniel Bristot de Oliveira struct task_struct *kthread; 208bce29ac9SDaniel Bristot de Oliveira bool sampling; 209bce29ac9SDaniel Bristot de Oliveira pid_t pid; 210bce29ac9SDaniel Bristot de Oliveira struct osn_nmi nmi; 211bce29ac9SDaniel Bristot de Oliveira struct osn_irq irq; 212bce29ac9SDaniel Bristot de Oliveira struct osn_softirq softirq; 213bce29ac9SDaniel Bristot de Oliveira struct osn_thread thread; 214bce29ac9SDaniel Bristot de Oliveira local_t int_counter; 215bce29ac9SDaniel Bristot de Oliveira }; 216bce29ac9SDaniel Bristot de Oliveira 217bce29ac9SDaniel Bristot de Oliveira /* 218bce29ac9SDaniel Bristot de Oliveira * Per-cpu runtime information. 219bce29ac9SDaniel Bristot de Oliveira */ 2207a025e06STom Rix static DEFINE_PER_CPU(struct osnoise_variables, per_cpu_osnoise_var); 221bce29ac9SDaniel Bristot de Oliveira 222bce29ac9SDaniel Bristot de Oliveira /* 223bce29ac9SDaniel Bristot de Oliveira * this_cpu_osn_var - Return the per-cpu osnoise_variables on its relative CPU 224bce29ac9SDaniel Bristot de Oliveira */ 225bce29ac9SDaniel Bristot de Oliveira static inline struct osnoise_variables *this_cpu_osn_var(void) 226bce29ac9SDaniel Bristot de Oliveira { 227bce29ac9SDaniel Bristot de Oliveira return this_cpu_ptr(&per_cpu_osnoise_var); 228bce29ac9SDaniel Bristot de Oliveira } 229bce29ac9SDaniel Bristot de Oliveira 230a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 231bce29ac9SDaniel Bristot de Oliveira /* 232a955d7eaSDaniel Bristot de Oliveira * Runtime information for the timer mode. 233bce29ac9SDaniel Bristot de Oliveira */ 234a955d7eaSDaniel Bristot de Oliveira struct timerlat_variables { 235a955d7eaSDaniel Bristot de Oliveira struct task_struct *kthread; 236a955d7eaSDaniel Bristot de Oliveira struct hrtimer timer; 237a955d7eaSDaniel Bristot de Oliveira u64 rel_period; 238a955d7eaSDaniel Bristot de Oliveira u64 abs_period; 239a955d7eaSDaniel Bristot de Oliveira bool tracing_thread; 240a955d7eaSDaniel Bristot de Oliveira u64 count; 241a955d7eaSDaniel Bristot de Oliveira }; 242a955d7eaSDaniel Bristot de Oliveira 2437a025e06STom Rix static DEFINE_PER_CPU(struct timerlat_variables, per_cpu_timerlat_var); 244a955d7eaSDaniel Bristot de Oliveira 245a955d7eaSDaniel Bristot de Oliveira /* 246a955d7eaSDaniel Bristot de Oliveira * this_cpu_tmr_var - Return the per-cpu timerlat_variables on its relative CPU 247a955d7eaSDaniel Bristot de Oliveira */ 248a955d7eaSDaniel Bristot de Oliveira static inline struct timerlat_variables *this_cpu_tmr_var(void) 249bce29ac9SDaniel Bristot de Oliveira { 250a955d7eaSDaniel Bristot de Oliveira return this_cpu_ptr(&per_cpu_timerlat_var); 251a955d7eaSDaniel Bristot de Oliveira } 252a955d7eaSDaniel Bristot de Oliveira 253a955d7eaSDaniel Bristot de Oliveira /* 254a955d7eaSDaniel Bristot de Oliveira * tlat_var_reset - Reset the values of the given timerlat_variables 255a955d7eaSDaniel Bristot de Oliveira */ 256a955d7eaSDaniel Bristot de Oliveira static inline void tlat_var_reset(void) 257a955d7eaSDaniel Bristot de Oliveira { 258a955d7eaSDaniel Bristot de Oliveira struct timerlat_variables *tlat_var; 259a955d7eaSDaniel Bristot de Oliveira int cpu; 260bce29ac9SDaniel Bristot de Oliveira /* 261bce29ac9SDaniel Bristot de Oliveira * So far, all the values are initialized as 0, so 262bce29ac9SDaniel Bristot de Oliveira * zeroing the structure is perfect. 263bce29ac9SDaniel Bristot de Oliveira */ 264a955d7eaSDaniel Bristot de Oliveira for_each_cpu(cpu, cpu_online_mask) { 265a955d7eaSDaniel Bristot de Oliveira tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu); 266a955d7eaSDaniel Bristot de Oliveira memset(tlat_var, 0, sizeof(*tlat_var)); 267a955d7eaSDaniel Bristot de Oliveira } 268a955d7eaSDaniel Bristot de Oliveira } 269a955d7eaSDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */ 270a955d7eaSDaniel Bristot de Oliveira #define tlat_var_reset() do {} while (0) 271a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */ 272a955d7eaSDaniel Bristot de Oliveira 273a955d7eaSDaniel Bristot de Oliveira /* 274a955d7eaSDaniel Bristot de Oliveira * osn_var_reset - Reset the values of the given osnoise_variables 275a955d7eaSDaniel Bristot de Oliveira */ 276a955d7eaSDaniel Bristot de Oliveira static inline void osn_var_reset(void) 277a955d7eaSDaniel Bristot de Oliveira { 278a955d7eaSDaniel Bristot de Oliveira struct osnoise_variables *osn_var; 279a955d7eaSDaniel Bristot de Oliveira int cpu; 280a955d7eaSDaniel Bristot de Oliveira 281a955d7eaSDaniel Bristot de Oliveira /* 282a955d7eaSDaniel Bristot de Oliveira * So far, all the values are initialized as 0, so 283a955d7eaSDaniel Bristot de Oliveira * zeroing the structure is perfect. 284a955d7eaSDaniel Bristot de Oliveira */ 285a955d7eaSDaniel Bristot de Oliveira for_each_cpu(cpu, cpu_online_mask) { 286a955d7eaSDaniel Bristot de Oliveira osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu); 287bce29ac9SDaniel Bristot de Oliveira memset(osn_var, 0, sizeof(*osn_var)); 288bce29ac9SDaniel Bristot de Oliveira } 289a955d7eaSDaniel Bristot de Oliveira } 290bce29ac9SDaniel Bristot de Oliveira 291bce29ac9SDaniel Bristot de Oliveira /* 292bce29ac9SDaniel Bristot de Oliveira * osn_var_reset_all - Reset the value of all per-cpu osnoise_variables 293bce29ac9SDaniel Bristot de Oliveira */ 294bce29ac9SDaniel Bristot de Oliveira static inline void osn_var_reset_all(void) 295bce29ac9SDaniel Bristot de Oliveira { 296a955d7eaSDaniel Bristot de Oliveira osn_var_reset(); 297a955d7eaSDaniel Bristot de Oliveira tlat_var_reset(); 298bce29ac9SDaniel Bristot de Oliveira } 299bce29ac9SDaniel Bristot de Oliveira 300bce29ac9SDaniel Bristot de Oliveira /* 301bce29ac9SDaniel Bristot de Oliveira * Tells NMIs to call back to the osnoise tracer to record timestamps. 302bce29ac9SDaniel Bristot de Oliveira */ 303bce29ac9SDaniel Bristot de Oliveira bool trace_osnoise_callback_enabled; 304bce29ac9SDaniel Bristot de Oliveira 305bce29ac9SDaniel Bristot de Oliveira /* 306bce29ac9SDaniel Bristot de Oliveira * osnoise sample structure definition. Used to store the statistics of a 307bce29ac9SDaniel Bristot de Oliveira * sample run. 308bce29ac9SDaniel Bristot de Oliveira */ 309bce29ac9SDaniel Bristot de Oliveira struct osnoise_sample { 310bce29ac9SDaniel Bristot de Oliveira u64 runtime; /* runtime */ 311bce29ac9SDaniel Bristot de Oliveira u64 noise; /* noise */ 312bce29ac9SDaniel Bristot de Oliveira u64 max_sample; /* max single noise sample */ 313bce29ac9SDaniel Bristot de Oliveira int hw_count; /* # HW (incl. hypervisor) interference */ 314bce29ac9SDaniel Bristot de Oliveira int nmi_count; /* # NMIs during this sample */ 315bce29ac9SDaniel Bristot de Oliveira int irq_count; /* # IRQs during this sample */ 316bce29ac9SDaniel Bristot de Oliveira int softirq_count; /* # softirqs during this sample */ 317bce29ac9SDaniel Bristot de Oliveira int thread_count; /* # threads during this sample */ 318bce29ac9SDaniel Bristot de Oliveira }; 319bce29ac9SDaniel Bristot de Oliveira 320a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 321a955d7eaSDaniel Bristot de Oliveira /* 322a955d7eaSDaniel Bristot de Oliveira * timerlat sample structure definition. Used to store the statistics of 323a955d7eaSDaniel Bristot de Oliveira * a sample run. 324a955d7eaSDaniel Bristot de Oliveira */ 325a955d7eaSDaniel Bristot de Oliveira struct timerlat_sample { 326a955d7eaSDaniel Bristot de Oliveira u64 timer_latency; /* timer_latency */ 327a955d7eaSDaniel Bristot de Oliveira unsigned int seqnum; /* unique sequence */ 328a955d7eaSDaniel Bristot de Oliveira int context; /* timer context */ 329a955d7eaSDaniel Bristot de Oliveira }; 330a955d7eaSDaniel Bristot de Oliveira #endif 331a955d7eaSDaniel Bristot de Oliveira 332bce29ac9SDaniel Bristot de Oliveira /* 333bce29ac9SDaniel Bristot de Oliveira * Protect the interface. 334bce29ac9SDaniel Bristot de Oliveira */ 3357a025e06STom Rix static struct mutex interface_lock; 336bce29ac9SDaniel Bristot de Oliveira 337bce29ac9SDaniel Bristot de Oliveira /* 338bce29ac9SDaniel Bristot de Oliveira * Tracer data. 339bce29ac9SDaniel Bristot de Oliveira */ 340bce29ac9SDaniel Bristot de Oliveira static struct osnoise_data { 341bce29ac9SDaniel Bristot de Oliveira u64 sample_period; /* total sampling period */ 342bce29ac9SDaniel Bristot de Oliveira u64 sample_runtime; /* active sampling portion of period */ 343a955d7eaSDaniel Bristot de Oliveira u64 stop_tracing; /* stop trace in the internal operation (loop/irq) */ 344a955d7eaSDaniel Bristot de Oliveira u64 stop_tracing_total; /* stop trace in the final operation (report/thread) */ 345a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 346a955d7eaSDaniel Bristot de Oliveira u64 timerlat_period; /* timerlat period */ 347a955d7eaSDaniel Bristot de Oliveira u64 print_stack; /* print IRQ stack if total > */ 348a955d7eaSDaniel Bristot de Oliveira int timerlat_tracer; /* timerlat tracer */ 349a955d7eaSDaniel Bristot de Oliveira #endif 350bce29ac9SDaniel Bristot de Oliveira bool tainted; /* infor users and developers about a problem */ 351bce29ac9SDaniel Bristot de Oliveira } osnoise_data = { 352bce29ac9SDaniel Bristot de Oliveira .sample_period = DEFAULT_SAMPLE_PERIOD, 353bce29ac9SDaniel Bristot de Oliveira .sample_runtime = DEFAULT_SAMPLE_RUNTIME, 354bce29ac9SDaniel Bristot de Oliveira .stop_tracing = 0, 355bce29ac9SDaniel Bristot de Oliveira .stop_tracing_total = 0, 356a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 357a955d7eaSDaniel Bristot de Oliveira .print_stack = 0, 358a955d7eaSDaniel Bristot de Oliveira .timerlat_period = DEFAULT_TIMERLAT_PERIOD, 359a955d7eaSDaniel Bristot de Oliveira .timerlat_tracer = 0, 360a955d7eaSDaniel Bristot de Oliveira #endif 361bce29ac9SDaniel Bristot de Oliveira }; 362bce29ac9SDaniel Bristot de Oliveira 363ccb67544SDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 364ccb67544SDaniel Bristot de Oliveira static inline bool timerlat_enabled(void) 365ccb67544SDaniel Bristot de Oliveira { 366ccb67544SDaniel Bristot de Oliveira return osnoise_data.timerlat_tracer; 367ccb67544SDaniel Bristot de Oliveira } 368ccb67544SDaniel Bristot de Oliveira 369ccb67544SDaniel Bristot de Oliveira static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var) 370ccb67544SDaniel Bristot de Oliveira { 371ccb67544SDaniel Bristot de Oliveira struct timerlat_variables *tlat_var = this_cpu_tmr_var(); 372ccb67544SDaniel Bristot de Oliveira /* 373ccb67544SDaniel Bristot de Oliveira * If the timerlat is enabled, but the irq handler did 374ccb67544SDaniel Bristot de Oliveira * not run yet enabling timerlat_tracer, do not trace. 375ccb67544SDaniel Bristot de Oliveira */ 376ccb67544SDaniel Bristot de Oliveira if (!tlat_var->tracing_thread) { 377ccb67544SDaniel Bristot de Oliveira osn_var->softirq.arrival_time = 0; 378ccb67544SDaniel Bristot de Oliveira osn_var->softirq.delta_start = 0; 379ccb67544SDaniel Bristot de Oliveira return 0; 380ccb67544SDaniel Bristot de Oliveira } 381ccb67544SDaniel Bristot de Oliveira return 1; 382ccb67544SDaniel Bristot de Oliveira } 383ccb67544SDaniel Bristot de Oliveira 384ccb67544SDaniel Bristot de Oliveira static inline int timerlat_thread_exit(struct osnoise_variables *osn_var) 385ccb67544SDaniel Bristot de Oliveira { 386ccb67544SDaniel Bristot de Oliveira struct timerlat_variables *tlat_var = this_cpu_tmr_var(); 387ccb67544SDaniel Bristot de Oliveira /* 388ccb67544SDaniel Bristot de Oliveira * If the timerlat is enabled, but the irq handler did 389ccb67544SDaniel Bristot de Oliveira * not run yet enabling timerlat_tracer, do not trace. 390ccb67544SDaniel Bristot de Oliveira */ 391ccb67544SDaniel Bristot de Oliveira if (!tlat_var->tracing_thread) { 392ccb67544SDaniel Bristot de Oliveira osn_var->thread.delta_start = 0; 393ccb67544SDaniel Bristot de Oliveira osn_var->thread.arrival_time = 0; 394ccb67544SDaniel Bristot de Oliveira return 0; 395ccb67544SDaniel Bristot de Oliveira } 396ccb67544SDaniel Bristot de Oliveira return 1; 397ccb67544SDaniel Bristot de Oliveira } 398ccb67544SDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */ 399ccb67544SDaniel Bristot de Oliveira static inline bool timerlat_enabled(void) 400ccb67544SDaniel Bristot de Oliveira { 401ccb67544SDaniel Bristot de Oliveira return false; 402ccb67544SDaniel Bristot de Oliveira } 403ccb67544SDaniel Bristot de Oliveira 404ccb67544SDaniel Bristot de Oliveira static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var) 405ccb67544SDaniel Bristot de Oliveira { 406ccb67544SDaniel Bristot de Oliveira return 1; 407ccb67544SDaniel Bristot de Oliveira } 408ccb67544SDaniel Bristot de Oliveira static inline int timerlat_thread_exit(struct osnoise_variables *osn_var) 409ccb67544SDaniel Bristot de Oliveira { 410ccb67544SDaniel Bristot de Oliveira return 1; 411ccb67544SDaniel Bristot de Oliveira } 412ccb67544SDaniel Bristot de Oliveira #endif 413ccb67544SDaniel Bristot de Oliveira 414d03721a6SDaniel Bristot de Oliveira #ifdef CONFIG_PREEMPT_RT 415bce29ac9SDaniel Bristot de Oliveira /* 416bce29ac9SDaniel Bristot de Oliveira * Print the osnoise header info. 417bce29ac9SDaniel Bristot de Oliveira */ 418bce29ac9SDaniel Bristot de Oliveira static void print_osnoise_headers(struct seq_file *s) 419bce29ac9SDaniel Bristot de Oliveira { 420bce29ac9SDaniel Bristot de Oliveira if (osnoise_data.tainted) 421bce29ac9SDaniel Bristot de Oliveira seq_puts(s, "# osnoise is tainted!\n"); 422bce29ac9SDaniel Bristot de Oliveira 423d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# _-------=> irqs-off\n"); 424d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# / _------=> need-resched\n"); 425d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# | / _-----=> need-resched-lazy\n"); 426d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# || / _----=> hardirq/softirq\n"); 427d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# ||| / _---=> preempt-depth\n"); 428d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n"); 429d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# ||||| / _-=> migrate-disable\n"); 430d03721a6SDaniel Bristot de Oliveira 431d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# |||||| / "); 432d03721a6SDaniel Bristot de Oliveira seq_puts(s, " MAX\n"); 433d03721a6SDaniel Bristot de Oliveira 434d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# ||||| / "); 435d03721a6SDaniel Bristot de Oliveira seq_puts(s, " SINGLE Interference counters:\n"); 436d03721a6SDaniel Bristot de Oliveira 437d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# ||||||| RUNTIME "); 438d03721a6SDaniel Bristot de Oliveira seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n"); 439d03721a6SDaniel Bristot de Oliveira 440d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP IN US "); 441d03721a6SDaniel Bristot de Oliveira seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n"); 442d03721a6SDaniel Bristot de Oliveira 443d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# | | | ||||||| | | "); 444d03721a6SDaniel Bristot de Oliveira seq_puts(s, " | | | | | | | |\n"); 445d03721a6SDaniel Bristot de Oliveira } 446d03721a6SDaniel Bristot de Oliveira #else /* CONFIG_PREEMPT_RT */ 447d03721a6SDaniel Bristot de Oliveira static void print_osnoise_headers(struct seq_file *s) 448d03721a6SDaniel Bristot de Oliveira { 449d03721a6SDaniel Bristot de Oliveira if (osnoise_data.tainted) 450d03721a6SDaniel Bristot de Oliveira seq_puts(s, "# osnoise is tainted!\n"); 451d03721a6SDaniel Bristot de Oliveira 452bce29ac9SDaniel Bristot de Oliveira seq_puts(s, "# _-----=> irqs-off\n"); 453bce29ac9SDaniel Bristot de Oliveira seq_puts(s, "# / _----=> need-resched\n"); 454bce29ac9SDaniel Bristot de Oliveira seq_puts(s, "# | / _---=> hardirq/softirq\n"); 455e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# || / _--=> preempt-depth\n"); 456e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# ||| / _-=> migrate-disable "); 457bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " MAX\n"); 458e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# |||| / delay "); 459bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " SINGLE Interference counters:\n"); 460bce29ac9SDaniel Bristot de Oliveira 461e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# ||||| RUNTIME "); 462bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n"); 463bce29ac9SDaniel Bristot de Oliveira 464e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP IN US "); 465bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n"); 466bce29ac9SDaniel Bristot de Oliveira 467e0f3b18bSDaniel Bristot de Oliveira seq_puts(s, "# | | | ||||| | | "); 468bce29ac9SDaniel Bristot de Oliveira seq_puts(s, " | | | | | | | |\n"); 469bce29ac9SDaniel Bristot de Oliveira } 470d03721a6SDaniel Bristot de Oliveira #endif /* CONFIG_PREEMPT_RT */ 471bce29ac9SDaniel Bristot de Oliveira 472bce29ac9SDaniel Bristot de Oliveira /* 473bce29ac9SDaniel Bristot de Oliveira * osnoise_taint - report an osnoise error. 474bce29ac9SDaniel Bristot de Oliveira */ 475bce29ac9SDaniel Bristot de Oliveira #define osnoise_taint(msg) ({ \ 476dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst; \ 477dae18134SDaniel Bristot de Oliveira struct trace_buffer *buffer; \ 478bce29ac9SDaniel Bristot de Oliveira \ 479dae18134SDaniel Bristot de Oliveira rcu_read_lock(); \ 480dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) { \ 481dae18134SDaniel Bristot de Oliveira buffer = inst->tr->array_buffer.buffer; \ 482dae18134SDaniel Bristot de Oliveira trace_array_printk_buf(buffer, _THIS_IP_, msg); \ 483dae18134SDaniel Bristot de Oliveira } \ 484dae18134SDaniel Bristot de Oliveira rcu_read_unlock(); \ 485bce29ac9SDaniel Bristot de Oliveira osnoise_data.tainted = true; \ 486bce29ac9SDaniel Bristot de Oliveira }) 487bce29ac9SDaniel Bristot de Oliveira 488bce29ac9SDaniel Bristot de Oliveira /* 489bce29ac9SDaniel Bristot de Oliveira * Record an osnoise_sample into the tracer buffer. 490bce29ac9SDaniel Bristot de Oliveira */ 491dae18134SDaniel Bristot de Oliveira static void 492dae18134SDaniel Bristot de Oliveira __trace_osnoise_sample(struct osnoise_sample *sample, struct trace_buffer *buffer) 493bce29ac9SDaniel Bristot de Oliveira { 494bce29ac9SDaniel Bristot de Oliveira struct trace_event_call *call = &event_osnoise; 495bce29ac9SDaniel Bristot de Oliveira struct ring_buffer_event *event; 496bce29ac9SDaniel Bristot de Oliveira struct osnoise_entry *entry; 497bce29ac9SDaniel Bristot de Oliveira 498bce29ac9SDaniel Bristot de Oliveira event = trace_buffer_lock_reserve(buffer, TRACE_OSNOISE, sizeof(*entry), 499bce29ac9SDaniel Bristot de Oliveira tracing_gen_ctx()); 500bce29ac9SDaniel Bristot de Oliveira if (!event) 501bce29ac9SDaniel Bristot de Oliveira return; 502bce29ac9SDaniel Bristot de Oliveira entry = ring_buffer_event_data(event); 503bce29ac9SDaniel Bristot de Oliveira entry->runtime = sample->runtime; 504bce29ac9SDaniel Bristot de Oliveira entry->noise = sample->noise; 505bce29ac9SDaniel Bristot de Oliveira entry->max_sample = sample->max_sample; 506bce29ac9SDaniel Bristot de Oliveira entry->hw_count = sample->hw_count; 507bce29ac9SDaniel Bristot de Oliveira entry->nmi_count = sample->nmi_count; 508bce29ac9SDaniel Bristot de Oliveira entry->irq_count = sample->irq_count; 509bce29ac9SDaniel Bristot de Oliveira entry->softirq_count = sample->softirq_count; 510bce29ac9SDaniel Bristot de Oliveira entry->thread_count = sample->thread_count; 511bce29ac9SDaniel Bristot de Oliveira 512bce29ac9SDaniel Bristot de Oliveira if (!call_filter_check_discard(call, entry, buffer, event)) 513bce29ac9SDaniel Bristot de Oliveira trace_buffer_unlock_commit_nostack(buffer, event); 514bce29ac9SDaniel Bristot de Oliveira } 515bce29ac9SDaniel Bristot de Oliveira 516dae18134SDaniel Bristot de Oliveira /* 517dae18134SDaniel Bristot de Oliveira * Record an osnoise_sample on all osnoise instances. 518dae18134SDaniel Bristot de Oliveira */ 519dae18134SDaniel Bristot de Oliveira static void trace_osnoise_sample(struct osnoise_sample *sample) 520dae18134SDaniel Bristot de Oliveira { 521dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst; 522dae18134SDaniel Bristot de Oliveira struct trace_buffer *buffer; 523dae18134SDaniel Bristot de Oliveira 524dae18134SDaniel Bristot de Oliveira rcu_read_lock(); 525dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) { 526dae18134SDaniel Bristot de Oliveira buffer = inst->tr->array_buffer.buffer; 527dae18134SDaniel Bristot de Oliveira __trace_osnoise_sample(sample, buffer); 528dae18134SDaniel Bristot de Oliveira } 529dae18134SDaniel Bristot de Oliveira rcu_read_unlock(); 530dae18134SDaniel Bristot de Oliveira } 531dae18134SDaniel Bristot de Oliveira 532a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 533a955d7eaSDaniel Bristot de Oliveira /* 534a955d7eaSDaniel Bristot de Oliveira * Print the timerlat header info. 535a955d7eaSDaniel Bristot de Oliveira */ 536e1c4ad4aSDaniel Bristot de Oliveira #ifdef CONFIG_PREEMPT_RT 537e1c4ad4aSDaniel Bristot de Oliveira static void print_timerlat_headers(struct seq_file *s) 538e1c4ad4aSDaniel Bristot de Oliveira { 539e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# _-------=> irqs-off\n"); 540e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# / _------=> need-resched\n"); 541e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# | / _-----=> need-resched-lazy\n"); 542e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# || / _----=> hardirq/softirq\n"); 543e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# ||| / _---=> preempt-depth\n"); 544e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n"); 545e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# ||||| / _-=> migrate-disable\n"); 546e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# |||||| /\n"); 547e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# ||||||| ACTIVATION\n"); 548e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP ID "); 549e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, " CONTEXT LATENCY\n"); 550e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, "# | | | ||||||| | | "); 551e1c4ad4aSDaniel Bristot de Oliveira seq_puts(s, " | |\n"); 552e1c4ad4aSDaniel Bristot de Oliveira } 553e1c4ad4aSDaniel Bristot de Oliveira #else /* CONFIG_PREEMPT_RT */ 554a955d7eaSDaniel Bristot de Oliveira static void print_timerlat_headers(struct seq_file *s) 555a955d7eaSDaniel Bristot de Oliveira { 556a955d7eaSDaniel Bristot de Oliveira seq_puts(s, "# _-----=> irqs-off\n"); 557a955d7eaSDaniel Bristot de Oliveira seq_puts(s, "# / _----=> need-resched\n"); 558a955d7eaSDaniel Bristot de Oliveira seq_puts(s, "# | / _---=> hardirq/softirq\n"); 559a955d7eaSDaniel Bristot de Oliveira seq_puts(s, "# || / _--=> preempt-depth\n"); 560aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# ||| / _-=> migrate-disable\n"); 561aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# |||| / delay\n"); 562aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# ||||| ACTIVATION\n"); 563aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP ID "); 564a955d7eaSDaniel Bristot de Oliveira seq_puts(s, " CONTEXT LATENCY\n"); 565aeafcb82SDaniel Bristot de Oliveira seq_puts(s, "# | | | ||||| | | "); 566a955d7eaSDaniel Bristot de Oliveira seq_puts(s, " | |\n"); 567a955d7eaSDaniel Bristot de Oliveira } 568e1c4ad4aSDaniel Bristot de Oliveira #endif /* CONFIG_PREEMPT_RT */ 569a955d7eaSDaniel Bristot de Oliveira 570dae18134SDaniel Bristot de Oliveira static void 571dae18134SDaniel Bristot de Oliveira __trace_timerlat_sample(struct timerlat_sample *sample, struct trace_buffer *buffer) 572a955d7eaSDaniel Bristot de Oliveira { 573a955d7eaSDaniel Bristot de Oliveira struct trace_event_call *call = &event_osnoise; 574a955d7eaSDaniel Bristot de Oliveira struct ring_buffer_event *event; 575a955d7eaSDaniel Bristot de Oliveira struct timerlat_entry *entry; 576a955d7eaSDaniel Bristot de Oliveira 577a955d7eaSDaniel Bristot de Oliveira event = trace_buffer_lock_reserve(buffer, TRACE_TIMERLAT, sizeof(*entry), 578a955d7eaSDaniel Bristot de Oliveira tracing_gen_ctx()); 579a955d7eaSDaniel Bristot de Oliveira if (!event) 580a955d7eaSDaniel Bristot de Oliveira return; 581a955d7eaSDaniel Bristot de Oliveira entry = ring_buffer_event_data(event); 582a955d7eaSDaniel Bristot de Oliveira entry->seqnum = sample->seqnum; 583a955d7eaSDaniel Bristot de Oliveira entry->context = sample->context; 584a955d7eaSDaniel Bristot de Oliveira entry->timer_latency = sample->timer_latency; 585a955d7eaSDaniel Bristot de Oliveira 586a955d7eaSDaniel Bristot de Oliveira if (!call_filter_check_discard(call, entry, buffer, event)) 587a955d7eaSDaniel Bristot de Oliveira trace_buffer_unlock_commit_nostack(buffer, event); 588a955d7eaSDaniel Bristot de Oliveira } 589a955d7eaSDaniel Bristot de Oliveira 590dae18134SDaniel Bristot de Oliveira /* 591dae18134SDaniel Bristot de Oliveira * Record an timerlat_sample into the tracer buffer. 592dae18134SDaniel Bristot de Oliveira */ 593dae18134SDaniel Bristot de Oliveira static void trace_timerlat_sample(struct timerlat_sample *sample) 594dae18134SDaniel Bristot de Oliveira { 595dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst; 596dae18134SDaniel Bristot de Oliveira struct trace_buffer *buffer; 597dae18134SDaniel Bristot de Oliveira 598dae18134SDaniel Bristot de Oliveira rcu_read_lock(); 599dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) { 600dae18134SDaniel Bristot de Oliveira buffer = inst->tr->array_buffer.buffer; 601dae18134SDaniel Bristot de Oliveira __trace_timerlat_sample(sample, buffer); 602dae18134SDaniel Bristot de Oliveira } 603dae18134SDaniel Bristot de Oliveira rcu_read_unlock(); 604dae18134SDaniel Bristot de Oliveira } 605dae18134SDaniel Bristot de Oliveira 606a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_STACKTRACE 607a955d7eaSDaniel Bristot de Oliveira 608a955d7eaSDaniel Bristot de Oliveira #define MAX_CALLS 256 609a955d7eaSDaniel Bristot de Oliveira 610a955d7eaSDaniel Bristot de Oliveira /* 611a955d7eaSDaniel Bristot de Oliveira * Stack trace will take place only at IRQ level, so, no need 612a955d7eaSDaniel Bristot de Oliveira * to control nesting here. 613a955d7eaSDaniel Bristot de Oliveira */ 614a955d7eaSDaniel Bristot de Oliveira struct trace_stack { 615a955d7eaSDaniel Bristot de Oliveira int stack_size; 616a955d7eaSDaniel Bristot de Oliveira int nr_entries; 617a955d7eaSDaniel Bristot de Oliveira unsigned long calls[MAX_CALLS]; 618a955d7eaSDaniel Bristot de Oliveira }; 619a955d7eaSDaniel Bristot de Oliveira 620a955d7eaSDaniel Bristot de Oliveira static DEFINE_PER_CPU(struct trace_stack, trace_stack); 621a955d7eaSDaniel Bristot de Oliveira 622a955d7eaSDaniel Bristot de Oliveira /* 623a955d7eaSDaniel Bristot de Oliveira * timerlat_save_stack - save a stack trace without printing 624a955d7eaSDaniel Bristot de Oliveira * 625a955d7eaSDaniel Bristot de Oliveira * Save the current stack trace without printing. The 626a955d7eaSDaniel Bristot de Oliveira * stack will be printed later, after the end of the measurement. 627a955d7eaSDaniel Bristot de Oliveira */ 628a955d7eaSDaniel Bristot de Oliveira static void timerlat_save_stack(int skip) 629a955d7eaSDaniel Bristot de Oliveira { 630a955d7eaSDaniel Bristot de Oliveira unsigned int size, nr_entries; 631a955d7eaSDaniel Bristot de Oliveira struct trace_stack *fstack; 632a955d7eaSDaniel Bristot de Oliveira 633a955d7eaSDaniel Bristot de Oliveira fstack = this_cpu_ptr(&trace_stack); 634a955d7eaSDaniel Bristot de Oliveira 635a955d7eaSDaniel Bristot de Oliveira size = ARRAY_SIZE(fstack->calls); 636a955d7eaSDaniel Bristot de Oliveira 637a955d7eaSDaniel Bristot de Oliveira nr_entries = stack_trace_save(fstack->calls, size, skip); 638a955d7eaSDaniel Bristot de Oliveira 639a955d7eaSDaniel Bristot de Oliveira fstack->stack_size = nr_entries * sizeof(unsigned long); 640a955d7eaSDaniel Bristot de Oliveira fstack->nr_entries = nr_entries; 641a955d7eaSDaniel Bristot de Oliveira 642a955d7eaSDaniel Bristot de Oliveira return; 643a955d7eaSDaniel Bristot de Oliveira 644a955d7eaSDaniel Bristot de Oliveira } 645dae18134SDaniel Bristot de Oliveira 646dae18134SDaniel Bristot de Oliveira static void 647dae18134SDaniel Bristot de Oliveira __timerlat_dump_stack(struct trace_buffer *buffer, struct trace_stack *fstack, unsigned int size) 648a955d7eaSDaniel Bristot de Oliveira { 649a955d7eaSDaniel Bristot de Oliveira struct trace_event_call *call = &event_osnoise; 650a955d7eaSDaniel Bristot de Oliveira struct ring_buffer_event *event; 651a955d7eaSDaniel Bristot de Oliveira struct stack_entry *entry; 652a955d7eaSDaniel Bristot de Oliveira 653a955d7eaSDaniel Bristot de Oliveira event = trace_buffer_lock_reserve(buffer, TRACE_STACK, sizeof(*entry) + size, 654a955d7eaSDaniel Bristot de Oliveira tracing_gen_ctx()); 655a955d7eaSDaniel Bristot de Oliveira if (!event) 656dae18134SDaniel Bristot de Oliveira return; 657a955d7eaSDaniel Bristot de Oliveira 658a955d7eaSDaniel Bristot de Oliveira entry = ring_buffer_event_data(event); 659a955d7eaSDaniel Bristot de Oliveira 660a955d7eaSDaniel Bristot de Oliveira memcpy(&entry->caller, fstack->calls, size); 661a955d7eaSDaniel Bristot de Oliveira entry->size = fstack->nr_entries; 662a955d7eaSDaniel Bristot de Oliveira 663a955d7eaSDaniel Bristot de Oliveira if (!call_filter_check_discard(call, entry, buffer, event)) 664a955d7eaSDaniel Bristot de Oliveira trace_buffer_unlock_commit_nostack(buffer, event); 665dae18134SDaniel Bristot de Oliveira } 666a955d7eaSDaniel Bristot de Oliveira 667dae18134SDaniel Bristot de Oliveira /* 668dae18134SDaniel Bristot de Oliveira * timerlat_dump_stack - dump a stack trace previously saved 669dae18134SDaniel Bristot de Oliveira */ 670b14f4568SDaniel Bristot de Oliveira static void timerlat_dump_stack(u64 latency) 671dae18134SDaniel Bristot de Oliveira { 672dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst; 673dae18134SDaniel Bristot de Oliveira struct trace_buffer *buffer; 674dae18134SDaniel Bristot de Oliveira struct trace_stack *fstack; 675dae18134SDaniel Bristot de Oliveira unsigned int size; 676dae18134SDaniel Bristot de Oliveira 677b14f4568SDaniel Bristot de Oliveira /* 678b14f4568SDaniel Bristot de Oliveira * trace only if latency > print_stack config, if enabled. 679b14f4568SDaniel Bristot de Oliveira */ 680b14f4568SDaniel Bristot de Oliveira if (!osnoise_data.print_stack || osnoise_data.print_stack > latency) 681b14f4568SDaniel Bristot de Oliveira return; 682b14f4568SDaniel Bristot de Oliveira 683dae18134SDaniel Bristot de Oliveira preempt_disable_notrace(); 684dae18134SDaniel Bristot de Oliveira fstack = this_cpu_ptr(&trace_stack); 685dae18134SDaniel Bristot de Oliveira size = fstack->stack_size; 686dae18134SDaniel Bristot de Oliveira 687dae18134SDaniel Bristot de Oliveira rcu_read_lock(); 688dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) { 689dae18134SDaniel Bristot de Oliveira buffer = inst->tr->array_buffer.buffer; 690dae18134SDaniel Bristot de Oliveira __timerlat_dump_stack(buffer, fstack, size); 691dae18134SDaniel Bristot de Oliveira 692dae18134SDaniel Bristot de Oliveira } 693dae18134SDaniel Bristot de Oliveira rcu_read_unlock(); 694a955d7eaSDaniel Bristot de Oliveira preempt_enable_notrace(); 695a955d7eaSDaniel Bristot de Oliveira } 696b14f4568SDaniel Bristot de Oliveira #else /* CONFIG_STACKTRACE */ 697b14f4568SDaniel Bristot de Oliveira #define timerlat_dump_stack(u64 latency) do {} while (0) 698a955d7eaSDaniel Bristot de Oliveira #define timerlat_save_stack(a) do {} while (0) 699a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_STACKTRACE */ 700a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */ 701a955d7eaSDaniel Bristot de Oliveira 702bce29ac9SDaniel Bristot de Oliveira /* 703bce29ac9SDaniel Bristot de Oliveira * Macros to encapsulate the time capturing infrastructure. 704bce29ac9SDaniel Bristot de Oliveira */ 705bce29ac9SDaniel Bristot de Oliveira #define time_get() trace_clock_local() 706bce29ac9SDaniel Bristot de Oliveira #define time_to_us(x) div_u64(x, 1000) 707bce29ac9SDaniel Bristot de Oliveira #define time_sub(a, b) ((a) - (b)) 708bce29ac9SDaniel Bristot de Oliveira 709bce29ac9SDaniel Bristot de Oliveira /* 710bce29ac9SDaniel Bristot de Oliveira * cond_move_irq_delta_start - Forward the delta_start of a running IRQ 711bce29ac9SDaniel Bristot de Oliveira * 712bce29ac9SDaniel Bristot de Oliveira * If an IRQ is preempted by an NMI, its delta_start is pushed forward 713bce29ac9SDaniel Bristot de Oliveira * to discount the NMI interference. 714bce29ac9SDaniel Bristot de Oliveira * 715bce29ac9SDaniel Bristot de Oliveira * See get_int_safe_duration(). 716bce29ac9SDaniel Bristot de Oliveira */ 717bce29ac9SDaniel Bristot de Oliveira static inline void 718bce29ac9SDaniel Bristot de Oliveira cond_move_irq_delta_start(struct osnoise_variables *osn_var, u64 duration) 719bce29ac9SDaniel Bristot de Oliveira { 720bce29ac9SDaniel Bristot de Oliveira if (osn_var->irq.delta_start) 721bce29ac9SDaniel Bristot de Oliveira osn_var->irq.delta_start += duration; 722bce29ac9SDaniel Bristot de Oliveira } 723bce29ac9SDaniel Bristot de Oliveira 724bce29ac9SDaniel Bristot de Oliveira #ifndef CONFIG_PREEMPT_RT 725bce29ac9SDaniel Bristot de Oliveira /* 726bce29ac9SDaniel Bristot de Oliveira * cond_move_softirq_delta_start - Forward the delta_start of a running softirq. 727bce29ac9SDaniel Bristot de Oliveira * 728bce29ac9SDaniel Bristot de Oliveira * If a softirq is preempted by an IRQ or NMI, its delta_start is pushed 729bce29ac9SDaniel Bristot de Oliveira * forward to discount the interference. 730bce29ac9SDaniel Bristot de Oliveira * 731bce29ac9SDaniel Bristot de Oliveira * See get_int_safe_duration(). 732bce29ac9SDaniel Bristot de Oliveira */ 733bce29ac9SDaniel Bristot de Oliveira static inline void 734bce29ac9SDaniel Bristot de Oliveira cond_move_softirq_delta_start(struct osnoise_variables *osn_var, u64 duration) 735bce29ac9SDaniel Bristot de Oliveira { 736bce29ac9SDaniel Bristot de Oliveira if (osn_var->softirq.delta_start) 737bce29ac9SDaniel Bristot de Oliveira osn_var->softirq.delta_start += duration; 738bce29ac9SDaniel Bristot de Oliveira } 739bce29ac9SDaniel Bristot de Oliveira #else /* CONFIG_PREEMPT_RT */ 740bce29ac9SDaniel Bristot de Oliveira #define cond_move_softirq_delta_start(osn_var, duration) do {} while (0) 741bce29ac9SDaniel Bristot de Oliveira #endif 742bce29ac9SDaniel Bristot de Oliveira 743bce29ac9SDaniel Bristot de Oliveira /* 744bce29ac9SDaniel Bristot de Oliveira * cond_move_thread_delta_start - Forward the delta_start of a running thread 745bce29ac9SDaniel Bristot de Oliveira * 746bce29ac9SDaniel Bristot de Oliveira * If a noisy thread is preempted by an softirq, IRQ or NMI, its delta_start 747bce29ac9SDaniel Bristot de Oliveira * is pushed forward to discount the interference. 748bce29ac9SDaniel Bristot de Oliveira * 749bce29ac9SDaniel Bristot de Oliveira * See get_int_safe_duration(). 750bce29ac9SDaniel Bristot de Oliveira */ 751bce29ac9SDaniel Bristot de Oliveira static inline void 752bce29ac9SDaniel Bristot de Oliveira cond_move_thread_delta_start(struct osnoise_variables *osn_var, u64 duration) 753bce29ac9SDaniel Bristot de Oliveira { 754bce29ac9SDaniel Bristot de Oliveira if (osn_var->thread.delta_start) 755bce29ac9SDaniel Bristot de Oliveira osn_var->thread.delta_start += duration; 756bce29ac9SDaniel Bristot de Oliveira } 757bce29ac9SDaniel Bristot de Oliveira 758bce29ac9SDaniel Bristot de Oliveira /* 759bce29ac9SDaniel Bristot de Oliveira * get_int_safe_duration - Get the duration of a window 760bce29ac9SDaniel Bristot de Oliveira * 761bce29ac9SDaniel Bristot de Oliveira * The irq, softirq and thread varaibles need to have its duration without 762bce29ac9SDaniel Bristot de Oliveira * the interference from higher priority interrupts. Instead of keeping a 763bce29ac9SDaniel Bristot de Oliveira * variable to discount the interrupt interference from these variables, the 764bce29ac9SDaniel Bristot de Oliveira * starting time of these variables are pushed forward with the interrupt's 765bce29ac9SDaniel Bristot de Oliveira * duration. In this way, a single variable is used to: 766bce29ac9SDaniel Bristot de Oliveira * 767bce29ac9SDaniel Bristot de Oliveira * - Know if a given window is being measured. 768bce29ac9SDaniel Bristot de Oliveira * - Account its duration. 769bce29ac9SDaniel Bristot de Oliveira * - Discount the interference. 770bce29ac9SDaniel Bristot de Oliveira * 771bce29ac9SDaniel Bristot de Oliveira * To avoid getting inconsistent values, e.g.,: 772bce29ac9SDaniel Bristot de Oliveira * 773bce29ac9SDaniel Bristot de Oliveira * now = time_get() 774bce29ac9SDaniel Bristot de Oliveira * ---> interrupt! 775bce29ac9SDaniel Bristot de Oliveira * delta_start -= int duration; 776bce29ac9SDaniel Bristot de Oliveira * <--- 777bce29ac9SDaniel Bristot de Oliveira * duration = now - delta_start; 778bce29ac9SDaniel Bristot de Oliveira * 779bce29ac9SDaniel Bristot de Oliveira * result: negative duration if the variable duration before the 780bce29ac9SDaniel Bristot de Oliveira * interrupt was smaller than the interrupt execution. 781bce29ac9SDaniel Bristot de Oliveira * 782bce29ac9SDaniel Bristot de Oliveira * A counter of interrupts is used. If the counter increased, try 783bce29ac9SDaniel Bristot de Oliveira * to capture an interference safe duration. 784bce29ac9SDaniel Bristot de Oliveira */ 785bce29ac9SDaniel Bristot de Oliveira static inline s64 786bce29ac9SDaniel Bristot de Oliveira get_int_safe_duration(struct osnoise_variables *osn_var, u64 *delta_start) 787bce29ac9SDaniel Bristot de Oliveira { 788bce29ac9SDaniel Bristot de Oliveira u64 int_counter, now; 789bce29ac9SDaniel Bristot de Oliveira s64 duration; 790bce29ac9SDaniel Bristot de Oliveira 791bce29ac9SDaniel Bristot de Oliveira do { 792bce29ac9SDaniel Bristot de Oliveira int_counter = local_read(&osn_var->int_counter); 793bce29ac9SDaniel Bristot de Oliveira /* synchronize with interrupts */ 794bce29ac9SDaniel Bristot de Oliveira barrier(); 795bce29ac9SDaniel Bristot de Oliveira 796bce29ac9SDaniel Bristot de Oliveira now = time_get(); 797bce29ac9SDaniel Bristot de Oliveira duration = (now - *delta_start); 798bce29ac9SDaniel Bristot de Oliveira 799bce29ac9SDaniel Bristot de Oliveira /* synchronize with interrupts */ 800bce29ac9SDaniel Bristot de Oliveira barrier(); 801bce29ac9SDaniel Bristot de Oliveira } while (int_counter != local_read(&osn_var->int_counter)); 802bce29ac9SDaniel Bristot de Oliveira 803bce29ac9SDaniel Bristot de Oliveira /* 804bce29ac9SDaniel Bristot de Oliveira * This is an evidence of race conditions that cause 805bce29ac9SDaniel Bristot de Oliveira * a value to be "discounted" too much. 806bce29ac9SDaniel Bristot de Oliveira */ 807bce29ac9SDaniel Bristot de Oliveira if (duration < 0) 808bce29ac9SDaniel Bristot de Oliveira osnoise_taint("Negative duration!\n"); 809bce29ac9SDaniel Bristot de Oliveira 810bce29ac9SDaniel Bristot de Oliveira *delta_start = 0; 811bce29ac9SDaniel Bristot de Oliveira 812bce29ac9SDaniel Bristot de Oliveira return duration; 813bce29ac9SDaniel Bristot de Oliveira } 814bce29ac9SDaniel Bristot de Oliveira 815bce29ac9SDaniel Bristot de Oliveira /* 816bce29ac9SDaniel Bristot de Oliveira * 817bce29ac9SDaniel Bristot de Oliveira * set_int_safe_time - Save the current time on *time, aware of interference 818bce29ac9SDaniel Bristot de Oliveira * 819bce29ac9SDaniel Bristot de Oliveira * Get the time, taking into consideration a possible interference from 820bce29ac9SDaniel Bristot de Oliveira * higher priority interrupts. 821bce29ac9SDaniel Bristot de Oliveira * 822bce29ac9SDaniel Bristot de Oliveira * See get_int_safe_duration() for an explanation. 823bce29ac9SDaniel Bristot de Oliveira */ 824bce29ac9SDaniel Bristot de Oliveira static u64 825bce29ac9SDaniel Bristot de Oliveira set_int_safe_time(struct osnoise_variables *osn_var, u64 *time) 826bce29ac9SDaniel Bristot de Oliveira { 827bce29ac9SDaniel Bristot de Oliveira u64 int_counter; 828bce29ac9SDaniel Bristot de Oliveira 829bce29ac9SDaniel Bristot de Oliveira do { 830bce29ac9SDaniel Bristot de Oliveira int_counter = local_read(&osn_var->int_counter); 831bce29ac9SDaniel Bristot de Oliveira /* synchronize with interrupts */ 832bce29ac9SDaniel Bristot de Oliveira barrier(); 833bce29ac9SDaniel Bristot de Oliveira 834bce29ac9SDaniel Bristot de Oliveira *time = time_get(); 835bce29ac9SDaniel Bristot de Oliveira 836bce29ac9SDaniel Bristot de Oliveira /* synchronize with interrupts */ 837bce29ac9SDaniel Bristot de Oliveira barrier(); 838bce29ac9SDaniel Bristot de Oliveira } while (int_counter != local_read(&osn_var->int_counter)); 839bce29ac9SDaniel Bristot de Oliveira 840bce29ac9SDaniel Bristot de Oliveira return int_counter; 841bce29ac9SDaniel Bristot de Oliveira } 842bce29ac9SDaniel Bristot de Oliveira 843a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 844a955d7eaSDaniel Bristot de Oliveira /* 845a955d7eaSDaniel Bristot de Oliveira * copy_int_safe_time - Copy *src into *desc aware of interference 846a955d7eaSDaniel Bristot de Oliveira */ 847a955d7eaSDaniel Bristot de Oliveira static u64 848a955d7eaSDaniel Bristot de Oliveira copy_int_safe_time(struct osnoise_variables *osn_var, u64 *dst, u64 *src) 849a955d7eaSDaniel Bristot de Oliveira { 850a955d7eaSDaniel Bristot de Oliveira u64 int_counter; 851a955d7eaSDaniel Bristot de Oliveira 852a955d7eaSDaniel Bristot de Oliveira do { 853a955d7eaSDaniel Bristot de Oliveira int_counter = local_read(&osn_var->int_counter); 854a955d7eaSDaniel Bristot de Oliveira /* synchronize with interrupts */ 855a955d7eaSDaniel Bristot de Oliveira barrier(); 856a955d7eaSDaniel Bristot de Oliveira 857a955d7eaSDaniel Bristot de Oliveira *dst = *src; 858a955d7eaSDaniel Bristot de Oliveira 859a955d7eaSDaniel Bristot de Oliveira /* synchronize with interrupts */ 860a955d7eaSDaniel Bristot de Oliveira barrier(); 861a955d7eaSDaniel Bristot de Oliveira } while (int_counter != local_read(&osn_var->int_counter)); 862a955d7eaSDaniel Bristot de Oliveira 863a955d7eaSDaniel Bristot de Oliveira return int_counter; 864a955d7eaSDaniel Bristot de Oliveira } 865a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */ 866a955d7eaSDaniel Bristot de Oliveira 867bce29ac9SDaniel Bristot de Oliveira /* 868bce29ac9SDaniel Bristot de Oliveira * trace_osnoise_callback - NMI entry/exit callback 869bce29ac9SDaniel Bristot de Oliveira * 870bce29ac9SDaniel Bristot de Oliveira * This function is called at the entry and exit NMI code. The bool enter 871bce29ac9SDaniel Bristot de Oliveira * distinguishes between either case. This function is used to note a NMI 872bce29ac9SDaniel Bristot de Oliveira * occurrence, compute the noise caused by the NMI, and to remove the noise 873bce29ac9SDaniel Bristot de Oliveira * it is potentially causing on other interference variables. 874bce29ac9SDaniel Bristot de Oliveira */ 875bce29ac9SDaniel Bristot de Oliveira void trace_osnoise_callback(bool enter) 876bce29ac9SDaniel Bristot de Oliveira { 877bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var(); 878bce29ac9SDaniel Bristot de Oliveira u64 duration; 879bce29ac9SDaniel Bristot de Oliveira 880bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling) 881bce29ac9SDaniel Bristot de Oliveira return; 882bce29ac9SDaniel Bristot de Oliveira 883bce29ac9SDaniel Bristot de Oliveira /* 884bce29ac9SDaniel Bristot de Oliveira * Currently trace_clock_local() calls sched_clock() and the 885bce29ac9SDaniel Bristot de Oliveira * generic version is not NMI safe. 886bce29ac9SDaniel Bristot de Oliveira */ 887bce29ac9SDaniel Bristot de Oliveira if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) { 888bce29ac9SDaniel Bristot de Oliveira if (enter) { 889bce29ac9SDaniel Bristot de Oliveira osn_var->nmi.delta_start = time_get(); 890bce29ac9SDaniel Bristot de Oliveira local_inc(&osn_var->int_counter); 891bce29ac9SDaniel Bristot de Oliveira } else { 892bce29ac9SDaniel Bristot de Oliveira duration = time_get() - osn_var->nmi.delta_start; 893bce29ac9SDaniel Bristot de Oliveira 894bce29ac9SDaniel Bristot de Oliveira trace_nmi_noise(osn_var->nmi.delta_start, duration); 895bce29ac9SDaniel Bristot de Oliveira 896bce29ac9SDaniel Bristot de Oliveira cond_move_irq_delta_start(osn_var, duration); 897bce29ac9SDaniel Bristot de Oliveira cond_move_softirq_delta_start(osn_var, duration); 898bce29ac9SDaniel Bristot de Oliveira cond_move_thread_delta_start(osn_var, duration); 899bce29ac9SDaniel Bristot de Oliveira } 900bce29ac9SDaniel Bristot de Oliveira } 901bce29ac9SDaniel Bristot de Oliveira 902bce29ac9SDaniel Bristot de Oliveira if (enter) 903bce29ac9SDaniel Bristot de Oliveira osn_var->nmi.count++; 904bce29ac9SDaniel Bristot de Oliveira } 905bce29ac9SDaniel Bristot de Oliveira 906bce29ac9SDaniel Bristot de Oliveira /* 907bce29ac9SDaniel Bristot de Oliveira * osnoise_trace_irq_entry - Note the starting of an IRQ 908bce29ac9SDaniel Bristot de Oliveira * 909bce29ac9SDaniel Bristot de Oliveira * Save the starting time of an IRQ. As IRQs are non-preemptive to other IRQs, 910bce29ac9SDaniel Bristot de Oliveira * it is safe to use a single variable (ons_var->irq) to save the statistics. 911bce29ac9SDaniel Bristot de Oliveira * The arrival_time is used to report... the arrival time. The delta_start 912bce29ac9SDaniel Bristot de Oliveira * is used to compute the duration at the IRQ exit handler. See 913bce29ac9SDaniel Bristot de Oliveira * cond_move_irq_delta_start(). 914bce29ac9SDaniel Bristot de Oliveira */ 915bce29ac9SDaniel Bristot de Oliveira void osnoise_trace_irq_entry(int id) 916bce29ac9SDaniel Bristot de Oliveira { 917bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var(); 918bce29ac9SDaniel Bristot de Oliveira 919bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling) 920bce29ac9SDaniel Bristot de Oliveira return; 921bce29ac9SDaniel Bristot de Oliveira /* 922bce29ac9SDaniel Bristot de Oliveira * This value will be used in the report, but not to compute 923bce29ac9SDaniel Bristot de Oliveira * the execution time, so it is safe to get it unsafe. 924bce29ac9SDaniel Bristot de Oliveira */ 925bce29ac9SDaniel Bristot de Oliveira osn_var->irq.arrival_time = time_get(); 926bce29ac9SDaniel Bristot de Oliveira set_int_safe_time(osn_var, &osn_var->irq.delta_start); 927bce29ac9SDaniel Bristot de Oliveira osn_var->irq.count++; 928bce29ac9SDaniel Bristot de Oliveira 929bce29ac9SDaniel Bristot de Oliveira local_inc(&osn_var->int_counter); 930bce29ac9SDaniel Bristot de Oliveira } 931bce29ac9SDaniel Bristot de Oliveira 932bce29ac9SDaniel Bristot de Oliveira /* 933bce29ac9SDaniel Bristot de Oliveira * osnoise_irq_exit - Note the end of an IRQ, sava data and trace 934bce29ac9SDaniel Bristot de Oliveira * 935bce29ac9SDaniel Bristot de Oliveira * Computes the duration of the IRQ noise, and trace it. Also discounts the 936bce29ac9SDaniel Bristot de Oliveira * interference from other sources of noise could be currently being accounted. 937bce29ac9SDaniel Bristot de Oliveira */ 938bce29ac9SDaniel Bristot de Oliveira void osnoise_trace_irq_exit(int id, const char *desc) 939bce29ac9SDaniel Bristot de Oliveira { 940bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var(); 941022632f6SDaniel Bristot de Oliveira s64 duration; 942bce29ac9SDaniel Bristot de Oliveira 943bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling) 944bce29ac9SDaniel Bristot de Oliveira return; 945bce29ac9SDaniel Bristot de Oliveira 946bce29ac9SDaniel Bristot de Oliveira duration = get_int_safe_duration(osn_var, &osn_var->irq.delta_start); 947bce29ac9SDaniel Bristot de Oliveira trace_irq_noise(id, desc, osn_var->irq.arrival_time, duration); 948bce29ac9SDaniel Bristot de Oliveira osn_var->irq.arrival_time = 0; 949bce29ac9SDaniel Bristot de Oliveira cond_move_softirq_delta_start(osn_var, duration); 950bce29ac9SDaniel Bristot de Oliveira cond_move_thread_delta_start(osn_var, duration); 951bce29ac9SDaniel Bristot de Oliveira } 952bce29ac9SDaniel Bristot de Oliveira 953bce29ac9SDaniel Bristot de Oliveira /* 954bce29ac9SDaniel Bristot de Oliveira * trace_irqentry_callback - Callback to the irq:irq_entry traceevent 955bce29ac9SDaniel Bristot de Oliveira * 956bce29ac9SDaniel Bristot de Oliveira * Used to note the starting of an IRQ occurece. 957bce29ac9SDaniel Bristot de Oliveira */ 958bce29ac9SDaniel Bristot de Oliveira static void trace_irqentry_callback(void *data, int irq, 959bce29ac9SDaniel Bristot de Oliveira struct irqaction *action) 960bce29ac9SDaniel Bristot de Oliveira { 961bce29ac9SDaniel Bristot de Oliveira osnoise_trace_irq_entry(irq); 962bce29ac9SDaniel Bristot de Oliveira } 963bce29ac9SDaniel Bristot de Oliveira 964bce29ac9SDaniel Bristot de Oliveira /* 965bce29ac9SDaniel Bristot de Oliveira * trace_irqexit_callback - Callback to the irq:irq_exit traceevent 966bce29ac9SDaniel Bristot de Oliveira * 967bce29ac9SDaniel Bristot de Oliveira * Used to note the end of an IRQ occurece. 968bce29ac9SDaniel Bristot de Oliveira */ 969bce29ac9SDaniel Bristot de Oliveira static void trace_irqexit_callback(void *data, int irq, 970bce29ac9SDaniel Bristot de Oliveira struct irqaction *action, int ret) 971bce29ac9SDaniel Bristot de Oliveira { 972bce29ac9SDaniel Bristot de Oliveira osnoise_trace_irq_exit(irq, action->name); 973bce29ac9SDaniel Bristot de Oliveira } 974bce29ac9SDaniel Bristot de Oliveira 975bce29ac9SDaniel Bristot de Oliveira /* 976bce29ac9SDaniel Bristot de Oliveira * arch specific register function. 977bce29ac9SDaniel Bristot de Oliveira */ 978bce29ac9SDaniel Bristot de Oliveira int __weak osnoise_arch_register(void) 979bce29ac9SDaniel Bristot de Oliveira { 980bce29ac9SDaniel Bristot de Oliveira return 0; 981bce29ac9SDaniel Bristot de Oliveira } 982bce29ac9SDaniel Bristot de Oliveira 983bce29ac9SDaniel Bristot de Oliveira /* 984bce29ac9SDaniel Bristot de Oliveira * arch specific unregister function. 985bce29ac9SDaniel Bristot de Oliveira */ 986bce29ac9SDaniel Bristot de Oliveira void __weak osnoise_arch_unregister(void) 987bce29ac9SDaniel Bristot de Oliveira { 988bce29ac9SDaniel Bristot de Oliveira return; 989bce29ac9SDaniel Bristot de Oliveira } 990bce29ac9SDaniel Bristot de Oliveira 991bce29ac9SDaniel Bristot de Oliveira /* 992bce29ac9SDaniel Bristot de Oliveira * hook_irq_events - Hook IRQ handling events 993bce29ac9SDaniel Bristot de Oliveira * 994bce29ac9SDaniel Bristot de Oliveira * This function hooks the IRQ related callbacks to the respective trace 995bce29ac9SDaniel Bristot de Oliveira * events. 996bce29ac9SDaniel Bristot de Oliveira */ 997f7d9f637SDaniel Bristot de Oliveira static int hook_irq_events(void) 998bce29ac9SDaniel Bristot de Oliveira { 999bce29ac9SDaniel Bristot de Oliveira int ret; 1000bce29ac9SDaniel Bristot de Oliveira 1001bce29ac9SDaniel Bristot de Oliveira ret = register_trace_irq_handler_entry(trace_irqentry_callback, NULL); 1002bce29ac9SDaniel Bristot de Oliveira if (ret) 1003bce29ac9SDaniel Bristot de Oliveira goto out_err; 1004bce29ac9SDaniel Bristot de Oliveira 1005bce29ac9SDaniel Bristot de Oliveira ret = register_trace_irq_handler_exit(trace_irqexit_callback, NULL); 1006bce29ac9SDaniel Bristot de Oliveira if (ret) 1007bce29ac9SDaniel Bristot de Oliveira goto out_unregister_entry; 1008bce29ac9SDaniel Bristot de Oliveira 1009bce29ac9SDaniel Bristot de Oliveira ret = osnoise_arch_register(); 1010bce29ac9SDaniel Bristot de Oliveira if (ret) 1011bce29ac9SDaniel Bristot de Oliveira goto out_irq_exit; 1012bce29ac9SDaniel Bristot de Oliveira 1013bce29ac9SDaniel Bristot de Oliveira return 0; 1014bce29ac9SDaniel Bristot de Oliveira 1015bce29ac9SDaniel Bristot de Oliveira out_irq_exit: 1016bce29ac9SDaniel Bristot de Oliveira unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL); 1017bce29ac9SDaniel Bristot de Oliveira out_unregister_entry: 1018bce29ac9SDaniel Bristot de Oliveira unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL); 1019bce29ac9SDaniel Bristot de Oliveira out_err: 1020bce29ac9SDaniel Bristot de Oliveira return -EINVAL; 1021bce29ac9SDaniel Bristot de Oliveira } 1022bce29ac9SDaniel Bristot de Oliveira 1023bce29ac9SDaniel Bristot de Oliveira /* 1024bce29ac9SDaniel Bristot de Oliveira * unhook_irq_events - Unhook IRQ handling events 1025bce29ac9SDaniel Bristot de Oliveira * 1026bce29ac9SDaniel Bristot de Oliveira * This function unhooks the IRQ related callbacks to the respective trace 1027bce29ac9SDaniel Bristot de Oliveira * events. 1028bce29ac9SDaniel Bristot de Oliveira */ 1029f7d9f637SDaniel Bristot de Oliveira static void unhook_irq_events(void) 1030bce29ac9SDaniel Bristot de Oliveira { 1031bce29ac9SDaniel Bristot de Oliveira osnoise_arch_unregister(); 1032bce29ac9SDaniel Bristot de Oliveira unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL); 1033bce29ac9SDaniel Bristot de Oliveira unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL); 1034bce29ac9SDaniel Bristot de Oliveira } 1035bce29ac9SDaniel Bristot de Oliveira 1036bce29ac9SDaniel Bristot de Oliveira #ifndef CONFIG_PREEMPT_RT 1037bce29ac9SDaniel Bristot de Oliveira /* 1038bce29ac9SDaniel Bristot de Oliveira * trace_softirq_entry_callback - Note the starting of a softirq 1039bce29ac9SDaniel Bristot de Oliveira * 1040bce29ac9SDaniel Bristot de Oliveira * Save the starting time of a softirq. As softirqs are non-preemptive to 1041bce29ac9SDaniel Bristot de Oliveira * other softirqs, it is safe to use a single variable (ons_var->softirq) 1042bce29ac9SDaniel Bristot de Oliveira * to save the statistics. The arrival_time is used to report... the 1043bce29ac9SDaniel Bristot de Oliveira * arrival time. The delta_start is used to compute the duration at the 1044bce29ac9SDaniel Bristot de Oliveira * softirq exit handler. See cond_move_softirq_delta_start(). 1045bce29ac9SDaniel Bristot de Oliveira */ 1046f7d9f637SDaniel Bristot de Oliveira static void trace_softirq_entry_callback(void *data, unsigned int vec_nr) 1047bce29ac9SDaniel Bristot de Oliveira { 1048bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var(); 1049bce29ac9SDaniel Bristot de Oliveira 1050bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling) 1051bce29ac9SDaniel Bristot de Oliveira return; 1052bce29ac9SDaniel Bristot de Oliveira /* 1053bce29ac9SDaniel Bristot de Oliveira * This value will be used in the report, but not to compute 1054bce29ac9SDaniel Bristot de Oliveira * the execution time, so it is safe to get it unsafe. 1055bce29ac9SDaniel Bristot de Oliveira */ 1056bce29ac9SDaniel Bristot de Oliveira osn_var->softirq.arrival_time = time_get(); 1057bce29ac9SDaniel Bristot de Oliveira set_int_safe_time(osn_var, &osn_var->softirq.delta_start); 1058bce29ac9SDaniel Bristot de Oliveira osn_var->softirq.count++; 1059bce29ac9SDaniel Bristot de Oliveira 1060bce29ac9SDaniel Bristot de Oliveira local_inc(&osn_var->int_counter); 1061bce29ac9SDaniel Bristot de Oliveira } 1062bce29ac9SDaniel Bristot de Oliveira 1063bce29ac9SDaniel Bristot de Oliveira /* 1064bce29ac9SDaniel Bristot de Oliveira * trace_softirq_exit_callback - Note the end of an softirq 1065bce29ac9SDaniel Bristot de Oliveira * 1066bce29ac9SDaniel Bristot de Oliveira * Computes the duration of the softirq noise, and trace it. Also discounts the 1067bce29ac9SDaniel Bristot de Oliveira * interference from other sources of noise could be currently being accounted. 1068bce29ac9SDaniel Bristot de Oliveira */ 1069f7d9f637SDaniel Bristot de Oliveira static void trace_softirq_exit_callback(void *data, unsigned int vec_nr) 1070bce29ac9SDaniel Bristot de Oliveira { 1071bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var(); 1072022632f6SDaniel Bristot de Oliveira s64 duration; 1073bce29ac9SDaniel Bristot de Oliveira 1074bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling) 1075bce29ac9SDaniel Bristot de Oliveira return; 1076bce29ac9SDaniel Bristot de Oliveira 1077ccb67544SDaniel Bristot de Oliveira if (unlikely(timerlat_enabled())) 1078ccb67544SDaniel Bristot de Oliveira if (!timerlat_softirq_exit(osn_var)) 1079a955d7eaSDaniel Bristot de Oliveira return; 1080a955d7eaSDaniel Bristot de Oliveira 1081bce29ac9SDaniel Bristot de Oliveira duration = get_int_safe_duration(osn_var, &osn_var->softirq.delta_start); 1082bce29ac9SDaniel Bristot de Oliveira trace_softirq_noise(vec_nr, osn_var->softirq.arrival_time, duration); 1083bce29ac9SDaniel Bristot de Oliveira cond_move_thread_delta_start(osn_var, duration); 1084bce29ac9SDaniel Bristot de Oliveira osn_var->softirq.arrival_time = 0; 1085bce29ac9SDaniel Bristot de Oliveira } 1086bce29ac9SDaniel Bristot de Oliveira 1087bce29ac9SDaniel Bristot de Oliveira /* 1088bce29ac9SDaniel Bristot de Oliveira * hook_softirq_events - Hook softirq handling events 1089bce29ac9SDaniel Bristot de Oliveira * 1090bce29ac9SDaniel Bristot de Oliveira * This function hooks the softirq related callbacks to the respective trace 1091bce29ac9SDaniel Bristot de Oliveira * events. 1092bce29ac9SDaniel Bristot de Oliveira */ 1093bce29ac9SDaniel Bristot de Oliveira static int hook_softirq_events(void) 1094bce29ac9SDaniel Bristot de Oliveira { 1095bce29ac9SDaniel Bristot de Oliveira int ret; 1096bce29ac9SDaniel Bristot de Oliveira 1097bce29ac9SDaniel Bristot de Oliveira ret = register_trace_softirq_entry(trace_softirq_entry_callback, NULL); 1098bce29ac9SDaniel Bristot de Oliveira if (ret) 1099bce29ac9SDaniel Bristot de Oliveira goto out_err; 1100bce29ac9SDaniel Bristot de Oliveira 1101bce29ac9SDaniel Bristot de Oliveira ret = register_trace_softirq_exit(trace_softirq_exit_callback, NULL); 1102bce29ac9SDaniel Bristot de Oliveira if (ret) 1103bce29ac9SDaniel Bristot de Oliveira goto out_unreg_entry; 1104bce29ac9SDaniel Bristot de Oliveira 1105bce29ac9SDaniel Bristot de Oliveira return 0; 1106bce29ac9SDaniel Bristot de Oliveira 1107bce29ac9SDaniel Bristot de Oliveira out_unreg_entry: 1108bce29ac9SDaniel Bristot de Oliveira unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL); 1109bce29ac9SDaniel Bristot de Oliveira out_err: 1110bce29ac9SDaniel Bristot de Oliveira return -EINVAL; 1111bce29ac9SDaniel Bristot de Oliveira } 1112bce29ac9SDaniel Bristot de Oliveira 1113bce29ac9SDaniel Bristot de Oliveira /* 1114bce29ac9SDaniel Bristot de Oliveira * unhook_softirq_events - Unhook softirq handling events 1115bce29ac9SDaniel Bristot de Oliveira * 1116bce29ac9SDaniel Bristot de Oliveira * This function hooks the softirq related callbacks to the respective trace 1117bce29ac9SDaniel Bristot de Oliveira * events. 1118bce29ac9SDaniel Bristot de Oliveira */ 1119bce29ac9SDaniel Bristot de Oliveira static void unhook_softirq_events(void) 1120bce29ac9SDaniel Bristot de Oliveira { 1121bce29ac9SDaniel Bristot de Oliveira unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL); 1122bce29ac9SDaniel Bristot de Oliveira unregister_trace_softirq_exit(trace_softirq_exit_callback, NULL); 1123bce29ac9SDaniel Bristot de Oliveira } 1124bce29ac9SDaniel Bristot de Oliveira #else /* CONFIG_PREEMPT_RT */ 1125bce29ac9SDaniel Bristot de Oliveira /* 1126bce29ac9SDaniel Bristot de Oliveira * softirq are threads on the PREEMPT_RT mode. 1127bce29ac9SDaniel Bristot de Oliveira */ 1128bce29ac9SDaniel Bristot de Oliveira static int hook_softirq_events(void) 1129bce29ac9SDaniel Bristot de Oliveira { 1130bce29ac9SDaniel Bristot de Oliveira return 0; 1131bce29ac9SDaniel Bristot de Oliveira } 1132bce29ac9SDaniel Bristot de Oliveira static void unhook_softirq_events(void) 1133bce29ac9SDaniel Bristot de Oliveira { 1134bce29ac9SDaniel Bristot de Oliveira } 1135bce29ac9SDaniel Bristot de Oliveira #endif 1136bce29ac9SDaniel Bristot de Oliveira 1137bce29ac9SDaniel Bristot de Oliveira /* 1138bce29ac9SDaniel Bristot de Oliveira * thread_entry - Record the starting of a thread noise window 1139bce29ac9SDaniel Bristot de Oliveira * 1140bce29ac9SDaniel Bristot de Oliveira * It saves the context switch time for a noisy thread, and increments 1141bce29ac9SDaniel Bristot de Oliveira * the interference counters. 1142bce29ac9SDaniel Bristot de Oliveira */ 1143bce29ac9SDaniel Bristot de Oliveira static void 1144bce29ac9SDaniel Bristot de Oliveira thread_entry(struct osnoise_variables *osn_var, struct task_struct *t) 1145bce29ac9SDaniel Bristot de Oliveira { 1146bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling) 1147bce29ac9SDaniel Bristot de Oliveira return; 1148bce29ac9SDaniel Bristot de Oliveira /* 1149bce29ac9SDaniel Bristot de Oliveira * The arrival time will be used in the report, but not to compute 1150bce29ac9SDaniel Bristot de Oliveira * the execution time, so it is safe to get it unsafe. 1151bce29ac9SDaniel Bristot de Oliveira */ 1152bce29ac9SDaniel Bristot de Oliveira osn_var->thread.arrival_time = time_get(); 1153bce29ac9SDaniel Bristot de Oliveira 1154bce29ac9SDaniel Bristot de Oliveira set_int_safe_time(osn_var, &osn_var->thread.delta_start); 1155bce29ac9SDaniel Bristot de Oliveira 1156bce29ac9SDaniel Bristot de Oliveira osn_var->thread.count++; 1157bce29ac9SDaniel Bristot de Oliveira local_inc(&osn_var->int_counter); 1158bce29ac9SDaniel Bristot de Oliveira } 1159bce29ac9SDaniel Bristot de Oliveira 1160bce29ac9SDaniel Bristot de Oliveira /* 1161bce29ac9SDaniel Bristot de Oliveira * thread_exit - Report the end of a thread noise window 1162bce29ac9SDaniel Bristot de Oliveira * 1163bce29ac9SDaniel Bristot de Oliveira * It computes the total noise from a thread, tracing if needed. 1164bce29ac9SDaniel Bristot de Oliveira */ 1165bce29ac9SDaniel Bristot de Oliveira static void 1166bce29ac9SDaniel Bristot de Oliveira thread_exit(struct osnoise_variables *osn_var, struct task_struct *t) 1167bce29ac9SDaniel Bristot de Oliveira { 1168022632f6SDaniel Bristot de Oliveira s64 duration; 1169bce29ac9SDaniel Bristot de Oliveira 1170bce29ac9SDaniel Bristot de Oliveira if (!osn_var->sampling) 1171bce29ac9SDaniel Bristot de Oliveira return; 1172bce29ac9SDaniel Bristot de Oliveira 1173ccb67544SDaniel Bristot de Oliveira if (unlikely(timerlat_enabled())) 1174ccb67544SDaniel Bristot de Oliveira if (!timerlat_thread_exit(osn_var)) 1175a955d7eaSDaniel Bristot de Oliveira return; 1176a955d7eaSDaniel Bristot de Oliveira 1177bce29ac9SDaniel Bristot de Oliveira duration = get_int_safe_duration(osn_var, &osn_var->thread.delta_start); 1178bce29ac9SDaniel Bristot de Oliveira 1179bce29ac9SDaniel Bristot de Oliveira trace_thread_noise(t, osn_var->thread.arrival_time, duration); 1180bce29ac9SDaniel Bristot de Oliveira 1181bce29ac9SDaniel Bristot de Oliveira osn_var->thread.arrival_time = 0; 1182bce29ac9SDaniel Bristot de Oliveira } 1183bce29ac9SDaniel Bristot de Oliveira 1184bce29ac9SDaniel Bristot de Oliveira /* 1185bce29ac9SDaniel Bristot de Oliveira * trace_sched_switch - sched:sched_switch trace event handler 1186bce29ac9SDaniel Bristot de Oliveira * 1187bce29ac9SDaniel Bristot de Oliveira * This function is hooked to the sched:sched_switch trace event, and it is 1188bce29ac9SDaniel Bristot de Oliveira * used to record the beginning and to report the end of a thread noise window. 1189bce29ac9SDaniel Bristot de Oliveira */ 1190f7d9f637SDaniel Bristot de Oliveira static void 1191fa2c3254SValentin Schneider trace_sched_switch_callback(void *data, bool preempt, 1192fa2c3254SValentin Schneider struct task_struct *p, 11939c2136beSDelyan Kratunov struct task_struct *n, 11949c2136beSDelyan Kratunov unsigned int prev_state) 1195bce29ac9SDaniel Bristot de Oliveira { 1196bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var(); 119730838fcdSDaniel Bristot de Oliveira int workload = test_bit(OSN_WORKLOAD, &osnoise_options); 1198bce29ac9SDaniel Bristot de Oliveira 119930838fcdSDaniel Bristot de Oliveira if ((p->pid != osn_var->pid) || !workload) 1200bce29ac9SDaniel Bristot de Oliveira thread_exit(osn_var, p); 1201bce29ac9SDaniel Bristot de Oliveira 120230838fcdSDaniel Bristot de Oliveira if ((n->pid != osn_var->pid) || !workload) 1203bce29ac9SDaniel Bristot de Oliveira thread_entry(osn_var, n); 1204bce29ac9SDaniel Bristot de Oliveira } 1205bce29ac9SDaniel Bristot de Oliveira 1206bce29ac9SDaniel Bristot de Oliveira /* 1207bce29ac9SDaniel Bristot de Oliveira * hook_thread_events - Hook the insturmentation for thread noise 1208bce29ac9SDaniel Bristot de Oliveira * 1209bce29ac9SDaniel Bristot de Oliveira * Hook the osnoise tracer callbacks to handle the noise from other 1210bce29ac9SDaniel Bristot de Oliveira * threads on the necessary kernel events. 1211bce29ac9SDaniel Bristot de Oliveira */ 1212f7d9f637SDaniel Bristot de Oliveira static int hook_thread_events(void) 1213bce29ac9SDaniel Bristot de Oliveira { 1214bce29ac9SDaniel Bristot de Oliveira int ret; 1215bce29ac9SDaniel Bristot de Oliveira 1216bce29ac9SDaniel Bristot de Oliveira ret = register_trace_sched_switch(trace_sched_switch_callback, NULL); 1217bce29ac9SDaniel Bristot de Oliveira if (ret) 1218bce29ac9SDaniel Bristot de Oliveira return -EINVAL; 1219bce29ac9SDaniel Bristot de Oliveira 1220bce29ac9SDaniel Bristot de Oliveira return 0; 1221bce29ac9SDaniel Bristot de Oliveira } 1222bce29ac9SDaniel Bristot de Oliveira 1223bce29ac9SDaniel Bristot de Oliveira /* 1224bce29ac9SDaniel Bristot de Oliveira * unhook_thread_events - *nhook the insturmentation for thread noise 1225bce29ac9SDaniel Bristot de Oliveira * 1226bce29ac9SDaniel Bristot de Oliveira * Unook the osnoise tracer callbacks to handle the noise from other 1227bce29ac9SDaniel Bristot de Oliveira * threads on the necessary kernel events. 1228bce29ac9SDaniel Bristot de Oliveira */ 1229f7d9f637SDaniel Bristot de Oliveira static void unhook_thread_events(void) 1230bce29ac9SDaniel Bristot de Oliveira { 1231bce29ac9SDaniel Bristot de Oliveira unregister_trace_sched_switch(trace_sched_switch_callback, NULL); 1232bce29ac9SDaniel Bristot de Oliveira } 1233bce29ac9SDaniel Bristot de Oliveira 1234bce29ac9SDaniel Bristot de Oliveira /* 1235bce29ac9SDaniel Bristot de Oliveira * save_osn_sample_stats - Save the osnoise_sample statistics 1236bce29ac9SDaniel Bristot de Oliveira * 1237bce29ac9SDaniel Bristot de Oliveira * Save the osnoise_sample statistics before the sampling phase. These 1238bce29ac9SDaniel Bristot de Oliveira * values will be used later to compute the diff betwneen the statistics 1239bce29ac9SDaniel Bristot de Oliveira * before and after the osnoise sampling. 1240bce29ac9SDaniel Bristot de Oliveira */ 1241f7d9f637SDaniel Bristot de Oliveira static void 1242f7d9f637SDaniel Bristot de Oliveira save_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s) 1243bce29ac9SDaniel Bristot de Oliveira { 1244bce29ac9SDaniel Bristot de Oliveira s->nmi_count = osn_var->nmi.count; 1245bce29ac9SDaniel Bristot de Oliveira s->irq_count = osn_var->irq.count; 1246bce29ac9SDaniel Bristot de Oliveira s->softirq_count = osn_var->softirq.count; 1247bce29ac9SDaniel Bristot de Oliveira s->thread_count = osn_var->thread.count; 1248bce29ac9SDaniel Bristot de Oliveira } 1249bce29ac9SDaniel Bristot de Oliveira 1250bce29ac9SDaniel Bristot de Oliveira /* 1251bce29ac9SDaniel Bristot de Oliveira * diff_osn_sample_stats - Compute the osnoise_sample statistics 1252bce29ac9SDaniel Bristot de Oliveira * 1253bce29ac9SDaniel Bristot de Oliveira * After a sample period, compute the difference on the osnoise_sample 1254bce29ac9SDaniel Bristot de Oliveira * statistics. The struct osnoise_sample *s contains the statistics saved via 1255bce29ac9SDaniel Bristot de Oliveira * save_osn_sample_stats() before the osnoise sampling. 1256bce29ac9SDaniel Bristot de Oliveira */ 1257f7d9f637SDaniel Bristot de Oliveira static void 1258f7d9f637SDaniel Bristot de Oliveira diff_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s) 1259bce29ac9SDaniel Bristot de Oliveira { 1260bce29ac9SDaniel Bristot de Oliveira s->nmi_count = osn_var->nmi.count - s->nmi_count; 1261bce29ac9SDaniel Bristot de Oliveira s->irq_count = osn_var->irq.count - s->irq_count; 1262bce29ac9SDaniel Bristot de Oliveira s->softirq_count = osn_var->softirq.count - s->softirq_count; 1263bce29ac9SDaniel Bristot de Oliveira s->thread_count = osn_var->thread.count - s->thread_count; 1264bce29ac9SDaniel Bristot de Oliveira } 1265bce29ac9SDaniel Bristot de Oliveira 1266bce29ac9SDaniel Bristot de Oliveira /* 1267bce29ac9SDaniel Bristot de Oliveira * osnoise_stop_tracing - Stop tracing and the tracer. 1268bce29ac9SDaniel Bristot de Oliveira */ 12690e05ba49SDaniel Bristot de Oliveira static __always_inline void osnoise_stop_tracing(void) 1270bce29ac9SDaniel Bristot de Oliveira { 1271dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst; 1272dae18134SDaniel Bristot de Oliveira struct trace_array *tr; 12730e05ba49SDaniel Bristot de Oliveira 1274dae18134SDaniel Bristot de Oliveira rcu_read_lock(); 1275dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) { 1276dae18134SDaniel Bristot de Oliveira tr = inst->tr; 12770e05ba49SDaniel Bristot de Oliveira trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_, 12780e05ba49SDaniel Bristot de Oliveira "stop tracing hit on cpu %d\n", smp_processor_id()); 12790e05ba49SDaniel Bristot de Oliveira 12801603dda4SDaniel Bristot de Oliveira if (test_bit(OSN_PANIC_ON_STOP, &osnoise_options)) 12811603dda4SDaniel Bristot de Oliveira panic("tracer hit stop condition on CPU %d\n", smp_processor_id()); 12821603dda4SDaniel Bristot de Oliveira 1283bce29ac9SDaniel Bristot de Oliveira tracer_tracing_off(tr); 1284bce29ac9SDaniel Bristot de Oliveira } 1285dae18134SDaniel Bristot de Oliveira rcu_read_unlock(); 1286dae18134SDaniel Bristot de Oliveira } 1287dae18134SDaniel Bristot de Oliveira 1288dae18134SDaniel Bristot de Oliveira /* 1289dae18134SDaniel Bristot de Oliveira * notify_new_max_latency - Notify a new max latency via fsnotify interface. 1290dae18134SDaniel Bristot de Oliveira */ 1291dae18134SDaniel Bristot de Oliveira static void notify_new_max_latency(u64 latency) 1292dae18134SDaniel Bristot de Oliveira { 1293dae18134SDaniel Bristot de Oliveira struct osnoise_instance *inst; 1294dae18134SDaniel Bristot de Oliveira struct trace_array *tr; 1295dae18134SDaniel Bristot de Oliveira 1296dae18134SDaniel Bristot de Oliveira rcu_read_lock(); 1297dae18134SDaniel Bristot de Oliveira list_for_each_entry_rcu(inst, &osnoise_instances, list) { 1298dae18134SDaniel Bristot de Oliveira tr = inst->tr; 1299d3cba7f0SDaniel Bristot de Oliveira if (tracer_tracing_is_on(tr) && tr->max_latency < latency) { 1300dae18134SDaniel Bristot de Oliveira tr->max_latency = latency; 1301dae18134SDaniel Bristot de Oliveira latency_fsnotify(tr); 1302dae18134SDaniel Bristot de Oliveira } 1303dae18134SDaniel Bristot de Oliveira } 1304dae18134SDaniel Bristot de Oliveira rcu_read_unlock(); 1305dae18134SDaniel Bristot de Oliveira } 1306bce29ac9SDaniel Bristot de Oliveira 1307bce29ac9SDaniel Bristot de Oliveira /* 1308bce29ac9SDaniel Bristot de Oliveira * run_osnoise - Sample the time and look for osnoise 1309bce29ac9SDaniel Bristot de Oliveira * 1310bce29ac9SDaniel Bristot de Oliveira * Used to capture the time, looking for potential osnoise latency repeatedly. 1311bce29ac9SDaniel Bristot de Oliveira * Different from hwlat_detector, it is called with preemption and interrupts 1312bce29ac9SDaniel Bristot de Oliveira * enabled. This allows irqs, softirqs and threads to run, interfering on the 1313bce29ac9SDaniel Bristot de Oliveira * osnoise sampling thread, as they would do with a regular thread. 1314bce29ac9SDaniel Bristot de Oliveira */ 1315bce29ac9SDaniel Bristot de Oliveira static int run_osnoise(void) 1316bce29ac9SDaniel Bristot de Oliveira { 1317b5dce200SDaniel Bristot de Oliveira bool disable_irq = test_bit(OSN_IRQ_DISABLE, &osnoise_options); 1318bce29ac9SDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var(); 1319bce29ac9SDaniel Bristot de Oliveira u64 start, sample, last_sample; 1320bce29ac9SDaniel Bristot de Oliveira u64 last_int_count, int_count; 132119c3eaa7SDaniel Bristot de Oliveira s64 noise = 0, max_noise = 0; 1322bce29ac9SDaniel Bristot de Oliveira s64 total, last_total = 0; 1323bce29ac9SDaniel Bristot de Oliveira struct osnoise_sample s; 1324b5dce200SDaniel Bristot de Oliveira bool disable_preemption; 1325bce29ac9SDaniel Bristot de Oliveira unsigned int threshold; 1326bce29ac9SDaniel Bristot de Oliveira u64 runtime, stop_in; 132719c3eaa7SDaniel Bristot de Oliveira u64 sum_noise = 0; 132819c3eaa7SDaniel Bristot de Oliveira int hw_count = 0; 1329bce29ac9SDaniel Bristot de Oliveira int ret = -1; 1330bce29ac9SDaniel Bristot de Oliveira 1331bce29ac9SDaniel Bristot de Oliveira /* 1332b5dce200SDaniel Bristot de Oliveira * Disabling preemption is only required if IRQs are enabled, 1333b5dce200SDaniel Bristot de Oliveira * and the options is set on. 1334b5dce200SDaniel Bristot de Oliveira */ 1335b5dce200SDaniel Bristot de Oliveira disable_preemption = !disable_irq && test_bit(OSN_PREEMPT_DISABLE, &osnoise_options); 1336b5dce200SDaniel Bristot de Oliveira 1337b5dce200SDaniel Bristot de Oliveira /* 1338bce29ac9SDaniel Bristot de Oliveira * Considers the current thread as the workload. 1339bce29ac9SDaniel Bristot de Oliveira */ 1340bce29ac9SDaniel Bristot de Oliveira osn_var->pid = current->pid; 1341bce29ac9SDaniel Bristot de Oliveira 1342bce29ac9SDaniel Bristot de Oliveira /* 1343bce29ac9SDaniel Bristot de Oliveira * Save the current stats for the diff 1344bce29ac9SDaniel Bristot de Oliveira */ 1345bce29ac9SDaniel Bristot de Oliveira save_osn_sample_stats(osn_var, &s); 1346bce29ac9SDaniel Bristot de Oliveira 1347bce29ac9SDaniel Bristot de Oliveira /* 1348bce29ac9SDaniel Bristot de Oliveira * if threshold is 0, use the default value of 5 us. 1349bce29ac9SDaniel Bristot de Oliveira */ 1350bce29ac9SDaniel Bristot de Oliveira threshold = tracing_thresh ? : 5000; 1351bce29ac9SDaniel Bristot de Oliveira 1352bce29ac9SDaniel Bristot de Oliveira /* 1353b5dce200SDaniel Bristot de Oliveira * Apply PREEMPT and IRQ disabled options. 1354b5dce200SDaniel Bristot de Oliveira */ 1355b5dce200SDaniel Bristot de Oliveira if (disable_irq) 1356b5dce200SDaniel Bristot de Oliveira local_irq_disable(); 1357b5dce200SDaniel Bristot de Oliveira 1358b5dce200SDaniel Bristot de Oliveira if (disable_preemption) 1359b5dce200SDaniel Bristot de Oliveira preempt_disable(); 1360b5dce200SDaniel Bristot de Oliveira 1361b5dce200SDaniel Bristot de Oliveira /* 1362bce29ac9SDaniel Bristot de Oliveira * Make sure NMIs see sampling first 1363bce29ac9SDaniel Bristot de Oliveira */ 1364bce29ac9SDaniel Bristot de Oliveira osn_var->sampling = true; 1365bce29ac9SDaniel Bristot de Oliveira barrier(); 1366bce29ac9SDaniel Bristot de Oliveira 1367bce29ac9SDaniel Bristot de Oliveira /* 1368bce29ac9SDaniel Bristot de Oliveira * Transform the *_us config to nanoseconds to avoid the 1369bce29ac9SDaniel Bristot de Oliveira * division on the main loop. 1370bce29ac9SDaniel Bristot de Oliveira */ 1371bce29ac9SDaniel Bristot de Oliveira runtime = osnoise_data.sample_runtime * NSEC_PER_USEC; 1372bce29ac9SDaniel Bristot de Oliveira stop_in = osnoise_data.stop_tracing * NSEC_PER_USEC; 1373bce29ac9SDaniel Bristot de Oliveira 1374bce29ac9SDaniel Bristot de Oliveira /* 1375bce29ac9SDaniel Bristot de Oliveira * Start timestemp 1376bce29ac9SDaniel Bristot de Oliveira */ 1377bce29ac9SDaniel Bristot de Oliveira start = time_get(); 1378bce29ac9SDaniel Bristot de Oliveira 1379bce29ac9SDaniel Bristot de Oliveira /* 1380bce29ac9SDaniel Bristot de Oliveira * "previous" loop. 1381bce29ac9SDaniel Bristot de Oliveira */ 1382bce29ac9SDaniel Bristot de Oliveira last_int_count = set_int_safe_time(osn_var, &last_sample); 1383bce29ac9SDaniel Bristot de Oliveira 1384bce29ac9SDaniel Bristot de Oliveira do { 1385bce29ac9SDaniel Bristot de Oliveira /* 1386bce29ac9SDaniel Bristot de Oliveira * Get sample! 1387bce29ac9SDaniel Bristot de Oliveira */ 1388bce29ac9SDaniel Bristot de Oliveira int_count = set_int_safe_time(osn_var, &sample); 1389bce29ac9SDaniel Bristot de Oliveira 1390bce29ac9SDaniel Bristot de Oliveira noise = time_sub(sample, last_sample); 1391bce29ac9SDaniel Bristot de Oliveira 1392bce29ac9SDaniel Bristot de Oliveira /* 1393bce29ac9SDaniel Bristot de Oliveira * This shouldn't happen. 1394bce29ac9SDaniel Bristot de Oliveira */ 1395bce29ac9SDaniel Bristot de Oliveira if (noise < 0) { 1396bce29ac9SDaniel Bristot de Oliveira osnoise_taint("negative noise!"); 1397bce29ac9SDaniel Bristot de Oliveira goto out; 1398bce29ac9SDaniel Bristot de Oliveira } 1399bce29ac9SDaniel Bristot de Oliveira 1400bce29ac9SDaniel Bristot de Oliveira /* 1401bce29ac9SDaniel Bristot de Oliveira * Sample runtime. 1402bce29ac9SDaniel Bristot de Oliveira */ 1403bce29ac9SDaniel Bristot de Oliveira total = time_sub(sample, start); 1404bce29ac9SDaniel Bristot de Oliveira 1405bce29ac9SDaniel Bristot de Oliveira /* 1406bce29ac9SDaniel Bristot de Oliveira * Check for possible overflows. 1407bce29ac9SDaniel Bristot de Oliveira */ 1408bce29ac9SDaniel Bristot de Oliveira if (total < last_total) { 1409bce29ac9SDaniel Bristot de Oliveira osnoise_taint("total overflow!"); 1410bce29ac9SDaniel Bristot de Oliveira break; 1411bce29ac9SDaniel Bristot de Oliveira } 1412bce29ac9SDaniel Bristot de Oliveira 1413bce29ac9SDaniel Bristot de Oliveira last_total = total; 1414bce29ac9SDaniel Bristot de Oliveira 1415bce29ac9SDaniel Bristot de Oliveira if (noise >= threshold) { 1416bce29ac9SDaniel Bristot de Oliveira int interference = int_count - last_int_count; 1417bce29ac9SDaniel Bristot de Oliveira 1418bce29ac9SDaniel Bristot de Oliveira if (noise > max_noise) 1419bce29ac9SDaniel Bristot de Oliveira max_noise = noise; 1420bce29ac9SDaniel Bristot de Oliveira 1421bce29ac9SDaniel Bristot de Oliveira if (!interference) 1422bce29ac9SDaniel Bristot de Oliveira hw_count++; 1423bce29ac9SDaniel Bristot de Oliveira 1424bce29ac9SDaniel Bristot de Oliveira sum_noise += noise; 1425bce29ac9SDaniel Bristot de Oliveira 1426bce29ac9SDaniel Bristot de Oliveira trace_sample_threshold(last_sample, noise, interference); 1427bce29ac9SDaniel Bristot de Oliveira 1428bce29ac9SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing) 1429bce29ac9SDaniel Bristot de Oliveira if (noise > stop_in) 1430bce29ac9SDaniel Bristot de Oliveira osnoise_stop_tracing(); 1431bce29ac9SDaniel Bristot de Oliveira } 1432bce29ac9SDaniel Bristot de Oliveira 1433bce29ac9SDaniel Bristot de Oliveira /* 1434caf4c86bSNicolas Saenz Julienne * In some cases, notably when running on a nohz_full CPU with 1435caf4c86bSNicolas Saenz Julienne * a stopped tick PREEMPT_RCU has no way to account for QSs. 1436caf4c86bSNicolas Saenz Julienne * This will eventually cause unwarranted noise as PREEMPT_RCU 1437caf4c86bSNicolas Saenz Julienne * will force preemption as the means of ending the current 1438caf4c86bSNicolas Saenz Julienne * grace period. We avoid this problem by calling 1439caf4c86bSNicolas Saenz Julienne * rcu_momentary_dyntick_idle(), which performs a zero duration 1440caf4c86bSNicolas Saenz Julienne * EQS allowing PREEMPT_RCU to end the current grace period. 1441caf4c86bSNicolas Saenz Julienne * This call shouldn't be wrapped inside an RCU critical 1442caf4c86bSNicolas Saenz Julienne * section. 1443caf4c86bSNicolas Saenz Julienne * 1444caf4c86bSNicolas Saenz Julienne * Note that in non PREEMPT_RCU kernels QSs are handled through 1445caf4c86bSNicolas Saenz Julienne * cond_resched() 1446caf4c86bSNicolas Saenz Julienne */ 1447caf4c86bSNicolas Saenz Julienne if (IS_ENABLED(CONFIG_PREEMPT_RCU)) { 1448b5dce200SDaniel Bristot de Oliveira if (!disable_irq) 1449caf4c86bSNicolas Saenz Julienne local_irq_disable(); 1450b5dce200SDaniel Bristot de Oliveira 1451caf4c86bSNicolas Saenz Julienne rcu_momentary_dyntick_idle(); 1452b5dce200SDaniel Bristot de Oliveira 1453b5dce200SDaniel Bristot de Oliveira if (!disable_irq) 1454caf4c86bSNicolas Saenz Julienne local_irq_enable(); 1455caf4c86bSNicolas Saenz Julienne } 1456caf4c86bSNicolas Saenz Julienne 1457caf4c86bSNicolas Saenz Julienne /* 1458bce29ac9SDaniel Bristot de Oliveira * For the non-preemptive kernel config: let threads runs, if 1459b5dce200SDaniel Bristot de Oliveira * they so wish, unless set not do to so. 1460bce29ac9SDaniel Bristot de Oliveira */ 1461b5dce200SDaniel Bristot de Oliveira if (!disable_irq && !disable_preemption) 1462bce29ac9SDaniel Bristot de Oliveira cond_resched(); 1463bce29ac9SDaniel Bristot de Oliveira 1464bce29ac9SDaniel Bristot de Oliveira last_sample = sample; 1465bce29ac9SDaniel Bristot de Oliveira last_int_count = int_count; 1466bce29ac9SDaniel Bristot de Oliveira 1467bce29ac9SDaniel Bristot de Oliveira } while (total < runtime && !kthread_should_stop()); 1468bce29ac9SDaniel Bristot de Oliveira 1469bce29ac9SDaniel Bristot de Oliveira /* 1470bce29ac9SDaniel Bristot de Oliveira * Finish the above in the view for interrupts. 1471bce29ac9SDaniel Bristot de Oliveira */ 1472bce29ac9SDaniel Bristot de Oliveira barrier(); 1473bce29ac9SDaniel Bristot de Oliveira 1474bce29ac9SDaniel Bristot de Oliveira osn_var->sampling = false; 1475bce29ac9SDaniel Bristot de Oliveira 1476bce29ac9SDaniel Bristot de Oliveira /* 1477bce29ac9SDaniel Bristot de Oliveira * Make sure sampling data is no longer updated. 1478bce29ac9SDaniel Bristot de Oliveira */ 1479bce29ac9SDaniel Bristot de Oliveira barrier(); 1480bce29ac9SDaniel Bristot de Oliveira 1481bce29ac9SDaniel Bristot de Oliveira /* 1482b5dce200SDaniel Bristot de Oliveira * Return to the preemptive state. 1483b5dce200SDaniel Bristot de Oliveira */ 1484b5dce200SDaniel Bristot de Oliveira if (disable_preemption) 1485b5dce200SDaniel Bristot de Oliveira preempt_enable(); 1486b5dce200SDaniel Bristot de Oliveira 1487b5dce200SDaniel Bristot de Oliveira if (disable_irq) 1488b5dce200SDaniel Bristot de Oliveira local_irq_enable(); 1489b5dce200SDaniel Bristot de Oliveira 1490b5dce200SDaniel Bristot de Oliveira /* 1491bce29ac9SDaniel Bristot de Oliveira * Save noise info. 1492bce29ac9SDaniel Bristot de Oliveira */ 1493bce29ac9SDaniel Bristot de Oliveira s.noise = time_to_us(sum_noise); 1494bce29ac9SDaniel Bristot de Oliveira s.runtime = time_to_us(total); 1495bce29ac9SDaniel Bristot de Oliveira s.max_sample = time_to_us(max_noise); 1496bce29ac9SDaniel Bristot de Oliveira s.hw_count = hw_count; 1497bce29ac9SDaniel Bristot de Oliveira 1498bce29ac9SDaniel Bristot de Oliveira /* Save interference stats info */ 1499bce29ac9SDaniel Bristot de Oliveira diff_osn_sample_stats(osn_var, &s); 1500bce29ac9SDaniel Bristot de Oliveira 1501bce29ac9SDaniel Bristot de Oliveira trace_osnoise_sample(&s); 1502bce29ac9SDaniel Bristot de Oliveira 1503dae18134SDaniel Bristot de Oliveira notify_new_max_latency(max_noise); 1504bce29ac9SDaniel Bristot de Oliveira 1505bce29ac9SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing_total) 1506bce29ac9SDaniel Bristot de Oliveira if (s.noise > osnoise_data.stop_tracing_total) 1507bce29ac9SDaniel Bristot de Oliveira osnoise_stop_tracing(); 1508bce29ac9SDaniel Bristot de Oliveira 1509bce29ac9SDaniel Bristot de Oliveira return 0; 1510bce29ac9SDaniel Bristot de Oliveira out: 1511bce29ac9SDaniel Bristot de Oliveira return ret; 1512bce29ac9SDaniel Bristot de Oliveira } 1513bce29ac9SDaniel Bristot de Oliveira 1514bce29ac9SDaniel Bristot de Oliveira static struct cpumask osnoise_cpumask; 1515bce29ac9SDaniel Bristot de Oliveira static struct cpumask save_cpumask; 1516bce29ac9SDaniel Bristot de Oliveira 1517bce29ac9SDaniel Bristot de Oliveira /* 1518dd990352SDaniel Bristot de Oliveira * osnoise_sleep - sleep until the next period 1519dd990352SDaniel Bristot de Oliveira */ 1520dd990352SDaniel Bristot de Oliveira static void osnoise_sleep(void) 1521dd990352SDaniel Bristot de Oliveira { 1522dd990352SDaniel Bristot de Oliveira u64 interval; 1523dd990352SDaniel Bristot de Oliveira ktime_t wake_time; 1524dd990352SDaniel Bristot de Oliveira 1525dd990352SDaniel Bristot de Oliveira mutex_lock(&interface_lock); 1526dd990352SDaniel Bristot de Oliveira interval = osnoise_data.sample_period - osnoise_data.sample_runtime; 1527dd990352SDaniel Bristot de Oliveira mutex_unlock(&interface_lock); 1528dd990352SDaniel Bristot de Oliveira 1529dd990352SDaniel Bristot de Oliveira /* 1530dd990352SDaniel Bristot de Oliveira * differently from hwlat_detector, the osnoise tracer can run 1531dd990352SDaniel Bristot de Oliveira * without a pause because preemption is on. 1532dd990352SDaniel Bristot de Oliveira */ 1533dd990352SDaniel Bristot de Oliveira if (!interval) { 1534dd990352SDaniel Bristot de Oliveira /* Let synchronize_rcu_tasks() make progress */ 1535dd990352SDaniel Bristot de Oliveira cond_resched_tasks_rcu_qs(); 1536dd990352SDaniel Bristot de Oliveira return; 1537dd990352SDaniel Bristot de Oliveira } 1538dd990352SDaniel Bristot de Oliveira 1539dd990352SDaniel Bristot de Oliveira wake_time = ktime_add_us(ktime_get(), interval); 1540dd990352SDaniel Bristot de Oliveira __set_current_state(TASK_INTERRUPTIBLE); 1541dd990352SDaniel Bristot de Oliveira 1542b18c58afSDavidlohr Bueso while (schedule_hrtimeout(&wake_time, HRTIMER_MODE_ABS)) { 1543dd990352SDaniel Bristot de Oliveira if (kthread_should_stop()) 1544dd990352SDaniel Bristot de Oliveira break; 1545dd990352SDaniel Bristot de Oliveira } 1546dd990352SDaniel Bristot de Oliveira } 1547dd990352SDaniel Bristot de Oliveira 1548dd990352SDaniel Bristot de Oliveira /* 1549*4998e7fdSDaniel Bristot de Oliveira * osnoise_migration_pending - checks if the task needs to migrate 1550*4998e7fdSDaniel Bristot de Oliveira * 1551*4998e7fdSDaniel Bristot de Oliveira * osnoise/timerlat threads are per-cpu. If there is a pending request to 1552*4998e7fdSDaniel Bristot de Oliveira * migrate the thread away from the current CPU, something bad has happened. 1553*4998e7fdSDaniel Bristot de Oliveira * Play the good citizen and leave. 1554*4998e7fdSDaniel Bristot de Oliveira * 1555*4998e7fdSDaniel Bristot de Oliveira * Returns 0 if it is safe to continue, 1 otherwise. 1556*4998e7fdSDaniel Bristot de Oliveira */ 1557*4998e7fdSDaniel Bristot de Oliveira static inline int osnoise_migration_pending(void) 1558*4998e7fdSDaniel Bristot de Oliveira { 1559*4998e7fdSDaniel Bristot de Oliveira if (!current->migration_pending) 1560*4998e7fdSDaniel Bristot de Oliveira return 0; 1561*4998e7fdSDaniel Bristot de Oliveira 1562*4998e7fdSDaniel Bristot de Oliveira /* 1563*4998e7fdSDaniel Bristot de Oliveira * If migration is pending, there is a task waiting for the 1564*4998e7fdSDaniel Bristot de Oliveira * tracer to enable migration. The tracer does not allow migration, 1565*4998e7fdSDaniel Bristot de Oliveira * thus: taint and leave to unblock the blocked thread. 1566*4998e7fdSDaniel Bristot de Oliveira */ 1567*4998e7fdSDaniel Bristot de Oliveira osnoise_taint("migration requested to osnoise threads, leaving."); 1568*4998e7fdSDaniel Bristot de Oliveira 1569*4998e7fdSDaniel Bristot de Oliveira /* 1570*4998e7fdSDaniel Bristot de Oliveira * Unset this thread from the threads managed by the interface. 1571*4998e7fdSDaniel Bristot de Oliveira * The tracers are responsible for cleaning their env before 1572*4998e7fdSDaniel Bristot de Oliveira * exiting. 1573*4998e7fdSDaniel Bristot de Oliveira */ 1574*4998e7fdSDaniel Bristot de Oliveira mutex_lock(&interface_lock); 1575*4998e7fdSDaniel Bristot de Oliveira this_cpu_osn_var()->kthread = NULL; 1576*4998e7fdSDaniel Bristot de Oliveira mutex_unlock(&interface_lock); 1577*4998e7fdSDaniel Bristot de Oliveira 1578*4998e7fdSDaniel Bristot de Oliveira return 1; 1579*4998e7fdSDaniel Bristot de Oliveira } 1580*4998e7fdSDaniel Bristot de Oliveira 1581*4998e7fdSDaniel Bristot de Oliveira /* 1582bce29ac9SDaniel Bristot de Oliveira * osnoise_main - The osnoise detection kernel thread 1583bce29ac9SDaniel Bristot de Oliveira * 1584bce29ac9SDaniel Bristot de Oliveira * Calls run_osnoise() function to measure the osnoise for the configured runtime, 1585bce29ac9SDaniel Bristot de Oliveira * every period. 1586bce29ac9SDaniel Bristot de Oliveira */ 1587bce29ac9SDaniel Bristot de Oliveira static int osnoise_main(void *data) 1588bce29ac9SDaniel Bristot de Oliveira { 1589*4998e7fdSDaniel Bristot de Oliveira unsigned long flags; 1590*4998e7fdSDaniel Bristot de Oliveira 1591*4998e7fdSDaniel Bristot de Oliveira /* 1592*4998e7fdSDaniel Bristot de Oliveira * This thread was created pinned to the CPU using PF_NO_SETAFFINITY. 1593*4998e7fdSDaniel Bristot de Oliveira * The problem is that cgroup does not allow PF_NO_SETAFFINITY thread. 1594*4998e7fdSDaniel Bristot de Oliveira * 1595*4998e7fdSDaniel Bristot de Oliveira * To work around this limitation, disable migration and remove the 1596*4998e7fdSDaniel Bristot de Oliveira * flag. 1597*4998e7fdSDaniel Bristot de Oliveira */ 1598*4998e7fdSDaniel Bristot de Oliveira migrate_disable(); 1599*4998e7fdSDaniel Bristot de Oliveira raw_spin_lock_irqsave(¤t->pi_lock, flags); 1600*4998e7fdSDaniel Bristot de Oliveira current->flags &= ~(PF_NO_SETAFFINITY); 1601*4998e7fdSDaniel Bristot de Oliveira raw_spin_unlock_irqrestore(¤t->pi_lock, flags); 1602bce29ac9SDaniel Bristot de Oliveira 1603bce29ac9SDaniel Bristot de Oliveira while (!kthread_should_stop()) { 1604*4998e7fdSDaniel Bristot de Oliveira if (osnoise_migration_pending()) 1605*4998e7fdSDaniel Bristot de Oliveira break; 1606*4998e7fdSDaniel Bristot de Oliveira 1607bce29ac9SDaniel Bristot de Oliveira run_osnoise(); 1608dd990352SDaniel Bristot de Oliveira osnoise_sleep(); 1609bce29ac9SDaniel Bristot de Oliveira } 1610bce29ac9SDaniel Bristot de Oliveira 1611*4998e7fdSDaniel Bristot de Oliveira migrate_enable(); 1612bce29ac9SDaniel Bristot de Oliveira return 0; 1613bce29ac9SDaniel Bristot de Oliveira } 1614bce29ac9SDaniel Bristot de Oliveira 1615a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 1616a955d7eaSDaniel Bristot de Oliveira /* 1617a955d7eaSDaniel Bristot de Oliveira * timerlat_irq - hrtimer handler for timerlat. 1618a955d7eaSDaniel Bristot de Oliveira */ 1619a955d7eaSDaniel Bristot de Oliveira static enum hrtimer_restart timerlat_irq(struct hrtimer *timer) 1620a955d7eaSDaniel Bristot de Oliveira { 1621a955d7eaSDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var(); 1622a955d7eaSDaniel Bristot de Oliveira struct timerlat_variables *tlat; 1623a955d7eaSDaniel Bristot de Oliveira struct timerlat_sample s; 1624a955d7eaSDaniel Bristot de Oliveira u64 now; 1625a955d7eaSDaniel Bristot de Oliveira u64 diff; 1626a955d7eaSDaniel Bristot de Oliveira 1627a955d7eaSDaniel Bristot de Oliveira /* 1628a955d7eaSDaniel Bristot de Oliveira * I am not sure if the timer was armed for this CPU. So, get 1629a955d7eaSDaniel Bristot de Oliveira * the timerlat struct from the timer itself, not from this 1630a955d7eaSDaniel Bristot de Oliveira * CPU. 1631a955d7eaSDaniel Bristot de Oliveira */ 1632a955d7eaSDaniel Bristot de Oliveira tlat = container_of(timer, struct timerlat_variables, timer); 1633a955d7eaSDaniel Bristot de Oliveira 1634a955d7eaSDaniel Bristot de Oliveira now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer)); 1635a955d7eaSDaniel Bristot de Oliveira 1636a955d7eaSDaniel Bristot de Oliveira /* 1637a955d7eaSDaniel Bristot de Oliveira * Enable the osnoise: events for thread an softirq. 1638a955d7eaSDaniel Bristot de Oliveira */ 1639a955d7eaSDaniel Bristot de Oliveira tlat->tracing_thread = true; 1640a955d7eaSDaniel Bristot de Oliveira 1641a955d7eaSDaniel Bristot de Oliveira osn_var->thread.arrival_time = time_get(); 1642a955d7eaSDaniel Bristot de Oliveira 1643a955d7eaSDaniel Bristot de Oliveira /* 1644a955d7eaSDaniel Bristot de Oliveira * A hardirq is running: the timer IRQ. It is for sure preempting 1645a955d7eaSDaniel Bristot de Oliveira * a thread, and potentially preempting a softirq. 1646a955d7eaSDaniel Bristot de Oliveira * 1647a955d7eaSDaniel Bristot de Oliveira * At this point, it is not interesting to know the duration of the 1648a955d7eaSDaniel Bristot de Oliveira * preempted thread (and maybe softirq), but how much time they will 1649a955d7eaSDaniel Bristot de Oliveira * delay the beginning of the execution of the timer thread. 1650a955d7eaSDaniel Bristot de Oliveira * 1651a955d7eaSDaniel Bristot de Oliveira * To get the correct (net) delay added by the softirq, its delta_start 1652a955d7eaSDaniel Bristot de Oliveira * is set as the IRQ one. In this way, at the return of the IRQ, the delta 1653a955d7eaSDaniel Bristot de Oliveira * start of the sofitrq will be zeroed, accounting then only the time 1654a955d7eaSDaniel Bristot de Oliveira * after that. 1655a955d7eaSDaniel Bristot de Oliveira * 1656a955d7eaSDaniel Bristot de Oliveira * The thread follows the same principle. However, if a softirq is 1657a955d7eaSDaniel Bristot de Oliveira * running, the thread needs to receive the softirq delta_start. The 1658a955d7eaSDaniel Bristot de Oliveira * reason being is that the softirq will be the last to be unfolded, 1659a955d7eaSDaniel Bristot de Oliveira * resseting the thread delay to zero. 166001e181c7SDaniel Bristot de Oliveira * 166101e181c7SDaniel Bristot de Oliveira * The PREEMPT_RT is a special case, though. As softirqs run as threads 166201e181c7SDaniel Bristot de Oliveira * on RT, moving the thread is enough. 1663a955d7eaSDaniel Bristot de Oliveira */ 166401e181c7SDaniel Bristot de Oliveira if (!IS_ENABLED(CONFIG_PREEMPT_RT) && osn_var->softirq.delta_start) { 1665a955d7eaSDaniel Bristot de Oliveira copy_int_safe_time(osn_var, &osn_var->thread.delta_start, 1666a955d7eaSDaniel Bristot de Oliveira &osn_var->softirq.delta_start); 1667a955d7eaSDaniel Bristot de Oliveira 1668a955d7eaSDaniel Bristot de Oliveira copy_int_safe_time(osn_var, &osn_var->softirq.delta_start, 1669a955d7eaSDaniel Bristot de Oliveira &osn_var->irq.delta_start); 1670a955d7eaSDaniel Bristot de Oliveira } else { 1671a955d7eaSDaniel Bristot de Oliveira copy_int_safe_time(osn_var, &osn_var->thread.delta_start, 1672a955d7eaSDaniel Bristot de Oliveira &osn_var->irq.delta_start); 1673a955d7eaSDaniel Bristot de Oliveira } 1674a955d7eaSDaniel Bristot de Oliveira 1675a955d7eaSDaniel Bristot de Oliveira /* 1676a955d7eaSDaniel Bristot de Oliveira * Compute the current time with the expected time. 1677a955d7eaSDaniel Bristot de Oliveira */ 1678a955d7eaSDaniel Bristot de Oliveira diff = now - tlat->abs_period; 1679a955d7eaSDaniel Bristot de Oliveira 1680a955d7eaSDaniel Bristot de Oliveira tlat->count++; 1681a955d7eaSDaniel Bristot de Oliveira s.seqnum = tlat->count; 1682a955d7eaSDaniel Bristot de Oliveira s.timer_latency = diff; 1683a955d7eaSDaniel Bristot de Oliveira s.context = IRQ_CONTEXT; 1684a955d7eaSDaniel Bristot de Oliveira 1685a955d7eaSDaniel Bristot de Oliveira trace_timerlat_sample(&s); 1686a955d7eaSDaniel Bristot de Oliveira 1687aa748949SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing) { 1688aa748949SDaniel Bristot de Oliveira if (time_to_us(diff) >= osnoise_data.stop_tracing) { 16894dd2aea2SDaniel Bristot de Oliveira 16904dd2aea2SDaniel Bristot de Oliveira /* 16914dd2aea2SDaniel Bristot de Oliveira * At this point, if stop_tracing is set and <= print_stack, 16924dd2aea2SDaniel Bristot de Oliveira * print_stack is set and would be printed in the thread handler. 16934dd2aea2SDaniel Bristot de Oliveira * 16944dd2aea2SDaniel Bristot de Oliveira * Thus, print the stack trace as it is helpful to define the 16954dd2aea2SDaniel Bristot de Oliveira * root cause of an IRQ latency. 16964dd2aea2SDaniel Bristot de Oliveira */ 16974dd2aea2SDaniel Bristot de Oliveira if (osnoise_data.stop_tracing <= osnoise_data.print_stack) { 16984dd2aea2SDaniel Bristot de Oliveira timerlat_save_stack(0); 16994dd2aea2SDaniel Bristot de Oliveira timerlat_dump_stack(time_to_us(diff)); 17004dd2aea2SDaniel Bristot de Oliveira } 17014dd2aea2SDaniel Bristot de Oliveira 1702a955d7eaSDaniel Bristot de Oliveira osnoise_stop_tracing(); 1703a955d7eaSDaniel Bristot de Oliveira notify_new_max_latency(diff); 1704a955d7eaSDaniel Bristot de Oliveira 1705632478a0SDaniel Bristot de Oliveira wake_up_process(tlat->kthread); 1706632478a0SDaniel Bristot de Oliveira 17079c556e5aSDaniel Bristot de Oliveira return HRTIMER_NORESTART; 1708aa748949SDaniel Bristot de Oliveira } 1709aa748949SDaniel Bristot de Oliveira } 1710a955d7eaSDaniel Bristot de Oliveira 1711a955d7eaSDaniel Bristot de Oliveira wake_up_process(tlat->kthread); 1712a955d7eaSDaniel Bristot de Oliveira 1713a955d7eaSDaniel Bristot de Oliveira if (osnoise_data.print_stack) 1714a955d7eaSDaniel Bristot de Oliveira timerlat_save_stack(0); 1715a955d7eaSDaniel Bristot de Oliveira 1716a955d7eaSDaniel Bristot de Oliveira return HRTIMER_NORESTART; 1717a955d7eaSDaniel Bristot de Oliveira } 1718a955d7eaSDaniel Bristot de Oliveira 1719a955d7eaSDaniel Bristot de Oliveira /* 1720a955d7eaSDaniel Bristot de Oliveira * wait_next_period - Wait for the next period for timerlat 1721a955d7eaSDaniel Bristot de Oliveira */ 1722a955d7eaSDaniel Bristot de Oliveira static int wait_next_period(struct timerlat_variables *tlat) 1723a955d7eaSDaniel Bristot de Oliveira { 1724a955d7eaSDaniel Bristot de Oliveira ktime_t next_abs_period, now; 1725a955d7eaSDaniel Bristot de Oliveira u64 rel_period = osnoise_data.timerlat_period * 1000; 1726a955d7eaSDaniel Bristot de Oliveira 1727a955d7eaSDaniel Bristot de Oliveira now = hrtimer_cb_get_time(&tlat->timer); 1728a955d7eaSDaniel Bristot de Oliveira next_abs_period = ns_to_ktime(tlat->abs_period + rel_period); 1729a955d7eaSDaniel Bristot de Oliveira 1730a955d7eaSDaniel Bristot de Oliveira /* 1731a955d7eaSDaniel Bristot de Oliveira * Save the next abs_period. 1732a955d7eaSDaniel Bristot de Oliveira */ 1733a955d7eaSDaniel Bristot de Oliveira tlat->abs_period = (u64) ktime_to_ns(next_abs_period); 1734a955d7eaSDaniel Bristot de Oliveira 1735a955d7eaSDaniel Bristot de Oliveira /* 1736a955d7eaSDaniel Bristot de Oliveira * If the new abs_period is in the past, skip the activation. 1737a955d7eaSDaniel Bristot de Oliveira */ 1738a955d7eaSDaniel Bristot de Oliveira while (ktime_compare(now, next_abs_period) > 0) { 1739a955d7eaSDaniel Bristot de Oliveira next_abs_period = ns_to_ktime(tlat->abs_period + rel_period); 1740a955d7eaSDaniel Bristot de Oliveira tlat->abs_period = (u64) ktime_to_ns(next_abs_period); 1741a955d7eaSDaniel Bristot de Oliveira } 1742a955d7eaSDaniel Bristot de Oliveira 1743a955d7eaSDaniel Bristot de Oliveira set_current_state(TASK_INTERRUPTIBLE); 1744a955d7eaSDaniel Bristot de Oliveira 1745a955d7eaSDaniel Bristot de Oliveira hrtimer_start(&tlat->timer, next_abs_period, HRTIMER_MODE_ABS_PINNED_HARD); 1746a955d7eaSDaniel Bristot de Oliveira schedule(); 1747a955d7eaSDaniel Bristot de Oliveira return 1; 1748a955d7eaSDaniel Bristot de Oliveira } 1749a955d7eaSDaniel Bristot de Oliveira 1750a955d7eaSDaniel Bristot de Oliveira /* 1751a955d7eaSDaniel Bristot de Oliveira * timerlat_main- Timerlat main 1752a955d7eaSDaniel Bristot de Oliveira */ 1753a955d7eaSDaniel Bristot de Oliveira static int timerlat_main(void *data) 1754a955d7eaSDaniel Bristot de Oliveira { 1755a955d7eaSDaniel Bristot de Oliveira struct osnoise_variables *osn_var = this_cpu_osn_var(); 1756a955d7eaSDaniel Bristot de Oliveira struct timerlat_variables *tlat = this_cpu_tmr_var(); 1757a955d7eaSDaniel Bristot de Oliveira struct timerlat_sample s; 1758a955d7eaSDaniel Bristot de Oliveira struct sched_param sp; 1759*4998e7fdSDaniel Bristot de Oliveira unsigned long flags; 1760a955d7eaSDaniel Bristot de Oliveira u64 now, diff; 1761a955d7eaSDaniel Bristot de Oliveira 1762a955d7eaSDaniel Bristot de Oliveira /* 1763a955d7eaSDaniel Bristot de Oliveira * Make the thread RT, that is how cyclictest is usually used. 1764a955d7eaSDaniel Bristot de Oliveira */ 1765a955d7eaSDaniel Bristot de Oliveira sp.sched_priority = DEFAULT_TIMERLAT_PRIO; 1766a955d7eaSDaniel Bristot de Oliveira sched_setscheduler_nocheck(current, SCHED_FIFO, &sp); 1767a955d7eaSDaniel Bristot de Oliveira 1768*4998e7fdSDaniel Bristot de Oliveira /* 1769*4998e7fdSDaniel Bristot de Oliveira * This thread was created pinned to the CPU using PF_NO_SETAFFINITY. 1770*4998e7fdSDaniel Bristot de Oliveira * The problem is that cgroup does not allow PF_NO_SETAFFINITY thread. 1771*4998e7fdSDaniel Bristot de Oliveira * 1772*4998e7fdSDaniel Bristot de Oliveira * To work around this limitation, disable migration and remove the 1773*4998e7fdSDaniel Bristot de Oliveira * flag. 1774*4998e7fdSDaniel Bristot de Oliveira */ 1775*4998e7fdSDaniel Bristot de Oliveira migrate_disable(); 1776*4998e7fdSDaniel Bristot de Oliveira raw_spin_lock_irqsave(¤t->pi_lock, flags); 1777*4998e7fdSDaniel Bristot de Oliveira current->flags &= ~(PF_NO_SETAFFINITY); 1778*4998e7fdSDaniel Bristot de Oliveira raw_spin_unlock_irqrestore(¤t->pi_lock, flags); 1779*4998e7fdSDaniel Bristot de Oliveira 1780a955d7eaSDaniel Bristot de Oliveira tlat->count = 0; 1781a955d7eaSDaniel Bristot de Oliveira tlat->tracing_thread = false; 1782a955d7eaSDaniel Bristot de Oliveira 1783a955d7eaSDaniel Bristot de Oliveira hrtimer_init(&tlat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD); 1784a955d7eaSDaniel Bristot de Oliveira tlat->timer.function = timerlat_irq; 1785a955d7eaSDaniel Bristot de Oliveira tlat->kthread = current; 1786a955d7eaSDaniel Bristot de Oliveira osn_var->pid = current->pid; 1787a955d7eaSDaniel Bristot de Oliveira /* 1788a955d7eaSDaniel Bristot de Oliveira * Anotate the arrival time. 1789a955d7eaSDaniel Bristot de Oliveira */ 1790a955d7eaSDaniel Bristot de Oliveira tlat->abs_period = hrtimer_cb_get_time(&tlat->timer); 1791a955d7eaSDaniel Bristot de Oliveira 1792a955d7eaSDaniel Bristot de Oliveira wait_next_period(tlat); 1793a955d7eaSDaniel Bristot de Oliveira 1794a955d7eaSDaniel Bristot de Oliveira osn_var->sampling = 1; 1795a955d7eaSDaniel Bristot de Oliveira 1796a955d7eaSDaniel Bristot de Oliveira while (!kthread_should_stop()) { 1797*4998e7fdSDaniel Bristot de Oliveira 1798a955d7eaSDaniel Bristot de Oliveira now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer)); 1799a955d7eaSDaniel Bristot de Oliveira diff = now - tlat->abs_period; 1800a955d7eaSDaniel Bristot de Oliveira 1801a955d7eaSDaniel Bristot de Oliveira s.seqnum = tlat->count; 1802a955d7eaSDaniel Bristot de Oliveira s.timer_latency = diff; 1803a955d7eaSDaniel Bristot de Oliveira s.context = THREAD_CONTEXT; 1804a955d7eaSDaniel Bristot de Oliveira 1805a955d7eaSDaniel Bristot de Oliveira trace_timerlat_sample(&s); 1806a955d7eaSDaniel Bristot de Oliveira 1807b9f451a9SDaniel Bristot de Oliveira notify_new_max_latency(diff); 1808b9f451a9SDaniel Bristot de Oliveira 1809b14f4568SDaniel Bristot de Oliveira timerlat_dump_stack(time_to_us(diff)); 1810a955d7eaSDaniel Bristot de Oliveira 1811a955d7eaSDaniel Bristot de Oliveira tlat->tracing_thread = false; 1812a955d7eaSDaniel Bristot de Oliveira if (osnoise_data.stop_tracing_total) 1813a955d7eaSDaniel Bristot de Oliveira if (time_to_us(diff) >= osnoise_data.stop_tracing_total) 1814a955d7eaSDaniel Bristot de Oliveira osnoise_stop_tracing(); 1815a955d7eaSDaniel Bristot de Oliveira 1816*4998e7fdSDaniel Bristot de Oliveira if (osnoise_migration_pending()) 1817*4998e7fdSDaniel Bristot de Oliveira break; 1818*4998e7fdSDaniel Bristot de Oliveira 1819a955d7eaSDaniel Bristot de Oliveira wait_next_period(tlat); 1820a955d7eaSDaniel Bristot de Oliveira } 1821a955d7eaSDaniel Bristot de Oliveira 1822a955d7eaSDaniel Bristot de Oliveira hrtimer_cancel(&tlat->timer); 1823*4998e7fdSDaniel Bristot de Oliveira migrate_enable(); 1824a955d7eaSDaniel Bristot de Oliveira return 0; 1825a955d7eaSDaniel Bristot de Oliveira } 1826ccb67544SDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */ 1827ccb67544SDaniel Bristot de Oliveira static int timerlat_main(void *data) 1828ccb67544SDaniel Bristot de Oliveira { 1829ccb67544SDaniel Bristot de Oliveira return 0; 1830ccb67544SDaniel Bristot de Oliveira } 1831a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */ 1832a955d7eaSDaniel Bristot de Oliveira 1833bce29ac9SDaniel Bristot de Oliveira /* 1834c8895e27SDaniel Bristot de Oliveira * stop_kthread - stop a workload thread 1835c8895e27SDaniel Bristot de Oliveira */ 1836c8895e27SDaniel Bristot de Oliveira static void stop_kthread(unsigned int cpu) 1837c8895e27SDaniel Bristot de Oliveira { 1838c8895e27SDaniel Bristot de Oliveira struct task_struct *kthread; 1839c8895e27SDaniel Bristot de Oliveira 1840c8895e27SDaniel Bristot de Oliveira kthread = per_cpu(per_cpu_osnoise_var, cpu).kthread; 184130838fcdSDaniel Bristot de Oliveira if (kthread) { 1842c8895e27SDaniel Bristot de Oliveira kthread_stop(kthread); 1843c8895e27SDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).kthread = NULL; 184430838fcdSDaniel Bristot de Oliveira } else { 184530838fcdSDaniel Bristot de Oliveira if (!test_bit(OSN_WORKLOAD, &osnoise_options)) { 184630838fcdSDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).sampling = false; 184730838fcdSDaniel Bristot de Oliveira barrier(); 184830838fcdSDaniel Bristot de Oliveira return; 184930838fcdSDaniel Bristot de Oliveira } 185030838fcdSDaniel Bristot de Oliveira } 1851c8895e27SDaniel Bristot de Oliveira } 1852c8895e27SDaniel Bristot de Oliveira 1853c8895e27SDaniel Bristot de Oliveira /* 1854c8895e27SDaniel Bristot de Oliveira * stop_per_cpu_kthread - Stop per-cpu threads 1855bce29ac9SDaniel Bristot de Oliveira * 1856bce29ac9SDaniel Bristot de Oliveira * Stop the osnoise sampling htread. Use this on unload and at system 1857bce29ac9SDaniel Bristot de Oliveira * shutdown. 1858bce29ac9SDaniel Bristot de Oliveira */ 1859bce29ac9SDaniel Bristot de Oliveira static void stop_per_cpu_kthreads(void) 1860bce29ac9SDaniel Bristot de Oliveira { 1861bce29ac9SDaniel Bristot de Oliveira int cpu; 1862bce29ac9SDaniel Bristot de Oliveira 186399c37d1aSSebastian Andrzej Siewior cpus_read_lock(); 1864bce29ac9SDaniel Bristot de Oliveira 1865bce29ac9SDaniel Bristot de Oliveira for_each_online_cpu(cpu) 1866c8895e27SDaniel Bristot de Oliveira stop_kthread(cpu); 1867bce29ac9SDaniel Bristot de Oliveira 186899c37d1aSSebastian Andrzej Siewior cpus_read_unlock(); 1869c8895e27SDaniel Bristot de Oliveira } 1870c8895e27SDaniel Bristot de Oliveira 1871c8895e27SDaniel Bristot de Oliveira /* 1872c8895e27SDaniel Bristot de Oliveira * start_kthread - Start a workload tread 1873c8895e27SDaniel Bristot de Oliveira */ 1874c8895e27SDaniel Bristot de Oliveira static int start_kthread(unsigned int cpu) 1875c8895e27SDaniel Bristot de Oliveira { 1876c8895e27SDaniel Bristot de Oliveira struct task_struct *kthread; 1877c8895e27SDaniel Bristot de Oliveira void *main = osnoise_main; 1878c8895e27SDaniel Bristot de Oliveira char comm[24]; 1879c8895e27SDaniel Bristot de Oliveira 1880ccb67544SDaniel Bristot de Oliveira if (timerlat_enabled()) { 1881a955d7eaSDaniel Bristot de Oliveira snprintf(comm, 24, "timerlat/%d", cpu); 1882a955d7eaSDaniel Bristot de Oliveira main = timerlat_main; 1883a955d7eaSDaniel Bristot de Oliveira } else { 188430838fcdSDaniel Bristot de Oliveira /* if no workload, just return */ 188530838fcdSDaniel Bristot de Oliveira if (!test_bit(OSN_WORKLOAD, &osnoise_options)) { 188630838fcdSDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).sampling = true; 188730838fcdSDaniel Bristot de Oliveira barrier(); 188830838fcdSDaniel Bristot de Oliveira return 0; 188930838fcdSDaniel Bristot de Oliveira } 189030838fcdSDaniel Bristot de Oliveira 1891bce29ac9SDaniel Bristot de Oliveira snprintf(comm, 24, "osnoise/%d", cpu); 1892a955d7eaSDaniel Bristot de Oliveira } 1893ccb67544SDaniel Bristot de Oliveira 189411e4e352SCai Huoqing kthread = kthread_run_on_cpu(main, NULL, cpu, comm); 1895bce29ac9SDaniel Bristot de Oliveira 1896bce29ac9SDaniel Bristot de Oliveira if (IS_ERR(kthread)) { 1897bce29ac9SDaniel Bristot de Oliveira pr_err(BANNER "could not start sampling thread\n"); 1898bce29ac9SDaniel Bristot de Oliveira stop_per_cpu_kthreads(); 1899bce29ac9SDaniel Bristot de Oliveira return -ENOMEM; 1900bce29ac9SDaniel Bristot de Oliveira } 1901bce29ac9SDaniel Bristot de Oliveira 1902bce29ac9SDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).kthread = kthread; 1903bce29ac9SDaniel Bristot de Oliveira 1904bce29ac9SDaniel Bristot de Oliveira return 0; 1905bce29ac9SDaniel Bristot de Oliveira } 1906bce29ac9SDaniel Bristot de Oliveira 1907bce29ac9SDaniel Bristot de Oliveira /* 1908c8895e27SDaniel Bristot de Oliveira * start_per_cpu_kthread - Kick off per-cpu osnoise sampling kthreads 1909c8895e27SDaniel Bristot de Oliveira * 1910c8895e27SDaniel Bristot de Oliveira * This starts the kernel thread that will look for osnoise on many 1911c8895e27SDaniel Bristot de Oliveira * cpus. 1912c8895e27SDaniel Bristot de Oliveira */ 191315ca4bdbSDaniel Bristot de Oliveira static int start_per_cpu_kthreads(void) 1914c8895e27SDaniel Bristot de Oliveira { 1915c8895e27SDaniel Bristot de Oliveira struct cpumask *current_mask = &save_cpumask; 19164b6b08f2SQiang.Zhang int retval = 0; 1917c8895e27SDaniel Bristot de Oliveira int cpu; 1918c8895e27SDaniel Bristot de Oliveira 191999c37d1aSSebastian Andrzej Siewior cpus_read_lock(); 1920c8895e27SDaniel Bristot de Oliveira /* 192166df27f1SDaniel Bristot de Oliveira * Run only on online CPUs in which osnoise is allowed to run. 1922c8895e27SDaniel Bristot de Oliveira */ 192366df27f1SDaniel Bristot de Oliveira cpumask_and(current_mask, cpu_online_mask, &osnoise_cpumask); 1924c8895e27SDaniel Bristot de Oliveira 1925c8895e27SDaniel Bristot de Oliveira for_each_possible_cpu(cpu) 1926c8895e27SDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).kthread = NULL; 1927c8895e27SDaniel Bristot de Oliveira 1928c8895e27SDaniel Bristot de Oliveira for_each_cpu(cpu, current_mask) { 1929c8895e27SDaniel Bristot de Oliveira retval = start_kthread(cpu); 1930c8895e27SDaniel Bristot de Oliveira if (retval) { 193199ee9317SNico Pache cpus_read_unlock(); 1932c8895e27SDaniel Bristot de Oliveira stop_per_cpu_kthreads(); 193399ee9317SNico Pache return retval; 1934c8895e27SDaniel Bristot de Oliveira } 1935c8895e27SDaniel Bristot de Oliveira } 1936c8895e27SDaniel Bristot de Oliveira 193799c37d1aSSebastian Andrzej Siewior cpus_read_unlock(); 1938c8895e27SDaniel Bristot de Oliveira 19394b6b08f2SQiang.Zhang return retval; 1940c8895e27SDaniel Bristot de Oliveira } 1941c8895e27SDaniel Bristot de Oliveira 1942c8895e27SDaniel Bristot de Oliveira #ifdef CONFIG_HOTPLUG_CPU 1943c8895e27SDaniel Bristot de Oliveira static void osnoise_hotplug_workfn(struct work_struct *dummy) 1944c8895e27SDaniel Bristot de Oliveira { 1945c8895e27SDaniel Bristot de Oliveira unsigned int cpu = smp_processor_id(); 1946c8895e27SDaniel Bristot de Oliveira 1947c8895e27SDaniel Bristot de Oliveira mutex_lock(&trace_types_lock); 1948c8895e27SDaniel Bristot de Oliveira 1949dae18134SDaniel Bristot de Oliveira if (!osnoise_has_registered_instances()) 1950c8895e27SDaniel Bristot de Oliveira goto out_unlock_trace; 1951c8895e27SDaniel Bristot de Oliveira 1952c8895e27SDaniel Bristot de Oliveira mutex_lock(&interface_lock); 195399c37d1aSSebastian Andrzej Siewior cpus_read_lock(); 1954c8895e27SDaniel Bristot de Oliveira 1955c8895e27SDaniel Bristot de Oliveira if (!cpumask_test_cpu(cpu, &osnoise_cpumask)) 1956c8895e27SDaniel Bristot de Oliveira goto out_unlock; 1957c8895e27SDaniel Bristot de Oliveira 1958c8895e27SDaniel Bristot de Oliveira start_kthread(cpu); 1959c8895e27SDaniel Bristot de Oliveira 1960c8895e27SDaniel Bristot de Oliveira out_unlock: 196199c37d1aSSebastian Andrzej Siewior cpus_read_unlock(); 1962c8895e27SDaniel Bristot de Oliveira mutex_unlock(&interface_lock); 1963c8895e27SDaniel Bristot de Oliveira out_unlock_trace: 1964c8895e27SDaniel Bristot de Oliveira mutex_unlock(&trace_types_lock); 1965c8895e27SDaniel Bristot de Oliveira } 1966c8895e27SDaniel Bristot de Oliveira 1967c8895e27SDaniel Bristot de Oliveira static DECLARE_WORK(osnoise_hotplug_work, osnoise_hotplug_workfn); 1968c8895e27SDaniel Bristot de Oliveira 1969c8895e27SDaniel Bristot de Oliveira /* 1970c8895e27SDaniel Bristot de Oliveira * osnoise_cpu_init - CPU hotplug online callback function 1971c8895e27SDaniel Bristot de Oliveira */ 1972c8895e27SDaniel Bristot de Oliveira static int osnoise_cpu_init(unsigned int cpu) 1973c8895e27SDaniel Bristot de Oliveira { 1974c8895e27SDaniel Bristot de Oliveira schedule_work_on(cpu, &osnoise_hotplug_work); 1975c8895e27SDaniel Bristot de Oliveira return 0; 1976c8895e27SDaniel Bristot de Oliveira } 1977c8895e27SDaniel Bristot de Oliveira 1978c8895e27SDaniel Bristot de Oliveira /* 1979c8895e27SDaniel Bristot de Oliveira * osnoise_cpu_die - CPU hotplug offline callback function 1980c8895e27SDaniel Bristot de Oliveira */ 1981c8895e27SDaniel Bristot de Oliveira static int osnoise_cpu_die(unsigned int cpu) 1982c8895e27SDaniel Bristot de Oliveira { 1983c8895e27SDaniel Bristot de Oliveira stop_kthread(cpu); 1984c8895e27SDaniel Bristot de Oliveira return 0; 1985c8895e27SDaniel Bristot de Oliveira } 1986c8895e27SDaniel Bristot de Oliveira 1987c8895e27SDaniel Bristot de Oliveira static void osnoise_init_hotplug_support(void) 1988c8895e27SDaniel Bristot de Oliveira { 1989c8895e27SDaniel Bristot de Oliveira int ret; 1990c8895e27SDaniel Bristot de Oliveira 1991c8895e27SDaniel Bristot de Oliveira ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "trace/osnoise:online", 1992c8895e27SDaniel Bristot de Oliveira osnoise_cpu_init, osnoise_cpu_die); 1993c8895e27SDaniel Bristot de Oliveira if (ret < 0) 1994c8895e27SDaniel Bristot de Oliveira pr_warn(BANNER "Error to init cpu hotplug support\n"); 1995c8895e27SDaniel Bristot de Oliveira 1996c8895e27SDaniel Bristot de Oliveira return; 1997c8895e27SDaniel Bristot de Oliveira } 1998c8895e27SDaniel Bristot de Oliveira #else /* CONFIG_HOTPLUG_CPU */ 1999c8895e27SDaniel Bristot de Oliveira static void osnoise_init_hotplug_support(void) 2000c8895e27SDaniel Bristot de Oliveira { 2001498627b4SDaniel Bristot de Oliveira return; 2002c8895e27SDaniel Bristot de Oliveira } 2003c8895e27SDaniel Bristot de Oliveira #endif /* CONFIG_HOTPLUG_CPU */ 2004c8895e27SDaniel Bristot de Oliveira 2005c8895e27SDaniel Bristot de Oliveira /* 2006b179d48bSDaniel Bristot de Oliveira * seq file functions for the osnoise/options file. 2007b179d48bSDaniel Bristot de Oliveira */ 2008b179d48bSDaniel Bristot de Oliveira static void *s_options_start(struct seq_file *s, loff_t *pos) 2009b179d48bSDaniel Bristot de Oliveira { 2010b179d48bSDaniel Bristot de Oliveira int option = *pos; 2011b179d48bSDaniel Bristot de Oliveira 2012b179d48bSDaniel Bristot de Oliveira mutex_lock(&interface_lock); 2013b179d48bSDaniel Bristot de Oliveira 2014b179d48bSDaniel Bristot de Oliveira if (option >= OSN_MAX) 2015b179d48bSDaniel Bristot de Oliveira return NULL; 2016b179d48bSDaniel Bristot de Oliveira 2017b179d48bSDaniel Bristot de Oliveira return pos; 2018b179d48bSDaniel Bristot de Oliveira } 2019b179d48bSDaniel Bristot de Oliveira 2020b179d48bSDaniel Bristot de Oliveira static void *s_options_next(struct seq_file *s, void *v, loff_t *pos) 2021b179d48bSDaniel Bristot de Oliveira { 2022b179d48bSDaniel Bristot de Oliveira int option = ++(*pos); 2023b179d48bSDaniel Bristot de Oliveira 2024b179d48bSDaniel Bristot de Oliveira if (option >= OSN_MAX) 2025b179d48bSDaniel Bristot de Oliveira return NULL; 2026b179d48bSDaniel Bristot de Oliveira 2027b179d48bSDaniel Bristot de Oliveira return pos; 2028b179d48bSDaniel Bristot de Oliveira } 2029b179d48bSDaniel Bristot de Oliveira 2030b179d48bSDaniel Bristot de Oliveira static int s_options_show(struct seq_file *s, void *v) 2031b179d48bSDaniel Bristot de Oliveira { 2032b179d48bSDaniel Bristot de Oliveira loff_t *pos = v; 2033b179d48bSDaniel Bristot de Oliveira int option = *pos; 2034b179d48bSDaniel Bristot de Oliveira 2035b179d48bSDaniel Bristot de Oliveira if (option == OSN_DEFAULTS) { 2036b179d48bSDaniel Bristot de Oliveira if (osnoise_options == OSN_DEFAULT_OPTIONS) 2037b179d48bSDaniel Bristot de Oliveira seq_printf(s, "%s", osnoise_options_str[option]); 2038b179d48bSDaniel Bristot de Oliveira else 2039b179d48bSDaniel Bristot de Oliveira seq_printf(s, "NO_%s", osnoise_options_str[option]); 2040b179d48bSDaniel Bristot de Oliveira goto out; 2041b179d48bSDaniel Bristot de Oliveira } 2042b179d48bSDaniel Bristot de Oliveira 2043b179d48bSDaniel Bristot de Oliveira if (test_bit(option, &osnoise_options)) 2044b179d48bSDaniel Bristot de Oliveira seq_printf(s, "%s", osnoise_options_str[option]); 2045b179d48bSDaniel Bristot de Oliveira else 2046b179d48bSDaniel Bristot de Oliveira seq_printf(s, "NO_%s", osnoise_options_str[option]); 2047b179d48bSDaniel Bristot de Oliveira 2048b179d48bSDaniel Bristot de Oliveira out: 2049b179d48bSDaniel Bristot de Oliveira if (option != OSN_MAX) 2050b179d48bSDaniel Bristot de Oliveira seq_puts(s, " "); 2051b179d48bSDaniel Bristot de Oliveira 2052b179d48bSDaniel Bristot de Oliveira return 0; 2053b179d48bSDaniel Bristot de Oliveira } 2054b179d48bSDaniel Bristot de Oliveira 2055b179d48bSDaniel Bristot de Oliveira static void s_options_stop(struct seq_file *s, void *v) 2056b179d48bSDaniel Bristot de Oliveira { 2057b179d48bSDaniel Bristot de Oliveira seq_puts(s, "\n"); 2058b179d48bSDaniel Bristot de Oliveira mutex_unlock(&interface_lock); 2059b179d48bSDaniel Bristot de Oliveira } 2060b179d48bSDaniel Bristot de Oliveira 2061b179d48bSDaniel Bristot de Oliveira static const struct seq_operations osnoise_options_seq_ops = { 2062b179d48bSDaniel Bristot de Oliveira .start = s_options_start, 2063b179d48bSDaniel Bristot de Oliveira .next = s_options_next, 2064b179d48bSDaniel Bristot de Oliveira .show = s_options_show, 2065b179d48bSDaniel Bristot de Oliveira .stop = s_options_stop 2066b179d48bSDaniel Bristot de Oliveira }; 2067b179d48bSDaniel Bristot de Oliveira 2068b179d48bSDaniel Bristot de Oliveira static int osnoise_options_open(struct inode *inode, struct file *file) 2069b179d48bSDaniel Bristot de Oliveira { 2070b179d48bSDaniel Bristot de Oliveira return seq_open(file, &osnoise_options_seq_ops); 2071b179d48bSDaniel Bristot de Oliveira }; 2072b179d48bSDaniel Bristot de Oliveira 2073b179d48bSDaniel Bristot de Oliveira /** 2074b179d48bSDaniel Bristot de Oliveira * osnoise_options_write - Write function for "options" entry 2075b179d48bSDaniel Bristot de Oliveira * @filp: The active open file structure 2076b179d48bSDaniel Bristot de Oliveira * @ubuf: The user buffer that contains the value to write 2077b179d48bSDaniel Bristot de Oliveira * @cnt: The maximum number of bytes to write to "file" 2078b179d48bSDaniel Bristot de Oliveira * @ppos: The current position in @file 2079b179d48bSDaniel Bristot de Oliveira * 2080b179d48bSDaniel Bristot de Oliveira * Writing the option name sets the option, writing the "NO_" 2081b179d48bSDaniel Bristot de Oliveira * prefix in front of the option name disables it. 2082b179d48bSDaniel Bristot de Oliveira * 2083b179d48bSDaniel Bristot de Oliveira * Writing "DEFAULTS" resets the option values to the default ones. 2084b179d48bSDaniel Bristot de Oliveira */ 2085b179d48bSDaniel Bristot de Oliveira static ssize_t osnoise_options_write(struct file *filp, const char __user *ubuf, 2086b179d48bSDaniel Bristot de Oliveira size_t cnt, loff_t *ppos) 2087b179d48bSDaniel Bristot de Oliveira { 2088b179d48bSDaniel Bristot de Oliveira int running, option, enable, retval; 2089b179d48bSDaniel Bristot de Oliveira char buf[256], *option_str; 2090b179d48bSDaniel Bristot de Oliveira 2091b179d48bSDaniel Bristot de Oliveira if (cnt >= 256) 2092b179d48bSDaniel Bristot de Oliveira return -EINVAL; 2093b179d48bSDaniel Bristot de Oliveira 2094b179d48bSDaniel Bristot de Oliveira if (copy_from_user(buf, ubuf, cnt)) 2095b179d48bSDaniel Bristot de Oliveira return -EFAULT; 2096b179d48bSDaniel Bristot de Oliveira 2097b179d48bSDaniel Bristot de Oliveira buf[cnt] = 0; 2098b179d48bSDaniel Bristot de Oliveira 2099b179d48bSDaniel Bristot de Oliveira if (strncmp(buf, "NO_", 3)) { 2100b179d48bSDaniel Bristot de Oliveira option_str = strstrip(buf); 2101b179d48bSDaniel Bristot de Oliveira enable = true; 2102b179d48bSDaniel Bristot de Oliveira } else { 2103b179d48bSDaniel Bristot de Oliveira option_str = strstrip(&buf[3]); 2104b179d48bSDaniel Bristot de Oliveira enable = false; 2105b179d48bSDaniel Bristot de Oliveira } 2106b179d48bSDaniel Bristot de Oliveira 2107b179d48bSDaniel Bristot de Oliveira option = match_string(osnoise_options_str, OSN_MAX, option_str); 2108b179d48bSDaniel Bristot de Oliveira if (option < 0) 2109b179d48bSDaniel Bristot de Oliveira return -EINVAL; 2110b179d48bSDaniel Bristot de Oliveira 2111b179d48bSDaniel Bristot de Oliveira /* 2112b179d48bSDaniel Bristot de Oliveira * trace_types_lock is taken to avoid concurrency on start/stop. 2113b179d48bSDaniel Bristot de Oliveira */ 2114b179d48bSDaniel Bristot de Oliveira mutex_lock(&trace_types_lock); 2115b179d48bSDaniel Bristot de Oliveira running = osnoise_has_registered_instances(); 2116b179d48bSDaniel Bristot de Oliveira if (running) 2117b179d48bSDaniel Bristot de Oliveira stop_per_cpu_kthreads(); 2118b179d48bSDaniel Bristot de Oliveira 2119b179d48bSDaniel Bristot de Oliveira mutex_lock(&interface_lock); 2120b179d48bSDaniel Bristot de Oliveira /* 2121b179d48bSDaniel Bristot de Oliveira * avoid CPU hotplug operations that might read options. 2122b179d48bSDaniel Bristot de Oliveira */ 2123b179d48bSDaniel Bristot de Oliveira cpus_read_lock(); 2124b179d48bSDaniel Bristot de Oliveira 2125b179d48bSDaniel Bristot de Oliveira retval = cnt; 2126b179d48bSDaniel Bristot de Oliveira 2127b179d48bSDaniel Bristot de Oliveira if (enable) { 2128b179d48bSDaniel Bristot de Oliveira if (option == OSN_DEFAULTS) 2129b179d48bSDaniel Bristot de Oliveira osnoise_options = OSN_DEFAULT_OPTIONS; 2130b179d48bSDaniel Bristot de Oliveira else 2131b179d48bSDaniel Bristot de Oliveira set_bit(option, &osnoise_options); 2132b179d48bSDaniel Bristot de Oliveira } else { 2133b179d48bSDaniel Bristot de Oliveira if (option == OSN_DEFAULTS) 2134b179d48bSDaniel Bristot de Oliveira retval = -EINVAL; 2135b179d48bSDaniel Bristot de Oliveira else 2136b179d48bSDaniel Bristot de Oliveira clear_bit(option, &osnoise_options); 2137b179d48bSDaniel Bristot de Oliveira } 2138b179d48bSDaniel Bristot de Oliveira 2139b179d48bSDaniel Bristot de Oliveira cpus_read_unlock(); 2140b179d48bSDaniel Bristot de Oliveira mutex_unlock(&interface_lock); 2141b179d48bSDaniel Bristot de Oliveira 2142b179d48bSDaniel Bristot de Oliveira if (running) 2143b179d48bSDaniel Bristot de Oliveira start_per_cpu_kthreads(); 2144b179d48bSDaniel Bristot de Oliveira mutex_unlock(&trace_types_lock); 2145b179d48bSDaniel Bristot de Oliveira 2146b179d48bSDaniel Bristot de Oliveira return retval; 2147b179d48bSDaniel Bristot de Oliveira } 2148b179d48bSDaniel Bristot de Oliveira 2149b179d48bSDaniel Bristot de Oliveira /* 2150bce29ac9SDaniel Bristot de Oliveira * osnoise_cpus_read - Read function for reading the "cpus" file 2151bce29ac9SDaniel Bristot de Oliveira * @filp: The active open file structure 2152bce29ac9SDaniel Bristot de Oliveira * @ubuf: The userspace provided buffer to read value into 2153bce29ac9SDaniel Bristot de Oliveira * @cnt: The maximum number of bytes to read 2154bce29ac9SDaniel Bristot de Oliveira * @ppos: The current "file" position 2155bce29ac9SDaniel Bristot de Oliveira * 2156bce29ac9SDaniel Bristot de Oliveira * Prints the "cpus" output into the user-provided buffer. 2157bce29ac9SDaniel Bristot de Oliveira */ 2158bce29ac9SDaniel Bristot de Oliveira static ssize_t 2159bce29ac9SDaniel Bristot de Oliveira osnoise_cpus_read(struct file *filp, char __user *ubuf, size_t count, 2160bce29ac9SDaniel Bristot de Oliveira loff_t *ppos) 2161bce29ac9SDaniel Bristot de Oliveira { 2162bce29ac9SDaniel Bristot de Oliveira char *mask_str; 2163bce29ac9SDaniel Bristot de Oliveira int len; 2164bce29ac9SDaniel Bristot de Oliveira 2165bce29ac9SDaniel Bristot de Oliveira mutex_lock(&interface_lock); 2166bce29ac9SDaniel Bristot de Oliveira 2167bce29ac9SDaniel Bristot de Oliveira len = snprintf(NULL, 0, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)) + 1; 2168bce29ac9SDaniel Bristot de Oliveira mask_str = kmalloc(len, GFP_KERNEL); 2169bce29ac9SDaniel Bristot de Oliveira if (!mask_str) { 2170bce29ac9SDaniel Bristot de Oliveira count = -ENOMEM; 2171bce29ac9SDaniel Bristot de Oliveira goto out_unlock; 2172bce29ac9SDaniel Bristot de Oliveira } 2173bce29ac9SDaniel Bristot de Oliveira 2174bce29ac9SDaniel Bristot de Oliveira len = snprintf(mask_str, len, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)); 2175bce29ac9SDaniel Bristot de Oliveira if (len >= count) { 2176bce29ac9SDaniel Bristot de Oliveira count = -EINVAL; 2177bce29ac9SDaniel Bristot de Oliveira goto out_free; 2178bce29ac9SDaniel Bristot de Oliveira } 2179bce29ac9SDaniel Bristot de Oliveira 2180bce29ac9SDaniel Bristot de Oliveira count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len); 2181bce29ac9SDaniel Bristot de Oliveira 2182bce29ac9SDaniel Bristot de Oliveira out_free: 2183bce29ac9SDaniel Bristot de Oliveira kfree(mask_str); 2184bce29ac9SDaniel Bristot de Oliveira out_unlock: 2185bce29ac9SDaniel Bristot de Oliveira mutex_unlock(&interface_lock); 2186bce29ac9SDaniel Bristot de Oliveira 2187bce29ac9SDaniel Bristot de Oliveira return count; 2188bce29ac9SDaniel Bristot de Oliveira } 2189bce29ac9SDaniel Bristot de Oliveira 2190bce29ac9SDaniel Bristot de Oliveira /* 2191bce29ac9SDaniel Bristot de Oliveira * osnoise_cpus_write - Write function for "cpus" entry 2192bce29ac9SDaniel Bristot de Oliveira * @filp: The active open file structure 2193bce29ac9SDaniel Bristot de Oliveira * @ubuf: The user buffer that contains the value to write 2194bce29ac9SDaniel Bristot de Oliveira * @cnt: The maximum number of bytes to write to "file" 2195bce29ac9SDaniel Bristot de Oliveira * @ppos: The current position in @file 2196bce29ac9SDaniel Bristot de Oliveira * 2197bce29ac9SDaniel Bristot de Oliveira * This function provides a write implementation for the "cpus" 2198bce29ac9SDaniel Bristot de Oliveira * interface to the osnoise trace. By default, it lists all CPUs, 2199bce29ac9SDaniel Bristot de Oliveira * in this way, allowing osnoise threads to run on any online CPU 2200bce29ac9SDaniel Bristot de Oliveira * of the system. It serves to restrict the execution of osnoise to the 220166df27f1SDaniel Bristot de Oliveira * set of CPUs writing via this interface. Why not use "tracing_cpumask"? 220266df27f1SDaniel Bristot de Oliveira * Because the user might be interested in tracing what is running on 220366df27f1SDaniel Bristot de Oliveira * other CPUs. For instance, one might run osnoise in one HT CPU 220466df27f1SDaniel Bristot de Oliveira * while observing what is running on the sibling HT CPU. 2205bce29ac9SDaniel Bristot de Oliveira */ 2206bce29ac9SDaniel Bristot de Oliveira static ssize_t 2207bce29ac9SDaniel Bristot de Oliveira osnoise_cpus_write(struct file *filp, const char __user *ubuf, size_t count, 2208bce29ac9SDaniel Bristot de Oliveira loff_t *ppos) 2209bce29ac9SDaniel Bristot de Oliveira { 2210bce29ac9SDaniel Bristot de Oliveira cpumask_var_t osnoise_cpumask_new; 2211bce29ac9SDaniel Bristot de Oliveira int running, err; 2212bce29ac9SDaniel Bristot de Oliveira char buf[256]; 2213bce29ac9SDaniel Bristot de Oliveira 2214bce29ac9SDaniel Bristot de Oliveira if (count >= 256) 2215bce29ac9SDaniel Bristot de Oliveira return -EINVAL; 2216bce29ac9SDaniel Bristot de Oliveira 2217bce29ac9SDaniel Bristot de Oliveira if (copy_from_user(buf, ubuf, count)) 2218bce29ac9SDaniel Bristot de Oliveira return -EFAULT; 2219bce29ac9SDaniel Bristot de Oliveira 2220bce29ac9SDaniel Bristot de Oliveira if (!zalloc_cpumask_var(&osnoise_cpumask_new, GFP_KERNEL)) 2221bce29ac9SDaniel Bristot de Oliveira return -ENOMEM; 2222bce29ac9SDaniel Bristot de Oliveira 2223bce29ac9SDaniel Bristot de Oliveira err = cpulist_parse(buf, osnoise_cpumask_new); 2224bce29ac9SDaniel Bristot de Oliveira if (err) 2225bce29ac9SDaniel Bristot de Oliveira goto err_free; 2226bce29ac9SDaniel Bristot de Oliveira 2227bce29ac9SDaniel Bristot de Oliveira /* 2228dae18134SDaniel Bristot de Oliveira * trace_types_lock is taken to avoid concurrency on start/stop. 2229bce29ac9SDaniel Bristot de Oliveira */ 2230bce29ac9SDaniel Bristot de Oliveira mutex_lock(&trace_types_lock); 2231dae18134SDaniel Bristot de Oliveira running = osnoise_has_registered_instances(); 2232bce29ac9SDaniel Bristot de Oliveira if (running) 22332bd1bdf0SDaniel Bristot de Oliveira stop_per_cpu_kthreads(); 2234bce29ac9SDaniel Bristot de Oliveira 2235bce29ac9SDaniel Bristot de Oliveira mutex_lock(&interface_lock); 2236c8895e27SDaniel Bristot de Oliveira /* 2237c8895e27SDaniel Bristot de Oliveira * osnoise_cpumask is read by CPU hotplug operations. 2238c8895e27SDaniel Bristot de Oliveira */ 223999c37d1aSSebastian Andrzej Siewior cpus_read_lock(); 2240c8895e27SDaniel Bristot de Oliveira 2241bce29ac9SDaniel Bristot de Oliveira cpumask_copy(&osnoise_cpumask, osnoise_cpumask_new); 2242c8895e27SDaniel Bristot de Oliveira 224399c37d1aSSebastian Andrzej Siewior cpus_read_unlock(); 2244bce29ac9SDaniel Bristot de Oliveira mutex_unlock(&interface_lock); 2245bce29ac9SDaniel Bristot de Oliveira 2246bce29ac9SDaniel Bristot de Oliveira if (running) 22472bd1bdf0SDaniel Bristot de Oliveira start_per_cpu_kthreads(); 2248bce29ac9SDaniel Bristot de Oliveira mutex_unlock(&trace_types_lock); 2249bce29ac9SDaniel Bristot de Oliveira 2250bce29ac9SDaniel Bristot de Oliveira free_cpumask_var(osnoise_cpumask_new); 2251bce29ac9SDaniel Bristot de Oliveira return count; 2252bce29ac9SDaniel Bristot de Oliveira 2253bce29ac9SDaniel Bristot de Oliveira err_free: 2254bce29ac9SDaniel Bristot de Oliveira free_cpumask_var(osnoise_cpumask_new); 2255bce29ac9SDaniel Bristot de Oliveira 2256bce29ac9SDaniel Bristot de Oliveira return err; 2257bce29ac9SDaniel Bristot de Oliveira } 2258bce29ac9SDaniel Bristot de Oliveira 2259bce29ac9SDaniel Bristot de Oliveira /* 2260bce29ac9SDaniel Bristot de Oliveira * osnoise/runtime_us: cannot be greater than the period. 2261bce29ac9SDaniel Bristot de Oliveira */ 2262bce29ac9SDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_runtime = { 2263bce29ac9SDaniel Bristot de Oliveira .lock = &interface_lock, 2264bce29ac9SDaniel Bristot de Oliveira .val = &osnoise_data.sample_runtime, 2265bce29ac9SDaniel Bristot de Oliveira .max = &osnoise_data.sample_period, 2266bce29ac9SDaniel Bristot de Oliveira .min = NULL, 2267bce29ac9SDaniel Bristot de Oliveira }; 2268bce29ac9SDaniel Bristot de Oliveira 2269bce29ac9SDaniel Bristot de Oliveira /* 2270bce29ac9SDaniel Bristot de Oliveira * osnoise/period_us: cannot be smaller than the runtime. 2271bce29ac9SDaniel Bristot de Oliveira */ 2272bce29ac9SDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_period = { 2273bce29ac9SDaniel Bristot de Oliveira .lock = &interface_lock, 2274bce29ac9SDaniel Bristot de Oliveira .val = &osnoise_data.sample_period, 2275bce29ac9SDaniel Bristot de Oliveira .max = NULL, 2276bce29ac9SDaniel Bristot de Oliveira .min = &osnoise_data.sample_runtime, 2277bce29ac9SDaniel Bristot de Oliveira }; 2278bce29ac9SDaniel Bristot de Oliveira 2279bce29ac9SDaniel Bristot de Oliveira /* 2280bce29ac9SDaniel Bristot de Oliveira * osnoise/stop_tracing_us: no limit. 2281bce29ac9SDaniel Bristot de Oliveira */ 2282bce29ac9SDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_stop_tracing_in = { 2283bce29ac9SDaniel Bristot de Oliveira .lock = &interface_lock, 2284bce29ac9SDaniel Bristot de Oliveira .val = &osnoise_data.stop_tracing, 2285bce29ac9SDaniel Bristot de Oliveira .max = NULL, 2286bce29ac9SDaniel Bristot de Oliveira .min = NULL, 2287bce29ac9SDaniel Bristot de Oliveira }; 2288bce29ac9SDaniel Bristot de Oliveira 2289bce29ac9SDaniel Bristot de Oliveira /* 2290bce29ac9SDaniel Bristot de Oliveira * osnoise/stop_tracing_total_us: no limit. 2291bce29ac9SDaniel Bristot de Oliveira */ 2292bce29ac9SDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_stop_tracing_total = { 2293bce29ac9SDaniel Bristot de Oliveira .lock = &interface_lock, 2294bce29ac9SDaniel Bristot de Oliveira .val = &osnoise_data.stop_tracing_total, 2295bce29ac9SDaniel Bristot de Oliveira .max = NULL, 2296bce29ac9SDaniel Bristot de Oliveira .min = NULL, 2297bce29ac9SDaniel Bristot de Oliveira }; 2298bce29ac9SDaniel Bristot de Oliveira 2299a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 2300a955d7eaSDaniel Bristot de Oliveira /* 2301a955d7eaSDaniel Bristot de Oliveira * osnoise/print_stack: print the stacktrace of the IRQ handler if the total 2302a955d7eaSDaniel Bristot de Oliveira * latency is higher than val. 2303a955d7eaSDaniel Bristot de Oliveira */ 2304a955d7eaSDaniel Bristot de Oliveira static struct trace_min_max_param osnoise_print_stack = { 2305a955d7eaSDaniel Bristot de Oliveira .lock = &interface_lock, 2306a955d7eaSDaniel Bristot de Oliveira .val = &osnoise_data.print_stack, 2307a955d7eaSDaniel Bristot de Oliveira .max = NULL, 2308a955d7eaSDaniel Bristot de Oliveira .min = NULL, 2309a955d7eaSDaniel Bristot de Oliveira }; 2310a955d7eaSDaniel Bristot de Oliveira 2311a955d7eaSDaniel Bristot de Oliveira /* 2312a955d7eaSDaniel Bristot de Oliveira * osnoise/timerlat_period: min 100 us, max 1 s 2313a955d7eaSDaniel Bristot de Oliveira */ 23147a025e06STom Rix static u64 timerlat_min_period = 100; 23157a025e06STom Rix static u64 timerlat_max_period = 1000000; 2316a955d7eaSDaniel Bristot de Oliveira static struct trace_min_max_param timerlat_period = { 2317a955d7eaSDaniel Bristot de Oliveira .lock = &interface_lock, 2318a955d7eaSDaniel Bristot de Oliveira .val = &osnoise_data.timerlat_period, 2319a955d7eaSDaniel Bristot de Oliveira .max = &timerlat_max_period, 2320a955d7eaSDaniel Bristot de Oliveira .min = &timerlat_min_period, 2321a955d7eaSDaniel Bristot de Oliveira }; 2322a955d7eaSDaniel Bristot de Oliveira #endif 2323a955d7eaSDaniel Bristot de Oliveira 2324bce29ac9SDaniel Bristot de Oliveira static const struct file_operations cpus_fops = { 2325bce29ac9SDaniel Bristot de Oliveira .open = tracing_open_generic, 2326bce29ac9SDaniel Bristot de Oliveira .read = osnoise_cpus_read, 2327bce29ac9SDaniel Bristot de Oliveira .write = osnoise_cpus_write, 2328bce29ac9SDaniel Bristot de Oliveira .llseek = generic_file_llseek, 2329bce29ac9SDaniel Bristot de Oliveira }; 2330bce29ac9SDaniel Bristot de Oliveira 2331b179d48bSDaniel Bristot de Oliveira static const struct file_operations osnoise_options_fops = { 2332b179d48bSDaniel Bristot de Oliveira .open = osnoise_options_open, 2333b179d48bSDaniel Bristot de Oliveira .read = seq_read, 2334b179d48bSDaniel Bristot de Oliveira .llseek = seq_lseek, 2335b179d48bSDaniel Bristot de Oliveira .release = seq_release, 2336b179d48bSDaniel Bristot de Oliveira .write = osnoise_options_write 2337b179d48bSDaniel Bristot de Oliveira }; 2338b179d48bSDaniel Bristot de Oliveira 2339ccb67544SDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 2340b14f4568SDaniel Bristot de Oliveira #ifdef CONFIG_STACKTRACE 2341b14f4568SDaniel Bristot de Oliveira static int init_timerlat_stack_tracefs(struct dentry *top_dir) 2342b14f4568SDaniel Bristot de Oliveira { 2343b14f4568SDaniel Bristot de Oliveira struct dentry *tmp; 2344b14f4568SDaniel Bristot de Oliveira 2345b14f4568SDaniel Bristot de Oliveira tmp = tracefs_create_file("print_stack", TRACE_MODE_WRITE, top_dir, 2346b14f4568SDaniel Bristot de Oliveira &osnoise_print_stack, &trace_min_max_fops); 2347b14f4568SDaniel Bristot de Oliveira if (!tmp) 2348b14f4568SDaniel Bristot de Oliveira return -ENOMEM; 2349b14f4568SDaniel Bristot de Oliveira 2350b14f4568SDaniel Bristot de Oliveira return 0; 2351b14f4568SDaniel Bristot de Oliveira } 2352b14f4568SDaniel Bristot de Oliveira #else /* CONFIG_STACKTRACE */ 2353b14f4568SDaniel Bristot de Oliveira static int init_timerlat_stack_tracefs(struct dentry *top_dir) 2354b14f4568SDaniel Bristot de Oliveira { 2355b14f4568SDaniel Bristot de Oliveira return 0; 2356b14f4568SDaniel Bristot de Oliveira } 2357b14f4568SDaniel Bristot de Oliveira #endif /* CONFIG_STACKTRACE */ 2358b14f4568SDaniel Bristot de Oliveira 2359ccb67544SDaniel Bristot de Oliveira /* 2360ccb67544SDaniel Bristot de Oliveira * init_timerlat_tracefs - A function to initialize the timerlat interface files 2361ccb67544SDaniel Bristot de Oliveira */ 2362ccb67544SDaniel Bristot de Oliveira static int init_timerlat_tracefs(struct dentry *top_dir) 2363ccb67544SDaniel Bristot de Oliveira { 2364ccb67544SDaniel Bristot de Oliveira struct dentry *tmp; 2365ccb67544SDaniel Bristot de Oliveira 2366ccb67544SDaniel Bristot de Oliveira tmp = tracefs_create_file("timerlat_period_us", TRACE_MODE_WRITE, top_dir, 2367ccb67544SDaniel Bristot de Oliveira &timerlat_period, &trace_min_max_fops); 2368ccb67544SDaniel Bristot de Oliveira if (!tmp) 2369ccb67544SDaniel Bristot de Oliveira return -ENOMEM; 2370ccb67544SDaniel Bristot de Oliveira 2371b14f4568SDaniel Bristot de Oliveira return init_timerlat_stack_tracefs(top_dir); 2372ccb67544SDaniel Bristot de Oliveira } 2373ccb67544SDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */ 2374ccb67544SDaniel Bristot de Oliveira static int init_timerlat_tracefs(struct dentry *top_dir) 2375ccb67544SDaniel Bristot de Oliveira { 2376ccb67544SDaniel Bristot de Oliveira return 0; 2377ccb67544SDaniel Bristot de Oliveira } 2378ccb67544SDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */ 2379ccb67544SDaniel Bristot de Oliveira 2380bce29ac9SDaniel Bristot de Oliveira /* 2381bce29ac9SDaniel Bristot de Oliveira * init_tracefs - A function to initialize the tracefs interface files 2382bce29ac9SDaniel Bristot de Oliveira * 2383a955d7eaSDaniel Bristot de Oliveira * This function creates entries in tracefs for "osnoise" and "timerlat". 2384a955d7eaSDaniel Bristot de Oliveira * It creates these directories in the tracing directory, and within that 2385a955d7eaSDaniel Bristot de Oliveira * directory the use can change and view the configs. 2386bce29ac9SDaniel Bristot de Oliveira */ 2387bce29ac9SDaniel Bristot de Oliveira static int init_tracefs(void) 2388bce29ac9SDaniel Bristot de Oliveira { 2389bce29ac9SDaniel Bristot de Oliveira struct dentry *top_dir; 2390bce29ac9SDaniel Bristot de Oliveira struct dentry *tmp; 2391bce29ac9SDaniel Bristot de Oliveira int ret; 2392bce29ac9SDaniel Bristot de Oliveira 2393bce29ac9SDaniel Bristot de Oliveira ret = tracing_init_dentry(); 2394bce29ac9SDaniel Bristot de Oliveira if (ret) 2395bce29ac9SDaniel Bristot de Oliveira return -ENOMEM; 2396bce29ac9SDaniel Bristot de Oliveira 2397bce29ac9SDaniel Bristot de Oliveira top_dir = tracefs_create_dir("osnoise", NULL); 2398bce29ac9SDaniel Bristot de Oliveira if (!top_dir) 2399a955d7eaSDaniel Bristot de Oliveira return 0; 2400bce29ac9SDaniel Bristot de Oliveira 240121ccc9cdSSteven Rostedt (VMware) tmp = tracefs_create_file("period_us", TRACE_MODE_WRITE, top_dir, 2402bce29ac9SDaniel Bristot de Oliveira &osnoise_period, &trace_min_max_fops); 2403bce29ac9SDaniel Bristot de Oliveira if (!tmp) 2404bce29ac9SDaniel Bristot de Oliveira goto err; 2405bce29ac9SDaniel Bristot de Oliveira 240621ccc9cdSSteven Rostedt (VMware) tmp = tracefs_create_file("runtime_us", TRACE_MODE_WRITE, top_dir, 2407bce29ac9SDaniel Bristot de Oliveira &osnoise_runtime, &trace_min_max_fops); 2408bce29ac9SDaniel Bristot de Oliveira if (!tmp) 2409bce29ac9SDaniel Bristot de Oliveira goto err; 2410bce29ac9SDaniel Bristot de Oliveira 241121ccc9cdSSteven Rostedt (VMware) tmp = tracefs_create_file("stop_tracing_us", TRACE_MODE_WRITE, top_dir, 2412bce29ac9SDaniel Bristot de Oliveira &osnoise_stop_tracing_in, &trace_min_max_fops); 2413bce29ac9SDaniel Bristot de Oliveira if (!tmp) 2414bce29ac9SDaniel Bristot de Oliveira goto err; 2415bce29ac9SDaniel Bristot de Oliveira 241621ccc9cdSSteven Rostedt (VMware) tmp = tracefs_create_file("stop_tracing_total_us", TRACE_MODE_WRITE, top_dir, 2417bce29ac9SDaniel Bristot de Oliveira &osnoise_stop_tracing_total, &trace_min_max_fops); 2418bce29ac9SDaniel Bristot de Oliveira if (!tmp) 2419bce29ac9SDaniel Bristot de Oliveira goto err; 2420bce29ac9SDaniel Bristot de Oliveira 242121ccc9cdSSteven Rostedt (VMware) tmp = trace_create_file("cpus", TRACE_MODE_WRITE, top_dir, NULL, &cpus_fops); 2422bce29ac9SDaniel Bristot de Oliveira if (!tmp) 2423bce29ac9SDaniel Bristot de Oliveira goto err; 2424a955d7eaSDaniel Bristot de Oliveira 2425b179d48bSDaniel Bristot de Oliveira tmp = trace_create_file("options", TRACE_MODE_WRITE, top_dir, NULL, 2426b179d48bSDaniel Bristot de Oliveira &osnoise_options_fops); 2427b179d48bSDaniel Bristot de Oliveira if (!tmp) 2428b179d48bSDaniel Bristot de Oliveira goto err; 2429b179d48bSDaniel Bristot de Oliveira 2430ccb67544SDaniel Bristot de Oliveira ret = init_timerlat_tracefs(top_dir); 2431ccb67544SDaniel Bristot de Oliveira if (ret) 2432a955d7eaSDaniel Bristot de Oliveira goto err; 2433bce29ac9SDaniel Bristot de Oliveira 2434bce29ac9SDaniel Bristot de Oliveira return 0; 2435bce29ac9SDaniel Bristot de Oliveira 2436bce29ac9SDaniel Bristot de Oliveira err: 2437bce29ac9SDaniel Bristot de Oliveira tracefs_remove(top_dir); 2438bce29ac9SDaniel Bristot de Oliveira return -ENOMEM; 2439bce29ac9SDaniel Bristot de Oliveira } 2440bce29ac9SDaniel Bristot de Oliveira 2441bce29ac9SDaniel Bristot de Oliveira static int osnoise_hook_events(void) 2442bce29ac9SDaniel Bristot de Oliveira { 2443bce29ac9SDaniel Bristot de Oliveira int retval; 2444bce29ac9SDaniel Bristot de Oliveira 2445bce29ac9SDaniel Bristot de Oliveira /* 2446bce29ac9SDaniel Bristot de Oliveira * Trace is already hooked, we are re-enabling from 2447bce29ac9SDaniel Bristot de Oliveira * a stop_tracing_*. 2448bce29ac9SDaniel Bristot de Oliveira */ 2449bce29ac9SDaniel Bristot de Oliveira if (trace_osnoise_callback_enabled) 2450bce29ac9SDaniel Bristot de Oliveira return 0; 2451bce29ac9SDaniel Bristot de Oliveira 2452bce29ac9SDaniel Bristot de Oliveira retval = hook_irq_events(); 2453bce29ac9SDaniel Bristot de Oliveira if (retval) 2454bce29ac9SDaniel Bristot de Oliveira return -EINVAL; 2455bce29ac9SDaniel Bristot de Oliveira 2456bce29ac9SDaniel Bristot de Oliveira retval = hook_softirq_events(); 2457bce29ac9SDaniel Bristot de Oliveira if (retval) 2458bce29ac9SDaniel Bristot de Oliveira goto out_unhook_irq; 2459bce29ac9SDaniel Bristot de Oliveira 2460bce29ac9SDaniel Bristot de Oliveira retval = hook_thread_events(); 2461bce29ac9SDaniel Bristot de Oliveira /* 2462bce29ac9SDaniel Bristot de Oliveira * All fine! 2463bce29ac9SDaniel Bristot de Oliveira */ 2464bce29ac9SDaniel Bristot de Oliveira if (!retval) 2465bce29ac9SDaniel Bristot de Oliveira return 0; 2466bce29ac9SDaniel Bristot de Oliveira 2467bce29ac9SDaniel Bristot de Oliveira unhook_softirq_events(); 2468bce29ac9SDaniel Bristot de Oliveira out_unhook_irq: 2469bce29ac9SDaniel Bristot de Oliveira unhook_irq_events(); 2470bce29ac9SDaniel Bristot de Oliveira return -EINVAL; 2471bce29ac9SDaniel Bristot de Oliveira } 2472bce29ac9SDaniel Bristot de Oliveira 24730878355bSNikita Yushchenko static void osnoise_unhook_events(void) 24740878355bSNikita Yushchenko { 24750878355bSNikita Yushchenko unhook_thread_events(); 24760878355bSNikita Yushchenko unhook_softirq_events(); 24770878355bSNikita Yushchenko unhook_irq_events(); 24780878355bSNikita Yushchenko } 24790878355bSNikita Yushchenko 248015ca4bdbSDaniel Bristot de Oliveira /* 248115ca4bdbSDaniel Bristot de Oliveira * osnoise_workload_start - start the workload and hook to events 248215ca4bdbSDaniel Bristot de Oliveira */ 248315ca4bdbSDaniel Bristot de Oliveira static int osnoise_workload_start(void) 2484bce29ac9SDaniel Bristot de Oliveira { 2485bce29ac9SDaniel Bristot de Oliveira int retval; 2486bce29ac9SDaniel Bristot de Oliveira 24872fac8d64SDaniel Bristot de Oliveira /* 24882fac8d64SDaniel Bristot de Oliveira * Instances need to be registered after calling workload 24892fac8d64SDaniel Bristot de Oliveira * start. Hence, if there is already an instance, the 24902fac8d64SDaniel Bristot de Oliveira * workload was already registered. Otherwise, this 24912fac8d64SDaniel Bristot de Oliveira * code is on the way to register the first instance, 24922fac8d64SDaniel Bristot de Oliveira * and the workload will start. 24932fac8d64SDaniel Bristot de Oliveira */ 24942fac8d64SDaniel Bristot de Oliveira if (osnoise_has_registered_instances()) 24952fac8d64SDaniel Bristot de Oliveira return 0; 24962fac8d64SDaniel Bristot de Oliveira 2497bce29ac9SDaniel Bristot de Oliveira osn_var_reset_all(); 2498bce29ac9SDaniel Bristot de Oliveira 2499bce29ac9SDaniel Bristot de Oliveira retval = osnoise_hook_events(); 2500bce29ac9SDaniel Bristot de Oliveira if (retval) 2501a955d7eaSDaniel Bristot de Oliveira return retval; 2502c3b6343cSDaniel Bristot de Oliveira 2503bce29ac9SDaniel Bristot de Oliveira /* 2504c3b6343cSDaniel Bristot de Oliveira * Make sure that ftrace_nmi_enter/exit() see reset values 2505c3b6343cSDaniel Bristot de Oliveira * before enabling trace_osnoise_callback_enabled. 2506bce29ac9SDaniel Bristot de Oliveira */ 2507bce29ac9SDaniel Bristot de Oliveira barrier(); 2508bce29ac9SDaniel Bristot de Oliveira trace_osnoise_callback_enabled = true; 2509bce29ac9SDaniel Bristot de Oliveira 251015ca4bdbSDaniel Bristot de Oliveira retval = start_per_cpu_kthreads(); 2511a955d7eaSDaniel Bristot de Oliveira if (retval) { 25120878355bSNikita Yushchenko trace_osnoise_callback_enabled = false; 25130878355bSNikita Yushchenko /* 25140878355bSNikita Yushchenko * Make sure that ftrace_nmi_enter/exit() see 25150878355bSNikita Yushchenko * trace_osnoise_callback_enabled as false before continuing. 25160878355bSNikita Yushchenko */ 25170878355bSNikita Yushchenko barrier(); 25180878355bSNikita Yushchenko 25190878355bSNikita Yushchenko osnoise_unhook_events(); 2520a955d7eaSDaniel Bristot de Oliveira return retval; 2521a955d7eaSDaniel Bristot de Oliveira } 2522a955d7eaSDaniel Bristot de Oliveira 2523a955d7eaSDaniel Bristot de Oliveira return 0; 2524a955d7eaSDaniel Bristot de Oliveira } 2525a955d7eaSDaniel Bristot de Oliveira 252615ca4bdbSDaniel Bristot de Oliveira /* 252715ca4bdbSDaniel Bristot de Oliveira * osnoise_workload_stop - stop the workload and unhook the events 252815ca4bdbSDaniel Bristot de Oliveira */ 252915ca4bdbSDaniel Bristot de Oliveira static void osnoise_workload_stop(void) 2530bce29ac9SDaniel Bristot de Oliveira { 25312fac8d64SDaniel Bristot de Oliveira /* 25322fac8d64SDaniel Bristot de Oliveira * Instances need to be unregistered before calling 25332fac8d64SDaniel Bristot de Oliveira * stop. Hence, if there is a registered instance, more 25342fac8d64SDaniel Bristot de Oliveira * than one instance is running, and the workload will not 25352fac8d64SDaniel Bristot de Oliveira * yet stop. Otherwise, this code is on the way to disable 25362fac8d64SDaniel Bristot de Oliveira * the last instance, and the workload can stop. 25372fac8d64SDaniel Bristot de Oliveira */ 2538dae18134SDaniel Bristot de Oliveira if (osnoise_has_registered_instances()) 2539bce29ac9SDaniel Bristot de Oliveira return; 2540bce29ac9SDaniel Bristot de Oliveira 2541f0cfe17bSDaniel Bristot de Oliveira /* 2542f0cfe17bSDaniel Bristot de Oliveira * If callbacks were already disabled in a previous stop 2543f0cfe17bSDaniel Bristot de Oliveira * call, there is no need to disable then again. 2544f0cfe17bSDaniel Bristot de Oliveira * 2545f0cfe17bSDaniel Bristot de Oliveira * For instance, this happens when tracing is stopped via: 2546f0cfe17bSDaniel Bristot de Oliveira * echo 0 > tracing_on 2547f0cfe17bSDaniel Bristot de Oliveira * echo nop > current_tracer. 2548f0cfe17bSDaniel Bristot de Oliveira */ 2549f0cfe17bSDaniel Bristot de Oliveira if (!trace_osnoise_callback_enabled) 2550f0cfe17bSDaniel Bristot de Oliveira return; 2551f0cfe17bSDaniel Bristot de Oliveira 2552bce29ac9SDaniel Bristot de Oliveira trace_osnoise_callback_enabled = false; 2553c3b6343cSDaniel Bristot de Oliveira /* 2554c3b6343cSDaniel Bristot de Oliveira * Make sure that ftrace_nmi_enter/exit() see 2555c3b6343cSDaniel Bristot de Oliveira * trace_osnoise_callback_enabled as false before continuing. 2556c3b6343cSDaniel Bristot de Oliveira */ 2557bce29ac9SDaniel Bristot de Oliveira barrier(); 2558bce29ac9SDaniel Bristot de Oliveira 2559bce29ac9SDaniel Bristot de Oliveira stop_per_cpu_kthreads(); 2560bce29ac9SDaniel Bristot de Oliveira 25610878355bSNikita Yushchenko osnoise_unhook_events(); 2562bce29ac9SDaniel Bristot de Oliveira } 2563bce29ac9SDaniel Bristot de Oliveira 256415ca4bdbSDaniel Bristot de Oliveira static void osnoise_tracer_start(struct trace_array *tr) 256515ca4bdbSDaniel Bristot de Oliveira { 256615ca4bdbSDaniel Bristot de Oliveira int retval; 256715ca4bdbSDaniel Bristot de Oliveira 25682fac8d64SDaniel Bristot de Oliveira /* 25692fac8d64SDaniel Bristot de Oliveira * If the instance is already registered, there is no need to 25702fac8d64SDaniel Bristot de Oliveira * register it again. 25712fac8d64SDaniel Bristot de Oliveira */ 25722fac8d64SDaniel Bristot de Oliveira if (osnoise_instance_registered(tr)) 257315ca4bdbSDaniel Bristot de Oliveira return; 257415ca4bdbSDaniel Bristot de Oliveira 257515ca4bdbSDaniel Bristot de Oliveira retval = osnoise_workload_start(); 257615ca4bdbSDaniel Bristot de Oliveira if (retval) 257715ca4bdbSDaniel Bristot de Oliveira pr_err(BANNER "Error starting osnoise tracer\n"); 257815ca4bdbSDaniel Bristot de Oliveira 2579dae18134SDaniel Bristot de Oliveira osnoise_register_instance(tr); 258015ca4bdbSDaniel Bristot de Oliveira } 258115ca4bdbSDaniel Bristot de Oliveira 258215ca4bdbSDaniel Bristot de Oliveira static void osnoise_tracer_stop(struct trace_array *tr) 258315ca4bdbSDaniel Bristot de Oliveira { 2584dae18134SDaniel Bristot de Oliveira osnoise_unregister_instance(tr); 258515ca4bdbSDaniel Bristot de Oliveira osnoise_workload_stop(); 258615ca4bdbSDaniel Bristot de Oliveira } 258715ca4bdbSDaniel Bristot de Oliveira 2588bce29ac9SDaniel Bristot de Oliveira static int osnoise_tracer_init(struct trace_array *tr) 2589bce29ac9SDaniel Bristot de Oliveira { 25902fac8d64SDaniel Bristot de Oliveira /* 25912fac8d64SDaniel Bristot de Oliveira * Only allow osnoise tracer if timerlat tracer is not running 25922fac8d64SDaniel Bristot de Oliveira * already. 25932fac8d64SDaniel Bristot de Oliveira */ 25942fac8d64SDaniel Bristot de Oliveira if (timerlat_enabled()) 2595bce29ac9SDaniel Bristot de Oliveira return -EBUSY; 2596bce29ac9SDaniel Bristot de Oliveira 2597bce29ac9SDaniel Bristot de Oliveira tr->max_latency = 0; 2598bce29ac9SDaniel Bristot de Oliveira 2599bce29ac9SDaniel Bristot de Oliveira osnoise_tracer_start(tr); 2600bce29ac9SDaniel Bristot de Oliveira return 0; 2601bce29ac9SDaniel Bristot de Oliveira } 2602bce29ac9SDaniel Bristot de Oliveira 2603bce29ac9SDaniel Bristot de Oliveira static void osnoise_tracer_reset(struct trace_array *tr) 2604bce29ac9SDaniel Bristot de Oliveira { 2605bce29ac9SDaniel Bristot de Oliveira osnoise_tracer_stop(tr); 2606bce29ac9SDaniel Bristot de Oliveira } 2607bce29ac9SDaniel Bristot de Oliveira 2608bce29ac9SDaniel Bristot de Oliveira static struct tracer osnoise_tracer __read_mostly = { 2609bce29ac9SDaniel Bristot de Oliveira .name = "osnoise", 2610bce29ac9SDaniel Bristot de Oliveira .init = osnoise_tracer_init, 2611bce29ac9SDaniel Bristot de Oliveira .reset = osnoise_tracer_reset, 2612bce29ac9SDaniel Bristot de Oliveira .start = osnoise_tracer_start, 2613bce29ac9SDaniel Bristot de Oliveira .stop = osnoise_tracer_stop, 2614bce29ac9SDaniel Bristot de Oliveira .print_header = print_osnoise_headers, 2615bce29ac9SDaniel Bristot de Oliveira .allow_instances = true, 2616bce29ac9SDaniel Bristot de Oliveira }; 2617bce29ac9SDaniel Bristot de Oliveira 2618a955d7eaSDaniel Bristot de Oliveira #ifdef CONFIG_TIMERLAT_TRACER 2619a955d7eaSDaniel Bristot de Oliveira static void timerlat_tracer_start(struct trace_array *tr) 2620a955d7eaSDaniel Bristot de Oliveira { 2621a955d7eaSDaniel Bristot de Oliveira int retval; 2622a955d7eaSDaniel Bristot de Oliveira 26232fac8d64SDaniel Bristot de Oliveira /* 26242fac8d64SDaniel Bristot de Oliveira * If the instance is already registered, there is no need to 26252fac8d64SDaniel Bristot de Oliveira * register it again. 26262fac8d64SDaniel Bristot de Oliveira */ 26272fac8d64SDaniel Bristot de Oliveira if (osnoise_instance_registered(tr)) 2628a955d7eaSDaniel Bristot de Oliveira return; 2629a955d7eaSDaniel Bristot de Oliveira 263015ca4bdbSDaniel Bristot de Oliveira retval = osnoise_workload_start(); 2631a955d7eaSDaniel Bristot de Oliveira if (retval) 26322fac8d64SDaniel Bristot de Oliveira pr_err(BANNER "Error starting timerlat tracer\n"); 2633a955d7eaSDaniel Bristot de Oliveira 2634dae18134SDaniel Bristot de Oliveira osnoise_register_instance(tr); 2635dae18134SDaniel Bristot de Oliveira 2636a955d7eaSDaniel Bristot de Oliveira return; 2637a955d7eaSDaniel Bristot de Oliveira } 2638a955d7eaSDaniel Bristot de Oliveira 2639a955d7eaSDaniel Bristot de Oliveira static void timerlat_tracer_stop(struct trace_array *tr) 2640a955d7eaSDaniel Bristot de Oliveira { 2641a955d7eaSDaniel Bristot de Oliveira int cpu; 2642a955d7eaSDaniel Bristot de Oliveira 26432fac8d64SDaniel Bristot de Oliveira osnoise_unregister_instance(tr); 2644a955d7eaSDaniel Bristot de Oliveira 26452fac8d64SDaniel Bristot de Oliveira /* 26462fac8d64SDaniel Bristot de Oliveira * Instruct the threads to stop only if this is the last instance. 26472fac8d64SDaniel Bristot de Oliveira */ 26482fac8d64SDaniel Bristot de Oliveira if (!osnoise_has_registered_instances()) { 2649a955d7eaSDaniel Bristot de Oliveira for_each_online_cpu(cpu) 2650a955d7eaSDaniel Bristot de Oliveira per_cpu(per_cpu_osnoise_var, cpu).sampling = 0; 26512fac8d64SDaniel Bristot de Oliveira } 2652a955d7eaSDaniel Bristot de Oliveira 26532fac8d64SDaniel Bristot de Oliveira osnoise_workload_stop(); 2654a955d7eaSDaniel Bristot de Oliveira } 2655a955d7eaSDaniel Bristot de Oliveira 2656a955d7eaSDaniel Bristot de Oliveira static int timerlat_tracer_init(struct trace_array *tr) 2657a955d7eaSDaniel Bristot de Oliveira { 26582fac8d64SDaniel Bristot de Oliveira /* 26592fac8d64SDaniel Bristot de Oliveira * Only allow timerlat tracer if osnoise tracer is not running already. 26602fac8d64SDaniel Bristot de Oliveira */ 26612fac8d64SDaniel Bristot de Oliveira if (osnoise_has_registered_instances() && !osnoise_data.timerlat_tracer) 2662a955d7eaSDaniel Bristot de Oliveira return -EBUSY; 2663a955d7eaSDaniel Bristot de Oliveira 26642fac8d64SDaniel Bristot de Oliveira /* 26652fac8d64SDaniel Bristot de Oliveira * If this is the first instance, set timerlat_tracer to block 26662fac8d64SDaniel Bristot de Oliveira * osnoise tracer start. 26672fac8d64SDaniel Bristot de Oliveira */ 26682fac8d64SDaniel Bristot de Oliveira if (!osnoise_has_registered_instances()) 26692fac8d64SDaniel Bristot de Oliveira osnoise_data.timerlat_tracer = 1; 2670a955d7eaSDaniel Bristot de Oliveira 26712fac8d64SDaniel Bristot de Oliveira tr->max_latency = 0; 2672a955d7eaSDaniel Bristot de Oliveira timerlat_tracer_start(tr); 2673a955d7eaSDaniel Bristot de Oliveira 2674a955d7eaSDaniel Bristot de Oliveira return 0; 2675a955d7eaSDaniel Bristot de Oliveira } 2676a955d7eaSDaniel Bristot de Oliveira 2677a955d7eaSDaniel Bristot de Oliveira static void timerlat_tracer_reset(struct trace_array *tr) 2678a955d7eaSDaniel Bristot de Oliveira { 2679a955d7eaSDaniel Bristot de Oliveira timerlat_tracer_stop(tr); 26802fac8d64SDaniel Bristot de Oliveira 26812fac8d64SDaniel Bristot de Oliveira /* 26822fac8d64SDaniel Bristot de Oliveira * If this is the last instance, reset timerlat_tracer allowing 26832fac8d64SDaniel Bristot de Oliveira * osnoise to be started. 26842fac8d64SDaniel Bristot de Oliveira */ 26852fac8d64SDaniel Bristot de Oliveira if (!osnoise_has_registered_instances()) 26862fac8d64SDaniel Bristot de Oliveira osnoise_data.timerlat_tracer = 0; 2687a955d7eaSDaniel Bristot de Oliveira } 2688a955d7eaSDaniel Bristot de Oliveira 2689a955d7eaSDaniel Bristot de Oliveira static struct tracer timerlat_tracer __read_mostly = { 2690a955d7eaSDaniel Bristot de Oliveira .name = "timerlat", 2691a955d7eaSDaniel Bristot de Oliveira .init = timerlat_tracer_init, 2692a955d7eaSDaniel Bristot de Oliveira .reset = timerlat_tracer_reset, 2693a955d7eaSDaniel Bristot de Oliveira .start = timerlat_tracer_start, 2694a955d7eaSDaniel Bristot de Oliveira .stop = timerlat_tracer_stop, 2695a955d7eaSDaniel Bristot de Oliveira .print_header = print_timerlat_headers, 2696a955d7eaSDaniel Bristot de Oliveira .allow_instances = true, 2697a955d7eaSDaniel Bristot de Oliveira }; 2698ccb67544SDaniel Bristot de Oliveira 2699ccb67544SDaniel Bristot de Oliveira __init static int init_timerlat_tracer(void) 2700ccb67544SDaniel Bristot de Oliveira { 2701ccb67544SDaniel Bristot de Oliveira return register_tracer(&timerlat_tracer); 2702ccb67544SDaniel Bristot de Oliveira } 2703ccb67544SDaniel Bristot de Oliveira #else /* CONFIG_TIMERLAT_TRACER */ 2704ccb67544SDaniel Bristot de Oliveira __init static int init_timerlat_tracer(void) 2705ccb67544SDaniel Bristot de Oliveira { 2706ccb67544SDaniel Bristot de Oliveira return 0; 2707ccb67544SDaniel Bristot de Oliveira } 2708a955d7eaSDaniel Bristot de Oliveira #endif /* CONFIG_TIMERLAT_TRACER */ 2709a955d7eaSDaniel Bristot de Oliveira 2710bce29ac9SDaniel Bristot de Oliveira __init static int init_osnoise_tracer(void) 2711bce29ac9SDaniel Bristot de Oliveira { 2712bce29ac9SDaniel Bristot de Oliveira int ret; 2713bce29ac9SDaniel Bristot de Oliveira 2714bce29ac9SDaniel Bristot de Oliveira mutex_init(&interface_lock); 2715bce29ac9SDaniel Bristot de Oliveira 2716bce29ac9SDaniel Bristot de Oliveira cpumask_copy(&osnoise_cpumask, cpu_all_mask); 2717bce29ac9SDaniel Bristot de Oliveira 2718bce29ac9SDaniel Bristot de Oliveira ret = register_tracer(&osnoise_tracer); 2719a955d7eaSDaniel Bristot de Oliveira if (ret) { 2720a955d7eaSDaniel Bristot de Oliveira pr_err(BANNER "Error registering osnoise!\n"); 2721bce29ac9SDaniel Bristot de Oliveira return ret; 2722a955d7eaSDaniel Bristot de Oliveira } 2723a955d7eaSDaniel Bristot de Oliveira 2724ccb67544SDaniel Bristot de Oliveira ret = init_timerlat_tracer(); 2725a955d7eaSDaniel Bristot de Oliveira if (ret) { 2726ccb67544SDaniel Bristot de Oliveira pr_err(BANNER "Error registering timerlat!\n"); 2727a955d7eaSDaniel Bristot de Oliveira return ret; 2728a955d7eaSDaniel Bristot de Oliveira } 2729ccb67544SDaniel Bristot de Oliveira 2730c8895e27SDaniel Bristot de Oliveira osnoise_init_hotplug_support(); 2731bce29ac9SDaniel Bristot de Oliveira 2732dae18134SDaniel Bristot de Oliveira INIT_LIST_HEAD_RCU(&osnoise_instances); 2733dae18134SDaniel Bristot de Oliveira 2734bce29ac9SDaniel Bristot de Oliveira init_tracefs(); 2735bce29ac9SDaniel Bristot de Oliveira 2736bce29ac9SDaniel Bristot de Oliveira return 0; 2737bce29ac9SDaniel Bristot de Oliveira } 2738bce29ac9SDaniel Bristot de Oliveira late_initcall(init_osnoise_tracer); 2739