Skip to contentSkip to conversation

Reduce the intel_pstate timer overhead

PATCH Superseded Philippe Longepe 2 participants 9 messages Mar 6, 2016 – Mar 7, 2016

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Reduce the intel_pstate timer overhead

This serie includes the following code optimizations:

Patch 1: Remove extra conversions in pid calculation
Patch 2: Optimize calculation for max/min_perf_adj
Patch 3: Move the intel_pstate_calc_busy into get_target_pstate_use_performance Patch 4: Remove the freq calculation from the intel_pstate_calc_busy function

Philippe Longepe (4):
Remove extra conversions in pid calculation
Optimize calculation for max/min_perf_adj
Move the intel_pstate_calc_busy into get_target_pstate_use_performance
intel_pstate: Remove the freq calculation from the
intel_pstate_calc_busy function

 drivers/cpufreq/intel_pstate.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v5 1/4 Remove extra conversions in pid calculation

pid->setpoint and pid->deadband can be initialize in float so we can remove the int_tofp in pid_calc.

---
 drivers/cpufreq/intel_pstate.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/cpufreq/intel_pstate.c +4 -4
index eb0aef0..114e4e0 100644
@@ -198,8 +198,8 @@ static struct perf_limits *limits = &powersave_limits;
 
 static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
 			     int deadband, int integral) {
-	pid->setpoint = setpoint;
-	pid->deadband  = deadband;
+	pid->setpoint = int_tofp(setpoint);
+	pid->deadband  = int_tofp(deadband);
 	pid->integral  = int_tofp(integral);
 	pid->last_err  = int_tofp(setpoint) - int_tofp(busy);
 }
@@ -225,9 +225,9 @@ static signed int pid_calc(struct _pid *pid, int32_t busy)
 	int32_t pterm, dterm, fp_error;
 	int32_t integral_limit;
 
-	fp_error = int_tofp(pid->setpoint) - busy;
+	fp_error = pid->setpoint - busy;
 
-	if (abs(fp_error) <= int_tofp(pid->deadband))
+	if (abs(fp_error) <= pid->deadband)
 		return 0;
 
 	pterm = mul_fp(pid->p_gain, fp_error);
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH V5 1/4] Remove extra conversions in pid calculation

Quoted 2 lines · Philippe Longepe

pid->setpoint and pid->deadband can be initialize in float so we can remove the int_tofp in pid_calc.

Rafael had some comments on this patch:
"s/initialize/initialized/

Quoted 1 line

can remove the int_tofp in pid_calc.

This is not "float", but "fixed point".

Also "avoid" rather than "remove".
"
Not sure if Rafael is OK without change.

Quoted 36 lines

---
 drivers/cpufreq/intel_pstate.c | 8 ++++----

 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/cpufreq/intel_pstate.c +0 -0
index eb0aef0..114e4e0 100644
@@ -198,8 +198,8 @@ static struct perf_limits *limits =
 &powersave_limits;


 static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
       int deadband, int integral) {

drivers/cpufreq/intel_pstate.c +2 -2
-	pid->setpoint = setpoint;
-	pid->deadband  = deadband;
+	pid->setpoint = int_tofp(setpoint);
+	pid->deadband  = int_tofp(deadband);

  pid->integral  = int_tofp(integral); pid->last_err  = int_tofp(setpoint) - int_tofp(busy); }

drivers/cpufreq/intel_pstate.c +0 -0
@@ -225,9 +225,9 @@ static signed int pid_calc(struct _pid *pid,
 int32_t busy)
  	int32_t pterm, dterm, fp_error;
  	int32_t integral_limit;

drivers/cpufreq/intel_pstate.c +1 -1
-	fp_error = int_tofp(pid->setpoint) - busy;
+	fp_error = pid->setpoint - busy;

drivers/cpufreq/intel_pstate.c +1 -1
-	if (abs(fp_error) <= int_tofp(pid->deadband))
+	if (abs(fp_error) <= pid->deadband)

  return 0;

  pterm = mul_fp(pid->p_gain, fp_error);

#3 lore raw

PATCH v5 2/4 Optimize calculation for max/min_perf_adj

In reply to Philippe Longepe

mul_fp(int_tofp(A), B) expands to:
((A << FRAC_BITS) * B) >> FRAC_BITS, so the same result can be obtained via simple multiplication A * B. Apply this observation to max_perf * limits->max_perf and max_perf * limits->min_perf in intel_pstate_get_min_max()."

---
 drivers/cpufreq/intel_pstate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cpufreq/intel_pstate.c +2 -2
index 114e4e0..c46d23a 100644
@@ -828,11 +828,11 @@ static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
 	 * policy, or by cpu specific default values determined through
 	 * experimentation.
 	 */
