一、负载
1.负载查看过程
top命令的负载值是从proc/loadavg伪文件夹里,可以通过trace命令跟踪top。伪文件proc/loadavg定义在/fs/proc/loadavg.c文件中,该文件创建proc/loadavg,并定义操作函数loadavg_proc_show。
static int __init proc_loadavg_init(void)
{
proc_create_single("loadavg", 0, NULL, loadavg_proc_show);
return 0;
}
当打开proc/loadavg时,会调用loadavg_proc_show函数进行处理,负载的计算是在这里完成的。
static int loadavg_proc_show(struct seq_file *m, void *v)
{
unsigned long avnrun[3];
get_avenrun(avnrun, FIXED_1/200, 0);
seq_printf(m, "%lu.%02lu %lu.%02lu %lu.%02lu %u/%d %d\n",
LOAD_INT(avnrun[0]), LOAD_FRAC(avnrun[0]),
LOAD_INT(avnrun[1]), LOAD_FRAC(avnrun[1]),
LOAD_INT(avnrun[2]), LOAD_FRAC(avnrun[2]),
nr_running(), nr_threads,
idr_get_cursor(&task_active_pid_ns(current)->idr) - 1);
return 0;
}
- 调用
get_avenrun读取当前负载值,get_avenrun是在访问avnrun这个全局数组。
void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
{
loads[0] = (avenrun[0] + offset) << shift;
loads[1] = (avenrun[1] + offset) << shift;
loads[2] = (avenrun[2] + offset) << shift;
}
2.内核负载计算过程
2.1 PerCPU定期汇总负载
高分辨率定时器定时将每个CPU上的负载数据汇总到全局瞬时负载变量calc_load_tasks中。
void tick_setup_sched_timer(void)
{
struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
ktime_t now = ktime_get();
/*
* Emulate tick processing via per-CPU hrtimers:
*/
hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
ts->sched_timer.function = tick_sched_timer;
...
}
- 定时器到期函数设置成
tick_sched_timer,通过这个函数让每个CPU都周期性的执行一些任务。 tick_sched_timer调用scheduler_tick,scheduler_tick会将当前负载值刷新到calc_load_tasks中。
void scheduler_tick(void)
{
int cpu = smp_processor_id();
struct rq *rq = cpu_rq(cpu);
...
calc_global_load_tick(rq);
...
}
scheduler_tick获取当前CPU及对应的运行队列rq,调用calc_global_load_tick将当前CPU负载刷新到全局数组。
void calc_global_load_tick(struct rq *this_rq)
{
long delta;
if (time_before(jiffies, this_rq->calc_load_update))
return;
delta = calc_load_fold_active(this_rq, 0);
if (delta)
atomic_long_add(delta, &calc_load_tasks);
this_rq->calc_load_update += LOAD_FREQ;
}
通过calc_load_fold_active获取当前运行队列相对负载值,并用atomic_long_add添加到全局瞬时负载值calc_load_tasks上。
long calc_load_fold_active(struct rq *this_rq, long adjust)
{
long nr_active, delta = 0;
nr_active = this_rq->nr_running - adjust;
nr_active += (int)this_rq->nr_uninterruptible;
if (nr_active != this_rq->calc_load_active) {
delta = nr_active - this_rq->calc_load_active;
this_rq->calc_load_active = nr_active;
}
return delta;
}
计算了nr_running和nr_uninterruptible两种状态进程的数量,也就是用户空间中R和D两种状态的task数。
- R:
TASK_RUNNING,可执行状态。 - D:
TASK_UNINTERRUPTIBLE,不可中断的睡眠状态。
calc_load_tasks是长期存在的数据,在刷新进程数时,只刷新变化的量。
2.2 定时计算系统平均负载
在Linux中使用指数加权移动平均的平均数计算法。这种方法在实际计算的时候只需上一个时间的平均数即可,不需要保存所有瞬时负载。越靠近现在的时间点,权重越高。
每次时钟节拍到来会调用do_timer函数。
void do_timer(unsigned long ticks)
{
jiffies_64 += ticks;
calc_global_load();
}
calc_global_load是平均负载计算的核心,获取当前瞬时负载calc_load_tasks,保存在avenrun中。
void calc_global_load(void)
{
...
active = atomic_long_read(&calc_load_tasks);
active = active > 0 ? active * FIXED_1 : 0;
avenrun[0] = calc_load(avenrun[0], EXP_1, active);
avenrun[1] = calc_load(avenrun[1], EXP_5, active);
avenrun[2] = calc_load(avenrun[2], EXP_15, active);
...
}
calc_load采用指数加权移动平均来计算过去1、5、15分钟的平均负载。
/*
* a1 = a0 * e + a * (1 - e)
*/
static inline unsigned long
calc_load(unsigned long load, unsigned long exp, unsigned long active)
{
unsigned long newload;
newload = load * exp + active * (FIXED_1 - exp);
if (active >= load)
newload += FIXED_1-1;
return newload / FIXED_1;
}
二、CPU利用率
1.top命令使用的数据
计算CPU利用率用的是/proc/stat伪文件。
top命令访问/proc/stat获取各项CPU使用数据。- 通过
stat_open处理对/proc/stat的访问。 - 内核访问数据来自
kernel_cpustat数组。
/proc/stat文件处理函数是stat_proc_ops。
static int __init proc_stat_init(void)
{
proc_create("stat", 0, NULL, &stat_proc_ops);
return 0;
}
static const struct proc_ops stat_proc_ops = {
.proc_flags = PROC_ENTRY_PERMANENT,
.proc_open = stat_open,
.proc_read_iter = seq_read_iter,
.proc_lseek = seq_lseek,
.proc_release = single_release,
};
stat_open依次调用single_open_size、show_stat来输出数据内容。
static int show_stat(struct seq_file *p, void *v)
{
int i, j;
u64 user, nice, system, idle, iowait, irq, softirq, steal;
...
for_each_possible_cpu(i) {
struct kernel_cpustat kcpustat;
u64 *cpustat = kcpustat.cpustat;
kcpustat_cpu_fetch(&kcpustat, i);
user += cpustat[CPUTIME_USER];
nice += cpustat[CPUTIME_NICE];
system += cpustat[CPUTIME_SYSTEM];
idle += get_idle_time(&kcpustat, i);
iowait += get_iowait_time(&kcpustat, i);
irq += cpustat[CPUTIME_IRQ];
softirq += cpustat[CPUTIME_SOFTIRQ];
steal += cpustat[CPUTIME_STEAL];
guest += cpustat[CPUTIME_GUEST];
guest_nice += cpustat[CPUTIME_GUEST_NICE];
sum += kstat_cpu_irqs_sum(i);
sum += arch_irq_stat_cpu(i);
...
}
sum += arch_irq_stat();
seq_put_decimal_ull(p, "cpu ", nsec_to_clock_t(user));
seq_put_decimal_ull(p, " ", nsec_to_clock_t(nice));
seq_put_decimal_ull(p, " ", nsec_to_clock_t(system));
seq_put_decimal_ull(p, " ", nsec_to_clock_t(idle));
seq_put_decimal_ull(p, " ", nsec_to_clock_t(iowait));
seq_put_decimal_ull(p, " ", nsec_to_clock_t(irq));
seq_put_decimal_ull(p, " ", nsec_to_clock_t(softirq));
seq_put_decimal_ull(p, " ", nsec_to_clock_t(steal));
seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest));
seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest_nice));
seq_putc(p, '\n');
...
}
for_each_possible_cpu通过kcpustat_cpu_fetch函数读取了kernel_cpustat全局变量里的值。包括user、nice、system、idle、iowait、irq等。
DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
#define kcpustat_cpu(cpu) per_cpu(kernel_cpustat, cpu)
static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
{
*dst = kcpustat_cpu(cpu);
}
for_each_possible_cpu将每一个核的利用率加起来,通过seq_put_decimal_ull将数据输出。
2.统计数据的来源
一个节拍长度时间是通过CONFIG_HZ定义的。每当时间中断到来,都调用update_process_times更新系统时间,更新的时间存储在kernel_cpustat中。
void update_process_times(int user_tick)
{
struct task_struct *p = current;
PRANDOM_ADD_NOISE(jiffies, user_tick, p, 0);
/* Note: this timer irq context must be accounted for as well. */
account_process_tick(p, user_tick);
...
}
user_tick指采样在用户态,调用account_process_tick函数。
void account_process_tick(struct task_struct *p, int user_tick)
{
...
cputime = TICK_NSEC;
...
if (user_tick)
account_user_time(p, cputime);
else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
account_system_time(p, HARDIRQ_OFFSET, cputime);
else
account_idle_time(cputime);
}
account_user_time、account_system_time、account_idle_time来统计用户态、内核态和空闲时间。
2.1 用户态时间统计
void account_user_time(struct task_struct *p, u64 cputime)
{
int index;
/* Add user time to process. */
p->utime += cputime;
account_group_user_time(p, cputime);
index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
/* Add user time to cpustat. */
task_group_account_field(p, index, cputime);
/* Account for user time used */
acct_account_cputime(p);
}
- 如果进程
nice值大于0,那么增加到CPU统计结构的nice字段中。 - 如果进程
nice值小于等于0,那么增加到CPU统计结构的user字段中。
调用task_group_account_field把时间加到kernel_cpustat中。
static inline void task_group_account_field(struct task_struct *p, int index,
u64 tmp)
{
__this_cpu_add(kernel_cpustat.cpustat[index], tmp);
cgroup_account_cputime_field(p, index, tmp);
}
2.2 内核态时间统计
void account_system_time(struct task_struct *p, int hardirq_offset, u64 cputime)
{
...
if (hardirq_count() - hardirq_offset)
index = CPUTIME_IRQ;
else if (in_serving_softirq())
index = CPUTIME_SOFTIRQ;
else
index = CPUTIME_SYSTEM;
account_system_index_time(p, cputime, index);
}
- 处于硬中断执行上下文,那么统计到
irq字段。 - 处于软中断执行上下文,那么统计到
softirq字段。 - 其他统计到
system中。
之后调用account_system_index_time、task_group_account_field,把时间加到kernel_cpustat中。
2.3 空闲时间累计
void account_idle_time(u64 cputime)
{
u64 *cpustat = kcpustat_this_cpu->cpustat;
struct rq *rq = this_rq();
if (atomic_read(&rq->nr_iowait) > 0)
cpustat[CPUTIME_IOWAIT] += cputime;
else
cpustat[CPUTIME_IDLE] += cputime;
}
309




被折叠的 条评论
为什么被折叠?



