Received: by 10.192.165.148 with SMTP id m20csp3530332imm; Mon, 7 May 2018 14:02:02 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqq/KV2Rc/JG7sniF5FsiE2RHq16g3CpySI5euoxd+EuEZhk23maEQqaVul5GLqLVjkOaiC X-Received: by 2002:a17:902:7685:: with SMTP id m5-v6mr7480061pll.340.1525726922891; Mon, 07 May 2018 14:02:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525726922; cv=none; d=google.com; s=arc-20160816; b=bD1nz2nyeGbSueaD7Zqgj7d3dmWujau2V/UTDAbMCA7GZ/hXDvwX+33KiLhT7wrPPS 1NvLVVgH2aLJvnz67xrWv2g7XapsS6jpaRzRifIVoGtBg2bj7KkwwXH04KeJqQ2Ewhcb xoVWMpWAykDtbnWz98jZGWXUxcELbpydm19Fa4t2y5K17JvUd7fGyVvFlvBOb++YTuZK 6F8tS3a6b6wjTo+ztGyrTxm/oW7vWjd4QJ5KTgH/0uYyr4LR8RMwDeuksoElUZcmi6kL +KubZIU+yqG48JYLZSSevTasX0xwPAlrBG4zWOtroywAP4GwOo7A1Vvi2aSeTBH+MP+9 wl6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Q9tf/aX05pddBBcSMfINSla3wt3pXRBjeLsf3chgvp0=; b=l/92JKfEKZSSQW2vcEYJN8MmCorlngXTNeJnK/o7pAVBbtiy9w9xYo62YWCxhwGfYH saMVZTU2W83/wb2pQshXm7w4sGG1EIldIAH+XFTJEAZtnvQtpPDTUmSTDUWiMoZvKEpi TbBpa59A8S6IvFE2rUl/uGA+QdoI2/UCZuxSUL2NMG+1OqmdVVCtQOnwnjgRzy75PVeI W6LIZqTsFTDGXq7zjFh1xf6b2O0Jq3AfXrnizG+IQbGKKxM+QiiO+S2DTGAn7obRh1lI /aQCdciGJ1Y/+unEEHiFHaHV6v5JoeZy6zE7yCLiPo5mJR0CiAD4FcTLsfKKb2thzSS7 Lviw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@cmpxchg.org header.s=x header.b=RmO+/FrV; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 14-v6si22363534ple.450.2018.05.07.14.01.48; Mon, 07 May 2018 14:02:02 -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=fail header.i=@cmpxchg.org header.s=x header.b=RmO+/FrV; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753427AbeEGVA6 (ORCPT + 99 others); Mon, 7 May 2018 17:00:58 -0400 Received: from gum.cmpxchg.org ([85.214.110.215]:49552 "EHLO gum.cmpxchg.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753327AbeEGVAP (ORCPT ); Mon, 7 May 2018 17:00:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=cmpxchg.org ; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender: Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Q9tf/aX05pddBBcSMfINSla3wt3pXRBjeLsf3chgvp0=; b=RmO+/FrVyVnL4yCOTItqEvd/iQ TGGGSTEsRQ1CxTOcEQiZ3iirslPVfEJ7qRa4kuQrHpSJNkYyvmfB+aPVMuLJ1up072cko4HSeeRKb 4BFouTtFYiN1UEWLsvwNwkfgi6de3ORl416bznDbdKbHd0diyBd4GuS59OU94E4trpyY=; From: Johannes Weiner To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-block@vger.kernel.org, cgroups@vger.kernel.org Cc: Ingo Molnar , Peter Zijlstra , Andrew Morton , Tejun Heo , Balbir Singh , Mike Galbraith , Oliver Yang , Shakeel Butt , xxx xxx , Taras Kondratiuk , Daniel Walker , Vinayak Menon , Ruslan Ruslichenko , kernel-team@fb.com Subject: [PATCH 5/7] sched: loadavg: make calc_load_n() public Date: Mon, 7 May 2018 17:01:33 -0400 Message-Id: <20180507210135.1823-6-hannes@cmpxchg.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180507210135.1823-1-hannes@cmpxchg.org> References: <20180507210135.1823-1-hannes@cmpxchg.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It's going to be used in the following patch. Keep the churn separate. Signed-off-by: Johannes Weiner --- include/linux/sched/loadavg.h | 69 +++++++++++++++++++++++++++++++++++ kernel/sched/loadavg.c | 69 ----------------------------------- 2 files changed, 69 insertions(+), 69 deletions(-) diff --git a/include/linux/sched/loadavg.h b/include/linux/sched/loadavg.h index cc9cc62bb1f8..0e4c24978751 100644 --- a/include/linux/sched/loadavg.h +++ b/include/linux/sched/loadavg.h @@ -37,6 +37,75 @@ calc_load(unsigned long load, unsigned long exp, unsigned long active) return newload / FIXED_1; } +/** + * fixed_power_int - compute: x^n, in O(log n) time + * + * @x: base of the power + * @frac_bits: fractional bits of @x + * @n: power to raise @x to. + * + * By exploiting the relation between the definition of the natural power + * function: x^n := x*x*...*x (x multiplied by itself for n times), and + * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i, + * (where: n_i \elem {0, 1}, the binary vector representing n), + * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is + * of course trivially computable in O(log_2 n), the length of our binary + * vector. + */ +static inline unsigned long +fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n) +{ + unsigned long result = 1UL << frac_bits; + + if (n) { + for (;;) { + if (n & 1) { + result *= x; + result += 1UL << (frac_bits - 1); + result >>= frac_bits; + } + n >>= 1; + if (!n) + break; + x *= x; + x += 1UL << (frac_bits - 1); + x >>= frac_bits; + } + } + + return result; +} + +/* + * a1 = a0 * e + a * (1 - e) + * + * a2 = a1 * e + a * (1 - e) + * = (a0 * e + a * (1 - e)) * e + a * (1 - e) + * = a0 * e^2 + a * (1 - e) * (1 + e) + * + * a3 = a2 * e + a * (1 - e) + * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e) + * = a0 * e^3 + a * (1 - e) * (1 + e + e^2) + * + * ... + * + * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1] + * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e) + * = a0 * e^n + a * (1 - e^n) + * + * [1] application of the geometric series: + * + * n 1 - x^(n+1) + * S_n := \Sum x^i = ------------- + * i=0 1 - x + */ +static inline unsigned long +calc_load_n(unsigned long load, unsigned long exp, + unsigned long active, unsigned int n) +{ + return calc_load(load, fixed_power_int(exp, FSHIFT, n), active); +} + #define LOAD_INT(x) ((x) >> FSHIFT) #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) diff --git a/kernel/sched/loadavg.c b/kernel/sched/loadavg.c index 54fbdfb2d86c..0736e349a54e 100644 --- a/kernel/sched/loadavg.c +++ b/kernel/sched/loadavg.c @@ -210,75 +210,6 @@ static long calc_load_nohz_fold(void) return delta; } -/** - * fixed_power_int - compute: x^n, in O(log n) time - * - * @x: base of the power - * @frac_bits: fractional bits of @x - * @n: power to raise @x to. - * - * By exploiting the relation between the definition of the natural power - * function: x^n := x*x*...*x (x multiplied by itself for n times), and - * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i, - * (where: n_i \elem {0, 1}, the binary vector representing n), - * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is - * of course trivially computable in O(log_2 n), the length of our binary - * vector. - */ -static unsigned long -fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n) -{ - unsigned long result = 1UL << frac_bits; - - if (n) { - for (;;) { - if (n & 1) { - result *= x; - result += 1UL << (frac_bits - 1); - result >>= frac_bits; - } - n >>= 1; - if (!n) - break; - x *= x; - x += 1UL << (frac_bits - 1); - x >>= frac_bits; - } - } - - return result; -} - -/* - * a1 = a0 * e + a * (1 - e) - * - * a2 = a1 * e + a * (1 - e) - * = (a0 * e + a * (1 - e)) * e + a * (1 - e) - * = a0 * e^2 + a * (1 - e) * (1 + e) - * - * a3 = a2 * e + a * (1 - e) - * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e) - * = a0 * e^3 + a * (1 - e) * (1 + e + e^2) - * - * ... - * - * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1] - * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e) - * = a0 * e^n + a * (1 - e^n) - * - * [1] application of the geometric series: - * - * n 1 - x^(n+1) - * S_n := \Sum x^i = ------------- - * i=0 1 - x - */ -static unsigned long -calc_load_n(unsigned long load, unsigned long exp, - unsigned long active, unsigned int n) -{ - return calc_load(load, fixed_power_int(exp, FSHIFT, n), active); -} - /* * NO_HZ can leave us missing all per-CPU ticks calling * calc_load_fold_active(), but since a NO_HZ CPU folds its delta into -- 2.17.0