【WALT】update_history() 代码详解

发布时间 2024-01-06 18:07:50作者: Cyrusandy

@

【WALT】update_history() 代码详解

代码版本:Linux4.9 android-msm-crosshatch-4.9-android12

代码展示

static void update_history(struct rq *rq, struct task_struct *p,
			 u32 runtime, int samples, int event)
{
	u32 *hist = &p->ravg.sum_history[0];
	int ridx, widx;
	u32 max = 0, avg, demand, pred_demand;
	u64 sum = 0;
	u64 prev_demand;

	// ⑴ 判断是否更新任务信息
	if (!runtime || is_idle_task(p) || exiting_task(p) || !samples)
		goto done;
	
	// ⑵ 更新历史窗口数据
	prev_demand = p->ravg.demand;
	
	widx = sched_ravg_hist_size - 1;
	ridx = widx - samples;
	for (; ridx >= 0; --widx, --ridx) {
		hist[widx] = hist[ridx];
		sum += hist[widx];
		if (hist[widx] > max)
			max = hist[widx];
	}
	for (widx = 0; widx < samples && widx < sched_ravg_hist_size; widx++) {
		hist[widx] = runtime;
		sum += hist[widx];
		if (hist[widx] > max)
			max = hist[widx];
	}

	p->ravg.sum = 0;

	// ⑶ 计算 demand
	if (sched_window_stats_policy == WINDOW_STATS_RECENT) {
		demand = runtime;
	} else if (sched_window_stats_policy == WINDOW_STATS_MAX) {
		demand = max;
	} else {
		avg = div64_u64(sum, sched_ravg_hist_size);
		if (sched_window_stats_policy == WINDOW_STATS_AVG)
			demand = avg;
		else
			demand = max(avg, runtime);
	}
	// ⑷ 计算 pred_demand
	pred_demand = predict_and_update_buckets(rq, p, runtime);

	trace_print_pred_demand(pred_demand, demand);

	// ⑸ 将 demand 与 pred_demand 更新到 rq 中
	if (!task_has_dl_policy(p) || !p->dl.dl_throttled) {
		if (task_on_rq_queued(p))
			p->sched_class->fixup_walt_sched_stats(rq, p, demand,
							       pred_demand);
		else if (rq->curr == p)
			walt_fixup_cum_window_demand(rq, demand);
	}

	// ⑹ 更新任务信息
	p->ravg.demand = demand;
	p->ravg.coloc_demand = div64_u64(sum, sched_ravg_hist_size);
	p->ravg.pred_demand = pred_demand;

done:
	trace_sched_update_history(rq, p, runtime, samples, event);
}

代码逻辑

update_history() 是在 WALT 算法中,任务执行到新的一个窗口的时候,对旧窗口内数据进行更新并对新窗口内 demand 进行预测的一个函数。

参数解释:

  1. update_history(struct rq *rq, struct task_struct *p, u32 runtime, int samples, int event)
    p 和 rq:当前要更新信息的任务以及任务所在的运行队列(就绪队列)

    runtime:在 update_task_demand() 中记录的任务的运行时间

    samples:在 update_task_demand() 中记录的窗口数。如果记录的是上一个窗口的,或者是 n 个窗口前的某个窗口的,samples = 1;如果记录的是前 n 个窗口,samples = n,且每个窗口的执行时间都是窗口大小的归一化值。

  2. u32 hist = &p->ravg.sum_history[0];
    hist 是一个指向 p->ravg.sum_history[0] 的指针。

    p->ravg.sum_history 是一个长度为 RAVG_HIST_SIZE_MAX 的数组,存放的是过去 RAVG_HIST_SIZE_MAX 个窗口中的任务的执行时间。
    此处的任务执行时间是在 update_task_demand() 中通过 scale_exec_time() 进行归一化后的时间。

    RAVG_HIST_SIZE_MAX 是用来计算 demand 所需要的窗口的数量。当前版本内核中 RAVG_HIST_SIZE_MAX 默认为 5。

  3. int ridx, widx;
    用来更新 p->ravg.sum_history 所需的参数。

  4. u32 max = 0, avg, demand, pred_demand;
    max 是 p->ravg.sum_history 中的最大值;avg 是 p->ravg.sum_history 中的平均值。

    demand:通过 p->ravg.sum_history 中的值来预测下一个窗口中负载的情况,即任务可能在下一个窗口中的运行时长的归一化值。这个值通常用于任务选核、迁核。

    pred_demand:根据桶算法来预测下一个窗口中负载的情况。这个值通常用于调频。

  5. u64 sum = 0;
    sum 是 p->ravg.sum_history 中的所有值的和。

  6. u64 prev_demand;
    这个值记录上一个窗口中计算而得的 demand 值。

⑴ 判断是否更新任务信息

if (!runtime || is_idle_task(p) || exiting_task(p) || !samples)

  • 如果上个窗口内没有执行时间;
  • 如果任务是 idle 任务;
  • 如果任务正在退出;
  • 如果 samples == 0,

就直接结束,不进行更新。

⑵ 更新历史窗口数据

先通过 prev_demand = p->ravg.demand; 保存上一个窗口的 demand。

