kernel/time/posix-cpu-timers.c
Source file repositories/reference/linux-study-clean/kernel/time/posix-cpu-timers.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/time/posix-cpu-timers.c- Extension
.c- Size
- 47349 bytes
- Lines
- 1674
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: implementation source
- Status
- source 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.
- 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/signal.hlinux/sched/cputime.hlinux/posix-timers.hlinux/errno.hlinux/math64.hlinux/uaccess.hlinux/kernel_stat.htrace/events/timer.hlinux/tick.hlinux/workqueue.hlinux/compat.hlinux/sched/deadline.hlinux/task_work.hposix-timers.h
Detected Declarations
function posix_cputimers_group_initfunction update_rlimit_cpufunction validate_clock_permissionsfunction clock_pid_typefunction bump_cpu_timerfunction expiry_cache_is_inactivefunction posix_cpu_clock_getresfunction posix_cpu_clock_setfunction cpu_clock_samplefunction store_samplesfunction task_sample_cputimefunction proc_sample_cputime_atomicfunction __update_gt_cputimefunction update_gt_cputimefunction sys_getitimerfunction thread_group_start_cputimefunction __thread_group_cputimefunction processfunction posix_cpu_clock_getfunction sys_timer_createfunction trigger_base_recalc_expiresfunction disarm_timerfunction posix_cpu_timer_delfunction cleanup_timerqueuefunction cleanup_timersfunction posix_cpu_timers_exitfunction posix_cpu_timers_exit_groupfunction arm_timerfunction cpu_timer_firefunction posix_cpu_timer_setfunction __posix_cpu_timer_getfunction posix_cpu_timer_getfunction collect_timerqueuefunction collect_posix_cputimersfunction check_dl_overrunfunction check_rlimitfunction check_thread_timersfunction stop_process_timersfunction check_cpu_itimerfunction check_process_timersfunction codefunction task_cputimers_expiredfunction fastpath_timer_checkfunction posix_cpu_timers_workfunction posix_cpu_timer_wait_runningfunction posix_cpu_timer_wait_running_nsleepfunction clear_posix_cputimers_workfunction posix_cputimers_init_work
Annotated Snippet
if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
/*
* If sched_clock is using a cycle counter, we
* don't have any idea of its true resolution
* exported, but it is much more than 1s/HZ.
*/
tp->tv_nsec = 1;
}
}
return error;
}
static int
posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp)
{
int error = validate_clock_permissions(clock);
/*
* You can never reset a CPU clock, but we check for other errors
* in the call before failing with EPERM.
*/
return error ? : -EPERM;
}
/*
* Sample a per-thread clock for the given task. clkid is validated.
*/
static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p)
{
u64 utime, stime;
if (clkid == CPUCLOCK_SCHED)
return task_sched_runtime(p);
task_cputime(p, &utime, &stime);
switch (clkid) {
case CPUCLOCK_PROF:
return utime + stime;
case CPUCLOCK_VIRT:
return utime;
default:
WARN_ON_ONCE(1);
}
return 0;
}
static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime)
{
samples[CPUCLOCK_PROF] = stime + utime;
samples[CPUCLOCK_VIRT] = utime;
samples[CPUCLOCK_SCHED] = rtime;
}
static void task_sample_cputime(struct task_struct *p, u64 *samples)
{
u64 stime, utime;
task_cputime(p, &utime, &stime);
store_samples(samples, stime, utime, p->se.sum_exec_runtime);
}
static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
u64 *samples)
{
u64 stime, utime, rtime;
utime = atomic64_read(&at->utime);
stime = atomic64_read(&at->stime);
rtime = atomic64_read(&at->sum_exec_runtime);
store_samples(samples, stime, utime, rtime);
}
/*
* Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
* to avoid race conditions with concurrent updates to cputime.
*/
static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
{
u64 curr_cputime = atomic64_read(cputime);
do {
if (sum_cputime <= curr_cputime)
return;
} while (!atomic64_try_cmpxchg(cputime, &curr_cputime, sum_cputime));
}
static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic,
struct task_cputime *sum)
{
Annotation
- Immediate include surface: `linux/sched/signal.h`, `linux/sched/cputime.h`, `linux/posix-timers.h`, `linux/errno.h`, `linux/math64.h`, `linux/uaccess.h`, `linux/kernel_stat.h`, `trace/events/timer.h`.
- Detected declarations: `function posix_cputimers_group_init`, `function update_rlimit_cpu`, `function validate_clock_permissions`, `function clock_pid_type`, `function bump_cpu_timer`, `function expiry_cache_is_inactive`, `function posix_cpu_clock_getres`, `function posix_cpu_clock_set`, `function cpu_clock_sample`, `function store_samples`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: source 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.