Linux CPU Time Guardians: Understanding cputime, cpufreq_stats, cpufreq_times, and cpuidle_time
The article explains Linux kernel CPU‑time accounting modules—cputime, cpufreq_stats, cpufreq_times, and cpuidle_time—detailing how each records processor usage, frequency transitions, per‑process frequency data, and idle‑state durations, and why they are essential for performance analysis and power‑optimization by system engineers.
This article provides a comprehensive overview of Linux kernel CPU time management modules, essential knowledge for Linux system engineers to understand program execution efficiency.
1. cputime - The Global Overview Module
The cputime module records CPU time in various states across all processors. By reading /proc/stat, users can see CPU usage categories. The kernel defines time types in include/linux/kernel_stat.h, with values corresponding to different CPU states. The familiar top tool calculates CPU utilization based on cputime data.
2. cpufreq_stats - Power Analysis Helper
Enabling CONFIG_CPU_FREQ_STAT宏 creates a stats directory under cpufreq driver sysfs. The time_in_state node shows time spent at each frequency within a cpufreq policy, measured in jiffies. This enables identification of the most frequently used CPU frequencies for power optimization.
3. cpufreq_times - Per-Process Frequency Tracking
Presented via /proc/[pid]/time_in_state, this module tracks time each process (thread) spends at various frequencies across cpufreq policies. Since each process spawn creates a new /proc/[pid] directory, this is the most numerous member of the CPU time family.
4. cpuidle_time - CPU Sleep Quality Recorder
This module records how long each CPU spends in each C-state (idle state). Displayed via cpuidle driver sysfs time node in microseconds, it shows the sleep quality of each CPU core.
Working Principles
cputime: Located in kernel/sched/cputime.c. On each timer interrupt, the kernel calls irqtime_account_process_process_tick() (requires CONFIG_IRQ_TIME_ACCOUNTING). Based on the current task type (softirq/user tick/idle/guest/kernel), the CPU time (typically 1 tick) is added to the corresponding category.
cpufreq_times: Located in drivers/cpufreq/cpufreq_times.c. When cpufreq policy frequency changes, cpufreq driver notifies cpufreq_times via cpufreq_notify_transition() or cpufreq_driver_fast_switch(). When cputime receives timer interrupts, it calls cpufreq_acct_update_power() to add ticks to the current task and frequency statistics.
cpufreq_stats: Located in drivers/cpufreq/cpufreq_stats.c. Similar to cpufreq_times but only involves cpufreq driver. When frequency changes, it calls cpufreq_stats_record_transition() to record the transition. The cpufreq_state module calculates time difference between current and previous jiffies, adding the delta to the previous frequency's time statistics.
cpuidle_time: Located in drivers/cpuidle/cpuidle.c. When no runnable tasks exist on a CPU runqueue, the scheduler runs the idle process. Through层层调用, it reaches cpuidle_enter_state(). Using local_clock() to record timestamps before and after entering idle (calling target_state->enter() callback), the difference is saved to the time member of cpuidle_state_usage structure.
Understanding these time accounting modules is crucial for system performance and power optimization.
OPPO Kernel Craftsman
Sharing Linux kernel-related cutting-edge technology, technical articles, technical news, and curated tutorials
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.