Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp168133pxf; Wed, 10 Mar 2021 03:28:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJzHfHRY/gATKv5XQRRNXdjszDkV43aGRnya1ChHU/yzMAdlsmUj4MoqDLWEjt1QM3U/NLbw X-Received: by 2002:a17:907:2b03:: with SMTP id gc3mr3159895ejc.448.1615375682580; Wed, 10 Mar 2021 03:28:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615375682; cv=none; d=google.com; s=arc-20160816; b=wgH4grxjt5y11ibKMz1mfCbqjWEutvbD3giacwxR74eExO7xaJVlawFo56MvU79hmE tm+fxEFLAF38/mCWkvX6avrc2hHzMwf7n5UmR50jCJZMSJiZ1NTZ2TLJf/hhpxSGrTSm LyX0TFzxdYMvDv4KVvv56vQkvMYLxcdmcLF56r8SfLZi0jI4Ejr4i3cIhz6KkRSAxDcj E1Z8Z6cD4T14pPP/ngNpXAp2uyGV11CGMhGWIRF5paSc8s81Ck/rNamHP/9P72AWH4z1 oQa9N8ZcoktuXW9WL3wPDiC2J4JspS589fJw2MOZ1tzRtEKotRe3RfZAQ3R49DqEkapF FKJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=HvrjFJMYhdryqo7+gfIubxeOurLBJ8nG/pBVN7R1ETM=; b=YQDXIB7dyFEkx8JoyyilOvj3PL1Bjam5dMaF/184LgmWPuBpUrP67Xw5Dua9q6Q4jJ OMyQxNh4SedGBGV4ykbHsu/+P5pFVDlntyuBe7UxgiqoBLvryF3KibA79Q3sW/gaFUA1 XLxgIUmfgP9TJHjfzU5wdR7RJHewvVU0OlH+LIZLWmtEQCrZjAzkCOovxT0VDA6EKt2g eu9iCgQ5KUIOVW/kIAFvNUUnu8e+FLzaBt2IrM/7oh8dMJ3W01CDpoZecta4bmR70xbd u4A9EbrpYuG4oHVcKNzKCc/n4+sE8dsVBL8sJQYxXvC6QaP6qJ7BGTR3Yu/bb3CGZaJ0 9UtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Z+PzM1zn; dkim=neutral (no key) header.i=@linutronix.de; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l12si11261949eds.123.2021.03.10.03.27.39; Wed, 10 Mar 2021 03:28:02 -0800 (PST) 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; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Z+PzM1zn; dkim=neutral (no key) header.i=@linutronix.de; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232776AbhCJL0q (ORCPT + 99 others); Wed, 10 Mar 2021 06:26:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232673AbhCJL0W (ORCPT ); Wed, 10 Mar 2021 06:26:22 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88592C06174A; Wed, 10 Mar 2021 03:26:22 -0800 (PST) Date: Wed, 10 Mar 2021 11:26:20 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1615375581; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HvrjFJMYhdryqo7+gfIubxeOurLBJ8nG/pBVN7R1ETM=; b=Z+PzM1znCYLOrLZjpWz85EteG6HVzTK8f1AzjY77IUnNr3Y7JdwYJzTgIQ3tQ01DZeJzhm 4YtDANaFJFr2AcBtNGJJ9TsPpg8vyIp1QInDxaEIn6gSoL4Mmtva2LMgu9HrEbFVfVJC7B wRlqQikkRLkBXaGtlG55ZDCwoeKHCbxqwBRPGgkXtCfK+UDBu3emwRW1NtjM/8SBzNO8jB s/tSlBEQN7+1w++7C/o8EmHJEkKcbbCJRcUbTxIroip6RB4HFDC5K0Kzy7UO3CTqQQ8E6r THAYkLTxRn7SKy7fWo5v70ElcVBGOM0elE8ebMaDTLjNRy6HSwgSr4UVjIVqPA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1615375581; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HvrjFJMYhdryqo7+gfIubxeOurLBJ8nG/pBVN7R1ETM=; b=Gw9QhcKFUfTQS8qFsbK1YmWe4Vpcu3L7dYxOfsiJZ6FZI+nkZy8Lj5OSBD2C7MP+N5b9LM /KN2gEyx1o/qVZAA== From: "tip-bot2 for Clement Courbet" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] sched: Optimize __calc_delta() Cc: Clement Courbet , Josh Don , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20210303224653.2579656-1-joshdon@google.com> References: <20210303224653.2579656-1-joshdon@google.com> MIME-Version: 1.0 Message-ID: <161537558053.398.12604635982582934903.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the sched/core branch of tip: Commit-ID: 1e17fb8edc5ad6587e9303ccdebce853bc8cf30c Gitweb: https://git.kernel.org/tip/1e17fb8edc5ad6587e9303ccdebce853bc8= cf30c Author: Clement Courbet AuthorDate: Wed, 03 Mar 2021 14:46:53 -08:00 Committer: Peter Zijlstra CommitterDate: Wed, 10 Mar 2021 09:51:49 +01:00 sched: Optimize __calc_delta() A significant portion of __calc_delta() time is spent in the loop shifting a u64 by 32 bits. Use `fls` instead of iterating. This is ~7x faster on benchmarks. The generic `fls` implementation (`generic_fls`) is still ~4x faster than the loop. Architectures that have a better implementation will make use of it. For example, on x86 we get an additional factor 2 in speed without dedicated implementation. On GCC, the asm versions of `fls` are about the same speed as the builtin. On Clang, the versions that use fls are more than twice as slow as the builtin. This is because the way the `fls` function is written, clang puts the value in memory: https://godbolt.org/z/EfMbYe. This bug is filed at https://bugs.llvm.org/show_bug.cgi?idI406. ``` name cpu/op BM_Calc<__calc_delta_loop> 9.57ms =C3=82=3DB112% BM_Calc<__calc_delta_generic_fls> 2.36ms =C3=82=3DB113% BM_Calc<__calc_delta_asm_fls> 2.45ms =C3=82=3DB113% BM_Calc<__calc_delta_asm_fls_nomem> 1.66ms =C3=82=3DB112% BM_Calc<__calc_delta_asm_fls64> 2.46ms =C3=82=3DB113% BM_Calc<__calc_delta_asm_fls64_nomem> 1.34ms =C3=82=3DB115% BM_Calc<__calc_delta_builtin> 1.32ms =C3=82=3DB111% ``` Signed-off-by: Clement Courbet Signed-off-by: Josh Don Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20210303224653.2579656-1-joshdon@google.com --- kernel/sched/fair.c | 19 +++++++++++-------- kernel/sched/sched.h | 1 + 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index f5d6541..2e2ab1e 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -229,22 +229,25 @@ static void __update_inv_weight(struct load_weight *lw) static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_we= ight *lw) { u64 fact =3D scale_load_down(weight); + u32 fact_hi =3D (u32)(fact >> 32); int shift =3D WMULT_SHIFT; + int fs; =20 __update_inv_weight(lw); =20 - if (unlikely(fact >> 32)) { - while (fact >> 32) { - fact >>=3D 1; - shift--; - } + if (unlikely(fact_hi)) { + fs =3D fls(fact_hi); + shift -=3D fs; + fact >>=3D fs; } =20 fact =3D mul_u32_u32(fact, lw->inv_weight); =20 - while (fact >> 32) { - fact >>=3D 1; - shift--; + fact_hi =3D (u32)(fact >> 32); + if (fact_hi) { + fs =3D fls(fact_hi); + shift -=3D fs; + fact >>=3D fs; } =20 return mul_u64_u32_shr(delta_exec, fact, shift); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index bb8bb06..d2e09a6 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -36,6 +36,7 @@ #include =20 #include +#include #include #include #include