Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * OS Noise Tracer: computes the OS Noise suffered by a running thread. |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 4 | * Timerlat Tracer: measures the wakeup latency of a timer triggered IRQ and thread. |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 5 | * |
| 6 | * Based on "hwlat_detector" tracer by: |
| 7 | * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com> |
| 8 | * Copyright (C) 2013-2016 Steven Rostedt, Red Hat, Inc. <srostedt@redhat.com> |
| 9 | * With feedback from Clark Williams <williams@redhat.com> |
| 10 | * |
| 11 | * And also based on the rtsl tracer presented on: |
| 12 | * DE OLIVEIRA, Daniel Bristot, et al. Demystifying the real-time linux |
| 13 | * scheduling latency. In: 32nd Euromicro Conference on Real-Time Systems |
| 14 | * (ECRTS 2020). Schloss Dagstuhl-Leibniz-Zentrum fur Informatik, 2020. |
| 15 | * |
| 16 | * Copyright (C) 2021 Daniel Bristot de Oliveira, Red Hat, Inc. <bristot@redhat.com> |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kthread.h> |
| 20 | #include <linux/tracefs.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/cpumask.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/sched/clock.h> |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 25 | #include <uapi/linux/sched/types.h> |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 26 | #include <linux/sched.h> |
| 27 | #include "trace.h" |
| 28 | |
| 29 | #ifdef CONFIG_X86_LOCAL_APIC |
| 30 | #include <asm/trace/irq_vectors.h> |
| 31 | #undef TRACE_INCLUDE_PATH |
| 32 | #undef TRACE_INCLUDE_FILE |
| 33 | #endif /* CONFIG_X86_LOCAL_APIC */ |
| 34 | |
| 35 | #include <trace/events/irq.h> |
| 36 | #include <trace/events/sched.h> |
| 37 | |
| 38 | #define CREATE_TRACE_POINTS |
| 39 | #include <trace/events/osnoise.h> |
| 40 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 41 | /* |
| 42 | * Default values. |
| 43 | */ |
| 44 | #define BANNER "osnoise: " |
| 45 | #define DEFAULT_SAMPLE_PERIOD 1000000 /* 1s */ |
| 46 | #define DEFAULT_SAMPLE_RUNTIME 1000000 /* 1s */ |
| 47 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 48 | #define DEFAULT_TIMERLAT_PERIOD 1000 /* 1ms */ |
| 49 | #define DEFAULT_TIMERLAT_PRIO 95 /* FIFO 95 */ |
| 50 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 51 | /* |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 52 | * trace_array of the enabled osnoise/timerlat instances. |
| 53 | */ |
| 54 | struct osnoise_instance { |
| 55 | struct list_head list; |
| 56 | struct trace_array *tr; |
| 57 | }; |
Daniel Bristot de Oliveira | d7458bc | 2021-11-11 23:07:42 +0100 | [diff] [blame] | 58 | |
| 59 | static struct list_head osnoise_instances; |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 60 | |
| 61 | static bool osnoise_has_registered_instances(void) |
| 62 | { |
| 63 | return !!list_first_or_null_rcu(&osnoise_instances, |
| 64 | struct osnoise_instance, |
| 65 | list); |
| 66 | } |
| 67 | |
| 68 | /* |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 69 | * osnoise_instance_registered - check if a tr is already registered |
| 70 | */ |
| 71 | static int osnoise_instance_registered(struct trace_array *tr) |
| 72 | { |
| 73 | struct osnoise_instance *inst; |
| 74 | int found = 0; |
| 75 | |
| 76 | rcu_read_lock(); |
| 77 | list_for_each_entry_rcu(inst, &osnoise_instances, list) { |
| 78 | if (inst->tr == tr) |
| 79 | found = 1; |
| 80 | } |
| 81 | rcu_read_unlock(); |
| 82 | |
| 83 | return found; |
| 84 | } |
| 85 | |
| 86 | /* |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 87 | * osnoise_register_instance - register a new trace instance |
| 88 | * |
| 89 | * Register a trace_array *tr in the list of instances running |
| 90 | * osnoise/timerlat tracers. |
| 91 | */ |
| 92 | static int osnoise_register_instance(struct trace_array *tr) |
| 93 | { |
| 94 | struct osnoise_instance *inst; |
| 95 | |
| 96 | /* |
| 97 | * register/unregister serialization is provided by trace's |
| 98 | * trace_types_lock. |
| 99 | */ |
| 100 | lockdep_assert_held(&trace_types_lock); |
| 101 | |
| 102 | inst = kmalloc(sizeof(*inst), GFP_KERNEL); |
| 103 | if (!inst) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | INIT_LIST_HEAD_RCU(&inst->list); |
| 107 | inst->tr = tr; |
| 108 | list_add_tail_rcu(&inst->list, &osnoise_instances); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * osnoise_unregister_instance - unregister a registered trace instance |
| 115 | * |
| 116 | * Remove the trace_array *tr from the list of instances running |
| 117 | * osnoise/timerlat tracers. |
| 118 | */ |
| 119 | static void osnoise_unregister_instance(struct trace_array *tr) |
| 120 | { |
| 121 | struct osnoise_instance *inst; |
| 122 | int found = 0; |
| 123 | |
| 124 | /* |
| 125 | * register/unregister serialization is provided by trace's |
| 126 | * trace_types_lock. |
| 127 | */ |
| 128 | lockdep_assert_held(&trace_types_lock); |
| 129 | |
| 130 | list_for_each_entry_rcu(inst, &osnoise_instances, list) { |
| 131 | if (inst->tr == tr) { |
| 132 | list_del_rcu(&inst->list); |
| 133 | found = 1; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (!found) |
| 139 | return; |
| 140 | |
Uladzislau Rezki (Sony) | a6ed2ae | 2021-11-24 12:03:08 +0100 | [diff] [blame] | 141 | kvfree_rcu(inst); |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 145 | * NMI runtime info. |
| 146 | */ |
| 147 | struct osn_nmi { |
| 148 | u64 count; |
| 149 | u64 delta_start; |
| 150 | }; |
| 151 | |
| 152 | /* |
| 153 | * IRQ runtime info. |
| 154 | */ |
| 155 | struct osn_irq { |
| 156 | u64 count; |
| 157 | u64 arrival_time; |
| 158 | u64 delta_start; |
| 159 | }; |
| 160 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 161 | #define IRQ_CONTEXT 0 |
| 162 | #define THREAD_CONTEXT 1 |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 163 | /* |
| 164 | * sofirq runtime info. |
| 165 | */ |
| 166 | struct osn_softirq { |
| 167 | u64 count; |
| 168 | u64 arrival_time; |
| 169 | u64 delta_start; |
| 170 | }; |
| 171 | |
| 172 | /* |
| 173 | * thread runtime info. |
| 174 | */ |
| 175 | struct osn_thread { |
| 176 | u64 count; |
| 177 | u64 arrival_time; |
| 178 | u64 delta_start; |
| 179 | }; |
| 180 | |
| 181 | /* |
| 182 | * Runtime information: this structure saves the runtime information used by |
| 183 | * one sampling thread. |
| 184 | */ |
| 185 | struct osnoise_variables { |
| 186 | struct task_struct *kthread; |
| 187 | bool sampling; |
| 188 | pid_t pid; |
| 189 | struct osn_nmi nmi; |
| 190 | struct osn_irq irq; |
| 191 | struct osn_softirq softirq; |
| 192 | struct osn_thread thread; |
| 193 | local_t int_counter; |
| 194 | }; |
| 195 | |
| 196 | /* |
| 197 | * Per-cpu runtime information. |
| 198 | */ |
| 199 | DEFINE_PER_CPU(struct osnoise_variables, per_cpu_osnoise_var); |
| 200 | |
| 201 | /* |
| 202 | * this_cpu_osn_var - Return the per-cpu osnoise_variables on its relative CPU |
| 203 | */ |
| 204 | static inline struct osnoise_variables *this_cpu_osn_var(void) |
| 205 | { |
| 206 | return this_cpu_ptr(&per_cpu_osnoise_var); |
| 207 | } |
| 208 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 209 | #ifdef CONFIG_TIMERLAT_TRACER |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 210 | /* |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 211 | * Runtime information for the timer mode. |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 212 | */ |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 213 | struct timerlat_variables { |
| 214 | struct task_struct *kthread; |
| 215 | struct hrtimer timer; |
| 216 | u64 rel_period; |
| 217 | u64 abs_period; |
| 218 | bool tracing_thread; |
| 219 | u64 count; |
| 220 | }; |
| 221 | |
| 222 | DEFINE_PER_CPU(struct timerlat_variables, per_cpu_timerlat_var); |
| 223 | |
| 224 | /* |
| 225 | * this_cpu_tmr_var - Return the per-cpu timerlat_variables on its relative CPU |
| 226 | */ |
| 227 | static inline struct timerlat_variables *this_cpu_tmr_var(void) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 228 | { |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 229 | return this_cpu_ptr(&per_cpu_timerlat_var); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * tlat_var_reset - Reset the values of the given timerlat_variables |
| 234 | */ |
| 235 | static inline void tlat_var_reset(void) |
| 236 | { |
| 237 | struct timerlat_variables *tlat_var; |
| 238 | int cpu; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 239 | /* |
| 240 | * So far, all the values are initialized as 0, so |
| 241 | * zeroing the structure is perfect. |
| 242 | */ |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 243 | for_each_cpu(cpu, cpu_online_mask) { |
| 244 | tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu); |
| 245 | memset(tlat_var, 0, sizeof(*tlat_var)); |
| 246 | } |
| 247 | } |
| 248 | #else /* CONFIG_TIMERLAT_TRACER */ |
| 249 | #define tlat_var_reset() do {} while (0) |
| 250 | #endif /* CONFIG_TIMERLAT_TRACER */ |
| 251 | |
| 252 | /* |
| 253 | * osn_var_reset - Reset the values of the given osnoise_variables |
| 254 | */ |
| 255 | static inline void osn_var_reset(void) |
| 256 | { |
| 257 | struct osnoise_variables *osn_var; |
| 258 | int cpu; |
| 259 | |
| 260 | /* |
| 261 | * So far, all the values are initialized as 0, so |
| 262 | * zeroing the structure is perfect. |
| 263 | */ |
| 264 | for_each_cpu(cpu, cpu_online_mask) { |
| 265 | osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu); |
| 266 | memset(osn_var, 0, sizeof(*osn_var)); |
| 267 | } |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
| 271 | * osn_var_reset_all - Reset the value of all per-cpu osnoise_variables |
| 272 | */ |
| 273 | static inline void osn_var_reset_all(void) |
| 274 | { |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 275 | osn_var_reset(); |
| 276 | tlat_var_reset(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Tells NMIs to call back to the osnoise tracer to record timestamps. |
| 281 | */ |
| 282 | bool trace_osnoise_callback_enabled; |
| 283 | |
| 284 | /* |
| 285 | * osnoise sample structure definition. Used to store the statistics of a |
| 286 | * sample run. |
| 287 | */ |
| 288 | struct osnoise_sample { |
| 289 | u64 runtime; /* runtime */ |
| 290 | u64 noise; /* noise */ |
| 291 | u64 max_sample; /* max single noise sample */ |
| 292 | int hw_count; /* # HW (incl. hypervisor) interference */ |
| 293 | int nmi_count; /* # NMIs during this sample */ |
| 294 | int irq_count; /* # IRQs during this sample */ |
| 295 | int softirq_count; /* # softirqs during this sample */ |
| 296 | int thread_count; /* # threads during this sample */ |
| 297 | }; |
| 298 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 299 | #ifdef CONFIG_TIMERLAT_TRACER |
| 300 | /* |
| 301 | * timerlat sample structure definition. Used to store the statistics of |
| 302 | * a sample run. |
| 303 | */ |
| 304 | struct timerlat_sample { |
| 305 | u64 timer_latency; /* timer_latency */ |
| 306 | unsigned int seqnum; /* unique sequence */ |
| 307 | int context; /* timer context */ |
| 308 | }; |
| 309 | #endif |
| 310 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 311 | /* |
| 312 | * Protect the interface. |
| 313 | */ |
| 314 | struct mutex interface_lock; |
| 315 | |
| 316 | /* |
| 317 | * Tracer data. |
| 318 | */ |
| 319 | static struct osnoise_data { |
| 320 | u64 sample_period; /* total sampling period */ |
| 321 | u64 sample_runtime; /* active sampling portion of period */ |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 322 | u64 stop_tracing; /* stop trace in the internal operation (loop/irq) */ |
| 323 | u64 stop_tracing_total; /* stop trace in the final operation (report/thread) */ |
| 324 | #ifdef CONFIG_TIMERLAT_TRACER |
| 325 | u64 timerlat_period; /* timerlat period */ |
| 326 | u64 print_stack; /* print IRQ stack if total > */ |
| 327 | int timerlat_tracer; /* timerlat tracer */ |
| 328 | #endif |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 329 | bool tainted; /* infor users and developers about a problem */ |
| 330 | } osnoise_data = { |
| 331 | .sample_period = DEFAULT_SAMPLE_PERIOD, |
| 332 | .sample_runtime = DEFAULT_SAMPLE_RUNTIME, |
| 333 | .stop_tracing = 0, |
| 334 | .stop_tracing_total = 0, |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 335 | #ifdef CONFIG_TIMERLAT_TRACER |
| 336 | .print_stack = 0, |
| 337 | .timerlat_period = DEFAULT_TIMERLAT_PERIOD, |
| 338 | .timerlat_tracer = 0, |
| 339 | #endif |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 340 | }; |
| 341 | |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 342 | #ifdef CONFIG_TIMERLAT_TRACER |
| 343 | static inline bool timerlat_enabled(void) |
| 344 | { |
| 345 | return osnoise_data.timerlat_tracer; |
| 346 | } |
| 347 | |
| 348 | static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var) |
| 349 | { |
| 350 | struct timerlat_variables *tlat_var = this_cpu_tmr_var(); |
| 351 | /* |
| 352 | * If the timerlat is enabled, but the irq handler did |
| 353 | * not run yet enabling timerlat_tracer, do not trace. |
| 354 | */ |
| 355 | if (!tlat_var->tracing_thread) { |
| 356 | osn_var->softirq.arrival_time = 0; |
| 357 | osn_var->softirq.delta_start = 0; |
| 358 | return 0; |
| 359 | } |
| 360 | return 1; |
| 361 | } |
| 362 | |
| 363 | static inline int timerlat_thread_exit(struct osnoise_variables *osn_var) |
| 364 | { |
| 365 | struct timerlat_variables *tlat_var = this_cpu_tmr_var(); |
| 366 | /* |
| 367 | * If the timerlat is enabled, but the irq handler did |
| 368 | * not run yet enabling timerlat_tracer, do not trace. |
| 369 | */ |
| 370 | if (!tlat_var->tracing_thread) { |
| 371 | osn_var->thread.delta_start = 0; |
| 372 | osn_var->thread.arrival_time = 0; |
| 373 | return 0; |
| 374 | } |
| 375 | return 1; |
| 376 | } |
| 377 | #else /* CONFIG_TIMERLAT_TRACER */ |
| 378 | static inline bool timerlat_enabled(void) |
| 379 | { |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var) |
| 384 | { |
| 385 | return 1; |
| 386 | } |
| 387 | static inline int timerlat_thread_exit(struct osnoise_variables *osn_var) |
| 388 | { |
| 389 | return 1; |
| 390 | } |
| 391 | #endif |
| 392 | |
Daniel Bristot de Oliveira | d03721a | 2021-07-18 11:07:53 +0200 | [diff] [blame] | 393 | #ifdef CONFIG_PREEMPT_RT |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 394 | /* |
| 395 | * Print the osnoise header info. |
| 396 | */ |
| 397 | static void print_osnoise_headers(struct seq_file *s) |
| 398 | { |
| 399 | if (osnoise_data.tainted) |
| 400 | seq_puts(s, "# osnoise is tainted!\n"); |
| 401 | |
Daniel Bristot de Oliveira | d03721a | 2021-07-18 11:07:53 +0200 | [diff] [blame] | 402 | seq_puts(s, "# _-------=> irqs-off\n"); |
| 403 | seq_puts(s, "# / _------=> need-resched\n"); |
| 404 | seq_puts(s, "# | / _-----=> need-resched-lazy\n"); |
| 405 | seq_puts(s, "# || / _----=> hardirq/softirq\n"); |
| 406 | seq_puts(s, "# ||| / _---=> preempt-depth\n"); |
| 407 | seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n"); |
| 408 | seq_puts(s, "# ||||| / _-=> migrate-disable\n"); |
| 409 | |
| 410 | seq_puts(s, "# |||||| / "); |
| 411 | seq_puts(s, " MAX\n"); |
| 412 | |
| 413 | seq_puts(s, "# ||||| / "); |
| 414 | seq_puts(s, " SINGLE Interference counters:\n"); |
| 415 | |
| 416 | seq_puts(s, "# ||||||| RUNTIME "); |
| 417 | seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n"); |
| 418 | |
| 419 | seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP IN US "); |
| 420 | seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n"); |
| 421 | |
| 422 | seq_puts(s, "# | | | ||||||| | | "); |
| 423 | seq_puts(s, " | | | | | | | |\n"); |
| 424 | } |
| 425 | #else /* CONFIG_PREEMPT_RT */ |
| 426 | static void print_osnoise_headers(struct seq_file *s) |
| 427 | { |
| 428 | if (osnoise_data.tainted) |
| 429 | seq_puts(s, "# osnoise is tainted!\n"); |
| 430 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 431 | seq_puts(s, "# _-----=> irqs-off\n"); |
| 432 | seq_puts(s, "# / _----=> need-resched\n"); |
| 433 | seq_puts(s, "# | / _---=> hardirq/softirq\n"); |
Daniel Bristot de Oliveira | e0f3b18 | 2021-10-15 17:07:50 +0200 | [diff] [blame] | 434 | seq_puts(s, "# || / _--=> preempt-depth\n"); |
| 435 | seq_puts(s, "# ||| / _-=> migrate-disable "); |
| 436 | seq_puts(s, " MAX\n"); |
| 437 | seq_puts(s, "# |||| / delay "); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 438 | seq_puts(s, " SINGLE Interference counters:\n"); |
| 439 | |
Daniel Bristot de Oliveira | e0f3b18 | 2021-10-15 17:07:50 +0200 | [diff] [blame] | 440 | seq_puts(s, "# ||||| RUNTIME "); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 441 | seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n"); |
| 442 | |
Daniel Bristot de Oliveira | e0f3b18 | 2021-10-15 17:07:50 +0200 | [diff] [blame] | 443 | seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP IN US "); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 444 | seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n"); |
| 445 | |
Daniel Bristot de Oliveira | e0f3b18 | 2021-10-15 17:07:50 +0200 | [diff] [blame] | 446 | seq_puts(s, "# | | | ||||| | | "); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 447 | seq_puts(s, " | | | | | | | |\n"); |
| 448 | } |
Daniel Bristot de Oliveira | d03721a | 2021-07-18 11:07:53 +0200 | [diff] [blame] | 449 | #endif /* CONFIG_PREEMPT_RT */ |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 450 | |
| 451 | /* |
| 452 | * osnoise_taint - report an osnoise error. |
| 453 | */ |
| 454 | #define osnoise_taint(msg) ({ \ |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 455 | struct osnoise_instance *inst; \ |
| 456 | struct trace_buffer *buffer; \ |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 457 | \ |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 458 | rcu_read_lock(); \ |
| 459 | list_for_each_entry_rcu(inst, &osnoise_instances, list) { \ |
| 460 | buffer = inst->tr->array_buffer.buffer; \ |
| 461 | trace_array_printk_buf(buffer, _THIS_IP_, msg); \ |
| 462 | } \ |
| 463 | rcu_read_unlock(); \ |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 464 | osnoise_data.tainted = true; \ |
| 465 | }) |
| 466 | |
| 467 | /* |
| 468 | * Record an osnoise_sample into the tracer buffer. |
| 469 | */ |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 470 | static void |
| 471 | __trace_osnoise_sample(struct osnoise_sample *sample, struct trace_buffer *buffer) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 472 | { |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 473 | struct trace_event_call *call = &event_osnoise; |
| 474 | struct ring_buffer_event *event; |
| 475 | struct osnoise_entry *entry; |
| 476 | |
| 477 | event = trace_buffer_lock_reserve(buffer, TRACE_OSNOISE, sizeof(*entry), |
| 478 | tracing_gen_ctx()); |
| 479 | if (!event) |
| 480 | return; |
| 481 | entry = ring_buffer_event_data(event); |
| 482 | entry->runtime = sample->runtime; |
| 483 | entry->noise = sample->noise; |
| 484 | entry->max_sample = sample->max_sample; |
| 485 | entry->hw_count = sample->hw_count; |
| 486 | entry->nmi_count = sample->nmi_count; |
| 487 | entry->irq_count = sample->irq_count; |
| 488 | entry->softirq_count = sample->softirq_count; |
| 489 | entry->thread_count = sample->thread_count; |
| 490 | |
| 491 | if (!call_filter_check_discard(call, entry, buffer, event)) |
| 492 | trace_buffer_unlock_commit_nostack(buffer, event); |
| 493 | } |
| 494 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 495 | /* |
| 496 | * Record an osnoise_sample on all osnoise instances. |
| 497 | */ |
| 498 | static void trace_osnoise_sample(struct osnoise_sample *sample) |
| 499 | { |
| 500 | struct osnoise_instance *inst; |
| 501 | struct trace_buffer *buffer; |
| 502 | |
| 503 | rcu_read_lock(); |
| 504 | list_for_each_entry_rcu(inst, &osnoise_instances, list) { |
| 505 | buffer = inst->tr->array_buffer.buffer; |
| 506 | __trace_osnoise_sample(sample, buffer); |
| 507 | } |
| 508 | rcu_read_unlock(); |
| 509 | } |
| 510 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 511 | #ifdef CONFIG_TIMERLAT_TRACER |
| 512 | /* |
| 513 | * Print the timerlat header info. |
| 514 | */ |
Daniel Bristot de Oliveira | e1c4ad4 | 2021-07-18 11:07:54 +0200 | [diff] [blame] | 515 | #ifdef CONFIG_PREEMPT_RT |
| 516 | static void print_timerlat_headers(struct seq_file *s) |
| 517 | { |
| 518 | seq_puts(s, "# _-------=> irqs-off\n"); |
| 519 | seq_puts(s, "# / _------=> need-resched\n"); |
| 520 | seq_puts(s, "# | / _-----=> need-resched-lazy\n"); |
| 521 | seq_puts(s, "# || / _----=> hardirq/softirq\n"); |
| 522 | seq_puts(s, "# ||| / _---=> preempt-depth\n"); |
| 523 | seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n"); |
| 524 | seq_puts(s, "# ||||| / _-=> migrate-disable\n"); |
| 525 | seq_puts(s, "# |||||| /\n"); |
| 526 | seq_puts(s, "# ||||||| ACTIVATION\n"); |
| 527 | seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP ID "); |
| 528 | seq_puts(s, " CONTEXT LATENCY\n"); |
| 529 | seq_puts(s, "# | | | ||||||| | | "); |
| 530 | seq_puts(s, " | |\n"); |
| 531 | } |
| 532 | #else /* CONFIG_PREEMPT_RT */ |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 533 | static void print_timerlat_headers(struct seq_file *s) |
| 534 | { |
| 535 | seq_puts(s, "# _-----=> irqs-off\n"); |
| 536 | seq_puts(s, "# / _----=> need-resched\n"); |
| 537 | seq_puts(s, "# | / _---=> hardirq/softirq\n"); |
| 538 | seq_puts(s, "# || / _--=> preempt-depth\n"); |
Daniel Bristot de Oliveira | aeafcb8 | 2021-10-15 17:07:51 +0200 | [diff] [blame] | 539 | seq_puts(s, "# ||| / _-=> migrate-disable\n"); |
| 540 | seq_puts(s, "# |||| / delay\n"); |
| 541 | seq_puts(s, "# ||||| ACTIVATION\n"); |
| 542 | seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP ID "); |
| 543 | seq_puts(s, " CONTEXT LATENCY\n"); |
| 544 | seq_puts(s, "# | | | ||||| | | "); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 545 | seq_puts(s, " | |\n"); |
| 546 | } |
Daniel Bristot de Oliveira | e1c4ad4 | 2021-07-18 11:07:54 +0200 | [diff] [blame] | 547 | #endif /* CONFIG_PREEMPT_RT */ |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 548 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 549 | static void |
| 550 | __trace_timerlat_sample(struct timerlat_sample *sample, struct trace_buffer *buffer) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 551 | { |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 552 | struct trace_event_call *call = &event_osnoise; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 553 | struct ring_buffer_event *event; |
| 554 | struct timerlat_entry *entry; |
| 555 | |
| 556 | event = trace_buffer_lock_reserve(buffer, TRACE_TIMERLAT, sizeof(*entry), |
| 557 | tracing_gen_ctx()); |
| 558 | if (!event) |
| 559 | return; |
| 560 | entry = ring_buffer_event_data(event); |
| 561 | entry->seqnum = sample->seqnum; |
| 562 | entry->context = sample->context; |
| 563 | entry->timer_latency = sample->timer_latency; |
| 564 | |
| 565 | if (!call_filter_check_discard(call, entry, buffer, event)) |
| 566 | trace_buffer_unlock_commit_nostack(buffer, event); |
| 567 | } |
| 568 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 569 | /* |
| 570 | * Record an timerlat_sample into the tracer buffer. |
| 571 | */ |
| 572 | static void trace_timerlat_sample(struct timerlat_sample *sample) |
| 573 | { |
| 574 | struct osnoise_instance *inst; |
| 575 | struct trace_buffer *buffer; |
| 576 | |
| 577 | rcu_read_lock(); |
| 578 | list_for_each_entry_rcu(inst, &osnoise_instances, list) { |
| 579 | buffer = inst->tr->array_buffer.buffer; |
| 580 | __trace_timerlat_sample(sample, buffer); |
| 581 | } |
| 582 | rcu_read_unlock(); |
| 583 | } |
| 584 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 585 | #ifdef CONFIG_STACKTRACE |
| 586 | |
| 587 | #define MAX_CALLS 256 |
| 588 | |
| 589 | /* |
| 590 | * Stack trace will take place only at IRQ level, so, no need |
| 591 | * to control nesting here. |
| 592 | */ |
| 593 | struct trace_stack { |
| 594 | int stack_size; |
| 595 | int nr_entries; |
| 596 | unsigned long calls[MAX_CALLS]; |
| 597 | }; |
| 598 | |
| 599 | static DEFINE_PER_CPU(struct trace_stack, trace_stack); |
| 600 | |
| 601 | /* |
| 602 | * timerlat_save_stack - save a stack trace without printing |
| 603 | * |
| 604 | * Save the current stack trace without printing. The |
| 605 | * stack will be printed later, after the end of the measurement. |
| 606 | */ |
| 607 | static void timerlat_save_stack(int skip) |
| 608 | { |
| 609 | unsigned int size, nr_entries; |
| 610 | struct trace_stack *fstack; |
| 611 | |
| 612 | fstack = this_cpu_ptr(&trace_stack); |
| 613 | |
| 614 | size = ARRAY_SIZE(fstack->calls); |
| 615 | |
| 616 | nr_entries = stack_trace_save(fstack->calls, size, skip); |
| 617 | |
| 618 | fstack->stack_size = nr_entries * sizeof(unsigned long); |
| 619 | fstack->nr_entries = nr_entries; |
| 620 | |
| 621 | return; |
| 622 | |
| 623 | } |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 624 | |
| 625 | static void |
| 626 | __timerlat_dump_stack(struct trace_buffer *buffer, struct trace_stack *fstack, unsigned int size) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 627 | { |
| 628 | struct trace_event_call *call = &event_osnoise; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 629 | struct ring_buffer_event *event; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 630 | struct stack_entry *entry; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 631 | |
| 632 | event = trace_buffer_lock_reserve(buffer, TRACE_STACK, sizeof(*entry) + size, |
| 633 | tracing_gen_ctx()); |
| 634 | if (!event) |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 635 | return; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 636 | |
| 637 | entry = ring_buffer_event_data(event); |
| 638 | |
| 639 | memcpy(&entry->caller, fstack->calls, size); |
| 640 | entry->size = fstack->nr_entries; |
| 641 | |
| 642 | if (!call_filter_check_discard(call, entry, buffer, event)) |
| 643 | trace_buffer_unlock_commit_nostack(buffer, event); |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 644 | } |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 645 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 646 | /* |
| 647 | * timerlat_dump_stack - dump a stack trace previously saved |
| 648 | */ |
Daniel Bristot de Oliveira | b14f456 | 2021-10-31 19:05:03 +0100 | [diff] [blame] | 649 | static void timerlat_dump_stack(u64 latency) |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 650 | { |
| 651 | struct osnoise_instance *inst; |
| 652 | struct trace_buffer *buffer; |
| 653 | struct trace_stack *fstack; |
| 654 | unsigned int size; |
| 655 | |
Daniel Bristot de Oliveira | b14f456 | 2021-10-31 19:05:03 +0100 | [diff] [blame] | 656 | /* |
| 657 | * trace only if latency > print_stack config, if enabled. |
| 658 | */ |
| 659 | if (!osnoise_data.print_stack || osnoise_data.print_stack > latency) |
| 660 | return; |
| 661 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 662 | preempt_disable_notrace(); |
| 663 | fstack = this_cpu_ptr(&trace_stack); |
| 664 | size = fstack->stack_size; |
| 665 | |
| 666 | rcu_read_lock(); |
| 667 | list_for_each_entry_rcu(inst, &osnoise_instances, list) { |
| 668 | buffer = inst->tr->array_buffer.buffer; |
| 669 | __timerlat_dump_stack(buffer, fstack, size); |
| 670 | |
| 671 | } |
| 672 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 673 | preempt_enable_notrace(); |
| 674 | } |
Daniel Bristot de Oliveira | b14f456 | 2021-10-31 19:05:03 +0100 | [diff] [blame] | 675 | #else /* CONFIG_STACKTRACE */ |
| 676 | #define timerlat_dump_stack(u64 latency) do {} while (0) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 677 | #define timerlat_save_stack(a) do {} while (0) |
| 678 | #endif /* CONFIG_STACKTRACE */ |
| 679 | #endif /* CONFIG_TIMERLAT_TRACER */ |
| 680 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 681 | /* |
| 682 | * Macros to encapsulate the time capturing infrastructure. |
| 683 | */ |
| 684 | #define time_get() trace_clock_local() |
| 685 | #define time_to_us(x) div_u64(x, 1000) |
| 686 | #define time_sub(a, b) ((a) - (b)) |
| 687 | |
| 688 | /* |
| 689 | * cond_move_irq_delta_start - Forward the delta_start of a running IRQ |
| 690 | * |
| 691 | * If an IRQ is preempted by an NMI, its delta_start is pushed forward |
| 692 | * to discount the NMI interference. |
| 693 | * |
| 694 | * See get_int_safe_duration(). |
| 695 | */ |
| 696 | static inline void |
| 697 | cond_move_irq_delta_start(struct osnoise_variables *osn_var, u64 duration) |
| 698 | { |
| 699 | if (osn_var->irq.delta_start) |
| 700 | osn_var->irq.delta_start += duration; |
| 701 | } |
| 702 | |
| 703 | #ifndef CONFIG_PREEMPT_RT |
| 704 | /* |
| 705 | * cond_move_softirq_delta_start - Forward the delta_start of a running softirq. |
| 706 | * |
| 707 | * If a softirq is preempted by an IRQ or NMI, its delta_start is pushed |
| 708 | * forward to discount the interference. |
| 709 | * |
| 710 | * See get_int_safe_duration(). |
| 711 | */ |
| 712 | static inline void |
| 713 | cond_move_softirq_delta_start(struct osnoise_variables *osn_var, u64 duration) |
| 714 | { |
| 715 | if (osn_var->softirq.delta_start) |
| 716 | osn_var->softirq.delta_start += duration; |
| 717 | } |
| 718 | #else /* CONFIG_PREEMPT_RT */ |
| 719 | #define cond_move_softirq_delta_start(osn_var, duration) do {} while (0) |
| 720 | #endif |
| 721 | |
| 722 | /* |
| 723 | * cond_move_thread_delta_start - Forward the delta_start of a running thread |
| 724 | * |
| 725 | * If a noisy thread is preempted by an softirq, IRQ or NMI, its delta_start |
| 726 | * is pushed forward to discount the interference. |
| 727 | * |
| 728 | * See get_int_safe_duration(). |
| 729 | */ |
| 730 | static inline void |
| 731 | cond_move_thread_delta_start(struct osnoise_variables *osn_var, u64 duration) |
| 732 | { |
| 733 | if (osn_var->thread.delta_start) |
| 734 | osn_var->thread.delta_start += duration; |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * get_int_safe_duration - Get the duration of a window |
| 739 | * |
| 740 | * The irq, softirq and thread varaibles need to have its duration without |
| 741 | * the interference from higher priority interrupts. Instead of keeping a |
| 742 | * variable to discount the interrupt interference from these variables, the |
| 743 | * starting time of these variables are pushed forward with the interrupt's |
| 744 | * duration. In this way, a single variable is used to: |
| 745 | * |
| 746 | * - Know if a given window is being measured. |
| 747 | * - Account its duration. |
| 748 | * - Discount the interference. |
| 749 | * |
| 750 | * To avoid getting inconsistent values, e.g.,: |
| 751 | * |
| 752 | * now = time_get() |
| 753 | * ---> interrupt! |
| 754 | * delta_start -= int duration; |
| 755 | * <--- |
| 756 | * duration = now - delta_start; |
| 757 | * |
| 758 | * result: negative duration if the variable duration before the |
| 759 | * interrupt was smaller than the interrupt execution. |
| 760 | * |
| 761 | * A counter of interrupts is used. If the counter increased, try |
| 762 | * to capture an interference safe duration. |
| 763 | */ |
| 764 | static inline s64 |
| 765 | get_int_safe_duration(struct osnoise_variables *osn_var, u64 *delta_start) |
| 766 | { |
| 767 | u64 int_counter, now; |
| 768 | s64 duration; |
| 769 | |
| 770 | do { |
| 771 | int_counter = local_read(&osn_var->int_counter); |
| 772 | /* synchronize with interrupts */ |
| 773 | barrier(); |
| 774 | |
| 775 | now = time_get(); |
| 776 | duration = (now - *delta_start); |
| 777 | |
| 778 | /* synchronize with interrupts */ |
| 779 | barrier(); |
| 780 | } while (int_counter != local_read(&osn_var->int_counter)); |
| 781 | |
| 782 | /* |
| 783 | * This is an evidence of race conditions that cause |
| 784 | * a value to be "discounted" too much. |
| 785 | */ |
| 786 | if (duration < 0) |
| 787 | osnoise_taint("Negative duration!\n"); |
| 788 | |
| 789 | *delta_start = 0; |
| 790 | |
| 791 | return duration; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * |
| 796 | * set_int_safe_time - Save the current time on *time, aware of interference |
| 797 | * |
| 798 | * Get the time, taking into consideration a possible interference from |
| 799 | * higher priority interrupts. |
| 800 | * |
| 801 | * See get_int_safe_duration() for an explanation. |
| 802 | */ |
| 803 | static u64 |
| 804 | set_int_safe_time(struct osnoise_variables *osn_var, u64 *time) |
| 805 | { |
| 806 | u64 int_counter; |
| 807 | |
| 808 | do { |
| 809 | int_counter = local_read(&osn_var->int_counter); |
| 810 | /* synchronize with interrupts */ |
| 811 | barrier(); |
| 812 | |
| 813 | *time = time_get(); |
| 814 | |
| 815 | /* synchronize with interrupts */ |
| 816 | barrier(); |
| 817 | } while (int_counter != local_read(&osn_var->int_counter)); |
| 818 | |
| 819 | return int_counter; |
| 820 | } |
| 821 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 822 | #ifdef CONFIG_TIMERLAT_TRACER |
| 823 | /* |
| 824 | * copy_int_safe_time - Copy *src into *desc aware of interference |
| 825 | */ |
| 826 | static u64 |
| 827 | copy_int_safe_time(struct osnoise_variables *osn_var, u64 *dst, u64 *src) |
| 828 | { |
| 829 | u64 int_counter; |
| 830 | |
| 831 | do { |
| 832 | int_counter = local_read(&osn_var->int_counter); |
| 833 | /* synchronize with interrupts */ |
| 834 | barrier(); |
| 835 | |
| 836 | *dst = *src; |
| 837 | |
| 838 | /* synchronize with interrupts */ |
| 839 | barrier(); |
| 840 | } while (int_counter != local_read(&osn_var->int_counter)); |
| 841 | |
| 842 | return int_counter; |
| 843 | } |
| 844 | #endif /* CONFIG_TIMERLAT_TRACER */ |
| 845 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 846 | /* |
| 847 | * trace_osnoise_callback - NMI entry/exit callback |
| 848 | * |
| 849 | * This function is called at the entry and exit NMI code. The bool enter |
| 850 | * distinguishes between either case. This function is used to note a NMI |
| 851 | * occurrence, compute the noise caused by the NMI, and to remove the noise |
| 852 | * it is potentially causing on other interference variables. |
| 853 | */ |
| 854 | void trace_osnoise_callback(bool enter) |
| 855 | { |
| 856 | struct osnoise_variables *osn_var = this_cpu_osn_var(); |
| 857 | u64 duration; |
| 858 | |
| 859 | if (!osn_var->sampling) |
| 860 | return; |
| 861 | |
| 862 | /* |
| 863 | * Currently trace_clock_local() calls sched_clock() and the |
| 864 | * generic version is not NMI safe. |
| 865 | */ |
| 866 | if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) { |
| 867 | if (enter) { |
| 868 | osn_var->nmi.delta_start = time_get(); |
| 869 | local_inc(&osn_var->int_counter); |
| 870 | } else { |
| 871 | duration = time_get() - osn_var->nmi.delta_start; |
| 872 | |
| 873 | trace_nmi_noise(osn_var->nmi.delta_start, duration); |
| 874 | |
| 875 | cond_move_irq_delta_start(osn_var, duration); |
| 876 | cond_move_softirq_delta_start(osn_var, duration); |
| 877 | cond_move_thread_delta_start(osn_var, duration); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | if (enter) |
| 882 | osn_var->nmi.count++; |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * osnoise_trace_irq_entry - Note the starting of an IRQ |
| 887 | * |
| 888 | * Save the starting time of an IRQ. As IRQs are non-preemptive to other IRQs, |
| 889 | * it is safe to use a single variable (ons_var->irq) to save the statistics. |
| 890 | * The arrival_time is used to report... the arrival time. The delta_start |
| 891 | * is used to compute the duration at the IRQ exit handler. See |
| 892 | * cond_move_irq_delta_start(). |
| 893 | */ |
| 894 | void osnoise_trace_irq_entry(int id) |
| 895 | { |
| 896 | struct osnoise_variables *osn_var = this_cpu_osn_var(); |
| 897 | |
| 898 | if (!osn_var->sampling) |
| 899 | return; |
| 900 | /* |
| 901 | * This value will be used in the report, but not to compute |
| 902 | * the execution time, so it is safe to get it unsafe. |
| 903 | */ |
| 904 | osn_var->irq.arrival_time = time_get(); |
| 905 | set_int_safe_time(osn_var, &osn_var->irq.delta_start); |
| 906 | osn_var->irq.count++; |
| 907 | |
| 908 | local_inc(&osn_var->int_counter); |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * osnoise_irq_exit - Note the end of an IRQ, sava data and trace |
| 913 | * |
| 914 | * Computes the duration of the IRQ noise, and trace it. Also discounts the |
| 915 | * interference from other sources of noise could be currently being accounted. |
| 916 | */ |
| 917 | void osnoise_trace_irq_exit(int id, const char *desc) |
| 918 | { |
| 919 | struct osnoise_variables *osn_var = this_cpu_osn_var(); |
| 920 | int duration; |
| 921 | |
| 922 | if (!osn_var->sampling) |
| 923 | return; |
| 924 | |
| 925 | duration = get_int_safe_duration(osn_var, &osn_var->irq.delta_start); |
| 926 | trace_irq_noise(id, desc, osn_var->irq.arrival_time, duration); |
| 927 | osn_var->irq.arrival_time = 0; |
| 928 | cond_move_softirq_delta_start(osn_var, duration); |
| 929 | cond_move_thread_delta_start(osn_var, duration); |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * trace_irqentry_callback - Callback to the irq:irq_entry traceevent |
| 934 | * |
| 935 | * Used to note the starting of an IRQ occurece. |
| 936 | */ |
| 937 | static void trace_irqentry_callback(void *data, int irq, |
| 938 | struct irqaction *action) |
| 939 | { |
| 940 | osnoise_trace_irq_entry(irq); |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * trace_irqexit_callback - Callback to the irq:irq_exit traceevent |
| 945 | * |
| 946 | * Used to note the end of an IRQ occurece. |
| 947 | */ |
| 948 | static void trace_irqexit_callback(void *data, int irq, |
| 949 | struct irqaction *action, int ret) |
| 950 | { |
| 951 | osnoise_trace_irq_exit(irq, action->name); |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * arch specific register function. |
| 956 | */ |
| 957 | int __weak osnoise_arch_register(void) |
| 958 | { |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | * arch specific unregister function. |
| 964 | */ |
| 965 | void __weak osnoise_arch_unregister(void) |
| 966 | { |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * hook_irq_events - Hook IRQ handling events |
| 972 | * |
| 973 | * This function hooks the IRQ related callbacks to the respective trace |
| 974 | * events. |
| 975 | */ |
Daniel Bristot de Oliveira | f7d9f63 | 2021-06-28 11:45:47 +0200 | [diff] [blame] | 976 | static int hook_irq_events(void) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 977 | { |
| 978 | int ret; |
| 979 | |
| 980 | ret = register_trace_irq_handler_entry(trace_irqentry_callback, NULL); |
| 981 | if (ret) |
| 982 | goto out_err; |
| 983 | |
| 984 | ret = register_trace_irq_handler_exit(trace_irqexit_callback, NULL); |
| 985 | if (ret) |
| 986 | goto out_unregister_entry; |
| 987 | |
| 988 | ret = osnoise_arch_register(); |
| 989 | if (ret) |
| 990 | goto out_irq_exit; |
| 991 | |
| 992 | return 0; |
| 993 | |
| 994 | out_irq_exit: |
| 995 | unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL); |
| 996 | out_unregister_entry: |
| 997 | unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL); |
| 998 | out_err: |
| 999 | return -EINVAL; |
| 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * unhook_irq_events - Unhook IRQ handling events |
| 1004 | * |
| 1005 | * This function unhooks the IRQ related callbacks to the respective trace |
| 1006 | * events. |
| 1007 | */ |
Daniel Bristot de Oliveira | f7d9f63 | 2021-06-28 11:45:47 +0200 | [diff] [blame] | 1008 | static void unhook_irq_events(void) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1009 | { |
| 1010 | osnoise_arch_unregister(); |
| 1011 | unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL); |
| 1012 | unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL); |
| 1013 | } |
| 1014 | |
| 1015 | #ifndef CONFIG_PREEMPT_RT |
| 1016 | /* |
| 1017 | * trace_softirq_entry_callback - Note the starting of a softirq |
| 1018 | * |
| 1019 | * Save the starting time of a softirq. As softirqs are non-preemptive to |
| 1020 | * other softirqs, it is safe to use a single variable (ons_var->softirq) |
| 1021 | * to save the statistics. The arrival_time is used to report... the |
| 1022 | * arrival time. The delta_start is used to compute the duration at the |
| 1023 | * softirq exit handler. See cond_move_softirq_delta_start(). |
| 1024 | */ |
Daniel Bristot de Oliveira | f7d9f63 | 2021-06-28 11:45:47 +0200 | [diff] [blame] | 1025 | static void trace_softirq_entry_callback(void *data, unsigned int vec_nr) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1026 | { |
| 1027 | struct osnoise_variables *osn_var = this_cpu_osn_var(); |
| 1028 | |
| 1029 | if (!osn_var->sampling) |
| 1030 | return; |
| 1031 | /* |
| 1032 | * This value will be used in the report, but not to compute |
| 1033 | * the execution time, so it is safe to get it unsafe. |
| 1034 | */ |
| 1035 | osn_var->softirq.arrival_time = time_get(); |
| 1036 | set_int_safe_time(osn_var, &osn_var->softirq.delta_start); |
| 1037 | osn_var->softirq.count++; |
| 1038 | |
| 1039 | local_inc(&osn_var->int_counter); |
| 1040 | } |
| 1041 | |
| 1042 | /* |
| 1043 | * trace_softirq_exit_callback - Note the end of an softirq |
| 1044 | * |
| 1045 | * Computes the duration of the softirq noise, and trace it. Also discounts the |
| 1046 | * interference from other sources of noise could be currently being accounted. |
| 1047 | */ |
Daniel Bristot de Oliveira | f7d9f63 | 2021-06-28 11:45:47 +0200 | [diff] [blame] | 1048 | static void trace_softirq_exit_callback(void *data, unsigned int vec_nr) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1049 | { |
| 1050 | struct osnoise_variables *osn_var = this_cpu_osn_var(); |
| 1051 | int duration; |
| 1052 | |
| 1053 | if (!osn_var->sampling) |
| 1054 | return; |
| 1055 | |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 1056 | if (unlikely(timerlat_enabled())) |
| 1057 | if (!timerlat_softirq_exit(osn_var)) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1058 | return; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1059 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1060 | duration = get_int_safe_duration(osn_var, &osn_var->softirq.delta_start); |
| 1061 | trace_softirq_noise(vec_nr, osn_var->softirq.arrival_time, duration); |
| 1062 | cond_move_thread_delta_start(osn_var, duration); |
| 1063 | osn_var->softirq.arrival_time = 0; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * hook_softirq_events - Hook softirq handling events |
| 1068 | * |
| 1069 | * This function hooks the softirq related callbacks to the respective trace |
| 1070 | * events. |
| 1071 | */ |
| 1072 | static int hook_softirq_events(void) |
| 1073 | { |
| 1074 | int ret; |
| 1075 | |
| 1076 | ret = register_trace_softirq_entry(trace_softirq_entry_callback, NULL); |
| 1077 | if (ret) |
| 1078 | goto out_err; |
| 1079 | |
| 1080 | ret = register_trace_softirq_exit(trace_softirq_exit_callback, NULL); |
| 1081 | if (ret) |
| 1082 | goto out_unreg_entry; |
| 1083 | |
| 1084 | return 0; |
| 1085 | |
| 1086 | out_unreg_entry: |
| 1087 | unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL); |
| 1088 | out_err: |
| 1089 | return -EINVAL; |
| 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * unhook_softirq_events - Unhook softirq handling events |
| 1094 | * |
| 1095 | * This function hooks the softirq related callbacks to the respective trace |
| 1096 | * events. |
| 1097 | */ |
| 1098 | static void unhook_softirq_events(void) |
| 1099 | { |
| 1100 | unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL); |
| 1101 | unregister_trace_softirq_exit(trace_softirq_exit_callback, NULL); |
| 1102 | } |
| 1103 | #else /* CONFIG_PREEMPT_RT */ |
| 1104 | /* |
| 1105 | * softirq are threads on the PREEMPT_RT mode. |
| 1106 | */ |
| 1107 | static int hook_softirq_events(void) |
| 1108 | { |
| 1109 | return 0; |
| 1110 | } |
| 1111 | static void unhook_softirq_events(void) |
| 1112 | { |
| 1113 | } |
| 1114 | #endif |
| 1115 | |
| 1116 | /* |
| 1117 | * thread_entry - Record the starting of a thread noise window |
| 1118 | * |
| 1119 | * It saves the context switch time for a noisy thread, and increments |
| 1120 | * the interference counters. |
| 1121 | */ |
| 1122 | static void |
| 1123 | thread_entry(struct osnoise_variables *osn_var, struct task_struct *t) |
| 1124 | { |
| 1125 | if (!osn_var->sampling) |
| 1126 | return; |
| 1127 | /* |
| 1128 | * The arrival time will be used in the report, but not to compute |
| 1129 | * the execution time, so it is safe to get it unsafe. |
| 1130 | */ |
| 1131 | osn_var->thread.arrival_time = time_get(); |
| 1132 | |
| 1133 | set_int_safe_time(osn_var, &osn_var->thread.delta_start); |
| 1134 | |
| 1135 | osn_var->thread.count++; |
| 1136 | local_inc(&osn_var->int_counter); |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | * thread_exit - Report the end of a thread noise window |
| 1141 | * |
| 1142 | * It computes the total noise from a thread, tracing if needed. |
| 1143 | */ |
| 1144 | static void |
| 1145 | thread_exit(struct osnoise_variables *osn_var, struct task_struct *t) |
| 1146 | { |
| 1147 | int duration; |
| 1148 | |
| 1149 | if (!osn_var->sampling) |
| 1150 | return; |
| 1151 | |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 1152 | if (unlikely(timerlat_enabled())) |
| 1153 | if (!timerlat_thread_exit(osn_var)) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1154 | return; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1155 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1156 | duration = get_int_safe_duration(osn_var, &osn_var->thread.delta_start); |
| 1157 | |
| 1158 | trace_thread_noise(t, osn_var->thread.arrival_time, duration); |
| 1159 | |
| 1160 | osn_var->thread.arrival_time = 0; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * trace_sched_switch - sched:sched_switch trace event handler |
| 1165 | * |
| 1166 | * This function is hooked to the sched:sched_switch trace event, and it is |
| 1167 | * used to record the beginning and to report the end of a thread noise window. |
| 1168 | */ |
Daniel Bristot de Oliveira | f7d9f63 | 2021-06-28 11:45:47 +0200 | [diff] [blame] | 1169 | static void |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1170 | trace_sched_switch_callback(void *data, bool preempt, struct task_struct *p, |
| 1171 | struct task_struct *n) |
| 1172 | { |
| 1173 | struct osnoise_variables *osn_var = this_cpu_osn_var(); |
| 1174 | |
| 1175 | if (p->pid != osn_var->pid) |
| 1176 | thread_exit(osn_var, p); |
| 1177 | |
| 1178 | if (n->pid != osn_var->pid) |
| 1179 | thread_entry(osn_var, n); |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * hook_thread_events - Hook the insturmentation for thread noise |
| 1184 | * |
| 1185 | * Hook the osnoise tracer callbacks to handle the noise from other |
| 1186 | * threads on the necessary kernel events. |
| 1187 | */ |
Daniel Bristot de Oliveira | f7d9f63 | 2021-06-28 11:45:47 +0200 | [diff] [blame] | 1188 | static int hook_thread_events(void) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1189 | { |
| 1190 | int ret; |
| 1191 | |
| 1192 | ret = register_trace_sched_switch(trace_sched_switch_callback, NULL); |
| 1193 | if (ret) |
| 1194 | return -EINVAL; |
| 1195 | |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * unhook_thread_events - *nhook the insturmentation for thread noise |
| 1201 | * |
| 1202 | * Unook the osnoise tracer callbacks to handle the noise from other |
| 1203 | * threads on the necessary kernel events. |
| 1204 | */ |
Daniel Bristot de Oliveira | f7d9f63 | 2021-06-28 11:45:47 +0200 | [diff] [blame] | 1205 | static void unhook_thread_events(void) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1206 | { |
| 1207 | unregister_trace_sched_switch(trace_sched_switch_callback, NULL); |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * save_osn_sample_stats - Save the osnoise_sample statistics |
| 1212 | * |
| 1213 | * Save the osnoise_sample statistics before the sampling phase. These |
| 1214 | * values will be used later to compute the diff betwneen the statistics |
| 1215 | * before and after the osnoise sampling. |
| 1216 | */ |
Daniel Bristot de Oliveira | f7d9f63 | 2021-06-28 11:45:47 +0200 | [diff] [blame] | 1217 | static void |
| 1218 | save_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1219 | { |
| 1220 | s->nmi_count = osn_var->nmi.count; |
| 1221 | s->irq_count = osn_var->irq.count; |
| 1222 | s->softirq_count = osn_var->softirq.count; |
| 1223 | s->thread_count = osn_var->thread.count; |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * diff_osn_sample_stats - Compute the osnoise_sample statistics |
| 1228 | * |
| 1229 | * After a sample period, compute the difference on the osnoise_sample |
| 1230 | * statistics. The struct osnoise_sample *s contains the statistics saved via |
| 1231 | * save_osn_sample_stats() before the osnoise sampling. |
| 1232 | */ |
Daniel Bristot de Oliveira | f7d9f63 | 2021-06-28 11:45:47 +0200 | [diff] [blame] | 1233 | static void |
| 1234 | diff_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1235 | { |
| 1236 | s->nmi_count = osn_var->nmi.count - s->nmi_count; |
| 1237 | s->irq_count = osn_var->irq.count - s->irq_count; |
| 1238 | s->softirq_count = osn_var->softirq.count - s->softirq_count; |
| 1239 | s->thread_count = osn_var->thread.count - s->thread_count; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | * osnoise_stop_tracing - Stop tracing and the tracer. |
| 1244 | */ |
Daniel Bristot de Oliveira | 0e05ba4 | 2021-07-18 11:07:55 +0200 | [diff] [blame] | 1245 | static __always_inline void osnoise_stop_tracing(void) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1246 | { |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 1247 | struct osnoise_instance *inst; |
| 1248 | struct trace_array *tr; |
Daniel Bristot de Oliveira | 0e05ba4 | 2021-07-18 11:07:55 +0200 | [diff] [blame] | 1249 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 1250 | rcu_read_lock(); |
| 1251 | list_for_each_entry_rcu(inst, &osnoise_instances, list) { |
| 1252 | tr = inst->tr; |
| 1253 | trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_, |
| 1254 | "stop tracing hit on cpu %d\n", smp_processor_id()); |
Daniel Bristot de Oliveira | 0e05ba4 | 2021-07-18 11:07:55 +0200 | [diff] [blame] | 1255 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 1256 | tracer_tracing_off(tr); |
| 1257 | } |
| 1258 | rcu_read_unlock(); |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * notify_new_max_latency - Notify a new max latency via fsnotify interface. |
| 1263 | */ |
| 1264 | static void notify_new_max_latency(u64 latency) |
| 1265 | { |
| 1266 | struct osnoise_instance *inst; |
| 1267 | struct trace_array *tr; |
| 1268 | |
| 1269 | rcu_read_lock(); |
| 1270 | list_for_each_entry_rcu(inst, &osnoise_instances, list) { |
| 1271 | tr = inst->tr; |
| 1272 | if (tr->max_latency < latency) { |
| 1273 | tr->max_latency = latency; |
| 1274 | latency_fsnotify(tr); |
| 1275 | } |
| 1276 | } |
| 1277 | rcu_read_unlock(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * run_osnoise - Sample the time and look for osnoise |
| 1282 | * |
| 1283 | * Used to capture the time, looking for potential osnoise latency repeatedly. |
| 1284 | * Different from hwlat_detector, it is called with preemption and interrupts |
| 1285 | * enabled. This allows irqs, softirqs and threads to run, interfering on the |
| 1286 | * osnoise sampling thread, as they would do with a regular thread. |
| 1287 | */ |
| 1288 | static int run_osnoise(void) |
| 1289 | { |
| 1290 | struct osnoise_variables *osn_var = this_cpu_osn_var(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1291 | u64 start, sample, last_sample; |
| 1292 | u64 last_int_count, int_count; |
Daniel Bristot de Oliveira | 19c3eaa | 2021-06-29 19:10:26 +0200 | [diff] [blame] | 1293 | s64 noise = 0, max_noise = 0; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1294 | s64 total, last_total = 0; |
| 1295 | struct osnoise_sample s; |
| 1296 | unsigned int threshold; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1297 | u64 runtime, stop_in; |
Daniel Bristot de Oliveira | 19c3eaa | 2021-06-29 19:10:26 +0200 | [diff] [blame] | 1298 | u64 sum_noise = 0; |
| 1299 | int hw_count = 0; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1300 | int ret = -1; |
| 1301 | |
| 1302 | /* |
| 1303 | * Considers the current thread as the workload. |
| 1304 | */ |
| 1305 | osn_var->pid = current->pid; |
| 1306 | |
| 1307 | /* |
| 1308 | * Save the current stats for the diff |
| 1309 | */ |
| 1310 | save_osn_sample_stats(osn_var, &s); |
| 1311 | |
| 1312 | /* |
| 1313 | * if threshold is 0, use the default value of 5 us. |
| 1314 | */ |
| 1315 | threshold = tracing_thresh ? : 5000; |
| 1316 | |
| 1317 | /* |
| 1318 | * Make sure NMIs see sampling first |
| 1319 | */ |
| 1320 | osn_var->sampling = true; |
| 1321 | barrier(); |
| 1322 | |
| 1323 | /* |
| 1324 | * Transform the *_us config to nanoseconds to avoid the |
| 1325 | * division on the main loop. |
| 1326 | */ |
| 1327 | runtime = osnoise_data.sample_runtime * NSEC_PER_USEC; |
| 1328 | stop_in = osnoise_data.stop_tracing * NSEC_PER_USEC; |
| 1329 | |
| 1330 | /* |
| 1331 | * Start timestemp |
| 1332 | */ |
| 1333 | start = time_get(); |
| 1334 | |
| 1335 | /* |
| 1336 | * "previous" loop. |
| 1337 | */ |
| 1338 | last_int_count = set_int_safe_time(osn_var, &last_sample); |
| 1339 | |
| 1340 | do { |
| 1341 | /* |
| 1342 | * Get sample! |
| 1343 | */ |
| 1344 | int_count = set_int_safe_time(osn_var, &sample); |
| 1345 | |
| 1346 | noise = time_sub(sample, last_sample); |
| 1347 | |
| 1348 | /* |
| 1349 | * This shouldn't happen. |
| 1350 | */ |
| 1351 | if (noise < 0) { |
| 1352 | osnoise_taint("negative noise!"); |
| 1353 | goto out; |
| 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * Sample runtime. |
| 1358 | */ |
| 1359 | total = time_sub(sample, start); |
| 1360 | |
| 1361 | /* |
| 1362 | * Check for possible overflows. |
| 1363 | */ |
| 1364 | if (total < last_total) { |
| 1365 | osnoise_taint("total overflow!"); |
| 1366 | break; |
| 1367 | } |
| 1368 | |
| 1369 | last_total = total; |
| 1370 | |
| 1371 | if (noise >= threshold) { |
| 1372 | int interference = int_count - last_int_count; |
| 1373 | |
| 1374 | if (noise > max_noise) |
| 1375 | max_noise = noise; |
| 1376 | |
| 1377 | if (!interference) |
| 1378 | hw_count++; |
| 1379 | |
| 1380 | sum_noise += noise; |
| 1381 | |
| 1382 | trace_sample_threshold(last_sample, noise, interference); |
| 1383 | |
| 1384 | if (osnoise_data.stop_tracing) |
| 1385 | if (noise > stop_in) |
| 1386 | osnoise_stop_tracing(); |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * For the non-preemptive kernel config: let threads runs, if |
| 1391 | * they so wish. |
| 1392 | */ |
| 1393 | cond_resched(); |
| 1394 | |
| 1395 | last_sample = sample; |
| 1396 | last_int_count = int_count; |
| 1397 | |
| 1398 | } while (total < runtime && !kthread_should_stop()); |
| 1399 | |
| 1400 | /* |
| 1401 | * Finish the above in the view for interrupts. |
| 1402 | */ |
| 1403 | barrier(); |
| 1404 | |
| 1405 | osn_var->sampling = false; |
| 1406 | |
| 1407 | /* |
| 1408 | * Make sure sampling data is no longer updated. |
| 1409 | */ |
| 1410 | barrier(); |
| 1411 | |
| 1412 | /* |
| 1413 | * Save noise info. |
| 1414 | */ |
| 1415 | s.noise = time_to_us(sum_noise); |
| 1416 | s.runtime = time_to_us(total); |
| 1417 | s.max_sample = time_to_us(max_noise); |
| 1418 | s.hw_count = hw_count; |
| 1419 | |
| 1420 | /* Save interference stats info */ |
| 1421 | diff_osn_sample_stats(osn_var, &s); |
| 1422 | |
| 1423 | trace_osnoise_sample(&s); |
| 1424 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 1425 | notify_new_max_latency(max_noise); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1426 | |
| 1427 | if (osnoise_data.stop_tracing_total) |
| 1428 | if (s.noise > osnoise_data.stop_tracing_total) |
| 1429 | osnoise_stop_tracing(); |
| 1430 | |
| 1431 | return 0; |
| 1432 | out: |
| 1433 | return ret; |
| 1434 | } |
| 1435 | |
| 1436 | static struct cpumask osnoise_cpumask; |
| 1437 | static struct cpumask save_cpumask; |
| 1438 | |
| 1439 | /* |
| 1440 | * osnoise_main - The osnoise detection kernel thread |
| 1441 | * |
| 1442 | * Calls run_osnoise() function to measure the osnoise for the configured runtime, |
| 1443 | * every period. |
| 1444 | */ |
| 1445 | static int osnoise_main(void *data) |
| 1446 | { |
Daniel Bristot de Oliveira | 2a81afa | 2021-06-28 11:45:48 +0200 | [diff] [blame] | 1447 | u64 interval; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1448 | |
| 1449 | while (!kthread_should_stop()) { |
| 1450 | |
| 1451 | run_osnoise(); |
| 1452 | |
| 1453 | mutex_lock(&interface_lock); |
| 1454 | interval = osnoise_data.sample_period - osnoise_data.sample_runtime; |
| 1455 | mutex_unlock(&interface_lock); |
| 1456 | |
| 1457 | do_div(interval, USEC_PER_MSEC); |
| 1458 | |
| 1459 | /* |
| 1460 | * differently from hwlat_detector, the osnoise tracer can run |
| 1461 | * without a pause because preemption is on. |
| 1462 | */ |
Steven Rostedt (VMware) | b96285e | 2021-06-28 11:49:53 -0400 | [diff] [blame] | 1463 | if (interval < 1) { |
| 1464 | /* Let synchronize_rcu_tasks() make progress */ |
| 1465 | cond_resched_tasks_rcu_qs(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1466 | continue; |
Steven Rostedt (VMware) | b96285e | 2021-06-28 11:49:53 -0400 | [diff] [blame] | 1467 | } |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1468 | |
| 1469 | if (msleep_interruptible(interval)) |
| 1470 | break; |
| 1471 | } |
| 1472 | |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1476 | #ifdef CONFIG_TIMERLAT_TRACER |
| 1477 | /* |
| 1478 | * timerlat_irq - hrtimer handler for timerlat. |
| 1479 | */ |
| 1480 | static enum hrtimer_restart timerlat_irq(struct hrtimer *timer) |
| 1481 | { |
| 1482 | struct osnoise_variables *osn_var = this_cpu_osn_var(); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1483 | struct timerlat_variables *tlat; |
| 1484 | struct timerlat_sample s; |
| 1485 | u64 now; |
| 1486 | u64 diff; |
| 1487 | |
| 1488 | /* |
| 1489 | * I am not sure if the timer was armed for this CPU. So, get |
| 1490 | * the timerlat struct from the timer itself, not from this |
| 1491 | * CPU. |
| 1492 | */ |
| 1493 | tlat = container_of(timer, struct timerlat_variables, timer); |
| 1494 | |
| 1495 | now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer)); |
| 1496 | |
| 1497 | /* |
| 1498 | * Enable the osnoise: events for thread an softirq. |
| 1499 | */ |
| 1500 | tlat->tracing_thread = true; |
| 1501 | |
| 1502 | osn_var->thread.arrival_time = time_get(); |
| 1503 | |
| 1504 | /* |
| 1505 | * A hardirq is running: the timer IRQ. It is for sure preempting |
| 1506 | * a thread, and potentially preempting a softirq. |
| 1507 | * |
| 1508 | * At this point, it is not interesting to know the duration of the |
| 1509 | * preempted thread (and maybe softirq), but how much time they will |
| 1510 | * delay the beginning of the execution of the timer thread. |
| 1511 | * |
| 1512 | * To get the correct (net) delay added by the softirq, its delta_start |
| 1513 | * is set as the IRQ one. In this way, at the return of the IRQ, the delta |
| 1514 | * start of the sofitrq will be zeroed, accounting then only the time |
| 1515 | * after that. |
| 1516 | * |
| 1517 | * The thread follows the same principle. However, if a softirq is |
| 1518 | * running, the thread needs to receive the softirq delta_start. The |
| 1519 | * reason being is that the softirq will be the last to be unfolded, |
| 1520 | * resseting the thread delay to zero. |
Daniel Bristot de Oliveira | 01e181c | 2021-10-31 19:05:04 +0100 | [diff] [blame] | 1521 | * |
| 1522 | * The PREEMPT_RT is a special case, though. As softirqs run as threads |
| 1523 | * on RT, moving the thread is enough. |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1524 | */ |
Daniel Bristot de Oliveira | 01e181c | 2021-10-31 19:05:04 +0100 | [diff] [blame] | 1525 | if (!IS_ENABLED(CONFIG_PREEMPT_RT) && osn_var->softirq.delta_start) { |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1526 | copy_int_safe_time(osn_var, &osn_var->thread.delta_start, |
| 1527 | &osn_var->softirq.delta_start); |
| 1528 | |
| 1529 | copy_int_safe_time(osn_var, &osn_var->softirq.delta_start, |
| 1530 | &osn_var->irq.delta_start); |
| 1531 | } else { |
| 1532 | copy_int_safe_time(osn_var, &osn_var->thread.delta_start, |
| 1533 | &osn_var->irq.delta_start); |
| 1534 | } |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1535 | |
| 1536 | /* |
| 1537 | * Compute the current time with the expected time. |
| 1538 | */ |
| 1539 | diff = now - tlat->abs_period; |
| 1540 | |
| 1541 | tlat->count++; |
| 1542 | s.seqnum = tlat->count; |
| 1543 | s.timer_latency = diff; |
| 1544 | s.context = IRQ_CONTEXT; |
| 1545 | |
| 1546 | trace_timerlat_sample(&s); |
| 1547 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 1548 | notify_new_max_latency(diff); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1549 | |
| 1550 | if (osnoise_data.stop_tracing) |
| 1551 | if (time_to_us(diff) >= osnoise_data.stop_tracing) |
| 1552 | osnoise_stop_tracing(); |
| 1553 | |
| 1554 | wake_up_process(tlat->kthread); |
| 1555 | |
| 1556 | if (osnoise_data.print_stack) |
| 1557 | timerlat_save_stack(0); |
| 1558 | |
| 1559 | return HRTIMER_NORESTART; |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * wait_next_period - Wait for the next period for timerlat |
| 1564 | */ |
| 1565 | static int wait_next_period(struct timerlat_variables *tlat) |
| 1566 | { |
| 1567 | ktime_t next_abs_period, now; |
| 1568 | u64 rel_period = osnoise_data.timerlat_period * 1000; |
| 1569 | |
| 1570 | now = hrtimer_cb_get_time(&tlat->timer); |
| 1571 | next_abs_period = ns_to_ktime(tlat->abs_period + rel_period); |
| 1572 | |
| 1573 | /* |
| 1574 | * Save the next abs_period. |
| 1575 | */ |
| 1576 | tlat->abs_period = (u64) ktime_to_ns(next_abs_period); |
| 1577 | |
| 1578 | /* |
| 1579 | * If the new abs_period is in the past, skip the activation. |
| 1580 | */ |
| 1581 | while (ktime_compare(now, next_abs_period) > 0) { |
| 1582 | next_abs_period = ns_to_ktime(tlat->abs_period + rel_period); |
| 1583 | tlat->abs_period = (u64) ktime_to_ns(next_abs_period); |
| 1584 | } |
| 1585 | |
| 1586 | set_current_state(TASK_INTERRUPTIBLE); |
| 1587 | |
| 1588 | hrtimer_start(&tlat->timer, next_abs_period, HRTIMER_MODE_ABS_PINNED_HARD); |
| 1589 | schedule(); |
| 1590 | return 1; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * timerlat_main- Timerlat main |
| 1595 | */ |
| 1596 | static int timerlat_main(void *data) |
| 1597 | { |
| 1598 | struct osnoise_variables *osn_var = this_cpu_osn_var(); |
| 1599 | struct timerlat_variables *tlat = this_cpu_tmr_var(); |
| 1600 | struct timerlat_sample s; |
| 1601 | struct sched_param sp; |
| 1602 | u64 now, diff; |
| 1603 | |
| 1604 | /* |
| 1605 | * Make the thread RT, that is how cyclictest is usually used. |
| 1606 | */ |
| 1607 | sp.sched_priority = DEFAULT_TIMERLAT_PRIO; |
| 1608 | sched_setscheduler_nocheck(current, SCHED_FIFO, &sp); |
| 1609 | |
| 1610 | tlat->count = 0; |
| 1611 | tlat->tracing_thread = false; |
| 1612 | |
| 1613 | hrtimer_init(&tlat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD); |
| 1614 | tlat->timer.function = timerlat_irq; |
| 1615 | tlat->kthread = current; |
| 1616 | osn_var->pid = current->pid; |
| 1617 | /* |
| 1618 | * Anotate the arrival time. |
| 1619 | */ |
| 1620 | tlat->abs_period = hrtimer_cb_get_time(&tlat->timer); |
| 1621 | |
| 1622 | wait_next_period(tlat); |
| 1623 | |
| 1624 | osn_var->sampling = 1; |
| 1625 | |
| 1626 | while (!kthread_should_stop()) { |
| 1627 | now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer)); |
| 1628 | diff = now - tlat->abs_period; |
| 1629 | |
| 1630 | s.seqnum = tlat->count; |
| 1631 | s.timer_latency = diff; |
| 1632 | s.context = THREAD_CONTEXT; |
| 1633 | |
| 1634 | trace_timerlat_sample(&s); |
| 1635 | |
Daniel Bristot de Oliveira | b14f456 | 2021-10-31 19:05:03 +0100 | [diff] [blame] | 1636 | timerlat_dump_stack(time_to_us(diff)); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1637 | |
| 1638 | tlat->tracing_thread = false; |
| 1639 | if (osnoise_data.stop_tracing_total) |
| 1640 | if (time_to_us(diff) >= osnoise_data.stop_tracing_total) |
| 1641 | osnoise_stop_tracing(); |
| 1642 | |
| 1643 | wait_next_period(tlat); |
| 1644 | } |
| 1645 | |
| 1646 | hrtimer_cancel(&tlat->timer); |
| 1647 | return 0; |
| 1648 | } |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 1649 | #else /* CONFIG_TIMERLAT_TRACER */ |
| 1650 | static int timerlat_main(void *data) |
| 1651 | { |
| 1652 | return 0; |
| 1653 | } |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1654 | #endif /* CONFIG_TIMERLAT_TRACER */ |
| 1655 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1656 | /* |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1657 | * stop_kthread - stop a workload thread |
| 1658 | */ |
| 1659 | static void stop_kthread(unsigned int cpu) |
| 1660 | { |
| 1661 | struct task_struct *kthread; |
| 1662 | |
| 1663 | kthread = per_cpu(per_cpu_osnoise_var, cpu).kthread; |
| 1664 | if (kthread) |
| 1665 | kthread_stop(kthread); |
| 1666 | per_cpu(per_cpu_osnoise_var, cpu).kthread = NULL; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * stop_per_cpu_kthread - Stop per-cpu threads |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1671 | * |
| 1672 | * Stop the osnoise sampling htread. Use this on unload and at system |
| 1673 | * shutdown. |
| 1674 | */ |
| 1675 | static void stop_per_cpu_kthreads(void) |
| 1676 | { |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1677 | int cpu; |
| 1678 | |
Sebastian Andrzej Siewior | 99c37d1 | 2021-08-03 16:16:19 +0200 | [diff] [blame] | 1679 | cpus_read_lock(); |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1680 | |
| 1681 | for_each_online_cpu(cpu) |
| 1682 | stop_kthread(cpu); |
| 1683 | |
Sebastian Andrzej Siewior | 99c37d1 | 2021-08-03 16:16:19 +0200 | [diff] [blame] | 1684 | cpus_read_unlock(); |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * start_kthread - Start a workload tread |
| 1689 | */ |
| 1690 | static int start_kthread(unsigned int cpu) |
| 1691 | { |
| 1692 | struct task_struct *kthread; |
| 1693 | void *main = osnoise_main; |
| 1694 | char comm[24]; |
| 1695 | |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 1696 | if (timerlat_enabled()) { |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1697 | snprintf(comm, 24, "timerlat/%d", cpu); |
| 1698 | main = timerlat_main; |
| 1699 | } else { |
| 1700 | snprintf(comm, 24, "osnoise/%d", cpu); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1701 | } |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 1702 | |
Cai Huoqing | 11e4e35 | 2022-01-14 14:03:06 -0800 | [diff] [blame] | 1703 | kthread = kthread_run_on_cpu(main, NULL, cpu, comm); |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1704 | |
| 1705 | if (IS_ERR(kthread)) { |
| 1706 | pr_err(BANNER "could not start sampling thread\n"); |
| 1707 | stop_per_cpu_kthreads(); |
| 1708 | return -ENOMEM; |
| 1709 | } |
| 1710 | |
| 1711 | per_cpu(per_cpu_osnoise_var, cpu).kthread = kthread; |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1712 | |
| 1713 | return 0; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * start_per_cpu_kthread - Kick off per-cpu osnoise sampling kthreads |
| 1718 | * |
| 1719 | * This starts the kernel thread that will look for osnoise on many |
| 1720 | * cpus. |
| 1721 | */ |
Daniel Bristot de Oliveira | 15ca4bd | 2021-10-31 19:04:58 +0100 | [diff] [blame] | 1722 | static int start_per_cpu_kthreads(void) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1723 | { |
| 1724 | struct cpumask *current_mask = &save_cpumask; |
Qiang.Zhang | 4b6b08f | 2021-08-31 10:29:19 +0800 | [diff] [blame] | 1725 | int retval = 0; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1726 | int cpu; |
| 1727 | |
Sebastian Andrzej Siewior | 99c37d1 | 2021-08-03 16:16:19 +0200 | [diff] [blame] | 1728 | cpus_read_lock(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1729 | /* |
Daniel Bristot de Oliveira | 66df27f | 2021-10-31 19:04:56 +0100 | [diff] [blame] | 1730 | * Run only on online CPUs in which osnoise is allowed to run. |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1731 | */ |
Daniel Bristot de Oliveira | 66df27f | 2021-10-31 19:04:56 +0100 | [diff] [blame] | 1732 | cpumask_and(current_mask, cpu_online_mask, &osnoise_cpumask); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1733 | |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1734 | for_each_possible_cpu(cpu) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1735 | per_cpu(per_cpu_osnoise_var, cpu).kthread = NULL; |
| 1736 | |
| 1737 | for_each_cpu(cpu, current_mask) { |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1738 | retval = start_kthread(cpu); |
| 1739 | if (retval) { |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1740 | stop_per_cpu_kthreads(); |
Qiang.Zhang | 4b6b08f | 2021-08-31 10:29:19 +0800 | [diff] [blame] | 1741 | break; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1742 | } |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
Sebastian Andrzej Siewior | 99c37d1 | 2021-08-03 16:16:19 +0200 | [diff] [blame] | 1745 | cpus_read_unlock(); |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1746 | |
Qiang.Zhang | 4b6b08f | 2021-08-31 10:29:19 +0800 | [diff] [blame] | 1747 | return retval; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1750 | #ifdef CONFIG_HOTPLUG_CPU |
| 1751 | static void osnoise_hotplug_workfn(struct work_struct *dummy) |
| 1752 | { |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1753 | unsigned int cpu = smp_processor_id(); |
| 1754 | |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1755 | mutex_lock(&trace_types_lock); |
| 1756 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 1757 | if (!osnoise_has_registered_instances()) |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1758 | goto out_unlock_trace; |
| 1759 | |
| 1760 | mutex_lock(&interface_lock); |
Sebastian Andrzej Siewior | 99c37d1 | 2021-08-03 16:16:19 +0200 | [diff] [blame] | 1761 | cpus_read_lock(); |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1762 | |
| 1763 | if (!cpumask_test_cpu(cpu, &osnoise_cpumask)) |
| 1764 | goto out_unlock; |
| 1765 | |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1766 | start_kthread(cpu); |
| 1767 | |
| 1768 | out_unlock: |
Sebastian Andrzej Siewior | 99c37d1 | 2021-08-03 16:16:19 +0200 | [diff] [blame] | 1769 | cpus_read_unlock(); |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1770 | mutex_unlock(&interface_lock); |
| 1771 | out_unlock_trace: |
| 1772 | mutex_unlock(&trace_types_lock); |
| 1773 | } |
| 1774 | |
| 1775 | static DECLARE_WORK(osnoise_hotplug_work, osnoise_hotplug_workfn); |
| 1776 | |
| 1777 | /* |
| 1778 | * osnoise_cpu_init - CPU hotplug online callback function |
| 1779 | */ |
| 1780 | static int osnoise_cpu_init(unsigned int cpu) |
| 1781 | { |
| 1782 | schedule_work_on(cpu, &osnoise_hotplug_work); |
| 1783 | return 0; |
| 1784 | } |
| 1785 | |
| 1786 | /* |
| 1787 | * osnoise_cpu_die - CPU hotplug offline callback function |
| 1788 | */ |
| 1789 | static int osnoise_cpu_die(unsigned int cpu) |
| 1790 | { |
| 1791 | stop_kthread(cpu); |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | static void osnoise_init_hotplug_support(void) |
| 1796 | { |
| 1797 | int ret; |
| 1798 | |
| 1799 | ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "trace/osnoise:online", |
| 1800 | osnoise_cpu_init, osnoise_cpu_die); |
| 1801 | if (ret < 0) |
| 1802 | pr_warn(BANNER "Error to init cpu hotplug support\n"); |
| 1803 | |
| 1804 | return; |
| 1805 | } |
| 1806 | #else /* CONFIG_HOTPLUG_CPU */ |
| 1807 | static void osnoise_init_hotplug_support(void) |
| 1808 | { |
Daniel Bristot de Oliveira | 498627b | 2021-06-28 11:45:49 +0200 | [diff] [blame] | 1809 | return; |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1810 | } |
| 1811 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 1812 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1813 | /* |
| 1814 | * osnoise_cpus_read - Read function for reading the "cpus" file |
| 1815 | * @filp: The active open file structure |
| 1816 | * @ubuf: The userspace provided buffer to read value into |
| 1817 | * @cnt: The maximum number of bytes to read |
| 1818 | * @ppos: The current "file" position |
| 1819 | * |
| 1820 | * Prints the "cpus" output into the user-provided buffer. |
| 1821 | */ |
| 1822 | static ssize_t |
| 1823 | osnoise_cpus_read(struct file *filp, char __user *ubuf, size_t count, |
| 1824 | loff_t *ppos) |
| 1825 | { |
| 1826 | char *mask_str; |
| 1827 | int len; |
| 1828 | |
| 1829 | mutex_lock(&interface_lock); |
| 1830 | |
| 1831 | len = snprintf(NULL, 0, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)) + 1; |
| 1832 | mask_str = kmalloc(len, GFP_KERNEL); |
| 1833 | if (!mask_str) { |
| 1834 | count = -ENOMEM; |
| 1835 | goto out_unlock; |
| 1836 | } |
| 1837 | |
| 1838 | len = snprintf(mask_str, len, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)); |
| 1839 | if (len >= count) { |
| 1840 | count = -EINVAL; |
| 1841 | goto out_free; |
| 1842 | } |
| 1843 | |
| 1844 | count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len); |
| 1845 | |
| 1846 | out_free: |
| 1847 | kfree(mask_str); |
| 1848 | out_unlock: |
| 1849 | mutex_unlock(&interface_lock); |
| 1850 | |
| 1851 | return count; |
| 1852 | } |
| 1853 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1854 | /* |
| 1855 | * osnoise_cpus_write - Write function for "cpus" entry |
| 1856 | * @filp: The active open file structure |
| 1857 | * @ubuf: The user buffer that contains the value to write |
| 1858 | * @cnt: The maximum number of bytes to write to "file" |
| 1859 | * @ppos: The current position in @file |
| 1860 | * |
| 1861 | * This function provides a write implementation for the "cpus" |
| 1862 | * interface to the osnoise trace. By default, it lists all CPUs, |
| 1863 | * in this way, allowing osnoise threads to run on any online CPU |
| 1864 | * of the system. It serves to restrict the execution of osnoise to the |
Daniel Bristot de Oliveira | 66df27f | 2021-10-31 19:04:56 +0100 | [diff] [blame] | 1865 | * set of CPUs writing via this interface. Why not use "tracing_cpumask"? |
| 1866 | * Because the user might be interested in tracing what is running on |
| 1867 | * other CPUs. For instance, one might run osnoise in one HT CPU |
| 1868 | * while observing what is running on the sibling HT CPU. |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1869 | */ |
| 1870 | static ssize_t |
| 1871 | osnoise_cpus_write(struct file *filp, const char __user *ubuf, size_t count, |
| 1872 | loff_t *ppos) |
| 1873 | { |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1874 | cpumask_var_t osnoise_cpumask_new; |
| 1875 | int running, err; |
| 1876 | char buf[256]; |
| 1877 | |
| 1878 | if (count >= 256) |
| 1879 | return -EINVAL; |
| 1880 | |
| 1881 | if (copy_from_user(buf, ubuf, count)) |
| 1882 | return -EFAULT; |
| 1883 | |
| 1884 | if (!zalloc_cpumask_var(&osnoise_cpumask_new, GFP_KERNEL)) |
| 1885 | return -ENOMEM; |
| 1886 | |
| 1887 | err = cpulist_parse(buf, osnoise_cpumask_new); |
| 1888 | if (err) |
| 1889 | goto err_free; |
| 1890 | |
| 1891 | /* |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 1892 | * trace_types_lock is taken to avoid concurrency on start/stop. |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1893 | */ |
| 1894 | mutex_lock(&trace_types_lock); |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 1895 | running = osnoise_has_registered_instances(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1896 | if (running) |
Daniel Bristot de Oliveira | 2bd1bdf | 2021-10-31 19:04:59 +0100 | [diff] [blame] | 1897 | stop_per_cpu_kthreads(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1898 | |
| 1899 | mutex_lock(&interface_lock); |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1900 | /* |
| 1901 | * osnoise_cpumask is read by CPU hotplug operations. |
| 1902 | */ |
Sebastian Andrzej Siewior | 99c37d1 | 2021-08-03 16:16:19 +0200 | [diff] [blame] | 1903 | cpus_read_lock(); |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1904 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1905 | cpumask_copy(&osnoise_cpumask, osnoise_cpumask_new); |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 1906 | |
Sebastian Andrzej Siewior | 99c37d1 | 2021-08-03 16:16:19 +0200 | [diff] [blame] | 1907 | cpus_read_unlock(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1908 | mutex_unlock(&interface_lock); |
| 1909 | |
| 1910 | if (running) |
Daniel Bristot de Oliveira | 2bd1bdf | 2021-10-31 19:04:59 +0100 | [diff] [blame] | 1911 | start_per_cpu_kthreads(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1912 | mutex_unlock(&trace_types_lock); |
| 1913 | |
| 1914 | free_cpumask_var(osnoise_cpumask_new); |
| 1915 | return count; |
| 1916 | |
| 1917 | err_free: |
| 1918 | free_cpumask_var(osnoise_cpumask_new); |
| 1919 | |
| 1920 | return err; |
| 1921 | } |
| 1922 | |
| 1923 | /* |
| 1924 | * osnoise/runtime_us: cannot be greater than the period. |
| 1925 | */ |
| 1926 | static struct trace_min_max_param osnoise_runtime = { |
| 1927 | .lock = &interface_lock, |
| 1928 | .val = &osnoise_data.sample_runtime, |
| 1929 | .max = &osnoise_data.sample_period, |
| 1930 | .min = NULL, |
| 1931 | }; |
| 1932 | |
| 1933 | /* |
| 1934 | * osnoise/period_us: cannot be smaller than the runtime. |
| 1935 | */ |
| 1936 | static struct trace_min_max_param osnoise_period = { |
| 1937 | .lock = &interface_lock, |
| 1938 | .val = &osnoise_data.sample_period, |
| 1939 | .max = NULL, |
| 1940 | .min = &osnoise_data.sample_runtime, |
| 1941 | }; |
| 1942 | |
| 1943 | /* |
| 1944 | * osnoise/stop_tracing_us: no limit. |
| 1945 | */ |
| 1946 | static struct trace_min_max_param osnoise_stop_tracing_in = { |
| 1947 | .lock = &interface_lock, |
| 1948 | .val = &osnoise_data.stop_tracing, |
| 1949 | .max = NULL, |
| 1950 | .min = NULL, |
| 1951 | }; |
| 1952 | |
| 1953 | /* |
| 1954 | * osnoise/stop_tracing_total_us: no limit. |
| 1955 | */ |
| 1956 | static struct trace_min_max_param osnoise_stop_tracing_total = { |
| 1957 | .lock = &interface_lock, |
| 1958 | .val = &osnoise_data.stop_tracing_total, |
| 1959 | .max = NULL, |
| 1960 | .min = NULL, |
| 1961 | }; |
| 1962 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 1963 | #ifdef CONFIG_TIMERLAT_TRACER |
| 1964 | /* |
| 1965 | * osnoise/print_stack: print the stacktrace of the IRQ handler if the total |
| 1966 | * latency is higher than val. |
| 1967 | */ |
| 1968 | static struct trace_min_max_param osnoise_print_stack = { |
| 1969 | .lock = &interface_lock, |
| 1970 | .val = &osnoise_data.print_stack, |
| 1971 | .max = NULL, |
| 1972 | .min = NULL, |
| 1973 | }; |
| 1974 | |
| 1975 | /* |
| 1976 | * osnoise/timerlat_period: min 100 us, max 1 s |
| 1977 | */ |
| 1978 | u64 timerlat_min_period = 100; |
| 1979 | u64 timerlat_max_period = 1000000; |
| 1980 | static struct trace_min_max_param timerlat_period = { |
| 1981 | .lock = &interface_lock, |
| 1982 | .val = &osnoise_data.timerlat_period, |
| 1983 | .max = &timerlat_max_period, |
| 1984 | .min = &timerlat_min_period, |
| 1985 | }; |
| 1986 | #endif |
| 1987 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 1988 | static const struct file_operations cpus_fops = { |
| 1989 | .open = tracing_open_generic, |
| 1990 | .read = osnoise_cpus_read, |
| 1991 | .write = osnoise_cpus_write, |
| 1992 | .llseek = generic_file_llseek, |
| 1993 | }; |
| 1994 | |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 1995 | #ifdef CONFIG_TIMERLAT_TRACER |
Daniel Bristot de Oliveira | b14f456 | 2021-10-31 19:05:03 +0100 | [diff] [blame] | 1996 | #ifdef CONFIG_STACKTRACE |
| 1997 | static int init_timerlat_stack_tracefs(struct dentry *top_dir) |
| 1998 | { |
| 1999 | struct dentry *tmp; |
| 2000 | |
| 2001 | tmp = tracefs_create_file("print_stack", TRACE_MODE_WRITE, top_dir, |
| 2002 | &osnoise_print_stack, &trace_min_max_fops); |
| 2003 | if (!tmp) |
| 2004 | return -ENOMEM; |
| 2005 | |
| 2006 | return 0; |
| 2007 | } |
| 2008 | #else /* CONFIG_STACKTRACE */ |
| 2009 | static int init_timerlat_stack_tracefs(struct dentry *top_dir) |
| 2010 | { |
| 2011 | return 0; |
| 2012 | } |
| 2013 | #endif /* CONFIG_STACKTRACE */ |
| 2014 | |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 2015 | /* |
| 2016 | * init_timerlat_tracefs - A function to initialize the timerlat interface files |
| 2017 | */ |
| 2018 | static int init_timerlat_tracefs(struct dentry *top_dir) |
| 2019 | { |
| 2020 | struct dentry *tmp; |
| 2021 | |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 2022 | tmp = tracefs_create_file("timerlat_period_us", TRACE_MODE_WRITE, top_dir, |
| 2023 | &timerlat_period, &trace_min_max_fops); |
| 2024 | if (!tmp) |
| 2025 | return -ENOMEM; |
| 2026 | |
Daniel Bristot de Oliveira | b14f456 | 2021-10-31 19:05:03 +0100 | [diff] [blame] | 2027 | return init_timerlat_stack_tracefs(top_dir); |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 2028 | } |
| 2029 | #else /* CONFIG_TIMERLAT_TRACER */ |
| 2030 | static int init_timerlat_tracefs(struct dentry *top_dir) |
| 2031 | { |
| 2032 | return 0; |
| 2033 | } |
| 2034 | #endif /* CONFIG_TIMERLAT_TRACER */ |
| 2035 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2036 | /* |
| 2037 | * init_tracefs - A function to initialize the tracefs interface files |
| 2038 | * |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2039 | * This function creates entries in tracefs for "osnoise" and "timerlat". |
| 2040 | * It creates these directories in the tracing directory, and within that |
| 2041 | * directory the use can change and view the configs. |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2042 | */ |
| 2043 | static int init_tracefs(void) |
| 2044 | { |
| 2045 | struct dentry *top_dir; |
| 2046 | struct dentry *tmp; |
| 2047 | int ret; |
| 2048 | |
| 2049 | ret = tracing_init_dentry(); |
| 2050 | if (ret) |
| 2051 | return -ENOMEM; |
| 2052 | |
| 2053 | top_dir = tracefs_create_dir("osnoise", NULL); |
| 2054 | if (!top_dir) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2055 | return 0; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2056 | |
Steven Rostedt (VMware) | 21ccc9c | 2021-08-18 11:24:51 -0400 | [diff] [blame] | 2057 | tmp = tracefs_create_file("period_us", TRACE_MODE_WRITE, top_dir, |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2058 | &osnoise_period, &trace_min_max_fops); |
| 2059 | if (!tmp) |
| 2060 | goto err; |
| 2061 | |
Steven Rostedt (VMware) | 21ccc9c | 2021-08-18 11:24:51 -0400 | [diff] [blame] | 2062 | tmp = tracefs_create_file("runtime_us", TRACE_MODE_WRITE, top_dir, |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2063 | &osnoise_runtime, &trace_min_max_fops); |
| 2064 | if (!tmp) |
| 2065 | goto err; |
| 2066 | |
Steven Rostedt (VMware) | 21ccc9c | 2021-08-18 11:24:51 -0400 | [diff] [blame] | 2067 | tmp = tracefs_create_file("stop_tracing_us", TRACE_MODE_WRITE, top_dir, |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2068 | &osnoise_stop_tracing_in, &trace_min_max_fops); |
| 2069 | if (!tmp) |
| 2070 | goto err; |
| 2071 | |
Steven Rostedt (VMware) | 21ccc9c | 2021-08-18 11:24:51 -0400 | [diff] [blame] | 2072 | tmp = tracefs_create_file("stop_tracing_total_us", TRACE_MODE_WRITE, top_dir, |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2073 | &osnoise_stop_tracing_total, &trace_min_max_fops); |
| 2074 | if (!tmp) |
| 2075 | goto err; |
| 2076 | |
Steven Rostedt (VMware) | 21ccc9c | 2021-08-18 11:24:51 -0400 | [diff] [blame] | 2077 | tmp = trace_create_file("cpus", TRACE_MODE_WRITE, top_dir, NULL, &cpus_fops); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2078 | if (!tmp) |
| 2079 | goto err; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2080 | |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 2081 | ret = init_timerlat_tracefs(top_dir); |
| 2082 | if (ret) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2083 | goto err; |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2084 | |
| 2085 | return 0; |
| 2086 | |
| 2087 | err: |
| 2088 | tracefs_remove(top_dir); |
| 2089 | return -ENOMEM; |
| 2090 | } |
| 2091 | |
| 2092 | static int osnoise_hook_events(void) |
| 2093 | { |
| 2094 | int retval; |
| 2095 | |
| 2096 | /* |
| 2097 | * Trace is already hooked, we are re-enabling from |
| 2098 | * a stop_tracing_*. |
| 2099 | */ |
| 2100 | if (trace_osnoise_callback_enabled) |
| 2101 | return 0; |
| 2102 | |
| 2103 | retval = hook_irq_events(); |
| 2104 | if (retval) |
| 2105 | return -EINVAL; |
| 2106 | |
| 2107 | retval = hook_softirq_events(); |
| 2108 | if (retval) |
| 2109 | goto out_unhook_irq; |
| 2110 | |
| 2111 | retval = hook_thread_events(); |
| 2112 | /* |
| 2113 | * All fine! |
| 2114 | */ |
| 2115 | if (!retval) |
| 2116 | return 0; |
| 2117 | |
| 2118 | unhook_softirq_events(); |
| 2119 | out_unhook_irq: |
| 2120 | unhook_irq_events(); |
| 2121 | return -EINVAL; |
| 2122 | } |
| 2123 | |
Nikita Yushchenko | 0878355 | 2022-01-09 18:34:59 +0300 | [diff] [blame] | 2124 | static void osnoise_unhook_events(void) |
| 2125 | { |
| 2126 | unhook_thread_events(); |
| 2127 | unhook_softirq_events(); |
| 2128 | unhook_irq_events(); |
| 2129 | } |
| 2130 | |
Daniel Bristot de Oliveira | 15ca4bd | 2021-10-31 19:04:58 +0100 | [diff] [blame] | 2131 | /* |
| 2132 | * osnoise_workload_start - start the workload and hook to events |
| 2133 | */ |
| 2134 | static int osnoise_workload_start(void) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2135 | { |
| 2136 | int retval; |
| 2137 | |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2138 | /* |
| 2139 | * Instances need to be registered after calling workload |
| 2140 | * start. Hence, if there is already an instance, the |
| 2141 | * workload was already registered. Otherwise, this |
| 2142 | * code is on the way to register the first instance, |
| 2143 | * and the workload will start. |
| 2144 | */ |
| 2145 | if (osnoise_has_registered_instances()) |
| 2146 | return 0; |
| 2147 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2148 | osn_var_reset_all(); |
| 2149 | |
| 2150 | retval = osnoise_hook_events(); |
| 2151 | if (retval) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2152 | return retval; |
Daniel Bristot de Oliveira | c3b6343 | 2021-10-31 19:04:57 +0100 | [diff] [blame] | 2153 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2154 | /* |
Daniel Bristot de Oliveira | c3b6343 | 2021-10-31 19:04:57 +0100 | [diff] [blame] | 2155 | * Make sure that ftrace_nmi_enter/exit() see reset values |
| 2156 | * before enabling trace_osnoise_callback_enabled. |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2157 | */ |
| 2158 | barrier(); |
| 2159 | trace_osnoise_callback_enabled = true; |
| 2160 | |
Daniel Bristot de Oliveira | 15ca4bd | 2021-10-31 19:04:58 +0100 | [diff] [blame] | 2161 | retval = start_per_cpu_kthreads(); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2162 | if (retval) { |
Nikita Yushchenko | 0878355 | 2022-01-09 18:34:59 +0300 | [diff] [blame] | 2163 | trace_osnoise_callback_enabled = false; |
| 2164 | /* |
| 2165 | * Make sure that ftrace_nmi_enter/exit() see |
| 2166 | * trace_osnoise_callback_enabled as false before continuing. |
| 2167 | */ |
| 2168 | barrier(); |
| 2169 | |
| 2170 | osnoise_unhook_events(); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2171 | return retval; |
| 2172 | } |
| 2173 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2174 | return 0; |
| 2175 | } |
| 2176 | |
Daniel Bristot de Oliveira | 15ca4bd | 2021-10-31 19:04:58 +0100 | [diff] [blame] | 2177 | /* |
| 2178 | * osnoise_workload_stop - stop the workload and unhook the events |
| 2179 | */ |
| 2180 | static void osnoise_workload_stop(void) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2181 | { |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2182 | /* |
| 2183 | * Instances need to be unregistered before calling |
| 2184 | * stop. Hence, if there is a registered instance, more |
| 2185 | * than one instance is running, and the workload will not |
| 2186 | * yet stop. Otherwise, this code is on the way to disable |
| 2187 | * the last instance, and the workload can stop. |
| 2188 | */ |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 2189 | if (osnoise_has_registered_instances()) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2190 | return; |
| 2191 | |
| 2192 | trace_osnoise_callback_enabled = false; |
Daniel Bristot de Oliveira | c3b6343 | 2021-10-31 19:04:57 +0100 | [diff] [blame] | 2193 | /* |
| 2194 | * Make sure that ftrace_nmi_enter/exit() see |
| 2195 | * trace_osnoise_callback_enabled as false before continuing. |
| 2196 | */ |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2197 | barrier(); |
| 2198 | |
| 2199 | stop_per_cpu_kthreads(); |
| 2200 | |
Nikita Yushchenko | 0878355 | 2022-01-09 18:34:59 +0300 | [diff] [blame] | 2201 | osnoise_unhook_events(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2202 | } |
| 2203 | |
Daniel Bristot de Oliveira | 15ca4bd | 2021-10-31 19:04:58 +0100 | [diff] [blame] | 2204 | static void osnoise_tracer_start(struct trace_array *tr) |
| 2205 | { |
| 2206 | int retval; |
| 2207 | |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2208 | /* |
| 2209 | * If the instance is already registered, there is no need to |
| 2210 | * register it again. |
| 2211 | */ |
| 2212 | if (osnoise_instance_registered(tr)) |
Daniel Bristot de Oliveira | 15ca4bd | 2021-10-31 19:04:58 +0100 | [diff] [blame] | 2213 | return; |
| 2214 | |
| 2215 | retval = osnoise_workload_start(); |
| 2216 | if (retval) |
| 2217 | pr_err(BANNER "Error starting osnoise tracer\n"); |
| 2218 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 2219 | osnoise_register_instance(tr); |
Daniel Bristot de Oliveira | 15ca4bd | 2021-10-31 19:04:58 +0100 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | static void osnoise_tracer_stop(struct trace_array *tr) |
| 2223 | { |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 2224 | osnoise_unregister_instance(tr); |
Daniel Bristot de Oliveira | 15ca4bd | 2021-10-31 19:04:58 +0100 | [diff] [blame] | 2225 | osnoise_workload_stop(); |
| 2226 | } |
| 2227 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2228 | static int osnoise_tracer_init(struct trace_array *tr) |
| 2229 | { |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2230 | /* |
| 2231 | * Only allow osnoise tracer if timerlat tracer is not running |
| 2232 | * already. |
| 2233 | */ |
| 2234 | if (timerlat_enabled()) |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2235 | return -EBUSY; |
| 2236 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2237 | tr->max_latency = 0; |
| 2238 | |
| 2239 | osnoise_tracer_start(tr); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2240 | return 0; |
| 2241 | } |
| 2242 | |
| 2243 | static void osnoise_tracer_reset(struct trace_array *tr) |
| 2244 | { |
| 2245 | osnoise_tracer_stop(tr); |
| 2246 | } |
| 2247 | |
| 2248 | static struct tracer osnoise_tracer __read_mostly = { |
| 2249 | .name = "osnoise", |
| 2250 | .init = osnoise_tracer_init, |
| 2251 | .reset = osnoise_tracer_reset, |
| 2252 | .start = osnoise_tracer_start, |
| 2253 | .stop = osnoise_tracer_stop, |
| 2254 | .print_header = print_osnoise_headers, |
| 2255 | .allow_instances = true, |
| 2256 | }; |
| 2257 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2258 | #ifdef CONFIG_TIMERLAT_TRACER |
| 2259 | static void timerlat_tracer_start(struct trace_array *tr) |
| 2260 | { |
| 2261 | int retval; |
| 2262 | |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2263 | /* |
| 2264 | * If the instance is already registered, there is no need to |
| 2265 | * register it again. |
| 2266 | */ |
| 2267 | if (osnoise_instance_registered(tr)) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2268 | return; |
| 2269 | |
Daniel Bristot de Oliveira | 15ca4bd | 2021-10-31 19:04:58 +0100 | [diff] [blame] | 2270 | retval = osnoise_workload_start(); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2271 | if (retval) |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2272 | pr_err(BANNER "Error starting timerlat tracer\n"); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2273 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 2274 | osnoise_register_instance(tr); |
| 2275 | |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2276 | return; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | static void timerlat_tracer_stop(struct trace_array *tr) |
| 2280 | { |
| 2281 | int cpu; |
| 2282 | |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2283 | osnoise_unregister_instance(tr); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2284 | |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2285 | /* |
| 2286 | * Instruct the threads to stop only if this is the last instance. |
| 2287 | */ |
| 2288 | if (!osnoise_has_registered_instances()) { |
| 2289 | for_each_online_cpu(cpu) |
| 2290 | per_cpu(per_cpu_osnoise_var, cpu).sampling = 0; |
| 2291 | } |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2292 | |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2293 | osnoise_workload_stop(); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | static int timerlat_tracer_init(struct trace_array *tr) |
| 2297 | { |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2298 | /* |
| 2299 | * Only allow timerlat tracer if osnoise tracer is not running already. |
| 2300 | */ |
| 2301 | if (osnoise_has_registered_instances() && !osnoise_data.timerlat_tracer) |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2302 | return -EBUSY; |
| 2303 | |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2304 | /* |
| 2305 | * If this is the first instance, set timerlat_tracer to block |
| 2306 | * osnoise tracer start. |
| 2307 | */ |
| 2308 | if (!osnoise_has_registered_instances()) |
| 2309 | osnoise_data.timerlat_tracer = 1; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2310 | |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2311 | tr->max_latency = 0; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2312 | timerlat_tracer_start(tr); |
| 2313 | |
| 2314 | return 0; |
| 2315 | } |
| 2316 | |
| 2317 | static void timerlat_tracer_reset(struct trace_array *tr) |
| 2318 | { |
| 2319 | timerlat_tracer_stop(tr); |
Daniel Bristot de Oliveira | 2fac8d6 | 2021-10-31 19:05:02 +0100 | [diff] [blame] | 2320 | |
| 2321 | /* |
| 2322 | * If this is the last instance, reset timerlat_tracer allowing |
| 2323 | * osnoise to be started. |
| 2324 | */ |
| 2325 | if (!osnoise_has_registered_instances()) |
| 2326 | osnoise_data.timerlat_tracer = 0; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | static struct tracer timerlat_tracer __read_mostly = { |
| 2330 | .name = "timerlat", |
| 2331 | .init = timerlat_tracer_init, |
| 2332 | .reset = timerlat_tracer_reset, |
| 2333 | .start = timerlat_tracer_start, |
| 2334 | .stop = timerlat_tracer_stop, |
| 2335 | .print_header = print_timerlat_headers, |
| 2336 | .allow_instances = true, |
| 2337 | }; |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 2338 | |
| 2339 | __init static int init_timerlat_tracer(void) |
| 2340 | { |
| 2341 | return register_tracer(&timerlat_tracer); |
| 2342 | } |
| 2343 | #else /* CONFIG_TIMERLAT_TRACER */ |
| 2344 | __init static int init_timerlat_tracer(void) |
| 2345 | { |
| 2346 | return 0; |
| 2347 | } |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2348 | #endif /* CONFIG_TIMERLAT_TRACER */ |
| 2349 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2350 | __init static int init_osnoise_tracer(void) |
| 2351 | { |
| 2352 | int ret; |
| 2353 | |
| 2354 | mutex_init(&interface_lock); |
| 2355 | |
| 2356 | cpumask_copy(&osnoise_cpumask, cpu_all_mask); |
| 2357 | |
| 2358 | ret = register_tracer(&osnoise_tracer); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2359 | if (ret) { |
| 2360 | pr_err(BANNER "Error registering osnoise!\n"); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2361 | return ret; |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 2364 | ret = init_timerlat_tracer(); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2365 | if (ret) { |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 2366 | pr_err(BANNER "Error registering timerlat!\n"); |
Daniel Bristot de Oliveira | a955d7e | 2021-06-22 16:42:28 +0200 | [diff] [blame] | 2367 | return ret; |
| 2368 | } |
Daniel Bristot de Oliveira | ccb6754 | 2021-10-31 19:05:01 +0100 | [diff] [blame] | 2369 | |
Daniel Bristot de Oliveira | c8895e2 | 2021-06-22 16:42:32 +0200 | [diff] [blame] | 2370 | osnoise_init_hotplug_support(); |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2371 | |
Daniel Bristot de Oliveira | dae1813 | 2021-10-31 19:05:00 +0100 | [diff] [blame] | 2372 | INIT_LIST_HEAD_RCU(&osnoise_instances); |
| 2373 | |
Daniel Bristot de Oliveira | bce29ac | 2021-06-22 16:42:27 +0200 | [diff] [blame] | 2374 | init_tracefs(); |
| 2375 | |
| 2376 | return 0; |
| 2377 | } |
| 2378 | late_initcall(init_osnoise_tracer); |