21.1 Introduction to the Time Subsystem
The Linux time subsystem stands as one of the most complex components within the core kernel. Beyond merely counting seconds, it directly drives process scheduling, network timeouts, and low-power management. The initialization of this subsystem is a tightly coupled process. Structurally, the time subsystem's initialization can be categorized into three logical tiers: the hardware layer, the kernel framework layer, and the user interface/kernel layer.
The Hardware Layer provides the physical foundation for tracking time and is primarily composed of two categories of hardware devices: clock sources (Clocksource) and clock event devices (Clock Event Device).
-
A clock source continuously increments a counter, and the kernel reads the difference between counter intervals to compute elapsed time.
-
A clock event device acts as a programmable timer, which the kernel configures to trigger hardware interrupts at precise future moments to awaken the CPU to process tasks.
The Kernel Framework Layer abstracts the underlying hardware to provide unified, standard interfaces to the upper layers. It primarily encompasses the timekeeper (Timekeeping), clock tick control (Tick Control), periodic ticks (periodic tick), tickless operation (tickless or NO_HZ), and the generic time subsystem (Generic Time Subsystem).
-
The timekeeper maintains the system's real-world time (Wall Time), monotonic time (Monotonic Time), and alternative time bases.
-
The clock tick serves as the foundational rhythm for task scheduling; the task scheduler relies on it to determine the execution time allocated to running threads.
-
The tickless mode represents a vital feature of modern kernels, allowing the system to completely suspend periodic interrupts when a CPU core falls idle to achieve significant power savings.
-
The periodic tick represents the traditional fixed-frequency interrupt paradigm (most commonly operating at a frequency of 100 Hz or 1000 Hz).
-
The generic time subsystem manages the programmatic registration and automatic hot-swapping between diverse clock sources and clock event devices.
The User/Kernel Interface Layer (API Layer) represents the execution environment that developers interface with directly. It primarily includes low-resolution timers (Low-res Timers), high-resolution timers (HRTimers), and the global jiffies variable.
-
Low-resolution timers rely on the Timer Wheel algorithm; they are optimized to handle timeout tasks that do not mandate microsecond-level accuracy (operating on a millisecond granularity) and introduce exceptionally low overhead.
-
High-resolution timers are managed via an interval red-black tree structure, supporting nanosecond-level accuracy, and form the baseline operational foundation for the Completely Fair Scheduler and real-time systems.
-
The global jiffies variable records the aggregate count of "clock ticks" that have elapsed since system boot, routinely leveraged inside peripheral device drivers to handle basic delay checks.
During the hardware initialization phases detailed across the preceding chapters, the setup of the underlying clock hardware—encompassing internal CPU counters, control registers, and physical timer devices—has been successfully executed. This chapter outlines the initialization sequences of the remaining administrative modules.