[PATCH v2] time: Add TIME_MONOTONIC, TIME_ACTIVE, and TIME_THREAD_ACTIVE

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Tue Nov 25 12:56:38 GMT 2025



On 25/11/25 09:18, Mark Harris wrote:
> Adhemerval Zanella wrote:
>>
>> The TIME_MONOTONIC maps to POSIX's CLOCK_MONOTONIC, TIME_ACTIVE to
>> CLOCK_PROCESS_CPUTIME_ID, and TIME_THREAD_ACTIVE to
>> CLOCK_THREAD_CPUTIME_ID.
>>
>> No Linux specific timer are added as extension.
>>
>> Co-authored-by: Yonggang Luo <luoyonggang@gmail.com>
> 
> This may be a good opportunity to correct the longstanding discrepancy
> with the monotonic clock.
> 
> Although POSIX requires CLOCK_MONOTONIC to represent a time since some
> point in the past that does not change after system start-up, Linux
> does not conform to this because it excludes time while the system is
> suspended.  There was an attempt to correct this in the kernel in 2018
> (https://lore.kernel.org/lkml/20180301163331.987775783@linutronix.de/T/),
> but it was reverted due to dependence on the nonconforming behavior.
> Instead, Linux provides CLOCK_BOOTTIME with the behavior that POSIX
> requires for CLOCK_MONOTONIC.  Other systems including macOS and the
> BSDs have a conforming CLOCK_MONOTONIC, and may also provide
> CLOCK_UPTIME or CLOCK_UPTIME_RAW that behaves like Linux
> CLOCK_MONOTONIC.  The difference in naming complicates software that
> attempts to be portable.
> 
> timespec_get() offers another chance for an actually portable API for
> monotonic time.  The ISO C committee was aware of the discrepancy on
> Linux with CLOCK_MONOTONIC, and specifically defined TIME_MONOTONIC to
> allow either behavior, however it would benefit future users of the
> API if the behavior was actually consistent between systems.
> Otherwise everyone might as well stick to the #ifdefs that are
> currently required.  On FreeBSD, TIME_MONOTONIC corresponds to its
> POSIX-conforming CLOCK_MONOTONIC, and the same behavior could be
> provided on Linux by mapping TIME_MONOTONIC to CLOCK_BOOTTIME.  It
> could still be mapped by subtracting 1 if TIME_MONOTONIC is defined to
> (CLOCK_BOOTTIME+1).  Windows and macOS support C11's TIME_UTC but do
> not yet support C23's TIME_MONOTONIC .

Thanks for bringing this up and it is reasonable.  However this will add
a different discrepancy where from FreeBSD documentation TIME_MONOTONIC
maps to CLOCK_MONOTONIC, while for glibc it will map to CLOCK_BOOTIME.

This will make usage as described by the one in libc-coord [1] even
more confusing (exchanging clock id between C and POSIX interfaces);
even if CLOCK_MONOTONIC does not map semantically between systems.

I am not if we should just set TIME_MONOTONIC to CLOCK_BOOTTIME on
Linux or if we add a TIME_BOTTIME as a GNU extension.

[1] https://www.openwall.com/lists/libc-coord/2023/04/25/1



More information about the Libc-alpha mailing list