Ce contenu n'est pas disponible dans la langue sélectionnée.
15.2. POSIX Clocks
CLOCK_REALTIME
: it represents the time in the real world, also referred to as 'wall time' meaning the time as read from the clock on the wall. This clock is used to timestamp events, and when interfacing with the user. It can be modified by an user with the right privileges. However, user modification should be used with caution as it can lead to erroneous data if the clock has its value changed between two readings.CLOCK_MONOTONIC
: represents the time monotonically increased since the system boot. This clock cannot be set by any process, and is the preferred clock for calculating the time difference between events. The following examples in this section useCLOCK_MONOTONIC
as the POSIX clock.
Note
- clock_gettime()
- Linux System Programming by Robert Love
clock_gettime()
, which is defined at <time.h>
. The clock_gettime()
command takes two parameters: the POSIX clock ID and a timespec structure which will be filled with the duration used to read the clock. The following example shows the function to measure the cost of reading the clock:
Example 15.2. Using clock_gettime()
to Measure the Cost of Reading POSIX Clocks
clock_gettime()
, to verify the value of the rc
variable, or to ensure the content of the ts
structure is to be trusted. The clock_gettime()
manpage provides more information to help you write more reliable applications.
Important
clock_gettime()
function must be linked with the rt
library by adding '-lrt'
to the gcc
command line:
gcc clock_timing.c -o clock_timing -lrt
~]$ gcc clock_timing.c -o clock_timing -lrt
15.2.1. CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSE Copier lienLien copié sur presse-papiers!
clock_gettime()
and gettimeofday()
have a counterpart in the kernel, in the form of a system call. When a user process calls clock_gettime()
, the corresponding C library (glibc
) routine calls the sys_clock_gettime()
system call, which performs the requested operation and then returns the result to the user process.
CLOCK_MONOTONIC_COARSE
and CLOCK_REALTIME_COARSE
POSIX clocks was created in the form of a VDSO library function. The _COARSE
variants are faster to read and have a precision (also known as resolution) of one millisecond (ms).
15.2.2. Using clock_getres() to Compare Clock Resolution Copier lienLien copié sur presse-papiers!
clock_getres()
function you can check the resolution of a given POSIX clock. clock_getres()
uses the same two parameters as clock_gettime()
: the ID of the POSIX clock to be used, and a pointer to the timespec structure where the result is returned. The following function enables you to compare the precision between CLOCK_MONOTONIC
and CLOCK_MONOTONIC_COARSE
:
Example 15.3. Sample Output of clock_getres
15.2.3. Using C Code to Compare Clock Resolution Copier lienLien copié sur presse-papiers!
CLOCK_MONOTONIC
POSIX clock. All nine digits in the tv_nsec
field of the timespec structure are meaningful as the clock has a nanosecond resolution. The example function, named clock_test.c
, is as follows:
Example 15.4. Sample Output of clock_test.c
and clock_test_coarse.c
clock_test_coarse.c
and replacing CLOCK_MONOTONIC
with CLOCK_MONOTONIC_COARSE
, the result would look something like:
_COARSE
clocks have a one millisecond precision, therefore only the first three digits of the tv_nsec
field of the timespec structure are significant. The result above could be read as:
_COARSE
variants of the POSIX clocks are particularly useful in cases where timestamping can be performed with millisecond precision. The benefits are more evident on systems which use hardware clocks with high costs for the reading operations, such as ACPI_PM.
15.2.4. Using the time Command to Compare Cost of Reading Clocks Copier lienLien copié sur presse-papiers!
time
command to read the clock source 10 million times in a row, you can compare the costs of reading CLOCK_MONOTONIC
and CLOCK_MONOTONIC_COARSE
representations of the hardware clocks available. The following example uses TSC, HPET and ACPI_PM hardware clocks. For more information on how to decipher the output of the time
command see Section 15.1.1, “Reading Hardware Clock Sources”.
Example 15.5. Comparing the Cost of Reading POSIX Clocks
sys
time (the time spent by the kernel to perform tasks required by the user process) is greatly reduced when the _COARSE
clocks are used. This is particularly evident in the ACPI_PM clock timings, which indicates that _COARSE
variants of POSIX clocks yield high performance gains on clocks with high reading costs.