Kernel:Kernel Timers
- IRIX Kernel Timers
The IRIX kernel implements a **high-resolution, per-thread timer subsystem** designed to track the time threads spend in various modes (user, system, interrupt, etc.) with hardware-specific accuracy. This subsystem differs from Illumos/OpenSolaris in several notable ways due to differences in hardware assumptions and kernel design.
---
- Overview
IRIX kernel timers are responsible for:
- Maintaining **accumulated time** for individual threads across multiple timer modes.
- Providing **high-resolution snapshots** of thread execution time.
- Handling **timer switching** and accumulation safely in SMP or multi-threaded contexts.
- Measuring proportional **thread sleep/wait times**.
- Converting low-level hardware ticks into standard time units (seconds and nanoseconds).
---
- Key Concepts
- Snapshots
- Each thread maintains a **timer snapshot**, representing its current time state.
- Snapshots may consist of:
* A single high-resolution counter (if the platform supports absolute, monotonic timers), or * A combination of seconds and hardware-specific ticks to handle short-wrap timers.
- Snapshots are used to compute deltas between timer updates and to detect timer wrap-around.
- Timer Accumulation
- Each thread has a **per-thread timer package** that tracks:
* The currently active timer mode. * Accumulated time for all supported timers. * The last snapshot for delta calculations.
- When switching timers, IRIX computes the delta between the last snapshot and the current time, adding it to the accumulator for the previous timer mode.
- Timer Switching
- Threads can switch between different timer modes explicitly.
- Timer switching involves:
* Snapshotting the current timer. * Updating the accumulator of the previously active timer. * Setting the new timer as active.
- Race conditions are carefully handled by verifying that snapshots are consistent during the update.
- Reading Timers
- Timer reads return the accumulated time for a specific thread and timer mode.
- If the requested timer is currently active, IRIX computes the time since the last snapshot and adds it to the accumulator.
- Reading is designed to be **race-resilient**, looping until a consistent snapshot is obtained.
- Sleep Time Measurement
- IRIX provides a measure of **thread sleep/wait time**, representing how long a thread has been idle.
- This measure is proportional to real time but does not guarantee absolute accuracy.
- Threads in active timer modes (user, system, or interrupt) report zero sleep time.
- Conversion to Standard Time
- Low-level hardware ticks are converted into **seconds and nanoseconds** for user-facing APIs.
- Conversion uses hardware-specific frequency and unit scaling, reflecting differences in RTC or high-resolution counters.
- SMP and Atomic Operations
- On multi-processor systems, atomic operations ensure correct accumulation of timers.
- IRIX distinguishes **CELL IRIX** systems, which use specific atomic add instructions for timer updates.
- Non-CELL SMP systems rely on standard atomic or interrupt-safe mechanisms.
---
- Differences from Illumos/OpenSolaris
| Feature | IRIX | Illumos/OpenSolaris | Notes | | -------------------- | ------------------------------------------ | --------------------------------------- | ------------------------------------------------------------------------------- | | Timer representation | Hardware-specific ticks, may wrap | 64-bit monotonic nanoseconds (`hrtime`) | IRIX handles wrap-around; Solaris simpler | | Snapshots | Stored as ticks or [seconds + ticks] | Stored as cumulative `hrtime` | IRIX explicitly tracks dual-field snapshots for wrap detection | | Timer switching | Explicit per-thread function | Handled by scheduler/context switch | IRIX exposes manual switching, Solaris hides it internally | | Sleep time | Computed dynamically from snapshots | Maintained in thread scheduler counters | IRIX calculates on-demand; Solaris updates in scheduler | | SMP safety | Explicit atomic operations (CELL-specific) | Generic atomic operations | IRIX has platform-specific atomic paths; Solaris uses uniform atomic ops | | Time conversion | Scaled using hardware frequency | Division of nanoseconds | IRIX converts hardware ticks; Solaris works directly with `hrtime` | | Race handling | Looping snapshot checks | Scheduler guarantees consistency | IRIX implements explicit consistency checks; Solaris relies on locks/atomic ops |
---
- Key Takeaways
- IRIX kernel timers are **hardware-conscious**, designed for platforms with wrapping high-resolution counters.
- The subsystem provides **manual control** over timer modes and accumulators.
- It carefully manages **race conditions and SMP safety**, including platform-specific atomic operations.
- Unlike Illumos/OpenSolaris, IRIX must scale raw hardware ticks into standard time units and detect timer wrap-around.
- Sleep time and deltas are **computed dynamically** rather than tracked in scheduler-maintained counters.