Chapter 6. Application timestamping on RHEL for Real Time
Chapter 6. Application timestamping on RHEL for Real Time Applications that perform frequent timestamps are affected by the CPU cost of reading the clock. The high cost and amount of time used to read the clock can have a negative impact on an application's performance. You can reduce the cost of reading the clock by selecting a hardware clock that has a reading mechanism, faster than that of the default clock. In RHEL for Real Time, a further performance gain can be acquired by using POSIX clocks with the clock_gettime() function to produce clock readings with the lowest possible CPU cost. These benefits are more evident on systems which use hardware clocks with high reading costs. 6.1. Hardware clocks Multiple instances of clock sources found in multiprocessor systems, such as non-uniform memory access (NUMA) and Symmetric multiprocessing (SMP), interact among themselves and the way they react to system events, such as CPU frequency scaling or entering energy economy modes, determine whether they are suitable clock sources for the real-time kernel. The preferred clock source is the Time Stamp Counter (TSC). If the TSC is not available, the High Precision Event Timer (HPET) is the second best option. However, not all systems have HPET clocks, and some HPET clocks can be unreliable. In the absence of TSC and HPET, other options include the ACPI Power Management Timer (ACPI_PM), the Programmable Interval Timer (PIT), and the Real Time Clock (RTC). The last two options are either costly to read or have a low resolution (time granularity), therefore they are sub-optimal for use with the real-time kernel. 6.2. POSIX clocks POSIX is a standard for implementing and representing time sources. You can assign a POSIX clock to an application without affecting other applications in the system. This is in contrast to hardware clocks which are selected by the kernel and implemented across the system. The function used to read a given POSIX clock is clock_gettime() , which is defined at <time.h> . The kernel counterpart to clock_gettime() is a system call. When a user process calls clock_gettime() : The corresponding C library ( glibc ) calls the sys_clock_gettime() system call. sys_clock_gettime() performs the requested operation. sys_clock_gettime() returns the result to the user program. However, the context switch from the user application to the kernel has a CPU cost. Even though this cost is very low, if the operation is repeated thousands of times, the accumulated cost can have an impact on the overall performance of the application. To avoid context switching to the kernel, thus making it faster to read the clock, support for the CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSE POSIX clocks was added, in the form of a virtual dynamic shared object (VDSO) library function. Time readings performed by clock_gettime() , using one of the _COARSE clock variants, do not require kernel intervention and are executed entirely in user space. This yields a significant performance gain. Time readings for _COARSE clocks have a millisecond (ms) resolution, meaning that time intervals smaller than 1 ms are not recorded. The _COARSE variants of the POSIX clocks are suitable for any application that can accommodate millisecond clock resolution. Note To compare the cost and resolution of reading POSIX clocks with and without the _COARSE prefix, see the RHEL for Real Time Reference guide . 6.3. clock_gettime() function The following code shows an example of code using the clock_gettime() function with the CLOCK_MONOTONIC_COARSE POSIX clock: You can improve upon the example above by adding checks to verify the return code of clock_gettime() , to verify the value of the rc variable, or to ensure the content of the ts structure is to be trusted. Note The clock_gettime() man page provides more information about writing more reliable applications. Important Programs using the clock_gettime() function must be linked with the rt library by adding -lrt to the gcc command line. USD gcc clock_timing.c -o clock_timing -lrt 6.4. Additional resources clock_gettime() man page on your system
#include <time.h> main() { int rc; long i; struct timespec ts; for(i=0; i<10000000; i++) { rc = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); } }