| 1 |
Introduction: |
| 2 |
------------- |
| 3 |
|
| 4 |
The module hwlat is a special purpose kernel module that is |
| 5 |
used to detect if System Management Interrupts (SMIs) are causing event |
| 6 |
latencies in the Linux RT kernel. |
| 7 |
|
| 8 |
SMIs are usually not serviced by the Linux kernel. They are set up by |
| 9 |
BIOS code and are serviced by BIOS code, usually for critical events |
| 10 |
such as management of thermal sensors and fans. Sometimes though, SMIs |
| 11 |
are used for other tasks and those tasks can spend an inordinate |
| 12 |
amount of time in the handler (sometimes measured in milliseconds). |
| 13 |
Obviously if you are trying to keep event service latencies down in the |
| 14 |
microsecond range, this is a problem. |
| 15 |
|
| 16 |
The SMI detector works by hogging the cpu for configurable amounts of |
| 17 |
time (by calling stop_machine()), polling the Time Stamp Counter (TSC) |
| 18 |
register for some period, then looking for gaps in the TSC data. Any |
| 19 |
gap indicates a time when the polling was interrupted and since the |
| 20 |
machine is stopped and interrupts turned off the only thing that could |
| 21 |
do that would be an SMI. |
| 22 |
|
| 23 |
Note that the SMI detector should *NEVER* be used in a production |
| 24 |
environment. It is intended to be run manually to determine if the |
| 25 |
hardware platform has a problem with long SMI service routines. |
| 26 |
|
| 27 |
Usage: |
| 28 |
------ |
| 29 |
|
| 30 |
Loading the module hwlat passing the parameter "enabled=1" is the only |
| 31 |
step required to start the hwlat. It is possible to define a threshold |
| 32 |
in microseconds (us) above which latency spikes will be taken in account |
| 33 |
(parameter "threshold="). |
| 34 |
|
| 35 |
Example: |
| 36 |
|
| 37 |
# insmod ./hwlat.ko enabled=1 threshold=100 |
| 38 |
|
| 39 |
After the module is loaded, it creates a directory named "hwlat" under |
| 40 |
the debugfs mountpoint, "/debugfs/hwlat" for this text. It is necessary |
| 41 |
to have debugfs mounted. |
| 42 |
|
| 43 |
avg_smi_interval_us - average interval (usecs) between SMI latency spikes |
| 44 |
latency_threshold_us - minimum latency value to be considered (usecs) |
| 45 |
max_sample_us - maximum SMI latency spike observed (usecs) |
| 46 |
ms_between_samples - interval between samples (ms) |
| 47 |
ms_per_sample - sampling time (ms) |
| 48 |
sample_us - last sample (usecs). continuously updated, may be used |
| 49 |
to plot graphs or to create histograms |
| 50 |
smi_count - how many latency spikes have been observed |
| 51 |
|
| 52 |
|
| 53 |
# cd /debugfs/hwlat |
| 54 |
|
| 55 |
# cat * |
| 56 |
0 (avg_smi_interval_us) |
| 57 |
100 (latency_threshold_us) |
| 58 |
0 (latency_threshold_us) |
| 59 |
5000 (ms_between_samples) |
| 60 |
1 (ms_per_sample) |
| 61 |
3 (sample_us) |
| 62 |
0 (smi_count) |
| 63 |
|
| 64 |
|
| 65 |
The default values for ms_between_samples and ms_per_sample define that every |
| 66 |
5000ms (5s) samples will be collected during 1ms. It is possible to change the |
| 67 |
sampling time or the sampling interval writing to the related files. To collect |
| 68 |
samples during 5ms every 50ms: |
| 69 |
|
| 70 |
# echo 5 > ms_per_sample |
| 71 |
# echo 50 > ms_between_samples |
| 72 |
|
| 73 |
|
| 74 |
# cat ms_between_samples |
| 75 |
50 |
| 76 |
# cat ms_per_sample |
| 77 |
5 |
| 78 |
|
| 79 |
After a while, data may be verified to test the existence of SMI induced |
| 80 |
latencies: |
| 81 |
|
| 82 |
# cat smi_count |
| 83 |
2 |
| 84 |
# cat max_sample_us |
| 85 |
468 |
| 86 |
# cat avg_smi_interval_us |
| 87 |
1356306050 |
| 88 |
|
| 89 |
Depending on the latencies observed it is important to better adjust the |
| 90 |
sampling intervals to obtain more accurate measurements. Care must be taken |
| 91 |
to not create a continuous sampling situation, that might be perceived by the |
| 92 |
kernel as a deadlock. |
| 93 |
|
| 94 |
Python Script |
| 95 |
------------- |
| 96 |
|
| 97 |
A python script is provided in in the kernel scripts directory named |
| 98 |
'hwlatdetect'. This script handles mounting/unmounting the debugfs, |
| 99 |
loading/unloading the hwlat module, setting various parameters |
| 100 |
for the SMI detector and then starting and stopping the detection. The |
| 101 |
hwlatdetect script handles the following arguments: |
| 102 |
|
| 103 |
Usage: hwlatdetect [options] |
| 104 |
|
| 105 |
Options: |
| 106 |
-h, --help show this help message and exit |
| 107 |
--duration=DURATION total time to test for SMIs (<n>{smdw}) |
| 108 |
--threshold=THRESHOLD |
| 109 |
value above which is considered an SMI (microseconds |
| 110 |
--interval=INTERVAL time between samples (milliseconds) |
| 111 |
--sample_width=SAMPLE_WIDTH |
| 112 |
time to actually measure (milliseconds) |
| 113 |
--report=REPORT filename for sample data |
| 114 |
--cleanup force unload of module and umount of debugfs |
| 115 |
--debug turn on debugging prints |
| 116 |
--quiet turn off all screen output |
| 117 |
--watch print sample data to stdout as it arrives |
| 118 |
|
| 119 |
|
| 120 |
An example run looks like this: |
| 121 |
|
| 122 |
$ sudo hwlatdetect --duration=1m --report=smi.txt |
| 123 |
hwlatdetect: test duration 60 seconds |
| 124 |
parameters: |
| 125 |
Latency threshold: 100us |
| 126 |
Non-sampling gap: 5ms |
| 127 |
Sample length: 20ms |
| 128 |
Output File: smi.txt |
| 129 |
Starting test |
| 130 |
test finished |
| 131 |
Max Latency: 427us |
| 132 |
Samples recorded: 2201 |
| 133 |
Samples exceeding threshold: 7 |
| 134 |
sample data written to smi.txt |
| 135 |
$ |
| 136 |
|
| 137 |
The above run generated 2201 samples of which 27 exceeded the 100 |
| 138 |
microsecond threshold. |