From patchwork Tue Nov 3 09:27:19 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: plongepe X-Patchwork-Id: 7541021 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 B2B819F399 for ; Tue, 3 Nov 2015 09:33:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A165E20953 for ; Tue, 3 Nov 2015 09:33:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 726642094E for ; Tue, 3 Nov 2015 09:33:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751610AbbKCJ02 (ORCPT ); Tue, 3 Nov 2015 04:26:28 -0500 Received: from mga02.intel.com ([134.134.136.20]:28484 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751112AbbKCJ00 (ORCPT ); Tue, 3 Nov 2015 04:26:26 -0500 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP; 03 Nov 2015 01:26:25 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,238,1444719600"; d="scan'208";a="810475499" Received: from tllab185.tl.intel.com ([10.102.161.157]) by orsmga001.jf.intel.com with ESMTP; 03 Nov 2015 01:26:25 -0800 From: Philippe Longepe To: linux-pm@vger.kernel.org Cc: srinivas.pandruvada@linux.intel.com, Stephane Gasparini Subject: [PATCH v1 1/2] intel_pstate: Use the cpu load to determine the PercentPerformance Date: Tue, 3 Nov 2015 10:27:19 +0100 Message-Id: <1446542840-14982-1-git-send-email-philippe.longepe@linux.intel.com> X-Mailer: git-send-email 1.9.1 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_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 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 | 87 +++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 56 deletions(-) diff --git a/drivers/cpufreq/intel_pstate.c b/drivers/cpufreq/intel_pstate.c index 93a3c63..421903f 100644 --- a/drivers/cpufreq/intel_pstate.c +++ b/drivers/cpufreq/intel_pstate.c @@ -69,7 +69,7 @@ static inline int ceiling_fp(int32_t x) } struct sample { - int32_t core_pct_busy; + int32_t cpu_load; u64 aperf; u64 mperf; u64 tsc; @@ -993,21 +993,39 @@ static void intel_pstate_get_cpu_pstates(struct cpudata *cpu) intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate, false); } -static inline void intel_pstate_calc_busy(struct cpudata *cpu) +static inline int32_t intel_pstate_calc_busy(struct cpudata *cpu) { struct sample *sample = &cpu->sample; - int64_t core_pct; + struct pstate_data *pstate = &cpu->pstate; + int64_t core_busy_ratio; - core_pct = int_tofp(sample->aperf) * int_tofp(100); - core_pct = div64_u64(core_pct, int_tofp(sample->mperf)); + /* + * 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 = fp_toint( - mul_fp(int_tofp( - cpu->pstate.max_pstate_physical * - cpu->pstate.scaling / 100), - core_pct)); + sample->freq = div64_u64(sample->aperf * pstate->max_pstate * + pstate->scaling, sample->mperf); - sample->core_pct_busy = (int32_t)core_pct; + return core_busy_ratio; } static inline void intel_pstate_sample(struct cpudata *cpu) @@ -1036,8 +1054,6 @@ static inline void intel_pstate_sample(struct cpudata *cpu) cpu->sample.mperf -= cpu->prev_mperf; cpu->sample.tsc -= cpu->prev_tsc; - intel_pstate_calc_busy(cpu); - cpu->prev_aperf = aperf; cpu->prev_mperf = mperf; cpu->prev_tsc = tsc; @@ -1059,47 +1075,6 @@ static inline void intel_pstate_set_sample_time(struct cpudata *cpu) mod_timer_pinned(&cpu->timer, jiffies + delay); } -static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu) -{ - int32_t core_busy, max_pstate, current_pstate, sample_ratio; - s64 duration_us; - u32 sample_time; - - /* - * core_busy is the ratio of actual performance to max - * max_pstate is the max non turbo pstate available - * current_pstate was the pstate that was requested during - * the last sample period. - * - * We normalize core_busy, 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 = cpu->sample.core_pct_busy; - max_pstate = int_tofp(cpu->pstate.max_pstate_physical); - current_pstate = int_tofp(cpu->pstate.current_pstate); - core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate)); - - /* - * Since we have a deferred timer, it will not fire unless - * we are in C0. So, determine if the actual elapsed time - * is significantly greater (3x) than our sample interval. If it - * is, then we were idle for a long enough period of time - * to adjust our busyness. - */ - sample_time = pid_params.sample_rate_ms * USEC_PER_MSEC; - duration_us = ktime_us_delta(cpu->sample.time, - cpu->last_sample_time); - if (duration_us > sample_time * 3) { - sample_ratio = div_fp(int_tofp(sample_time), - int_tofp(duration_us)); - core_busy = mul_fp(core_busy, sample_ratio); - } - - return core_busy; -} - static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu) { int32_t busy_scaled; @@ -1111,7 +1086,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(cpu); + busy_scaled = intel_pstate_calc_busy(cpu); ctl = pid_calc(pid, busy_scaled); @@ -1119,7 +1094,7 @@ static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu) intel_pstate_set_pstate(cpu, cpu->pstate.current_pstate - ctl, true); sample = &cpu->sample; - trace_pstate_sample(fp_toint(sample->core_pct_busy), + trace_pstate_sample(fp_toint(busy_scaled), fp_toint(busy_scaled), from, cpu->pstate.current_pstate,