9

I am interested in evaluating the behavior (latency, frequency) of SMI handling on Linux machine running CentOS and used for a (very) soft real time application.

  1. What tools are recommended (hwlatdetect for CentOS?), and what is the best course of action to go about this?

  2. If no good tools are available for CentOS, am I correct to assume that installing a different OS on the same machine should yield the same results since the underlying hardware/bios are the same?

  3. Is there any source for ballpark figures on these parameters.

The machines are X86_64 architecture, running CentOS 6.4 (kernel 2.6.32-358.23.2.el2.centos.plus.x86_64.)

Avi Perel
  • 422
  • 3
  • 8
  • I'm not sure that SMI matters on Linux during normal operation. IMHO it is used only at power-up (and perhaps for ACPI related things). But I could be wrong. – Basile Starynkevitch Aug 20 '14 at 08:02
  • @BasileStarynkevitch You are wrong. On some systems SMIs are used for [all kinds of things](https://en.wikipedia.org/wiki/System_Management_Mode) possibly periodically during normal and abnormal operation of the system. Including but not limited to the control of power management features and monitoring. – maxschlepzig Sep 16 '19 at 17:37

5 Answers5

16

SMIs can certainly happen during normal operation. My home desktop has a chipset-driven SMI every second and a half which is enabled in the chipset. I've also seen some servers that have them twice a second due to a BIOS-driven CPU frequency scaling scheme. However, some systems can go long periods of time without an SMI occurring so it really depends.

Question #1: hwlatdetect is one option to detect the latency of SMIs occurring on your system. BIOSBITS is another option which is a bootable CD that can identify if SMIs are occuring. You can also write your own test by creating a kernel module that spins in a loop and takes timestamps (using RDTSC). If you see a long gap between two timestamp readings, you could consult CPU MSR 0x34 to see if the SMI counter incremented which would indicate that an SMI happened.

If you want to generate an SMI, you can make a kernel module that does an OUT CPU instruction to port 0xb2, e.g. write a value of 0 to this port. (You can also time this SMI by gathering a timestamp just before and just after the write to port 0xB2).

Question #2, SMIs operate at a layer below the OS so which OS you choose, shouldn't have any impact.

Question #3: BIOSBITS recommends that SMI latencies be kept under 150 microseconds.

Latency
  • 176
  • 3
6

SMI will put your system into SMM (System Management Mode) mode, which will postpone the normal execution of kernel during the SMI handling time period. In other words, SMM is neither real mode nor protected mode as we know of normal operation of kernel, instead it executes some special instruction kept in SMRAM (stored in Bios Firmware). To detect it's latency you can try to trigger an SMI (it can be software generated) and try to catch the total time spent in SMM mode. To accomplish this you can write a Linux kernel module, cause you'll be require some special privileges to issue an SMI (I think).

For real time systems I think it's nice if you can avoid these sort of interrupts like SMI.

rakib_
  • 136,911
  • 4
  • 20
  • 26
  • thank you for your input. Are you aware any tool that does this without the need to write the ko myself? also measuring the latency this way does not give any indications as to how often it called. – Avi Perel Aug 20 '14 at 09:32
  • I'm not aware of such tools. And yes, it won't give you any indication of how often it will be called. Remember it's not like any other interrupts, it's usually used to handle critical system issues. – rakib_ Aug 20 '14 at 12:58
  • 5
    @AviPerel On recent CPUs there is a MSR_SMI_COUNT register giving you the number of SMIs that have occurred. It won't give you latency information but it can give you indication of how often they happen. [Here](http://kernel.ubuntu.com/git?p=cking/debug-code/.git;a=commit;h=7e980f36442b2f11b496d3be2af0b189ab26dd3b) is a tool that polls this register and prints its value once a second. – resistor Dec 24 '14 at 21:38
  • 1
    With modern Linux you can get SMI count since boot of the CPU with `sudo rdmsr -d 0x34`. If that number doesn't change after the system has fully booted your BIOS is okay. If it increases during normal workload, your BIOS is not suitable for hard realtime operation. For example, my system outputs `6` after 9 days of uptime. – Mikko Rantalainen Oct 01 '21 at 12:54
  • @MikkoRantalainen Any advice to choose CPU/Motherboard/BIOS to minimize the SMI? Desktop CPU or server CPU? customized BIOS? – foool May 30 '22 at 06:45
  • If you want to avoid any SMI you either trust your luck or use a known good motherboard that has been verified (by you or somebody else) to not cause SMI. It would be great if some motherboard manufacturer actually promised to not use SMI to implement features on any given motherboard but I don't know about such a manufacturer. Note that during the testing you should intentionally raise the CPU temperature to throttling level to verify if your motherboard uses SMI in that case. Motherboard can be implemented without SMI but it would be a bit more expensive to manufacture in most cases. – Mikko Rantalainen May 30 '22 at 07:51
  • Also note that any testing you or anybody else does is BIOS/firmware version specific. The manufacturer can start or stop using SMI for any given BIOS/firmware version. – Mikko Rantalainen May 30 '22 at 07:52
4

You can check whether System Management Interrupts (SMI) are serviced or not with turbostat. For example:

# turbostat sleep 120
[check column SMI for value greater than 0]

Of course, from that you can also compute a SMI frequency.

Knowing that SMIs are actually happening at a certain rate is important information. But you also want to know how much time System Management Mode (SMM) spends in those interrupts. For example, if an SMI interruption is only very short than it might be irrelevant for your realtime application. On the other hand, if you have hardware with long SMI interruptions you probably want to talk to the vendor, configure the firmware differently (if possible) and or switch to other hardware with less intrusive SMM.

The perf tool has a mode that measures how many cycles are spend in SMM during SMIs (using the information provided by certain CPU counters). Example:

# perf stat -a -A --smi-cost -- sleep 120
 Performance counter stats for 'system wide':

               SMI cycles%                 SMI# 
CPU0                      0.0%                    0 
CPU1                      0.0%                    0 
CPU2                      0.0%                    0
CPU3                      0.0%                    0

    120.002927948 seconds time elapsed

You can also look at the raw values with:

# perf stat -a -A --smi-cost --metric-only -- sleep 120

From that you can compute how much time an SMI takes on average on your machine. (divide cycles difference by the number of cycles per time unit).

It certainly makes sense to cross check the CPU counter based results with empiric ones.

You can use the Linux Hardware Latency Detector that is integrated in the Linux Kernel. Usage example:

# echo hwlat > /sys/kernel/debug/tracing/current_tracer
# echo 1 > /sys/kernel/debug/tracing/tracing_thresh
# watch -d -n 5 cat /sys/kernel/debug/tracing/tracing_max_latency
# echo "Don't forget to disable it again"
# echo nop > /sys/kernel/debug/tracing/current_tracer

Those tools are available on CentOS/RHEL 7 and should be available on other distributions, as well.

Regarding ballpark figures: Recently I came across a HP 2011-ish ProLiant Gen8 Xeon server that fires 504 SMIs per minute. Perf computes a rate of 0.1 % in SMM, and based on the counter values the averge time spent in an SMI is as high as several microseconds - but the Linux hwlat detector doesn't detect such high interruptions on that system.

That SMI rate matches what HP documents in its Configuring and tuning HPE ProLiant Servers for low-latency applications guide (October, 2017):

Disabling System Management Interrupts to the processor provides one of the greatest benefits to low-latency environments. Disabling the Processor Power and Utilization Monitoring SMI has the greatest effect because it generates a processor interrupt eight times a second in G6 and later servers.

(emphasis mine; and that guide also documents other SMI sources)

On a Supermicro board with Intel Atom C3758 and an Intel NUC (i5-4250U) system of mine there are exactly zero SMIs counted.

On an Intel i7-6600U based Dell laptop, the system reports 8 SMIs per minute, but the aperf counter is lower than the (unhalted) cycles counter which isn't supposed to happen.

maxschlepzig
  • 35,645
  • 14
  • 145
  • 182
2

Actually, SMI is used for more than just keyboard emulation. Servers use SMI to report and correct ECC memory errors, ACPI uses SMI to communicate with BIOS and perform some tasks, even enabling and disabling ACPI is done through SMI, BIOS often intercepts power state changes through SMI... there's more, this is just a few examples.

kubik
  • 21
  • 1
  • 4
    The performance impact can be huge as well. I just go results from `hwlatdetect` which consistently measured over 300ms (!) latency, which in turn correlates with ECC memory scrubbing error events (via `mcelog`). In this particular case it's likely a stuck bit since the error always occurs on the same same address. Which might also explain why the correction is very slow. – mandrake Mar 15 '18 at 06:59
0

According to wikipage on System Management Mode, SMI is not used during normal operation, except perhaps to emulate a PS/2 keyboard with a USB physical keyboard.

And most Linux systems are able to drive genuine USB keyboard without that emulation. You could configure your BIOS to disable it.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    As of 2019, the Wikpedia article you have linked lists several other examples in the Usage Section for how SMIs are used during normal operation (on some systems): https://en.wikipedia.org/wiki/System_Management_Mode – maxschlepzig Sep 16 '19 at 17:40
  • BIOS/firmware can use SMI for any purpose they want. A well made motherboard and firmware would not use SMI at all but many manufacturers are doing bad products. – Mikko Rantalainen May 30 '22 at 08:00