sum_history[RAVG_HIST_SIZE_MAX]

介绍一下保存历史窗口负载的数组 sum_history[RAVG_HIST_SIZE_MAX]。

默认情况下,sched_ravg_hist_size = RAVG_HIST_SIZE_MAX = 5,其中 sched_ravg_hist_size 可以根据需求来进行调整。

widx = sched_ravg_hist_size - 1; widx 是设定中最旧窗口的下标。“设定中”的意思是,如果 sched_ravg_hist_size 不等于 RAVG_HIST_SIZE_MAX,那么 sum_history[widx] 指向的就是我们调整窗口数量之后的最旧的窗口。

ridx = widx - samples; ridx 是更新之后的最旧窗口的下标。“更新之后”的意思是,我们要保证 sum_history[0] 始终是上一个窗口的执行时间,如果更新了不止一个窗口,如更新了 samples 个窗口,那么最旧的 samples 个窗口就要被舍弃,同时将前面还未被舍弃的窗口往后挪 samples 个位置,这样才能将第 0 个位置留给上一个窗口。

执行过程如下:

设 samples = 2,即本次更新了 2 个窗口
sum_history:[  a  ] [  b  ] [  c  ] [  d  ] [  e  ]
               0       1       2       3       4
               ↑       ↑       ↑       ↑       ↑
                              ridx            widx
第一个 for 循环:
	for (; ridx >= 0; --widx, --ridx) {
		hist[widx] = hist[ridx];
		// 累加剩余的旧窗口的值
		sum += hist[widx];
		// 计算最大值
		if (hist[widx] > max)
			max = hist[widx];
	}
第一次循环后结果:
sum_history:[  a  ] [  b  ] [  c  ] [  d  ] [  c  ]
               0       1       2       3       4
               ↑       ↑       ↑       ↑       ↑
                      ridx            widx
第二次循环后结果:
sum_history:[  a  ] [  b  ] [  c  ] [  b  ] [  c  ]
               0       1       2       3       4
               ↑       ↑       ↑       ↑       ↑
              ridx            widx
第三次循环后结果:
sum_history:[  a  ] [  b  ] [  a  ] [  b  ] [  c  ]
               0       1       2       3       4
               ↑       ↑       ↑       ↑       ↑
      ridx            widx
第二个 for 循环的开头将 widx 设置为 0:
sum_history:[  a  ] [  b  ] [  a  ] [  b  ] [  c  ]
               0       1       2       3       4
               ↑       ↑       ↑       ↑       ↑
      ridx    widx      
	for (widx = 0; widx < samples && widx < sched_ravg_hist_size; widx++) {
		hist[widx] = runtime;
		// 累加新窗口的值
		sum += hist[widx];
		// 计算最大值
		if (hist[widx] > max)
			max = hist[widx];
	}
第一次循环后结果:
sum_history:[ s_w ] [  b  ] [  a  ] [  b  ] [  c  ]
               0       1       2       3       4
               ↑       ↑       ↑       ↑       ↑
      ridx            widx      
第一次循环后结果:
sum_history:[ s_w ] [ s_w ] [  a  ] [  b  ] [  c  ]
               0       1       2       3       4
               ↑       ↑       ↑       ↑       ↑
      ridx                    widx
因为 samples > 1 的时候 runtime 都是 scaled_window,所以填入的都是 s_w。 

最后令 p->ravg.sum = 0; ,进行下一次 WALT 算法中 runtime 的累加。

⑶ 计算 demand

在当前版本中,WALT 算法设置了四种策略,分别通过不同的方法来计算 demand。
其中,avg = div64_u64(sum, sched_ravg_hist_size);

  1. sched_window_stats_policy == WINDOW_STATS_RECENT
    demand = runtime; 直接将 demand 设置为上一个窗口的 runtime。
  2. sched_window_stats_policy == WINDOW_STATS_MAX
    demand = max; 直接将 demand 设置为历史窗口中 runtime 的最大值。
  3. sched_window_stats_policy == WINDOW_STATS_AVG
    demand = avg; 直接将 demnad 设置为历史窗口中 runtime 的平均值。
  4. sched_window_stats_policy == WINDOW_STATS_MAX_RECENT_AVG(默认情况)
    demand = max(avg, runtime); 在上一个窗口的 runtime 和 历史窗口中 runtime 的平均值中选取最大值作为 demand。

⑷ 计算 pred_demand

根据桶算法来计算 pred_demand:pred_demand = predict_and_update_buckets(rq, p, runtime);

点击此处查看 predict_and_update_buckets() 代码详解。

⑸ 将 demand 与 pred_demand 更新到 CPU 负载中

【WALT】调度与负载计算 中详细描述了 WALT 是如何将之前计算的 demand 和 pred_demand 更新到 CPU 负载之中的。

⑹ 更新任务信息

p->ravg.demand = demand;

p->ravg.coloc_demand = div64_u64(sum, sched_ravg_hist_size);
将历史窗口中的平均值赋进 coloc_demand 中。

p->ravg.pred_demand = pred_demand;

点击此处回到 WALT 入口函数 update_task_ravg()