之前看了entity_load和cfs_rq的负载的计算,他们的核心code都是一样的,至于具体差别后面有时间再去看,现在来看看cpu级别的负载的计算
cpu_load_update_active
cpu级别的负载的计算主要是涉及到一个数组
/*
* Called from scheduler_tick()
*/
void cpu_load_update_active(struct rq *this_rq)
{
unsigned long load = weighted_cpuload(this_rq);
//所有cpu的周期性的时钟已经被停掉了
if (tick_nohz_tick_stopped())
cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load);
else
cpu_load_update_periodic(this_rq, load);//periodic周期性的
}
weighted_cpuload(this_rq);实际上获取的是
cfs_rq->runnable_load_avg
关于kernel的no HZ模式:
负载均衡的触发条件之一是软中断。每个cpu上都设置了一个周期性的时钟中断,随着cpu性能的提升,时钟中断的频率可以上升到1000HZ,也就是每秒能处理1000个中断。
时钟中断的设计本身是存在bug的,即他都会周期性的去执行,如果处理器一直处于空闲状态,周期性的唤醒是不必要的,由此kernel引入了NOHZ模式,即关闭所有cpu的这个周期时钟中断。
cpu_load_update_periodic
周期性的去更新cpu load值
static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
{
#ifdef CONFIG_NO_HZ_COMMON
/* See the mess around cpu_load_update_nohz(). */
this_rq->last_load_update_tick = READ_ONCE(jiffies);
#endif
cpu_load_update(this_rq, load, 1);
}
/**
* __cpu_load_update - update the rq->cpu_load[] statistics
* @this_rq: The rq to update statistics for
* @this_load: The current load
* @pending_updates: The number of missed upda