// SPDX-License-Identifier: GPL-2.0 /* * trace_hwlat.c - A simple Hardware Latency detector. * * Use this tracer to detect large system latencies induced by the behavior of * certain underlying system hardware or firmware, independent of Linux itself. * The code was developed originally to detect the presence of SMIs on Intel * and AMD systems, although there is no dependency upon x86 herein. * * The classical example usage of this tracer is in detecting the presence of * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a * somewhat special form of hardware interrupt spawned from earlier CPU debug * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge * LPC (or other device) to generate a special interrupt under certain * circumstances, for example, upon expiration of a special SMI timer device, * due to certain external thermal readings, on certain I/O address accesses, * and other situations. An SMI hits a special CPU pin, triggers a special * SMI mode (complete with special memory map), and the OS is unaware. * * Although certain hardware-inducing latencies are necessary (for example, * a modern system often requires an SMI handler for correct thermal control * and remote management) they can wreak havoc upon any OS-level performance * guarantees toward low-latency, especially when the OS is not even made * aware of the presence of these interrupts. For this reason, we need a * somewhat brute force mechanism to detect these interrupts. In this case, * we do it by hogging all of the CPU(s) for configurable timer intervals, * sampling the built-in CPU timer, looking for discontiguous readings. * * WARNING: This implementation necessarily introduces latencies. Therefore, * you should NEVER use this tracer while running in a production * environment requiring any kind of low-latency performance * guarantee(s). * * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com> * Copyright (C) 2013-2016 Steven Rostedt, Red Hat, Inc. <srostedt@redhat.com> * * Includes useful feedback from Clark Williams <williams@redhat.com> *
*/ #include <linux/kthread.h> #include <linux/tracefs.h> #include <linux/uaccess.h> #include <linux/cpumask.h> #include <linux/delay.h> #include <linux/sched/clock.h> #include"trace.h"
/* Macros to encapsulate the time capturing infrastructure */ #define time_type u64 #define time_get() trace_clock_local() #define time_to_us(x) div_u64(x, 1000) #define time_sub(a, b) ((a) - (b)) #define init_time(a, b) (a = b) #define time_u64(a) a
/* * Currently trace_clock_local() calls sched_clock() and the * generic version is not NMI safe.
*/ if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) { if (enter)
kdata->nmi_ts_start = time_get(); else
kdata->nmi_total_ts += time_get() - kdata->nmi_ts_start;
}
t1 = time_get(); /* we'll look for a discontinuity */
t2 = time_get();
if (time_u64(last_t2)) { /* Check the delta from outer loop (t2 to next t1) */
outer_diff = time_to_us(time_sub(t1, last_t2)); /* This shouldn't happen */ if (outer_diff < 0) {
hwlat_err(BANNER "time running backwards\n"); goto out;
} if (outer_diff > outer_sample)
outer_sample = outer_diff;
}
last_t2 = t2;
total = time_to_us(time_sub(t2, start)); /* sample width */
/* Check for possible overflows */ if (total < last_total) {
hwlat_err("Time total overflowed\n"); break;
}
last_total = total;
/* This checks the inner loop (t1 to t2) */
diff = time_to_us(time_sub(t2, t1)); /* current diff */
if (diff > thresh || outer_diff > thresh) { if (!count)
ktime_get_real_ts64(&s.timestamp);
count++;
}
/* This shouldn't happen */ if (diff < 0) {
hwlat_err(BANNER "time running backwards\n"); goto out;
}
if (diff > sample)
sample = diff; /* only want highest value */
} while (total <= hwlat_data.sample_width);
barrier(); /* finish the above in the view for NMIs */
trace_hwlat_callback_enabled = false;
barrier(); /* Make sure nmi_total_ts is no longer updated */
ret = 0;
/* If we exceed the threshold value, we have found a hardware latency */ if (sample > thresh || outer_sample > thresh) {
u64 latency;
ret = 1;
/* We read in microseconds */ if (kdata->nmi_total_ts)
do_div(kdata->nmi_total_ts, NSEC_PER_USEC);
/* * If for some reason the user modifies the CPU affinity * of this thread, then stop migrating for the duration * of the current test.
*/ if (!cpumask_equal(current_mask, current->cpus_ptr)) goto change_mode;
change_mode:
hwlat_data.thread_mode = MODE_NONE;
pr_info(BANNER "cpumask changed while in round-robin mode, switching to mode none\n");
}
/* * kthread_fn - The CPU time sampling/hardware latency detection kernel thread * * Used to periodically sample the CPU TSC via a call to get_sample. We * disable interrupts, which does (intentionally) introduce latency since we * need to ensure nothing else might be running (and thus preempting). * Obviously this should never be used in production environments. * * Executes one loop interaction on each CPU in tracing_cpumask sysfs file.
*/ staticint kthread_fn(void *data)
{
u64 interval;
while (!kthread_should_stop()) {
if (hwlat_data.thread_mode == MODE_ROUND_ROBIN)
move_to_next_cpu();
do_div(interval, USEC_PER_MSEC); /* modifies interval value */
/* Always sleep for at least 1ms */ if (interval < 1)
interval = 1;
if (msleep_interruptible(interval)) break;
}
return 0;
}
/* * stop_stop_kthread - Inform the hardware latency sampling/detector kthread to stop * * This kicks the running hardware latency sampling/detector kernel thread and * tells it to stop sampling now. Use this on unload and at system shutdown.
*/ staticvoid stop_single_kthread(void)
{ struct hwlat_kthread_data *kdata = get_cpu_data(); struct task_struct *kthread;
cpus_read_lock();
kthread = kdata->kthread;
if (!kthread) goto out_put_cpus;
kthread_stop(kthread);
kdata->kthread = NULL;
out_put_cpus:
cpus_read_unlock();
}
/* * start_single_kthread - Kick off the hardware latency sampling/detector kthread * * This starts the kernel thread that will sit and sample the CPU timestamp * counter (TSC or similar) and look for potential hardware latencies.
*/ staticint start_single_kthread(struct trace_array *tr)
{ struct hwlat_kthread_data *kdata = get_cpu_data(); struct cpumask *current_mask = &save_cpumask; struct task_struct *kthread; int next_cpu;
cpus_read_lock(); if (kdata->kthread) goto out_put_cpus;
kthread = kthread_create(kthread_fn, NULL, "hwlatd"); if (IS_ERR(kthread)) {
pr_err(BANNER "could not start sampling thread\n");
cpus_read_unlock(); return -ENOMEM;
}
/* Just pick the first CPU on first iteration */
cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
/* * stop_per_cpu_kthreads - Inform the hardware latency sampling/detector kthread to stop * * This kicks the running hardware latency sampling/detector kernel threads and * tells it to stop sampling now. Use this on unload and at system shutdown.
*/ staticvoid stop_per_cpu_kthreads(void)
{ unsignedint cpu;
/* * hwlat_cpu_init - CPU hotplug online callback function
*/ staticint hwlat_cpu_init(unsignedint cpu)
{
schedule_work_on(cpu, &hwlat_hotplug_work); return 0;
}
/* * hwlat_cpu_die - CPU hotplug offline callback function
*/ staticint hwlat_cpu_die(unsignedint cpu)
{
stop_cpu_kthread(cpu); return 0;
}
staticvoid hwlat_init_hotplug_support(void)
{ int ret;
ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "trace/hwlat:online",
hwlat_cpu_init, hwlat_cpu_die); if (ret < 0)
pr_warn(BANNER "Error to init cpu hotplug support\n");
/* * start_per_cpu_kthreads - Kick off the hardware latency sampling/detector kthreads * * This starts the kernel threads that will sit on potentially all cpus and * sample the CPU timestamp counter (TSC or similar) and look for potential * hardware latencies.
*/ staticint start_per_cpu_kthreads(struct trace_array *tr)
{ struct cpumask *current_mask = &save_cpumask; unsignedint cpu; int retval;
cpus_read_lock(); /* * Run only on CPUs in which hwlat is allowed to run.
*/
cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
/** * hwlat_mode_write - Write function for "mode" entry * @filp: The active open file structure * @ubuf: The user buffer that contains the value to write * @cnt: The maximum number of bytes to write to "file" * @ppos: The current position in @file * * This function provides a write implementation for the "mode" interface * to the hardware latency detector. hwlatd has different operation modes. * The "none" sets the allowed cpumask for a single hwlatd thread at the * startup and lets the scheduler handle the migration. The default mode is * the "round-robin" one, in which a single hwlatd thread runs, migrating * among the allowed CPUs in a round-robin fashion. The "per-cpu" mode * creates one hwlatd thread per allowed CPU.
*/ static ssize_t hwlat_mode_write(struct file *filp, constchar __user *ubuf,
size_t cnt, loff_t *ppos)
{ struct trace_array *tr = hwlat_trace; constchar *mode; char buf[64]; int ret, i;
if (cnt >= sizeof(buf)) return -EINVAL;
if (copy_from_user(buf, ubuf, cnt)) return -EFAULT;
buf[cnt] = 0;
mode = strstrip(buf);
ret = -EINVAL;
/* * trace_types_lock is taken to avoid concurrency on start/stop * and hwlat_busy.
*/
mutex_lock(&trace_types_lock); if (hwlat_busy)
hwlat_tracer_stop(tr);
mutex_lock(&hwlat_data.lock);
for (i = 0; i < MODE_MAX; i++) { if (strcmp(mode, thread_mode_str[i]) == 0) {
hwlat_data.thread_mode = i;
ret = cnt;
}
}
mutex_unlock(&hwlat_data.lock);
if (hwlat_busy)
hwlat_tracer_start(tr);
mutex_unlock(&trace_types_lock);
*ppos += cnt;
return ret;
}
/* * The width parameter is read/write using the generic trace_min_max_param * method. The *val is protected by the hwlat_data lock and is upper * bounded by the window parameter.
*/ staticstruct trace_min_max_param hwlat_width = {
.lock = &hwlat_data.lock,
.val = &hwlat_data.sample_width,
.max = &hwlat_data.sample_window,
.min = NULL,
};
/* * The window parameter is read/write using the generic trace_min_max_param * method. The *val is protected by the hwlat_data lock and is lower * bounded by the width parameter.
*/ staticstruct trace_min_max_param hwlat_window = {
.lock = &hwlat_data.lock,
.val = &hwlat_data.sample_window,
.max = NULL,
.min = &hwlat_data.sample_width,
};
staticconststruct file_operations thread_mode_fops = {
.open = hwlat_mode_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
.write = hwlat_mode_write
}; /** * init_tracefs - A function to initialize the tracefs interface files * * This function creates entries in tracefs for "hwlat_detector". * It creates the hwlat_detector directory in the tracing directory, * and within that directory is the count, width and window files to * change and view those values.
*/ staticint init_tracefs(void)
{ int ret; struct dentry *top_dir;
ret = tracing_init_dentry(); if (ret) return -ENOMEM;
top_dir = tracefs_create_dir("hwlat_detector", NULL); if (!top_dir) return -ENOMEM;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.