2 * trace_hwlatdetect.c - A simple Hardware Latency detector.
4 * Use this tracer to detect large system latencies induced by the behavior of
5 * certain underlying system hardware or firmware, independent of Linux itself.
6 * The code was developed originally to detect the presence of SMIs on Intel
7 * and AMD systems, although there is no dependency upon x86 herein.
9 * The classical example usage of this tracer is in detecting the presence of
10 * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
11 * somewhat special form of hardware interrupt spawned from earlier CPU debug
12 * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
13 * LPC (or other device) to generate a special interrupt under certain
14 * circumstances, for example, upon expiration of a special SMI timer device,
15 * due to certain external thermal readings, on certain I/O address accesses,
16 * and other situations. An SMI hits a special CPU pin, triggers a special
17 * SMI mode (complete with special memory map), and the OS is unaware.
19 * Although certain hardware-inducing latencies are necessary (for example,
20 * a modern system often requires an SMI handler for correct thermal control
21 * and remote management) they can wreak havoc upon any OS-level performance
22 * guarantees toward low-latency, especially when the OS is not even made
23 * aware of the presence of these interrupts. For this reason, we need a
24 * somewhat brute force mechanism to detect these interrupts. In this case,
25 * we do it by hogging all of the CPU(s) for configurable timer intervals,
26 * sampling the built-in CPU timer, looking for discontiguous readings.
28 * WARNING: This implementation necessarily introduces latencies. Therefore,
29 * you should NEVER use this tracer while running in a production
30 * environment requiring any kind of low-latency performance
33 * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
34 * Copyright (C) 2013-2016 Steven Rostedt, Red Hat, Inc. <srostedt@redhat.com>
36 * Includes useful feedback from Clark Williams <clark@redhat.com>
38 * This file is licensed under the terms of the GNU General Public
39 * License version 2. This program is licensed "as is" without any
40 * warranty of any kind, whether express or implied.
42 #include <linux/kthread.h>
43 #include <linux/tracefs.h>
44 #include <linux/uaccess.h>
45 #include <linux/cpumask.h>
46 #include <linux/delay.h>
49 static struct trace_array *hwlat_trace;
51 #define U64STR_SIZE 22 /* 20 digits max */
53 #define BANNER "hwlat_detector: "
54 #define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
55 #define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
56 #define DEFAULT_LAT_THRESHOLD 10 /* 10us */
59 static struct task_struct *hwlat_kthread;
61 static struct dentry *hwlat_sample_width; /* sample width us */
62 static struct dentry *hwlat_sample_window; /* sample window us */
64 /* Save the previous tracing_thresh value */
65 static unsigned long save_tracing_thresh;
67 /* NMI timestamp counters */
68 static u64 nmi_ts_start;
69 static u64 nmi_total_ts;
73 /* Tells NMIs to call back to the hwlat tracer to record timestamps */
74 bool trace_hwlat_callback_enabled;
76 /* If the user changed threshold, remember it */
77 static u64 last_tracing_thresh = DEFAULT_LAT_THRESHOLD * NSEC_PER_USEC;
79 /* Individual latency samples are stored here when detected. */
81 u64 seqnum; /* unique sequence */
82 u64 duration; /* delta */
83 u64 outer_duration; /* delta (outer loop) */
84 u64 nmi_total_ts; /* Total time spent in NMIs */
85 struct timespec timestamp; /* wall time */
86 int nmi_count; /* # NMIs during this sample */
89 /* keep the global state somewhere. */
90 static struct hwlat_data {
92 struct mutex lock; /* protect changes */
94 u64 count; /* total since reset */
96 u64 sample_window; /* total sampling window (on+off) */
97 u64 sample_width; /* active sampling portion of window */
100 .sample_window = DEFAULT_SAMPLE_WINDOW,
101 .sample_width = DEFAULT_SAMPLE_WIDTH,
104 static void trace_hwlat_sample(struct hwlat_sample *sample)
106 struct trace_array *tr = hwlat_trace;
107 struct trace_event_call *call = &event_hwlat;
108 struct ring_buffer *buffer = tr->trace_buffer.buffer;
109 struct ring_buffer_event *event;
110 struct hwlat_entry *entry;
114 pc = preempt_count();
115 local_save_flags(flags);
117 event = trace_buffer_lock_reserve(buffer, TRACE_HWLAT, sizeof(*entry),
121 entry = ring_buffer_event_data(event);
122 entry->seqnum = sample->seqnum;
123 entry->duration = sample->duration;
124 entry->outer_duration = sample->outer_duration;
125 entry->timestamp = sample->timestamp;
126 entry->nmi_total_ts = sample->nmi_total_ts;
127 entry->nmi_count = sample->nmi_count;
129 if (!call_filter_check_discard(call, entry, buffer, event))
130 trace_buffer_unlock_commit_nostack(buffer, event);
133 /* Macros to encapsulate the time capturing infrastructure */
134 #define time_type u64
135 #define time_get() trace_clock_local()
136 #define time_to_us(x) div_u64(x, 1000)
137 #define time_sub(a, b) ((a) - (b))
138 #define init_time(a, b) (a = b)
139 #define time_u64(a) a
141 void trace_hwlat_callback(bool enter)
143 if (smp_processor_id() != nmi_cpu)
147 * Currently trace_clock_local() calls sched_clock() and the
148 * generic version is not NMI safe.
150 if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) {
152 nmi_ts_start = time_get();
154 nmi_total_ts = time_get() - nmi_ts_start;
162 * get_sample - sample the CPU TSC and look for likely hardware latencies
164 * Used to repeatedly capture the CPU TSC (or similar), looking for potential
165 * hardware-induced latency. Called with interrupts disabled and with
166 * hwlat_data.lock held.
168 static int get_sample(void)
170 struct trace_array *tr = hwlat_trace;
171 time_type start, t1, t2, last_t2;
172 s64 diff, total, last_total = 0;
174 u64 thresh = tracing_thresh;
175 u64 outer_sample = 0;
178 do_div(thresh, NSEC_PER_USEC); /* modifies interval value */
180 nmi_cpu = smp_processor_id();
183 /* Make sure NMIs see this first */
186 trace_hwlat_callback_enabled = true;
188 init_time(last_t2, 0);
189 start = time_get(); /* start timestamp */
193 t1 = time_get(); /* we'll look for a discontinuity */
196 if (time_u64(last_t2)) {
197 /* Check the delta from outer loop (t2 to next t1) */
198 diff = time_to_us(time_sub(t1, last_t2));
199 /* This shouldn't happen */
201 pr_err(BANNER "time running backwards\n");
204 if (diff > outer_sample)
209 total = time_to_us(time_sub(t2, start)); /* sample width */
211 /* Check for possible overflows */
212 if (total < last_total) {
213 pr_err("Time total overflowed\n");
218 /* This checks the inner loop (t1 to t2) */
219 diff = time_to_us(time_sub(t2, t1)); /* current diff */
221 /* This shouldn't happen */
223 pr_err(BANNER "time running backwards\n");
228 sample = diff; /* only want highest value */
230 } while (total <= hwlat_data.sample_width);
232 barrier(); /* finish the above in the view for NMIs */
233 trace_hwlat_callback_enabled = false;
234 barrier(); /* Make sure nmi_total_ts is no longer updated */
238 /* If we exceed the threshold value, we have found a hardware latency */
239 if (sample > thresh || outer_sample > thresh) {
240 struct hwlat_sample s;
244 /* We read in microseconds */
246 do_div(nmi_total_ts, NSEC_PER_USEC);
249 s.seqnum = hwlat_data.count;
251 s.outer_duration = outer_sample;
252 s.timestamp = CURRENT_TIME;
253 s.nmi_total_ts = nmi_total_ts;
254 s.nmi_count = nmi_count;
255 trace_hwlat_sample(&s);
257 /* Keep a running maximum ever recorded hardware latency */
258 if (sample > tr->max_latency)
259 tr->max_latency = sample;
266 static struct cpumask save_cpumask;
267 static bool disable_migrate;
269 static void move_to_next_cpu(bool initmask)
271 static struct cpumask *current_mask;
277 /* Just pick the first CPU on first iteration */
279 current_mask = &save_cpumask;
281 cpumask_and(current_mask, cpu_online_mask, tracing_buffer_mask);
283 next_cpu = cpumask_first(current_mask);
288 * If for some reason the user modifies the CPU affinity
289 * of this thread, than stop migrating for the duration
290 * of the current test.
292 if (!cpumask_equal(current_mask, ¤t->cpus_allowed))
296 cpumask_and(current_mask, cpu_online_mask, tracing_buffer_mask);
297 next_cpu = cpumask_next(smp_processor_id(), current_mask);
300 if (next_cpu >= nr_cpu_ids)
301 next_cpu = cpumask_first(current_mask);
304 if (next_cpu >= nr_cpu_ids) /* Shouldn't happen! */
307 cpumask_clear(current_mask);
308 cpumask_set_cpu(next_cpu, current_mask);
310 sched_setaffinity(0, current_mask);
314 disable_migrate = true;
318 * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
320 * Used to periodically sample the CPU TSC via a call to get_sample. We
321 * disable interrupts, which does (intentionally) introduce latency since we
322 * need to ensure nothing else might be running (and thus preempting).
323 * Obviously this should never be used in production environments.
325 * Currently this runs on which ever CPU it was scheduled on, but most
326 * real-world hardware latency situations occur across several CPUs,
327 * but we might later generalize this if we find there are any actualy
328 * systems with alternate SMI delivery or other hardware latencies.
330 static int kthread_fn(void *data)
333 bool initmask = true;
335 while (!kthread_should_stop()) {
337 move_to_next_cpu(initmask);
344 mutex_lock(&hwlat_data.lock);
345 interval = hwlat_data.sample_window - hwlat_data.sample_width;
346 mutex_unlock(&hwlat_data.lock);
348 do_div(interval, USEC_PER_MSEC); /* modifies interval value */
350 /* Always sleep for at least 1ms */
354 if (msleep_interruptible(interval))
362 * start_kthread - Kick off the hardware latency sampling/detector kthread
364 * This starts the kernel thread that will sit and sample the CPU timestamp
365 * counter (TSC or similar) and look for potential hardware latencies.
367 static int start_kthread(struct trace_array *tr)
369 struct task_struct *kthread;
371 kthread = kthread_create(kthread_fn, NULL, "hwlatd");
372 if (IS_ERR(kthread)) {
373 pr_err(BANNER "could not start sampling thread\n");
376 hwlat_kthread = kthread;
377 wake_up_process(kthread);
383 * stop_kthread - Inform the hardware latency samping/detector kthread to stop
385 * This kicks the running hardware latency sampling/detector kernel thread and
386 * tells it to stop sampling now. Use this on unload and at system shutdown.
388 static void stop_kthread(void)
392 kthread_stop(hwlat_kthread);
393 hwlat_kthread = NULL;
397 * hwlat_read - Wrapper read function for reading both window and width
398 * @filp: The active open file structure
399 * @ubuf: The userspace provided buffer to read value into
400 * @cnt: The maximum number of bytes to read
401 * @ppos: The current "file" position
403 * This function provides a generic read implementation for the global state
404 * "hwlat_data" structure filesystem entries.
406 static ssize_t hwlat_read(struct file *filp, char __user *ubuf,
407 size_t cnt, loff_t *ppos)
409 char buf[U64STR_SIZE];
410 u64 *entry = filp->private_data;
417 if (cnt > sizeof(buf))
422 len = snprintf(buf, sizeof(buf), "%llu\n", val);
424 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
428 * hwlat_width_write - Write function for "width" entry
429 * @filp: The active open file structure
430 * @ubuf: The user buffer that contains the value to write
431 * @cnt: The maximum number of bytes to write to "file"
432 * @ppos: The current position in @file
434 * This function provides a write implementation for the "width" interface
435 * to the hardware latency detector. It can be used to configure
436 * for how many us of the total window us we will actively sample for any
437 * hardware-induced latency periods. Obviously, it is not possible to
438 * sample constantly and have the system respond to a sample reader, or,
439 * worse, without having the system appear to have gone out to lunch. It
440 * is enforced that width is less that the total window size.
443 hwlat_width_write(struct file *filp, const char __user *ubuf,
444 size_t cnt, loff_t *ppos)
449 err = kstrtoull_from_user(ubuf, cnt, 10, &val);
453 mutex_lock(&hwlat_data.lock);
454 if (val < hwlat_data.sample_window)
455 hwlat_data.sample_width = val;
458 mutex_unlock(&hwlat_data.lock);
467 * hwlat_window_write - Write function for "window" entry
468 * @filp: The active open file structure
469 * @ubuf: The user buffer that contains the value to write
470 * @cnt: The maximum number of bytes to write to "file"
471 * @ppos: The current position in @file
473 * This function provides a write implementation for the "window" interface
474 * to the hardware latency detetector. The window is the total time
475 * in us that will be considered one sample period. Conceptually, windows
476 * occur back-to-back and contain a sample width period during which
477 * actual sampling occurs. Can be used to write a new total window size. It
478 * is enfoced that any value written must be greater than the sample width
479 * size, or an error results.
482 hwlat_window_write(struct file *filp, const char __user *ubuf,
483 size_t cnt, loff_t *ppos)
488 err = kstrtoull_from_user(ubuf, cnt, 10, &val);
492 mutex_lock(&hwlat_data.lock);
493 if (hwlat_data.sample_width < val)
494 hwlat_data.sample_window = val;
497 mutex_unlock(&hwlat_data.lock);
505 static const struct file_operations width_fops = {
506 .open = tracing_open_generic,
508 .write = hwlat_width_write,
511 static const struct file_operations window_fops = {
512 .open = tracing_open_generic,
514 .write = hwlat_window_write,
518 * init_tracefs - A function to initialize the tracefs interface files
520 * This function creates entries in tracefs for "hwlat_detector".
521 * It creates the hwlat_detector directory in the tracing directory,
522 * and within that directory is the count, width and window files to
523 * change and view those values.
525 static int init_tracefs(void)
527 struct dentry *d_tracer;
528 struct dentry *top_dir;
530 d_tracer = tracing_init_dentry();
531 if (IS_ERR(d_tracer))
534 top_dir = tracefs_create_dir("hwlat_detector", d_tracer);
538 hwlat_sample_window = tracefs_create_file("window", 0640,
540 &hwlat_data.sample_window,
542 if (!hwlat_sample_window)
545 hwlat_sample_width = tracefs_create_file("width", 0644,
547 &hwlat_data.sample_width,
549 if (!hwlat_sample_width)
555 tracefs_remove_recursive(top_dir);
559 static void hwlat_tracer_start(struct trace_array *tr)
563 err = start_kthread(tr);
565 pr_err(BANNER "Cannot start hwlat kthread\n");
568 static void hwlat_tracer_stop(struct trace_array *tr)
573 static bool hwlat_busy;
575 static int hwlat_tracer_init(struct trace_array *tr)
577 /* Only allow one instance to enable this */
583 disable_migrate = false;
584 hwlat_data.count = 0;
586 save_tracing_thresh = tracing_thresh;
588 /* tracing_thresh is in nsecs, we speak in usecs */
590 tracing_thresh = last_tracing_thresh;
592 if (tracer_tracing_is_on(tr))
593 hwlat_tracer_start(tr);
600 static void hwlat_tracer_reset(struct trace_array *tr)
604 /* the tracing threshold is static between runs */
605 last_tracing_thresh = tracing_thresh;
607 tracing_thresh = save_tracing_thresh;
611 static struct tracer hwlat_tracer __read_mostly =
614 .init = hwlat_tracer_init,
615 .reset = hwlat_tracer_reset,
616 .start = hwlat_tracer_start,
617 .stop = hwlat_tracer_stop,
618 .allow_instances = true,
621 __init static int init_hwlat_tracer(void)
625 mutex_init(&hwlat_data.lock);
627 ret = register_tracer(&hwlat_tracer);
635 late_initcall(init_hwlat_tracer);