kernel/sched/cputime.c
Source file repositories/reference/linux-study-clean/kernel/sched/cputime.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/sched/cputime.c- Extension
.c- Size
- 33065 bytes
- Lines
- 1303
- 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/sched/clock.hlinux/sched/cputime.hlinux/tsacct_kern.hsched.hasm/cputime.h
Detected Declarations
function enable_sched_clock_irqtimefunction disable_sched_clock_irqtimefunction irqtime_account_deltafunction irqtime_account_irqfunction irqtime_tick_accountedfunction irqtime_tick_accountedfunction task_group_account_fieldfunction account_user_timefunction account_guest_timefunction account_system_index_timefunction account_system_timefunction account_steal_timefunction account_idle_timefunction __account_forceidle_timefunction native_steal_clockfunction steal_account_process_timefunction account_other_timefunction read_sum_exec_runtimefunction read_sum_exec_runtimefunction tasksfunction __for_each_threadfunction irqtime_account_process_tickfunction irqtime_account_process_tickfunction kcpustat_idle_startfunction kcpustat_dyntick_stopfunction kcpustat_dyntick_startfunction kcpustat_irq_enterfunction kcpustat_irq_exitfunction kcpustat_field_dyntickfunction kcpustat_field_idlefunction kcpustat_field_iowaitfunction kcpustat_field_dyntickfunction get_cpu_sleep_time_usfunction timefunction timefunction vtime_account_irqfunction cputime_adjustfunction task_cputime_adjustedfunction thread_group_cputime_adjustedfunction account_process_tickfunction cputime_adjustfunction task_cputime_adjustedfunction thread_group_cputime_adjustedfunction vtime_deltafunction get_vtime_deltafunction vtime_account_systemfunction vtime_account_guestfunction __vtime_account_kernel
Annotated Snippet
__for_each_thread(sig, t) {
task_cputime(t, &utime, &stime);
times->utime += utime;
times->stime += stime;
times->sum_exec_runtime += read_sum_exec_runtime(t);
}
}
}
#ifdef CONFIG_IRQ_TIME_ACCOUNTING
/*
* Account a tick to a process and cpustat
* @p: the process that the CPU time gets accounted to
* @user_tick: is the tick from userspace
* @rq: the pointer to rq
*
* Tick demultiplexing follows the order
* - pending hardirq update
* - pending softirq update
* - user_time
* - idle_time
* - system time
* - check for guest_time
* - else account as system_time
*
* Check for hardirq is done both for system and user time as there is
* no timer going off while we are on hardirq and hence we may never get an
* opportunity to update it solely in system time.
* p->stime and friends are only updated on system time and not on IRQ
* softirq as those do not count in task exec_runtime any more.
*/
static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
int ticks)
{
u64 other, cputime = TICK_NSEC * ticks;
/*
* When returning from idle, many ticks can get accounted at
* once, including some ticks of steal, IRQ, and softirq time.
* Subtract those ticks from the amount of time accounted to
* idle, or potentially user or system time. Due to rounding,
* other time can exceed ticks occasionally.
*/
other = account_other_time(ULONG_MAX);
if (other >= cputime)
return;
cputime -= other;
if (this_cpu_ksoftirqd() == p) {
/*
* ksoftirqd time do not get accounted in cpu_softirq_time.
* So, we have to handle it separately here.
* Also, p->stime needs to be updated for ksoftirqd.
*/
account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
} else if (user_tick) {
account_user_time(p, cputime);
} else if (p == this_rq()->idle) {
account_idle_time(cputime);
} else if (p->flags & PF_VCPU) { /* System time or guest time */
account_guest_time(p, cputime);
} else {
account_system_index_time(p, cputime, CPUTIME_SYSTEM);
}
}
#else /* !CONFIG_IRQ_TIME_ACCOUNTING: */
static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
int nr_ticks) { }
#endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
#if defined(CONFIG_NO_HZ_COMMON) && !defined(CONFIG_HAVE_VIRT_CPU_ACCOUNTING_IDLE)
static void kcpustat_idle_stop(struct kernel_cpustat *kc, u64 now)
{
u64 *cpustat = kc->cpustat;
u64 delta, steal, steal_delta;
int iowait;
if (!kc->idle_elapse)
return;
iowait = nr_iowait_cpu(smp_processor_id()) > 0;
delta = now - kc->idle_entrytime;
steal = steal_account_process_time(delta);
/*
* Record the idle time after substracting the steal time from
* previous update sequence. Don't substract the steal time from
* the current update sequence to avoid readers moving backward.
Annotation
- Immediate include surface: `linux/sched/clock.h`, `linux/sched/cputime.h`, `linux/tsacct_kern.h`, `sched.h`, `asm/cputime.h`.
- Detected declarations: `function enable_sched_clock_irqtime`, `function disable_sched_clock_irqtime`, `function irqtime_account_delta`, `function irqtime_account_irq`, `function irqtime_tick_accounted`, `function irqtime_tick_accounted`, `function task_group_account_field`, `function account_user_time`, `function account_guest_time`, `function account_system_index_time`.
- 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.