From patchwork Mon Nov 23 13:06:41 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: plongepe X-Patchwork-Id: 7681021 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 2B7319F1BE for ; Mon, 23 Nov 2015 13:06:29 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 46BB120268 for ; Mon, 23 Nov 2015 13:06:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 509A9206D6 for ; Mon, 23 Nov 2015 13:06:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753920AbbKWNGX (ORCPT ); Mon, 23 Nov 2015 08:06:23 -0500 Received: from mga09.intel.com ([134.134.136.24]:14848 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753909AbbKWNGV (ORCPT ); Mon, 23 Nov 2015 08:06:21 -0500 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP; 23 Nov 2015 05:05:57 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,337,1444719600"; d="scan'208";a="857572123" Received: from tllab185.tl.intel.com ([10.102.161.63]) by orsmga002.jf.intel.com with ESMTP; 23 Nov 2015 05:05:56 -0800 From: Philippe Longepe To: linux-pm@vger.kernel.org Cc: srinivas.pandruvada@linux.intel.com, Philippe Longepe , Stephane Gasparini Subject: [PATCH v1 3/5] cpufreq: intel_pstate: account for non C0 time Date: Mon, 23 Nov 2015 14:06:41 +0100 Message-Id: <1448284006-13596-7-git-send-email-philippe.longepe@linux.intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1448284006-13596-1-git-send-email-philippe.longepe@linux.intel.com> References: <1448284006-13596-1-git-send-email-philippe.longepe@linux.intel.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Philippe Longepe Aperf and Mperf counter are not enough to determine the Target P-state because they measure performance only when the targeted processor is in the C0 state (active state). Because of that, we were computing the average P-state during the last period which can be very different from the average frequency (or percentage of performance). As defined in the SDM (section 14.2), the PercentPerformance is defined by: PercentPerformance = PercentBusy * (delta_aperf / delta_mperf); The PercentBusy (or load) can be estimated as the ratio of the mperf counter running at a constant frequency only during active periods (C0) and the time stamp counter running at the same frequency but also during idle. So, PercentBusy = 100 * (delta_mperf / delta_tsc) and, PercentPerformance = 100 * (delta_mperf / delta_tsc) * (delta_aperf / delta_mperf) That can be simplified with: PercentPerformance = 100 * (delta_aperf / delta_tsc) Signed-off-by: Philippe Longepe Signed-off-by: Stephane Gasparini --- drivers/cpufreq/intel_pstate.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/drivers/cpufreq/intel_pstate.c b/drivers/cpufreq/intel_pstate.c index fa27ec5..f9acfb0 100644 --- a/drivers/cpufreq/intel_pstate.c +++ b/drivers/cpufreq/intel_pstate.c @@ -66,6 +66,7 @@ static inline int ceiling_fp(int32_t x) struct sample { int32_t core_pct_perf; + int32_t cpu_load; u64 aperf; u64 mperf; u64 tsc; @@ -922,6 +923,43 @@ static inline void intel_pstate_set_sample_time(struct cpudata *cpu) mod_timer_pinned(&cpu->timer, jiffies + delay); } + +static inline int32_t intel_pstate_calc_scaled_busy(struct cpudata *cpu) +{ + struct sample *sample = &cpu->sample; + struct pstate_data *pstate = &cpu->pstate; + int64_t core_busy_ratio; + + /* + * The load can be estimated as the ratio of the mperf counter + * running at a constant frequency only during active periods + * (C0) and the time stamp counter running at the same frequency + * also during C-states. + */ + sample->cpu_load = div64_u64(100 * sample->mperf, sample->tsc); + + /* + * The target P-state can be estimated with the following formula: + * PercentPerformance = PercentBusy * (delta_aperf/delta_mperf); + * (see Section 14.2 from Intel Software Developer Manual) + * with PercentBusy = 100 * (delta_mperf / delta_tsc) and + * PercentPerformance can be simplified with: + * (delta_mperf * delta_aperf) / (delta_tsc * delta_mperf) = + * delta_aperf / delta_tsc. Finally, we normalize core_busy_ratio, + * which was our actual percent performance to what we requested + * during the last sample period. The result will be a percentage of + * busy at a specified pstate. + */ + core_busy_ratio = div64_u64(int_tofp(100) * sample->aperf * + pstate->max_pstate, sample->tsc * pstate->current_pstate); + + sample->freq = div64_u64(sample->aperf * pstate->max_pstate * + pstate->scaling, sample->mperf); + + return core_busy_ratio; +} + + static inline int32_t intel_pstate_get_scaled_busy_estimate(struct cpudata *cpu) { int32_t core_busy, max_pstate, current_pstate, sample_ratio; @@ -974,7 +1012,7 @@ static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu) from = cpu->pstate.current_pstate; pid = &cpu->pid; - busy_scaled = intel_pstate_get_scaled_busy_estimate(cpu); + busy_scaled = intel_pstate_calc_scaled_busy(cpu); ctl = pid_calc(pid, busy_scaled);