sys_clock_getres(9)
β¨ sys_clock_getres(9) β¨
okay so this is a page from the Kernel API Manual (section 9, kernel developer land π§) and it's all about sys_clock_getres β the syscall that tells you how precise a clock actually is. let's get into it bestie π
πΈ NAME πΈ
sys_clock_getres β get the resolution of a clock β°β¨
π SYNOPSIS π
long sys_clock_getres (const clockid_t which_clock , struct
__kernel_timespec __user * tp );
π ARGUMENTS π
which_clockβ the clock you want the resolution for π°οΈtpβ pointer to a a user spacetimespec64for storage π¦
π¦ DESCRIPTION π¦
so POSIX says, and I quote:
"The clock_getres function shall return the resolution of any clock. Clock resolutions are implementation-defined and cannot be set by a process. If the argument res is not NULL, the resolution of the specified clock shall be stored in the location pointed to by res. If res is NULL, the clock resolution is not returned. If the time argument of clock_settime is not a multiple of res, then the value is truncated to a multiple of res."
cute, love that for POSIX π but here's the tea: because hardware is messy, the real resolution can vary wildly and can even change during runtime when the underlying devices get replaced. and the kernel can use hardware devices with different resolutions for reading the time vs. arming timers π so yeah, the kernel deviates from the POSIX spec in a few ways:
1οΈβ£ the resolution returned to user space
for CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_TAI, CLOCK_REALTIME_ALARM, CLOCK_BOOTTIME_ALAREM and CLOCK_MONOTONIC_RAW, the kernel only differentiates two cases:
I) low resolution mode π’
when high resolution timers are disabled at compile or runtime, the resolution returned is nanoseconds per tick, which represents the precision at which timers expire.
II) high resolution mode π
when high resolution timers are enabled, the resolution returned is always one nanosecond, independent of the actual resolution of the underlying hardware devices. slay.
for CLOCK_*_ALARM the actual resolution depends on system state β when the system is running, the resolution is the same as the resolution of the other clocks. during suspend, the actual resolution is the resolution of the underlying RTC device, which might be way less precise than the clockevent device used during running state π΄
for CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE the resolution returned is always nanoseconds per tick.
for CLOCK_PROCESS_CPUTIME and CLOCK_THREAD_CPUTIME the resolution returned is always one nanosecond, under the assumption that the underlying scheduler clock has a better resolution than nanoseconds per tick.
for dynamic POSIX clocks (PTP devices) the resolution returned is always one nanosecond β¨
2οΈβ£ affect on sys_clock_settime
the kernel does not truncate the time that's handed in to sys_clock_settime. the kernel internal timekeeping is always using nanosecond precision, independent of the clocksource device used to read the time from. the resolution of that device only affects the precision of the time returned by sys_clock_gettime π₯
π RETURN π
0β success πtpcontains the resolution-EINVALβwhich_clockis not a valid clock ID π-EFAULTβ copying the resolution totpfaulted π-ENODEVβ dynamic POSIX clock is not backed by a device π-EOPNOTSUPPβ dynamic POSIX clock does not support getres π«
π SEE ALSO π
- kernel file
./kernel/time/posix-timers.c sys_timer_getoverrun(9)
that's the whole vibe bestie, go forth and check your clock resolutions responsibly πβ¨
β with love, from ./kernel/time, dated October 2026 ππ¦ (kernel api manual, section 9, periodt)
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms