2 * hwlat_detector.c - A simple Hardware Latency detector.
4 * Use this module 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 module 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 module in a production environment
30 * requiring any kind of low-latency performance guarantee(s).
32 * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
34 * Includes useful feedback from Clark Williams <clark@redhat.com>
36 * This file is licensed under the terms of the GNU General Public
37 * License version 2. This program is licensed "as is" without any
38 * warranty of any kind, whether express or implied.
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/ring_buffer.h>
44 #include <linux/time.h>
45 #include <linux/hrtimer.h>
46 #include <linux/kthread.h>
47 #include <linux/debugfs.h>
48 #include <linux/seq_file.h>
49 #include <linux/uaccess.h>
50 #include <linux/version.h>
51 #include <linux/delay.h>
52 #include <linux/slab.h>
53 #include <linux/trace_clock.h>
55 #define BUF_SIZE_DEFAULT 262144UL /* 8K*(sizeof(entry)) */
56 #define BUF_FLAGS (RB_FL_OVERWRITE) /* no block on full */
57 #define U64STR_SIZE 22 /* 20 digits max */
59 #define VERSION "1.0.0"
60 #define BANNER "hwlat_detector: "
61 #define DRVNAME "hwlat_detector"
62 #define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
63 #define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
64 #define DEFAULT_LAT_THRESHOLD 10 /* 10us */
68 MODULE_LICENSE("GPL");
69 MODULE_AUTHOR("Jon Masters <jcm@redhat.com>");
70 MODULE_DESCRIPTION("A simple hardware latency detector");
71 MODULE_VERSION(VERSION);
73 /* Module parameters */
79 module_param(debug, int, 0); /* enable debug */
80 module_param(enabled, int, 0); /* enable detector */
81 module_param(threshold, int, 0); /* latency threshold */
83 /* Buffering and sampling */
85 static struct ring_buffer *ring_buffer; /* sample buffer */
86 static DEFINE_MUTEX(ring_buffer_mutex); /* lock changes */
87 static unsigned long buf_size = BUF_SIZE_DEFAULT;
88 static struct task_struct *kthread; /* sampling thread */
90 /* DebugFS filesystem entries */
92 static struct dentry *debug_dir; /* debugfs directory */
93 static struct dentry *debug_max; /* maximum TSC delta */
94 static struct dentry *debug_count; /* total detect count */
95 static struct dentry *debug_sample_width; /* sample width us */
96 static struct dentry *debug_sample_window; /* sample window us */
97 static struct dentry *debug_sample; /* raw samples us */
98 static struct dentry *debug_threshold; /* threshold us */
99 static struct dentry *debug_enable; /* enable/disable */
101 /* Individual samples and global state */
103 struct sample; /* latency sample */
104 struct data; /* Global state */
106 /* Sampling functions */
107 static int __buffer_add_sample(struct sample *sample);
108 static struct sample *buffer_get_sample(struct sample *sample);
110 /* Threading and state */
111 static int kthread_fn(void *unused);
112 static int start_kthread(void);
113 static int stop_kthread(void);
114 static void __reset_stats(void);
115 static int init_stats(void);
117 /* Debugfs interface */
118 static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
119 size_t cnt, loff_t *ppos, const u64 *entry);
120 static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
121 size_t cnt, loff_t *ppos, u64 *entry);
122 static int debug_sample_fopen(struct inode *inode, struct file *filp);
123 static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
124 size_t cnt, loff_t *ppos);
125 static int debug_sample_release(struct inode *inode, struct file *filp);
126 static int debug_enable_fopen(struct inode *inode, struct file *filp);
127 static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
128 size_t cnt, loff_t *ppos);
129 static ssize_t debug_enable_fwrite(struct file *file,
130 const char __user *user_buffer,
131 size_t user_size, loff_t *offset);
133 /* Initialization functions */
134 static int init_debugfs(void);
135 static void free_debugfs(void);
136 static int detector_init(void);
137 static void detector_exit(void);
139 /* Individual latency samples are stored here when detected and packed into
140 * the ring_buffer circular buffer, where they are overwritten when
141 * more than buf_size/sizeof(sample) samples are received. */
143 u64 seqnum; /* unique sequence */
144 u64 duration; /* ktime delta */
145 u64 outer_duration; /* ktime delta (outer loop) */
146 struct timespec timestamp; /* wall time */
150 /* keep the global state somewhere. */
153 struct mutex lock; /* protect changes */
155 u64 count; /* total since reset */
156 u64 max_sample; /* max hardware latency */
157 u64 threshold; /* sample threshold level */
159 u64 sample_window; /* total sampling window (on+off) */
160 u64 sample_width; /* active sampling portion of window */
162 atomic_t sample_open; /* whether the sample file is open */
164 wait_queue_head_t wq; /* waitqeue for new sample values */
169 * __buffer_add_sample - add a new latency sample recording to the ring buffer
170 * @sample: The new latency sample value
172 * This receives a new latency sample and records it in a global ring buffer.
173 * No additional locking is used in this case.
175 static int __buffer_add_sample(struct sample *sample)
177 return ring_buffer_write(ring_buffer,
178 sizeof(struct sample), sample);
182 * buffer_get_sample - remove a hardware latency sample from the ring buffer
183 * @sample: Pre-allocated storage for the sample
185 * This retrieves a hardware latency sample from the global circular buffer
187 static struct sample *buffer_get_sample(struct sample *sample)
189 struct ring_buffer_event *e = NULL;
190 struct sample *s = NULL;
191 unsigned int cpu = 0;
196 mutex_lock(&ring_buffer_mutex);
197 for_each_online_cpu(cpu) {
198 e = ring_buffer_consume(ring_buffer, cpu, NULL, &sample->lost);
204 s = ring_buffer_event_data(e);
205 memcpy(sample, s, sizeof(struct sample));
208 mutex_unlock(&ring_buffer_mutex);
213 #ifndef CONFIG_TRACING
214 #define time_type ktime_t
215 #define time_get() ktime_get()
216 #define time_to_us(x) ktime_to_us(x)
217 #define time_sub(a, b) ktime_sub(a, b)
218 #define init_time(a, b) (a).tv64 = b
219 #define time_u64(a) ((a).tv64)
221 #define time_type u64
222 #define time_get() trace_clock_local()
223 #define time_to_us(x) div_u64(x, 1000)
224 #define time_sub(a, b) ((a) - (b))
225 #define init_time(a, b) (a = b)
226 #define time_u64(a) a
229 * get_sample - sample the CPU TSC and look for likely hardware latencies
231 * Used to repeatedly capture the CPU TSC (or similar), looking for potential
232 * hardware-induced latency. Called with interrupts disabled and with
235 static int get_sample(void)
237 time_type start, t1, t2, last_t2;
240 u64 outer_sample = 0;
243 init_time(last_t2, 0);
244 start = time_get(); /* start timestamp */
248 t1 = time_get(); /* we'll look for a discontinuity */
251 if (time_u64(last_t2)) {
252 /* Check the delta from outer loop (t2 to next t1) */
253 diff = time_to_us(time_sub(t1, last_t2));
254 /* This shouldn't happen */
256 pr_err(BANNER "time running backwards\n");
259 if (diff > outer_sample)
264 total = time_to_us(time_sub(t2, start)); /* sample width */
266 /* This checks the inner loop (t1 to t2) */
267 diff = time_to_us(time_sub(t2, t1)); /* current diff */
269 /* This shouldn't happen */
271 pr_err(BANNER "time running backwards\n");
276 sample = diff; /* only want highest value */
278 } while (total <= data.sample_width);
282 /* If we exceed the threshold value, we have found a hardware latency */
283 if (sample > data.threshold || outer_sample > data.threshold) {
289 s.seqnum = data.count;
291 s.outer_duration = outer_sample;
292 s.timestamp = CURRENT_TIME;
293 __buffer_add_sample(&s);
295 /* Keep a running maximum ever recorded hardware latency */
296 if (sample > data.max_sample)
297 data.max_sample = sample;
305 * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
306 * @unused: A required part of the kthread API.
308 * Used to periodically sample the CPU TSC via a call to get_sample. We
309 * disable interrupts, which does (intentionally) introduce latency since we
310 * need to ensure nothing else might be running (and thus pre-empting).
311 * Obviously this should never be used in production environments.
313 * Currently this runs on which ever CPU it was scheduled on, but most
314 * real-worald hardware latency situations occur across several CPUs,
315 * but we might later generalize this if we find there are any actualy
316 * systems with alternate SMI delivery or other hardware latencies.
318 static int kthread_fn(void *unused)
323 while (!kthread_should_stop()) {
325 mutex_lock(&data.lock);
332 wake_up(&data.wq); /* wake up reader(s) */
334 interval = data.sample_window - data.sample_width;
335 do_div(interval, USEC_PER_MSEC); /* modifies interval value */
337 mutex_unlock(&data.lock);
339 if (msleep_interruptible(interval))
347 * start_kthread - Kick off the hardware latency sampling/detector kthread
349 * This starts a kernel thread that will sit and sample the CPU timestamp
350 * counter (TSC or similar) and look for potential hardware latencies.
352 static int start_kthread(void)
354 kthread = kthread_run(kthread_fn, NULL,
356 if (IS_ERR(kthread)) {
357 pr_err(BANNER "could not start sampling thread\n");
366 * stop_kthread - Inform the hardware latency samping/detector kthread to stop
368 * This kicks the running hardware latency sampling/detector kernel thread and
369 * tells it to stop sampling now. Use this on unload and at system shutdown.
371 static int stop_kthread(void)
375 ret = kthread_stop(kthread);
381 * __reset_stats - Reset statistics for the hardware latency detector
383 * We use data to store various statistics and global state. We call this
384 * function in order to reset those when "enable" is toggled on or off, and
385 * also at initialization. Should be called with data.lock held.
387 static void __reset_stats(void)
391 ring_buffer_reset(ring_buffer); /* flush out old sample entries */
395 * init_stats - Setup global state statistics for the hardware latency detector
397 * We use data to store various statistics and global state. We also use
398 * a global ring buffer (ring_buffer) to keep raw samples of detected hardware
399 * induced system latencies. This function initializes these structures and
400 * allocates the global ring buffer also.
402 static int init_stats(void)
406 mutex_init(&data.lock);
407 init_waitqueue_head(&data.wq);
408 atomic_set(&data.sample_open, 0);
410 ring_buffer = ring_buffer_alloc(buf_size, BUF_FLAGS);
412 if (WARN(!ring_buffer, KERN_ERR BANNER
413 "failed to allocate ring buffer!\n"))
417 data.threshold = threshold ?: DEFAULT_LAT_THRESHOLD; /* threshold us */
418 data.sample_window = DEFAULT_SAMPLE_WINDOW; /* window us */
419 data.sample_width = DEFAULT_SAMPLE_WIDTH; /* width us */
429 * simple_data_read - Wrapper read function for global state debugfs entries
430 * @filp: The active open file structure for the debugfs "file"
431 * @ubuf: The userspace provided buffer to read value into
432 * @cnt: The maximum number of bytes to read
433 * @ppos: The current "file" position
434 * @entry: The entry to read from
436 * This function provides a generic read implementation for the global state
437 * "data" structure debugfs filesystem entries. It would be nice to use
438 * simple_attr_read directly, but we need to make sure that the data.lock
439 * is held during the actual read.
441 static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
442 size_t cnt, loff_t *ppos, const u64 *entry)
444 char buf[U64STR_SIZE];
448 memset(buf, 0, sizeof(buf));
453 mutex_lock(&data.lock);
455 mutex_unlock(&data.lock);
457 len = snprintf(buf, sizeof(buf), "%llu\n", (unsigned long long)val);
459 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
464 * simple_data_write - Wrapper write function for global state debugfs entries
465 * @filp: The active open file structure for the debugfs "file"
466 * @ubuf: The userspace provided buffer to write value from
467 * @cnt: The maximum number of bytes to write
468 * @ppos: The current "file" position
469 * @entry: The entry to write to
471 * This function provides a generic write implementation for the global state
472 * "data" structure debugfs filesystem entries. It would be nice to use
473 * simple_attr_write directly, but we need to make sure that the data.lock
474 * is held during the actual write.
476 static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
477 size_t cnt, loff_t *ppos, u64 *entry)
479 char buf[U64STR_SIZE];
480 int csize = min(cnt, sizeof(buf));
484 memset(buf, '\0', sizeof(buf));
485 if (copy_from_user(buf, ubuf, csize))
488 buf[U64STR_SIZE-1] = '\0'; /* just in case */
489 err = kstrtoull(buf, 10, &val);
493 mutex_lock(&data.lock);
495 mutex_unlock(&data.lock);
501 * debug_count_fopen - Open function for "count" debugfs entry
502 * @inode: The in-kernel inode representation of the debugfs "file"
503 * @filp: The active open file structure for the debugfs "file"
505 * This function provides an open implementation for the "count" debugfs
506 * interface to the hardware latency detector.
508 static int debug_count_fopen(struct inode *inode, struct file *filp)
514 * debug_count_fread - Read function for "count" debugfs entry
515 * @filp: The active open file structure for the debugfs "file"
516 * @ubuf: The userspace provided buffer to read value into
517 * @cnt: The maximum number of bytes to read
518 * @ppos: The current "file" position
520 * This function provides a read implementation for the "count" debugfs
521 * interface to the hardware latency detector. Can be used to read the
522 * number of latency readings exceeding the configured threshold since
523 * the detector was last reset (e.g. by writing a zero into "count").
525 static ssize_t debug_count_fread(struct file *filp, char __user *ubuf,
526 size_t cnt, loff_t *ppos)
528 return simple_data_read(filp, ubuf, cnt, ppos, &data.count);
532 * debug_count_fwrite - Write function for "count" debugfs entry
533 * @filp: The active open file structure for the debugfs "file"
534 * @ubuf: The user buffer that contains the value to write
535 * @cnt: The maximum number of bytes to write to "file"
536 * @ppos: The current position in the debugfs "file"
538 * This function provides a write implementation for the "count" debugfs
539 * interface to the hardware latency detector. Can be used to write a
540 * desired value, especially to zero the total count.
542 static ssize_t debug_count_fwrite(struct file *filp,
543 const char __user *ubuf,
547 return simple_data_write(filp, ubuf, cnt, ppos, &data.count);
551 * debug_enable_fopen - Dummy open function for "enable" debugfs interface
552 * @inode: The in-kernel inode representation of the debugfs "file"
553 * @filp: The active open file structure for the debugfs "file"
555 * This function provides an open implementation for the "enable" debugfs
556 * interface to the hardware latency detector.
558 static int debug_enable_fopen(struct inode *inode, struct file *filp)
564 * debug_enable_fread - Read function for "enable" debugfs interface
565 * @filp: The active open file structure for the debugfs "file"
566 * @ubuf: The userspace provided buffer to read value into
567 * @cnt: The maximum number of bytes to read
568 * @ppos: The current "file" position
570 * This function provides a read implementation for the "enable" debugfs
571 * interface to the hardware latency detector. Can be used to determine
572 * whether the detector is currently enabled ("0\n" or "1\n" returned).
574 static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
575 size_t cnt, loff_t *ppos)
579 if ((cnt < sizeof(buf)) || (*ppos))
582 buf[0] = enabled ? '1' : '0';
585 if (copy_to_user(ubuf, buf, strlen(buf)))
587 return *ppos = strlen(buf);
591 * debug_enable_fwrite - Write function for "enable" debugfs interface
592 * @filp: The active open file structure for the debugfs "file"
593 * @ubuf: The user buffer that contains the value to write
594 * @cnt: The maximum number of bytes to write to "file"
595 * @ppos: The current position in the debugfs "file"
597 * This function provides a write implementation for the "enable" debugfs
598 * interface to the hardware latency detector. Can be used to enable or
599 * disable the detector, which will have the side-effect of possibly
600 * also resetting the global stats and kicking off the measuring
601 * kthread (on an enable) or the converse (upon a disable).
603 static ssize_t debug_enable_fwrite(struct file *filp,
604 const char __user *ubuf,
609 int csize = min(cnt, sizeof(buf));
613 memset(buf, '\0', sizeof(buf));
614 if (copy_from_user(buf, ubuf, csize))
617 buf[sizeof(buf)-1] = '\0'; /* just in case */
618 err = kstrtoul(buf, 10, &val);
633 err = stop_kthread();
635 pr_err(BANNER "cannot stop kthread\n");
638 wake_up(&data.wq); /* reader(s) should return */
645 * debug_max_fopen - Open function for "max" debugfs entry
646 * @inode: The in-kernel inode representation of the debugfs "file"
647 * @filp: The active open file structure for the debugfs "file"
649 * This function provides an open implementation for the "max" debugfs
650 * interface to the hardware latency detector.
652 static int debug_max_fopen(struct inode *inode, struct file *filp)
658 * debug_max_fread - Read function for "max" debugfs entry
659 * @filp: The active open file structure for the debugfs "file"
660 * @ubuf: The userspace provided buffer to read value into
661 * @cnt: The maximum number of bytes to read
662 * @ppos: The current "file" position
664 * This function provides a read implementation for the "max" debugfs
665 * interface to the hardware latency detector. Can be used to determine
666 * the maximum latency value observed since it was last reset.
668 static ssize_t debug_max_fread(struct file *filp, char __user *ubuf,
669 size_t cnt, loff_t *ppos)
671 return simple_data_read(filp, ubuf, cnt, ppos, &data.max_sample);
675 * debug_max_fwrite - Write function for "max" debugfs entry
676 * @filp: The active open file structure for the debugfs "file"
677 * @ubuf: The user buffer that contains the value to write
678 * @cnt: The maximum number of bytes to write to "file"
679 * @ppos: The current position in the debugfs "file"
681 * This function provides a write implementation for the "max" debugfs
682 * interface to the hardware latency detector. Can be used to reset the
683 * maximum or set it to some other desired value - if, then, subsequent
684 * measurements exceed this value, the maximum will be updated.
686 static ssize_t debug_max_fwrite(struct file *filp,
687 const char __user *ubuf,
691 return simple_data_write(filp, ubuf, cnt, ppos, &data.max_sample);
696 * debug_sample_fopen - An open function for "sample" debugfs interface
697 * @inode: The in-kernel inode representation of this debugfs "file"
698 * @filp: The active open file structure for the debugfs "file"
700 * This function handles opening the "sample" file within the hardware
701 * latency detector debugfs directory interface. This file is used to read
702 * raw samples from the global ring_buffer and allows the user to see a
703 * running latency history. Can be opened blocking or non-blocking,
704 * affecting whether it behaves as a buffer read pipe, or does not.
705 * Implements simple locking to prevent multiple simultaneous use.
707 static int debug_sample_fopen(struct inode *inode, struct file *filp)
709 if (!atomic_add_unless(&data.sample_open, 1, 1))
716 * debug_sample_fread - A read function for "sample" debugfs interface
717 * @filp: The active open file structure for the debugfs "file"
718 * @ubuf: The user buffer that will contain the samples read
719 * @cnt: The maximum bytes to read from the debugfs "file"
720 * @ppos: The current position in the debugfs "file"
722 * This function handles reading from the "sample" file within the hardware
723 * latency detector debugfs directory interface. This file is used to read
724 * raw samples from the global ring_buffer and allows the user to see a
725 * running latency history. By default this will block pending a new
726 * value written into the sample buffer, unless there are already a
727 * number of value(s) waiting in the buffer, or the sample file was
728 * previously opened in a non-blocking mode of operation.
730 static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
731 size_t cnt, loff_t *ppos)
735 struct sample *sample = NULL;
740 sample = kzalloc(sizeof(struct sample), GFP_KERNEL);
744 while (!buffer_get_sample(sample)) {
748 if (filp->f_flags & O_NONBLOCK) {
753 prepare_to_wait(&data.wq, &wait, TASK_INTERRUPTIBLE);
755 finish_wait(&data.wq, &wait);
757 if (signal_pending(current)) {
762 if (!enabled) { /* enable was toggled */
768 len = snprintf(buf, sizeof(buf), "%010lu.%010lu\t%llu\t%llu\n",
769 sample->timestamp.tv_sec,
770 sample->timestamp.tv_nsec,
772 sample->outer_duration);
775 /* handling partial reads is more trouble than it's worth */
779 if (copy_to_user(ubuf, buf, len))
788 * debug_sample_release - Release function for "sample" debugfs interface
789 * @inode: The in-kernel inode represenation of the debugfs "file"
790 * @filp: The active open file structure for the debugfs "file"
792 * This function completes the close of the debugfs interface "sample" file.
793 * Frees the sample_open "lock" so that other users may open the interface.
795 static int debug_sample_release(struct inode *inode, struct file *filp)
797 atomic_dec(&data.sample_open);
803 * debug_threshold_fopen - Open function for "threshold" debugfs entry
804 * @inode: The in-kernel inode representation of the debugfs "file"
805 * @filp: The active open file structure for the debugfs "file"
807 * This function provides an open implementation for the "threshold" debugfs
808 * interface to the hardware latency detector.
810 static int debug_threshold_fopen(struct inode *inode, struct file *filp)
816 * debug_threshold_fread - Read function for "threshold" debugfs entry
817 * @filp: The active open file structure for the debugfs "file"
818 * @ubuf: The userspace provided buffer to read value into
819 * @cnt: The maximum number of bytes to read
820 * @ppos: The current "file" position
822 * This function provides a read implementation for the "threshold" debugfs
823 * interface to the hardware latency detector. It can be used to determine
824 * the current threshold level at which a latency will be recorded in the
825 * global ring buffer, typically on the order of 10us.
827 static ssize_t debug_threshold_fread(struct file *filp, char __user *ubuf,
828 size_t cnt, loff_t *ppos)
830 return simple_data_read(filp, ubuf, cnt, ppos, &data.threshold);
834 * debug_threshold_fwrite - Write function for "threshold" debugfs entry
835 * @filp: The active open file structure for the debugfs "file"
836 * @ubuf: The user buffer that contains the value to write
837 * @cnt: The maximum number of bytes to write to "file"
838 * @ppos: The current position in the debugfs "file"
840 * This function provides a write implementation for the "threshold" debugfs
841 * interface to the hardware latency detector. It can be used to configure
842 * the threshold level at which any subsequently detected latencies will
843 * be recorded into the global ring buffer.
845 static ssize_t debug_threshold_fwrite(struct file *filp,
846 const char __user *ubuf,
852 ret = simple_data_write(filp, ubuf, cnt, ppos, &data.threshold);
855 wake_up_process(kthread);
861 * debug_width_fopen - Open function for "width" debugfs entry
862 * @inode: The in-kernel inode representation of the debugfs "file"
863 * @filp: The active open file structure for the debugfs "file"
865 * This function provides an open implementation for the "width" debugfs
866 * interface to the hardware latency detector.
868 static int debug_width_fopen(struct inode *inode, struct file *filp)
874 * debug_width_fread - Read function for "width" debugfs entry
875 * @filp: The active open file structure for the debugfs "file"
876 * @ubuf: The userspace provided buffer to read value into
877 * @cnt: The maximum number of bytes to read
878 * @ppos: The current "file" position
880 * This function provides a read implementation for the "width" debugfs
881 * interface to the hardware latency detector. It can be used to determine
882 * for how many us of the total window us we will actively sample for any
883 * hardware-induced latecy periods. Obviously, it is not possible to
884 * sample constantly and have the system respond to a sample reader, or,
885 * worse, without having the system appear to have gone out to lunch.
887 static ssize_t debug_width_fread(struct file *filp, char __user *ubuf,
888 size_t cnt, loff_t *ppos)
890 return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_width);
894 * debug_width_fwrite - Write function for "width" debugfs entry
895 * @filp: The active open file structure for the debugfs "file"
896 * @ubuf: The user buffer that contains the value to write
897 * @cnt: The maximum number of bytes to write to "file"
898 * @ppos: The current position in the debugfs "file"
900 * This function provides a write implementation for the "width" debugfs
901 * interface to the hardware latency detector. It can be used to configure
902 * for how many us of the total window us we will actively sample for any
903 * hardware-induced latency periods. Obviously, it is not possible to
904 * sample constantly and have the system respond to a sample reader, or,
905 * worse, without having the system appear to have gone out to lunch. It
906 * is enforced that width is less that the total window size.
908 static ssize_t debug_width_fwrite(struct file *filp,
909 const char __user *ubuf,
913 char buf[U64STR_SIZE];
914 int csize = min(cnt, sizeof(buf));
918 memset(buf, '\0', sizeof(buf));
919 if (copy_from_user(buf, ubuf, csize))
922 buf[U64STR_SIZE-1] = '\0'; /* just in case */
923 err = kstrtoull(buf, 10, &val);
927 mutex_lock(&data.lock);
928 if (val < data.sample_window)
929 data.sample_width = val;
931 mutex_unlock(&data.lock);
934 mutex_unlock(&data.lock);
937 wake_up_process(kthread);
943 * debug_window_fopen - Open function for "window" debugfs entry
944 * @inode: The in-kernel inode representation of the debugfs "file"
945 * @filp: The active open file structure for the debugfs "file"
947 * This function provides an open implementation for the "window" debugfs
948 * interface to the hardware latency detector. The window is the total time
949 * in us that will be considered one sample period. Conceptually, windows
950 * occur back-to-back and contain a sample width period during which
951 * actual sampling occurs.
953 static int debug_window_fopen(struct inode *inode, struct file *filp)
959 * debug_window_fread - Read function for "window" debugfs entry
960 * @filp: The active open file structure for the debugfs "file"
961 * @ubuf: The userspace provided buffer to read value into
962 * @cnt: The maximum number of bytes to read
963 * @ppos: The current "file" position
965 * This function provides a read implementation for the "window" debugfs
966 * interface to the hardware latency detector. The window is the total time
967 * in us that will be considered one sample period. Conceptually, windows
968 * occur back-to-back and contain a sample width period during which
969 * actual sampling occurs. Can be used to read the total window size.
971 static ssize_t debug_window_fread(struct file *filp, char __user *ubuf,
972 size_t cnt, loff_t *ppos)
974 return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_window);
978 * debug_window_fwrite - Write function for "window" debugfs entry
979 * @filp: The active open file structure for the debugfs "file"
980 * @ubuf: The user buffer that contains the value to write
981 * @cnt: The maximum number of bytes to write to "file"
982 * @ppos: The current position in the debugfs "file"
984 * This function provides a write implementation for the "window" debufds
985 * interface to the hardware latency detetector. The window is the total time
986 * in us that will be considered one sample period. Conceptually, windows
987 * occur back-to-back and contain a sample width period during which
988 * actual sampling occurs. Can be used to write a new total window size. It
989 * is enfoced that any value written must be greater than the sample width
990 * size, or an error results.
992 static ssize_t debug_window_fwrite(struct file *filp,
993 const char __user *ubuf,
997 char buf[U64STR_SIZE];
998 int csize = min(cnt, sizeof(buf));
1002 memset(buf, '\0', sizeof(buf));
1003 if (copy_from_user(buf, ubuf, csize))
1006 buf[U64STR_SIZE-1] = '\0'; /* just in case */
1007 err = kstrtoull(buf, 10, &val);
1011 mutex_lock(&data.lock);
1012 if (data.sample_width < val)
1013 data.sample_window = val;
1015 mutex_unlock(&data.lock);
1018 mutex_unlock(&data.lock);
1024 * Function pointers for the "count" debugfs file operations
1026 static const struct file_operations count_fops = {
1027 .open = debug_count_fopen,
1028 .read = debug_count_fread,
1029 .write = debug_count_fwrite,
1030 .owner = THIS_MODULE,
1034 * Function pointers for the "enable" debugfs file operations
1036 static const struct file_operations enable_fops = {
1037 .open = debug_enable_fopen,
1038 .read = debug_enable_fread,
1039 .write = debug_enable_fwrite,
1040 .owner = THIS_MODULE,
1044 * Function pointers for the "max" debugfs file operations
1046 static const struct file_operations max_fops = {
1047 .open = debug_max_fopen,
1048 .read = debug_max_fread,
1049 .write = debug_max_fwrite,
1050 .owner = THIS_MODULE,
1054 * Function pointers for the "sample" debugfs file operations
1056 static const struct file_operations sample_fops = {
1057 .open = debug_sample_fopen,
1058 .read = debug_sample_fread,
1059 .release = debug_sample_release,
1060 .owner = THIS_MODULE,
1064 * Function pointers for the "threshold" debugfs file operations
1066 static const struct file_operations threshold_fops = {
1067 .open = debug_threshold_fopen,
1068 .read = debug_threshold_fread,
1069 .write = debug_threshold_fwrite,
1070 .owner = THIS_MODULE,
1074 * Function pointers for the "width" debugfs file operations
1076 static const struct file_operations width_fops = {
1077 .open = debug_width_fopen,
1078 .read = debug_width_fread,
1079 .write = debug_width_fwrite,
1080 .owner = THIS_MODULE,
1084 * Function pointers for the "window" debugfs file operations
1086 static const struct file_operations window_fops = {
1087 .open = debug_window_fopen,
1088 .read = debug_window_fread,
1089 .write = debug_window_fwrite,
1090 .owner = THIS_MODULE,
1094 * init_debugfs - A function to initialize the debugfs interface files
1096 * This function creates entries in debugfs for "hwlat_detector", including
1097 * files to read values from the detector, current samples, and the
1098 * maximum sample that has been captured since the hardware latency
1099 * dectector was started.
1101 static int init_debugfs(void)
1105 debug_dir = debugfs_create_dir(DRVNAME, NULL);
1109 debug_sample = debugfs_create_file("sample", 0444,
1115 debug_count = debugfs_create_file("count", 0444,
1121 debug_max = debugfs_create_file("max", 0444,
1127 debug_sample_window = debugfs_create_file("window", 0644,
1130 if (!debug_sample_window)
1133 debug_sample_width = debugfs_create_file("width", 0644,
1136 if (!debug_sample_width)
1139 debug_threshold = debugfs_create_file("threshold", 0644,
1142 if (!debug_threshold)
1145 debug_enable = debugfs_create_file("enable", 0644,
1146 debug_dir, &enabled,
1157 debugfs_remove(debug_threshold);
1159 debugfs_remove(debug_sample_width);
1161 debugfs_remove(debug_sample_window);
1163 debugfs_remove(debug_max);
1165 debugfs_remove(debug_count);
1167 debugfs_remove(debug_sample);
1169 debugfs_remove(debug_dir);
1176 * free_debugfs - A function to cleanup the debugfs file interface
1178 static void free_debugfs(void)
1180 /* could also use a debugfs_remove_recursive */
1181 debugfs_remove(debug_enable);
1182 debugfs_remove(debug_threshold);
1183 debugfs_remove(debug_sample_width);
1184 debugfs_remove(debug_sample_window);
1185 debugfs_remove(debug_max);
1186 debugfs_remove(debug_count);
1187 debugfs_remove(debug_sample);
1188 debugfs_remove(debug_dir);
1192 * detector_init - Standard module initialization code
1194 static int detector_init(void)
1198 pr_info(BANNER "version %s\n", VERSION);
1204 ret = init_debugfs();
1209 ret = start_kthread();
1214 ring_buffer_free(ring_buffer);
1221 * detector_exit - Standard module cleanup code
1223 static void detector_exit(void)
1229 err = stop_kthread();
1231 pr_err(BANNER "cannot stop kthread\n");
1235 ring_buffer_free(ring_buffer); /* free up the ring buffer */
1239 module_init(detector_init);
1240 module_exit(detector_exit);