Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using TSC as clock source improve timer and scheduling granularity?

In processors that support time stamp counter (TSC) Linux provides the high resolution timer option using TSC. From what I understand TSC is a register that can be read but doesn't provide the option to interrupt the CPU at a configured rate. So for the timer interrupt generation in Linux still has to rely on I/O APIC (on x86) with HZ value typically set to 1000 or 250.

Even though TSC gives time stamps at micro second granularity the timer/scheduling granularity will still be at 4ms or 1ms depending on the HZ value. Is this understanding correct ? Or is there an option to improve the timer granularity using the TSC ?

like image 325
Manohar Avatar asked Dec 19 '12 10:12

Manohar


1 Answers

In the default Linux 2.6 kernel, the Programmable Interrupt Controller (PIT) (available on all PCs) is used as a system timer [1]. PIT, as the name suggests, can be programmed (usually, when kernel boots up) to interrupt the CPU at a pre-determined rate. This pre-determined rate is the HZ value that you refer to, which is a statically compiled value equal to the kernel compilation parameter CONFIG_HZ.[2] So, you can modify CONFIG_HZ at compile time and the PIT would start interrupting the CPU at the said frequency. However, keep in mind, that PIT is internally driven by a clock of some 1.193 MHZ, so setting CONFIG_HZ larger than this value won't be a great idea. And as pointed out in [3]

the timer of the local APIC (Advanded Programmable Interrupt Controller) in multiprocessor systems is used for interprocessor synchronization

and going by explanation in [1], I believe that its PIT (and not local APIC) that is tied to the HZ value (at least until 2.6 kernel).

Now, coming to your question, in theory your idea looks correct. Time stamp counter like local APIC and PIT, is another time source[1]. In [4], you find a confirmation of this.

Linux may take advantage of this register to get much more accurate time measurements than those delivered by the Programmable Interval Timer. To do this, Linux must determine the clock signal frequency while initializing the system. In fact, because this frequency is not declared when compiling the kernel, the same kernel image may run on CPUs whose clocks may tick at any frequency.

However, remember that Time stamp counter is incremented at every CPU clock cycle. And this brings us to the tricky pitfalls associated with a counter associated with CPU clock cycles[5]. One example is that modern CPUs can change their CPU clock rate to save power and this would affect the value stored in Time-stamp counter. If that happens, you can estimate the effect it can have on your time measurements. Also, an absolutely idle kernel may call the HALT instruction that stops the processor altogether until an external interrupt is received. All this time, the TSC would never be incremented and you would lose some precious 'increments', that would have otherwise made your measurements more precise. In short, handling TSC is a hard problem, not particularly amenable to use as a programmable interrupt.

  1. Robert Love, LKD --3rd Edition. (Chapter 11)
  2. http://lxr.linux.no/linux+v2.6.31/arch/x86/include/asm/param.h#L5
  3. http://www.6test.edu.cn/~lujx/linux_networking/0131777203_ch02lev1sec7.html
  4. http://www.makelinux.net/books/ulk3/understandlk-CHP-6-SECT-1
  5. http://lwn.net/Articles/209101/
like image 190
gjain Avatar answered Oct 20 '22 19:10

gjain