Skip to contentSkip to conversation

sched/fair: fix initial util_avg calculation

PATCH Superseded Dawei Li 2 participants 2 messages Mar 8, 2024 – Mar 12, 2024 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dawei Li

author
#0 lore raw

PATCH 1/1 sched/fair: fix initial util_avg calculation

According to the comment for post_init_entity_util_avg(), it seems that we are assuming se->load.weight has the same scale/unit as that of cfs_rq->avg.load_avg.

As far as I understand, se->load.weight is the scaled-up load, instead of the true weight (as mapped directly from the nice value) of a task. When CONFIG_32BIT is set, we have load == weight; when CONFIG_64BIT is set, we have load == weight * 1024. However, cfs_rq->avg.load_avg is the sum of true weights of tasks, as se->avg.load_avg corresponds to the true weight of a task.

Based on how sa->util_avg is calculated in the code, we could be inflating sa->util_avg by 1024 times? Could this be the reason why "However, in many cases, the above util_avg does not give a desired value. ... "?

I'm not entirely sure about it. Posting this for clarification and comments.

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -2
index 6a16129f9a5c..0d13e52e1a92 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *		* se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
 	if (cap > 0) {
 		if (cfs_rq->avg.util_avg != 0) {
-			sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+			sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);
 			sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
 			if (sa->util_avg > cap)
Signature · 1 line
2.40.1
maintainer
#1 lore raw
Quoted 12 lines · Dawei Li

According to the comment for post_init_entity_util_avg(), it seems that we are assuming se->load.weight has the same scale/unit as that of cfs_rq->avg.load_avg.

As far as I understand, se->load.weight is the scaled-up load, instead of the true weight (as mapped directly from the nice value) of a task. When CONFIG_32BIT is set, we have load == weight; when CONFIG_64BIT is set, we have load == weight * 1024. However, cfs_rq->avg.load_avg is the sum of true weights of tasks, as se->avg.load_avg corresponds to the true weight of a task.

Quoted 4 lines

Based on how sa->util_avg is calculated in the code, we could be inflating sa->util_avg by 1024 times? Could this be the reason why "However, in many cases, the above util_avg does not give a desired value. ... "?

No, this is about the fact that this estimation of util_avg can give insane value because even the correct se_weight(se) can go up to 88761 whereas util_avg should never go above 1024

Quoted 3 lines

I'm not entirely sure about it. Posting this for clarification and comments.

Good catch.

The commit message needs to be updated to remove your question above but otherwise looks good

Quoted 32 lines
---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -3
index 6a16129f9a5c..0d13e52e1a92 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *             * se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
        if (cap > 0) {
                if (cfs_rq->avg.util_avg != 0) {
-                       sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+                       sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);
                        sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
                        if (sa->util_avg > cap)
--

2.40.1