kernel/sched/clock.c
Source file repositories/reference/linux-study-clean/kernel/sched/clock.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/sched/clock.c- Extension
.c- Size
- 13155 bytes
- Lines
- 512
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/sched/clock.hsched.h
Detected Declarations
struct sched_clock_datafunction sched_clockfunction sched_clock_stablefunction __scd_stampfunction __set_sched_clock_stablefunction __sched_clock_workfunction __clear_sched_clock_stablefunction clear_sched_clock_stablefunction __sched_clock_gtod_offsetfunction sched_clock_initfunction late_initcallfunction wrap_minfunction wrap_maxfunction sched_clock_localfunction local_clock_noinstrfunction local_clockfunction sched_clock_remotefunction cpu_clockfunction sched_clock_tickfunction sched_clock_tick_stablefunction sched_clock_idle_sleep_eventfunction sched_clock_idle_wakeup_eventfunction sched_clock_initfunction sched_clock_cpufunction hypervisorexport sched_clockexport local_clockexport sched_clock_cpuexport sched_clock_idle_sleep_eventexport sched_clock_idle_wakeup_event
Annotated Snippet
struct sched_clock_data {
u64 tick_raw;
u64 tick_gtod;
u64 clock;
};
static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
static __always_inline struct sched_clock_data *this_scd(void)
{
return this_cpu_ptr(&sched_clock_data);
}
notrace static inline struct sched_clock_data *cpu_sdc(int cpu)
{
return &per_cpu(sched_clock_data, cpu);
}
notrace int sched_clock_stable(void)
{
return static_branch_likely(&__sched_clock_stable);
}
notrace static void __scd_stamp(struct sched_clock_data *scd)
{
scd->tick_gtod = ktime_get_ns();
scd->tick_raw = sched_clock();
}
notrace static void __set_sched_clock_stable(void)
{
struct sched_clock_data *scd;
/*
* Since we're still unstable and the tick is already running, we have
* to disable IRQs in order to get a consistent scd->tick* reading.
*/
local_irq_disable();
scd = this_scd();
/*
* Attempt to make the (initial) unstable->stable transition continuous.
*/
__sched_clock_offset = (scd->tick_gtod + __gtod_offset) - (scd->tick_raw);
local_irq_enable();
printk(KERN_INFO "sched_clock: Marking stable (%lld, %lld)->(%lld, %lld)\n",
scd->tick_gtod, __gtod_offset,
scd->tick_raw, __sched_clock_offset);
static_branch_enable(&__sched_clock_stable);
tick_dep_clear(TICK_DEP_BIT_CLOCK_UNSTABLE);
}
/*
* If we ever get here, we're screwed, because we found out -- typically after
* the fact -- that TSC wasn't good. This means all our clocksources (including
* ktime) could have reported wrong values.
*
* What we do here is an attempt to fix up and continue sort of where we left
* off in a coherent manner.
*
* The only way to fully avoid random clock jumps is to boot with:
* "tsc=unstable".
*/
notrace static void __sched_clock_work(struct work_struct *work)
{
struct sched_clock_data *scd;
int cpu;
/* take a current timestamp and set 'now' */
preempt_disable();
scd = this_scd();
__scd_stamp(scd);
scd->clock = scd->tick_gtod + __gtod_offset;
preempt_enable();
/* clone to all CPUs */
for_each_possible_cpu(cpu)
per_cpu(sched_clock_data, cpu) = *scd;
printk(KERN_WARNING "TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'.\n");
printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
scd->tick_gtod, __gtod_offset,
scd->tick_raw, __sched_clock_offset);
disable_sched_clock_irqtime();
static_branch_disable(&__sched_clock_stable);
}
static DECLARE_WORK(sched_clock_work, __sched_clock_work);
Annotation
- Immediate include surface: `linux/sched/clock.h`, `sched.h`.
- Detected declarations: `struct sched_clock_data`, `function sched_clock`, `function sched_clock_stable`, `function __scd_stamp`, `function __set_sched_clock_stable`, `function __sched_clock_work`, `function __clear_sched_clock_stable`, `function clear_sched_clock_stable`, `function __sched_clock_gtod_offset`, `function sched_clock_init`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: integration implementation candidate.
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.