kernel/time/timer.c
Source file repositories/reference/linux-study-clean/kernel/time/timer.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/time/timer.c- Extension
.c- Size
- 79016 bytes
- Lines
- 2581
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel_stat.hlinux/export.hlinux/interrupt.hlinux/percpu.hlinux/init.hlinux/mm.hlinux/swap.hlinux/pid_namespace.hlinux/notifier.hlinux/thread_info.hlinux/time.hlinux/jiffies.hlinux/posix-timers.hlinux/cpu.hlinux/syscalls.hlinux/delay.hlinux/tick.hlinux/kallsyms.hlinux/irq_work.hlinux/sched/sysctl.hlinux/sched/nohz.hlinux/sched/debug.hlinux/slab.hlinux/compat.hlinux/random.hlinux/sysctl.hlinux/uaccess.hasm/unistd.hasm/div64.hasm/timex.hasm/io.htick-internal.h
Detected Declarations
struct timer_basestruct timer_hintfunction timers_update_migrationfunction timer_migration_handlerfunction timer_sysctl_initfunction timers_update_migrationfunction timers_update_nohzfunction is_timers_nohz_activefunction is_timers_nohz_activefunction round_jiffies_commonfunction __round_jiffies_relativefunction round_jiffiesfunction round_jiffies_relativefunction __round_jiffies_relativefunction round_jiffiesfunction round_jiffies_relativefunction timer_get_idxfunction timer_set_idxfunction calc_indexfunction calc_wheel_indexfunction trigger_dyntick_cpufunction enqueue_timerfunction internal_add_timerfunction timer_is_static_objectfunction timer_fixup_initfunction stub_timerfunction timer_fixup_activatefunction timer_fixup_freefunction timer_fixup_assert_initfunction debug_timer_initfunction debug_timer_activatefunction debug_timer_deactivatefunction debug_timer_assert_initfunction timer_init_key_on_stackfunction timer_destroy_on_stackfunction debug_timer_initfunction debug_deactivatefunction debug_assert_initfunction do_init_timerfunction timer_init_keyfunction detach_timerfunction detach_if_pendingfunction __forward_timer_basefunction forward_timer_basefunction __mod_timerfunction mod_timer_pendingfunction mod_timerfunction timer_reduce
Annotated Snippet
device_initcall(timer_sysctl_init);
#endif /* CONFIG_SYSCTL */
#else /* CONFIG_SMP */
static inline void timers_update_migration(void) { }
#endif /* !CONFIG_SMP */
static void timer_update_keys(struct work_struct *work)
{
mutex_lock(&timer_keys_mutex);
timers_update_migration();
static_branch_enable(&timers_nohz_active);
mutex_unlock(&timer_keys_mutex);
}
void timers_update_nohz(void)
{
schedule_work(&timer_update_work);
}
static inline bool is_timers_nohz_active(void)
{
return static_branch_unlikely(&timers_nohz_active);
}
#else
static inline bool is_timers_nohz_active(void) { return false; }
#endif /* NO_HZ_COMMON */
static unsigned long round_jiffies_common(unsigned long j, int cpu,
bool force_up)
{
int rem;
unsigned long original = j;
/*
* We don't want all cpus firing their timers at once hitting the
* same lock or cachelines, so we skew each extra cpu with an extra
* 3 jiffies. This 3 jiffies came originally from the mm/ code which
* already did this.
* The skew is done by adding 3*cpunr, then round, then subtract this
* extra offset again.
*/
j += cpu * 3;
rem = j % HZ;
/*
* If the target jiffy is just after a whole second (which can happen
* due to delays of the timer irq, long irq off times etc etc) then
* we should round down to the whole second, not up. Use 1/4th second
* as cutoff for this rounding as an extreme upper bound for this.
* But never round down if @force_up is set.
*/
if (rem < HZ/4 && !force_up) /* round down */
j = j - rem;
else /* round up */
j = j - rem + HZ;
/* now that we have rounded, subtract the extra skew again */
j -= cpu * 3;
/*
* Make sure j is still in the future. Otherwise return the
* unmodified value.
*/
return time_is_after_jiffies(j) ? j : original;
}
/**
* __round_jiffies_relative - function to round jiffies to a full second
* @j: the time in (relative) jiffies that should be rounded
* @cpu: the processor number on which the timeout will happen
*
* __round_jiffies_relative() rounds a time delta in the future (in jiffies)
* up or down to (approximately) full seconds. This is useful for timers
* for which the exact time they fire does not matter too much, as long as
* they fire approximately every X seconds.
*
* By rounding these timers to whole seconds, all such timers will fire
* at the same time, rather than at various times spread out. The goal
* of this is to have the CPU wake up less, which saves power.
*
* The exact rounding is skewed for each processor to avoid all
* processors firing at the exact same time, which could lead
* to lock contention or spurious cache line bouncing.
*
* The return value is the rounded version of the @j parameter.
*/
unsigned long __round_jiffies_relative(unsigned long j, int cpu)
{
unsigned long j0 = jiffies;
Annotation
- Immediate include surface: `linux/kernel_stat.h`, `linux/export.h`, `linux/interrupt.h`, `linux/percpu.h`, `linux/init.h`, `linux/mm.h`, `linux/swap.h`, `linux/pid_namespace.h`.
- Detected declarations: `struct timer_base`, `struct timer_hint`, `function timers_update_migration`, `function timer_migration_handler`, `function timer_sysctl_init`, `function timers_update_migration`, `function timers_update_nohz`, `function is_timers_nohz_active`, `function is_timers_nohz_active`, `function round_jiffies_common`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.