-	max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits->max_perf));
+	max_perf_adj = fp_toint(max_perf * limits->max_perf);
 	*max = clamp_t(int, max_perf_adj,
 			cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
 
-	min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits->min_perf));
+	min_perf = fp_toint(max_perf * limits->min_perf);
 	*min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
 }
 
Signature · 1 line
1.9.1
maintainer Acked-by on v5 2/4
#4 lore raw

Re: [PATCH V5 2/4] Optimize calculation for max/min_perf_adj

Quoted 8 lines · Philippe Longepe

mul_fp(int_tofp(A), B) expands to:
((A << FRAC_BITS) * B) >> FRAC_BITS, so the same result can be obtained
via simple multiplication A * B.  Apply this observation to max_perf * limits->max_perf and max_perf * limits->min_perf in intel_pstate_get_min_max()."

Quoted 29 lines

---
 drivers/cpufreq/intel_pstate.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cpufreq/intel_pstate.c +0 -0
index 114e4e0..c46d23a 100644
@@ -828,11 +828,11 @@ static void intel_pstate_get_min_max(struct
 cpudata *cpu, int *min, int *max)
  	 * policy, or by cpu specific default values determined

through
   * experimentation.
   */

drivers/cpufreq/intel_pstate.c +0 -1
-	max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits-
Quoted 1 line

max_perf));

drivers/cpufreq/intel_pstate.c +1 -0
+	max_perf_adj = fp_toint(max_perf * limits->max_perf);

  *max = clamp_t(int, max_perf_adj, cpu->pstate.min_pstate, cpu-

Quoted 1 line

pstate.turbo_pstate);

drivers/cpufreq/intel_pstate.c +0 -1
-	min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits-
Quoted 1 line

min_perf));

drivers/cpufreq/intel_pstate.c +1 -0
+	min_perf = fp_toint(max_perf * limits->min_perf);

  *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
 }

#5 lore raw

PATCH v5 3/4 Move the intel_pstate_calc_busy into get_target_pstate_use_performance

In reply to Philippe Longepe

The cpu_load algorithm doesn't need to invoke intel_pstate_calc_busy(), so move that call from intel_pstate_sample() to get_target_pstate_use_performance().

---
 drivers/cpufreq/intel_pstate.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/cpufreq/intel_pstate.c +2 -3
index c46d23a..903341f 100644
@@ -912,8 +912,6 @@ static inline void intel_pstate_sample(struct cpudata *cpu, u64 time)
 	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;
@@ -942,7 +940,6 @@ static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
 	mperf = cpu->sample.mperf + delta_iowait_mperf;
 	cpu->prev_cummulative_iowait = cummulative_iowait;
 
