Clock in the Linux System
An overview on hardware clock and system timer circuits
The timer circuits are programmed by the kernel, so they issue interrupts at a fixed, and predefined, frequency.For instance, IA-32 and AMD64 systems have at least one programmable interrupt timer (PIT) as a classical timer circuit, which is usually implemented by an 8254 CMOS chip.
The clock and timer circuits that are usually found with any nearly modern system of those architectures:
- Real Time Clock (RTC) The RTC is independent of the system's CPU and any other chips.
- Programmable Interrupt Timer (PIT) The PIT is a time-measuring device that can be compared to the alarm clock of a microwave oven: it makes the user aware that the cooking time interval has elapsed.
- Time Stamp Counter (TSC) All 80x86 microprocessors include a CLK input pin, which receives the clock signal of an external oscillator.
- CPU Local Timer The Local APIC (Advanced Programmable Interrupt Controller) present in recent 80x86 microprocessors provide yet another time measuring device, and it is a device, similar to the PIT, which can issue one-shot or periodic interrupts.
- High Precision Event Timer (HPET) The HPET is a timer chip that in some future time is expected to completely replace the PIT.
- ACPI Power Management Timer (ACPI PMT) The ACPI PMT is another clock device included in almost all ACPI-based motherboards.
As it is energized by a small battery, it continues to tick even when the system is switched off.
The RTC is capable of issuing interrupts at frequencies ranging between 2 Hz and 8,192 Hz.
Linux uses the RTC only to derive the time and date at boot time.
Instead of ringing a bell, the PIT issues a special interrupt called timer interrupt, which notifies the kernel that one more time interval has elapsed.
As the time goes by, the PIT goes on issuing interrupts forever at some fixed (architecture-specific) frequency established by the kernel.
Starting with the Pentium, 80x86 microprocessors sport a counter that is increased at each clock signal, and is accessible through the TSC register which can be read by means of the rdtsc assembly instruction.
When using this register the kernel has to take into consideration the frequency of the clock signal: if, for instance, the clock ticks at 1 GHz, the TSC is increased once every nanosecond.
Linux may take advantage of this register to get much more accurate time measurements.
There are, however, a few differences:
The APIC's timer counter is 32 bit long, while the PIT's timer counter is 16 bit long; The local APIC timer sends interrupts only to its processor, while the PIT raises a global interrupt, which may be handled by any CPU in the system;
The APIC's timer is based on the bus clock signal, and it can be programmed in such way to decrease the timer counter every 1, 2, 4, 8, 16, 32, 64, or 128 bus clock signals. Conversely, the PIT, which makes use of its own clock signals, can be programmed in a more flexible way.
It provides a number of hardware timers that can be exploited by the kernel.
Basically the chip includes up to eight 32 bit or 64 bit independent counters.
Each counter is driven by its own clock signal, whose frequency must be at least 10 MHz; therefore the counter is increased at least once in 100 nanoseconds.
Any counter is associated with at most 32 timers, each of which composed by a comparator and a match register. The HPET registers allow the kernel to read and write the values of the counters and of the match registers, to program one-shot interrupts, and to enable or disable periodic interrupts on the timers that support them.
Its clock signal has a fixed frequency of roughly 3.58 MHz.
The device is a simple counter increased at each clock tick.
May introduce issues with sub-millisecond resolution.
On modern hardware should be used as a clock source of last resort only.
$ cat /sys/devices/system/clocksource/clocksource0/available_clocksource tsc hpet acpi_pmkernel-parameters.html:
- clocksource= Override the default clocksource and use the clocksource with the name specified.
- [all] jiffies (this is the base, fallback clocksource)
- [ACPI] acpi_pm
- [X86-32] pit,hpet,tsc; scx200_hrt on Geode; cyclone on IBM x440
- [X86-64] hpet,tsc
- [ARM] imx_timer1,OSTS,netx_timer,mpu_timer2, pxa_timer,timer3,32k_counter,timer0_1
- [MIPS] MIPS
- [PARISC] cr16
- [S390] tod
- [SH] SuperH
- [SPARC64] tick
- clocksource.arm_arch_timer.evtstrm= Enable/disable the eventstream feature of the ARM architected timer so that code using WFE-based polling loops can be debugged more effectively on production systems.
Some clocksource names to choose from, depending on the [platform]
How to check what clock source is currently in use by the system
$ cat /sys/devices/system/clocksource/clocksource0/current_clocksource tsc
How to override system's configuration specifying a particular clock source
Dynamically and temporarily (not persistent to reboots):echo "acpi_pm" > /sys/devices/system/clocksource/clocksource0/current_clocksourcePermanently, adding clocksource= to the kernel parameters:
clocksource=acpi_pm
留言