|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
Linux动态频率调节系统CPUFreq之系统负载的检测8 M* F3 `4 G+ [; G! l
Q% P- m' ]7 z! @! U上一节我们提到,核心层启动一个governor后,会在每个使用该governor的cpu上建立一个工作队列,工作队列的执行函数是在common_dbs_data中gov_dbs_timer字段所指向的函数,理所当然,该函数由各个governor的具体代码来实现,对于ondemand governor,它的实现函数是od_dbs_timer。governor的公共层代码为我们提供了一个API:dbs_check_cpu,该API用来计算两个统计周期期间某个cpu的负载情况,我们先分析一下dbs_check_cpu:
0 T) l d/ E7 H [: b# s5 a {( s3 C. V
- void dbs_check_cpu(struct dbs_data *dbs_data, int cpu)
- {
- struct cpu_dbs_common_info *cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
- ......
- policy = cdbs->cur_policy;
- /* Get Absolute Load (in terms of freq for ondemand gov) */
- for_each_cpu(j, policy->cpus) {
- struct cpu_dbs_common_info *j_cdbs;
- ......
- j_cdbs = dbs_data->cdata->get_cpu_cdbs(j);
- ......
- cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
- wall_time = (unsigned int)
- (cur_wall_time - j_cdbs->prev_cpu_wall);
- j_cdbs->prev_cpu_wall = cur_wall_time;
- idle_time = (unsigned int)
- (cur_idle_time - j_cdbs->prev_cpu_idle);
- j_cdbs->prev_cpu_idle = cur_idle_time;
- ......
- load = 100 * (wall_time - idle_time) / wall_time;
- ......
- load *= cur_freq; /* 实际的代码不是这样,为了简化讨论,精简为实际的计算逻辑*/
- if (load > max_load)
- max_load = load;
- }
- dbs_data->cdata->gov_check_cpu(cpu, max_load);
- }0 a2 r1 `8 k2 ?# c, \; {/ q
: p. u, y) |) x
* | M5 ~5 a, ?% [( y- A. t7 o由代码可以看出,遍历该policy下每个online的cpu,取出该cpu对应的cpu_dbs_common_info结构,该结构中的prev_cpu_idle和prev_cpu_wall保存有上一次采样周期时记录的idle时间和运行时间,负载的计算其实很简单:
% R# J1 \( @ O! _) q" u7 n- idle_time = 本次idle时间 - 上次idle时间;
- wall_time = 本次总运行时间 - 上次总运行时间;
- 负载load = 100 * (wall_time - idle_time)/ wall_time;
- 把所有cpu中,负载最大值记入max_load中,作为选择频率的依据;9 D0 R- a; p4 c1 B$ I3 S
7 z' }& {5 u# B- [3 P6 G2 Z3 K! [
, `& ^ _+ U5 U/ l( a' |1 P
计算出最大负载max_load后,调用具体governor实现的gov_check_cpu回调函数,对于ondemand来说,该回调函数是:od_check_cpu,我们跟进去看看:2 f Y9 R$ g9 t/ j1 ^ N, q
- static void od_check_cpu(int cpu, unsigned int load_freq)
- {
- struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
- struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
- struct dbs_data *dbs_data = policy->governor_data;
- struct od_dbs_tuners *od_tuners = dbs_data->tuners;
- dbs_info->freq_lo = 0;
- /* Check for frequency increase */
- if (load_freq > od_tuners->up_threshold * policy->cur) {
- /* If switching to max speed, apply sampling_down_factor */
- if (policy->cur < policy->max)
- dbs_info->rate_mult =
- od_tuners->sampling_down_factor;
- dbs_freq_increase(policy, policy->max);
- return;
- }1 i! K0 _4 h) b3 B7 |2 T
0 a+ I5 `" Z; v2 _1 e" }7 X$ ^& X* Q
* t$ Q9 _7 {4 s' [- {( d& t8 E: S u) o& L. ^; r1 V+ ^; ^
当负载比预设的阀值高时(od_tuners->up_threshold,默认值是95%),立刻选择该policy最大的工作频率作为接下来的工作频率。如果负载没有达到预设的阀值,但是当前频率已经是最低频率了,则什么都不做,直接返回:- f: @% a Q4 ]: n
- if (policy->cur == policy->min)
- return;, H" z& {3 G. i' X( U
! i, B% I( I) Z7 U; @1 F# w5 a# d; ]( H. L# d
1 Q; ]4 P" z3 n: Q- I6 ?
* ~! ], r* x6 M运行到这里,cpu的频率可能已经在上面的过程中被设置为最大频率,实际上我们可能并不需要这么高的频率,所以接着判断,当负载低于另一个预设值时,这时需要计算一个合适于该负载的新频率:& y: W( m$ I* }* g( G- I
+ r% \ w1 f8 w6 m) }& A) c+ ~
- if (load_freq < od_tuners->adj_up_threshold
- * policy->cur) {
- unsigned int freq_next;
- freq_next = load_freq / od_tuners->adj_up_threshold;
- /* No longer fully busy, reset rate_mult */
- dbs_info->rate_mult = 1;
- if (freq_next < policy->min)
- freq_next = policy->min;
- if (!od_tuners->powersave_bias) {
- __cpufreq_driver_target(policy, freq_next,
- CPUFREQ_RELATION_L);
- return;
- }
- freq_next = od_ops.powersave_bias_target(policy, freq_next,
- CPUFREQ_RELATION_L);
- __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
- }
- } I$ F* `$ K9 H7 O; H
. [8 {, M L8 M3 Z6 Y. U9 l+ j) O
8 ~/ P% q3 k) }5 R3 ~6 ^5 j) ?! k) u7 k
& {) `& w4 t" e/ a( o$ e D
对于ondemand来说,因为传入的负载是乘上了当前频率后的归一化值,所以计算新频率时,直接用load_freq除以想要的负载即可。本来计算出来的频率直接通过__cpufreq_driver_target函数,交给cpufreq_driver调节频率即可,但是这里的处理考虑了powersave_bias的设置情况,当设置了powersave_bias时,表明我们为了进一步节省电力,我们希望在计算出来的新频率的基础上,再乘以一个powersave_bias设定的百分比,作为真正的运行频率,powersave_bias的值从0-1000,每一步代表0.1%。实际的情况比想象中稍微复杂一点,考虑到乘以一个powersave_bias后的新频率可能不在cpu所支持的频率表中,ondemand算法会在频率表中查找,分别找出最接近新频率的一个区间,由高低两个频率组成,低的频率记入od_cpu_dbs_info_s结构的freq_lo字段中,高的频率通过od_ops.powersave_bias_target回调返回。同时,od_ops.powersave_bias_target回调函数还计算出高低两个频率应该运行的时间,分别记入od_cpu_dbs_info_s结构的freq_hi_jiffies和freq_low_jiffies字段中。原则是,通过两个不同频率的运行时间的组合,使得综合结果接近我们想要的目标频率。详细的计算逻辑请参考函数:generic_powersave_bias_target。
' g5 D/ M$ {8 |8 x5 h' [, y2 H# Q9 F/ E9 s0 B& t2 ]1 l
讨论完上面两个函数,让我们回到本节的开头,负载的计算工作是在一个工作队列中发起的,前面说过,ondemand对应的工作队列的工作函数是od_dbs_timer,我们看看他的实现代码:, w8 f& M* z9 s
! x# }3 _( F; u6 i# {/ s# `
- static void od_dbs_timer(struct work_struct *work)
- {
- ......
- /* Common NORMAL_SAMPLE setup */
- core_dbs_info->sample_type = OD_NORMAL_SAMPLE;
- if (sample_type == OD_SUB_SAMPLE) {
- delay = core_dbs_info->freq_lo_jiffies;
- __cpufreq_driver_target(core_dbs_info->cdbs.cur_policy,
- core_dbs_info->freq_lo, CPUFREQ_RELATION_H);
- } else {
- dbs_check_cpu(dbs_data, cpu);
- if (core_dbs_info->freq_lo) {
- /* Setup timer for SUB_SAMPLE */
- core_dbs_info->sample_type = OD_SUB_SAMPLE;
- delay = core_dbs_info->freq_hi_jiffies;
- }
- }
- max_delay:
- if (!delay)
- delay = delay_for_sampling_rate(od_tuners->sampling_rate
- * core_dbs_info->rate_mult);
- gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy, delay, modify_all);
- mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
- }5 B% P4 l/ c% A# H& x
# }7 S; A8 v: K" x2 B
+ ?* X) o, r/ J1 X9 j- K" V
+ N( Q) [5 O3 d
1 y9 U; a h4 \2 M: s0 k8 y# D0 Y如果sample_type是OD_SUB_SAMPLE时,表明上一次采样时,需要用高低两个频率来模拟实际的目标频率中的第二步:需要运行freq_lo,并且持续时间为freq_lo_jiffies。否则,调用公共层计算负载的API:dbs_check_cpu,开始一次新的采样,当powersave_bias没有设置时,该函数返回前,所需要的新的目标频率会被设置,考虑到powersave_bias的设置情况,判断一下如果freq_lo被设置,说明需要用高低两个频率来模拟实际的目标频率,高频率已经在dbs_check_cpu返回前被设置(实际的设置工作是在od_check_cpu中),所以把sample_type设置为OD_SUB_SAMPLE,以便下一次运行工作函数进行采样时可以设置低频率运行。最后,调度工作队列在下一个采样时刻再次运行,这样,cpu的工作频率实现了在每个采样周期,根据实际的负载情况,动态地设定合适的工作频率进行运行,既满足了性能的需求,也降低了系统的功耗,达到了cpufreq系统的最终目的,整个流程可以参考下图:
& y; |% A8 w7 _( M" G) U! j
2 |- I. p- Y- l9 U; C/ i+ g
图 1 负载计算和频率选择6 m( Q- h# t+ k9 w, k) d
6 ?* h" y- ]# t( R1 Y- G3 q; _6 G5 i$ R, Z
|
|