Time Representation

The generic time framework uses the data type ktime_t to represent time values. Irregardless of the underlying architecture, the type always resolves to a 64-bit quantity. This makes the structure convenient to work with on 64-bit architectures as only simple integer operations are required for time-related operations.

To reduce the effort on 32-bit machines, the definition ensures that the two 32-bit values are ordered such that they can be directly interpreted as a 64-bit quantity without further ado — clearly this requires sorting the fields differently depending on the processor's endianness:

typedef union {

s64 tv64;

#if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR) struct {

# ifdef _BIG_ENDIAN

s32 sec, nsec;

# endif

If a 32-bit architecture provides functions that handle 64-bit quantities efficiently, it can set the configuration option ktime_scalar — IA-32 is the only architecture that makes use of this possibility at the moment. A separation into two 32-bit values is not performed in this case, but the representation of kernel times as direct 64-bit quantities is used.

Several auxiliary functions to handle ktime_t objects are defined by the kernel. Among them are the following:

□ ktime_sub and ktime_add are used to subtract and add ktime_ts, respectively.

□ ktime_add_ns adds a given number of nanoseconds to a ktime_t. ktime_add_us is another variant for microseconds. ktime_sub_ns and ktime_sub_us are also available.

□ ktime_set produces a ktime_t from a given number of seconds and nanoseconds.

□ Various functions of the type x_to_y convert between representation x and y, where the types ktime_t, timeval, clock_t, and timespec are possible.

9 Architectures that are currently migrating to the generic clock event framework can set GENERIC_CLOCKEVENTS_MIGR. This will build the code, but not use it at run time.

Note that a direct interpretation of a ktime_t as a number of nanoseconds would be possible on 64-bit machines, but can lead to problems on 32-bit machines. Thus, the function ktime_to_ns is provided to perform the conversion properly. The auxiliary function ktime_equal is provided to decide if two ktime_ts are identical.

To provide exchangeability with other time formats used in the kernel, some conversion functions are available:

ktime_t timespec_to_ktime(const struct timespec ts) ktime_t timeval_to_ktime(const struct timeval tv) struct timespec ktime_to_timespec(const ktime_t kt) struct timeval ktime_to_timeval(const ktime_t kt) s64 ktime_to_ns(const ktime_t kt) s64 ktime_to_us(const ktime_t kt)

The function names specify which quantity is converted into which, so there's no need to add anything further.

Continue reading here: Clock Sources

Was this article helpful?

+1 0