-
 	/*
 	 * The load can be estimated as the ratio of the mperf counter
 	 * running at a constant frequency during active periods
@@ -960,6 +957,8 @@ static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
 	int32_t core_busy, max_pstate, current_pstate, sample_ratio;
 	u64 duration_ns;
 
+	intel_pstate_calc_busy(cpu);
+
 	/*
 	 * core_busy is the ratio of actual performance to max
 	 * max_pstate is the max non turbo pstate available
Signature · 1 line
1.9.1
maintainer Acked-by on v5 3/4
#6 lore raw

Re: [PATCH V5 3/4] Move the intel_pstate_calc_busy into get_target_pstate_use_performance

Quoted 6 lines · Philippe Longepe

The cpu_load algorithm doesn't need to invoke
intel_pstate_calc_busy(),
so move that call from intel_pstate_sample() to
get_target_pstate_use_performance().

Quoted 39 lines

---
 drivers/cpufreq/intel_pstate.c | 5 ++---

 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/cpufreq/intel_pstate.c +0 -0
index c46d23a..903341f 100644
@@ -912,8 +912,6 @@ static inline void intel_pstate_sample(struct
 cpudata *cpu, u64 time)
  	cpu->sample.mperf -= cpu->prev_mperf;
  	cpu->sample.tsc -= cpu->prev_tsc;

drivers/cpufreq/intel_pstate.c +0 -2
-	intel_pstate_calc_busy(cpu);
-

  cpu->prev_aperf = aperf;
  cpu->prev_mperf = mperf;
  cpu->prev_tsc = tsc;

drivers/cpufreq/intel_pstate.c +0 -0
@@ -942,7 +940,6 @@ static inline int32_t
 get_target_pstate_use_cpu_load(struct cpudata *cpu)
  	mperf = cpu->sample.mperf + delta_iowait_mperf;
  	cpu->prev_cummulative_iowait = cummulative_iowait;


-
  /*
   * The load can be estimated as the ratio of the mperf counter
   * running at a constant frequency during active periods

drivers/cpufreq/intel_pstate.c +0 -0
@@ -960,6 +957,8 @@ static inline int32_t
 get_target_pstate_use_performance(struct cpudata *cpu)
  	int32_t core_busy, max_pstate, current_pstate, sample_ratio;
  	u64 duration_ns;

drivers/cpufreq/intel_pstate.c +2 -0
+	intel_pstate_calc_busy(cpu);
+

  /*
   * core_busy is the ratio of actual performance to max
   * max_pstate is the max non turbo pstate available

#7 lore raw

PATCH v5 4/4 intel_pstate: Remove the freq calculation from the intel_pstate_calc_busy function

In reply to Philippe Longepe

Use a helper function to compute the average pstate and call it only where it is needed (only when tracing or in intel_pstate_get).

---
 drivers/cpufreq/intel_pstate.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/cpufreq/intel_pstate.c +8 -8
index 903341f..6e07366 100644
@@ -878,12 +878,6 @@ static inline void intel_pstate_calc_busy(struct cpudata *cpu)
 	core_pct = int_tofp(sample->aperf) * int_tofp(100);
 	core_pct = div64_u64(core_pct, int_tofp(sample->mperf));
 
-	sample->freq = fp_toint(
-		mul_fp(int_tofp(
-			cpu->pstate.max_pstate_physical *
-			cpu->pstate.scaling / 100),
-			core_pct));
-
 	sample->core_pct_busy = (int32_t)core_pct;
 }
 
@@ -917,6 +911,12 @@ static inline void intel_pstate_sample(struct cpudata *cpu, u64 time)
 	cpu->prev_tsc = tsc;
 }
 
+static inline int32_t get_avg_frequency(struct cpudata *cpu)
+{
+	return div64_u64(cpu->pstate.max_pstate_physical * cpu->sample.aperf *
+		cpu->pstate.scaling, cpu->sample.mperf);
+}
+
 static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
 {
 	struct sample *sample = &cpu->sample;
@@ -1012,7 +1012,7 @@ static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
 		sample->mperf,
 		sample->aperf,
 		sample->tsc,
-		sample->freq);
+		get_avg_frequency(cpu));
 }
 
 static void intel_pstate_update_util(struct update_util_data *data, u64 time,
@@ -1101,7 +1101,7 @@ static unsigned int intel_pstate_get(unsigned int cpu_num)
 	if (!cpu)
 		return 0;
 	sample = &cpu->sample;
-	return sample->freq;
+	return get_avg_frequency(cpu);
 }
 
 static int intel_pstate_set_policy(struct cpufreq_policy *policy)
Signature · 1 line
1.9.1
maintainer Acked-by on v5 4/4
#8 lore raw

Re: [PATCH V5 4/4] intel_pstate: Remove the freq calculation from the intel_pstate_calc_busy function

Quoted 4 lines · Philippe Longepe

Use a helper function to compute the average pstate and call it only where it is needed (only when tracing or in intel_pstate_get).

Quoted 60 lines

---
 drivers/cpufreq/intel_pstate.c | 16 ++++++++--------

 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/cpufreq/intel_pstate.c +0 -0
index 903341f..6e07366 100644
@@ -878,12 +878,6 @@ static inline void intel_pstate_calc_busy(struct
 cpudata *cpu)
  	core_pct = int_tofp(sample->aperf) * int_tofp(100);
  	core_pct = div64_u64(core_pct, int_tofp(sample->mperf));

drivers/cpufreq/intel_pstate.c +0 -6
-	sample->freq = fp_toint(
-		mul_fp(int_tofp(
-			cpu->pstate.max_pstate_physical *
-			cpu->pstate.scaling / 100),
-			core_pct));
-

  sample->core_pct_busy = (int32_t)core_pct; }

drivers/cpufreq/intel_pstate.c +0 -0
@@ -917,6 +911,12 @@ static inline void intel_pstate_sample(struct
 cpudata *cpu, u64 time)
  	cpu->prev_tsc = tsc;
  }

drivers/cpufreq/intel_pstate.c +3 -0
+static inline int32_t get_avg_frequency(struct cpudata *cpu)
+{
+	return div64_u64(cpu->pstate.max_pstate_physical * cpu-
Quoted 1 line

sample.aperf *

drivers/cpufreq/intel_pstate.c +3 -0
+		cpu->pstate.scaling, cpu->sample.mperf);
+}
+

 static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
 {
  struct sample *sample = &cpu->sample;

drivers/cpufreq/intel_pstate.c +1 -1
@@ -1012,7 +1012,7 @@ static inline void
 intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
  		sample->mperf,
  		sample->aperf,
  		sample->tsc,
-		sample->freq);
+		get_avg_frequency(cpu));
  }


 static void intel_pstate_update_util(struct update_util_data *data, u64 time,

drivers/cpufreq/intel_pstate.c +1 -1
@@ -1101,7 +1101,7 @@ static unsigned int intel_pstate_get(unsigned
 int cpu_num)
  	if (!cpu)
  		return 0;
  	sample = &cpu->sample;
-	return sample->freq;
+	return get_avg_frequency(cpu);
  }


 static int intel_pstate_set_policy(struct cpufreq_policy *policy)