2.6. Using Hardware Clocks for System Timestamping
/sys/devices/system/clocksource/clocksource0/available_clocksource
file:
~]# cat /sys/devices/system/clocksource/clocksource0/available_clocksource
tsc hpet acpi_pm
/sys/devices/system/clocksource/clocksource0/current_clocksource
file:
~]# cat /sys/devices/system/clocksource/clocksource0/current_clocksource
tsc
Sometimes the best-performing clock for a system's main application is not used due to known problems on the clock. After ruling out all problematic clocks, the system can be left with a hardware clock that is unable to satisfy the minimum requirements of a real-time system.
/sys/devices/system/clocksource/clocksource0/available_clocksource
file and write the clock's name into the /sys/devices/system/clocksource/clocksource0/current_clocksource
file. For example, the following command sets HPET as the clock source in use:
~]# echo hpet > /sys/devices/system/clocksource/clocksource0/current_clocksource
Note
While there is no single clock which is ideal for all systems, TSC is generally the preferred clock source. To optimize the reliability of the TSC clock, you can configure additional parameters when booting the kernel, for example:
idle=poll
: Forces the clock to avoid entering the idle state.processor.max_cstate=1
: Prevents the clock from entering deeper C-states (energy saving mode), so it does not become out of sync.
Modern processors actively transition to higher power saving states (C-states) from lower states. Unfortunately, transitioning from a high power saving state back to a running state can consume more time than is optimal for a real-time application. To prevent these transitions, an application can use the Power Management Quality of Service (PM QoS) interface.
idle=poll
and processor.max_cstate=1
parameters (as listed in Configuring Additional Boot Parameters for the TSC Clock), but with a more fine-grained control of power saving states.
/dev/cpu_dma_latency
file open, the PM QoS interface prevents the processor from entering deep sleep states, which cause unexpected latencies when they are being exited. When the file is closed, the system returns to a power-saving state.
- Open the
/dev/cpu_dma_latency
file. Keep the file descriptor open for the duration of the low-latency operation. - Write a 32-bit number to it. This number represents a maximum response time in microseconds. For the fastest possible response time, use
0
.An example/dev/cpu_dma_latency
file is as follows:static int pm_qos_fd = -1; void start_low_latency(void) { s32_t target = 0; if (pm_qos_fd >= 0) return; pm_qos_fd = open("/dev/cpu_dma_latency", O_RDWR); if (pm_qos_fd < 0) { fprintf(stderr, "Failed to open PM QOS file: %s", strerror(errno)); exit(errno); } write(pm_qos_fd, &target, sizeof(target)); } void stop_low_latency(void) { if (pm_qos_fd >= 0) close(pm_qos_fd); }
The application will first callstart_low_latency()
, perform the required latency-sensitive processing, then callstop_low_latency()
.
For more information, or for further reading, the following book is related to the information given in this section.
- Linux System Programming by Robert Love