Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2230828ybl; Sun, 19 Jan 2020 22:17:33 -0800 (PST) X-Google-Smtp-Source: APXvYqxdXw0sNfwzHqv5QkzdhplVnQeWTvTAzWFFXsJtybyeV2VyuRS10O3oWPHqvJ+9f544vOX9 X-Received: by 2002:aca:d502:: with SMTP id m2mr11364984oig.41.1579501053363; Sun, 19 Jan 2020 22:17:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579501053; cv=none; d=google.com; s=arc-20160816; b=Jb4JlUmChPv0cKgikd+9s61ukzMaM0oaJvNCrnX1ucEo3CRbj42yEF0/jgwhkvneN8 CtiYQ++5kHe8NxT0GelcIf8mYle62pZA5s8lFR6gwUQKuCuY2Wp9YjErxHU/ePG33jtp K4Fe9zGTGV5Ed4EgoXMe3Sx+owKhliLADDtvnitjzV+LYm7pWckUm9w6FZL8kJ6PZOGq E3eJnqt52QQbrzAY6S44lfBBTN/pQmuuVlc6LESHkZTb0EI9Fh6VjRgZt/v6ktPIG5RP ZoI6yEkRAHKNX6tWrQP1xsz9QL7+2snBkPtjdnLCwX34eEwfJO4QBm9/VriSQeH6/vDo Fk2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=XcKOx6X1xb0tdZjWwv0Dj2TEaA6LrjG3UmiZoBUklqY=; b=taSyRCWC/ZUjouXQtK+Lw7t7fo2RxKW0ORqWMTuuTMY3p3cbs5qri3hRbjkgDpYqez uUPICPjE2ZWiq8Gew4Xik5E8hVeT6S9JIKTPjHJF9Yr9Ta9+xXSV2y6g8aoe8UqkHo8E bqE1oBwOyeMv4BJVfKLGMW19cAe3ZMdTBCdIIv+ZPmUqaGAGdUUTttYoy+iiS8R/f02W ee+rBa6gFwIxoJiQIibTrX26l07iHc1e/SGna1nnRcD3wNL25whl5yCHlpcd0BbpRA9u x1OdO8oQLeZHIJcPZeo5n54MF149owxOJNoc4Sy1LoRpkyUpnGKCaMcAdRcOuz8V+qpL 3q4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=VsFzxHaf; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j18si20446401otq.275.2020.01.19.22.17.20; Sun, 19 Jan 2020 22:17:33 -0800 (PST) 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=@gmail.com header.s=20161025 header.b=VsFzxHaf; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726775AbgATGO7 (ORCPT + 99 others); Mon, 20 Jan 2020 01:14:59 -0500 Received: from mail-oi1-f194.google.com ([209.85.167.194]:34746 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725783AbgATGO6 (ORCPT ); Mon, 20 Jan 2020 01:14:58 -0500 Received: by mail-oi1-f194.google.com with SMTP id l136so27605821oig.1 for ; Sun, 19 Jan 2020 22:14:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=XcKOx6X1xb0tdZjWwv0Dj2TEaA6LrjG3UmiZoBUklqY=; b=VsFzxHafM8zW3iEWuRTvUi1/iDuY2LLPXCOdVOn9fu3RYvyZrx3A9HdnfJHO4hhjg7 gFrWlJR0uVZawSfI6K7S4LuqoggVf0IXmVI7CN6EfxzaHttD8owD6XGleTOWkqz2gr4q 7e0wLddvbhZTfSFj6uCEcD06kntvPLESOSnwHxHHJhOL+6/k+PT5OqPOV8sTNeINTl8i uy2CJJ1AEr+pqQa2tUHifhlOp0uaKpJGL2HAAvT9suiHQi75Pu5I6+t6o6QiaJ9vlV3r JL999uvprvZ81dOdxYuyzU+/dBNKfwHfNBthDeqgCaYUv7NGNi9fOlcd5nq7/RysS5YZ I1yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=XcKOx6X1xb0tdZjWwv0Dj2TEaA6LrjG3UmiZoBUklqY=; b=HJTtjNwfCYpTelVHiv8nQ6ipnUggGZsGgOt61XPmoNcjMAWHc/OVNnvgfysHLn/un9 yGiTBXCEU6DypXgbHDxKapIJNCoSZsv4NmGlRegzmgIVI4qTT/U+35PMpnHO87bMinpi 8qLc/xws7WMoLWyWav84D10FM9WNZAarhrGoM5L2raM2aHJ4y1icFDWCfHQb4ax38O/b kGmIQ1B3Eax1vvBnumW/N6fAYowA4tMNx0mVZ3h3ePWtrJ+iNC7Qbz+cw1gE/h8rObKq soH/GWExcOLJzlSdKNbakGdHQGxs/WpDkOdStQEWtq5xGPHxmmGZee5x3deRgmqNOKfe vjRg== X-Gm-Message-State: APjAAAWcvgJocNFBRyxDzTqWwG7uBH6zBMZHhmd+tUIvjM+MnGuLDYqp 1Qq4jZAOBuTiv95zDT3evvaWsLctZEYSQPpXFMJQdg== X-Received: by 2002:aca:1913:: with SMTP id l19mr11221306oii.47.1579500897580; Sun, 19 Jan 2020 22:14:57 -0800 (PST) MIME-Version: 1.0 References: <1578876627-11938-1-git-send-email-wanpengli@tencent.com> In-Reply-To: <1578876627-11938-1-git-send-email-wanpengli@tencent.com> From: Wanpeng Li Date: Mon, 20 Jan 2020 14:14:46 +0800 Message-ID: Subject: Re: [PATCH RESEND v2] sched/nohz: Optimize get_nohz_timer_target() To: LKML Cc: Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Frederic Weisbecker Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ping Thomas, On Mon, 13 Jan 2020 at 08:50, Wanpeng Li wrote: > > From: Wanpeng Li > > On a machine, cpu 0 is used for housekeeping, the other 39 cpus in the > same socket 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. In addition, the last iterated busy > housekeeper can become a random candidate while current CPU is a better > fallback if it is a housekeeper. > > Reviewed-by: Frederic Weisbecker > Cc: Thomas Gleixner > Cc: Ingo Molnar > Cc: Peter Zijlstra > Cc: Frederic Weisbecker > Signed-off-by: Wanpeng Li > --- > v1 -> v2: > * current CPU is a better fallback if it is a housekeeper > > kernel/sched/core.c | 19 ++++++++++++------- > 1 file changed, 12 insertions(+), 7 deletions(-) > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 102dfcf..04a0f6a 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; > -- > 1.8.3.1 >