Message ID | 202312101719+0800-wangjinchao@xfusion.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6438250vqy; Sun, 10 Dec 2023 01:39:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IEYi5hlO9MqKMoSAdTHjdefTOBzOk3nCzL00k0PnPCk4BIJbIJ+zzSa1FXsVGabtYjN3d+V X-Received: by 2002:a05:6808:22a6:b0:3ba:380:87ad with SMTP id bo38-20020a05680822a600b003ba038087admr1149414oib.99.1702201169245; Sun, 10 Dec 2023 01:39:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702201169; cv=none; d=google.com; s=arc-20160816; b=Y3C4Fiy3iAh3dUO0DZCBDnvpCDMuC55nfdTN2R3wZG2FHVzgsS5uGF8T3dm0+eW5sh 11Yan99bMveuXngDUjqqKaNLZ8fAhmf/d1FhxzY8FttppOmvZrtX47wT2T8v0yOJ5CwU weXREZ+KwzwBDXwm0kLMmk04TEFRk5Pq7mQJxZYvb/7YWQXjZ+OH2wsT4gd0tztrW5nw Y4DyX0/w4Yfzljj1G+Bl1DKi6kWzbbEGIR+nHW4JH4SQRqCUhZPwHa0j+4+5BHHHMcmQ VUGpVWL5YwF7vIZdD9KF7su9jtMhiy/mvPbFoc53hUHGykQs9ZosEyWLm/r/Rlm3fqLZ 1zfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date; bh=M7cLVuXx/fWAzIEUg2Y+b74t8aLXqdFLIbba5aZvwRo=; fh=f40UFx4PIoN5VHjot5Te0Vsnl2epc2I3Vpkjl2f6MCo=; b=iih7DHPkjbiaEEGXX2hiA1EGKCkmqEeektgFY/kLFNPXLwCTEmCtCBdIOgMXPs2f2p yGfZNLk1vMVeqtcqWUvijrOjJwDf7+81s+ww3C3Y+ZoOvrjtLbceWeCDAcSWwvKt/QZR NCs1rQGvHqEGZj+gm7e/5avdOxHb/JhZ4ZfZwy1kLfP0bKuTyu6xOnAgX7cqmB1DFeGl 7Vom9FeRmPCySGUdRj0Vi+bK90egrxokyge7KLMcDeIRS/chlPzVFnotC2Sx8P/6XN2Z 0GDwhzyELI0dPXQP9Ci65/egfUaiwC4yKkZfAuy4xQ2KHnGsZjaQGjW9SjMEFyD+jzNI jgkg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id ch9-20020a17090af40900b002886d6c7ea2si4361814pjb.177.2023.12.10.01.39.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 01:39:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 48E66806B077; Sun, 10 Dec 2023 01:39:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231700AbjLJJjQ (ORCPT <rfc822;rua109.linux@gmail.com> + 99 others); Sun, 10 Dec 2023 04:39:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229584AbjLJJjP (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 10 Dec 2023 04:39:15 -0500 X-Greylist: delayed 1069 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Sun, 10 Dec 2023 01:39:20 PST Received: from wxsgout04.xfusion.com (wxsgout04.xfusion.com [36.139.87.180]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B10D0C5 for <linux-kernel@vger.kernel.org>; Sun, 10 Dec 2023 01:39:20 -0800 (PST) Received: from wuxshcsitd00600.xfusion.com (unknown [10.32.133.213]) by wxsgout04.xfusion.com (SkyGuard) with ESMTPS id 4Snzn42TQYz9ygN2; Sun, 10 Dec 2023 17:18:00 +0800 (CST) Received: from localhost (10.82.147.3) by wuxshcsitd00600.xfusion.com (10.32.133.213) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sun, 10 Dec 2023 17:21:24 +0800 Date: Sun, 10 Dec 2023 17:21:24 +0800 From: WangJinchao <wangjinchao@xfusion.com> To: Ingo Molnar <mingo@redhat.com>, Peter Zijlstra <peterz@infradead.org>, Juri Lelli <juri.lelli@redhat.com>, Vincent Guittot <vincent.guittot@linaro.org>, Dietmar Eggemann <dietmar.eggemann@arm.com>, Steven Rostedt <rostedt@goodmis.org>, Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>, Daniel Bristot de Oliveira <bristot@redhat.com>, Valentin Schneider <vschneid@redhat.com>, <linux-kernel@vger.kernel.org> CC: <stone.xulei@xfusion.com>, <wangjinchao@xfusion.com> Subject: [PATCH] sched/fair: merge same code in enqueue_task_fair Message-ID: <202312101719+0800-wangjinchao@xfusion.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline X-Originating-IP: [10.82.147.3] X-ClientProxiedBy: wuxshcsitd00603.xfusion.com (10.32.134.231) To wuxshcsitd00600.xfusion.com (10.32.133.213) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sun, 10 Dec 2023 01:39:25 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784887293053634358 X-GMAIL-MSGID: 1784887293053634358 |
Series |
sched/fair: merge same code in enqueue_task_fair
|
|
Commit Message
Wang Jinchao
Dec. 10, 2023, 9:21 a.m. UTC
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;
```
Signed-off-by: WangJinchao <wangjinchao@xfusion.com>
---
kernel/sched/fair.c | 31 ++++++++-----------------------
1 file changed, 8 insertions(+), 23 deletions(-)
Comments
On Sun, 10 Dec 2023 at 10:22, WangJinchao <wangjinchao@xfusion.com> wrote: > > 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 Could you clarify which bug you want to fix ? > > ```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 <wangjinchao@xfusion.com> > --- > kernel/sched/fair.c | 31 ++++++++----------------------- > 1 file changed, 8 insertions(+), 23 deletions(-) > > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c > index d7a3c63a2171..e1373bfd4f2e 100644 > --- a/kernel/sched/fair.c > +++ b/kernel/sched/fair.c > @@ -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 >
On Sun, 2023-12-10 at 17:21 +0800, WangJinchao wrote: > 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 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. > > ```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 <wangjinchao@xfusion.com> > --- > kernel/sched/fair.c | 31 ++++++++----------------------- > 1 file changed, 8 insertions(+), 23 deletions(-) > > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c > index d7a3c63a2171..e1373bfd4f2e 100644 > --- a/kernel/sched/fair.c > +++ b/kernel/sched/fair.c > @@ -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; > + } 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 > > cfs_rq->h_nr_running++; > cfs_rq->idle_h_nr_running += idle_h_nr_running;
On Mon, Dec 11, 2023 at 04:23:52PM +0100, Vincent Guittot wrote: > On Sun, 10 Dec 2023 at 10:22, WangJinchao <wangjinchao@xfusion.com> wrote: > > > > 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 > > 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. I will send out a v2 and update the description in it.
On Wed, 13 Dec 2023 at 08:04, Wang Jinchao <wangjinchao@xfusion.com> wrote: > > On Mon, Dec 11, 2023 at 04:23:52PM +0100, Vincent Guittot wrote: > > On Sun, 10 Dec 2023 at 10:22, WangJinchao <wangjinchao@xfusion.com> wrote: > > > > > > 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 > > > > 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 ? The duplication is done in order to have the same pattern in : enqueue_task_fair dequeue_task_fair throttle_cfs_rq unthrottle_cfs_rq so there is no need to change it > > I will send out a v2 and update the description in it.
On Wed, Dec 13, 2023 at 09:23:46AM +0100, Vincent Guittot wrote: > On Wed, 13 Dec 2023 at 08:04, Wang Jinchao <wangjinchao@xfusion.com> wrote: > > > > On Mon, Dec 11, 2023 at 04:23:52PM +0100, Vincent Guittot wrote: > > > On Sun, 10 Dec 2023 at 10:22, WangJinchao <wangjinchao@xfusion.com> wrote: > > > > > > > > 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 > > > > > > 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. > > The duplication is done in order to have the same pattern in : > enqueue_task_fair > dequeue_task_fair > throttle_cfs_rq > unthrottle_cfs_rq Due to the two points mentioned above, do we need to adjust all four functions? > > so there is no need to change it I plan to get familiar with the scheduler-related code first and then consider this. Thanks >
On 12/14/23 5:47 PM, Wang Jinchao Wrote: > On Wed, Dec 13, 2023 at 09:23:46AM +0100, Vincent Guittot wrote: >> On Wed, 13 Dec 2023 at 08:04, Wang Jinchao <wangjinchao@xfusion.com> wrote: >>> >>> On Mon, Dec 11, 2023 at 04:23:52PM +0100, Vincent Guittot wrote: >>>> On Sun, 10 Dec 2023 at 10:22, WangJinchao <wangjinchao@xfusion.com> wrote: >>>>> >>>>> 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 >>>> >>>> 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% But TBH this benefit is kind of weak to argue about, given that you don't have any data supporting it. > > 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. >> >> The duplication is done in order to have the same pattern in : >> enqueue_task_fair >> dequeue_task_fair >> throttle_cfs_rq >> unthrottle_cfs_rq > Due to the two points mentioned above, do we need to adjust all four functions? >> >> so there is no need to change it > I plan to get familiar with the scheduler-related code first and then consider this. > > Thanks >>
On Thu, Dec 14, 2023 at 08:10:57PM +0800, Abel Wu wrote: > On 12/14/23 5:47 PM, Wang Jinchao Wrote: > > On Wed, Dec 13, 2023 at 09:23:46AM +0100, Vincent Guittot wrote: > > > On Wed, 13 Dec 2023 at 08:04, Wang Jinchao <wangjinchao@xfusion.com> wrote: > > > > > > > > On Mon, Dec 11, 2023 at 04:23:52PM +0100, Vincent Guittot wrote: > > > > > On Sun, 10 Dec 2023 at 10:22, WangJinchao <wangjinchao@xfusion.com> wrote: > > > > > > > > > > > > 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 > > > > > > > > > > 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% > > 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. > > > > > 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. > > > > > > The duplication is done in order to have the same pattern in : > > > enqueue_task_fair > > > dequeue_task_fair > > > throttle_cfs_rq > > > unthrottle_cfs_rq > > Due to the two points mentioned above, do we need to adjust all four functions? > > > > > > so there is no need to change it > > I plan to get familiar with the scheduler-related code first and then consider this. > > > > Thanks > > >
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index d7a3c63a2171..e1373bfd4f2e 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -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;