Chapter 28. Application timestamping 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. 28.1. 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 . 28.2. The _COARSE clock variant in clock_gettime The example code output shows 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 28.3. Additional resources clock_gettime() man page on your system

Commands:
#include <time.h> main() { int rc; long i; struct timespec ts; for(i=0; i<10000000; i++) { rc = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); } }