Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2959246pxj; Mon, 10 May 2021 15:01:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQBd1TuxqKnVHtTz/HC3WIRGW4EmXcPiQpE7JfI0BJHJsippTxpED+NRW76MOdPDKHrlug X-Received: by 2002:a05:6e02:20c2:: with SMTP id 2mr24683176ilq.120.1620684064438; Mon, 10 May 2021 15:01:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620684064; cv=none; d=google.com; s=arc-20160816; b=v+Cf3RVcdlpjFkim87o60YXiAo3TkB7nHJedRgz35776GCOJUrfpxhIcuNe/JJH1GP oHGnFeAz6YIQqSZUZjnnuqD1Gv1n315e+EtonDY5ARGd6Oj7zmpWKgHEkoqrFjb+7jhC dquxG0fOM1e1dPrEFWmgGLwwmt135hO9cpaTnbJbD74e9Nav3gj3mQju5yHkWWQiFRBX sFJMulPr7R2kN249Fl2UTib9hk4mbVhWb/o1Bn0X0QmlZUhZuX/DSiVM8w4PXeQdnpVu 9lDtr3kcVP4U/a+sYMQ6J1JW3IX8zj4m8HFf/h8IYCDLGZiKIL28ftBlL8XoWE919XcS QjWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:from:subject:ironport-sdr:ironport-sdr; bh=sveyjF9sTbSvXCoeGndv15vvBs6AE4SCnyy40VC9ZRo=; b=AKoPbweixJV1fVpzxT0V3Z8AVFHl2uPBHO7/03HcCpFq+GvuQTX0V38xtNySdgNHGJ YGyc5FWZo2fCiNTY+/YWisvfA/4xpZHvfCF+HwWtBRcWY/H+aJ2Jz3TZnOoXdlhWRjYZ L/so5cW+v8ntJ2nVuW+9VydpRRH3/2L0LmYdW4ZbGPRNtBQJnfRNwN89kia6C/5R7NmL uraUNeEC5/n2ufxJTrm6pMDRp/niSh+f3p7yE5QlqOxsuLFuKwVIG2JmKwns4Uzs6+pX WdDDat6Zh933/14yfxNRG9QAtWLWHVF8Isq6mJkPrLeadVtG7dElbKY1UiWx+fVBr2n/ lMOg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a17si19478636iol.19.2021.05.10.15.00.51; Mon, 10 May 2021 15:01:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232855AbhEJWAs (ORCPT + 99 others); Mon, 10 May 2021 18:00:48 -0400 Received: from mga04.intel.com ([192.55.52.120]:59504 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231694AbhEJWAq (ORCPT ); Mon, 10 May 2021 18:00:46 -0400 IronPort-SDR: yYmWw7mgOPyB5XER8hZNsb8uD6l/zVvD4Ttad2GFeeZ3reOjPJGsOHq874QTDIUV295+n+HKIy ZbUs8XexOPQQ== X-IronPort-AV: E=McAfee;i="6200,9189,9980"; a="197314972" X-IronPort-AV: E=Sophos;i="5.82,288,1613462400"; d="scan'208";a="197314972" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 May 2021 14:59:41 -0700 IronPort-SDR: D/HVooyKbNssXp0NwS2XiCVZnvRmu036j47J+n1mSn7Vza8xdJ90LmLQMTwwo3y53iJ7bcLQaJ J6DjxhAnPgfA== X-IronPort-AV: E=Sophos;i="5.82,288,1613462400"; d="scan'208";a="461570180" Received: from schen9-mobl.amr.corp.intel.com ([10.212.130.234]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 May 2021 14:59:40 -0700 Subject: Re: [PATCH] sched/fair: Rate limit calls to update_blocked_averages() for NOHZ From: Tim Chen To: Vincent Guittot Cc: Joel Fernandes , linux-kernel , Paul McKenney , Frederic Weisbecker , Dietmar Eggeman , Qais Yousef , Ben Segall , Daniel Bristot de Oliveira , Ingo Molnar , Juri Lelli , Mel Gorman , Peter Zijlstra , Steven Rostedt , "Uladzislau Rezki (Sony)" , Neeraj upadhyay , Aubrey Li References: <20210122154600.1722680-1-joel@joelfernandes.org> <20210129172727.GA30719@vingu-book> <274d8ae5-8f4d-7662-0e04-2fbc92b416fc@linux.intel.com> <20210324134437.GA17675@vingu-book> <4aa674d9-db49-83d5-356f-a20f9e2a7935@linux.intel.com> <2d2294ce-f1d1-f827-754b-4541c1b43be8@linux.intel.com> Message-ID: Date: Mon, 10 May 2021 14:59:39 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <2d2294ce-f1d1-f827-754b-4541c1b43be8@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/9/21 10:59 AM, Tim Chen wrote: >>> 11.602 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb739 >>> 11.624 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739 >>> 11.642 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739 >>> 11.645 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739 >>> 11.977 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739 >>> 12.003 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739 >>> 12.015 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739 >>> 12.043 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739 >>> 12.567 ( ): probe:update_blocked_averages:(ffffffff810cf070) cpu=2 jiffies=0x1004fb73a >>> 13.856 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb73b >>> 13.910 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b >>> 14.003 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b >>> 14.159 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b >>> 14.203 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b >>> 14.223 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b >>> 14.301 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b >>> 14.504 ( ): probe:update_blocked_averages:(ffffffff810cf070) cpu=2 jiffies=0x1004fb73c >> I don't know exactly what you track with "next_balance=" in > > It is the rq->next_balance value as we enter the newidle_balance function. > >> probe:newidle_balance but it always starts with the same value >> 0x1004fb76c in the future to finish with a value 0x1004fb731 in the >> past. > > This indeed is odd as the next_balance should move forward and not backward. Vincent, I found a bug in newidle_balance() that moved the next balance time backward. I fixed it in patch 1 below. This corrects the next_balance time update and we now have proper load balance rate limiting. After putting in the other two changes previously discussed with you (patch 2 and 3 below), I see very good improvement (about +5%) in the database workload I was investigating. The costly update_blocked_averages() function is called much less frequently and consumed only 0.2% of cpu cycles instead of 2.6% before the changes. Including all three patches here together for easier review. The patches apply to the tip tree's sched/core branch. Thanks. Tim --->8--- From 848eb8f45b53b45cacf70022c98f632daabefe77 Mon Sep 17 00:00:00 2001 Message-Id: <848eb8f45b53b45cacf70022c98f632daabefe77.1620677280.git.tim.c.chen@linux.intel.com> From: Tim Chen Date: Fri, 7 May 2021 14:19:47 -0700 Subject: [PATCH 1/3] sched: Fix rq->next_balance time going backward In traces on newidle_balance(), this_rq->next_balance time goes backward from time to time, e.g. 11.602 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb739 11.624 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb739 13.856 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb73b 13.910 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73b 14.637 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb76c jiffies=0x1004fb73c 14.666 ( ): probe:newidle_balance:(ffffffff810d2470) this_rq=0xffff88fe7f8aae00 next_balance=0x1004fb731 jiffies=0x1004fb73c This was due to newidle_balance() updated this_rq->next_balance to an earlier time than its current value. The real intention was to make sure next_balance move this_rq->next_balance forward in its update: out: /* Move the next balance forward */ if (time_after(this_rq->next_balance, next_balance)) this_rq->next_balance = next_balance; The actual outcome was moving this_rq->next_balance backward, in the wrong direction. Fix the incorrect check on next_balance causing the problem. Signed-off-by: Tim Chen --- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 1d75af1ecfb4..b0b5698b2184 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -10681,7 +10681,7 @@ static int newidle_balance(struct rq *this_rq, struct rq_flags *rf) out: /* Move the next balance forward */ - if (time_after(this_rq->next_balance, next_balance)) + if (time_after(next_balance, this_rq->next_balance)) this_rq->next_balance = next_balance; if (pulled_task) -- 2.20.1 From f2c9af4af6438ad79076e1a664003dc01ad4fdf0 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <848eb8f45b53b45cacf70022c98f632daabefe77.1620677280.git.tim.c.chen@linux.intel.com> References: <848eb8f45b53b45cacf70022c98f632daabefe77.1620677280.git.tim.c.chen@linux.intel.com> From: Vincent Guittot Date: Fri, 7 May 2021 14:38:10 -0700 Subject: [PATCH 2/3] sched: Skip update_blocked_averages if we are defering load balance In newidle_balance(), the scheduler skips load balance to the new idle cpu when sd is this_rq and when this_rq->avg_idle < sd->max_newidle_lb_cost Doing a costly call to update_blocked_averages() will not be useful and simply adds overhead when this condition is true. Check the condition early in newidle_balance() to skip update_blocked_averages() when possible. Signed-off-by: Vincent Guittot Signed-off-by: Tim Chen --- kernel/sched/fair.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index b0b5698b2184..f828b75488a0 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -10612,17 +10612,20 @@ static int newidle_balance(struct rq *this_rq, struct rq_flags *rf) */ rq_unpin_lock(this_rq, rf); + rcu_read_lock(); + sd = rcu_dereference_check_sched_domain(this_rq->sd); + if (this_rq->avg_idle < sysctl_sched_migration_cost || - !READ_ONCE(this_rq->rd->overload)) { + !READ_ONCE(this_rq->rd->overload) || + (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { - rcu_read_lock(); - sd = rcu_dereference_check_sched_domain(this_rq->sd); if (sd) update_next_balance(sd, &next_balance); rcu_read_unlock(); goto out; } + rcu_read_unlock(); raw_spin_unlock(&this_rq->lock); -- 2.20.1 From c45d13c6156c3cdc340ef3ba523b8750642a9c50 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: <848eb8f45b53b45cacf70022c98f632daabefe77.1620677280.git.tim.c.chen@linux.intel.com> References: <848eb8f45b53b45cacf70022c98f632daabefe77.1620677280.git.tim.c.chen@linux.intel.com> From: Tim Chen Date: Fri, 7 May 2021 14:54:54 -0700 Subject: [PATCH 3/3] sched: Rate limit load balance in newidle_balance() Currently newidle_balance() could do load balancng even if the cpu is not due for a load balance. Make newidle_balance() check the next_balance time on the cpu's runqueue so it defers load balancing if it is not due for its load balance. Signed-off-by: Tim Chen --- kernel/sched/fair.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index f828b75488a0..8e00e1fdd6e0 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -10617,6 +10617,7 @@ static int newidle_balance(struct rq *this_rq, struct rq_flags *rf) if (this_rq->avg_idle < sysctl_sched_migration_cost || !READ_ONCE(this_rq->rd->overload) || + time_before(jiffies, this_rq->next_balance) || (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { if (sd) -- 2.20.1