From patchwork Thu Jul 12 17:29:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Weiner X-Patchwork-Id: 10522039 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3CEAF602C8 for ; Thu, 12 Jul 2018 17:27:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1CCF929AC1 for ; Thu, 12 Jul 2018 17:27:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1035029AD2; Thu, 12 Jul 2018 17:27:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7251829AC1 for ; Thu, 12 Jul 2018 17:27:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1AE416B026B; Thu, 12 Jul 2018 13:27:41 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 133D96B026D; Thu, 12 Jul 2018 13:27:41 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F16E36B026E; Thu, 12 Jul 2018 13:27:40 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id 89E236B026B for ; Thu, 12 Jul 2018 13:27:40 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id b12-v6so11118232edi.12 for ; Thu, 12 Jul 2018 10:27:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=7HJ/faUwNp70pvtX2vst2/bX0oNS92n5Rlox7XiLwSM=; b=SUIsy6HgS5UWVF3s6CJEeP9hteyePEqhHOq4waxJ2QXcIAcJbKbPce7uZV88VDRo9S ZBONUTFJu32aobc5cg62k1nrTxhoEIOQVS+h+pMYK0IHmGbYeCNYzG+9iwaJmgu9qp9O EDnXNKm+P1bYWf4QjD5YoSbNo8jP3DjEZUDamu+s00i0ScQzsqu+BsQ/u2eexE7ZOUvB c+qr5jgB7Cd5Odh+24haLfM/wq342MJzq7ly+H11l7UtZTfulj3M36AZgUfmxOmyEeNh 6HiZ2SXF7z5jJmWPuItpa6ZMT76tpKcr6TS8N13lkePeaLhWraVEmyVk5K6I1AGuDUSO CJxg== X-Gm-Message-State: AOUpUlEaat7F9ettXEKJLq3YiEcBieHs2ONiZqDqJE2IYcLv+rCIjMwz 3C+PWD6WapccnVU3pc7m0ApCMfsXtYFZhCd6EagQmMYc1F+VwZ1L6i0BUOILsxBErWrRIvgQMxE RxDezeR+A9cwKw6xpp/U3Z4D5Xe/PDAVaphYxqpOsu+IIMpVn8OM+CxyBkmUQ1mLjww== X-Received: by 2002:a50:a0c5:: with SMTP id 63-v6mr3411596edo.47.1531416460126; Thu, 12 Jul 2018 10:27:40 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfFEId+InBZrBhZzal0SBtEnjsJvplkfsD9lQOO3jKyijCXUlHezXiyW2Nj803Cp2GH9XDL X-Received: by 2002:a50:a0c5:: with SMTP id 63-v6mr3411554edo.47.1531416459246; Thu, 12 Jul 2018 10:27:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531416459; cv=none; d=google.com; s=arc-20160816; b=A8MVt8P6CnjfoM+fdtrnIodTLzpjFDqkZw6f3k5fNKRPNEAD3plVC4BzFL+PbJzzfN s7z//6ttYohN6eJPyjLHkco3c8UJOcMThPBVyTbjOwrnaa+QBWMP6boMu31GLYJwG0+i vf178aftTY4gwW2oh5aWYHY8M8AeRWFWk/C+KCfDTyhaMsgRjWBSppYSTXozn35OGp6H +aXvpjuw4nMm1TF5RYRcjhvAqxjjuunM427gWtYdpX5xZLfXdTnY+ueZqDqjw38EDu1x KERI7e0X2WQzUle+bipSi4o1XI44U2g5+fyOPciZG1L5MbdcNXHHVZl1JqmFDwTjSbzl x1cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=7HJ/faUwNp70pvtX2vst2/bX0oNS92n5Rlox7XiLwSM=; b=Wh3FOq3cs1Oce/bbzYjTF0pX2Ip6lv973C2ddgl64xpNq/u6sS2Ja3MLLjTU20Y/rA 0ZVQR2eXVITmuBPq9VaMibazWMOtxCd91/Ksmo7xAbKFHosW0oMm1H8vWAdLlap105ir oKVoDrrtbOLnae1xwiDwJiEPU4kDV116K/CwWBKQQFwEoNgJxyjkyrE7HH3nRz/BsvTz DfCU91R9PDWgMXun3cRzS8hel2z8NxC8Zua5jUB1KcMV6xC8NU6QLiRCtuoTURrzFYjZ 4C9T+zdaKgLyxYEf33j3QLgqwPPscGhDH/utB1yUsqov74L9+uSxTMQGekSLxvTeHfLU u/NQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg.org header.s=x header.b=I4CKegGk; spf=pass (google.com: domain of hannes@cmpxchg.org designates 85.214.110.215 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: from gum.cmpxchg.org (gum.cmpxchg.org. [85.214.110.215]) by mx.google.com with ESMTPS id p7-v6si248060edr.357.2018.07.12.10.27.39 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 12 Jul 2018 10:27:39 -0700 (PDT) Received-SPF: pass (google.com: domain of hannes@cmpxchg.org designates 85.214.110.215 as permitted sender) client-ip=85.214.110.215; Authentication-Results: mx.google.com; dkim=pass header.i=@cmpxchg.org header.s=x header.b=I4CKegGk; spf=pass (google.com: domain of hannes@cmpxchg.org designates 85.214.110.215 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org 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=7HJ/faUwNp70pvtX2vst2/bX0oNS92n5Rlox7XiLwSM=; b=I4CKegGkDcxrZsOqC1PuYMZy5h uu6hmqcVot67Tp3Qb3gLRE+pL7534XEEBBJtAWHsif7y2uTcQejMLqJwWmdeqXp/8ptIG7+Cm6fst ifbaiKSdY2IZzNZXRqwI3YhVv6tAZRzePDaXGQ7bW4zMv0PdLMggO9mTP1729GO7iTQA=; From: Johannes Weiner To: Ingo Molnar , Peter Zijlstra , Andrew Morton , Linus Torvalds Cc: Tejun Heo , Suren Baghdasaryan , Vinayak Menon , Christopher Lameter , Mike Galbraith , Shakeel Butt , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 05/10] sched: loadavg: make calc_load_n() public Date: Thu, 12 Jul 2018 13:29:37 -0400 Message-Id: <20180712172942.10094-6-hannes@cmpxchg.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180712172942.10094-1-hannes@cmpxchg.org> References: <20180712172942.10094-1-hannes@cmpxchg.org> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP It's going to be used in a later patch. Keep the churn separate. Signed-off-by: Johannes Weiner --- include/linux/sched/loadavg.h | 3 + kernel/sched/loadavg.c | 138 +++++++++++++++++----------------- 2 files changed, 72 insertions(+), 69 deletions(-) diff --git a/include/linux/sched/loadavg.h b/include/linux/sched/loadavg.h index cc9cc62bb1f8..4859bea47a7b 100644 --- a/include/linux/sched/loadavg.h +++ b/include/linux/sched/loadavg.h @@ -37,6 +37,9 @@ calc_load(unsigned long load, unsigned long exp, unsigned long active) return newload / FIXED_1; } +extern unsigned long calc_load_n(unsigned long load, unsigned long exp, + unsigned long active, unsigned int n); + #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..28a516575c18 100644 --- a/kernel/sched/loadavg.c +++ b/kernel/sched/loadavg.c @@ -91,6 +91,75 @@ long calc_load_fold_active(struct rq *this_rq, long adjust) 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 + */ +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); +} + #ifdef CONFIG_NO_HZ_COMMON /* * Handle NO_HZ for the global load-average. @@ -210,75 +279,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