On this page
Time Management
Coverage: timekeeping → CLOCK_MONOTONIC/CLOCK_REALTIME/CLOCK_BOOTTIME → NTP → PTP → vsyscall/vDSO → Hardware clocks such as TSC/HPET/ACPI Kernel versions: 2.6 ~ 6.x
Overview
Kernel time management answers three questions: "What time is it?", "How much time has passed?", and "When to wake up?". Although it seems simple, it involves the selection and arbitration of multiple clock sources, NTP time synchronization, and fast time access in user space.
The key concept is distinguishing between REALTIME (wall-clock time, adjustable by NTP, affected by leap seconds) and MONOTONIC (reliable measurement since boot, monotonically increasing).
Clock Types
// include/uapi/linux/time.h
// CLOCK_REALTIME: Wall-clock time (Unix epoch)
// Adjusted by NTP, can jump backwards (leap seconds, settimeofday)
// → Not suitable for delta measurements
// CLOCK_MONOTONIC: Time since boot
// Monotonically increasing, not adjusted by NTP, does not jump backwards
// → Suitable for measuring time intervals
// CLOCK_MONOTONIC_RAW: Raw hardware count
// Not subject to NTP frequency adjustments (harder to distort than MONOTONIC)
// → Suitable for precise benchmarking
// CLOCK_BOOTTIME: Includes suspend time
// Similar to MONOTONIC, but includes system sleep time
// → Suitable for scenarios requiring "true elapsed wall-clock time"
// CLOCK_TAI: International Atomic Time
// Offset from UTC by 37 seconds (as of 2024)
// Not affected by leap seconds
timekeeping Core
// kernel/time/timekeeping.c
// Core data structure:
;
// timekeeping_advance():
// Called once per tick (or hrtimer interrupt)
// 1. Read clocksource
// 2. Calculate delta cycles → delta_ns
// 3. Accumulate delta_ns into xtime_nsec (handling carry)
// 4. Apply NTP frequency correction (mult adjustment)
// 5. Update all derived times (MONOTONIC, BOOTTIME, TAI)
NTP Time Synchronization
// kernel/time/ntp.c
// Kernel NTP daemon:
// Not the user-space ntpd/chronyd — those are controllers
// The kernel NTP code performs the actual frequency and phase adjustments
// adjtimex() system call:
// User-space ntpd tells the kernel via adjtimex:
// 1. Current offset (phase error, in nanoseconds)
// 2. Current freq (frequency error, in PPM)
// 3. Maximum error
// Kernel: Adjusts timekeeper->mult (frequency) and offset (phase)
// PTP (Precision Time Protocol):
// Linux supports hardware timestamps via SO_TIMESTAMPING
// PHC (PTP Hardware Clock) drivers → /dev/ptp*
// → Nanosecond-level time synchronization (data centers/financial trading)
vDSO: Fast Time Access
// arch/x86/entry/vdso/
// Problem: If clock_gettime() uses a syscall → ~100ns per call (syscall + context switch)
// High-frequency calls (e.g., profiling) incur excessive overhead
// vDSO (virtual Dynamic Shared Object):
// The kernel injects a small piece of code into each process's address space
// → clock_gettime() executes in user space → bypasses the kernel!
// → Reads clocksource, applies timekeeper's offset and mult → returns time
// → Latency: ~20ns (vs ~100ns for syscall)
// vsyscall (legacy, deprecated) vs vDSO (modern):
// vsyscall: Fixed address, security risk (prone to ROP attacks)
// vDSO: ASLR random address, standard ELF .so
x86 vDSO Implementation
// arch/x86/entry/vdso/vclock_gettime.c
// clock_gettime(CLOCK_MONOTONIC) in user space:
notrace int
Hardware Clock Sources
| Clock Source | Precision | Latency | Stability | Scenario |
|---|---|---|---|---|
| TSC (rdtsc) | ~0.3ns | ~10 cycles | High (constant/invariant TSC) | Default preferred |
| HPET | ~100ns | ~500 cycles | High | Legacy systems or when TSC is unstable |
| ACPI PM Timer | ~280ns | ~1μs | Medium | Same as above |
| ARM Generic Timer | ~10ns | ~10 cycles | High | ARM64 default |
| KVM pvclock | ~20ns | ~200 cycles | Guaranteed by host | Virtualization |
# View the currently used clocksource
# View available list
References and Further Reading
- Kernel Documentation:
Documentation/timers/timekeeping.rst - LWN: "The vDSO and time", "Timekeeping in the Linux kernel"
- Source Code:
kernel/time/timekeeping.c— timekeeping corekernel/time/ntp.c— NTParch/x86/entry/vdso/— x86 vDSOarch/arm64/kernel/vdso/— ARM64 vDSOkernel/time/clocksource.c— clocksource management
Keywords: CLOCK_MONOTONIC, CLOCK_REALTIME, timekeeper, NTP, PTP, vDSO, TSC, HPET, clocksource