Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp960016ybd; Wed, 26 Jun 2019 08:46:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqzajO/Q1zk0nkI0VEDpGdxxF/h++/dfZTuDWWxs/U/L3s5uxG4pm/yoyZq9K9oHwEHrATNx X-Received: by 2002:a17:902:106:: with SMTP id 6mr6389523plb.64.1561563972268; Wed, 26 Jun 2019 08:46:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561563972; cv=none; d=google.com; s=arc-20160816; b=Y3ahxIfD8ZXVlbyzirtqZZYVGOxR9gSgWG77YtXHyyBmpC2FnRqzgtedAgw0mY/hWO JH2Lpg3o+1FgG7xIRY6Jnhi9KsQlknKeRi6pmuHIGXIfMC2/tRk/A5ggHVEZ0p4WLtJj YqRDV2+f3MCoUVH1u38lTrz7qq+jpPO9fZgimm4cUvQDvgFWifHBJR1XSXTSfpx12DZe svSYOcgHkai5LSDsv3JmRNWCbT+6LZuq31bSbBz1nfOk+TAmyNq2/qk3gaKxHILnLq8n E9qPlqisq/Hc+g/DAK3IFNWPwEaPjezg1zH2c4mLPz1OCeYQ9fWxP0w9GzWyI7aWTk1F epvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=wDkcKAhF2v3PECOXqoMw3z8hwHgY9X+Qva2hXbFTA2Q=; b=q8wmSn9y9nK93OzlitfB5clzDJXTppnPCyOt8mfz0SdfVZ0qyyz7aAXAMHSRpla40Z P8TlRdilQArf4QeTFbXzdahG4K4hasOsxYqchs8Ka1AVrwDVCR8cjFcqieB10ar2DCoh 7brB7QUgNEJWNrEbDhCJ4MiwC5JzrePpyxDYPZ+q6HO+bcZFTdxOYUV+OBLXg/CLg/i+ nW6qXZKBCARTZChEcHhr101fNdzXNXFU1u8nuSyF9wYtO1HgpBap8JUxNeqgisH0bX8G +FdImfAaFhRzke9/e13R1ukG0IJna7lfTSqbiGDO7tMqFhQnzTT/wolMpw88eR/OYV3k S+Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JLNkYyvz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o6si3492261pls.251.2019.06.26.08.45.55; Wed, 26 Jun 2019 08:46:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JLNkYyvz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726357AbfFZPpR (ORCPT + 99 others); Wed, 26 Jun 2019 11:45:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:54088 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725958AbfFZPpR (ORCPT ); Wed, 26 Jun 2019 11:45:17 -0400 Received: from localhost (lfbn-ncy-1-174-150.w83-194.abo.wanadoo.fr [83.194.254.150]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 782F62177B; Wed, 26 Jun 2019 15:45:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1561563915; bh=gYFjiQbQQDdwcLzpj1YTXjQ0WKeNjpheQWJEGAvEQpk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=JLNkYyvzIc+SzkkHxVfETGL/GzAQUAqWWgwZJUtBYoSVqAzHWXbvNKpbs6ty9ktyo CPopkxUT+hTK04+uqdHr4YZufTm4aPgTpvG+wzN3dycyqFBMUTPUfLfyIj7ruGYLQQ LrQ8Q/BTm1xUk8ZCyRiuDaVCL1jsGORFx5wgZye0= Date: Wed, 26 Jun 2019 17:45:13 +0200 From: Frederic Weisbecker To: Wanpeng Li Cc: LKML , Ingo Molnar , Peter Zijlstra , Ingo Molnar , Frederic Weisbecker Subject: Re: [PATCH] sched/nohz: Optimize get nohz timer target Message-ID: <20190626154511.GA29951@lenoir> References: <1560827581-8827-1-git-send-email-wanpengli@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 19, 2019 at 08:42:19AM +0800, Wanpeng Li wrote: > Cc Frederic, > On Tue, 18 Jun 2019 at 11:13, Wanpeng Li wrote: > > > > From: Wanpeng Li > > > > On a machine, cpu 0 is used for housekeeping, other 39 cpus are in > > nohz_full mode. We can observe huge time burn in the loop for seaching > > nearest busy housekeeper cpu by ftrace. > > > > 2) | get_nohz_timer_target() { > > 2) 0.240 us | housekeeping_test_cpu(); > > 2) 0.458 us | housekeeping_test_cpu(); > > > > ... > > > > 2) 0.292 us | housekeeping_test_cpu(); > > 2) 0.240 us | housekeeping_test_cpu(); > > 2) 0.227 us | housekeeping_any_cpu(); > > 2) + 43.460 us | } > > > > This patch optimizes the searching logic by finding a nearest housekeeper > > cpu in the housekeeping cpumask, it can minimize the worst searching time > > from ~44us to < 10us in my testing. > > > > Cc: Ingo Molnar > > Cc: Peter Zijlstra > > Signed-off-by: Wanpeng Li > > --- > > kernel/sched/core.c | 5 +++-- > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > > index 83bd6bb..db550cf 100644 > > --- a/kernel/sched/core.c > > +++ b/kernel/sched/core.c > > @@ -548,11 +548,12 @@ int get_nohz_timer_target(void) > > > > rcu_read_lock(); > > for_each_domain(cpu, sd) { > > - for_each_cpu(i, sched_domain_span(sd)) { > > + for_each_cpu_and(i, sched_domain_span(sd), > > + housekeeping_cpumask(HK_FLAG_TIMER)) { > > if (cpu == i) > > continue; > > > > - if (!idle_cpu(i) && housekeeping_cpu(i, HK_FLAG_TIMER)) { > > + if (!idle_cpu(i)) { > > cpu = i; > > goto unlock; > > } Nice, but you also need to handle the default case that doesn't make much sense anymore. It hasn't ever been clear anyway. The last iterated buzy housekeeper can become a random candidate while current CPU is a better fallback if it is a housekeeper. Also you're enhancing housekeeping_any_cpu() in another patch so give it a better chance: diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 4778c48a7fda..c5229d71540a 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -539,27 +539,32 @@ void resched_cpu(int cpu) */ int get_nohz_timer_target(void) { - int i, cpu = smp_processor_id(); + int i, cpu = smp_processor_id(), default_cpu = -1; struct sched_domain *sd; - if (!idle_cpu(cpu) && housekeeping_cpu(cpu, HK_FLAG_TIMER)) - return cpu; + if (housekeeping_cpu(cpu, HK_FLAG_TIMER)) { + if (!idle_cpu(cpu)) + return cpu; + default_cpu = cpu; + } rcu_read_lock(); for_each_domain(cpu, sd) { - for_each_cpu(i, sched_domain_span(sd)) { + for_each_cpu_and(i, sched_domain_span(sd), + housekeeping_cpumask(HK_FLAG_TIMER)) { if (cpu == i) continue; - if (!idle_cpu(i) && housekeeping_cpu(i, HK_FLAG_TIMER)) { + if (!idle_cpu(i)) { cpu = i; goto unlock; } } } - if (!housekeeping_cpu(cpu, HK_FLAG_TIMER)) - cpu = housekeeping_any_cpu(HK_FLAG_TIMER); + if (default_cpu == -1) + default_cpu = housekeeping_any_cpu(HK_FLAG_TIMER); + cpu = default_cpu; unlock: rcu_read_unlock(); return cpu;