sched/fair: merge same code in enqueue_task_fair
PATCH 1/1 sched/fair: merge same code in enqueue_task_fair
1. The code below is duplicated in two for loops and need to be consolidated
2. Fix the bug where a se's on_rq is true but its parent is not
```c cfs_rq->h_nr_running++; cfs_rq->idle_h_nr_running += idle_h_nr_running; if (cfs_rq_is_idle(cfs_rq)) idle_h_nr_running = 1; /* end evaluation on encountering a throttled cfs_rq */ if (cfs_rq_throttled(cfs_rq)) goto enqueue_throttle; ```
--- kernel/sched/fair.c | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-)
kernel/sched/fair.c +8 -23
@@ -6681,30 +6681,15 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)cpufreq_update_util (rq,SCHED_CPUFREQ_IOWAIT );for_each_sched_entity (se) { -if (se->on_rq) -break ; cfs_rq =cfs_rq_of (se); -enqueue_entity (cfs_rq, se, flags); - - cfs_rq->h_nr_running++; - cfs_rq->idle_h_nr_running += idle_h_nr_running; - -if (cfs_rq_is_idle (cfs_rq)) - idle_h_nr_running =1 ; - -/* end evaluation on encountering a throttled cfs_rq */ -if (cfs_rq_throttled (cfs_rq)) - goto enqueue_throttle; - - flags =ENQUEUE_WAKEUP ; - } - -for_each_sched_entity (se) { - cfs_rq =cfs_rq_of (se); - -update_load_avg (cfs_rq, se,UPDATE_TG ); -se_update_runnable (se); -update_cfs_group (se); +if (se->on_rq) { +update_load_avg (cfs_rq, se,UPDATE_TG ); +se_update_runnable (se); +update_cfs_group (se); + }else { +enqueue_entity (cfs_rq, se, flags); + flags =ENQUEUE_WAKEUP ; + } cfs_rq->h_nr_running++; cfs_rq->idle_h_nr_running += idle_h_nr_running;
Signature · 1 line
2.40.0
Vincent Guittot
maintainerQuoted 4 lines · WangJinchao
1. The code below is duplicated in two for loops and need to be consolidated
2. Fix the bug where a se'son_rqis true but its parent is not
Could you clarify which bug you want to fix ?
Quoted 64 lines
```c cfs_rq->h_nr_running++; cfs_rq->idle_h_nr_running += idle_h_nr_running; if (cfs_rq_is_idle(cfs_rq)) idle_h_nr_running = 1; /* end evaluation on encountering a throttled cfs_rq */ if (cfs_rq_throttled(cfs_rq)) goto enqueue_throttle; ```Signed-off-by WangJinchao <[email protected]>--- kernel/sched/fair.c | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-)kernel/sched/fair.c +8
-24@@ -6681,30 +6681,15 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)cpufreq_update_util (rq,SCHED_CPUFREQ_IOWAIT );for_each_sched_entity (se) { -if (se->on_rq) -break ; cfs_rq =cfs_rq_of (se); -enqueue_entity (cfs_rq, se, flags); - - cfs_rq->h_nr_running++; - cfs_rq->idle_h_nr_running += idle_h_nr_running; - -if (cfs_rq_is_idle (cfs_rq)) - idle_h_nr_running =1 ; - -/* end evaluation on encountering a throttled cfs_rq */ -if (cfs_rq_throttled (cfs_rq)) - goto enqueue_throttle; - - flags =ENQUEUE_WAKEUP ; - } - -for_each_sched_entity (se) { - cfs_rq =cfs_rq_of (se); - -update_load_avg (cfs_rq, se,UPDATE_TG ); -se_update_runnable (se); -update_cfs_group (se); +if (se->on_rq) { +update_load_avg (cfs_rq, se,UPDATE_TG ); +se_update_runnable (se); +update_cfs_group (se); + }else { +enqueue_entity (cfs_rq, se, flags); + flags =ENQUEUE_WAKEUP ; + } cfs_rq->h_nr_running++; cfs_rq->idle_h_nr_running += idle_h_nr_running; --2.40.0
Quoted 7 lines · Vincent Guittot
Quoted 4 lines · WangJinchao
1. The code below is duplicated in two for loops and need to be consolidated
2. Fix the bug where a se'son_rqis true but its parent is notCould you clarify which bug you want to fix ?
Taking into account the additional information provided by Tim, this is not a bug. Therefore, this patch is merely a logical simplification.
I will send out a v2 and update the description in it.
Vincent Guittot
maintainerQuoted 4 lines · Wang Jinchao
Quoted 2 lines · Vincent Guittot
Quoted 1 line · WangJinchao
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
1. The code below is duplicated in two for loops and need to be consolidated
2. Fix the bug where a se'son_rqis true but its parent is notCould you clarify which bug you want to fix ?
Taking into account the additional information provided by Tim, this is not a bug. Therefore, this patch is merely a logical simplification.
If there is no bug why changing it ?
The duplication is done in order to have the same pattern in :enqueue_task_fairdequeue_task_fairthrottle_cfs_rqunthrottle_cfs_rq
so there is no need to change it
Quoted 2 lines
I will send out a v2 and update the description in it.
Quoted 9 lines · Vincent Guittot
Quoted 8 lines · Wang Jinchao
Quoted 6 lines · Vincent Guittot
Quoted 4 lines · WangJinchao
1. The code below is duplicated in two for loops and need to be consolidated
2. Fix the bug where a se'son_rqis true but its parent is not
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Could you clarify which bug you want to fix ?
Taking into account the additional information provided by Tim, this is not a bug. Therefore, this patch is merely a logical simplification.
If there is no bug why changing it ?
For two reasons:
1. (from Abel Wu)
It doesn't need to, but it can actually bring some benefit from the point of view of text size, especially in warehouse-scale computers where icache is extremely contended.
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-56 (-56)
Function old new delta enqueue_task_fair 936 880 -56
Total: Before=64899, After=64843, chg -0.09%
2. For better code comprehension
I became curious when I reached this part, wondering why there is a lot of repetition inside these two for-loops. Then I thought about 'do not repeat yourself,' and I feel that merging them would lead to a clearer understanding. Of course, it might be because I am just starting to read scheduler-related code and am not yet familiar with the entire logic.
Quoted 6 lines
The duplication is done in order to have the same pattern in :
enqueue_task_fairdequeue_task_fairthrottle_cfs_rqunthrottle_cfs_rq
Due to the two points mentioned above, do we need to adjust all four functions?
Quoted 2 lines
so there is no need to change it
I plan to get familiar with the scheduler-related code first and then consider this.
Thanks
Quoted 1 line
On 12/14/23 5:47 PM, Wang Jinchao Wrote:
Quoted 22 lines · Vincent Guittot
Quoted 9 lines · Wang Jinchao
Quoted 8 lines · Vincent Guittot
Quoted 6 lines · WangJinchao
Quoted 4 lines
1. The code below is duplicated in two for loops and need to be consolidated
2. Fix the bug where a se'son_rqis true but its parent is notQuoted 6 lines
Quoted 4 lines
Quoted 1 line
Could you clarify which bug you want to fix ?
Taking into account the additional information provided by Tim, this is not a bug. Therefore, this patch is merely a logical simplification.
If there is no bug why changing it ?
For two reasons:
1. (from Abel Wu)
It doesn't need to, but it can actually bring some benefit from the point of view of text size, especially in warehouse-scale computers where icache is extremely contended.
Quoted 4 lines
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-56 (-56)
Function old new delta enqueue_task_fair 936 880 -56Total: Before=64899, After=64843, chg -0.09%
But TBH this benefit is kind of weak to argue about, given that you don't have any data supporting it.
Quoted 20 lines
2. For better code comprehension
I became curious when I reached this part, wondering why there is a lot of repetition inside these two for-loops. Then I thought about 'do not repeat yourself,' and I feel that merging them would lead to a clearer understanding. Of course, it might be because I am just starting to read scheduler-related code and am not yet familiar with the entire logic.Quoted 6 lines
The duplication is done in order to have the same pattern in :
enqueue_task_fairdequeue_task_fairthrottle_cfs_rqunthrottle_cfs_rqDue to the two points mentioned above, do we need to adjust all four functions?
Quoted 2 lines
so there is no need to change it
I plan to get familiar with the scheduler-related code first and then consider this.
Thanks
Quoted 1 line
Quoted 23 lines · Abel Wu
On 12/14/23 5:47 PM, Wang Jinchao Wrote:
Quoted 22 lines · Vincent Guittot
Quoted 9 lines · Wang Jinchao
Quoted 8 lines · Vincent Guittot
Quoted 6 lines · WangJinchao
Quoted 4 lines
1. The code below is duplicated in two for loops and need to be consolidated
2. Fix the bug where a se'son_rqis true but its parent is notQuoted 6 lines
Quoted 4 lines
Quoted 1 line
Could you clarify which bug you want to fix ?
Taking into account the additional information provided by Tim, this is not a bug. Therefore, this patch is merely a logical simplification.
If there is no bug why changing it ?
For two reasons:
1. (from Abel Wu)
It doesn't need to, but it can actually bring some benefit from the point of view of text size, especially in warehouse-scale computers where icache is extremely contended.
Quoted 7 lines
Quoted 4 lines
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-56 (-56)
Function old new delta enqueue_task_fair 936 880 -56Total: Before=64899, After=64843, chg -0.09%
But TBH this benefit is kind of weak to argue about, given that you don't have any data supporting it.
Agree. My initinal target is clear comprehension.
And thanks for your numbers.
Quoted 21 lines
Quoted 21 lines
2. For better code comprehension
I became curious when I reached this part, wondering why there is a lot of repetition inside these two for-loops. Then I thought about 'do not repeat yourself,' and I feel that merging them would lead to a clearer understanding. Of course, it might be because I am just starting to read scheduler-related code and am not yet familiar with the entire logic.Quoted 6 lines
The duplication is done in order to have the same pattern in :
enqueue_task_fairdequeue_task_fairthrottle_cfs_rqunthrottle_cfs_rqDue to the two points mentioned above, do we need to adjust all four functions?
Quoted 2 lines
so there is no need to change it
I plan to get familiar with the scheduler-related code first and then consider this.
Thanks
Quoted 1 line
Quoted 3 lines · WangJinchao
1. The code below is duplicated in two for loops and need to be consolidated
2. Fix the bug where a se'son_rqis true but its parent is not
In the current code, If a se is already on a rq, all the parents would have already been on rq too. I don't think there's a case where se's on_rq and parent
is not for the current code before your patch. Otherwise the child
would never get scheduled. I don't think we have seen such bug being reported.
Quoted 48 lines
```c cfs_rq->h_nr_running++; cfs_rq->idle_h_nr_running += idle_h_nr_running; if (cfs_rq_is_idle(cfs_rq)) idle_h_nr_running = 1; /* end evaluation on encountering a throttled cfs_rq */ if (cfs_rq_throttled(cfs_rq)) goto enqueue_throttle; ```Signed-off-by WangJinchao <[email protected]>--- kernel/sched/fair.c | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-)kernel/sched/fair.c +0
-21@@ -6681,30 +6681,15 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)cpufreq_update_util (rq,SCHED_CPUFREQ_IOWAIT );for_each_sched_entity (se) { -if (se->on_rq) -break ; cfs_rq =cfs_rq_of (se); -enqueue_entity (cfs_rq, se, flags); - - cfs_rq->h_nr_running++; - cfs_rq->idle_h_nr_running += idle_h_nr_running; - -if (cfs_rq_is_idle (cfs_rq)) - idle_h_nr_running =1 ; - -/* end evaluation on encountering a throttled cfs_rq */ -if (cfs_rq_throttled (cfs_rq)) - goto enqueue_throttle; - - flags =ENQUEUE_WAKEUP ; - } - -for_each_sched_entity (se) { - cfs_rq =cfs_rq_of (se); - -update_load_avg (cfs_rq, se,UPDATE_TG );
kernel/sched/fair.c enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) · line 6692
Quoted 10 lines
kernel/sched/fair.c +8
-2-se_update_runnable (se); -update_cfs_group (se); +if (se->on_rq) { +update_load_avg (cfs_rq, se,UPDATE_TG ); +se_update_runnable (se); +update_cfs_group (se); + }else { +enqueue_entity (cfs_rq, se, flags); + flags =ENQUEUE_WAKEUP ; + }
The code change looks like a reasonable simplification. Logic is the same as the old code, assuming that once a se's on_rq flag is true, all its parents are too.
Thanks.
Tim
Quoted 3 lines
kernel/sched/fair.c +0
-0cfs_rq->h_nr_running++; cfs_rq->idle_h_nr_running += idle_h_nr_running;