Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * trace_hwlatdetect.c - A simple Hardware Latency detector. |
| 3 | * |
| 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. |
| 8 | * |
| 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. |
| 18 | * |
| 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. |
| 27 | * |
| 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 |
| 31 | * guarantee(s). |
| 32 | * |
| 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> |
| 35 | * |
| 36 | * Includes useful feedback from Clark Williams <clark@redhat.com> |
| 37 | * |
| 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. |
| 41 | */ |
| 42 | #include <linux/kthread.h> |
| 43 | #include <linux/tracefs.h> |
| 44 | #include <linux/uaccess.h> |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 45 | #include <linux/cpumask.h> |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 46 | #include <linux/delay.h> |
| 47 | #include "trace.h" |
| 48 | |
| 49 | static struct trace_array *hwlat_trace; |
| 50 | |
| 51 | #define U64STR_SIZE 22 /* 20 digits max */ |
| 52 | |
| 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 */ |
| 57 | |
| 58 | /* sampling thread*/ |
| 59 | static struct task_struct *hwlat_kthread; |
| 60 | |
| 61 | static struct dentry *hwlat_sample_width; /* sample width us */ |
| 62 | static struct dentry *hwlat_sample_window; /* sample window us */ |
| 63 | |
| 64 | /* Save the previous tracing_thresh value */ |
| 65 | static unsigned long save_tracing_thresh; |
| 66 | |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 67 | /* NMI timestamp counters */ |
| 68 | static u64 nmi_ts_start; |
| 69 | static u64 nmi_total_ts; |
| 70 | static int nmi_count; |
| 71 | static int nmi_cpu; |
| 72 | |
| 73 | /* Tells NMIs to call back to the hwlat tracer to record timestamps */ |
| 74 | bool trace_hwlat_callback_enabled; |
| 75 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 76 | /* If the user changed threshold, remember it */ |
| 77 | static u64 last_tracing_thresh = DEFAULT_LAT_THRESHOLD * NSEC_PER_USEC; |
| 78 | |
| 79 | /* Individual latency samples are stored here when detected. */ |
| 80 | struct hwlat_sample { |
| 81 | u64 seqnum; /* unique sequence */ |
| 82 | u64 duration; /* delta */ |
| 83 | u64 outer_duration; /* delta (outer loop) */ |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 84 | u64 nmi_total_ts; /* Total time spent in NMIs */ |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 85 | struct timespec timestamp; /* wall time */ |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 86 | int nmi_count; /* # NMIs during this sample */ |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /* keep the global state somewhere. */ |
| 90 | static struct hwlat_data { |
| 91 | |
| 92 | struct mutex lock; /* protect changes */ |
| 93 | |
| 94 | u64 count; /* total since reset */ |
| 95 | |
| 96 | u64 sample_window; /* total sampling window (on+off) */ |
| 97 | u64 sample_width; /* active sampling portion of window */ |
| 98 | |
| 99 | } hwlat_data = { |
| 100 | .sample_window = DEFAULT_SAMPLE_WINDOW, |
| 101 | .sample_width = DEFAULT_SAMPLE_WIDTH, |
| 102 | }; |
| 103 | |
| 104 | static void trace_hwlat_sample(struct hwlat_sample *sample) |
| 105 | { |
| 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; |
| 111 | unsigned long flags; |
| 112 | int pc; |
| 113 | |
| 114 | pc = preempt_count(); |
| 115 | local_save_flags(flags); |
| 116 | |
| 117 | event = trace_buffer_lock_reserve(buffer, TRACE_HWLAT, sizeof(*entry), |
| 118 | flags, pc); |
| 119 | if (!event) |
| 120 | return; |
| 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; |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 126 | entry->nmi_total_ts = sample->nmi_total_ts; |
| 127 | entry->nmi_count = sample->nmi_count; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 128 | |
| 129 | if (!call_filter_check_discard(call, entry, buffer, event)) |
| 130 | __buffer_unlock_commit(buffer, event); |
| 131 | } |
| 132 | |
| 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 |
| 140 | |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 141 | void trace_hwlat_callback(bool enter) |
| 142 | { |
| 143 | if (smp_processor_id() != nmi_cpu) |
| 144 | return; |
| 145 | |
| 146 | /* |
| 147 | * Currently trace_clock_local() calls sched_clock() and the |
| 148 | * generic version is not NMI safe. |
| 149 | */ |
| 150 | if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) { |
| 151 | if (enter) |
| 152 | nmi_ts_start = time_get(); |
| 153 | else |
Srivatsa S. Bhat (VMware) | 81e3f1d | 2019-10-10 11:50:46 -0700 | [diff] [blame] | 154 | nmi_total_ts += time_get() - nmi_ts_start; |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (enter) |
| 158 | nmi_count++; |
| 159 | } |
| 160 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 161 | /** |
| 162 | * get_sample - sample the CPU TSC and look for likely hardware latencies |
| 163 | * |
| 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. |
| 167 | */ |
| 168 | static int get_sample(void) |
| 169 | { |
| 170 | struct trace_array *tr = hwlat_trace; |
| 171 | time_type start, t1, t2, last_t2; |
| 172 | s64 diff, total, last_total = 0; |
| 173 | u64 sample = 0; |
| 174 | u64 thresh = tracing_thresh; |
| 175 | u64 outer_sample = 0; |
| 176 | int ret = -1; |
| 177 | |
| 178 | do_div(thresh, NSEC_PER_USEC); /* modifies interval value */ |
| 179 | |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 180 | nmi_cpu = smp_processor_id(); |
| 181 | nmi_total_ts = 0; |
| 182 | nmi_count = 0; |
| 183 | /* Make sure NMIs see this first */ |
| 184 | barrier(); |
| 185 | |
| 186 | trace_hwlat_callback_enabled = true; |
| 187 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 188 | init_time(last_t2, 0); |
| 189 | start = time_get(); /* start timestamp */ |
| 190 | |
| 191 | do { |
| 192 | |
| 193 | t1 = time_get(); /* we'll look for a discontinuity */ |
| 194 | t2 = time_get(); |
| 195 | |
| 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 */ |
| 200 | if (diff < 0) { |
| 201 | pr_err(BANNER "time running backwards\n"); |
| 202 | goto out; |
| 203 | } |
| 204 | if (diff > outer_sample) |
| 205 | outer_sample = diff; |
| 206 | } |
| 207 | last_t2 = t2; |
| 208 | |
| 209 | total = time_to_us(time_sub(t2, start)); /* sample width */ |
| 210 | |
| 211 | /* Check for possible overflows */ |
| 212 | if (total < last_total) { |
| 213 | pr_err("Time total overflowed\n"); |
| 214 | break; |
| 215 | } |
| 216 | last_total = total; |
| 217 | |
| 218 | /* This checks the inner loop (t1 to t2) */ |
| 219 | diff = time_to_us(time_sub(t2, t1)); /* current diff */ |
| 220 | |
| 221 | /* This shouldn't happen */ |
| 222 | if (diff < 0) { |
| 223 | pr_err(BANNER "time running backwards\n"); |
| 224 | goto out; |
| 225 | } |
| 226 | |
| 227 | if (diff > sample) |
| 228 | sample = diff; /* only want highest value */ |
| 229 | |
| 230 | } while (total <= hwlat_data.sample_width); |
| 231 | |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 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 */ |
| 235 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 236 | ret = 0; |
| 237 | |
| 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; |
| 241 | |
| 242 | ret = 1; |
| 243 | |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 244 | /* We read in microseconds */ |
| 245 | if (nmi_total_ts) |
| 246 | do_div(nmi_total_ts, NSEC_PER_USEC); |
| 247 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 248 | hwlat_data.count++; |
| 249 | s.seqnum = hwlat_data.count; |
| 250 | s.duration = sample; |
| 251 | s.outer_duration = outer_sample; |
| 252 | s.timestamp = CURRENT_TIME; |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 253 | s.nmi_total_ts = nmi_total_ts; |
| 254 | s.nmi_count = nmi_count; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 255 | trace_hwlat_sample(&s); |
| 256 | |
| 257 | /* Keep a running maximum ever recorded hardware latency */ |
| 258 | if (sample > tr->max_latency) |
| 259 | tr->max_latency = sample; |
Srivatsa S. Bhat (VMware) | d9df3aa | 2019-10-10 11:51:01 -0700 | [diff] [blame] | 260 | if (outer_sample > tr->max_latency) |
| 261 | tr->max_latency = outer_sample; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | out: |
| 265 | return ret; |
| 266 | } |
| 267 | |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 268 | static struct cpumask save_cpumask; |
| 269 | static bool disable_migrate; |
| 270 | |
Steven Rostedt (VMware) | a93ae8d | 2017-01-30 19:27:10 -0500 | [diff] [blame] | 271 | static void move_to_next_cpu(bool initmask) |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 272 | { |
| 273 | static struct cpumask *current_mask; |
| 274 | int next_cpu; |
| 275 | |
| 276 | if (disable_migrate) |
| 277 | return; |
| 278 | |
| 279 | /* Just pick the first CPU on first iteration */ |
Steven Rostedt (VMware) | a93ae8d | 2017-01-30 19:27:10 -0500 | [diff] [blame] | 280 | if (initmask) { |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 281 | current_mask = &save_cpumask; |
| 282 | get_online_cpus(); |
| 283 | cpumask_and(current_mask, cpu_online_mask, tracing_buffer_mask); |
| 284 | put_online_cpus(); |
| 285 | next_cpu = cpumask_first(current_mask); |
| 286 | goto set_affinity; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * If for some reason the user modifies the CPU affinity |
| 291 | * of this thread, than stop migrating for the duration |
| 292 | * of the current test. |
| 293 | */ |
| 294 | if (!cpumask_equal(current_mask, ¤t->cpus_allowed)) |
| 295 | goto disable; |
| 296 | |
| 297 | get_online_cpus(); |
| 298 | cpumask_and(current_mask, cpu_online_mask, tracing_buffer_mask); |
| 299 | next_cpu = cpumask_next(smp_processor_id(), current_mask); |
| 300 | put_online_cpus(); |
| 301 | |
| 302 | if (next_cpu >= nr_cpu_ids) |
| 303 | next_cpu = cpumask_first(current_mask); |
| 304 | |
| 305 | set_affinity: |
| 306 | if (next_cpu >= nr_cpu_ids) /* Shouldn't happen! */ |
| 307 | goto disable; |
| 308 | |
| 309 | cpumask_clear(current_mask); |
| 310 | cpumask_set_cpu(next_cpu, current_mask); |
| 311 | |
| 312 | sched_setaffinity(0, current_mask); |
| 313 | return; |
| 314 | |
| 315 | disable: |
| 316 | disable_migrate = true; |
| 317 | } |
| 318 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 319 | /* |
| 320 | * kthread_fn - The CPU time sampling/hardware latency detection kernel thread |
| 321 | * |
| 322 | * Used to periodically sample the CPU TSC via a call to get_sample. We |
| 323 | * disable interrupts, which does (intentionally) introduce latency since we |
| 324 | * need to ensure nothing else might be running (and thus preempting). |
| 325 | * Obviously this should never be used in production environments. |
| 326 | * |
| 327 | * Currently this runs on which ever CPU it was scheduled on, but most |
| 328 | * real-world hardware latency situations occur across several CPUs, |
| 329 | * but we might later generalize this if we find there are any actualy |
| 330 | * systems with alternate SMI delivery or other hardware latencies. |
| 331 | */ |
| 332 | static int kthread_fn(void *data) |
| 333 | { |
| 334 | u64 interval; |
Steven Rostedt (VMware) | a93ae8d | 2017-01-30 19:27:10 -0500 | [diff] [blame] | 335 | bool initmask = true; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 336 | |
| 337 | while (!kthread_should_stop()) { |
| 338 | |
Steven Rostedt (VMware) | a93ae8d | 2017-01-30 19:27:10 -0500 | [diff] [blame] | 339 | move_to_next_cpu(initmask); |
| 340 | initmask = false; |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 341 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 342 | local_irq_disable(); |
| 343 | get_sample(); |
| 344 | local_irq_enable(); |
| 345 | |
| 346 | mutex_lock(&hwlat_data.lock); |
| 347 | interval = hwlat_data.sample_window - hwlat_data.sample_width; |
| 348 | mutex_unlock(&hwlat_data.lock); |
| 349 | |
| 350 | do_div(interval, USEC_PER_MSEC); /* modifies interval value */ |
| 351 | |
| 352 | /* Always sleep for at least 1ms */ |
| 353 | if (interval < 1) |
| 354 | interval = 1; |
| 355 | |
| 356 | if (msleep_interruptible(interval)) |
| 357 | break; |
| 358 | } |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * start_kthread - Kick off the hardware latency sampling/detector kthread |
| 365 | * |
| 366 | * This starts the kernel thread that will sit and sample the CPU timestamp |
| 367 | * counter (TSC or similar) and look for potential hardware latencies. |
| 368 | */ |
| 369 | static int start_kthread(struct trace_array *tr) |
| 370 | { |
| 371 | struct task_struct *kthread; |
| 372 | |
| 373 | kthread = kthread_create(kthread_fn, NULL, "hwlatd"); |
| 374 | if (IS_ERR(kthread)) { |
| 375 | pr_err(BANNER "could not start sampling thread\n"); |
| 376 | return -ENOMEM; |
| 377 | } |
| 378 | hwlat_kthread = kthread; |
| 379 | wake_up_process(kthread); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * stop_kthread - Inform the hardware latency samping/detector kthread to stop |
| 386 | * |
| 387 | * This kicks the running hardware latency sampling/detector kernel thread and |
| 388 | * tells it to stop sampling now. Use this on unload and at system shutdown. |
| 389 | */ |
| 390 | static void stop_kthread(void) |
| 391 | { |
| 392 | if (!hwlat_kthread) |
| 393 | return; |
| 394 | kthread_stop(hwlat_kthread); |
| 395 | hwlat_kthread = NULL; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * hwlat_read - Wrapper read function for reading both window and width |
| 400 | * @filp: The active open file structure |
| 401 | * @ubuf: The userspace provided buffer to read value into |
| 402 | * @cnt: The maximum number of bytes to read |
| 403 | * @ppos: The current "file" position |
| 404 | * |
| 405 | * This function provides a generic read implementation for the global state |
| 406 | * "hwlat_data" structure filesystem entries. |
| 407 | */ |
| 408 | static ssize_t hwlat_read(struct file *filp, char __user *ubuf, |
| 409 | size_t cnt, loff_t *ppos) |
| 410 | { |
| 411 | char buf[U64STR_SIZE]; |
| 412 | u64 *entry = filp->private_data; |
| 413 | u64 val; |
| 414 | int len; |
| 415 | |
| 416 | if (!entry) |
| 417 | return -EFAULT; |
| 418 | |
| 419 | if (cnt > sizeof(buf)) |
| 420 | cnt = sizeof(buf); |
| 421 | |
| 422 | val = *entry; |
| 423 | |
| 424 | len = snprintf(buf, sizeof(buf), "%llu\n", val); |
| 425 | |
| 426 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, len); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * hwlat_width_write - Write function for "width" entry |
| 431 | * @filp: The active open file structure |
| 432 | * @ubuf: The user buffer that contains the value to write |
| 433 | * @cnt: The maximum number of bytes to write to "file" |
| 434 | * @ppos: The current position in @file |
| 435 | * |
| 436 | * This function provides a write implementation for the "width" interface |
| 437 | * to the hardware latency detector. It can be used to configure |
| 438 | * for how many us of the total window us we will actively sample for any |
| 439 | * hardware-induced latency periods. Obviously, it is not possible to |
| 440 | * sample constantly and have the system respond to a sample reader, or, |
| 441 | * worse, without having the system appear to have gone out to lunch. It |
| 442 | * is enforced that width is less that the total window size. |
| 443 | */ |
| 444 | static ssize_t |
| 445 | hwlat_width_write(struct file *filp, const char __user *ubuf, |
| 446 | size_t cnt, loff_t *ppos) |
| 447 | { |
| 448 | u64 val; |
| 449 | int err; |
| 450 | |
| 451 | err = kstrtoull_from_user(ubuf, cnt, 10, &val); |
| 452 | if (err) |
| 453 | return err; |
| 454 | |
| 455 | mutex_lock(&hwlat_data.lock); |
| 456 | if (val < hwlat_data.sample_window) |
| 457 | hwlat_data.sample_width = val; |
| 458 | else |
| 459 | err = -EINVAL; |
| 460 | mutex_unlock(&hwlat_data.lock); |
| 461 | |
| 462 | if (err) |
| 463 | return err; |
| 464 | |
| 465 | return cnt; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * hwlat_window_write - Write function for "window" entry |
| 470 | * @filp: The active open file structure |
| 471 | * @ubuf: The user buffer that contains the value to write |
| 472 | * @cnt: The maximum number of bytes to write to "file" |
| 473 | * @ppos: The current position in @file |
| 474 | * |
| 475 | * This function provides a write implementation for the "window" interface |
| 476 | * to the hardware latency detetector. The window is the total time |
| 477 | * in us that will be considered one sample period. Conceptually, windows |
| 478 | * occur back-to-back and contain a sample width period during which |
| 479 | * actual sampling occurs. Can be used to write a new total window size. It |
| 480 | * is enfoced that any value written must be greater than the sample width |
| 481 | * size, or an error results. |
| 482 | */ |
| 483 | static ssize_t |
| 484 | hwlat_window_write(struct file *filp, const char __user *ubuf, |
| 485 | size_t cnt, loff_t *ppos) |
| 486 | { |
| 487 | u64 val; |
| 488 | int err; |
| 489 | |
| 490 | err = kstrtoull_from_user(ubuf, cnt, 10, &val); |
| 491 | if (err) |
| 492 | return err; |
| 493 | |
| 494 | mutex_lock(&hwlat_data.lock); |
| 495 | if (hwlat_data.sample_width < val) |
| 496 | hwlat_data.sample_window = val; |
| 497 | else |
| 498 | err = -EINVAL; |
| 499 | mutex_unlock(&hwlat_data.lock); |
| 500 | |
| 501 | if (err) |
| 502 | return err; |
| 503 | |
| 504 | return cnt; |
| 505 | } |
| 506 | |
| 507 | static const struct file_operations width_fops = { |
| 508 | .open = tracing_open_generic, |
| 509 | .read = hwlat_read, |
| 510 | .write = hwlat_width_write, |
| 511 | }; |
| 512 | |
| 513 | static const struct file_operations window_fops = { |
| 514 | .open = tracing_open_generic, |
| 515 | .read = hwlat_read, |
| 516 | .write = hwlat_window_write, |
| 517 | }; |
| 518 | |
| 519 | /** |
| 520 | * init_tracefs - A function to initialize the tracefs interface files |
| 521 | * |
| 522 | * This function creates entries in tracefs for "hwlat_detector". |
| 523 | * It creates the hwlat_detector directory in the tracing directory, |
| 524 | * and within that directory is the count, width and window files to |
| 525 | * change and view those values. |
| 526 | */ |
| 527 | static int init_tracefs(void) |
| 528 | { |
| 529 | struct dentry *d_tracer; |
| 530 | struct dentry *top_dir; |
| 531 | |
| 532 | d_tracer = tracing_init_dentry(); |
| 533 | if (IS_ERR(d_tracer)) |
| 534 | return -ENOMEM; |
| 535 | |
| 536 | top_dir = tracefs_create_dir("hwlat_detector", d_tracer); |
| 537 | if (!top_dir) |
| 538 | return -ENOMEM; |
| 539 | |
| 540 | hwlat_sample_window = tracefs_create_file("window", 0640, |
| 541 | top_dir, |
| 542 | &hwlat_data.sample_window, |
| 543 | &window_fops); |
| 544 | if (!hwlat_sample_window) |
| 545 | goto err; |
| 546 | |
| 547 | hwlat_sample_width = tracefs_create_file("width", 0644, |
| 548 | top_dir, |
| 549 | &hwlat_data.sample_width, |
| 550 | &width_fops); |
| 551 | if (!hwlat_sample_width) |
| 552 | goto err; |
| 553 | |
| 554 | return 0; |
| 555 | |
| 556 | err: |
| 557 | tracefs_remove_recursive(top_dir); |
| 558 | return -ENOMEM; |
| 559 | } |
| 560 | |
| 561 | static void hwlat_tracer_start(struct trace_array *tr) |
| 562 | { |
| 563 | int err; |
| 564 | |
| 565 | err = start_kthread(tr); |
| 566 | if (err) |
| 567 | pr_err(BANNER "Cannot start hwlat kthread\n"); |
| 568 | } |
| 569 | |
| 570 | static void hwlat_tracer_stop(struct trace_array *tr) |
| 571 | { |
| 572 | stop_kthread(); |
| 573 | } |
| 574 | |
| 575 | static bool hwlat_busy; |
| 576 | |
| 577 | static int hwlat_tracer_init(struct trace_array *tr) |
| 578 | { |
| 579 | /* Only allow one instance to enable this */ |
| 580 | if (hwlat_busy) |
| 581 | return -EBUSY; |
| 582 | |
| 583 | hwlat_trace = tr; |
| 584 | |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 585 | disable_migrate = false; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 586 | hwlat_data.count = 0; |
| 587 | tr->max_latency = 0; |
| 588 | save_tracing_thresh = tracing_thresh; |
| 589 | |
| 590 | /* tracing_thresh is in nsecs, we speak in usecs */ |
| 591 | if (!tracing_thresh) |
| 592 | tracing_thresh = last_tracing_thresh; |
| 593 | |
| 594 | if (tracer_tracing_is_on(tr)) |
| 595 | hwlat_tracer_start(tr); |
| 596 | |
| 597 | hwlat_busy = true; |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static void hwlat_tracer_reset(struct trace_array *tr) |
| 603 | { |
| 604 | stop_kthread(); |
| 605 | |
| 606 | /* the tracing threshold is static between runs */ |
| 607 | last_tracing_thresh = tracing_thresh; |
| 608 | |
| 609 | tracing_thresh = save_tracing_thresh; |
| 610 | hwlat_busy = false; |
| 611 | } |
| 612 | |
| 613 | static struct tracer hwlat_tracer __read_mostly = |
| 614 | { |
| 615 | .name = "hwlat", |
| 616 | .init = hwlat_tracer_init, |
| 617 | .reset = hwlat_tracer_reset, |
| 618 | .start = hwlat_tracer_start, |
| 619 | .stop = hwlat_tracer_stop, |
| 620 | .allow_instances = true, |
| 621 | }; |
| 622 | |
| 623 | __init static int init_hwlat_tracer(void) |
| 624 | { |
| 625 | int ret; |
| 626 | |
| 627 | mutex_init(&hwlat_data.lock); |
| 628 | |
| 629 | ret = register_tracer(&hwlat_tracer); |
| 630 | if (ret) |
| 631 | return ret; |
| 632 | |
| 633 | init_tracefs(); |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | late_initcall(init_hwlat_tracer); |