Skip to contentSkip to conversation

cpufreq: Use sorted frequency tables

PATCH In review Viresh Kumar 2 participants 7 messages Jun 1, 2016 – Jun 3, 2016

Subsystems (2)
lore
Share & tools
lore

Viresh Kumar

author maintainer
#0 lore raw

v2 cover cpufreq: Use sorted frequency tables

Hi Guys,

This work in inspired by some of the concerns raised by Steve in one of his patchset.

Currently, the cpufreq drivers aren't required to provide a sorted list of frequencies to the cpufreq-core and so traversing that list match a target frequency is very inefficient.

This is not bearable by, for example, the fast-switch path of schedutil governor and so we have moved the traversing logic local to the acpi-cpufreq driver for now. That is better handled in the core, but it has to be efficient first.

OTOH, even for traditional governors without a fast-switch path, it would be much better to be able to traverse this table quickly.

The ideal solution would be to keep a single *sorted* freq-table in struct cpufreq_policy. But there are few dependencies due to which it can't be done today (Hint: cpufreq drivers are abusing the 'index' passed to them, to refer to multiple arrays).

And so for now, lets create a separate table local to the cpufreq-core *only*.

This modifies the existing API cpufreq_frequency_table_target() to use the sorted table.

Lightly tested on Exynos board, frequencies were getting selected as expected.

V1->V2:
- This optimizes cpufreq_frequency_table_target() instead of new APIs.
- Is rebased over bleeding-edge + following cleanup series. [PATCH 0/8] cpufreq: cleanups and reorganization

--
viresh

Viresh Kumar (2):
cpufreq: Store sorted frequency table
cpufreq: Optimize cpufreq_frequency_table_target()

 drivers/cpufreq/acpi-cpufreq.c    |  15 +--
 drivers/cpufreq/cpufreq.c         |  23 ++--
 drivers/cpufreq/freq_table.c      | 247 ++++++++++++++++++++++++++------------
 drivers/cpufreq/s3c24xx-cpufreq.c |  13 +-
 include/linux/cpufreq.h           |  23 +++-
 5 files changed, 220 insertions(+), 101 deletions(-)
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#1 lore raw

PATCH v2 1/2 cpufreq: Store sorted frequency table

The drivers aren't required to provide a sorted frequency table today, and its not optimal to work on an unsorted freq table.

To simplify and improve code performance, create a frequency table within core and keep it sorted in ascending order.

Note that this should eventually replace policy->freq_table itself, which isn't done currently as few drivers will break due to that.

---
 drivers/cpufreq/cpufreq.c    | 23 ++++++++++-----
 drivers/cpufreq/freq_table.c | 67 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/cpufreq.h      |  3 ++
 3 files changed, 86 insertions(+), 7 deletions(-)
drivers/cpufreq/cpufreq.c +16 -7
index 07c933c6c29a..799e03daa4a3 100644
@@ -1137,6 +1137,16 @@ static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
 	kfree(policy);
 }
 
+static void cpufreq_policy_exit(struct cpufreq_policy *policy)
+{
+	if (!cpufreq_driver->exit)
+		return;
+
+	cpufreq_driver->exit(policy);
+	free_sorted_freq_table(policy);
+	policy->freq_table = NULL;
+}
+
 static int cpufreq_online(unsigned int cpu)
 {
 	struct cpufreq_policy *policy;
@@ -1178,6 +1188,10 @@ static int cpufreq_online(unsigned int cpu)
 		goto out_free_policy;
 	}
 
+	ret = create_sorted_freq_table(policy);
+	if (ret)
+		goto out_exit_policy;
+
 	down_write(&policy->rwsem);
 
 	if (new_policy) {
@@ -1291,9 +1305,7 @@ static int cpufreq_online(unsigned int cpu)
 
 out_exit_policy:
 	up_write(&policy->rwsem);
-
-	if (cpufreq_driver->exit)
-		cpufreq_driver->exit(policy);
+	cpufreq_policy_exit(policy);
 out_free_policy:
 	cpufreq_policy_free(policy, !new_policy);
 	return ret;
@@ -1378,10 +1390,7 @@ static void cpufreq_offline(unsigned int cpu)
 	 * since this is a core component, and is essential for the
 	 * subsequent light-weight ->init() to succeed.
 	 */
-	if (cpufreq_driver->exit) {
-		cpufreq_driver->exit(policy);
-		policy->freq_table = NULL;
-	}
+	cpufreq_policy_exit(policy);
 
 unlock:
 	up_write(&policy->rwsem);
drivers/cpufreq/freq_table.c +67 -0

79 lines not on this page · raw message

include/linux/cpufreq.h +3 -0
index c378776628b4..5e23eed2252c 100644
@@ -87,6 +87,7 @@ struct cpufreq_policy {
 
 	struct cpufreq_user_policy user_policy;
 	struct cpufreq_frequency_table	*freq_table;
+	struct cpufreq_frequency_table	*sorted_freq_table;
 
 	struct list_head        policy_list;
 	struct kobject		kobj;
@@ -592,6 +593,8 @@ static inline void dev_pm_opp_free_cpufreq_table(struct device *dev,
 
 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
 				    struct cpufreq_frequency_table *table);
+int create_sorted_freq_table(struct cpufreq_policy *policy);
+void free_sorted_freq_table(struct cpufreq_policy *policy);
 
 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
 				   struct cpufreq_frequency_table *table);
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#2 lore raw

PATCH v2 2/2 cpufreq: Optimize cpufreq_frequency_table_target()

In reply to Viresh Kumar

cpufreq core keeps another table of sorted frequencies now and that can be used to find a match quickly, instead of traversing the unsorted list in an inefficient way.

Create helper routines for separate relation types to optimize it further.

Ideally the new routine cpufreq_find_target_index() is required to be exported, but s3c24xx was abusing the earlier API and have to be supported for now. Added a FIXME for it.

Tested on Exynos board with both ondemand and schedutil governor and confirmed with help of various print messages that we are eventually switching to the desired frequency based on a target frequency.

---
 drivers/cpufreq/acpi-cpufreq.c    |  15 ++--
 drivers/cpufreq/freq_table.c      | 180 ++++++++++++++++++++++----------------
 drivers/cpufreq/s3c24xx-cpufreq.c |  13 ++-
 include/linux/cpufreq.h           |  20 ++++-
 4 files changed, 134 insertions(+), 94 deletions(-)
drivers/cpufreq/acpi-cpufreq.c +5 -10
index 32a15052f363..4f9f7504a17c 100644
@@ -468,20 +468,15 @@ unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy,
 	struct acpi_cpufreq_data *data = policy->driver_data;
 	struct acpi_processor_performance *perf;
 	struct cpufreq_frequency_table *entry;
-	unsigned int next_perf_state, next_freq, freq;
+	unsigned int next_perf_state, next_freq, index;
 
 	/*
 	 * Find the closest frequency above target_freq.
-	 *
-	 * The table is sorted in the reverse order with respect to the
-	 * frequency and all of the entries are valid (see the initialization).
 	 */
-	entry = policy->freq_table;
-	do {
-		entry++;
-		freq = entry->frequency;
-	} while (freq >= target_freq && freq != CPUFREQ_TABLE_END);
-	entry--;
+	index = cpufreq_frequency_table_target(policy, target_freq,
+					       CPUFREQ_RELATION_L);
+
+	entry = &policy->freq_table[index];
 	next_freq = entry->frequency;
 	next_perf_state = entry->driver_data;
 
drivers/cpufreq/freq_table.c +106 -74

205 lines not on this page · raw message

drivers/cpufreq/s3c24xx-cpufreq.c +6 -7
index 7b596fa38ad2..c9c2b4151b9f 100644
@@ -318,14 +318,13 @@ static int s3c_cpufreq_target(struct cpufreq_policy *policy,
 		tmp_policy.min = policy->min * 1000;
 		tmp_policy.max = policy->max * 1000;
 		tmp_policy.cpu = policy->cpu;
-		tmp_policy.freq_table = pll_reg;
 
-		/* cpufreq_frequency_table_target returns the index
-		 * of the table entry, not the value of
-		 * the table entry's index field. */
-
-		index = cpufreq_frequency_table_target(&tmp_policy, target_freq,
-						       relation);
+		/*
+		 * FIXME: We should be providing this freq-table to the cpufreq
+		 * core instead of managing it ourselves here.
+		 */
+		index = cpufreq_find_target_index(&tmp_policy, pll_reg,
+						  target_freq, relation);
 		pll = pll_reg + index;
 
 		s3c_freq_dbg("%s: target %u => %u\n",
include/linux/cpufreq.h +17 -3
index 5e23eed2252c..5aabec611e87 100644
@@ -600,14 +600,28 @@ int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
 				   struct cpufreq_frequency_table *table);
 int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy);
 
-int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
-				   unsigned int target_freq,
-				   unsigned int relation);
+int cpufreq_find_target_index(struct cpufreq_policy *policy,
+			      struct cpufreq_frequency_table *table,
+			      unsigned int target_freq, unsigned int relation);
 int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
 		unsigned int freq);
 
 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);
 
+/*
+ * Search in the sorted freq table local to the core and return index of
+ * policy->freq_table.
+ */
+static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
+						 unsigned int target_freq,
+						 unsigned int relation)
+{
+	int index = cpufreq_find_target_index(policy, policy->sorted_freq_table,
+					      target_freq, relation);
+
+	return policy->sorted_freq_table[index].driver_data;
+}
+
 #ifdef CONFIG_CPU_FREQ
 int cpufreq_boost_trigger_state(int state);
 int cpufreq_boost_enabled(void);
Signature · 1 line
2.7.1.410.g6faf27b
#3 lore raw

Re: [PATCH V2 2/2] cpufreq: Optimize cpufreq_frequency_table_target()

Quoted 38 lines · Viresh Kumar

cpufreq core keeps another table of sorted frequencies now and that can be used to find a match quickly, instead of traversing the unsorted list in an inefficient way.

Create helper routines for separate relation types to optimize it further.

Ideally the new routine cpufreq_find_target_index() is required to be exported, but s3c24xx was abusing the earlier API and have to be supported for now. Added a FIXME for it.

Tested on Exynos board with both ondemand and schedutil governor and confirmed with help of various print messages that we are eventually switching to the desired frequency based on a target frequency.

---
 drivers/cpufreq/acpi-cpufreq.c    |  15 ++--
 drivers/cpufreq/freq_table.c      | 180 ++++++++++++++++++++++----------------
 drivers/cpufreq/s3c24xx-cpufreq.c |  13 ++-
 include/linux/cpufreq.h           |  20 ++++-
 4 files changed, 134 insertions(+), 94 deletions(-)
drivers/cpufreq/acpi-cpufreq.c +1 -3
index 32a15052f363..4f9f7504a17c 100644
@@ -468,20 +468,15 @@ unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy,
 	struct acpi_cpufreq_data *data = policy->driver_data;
 	struct acpi_processor_performance *perf;
 	struct cpufreq_frequency_table *entry;
-	unsigned int next_perf_state, next_freq, freq;
+	unsigned int next_perf_state, next_freq, index;
 
 	/*
 	 * Find the closest frequency above target_freq.
-	 *
-	 * The table is sorted in the reverse order with respect to the

drivers/cpufreq/acpi-cpufreq.c unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy, · line 477

Quoted 10 lines
drivers/cpufreq/acpi-cpufreq.c +2 -7
-	 * frequency and all of the entries are valid (see the initialization).
 	 */
-	entry = policy->freq_table;
-	do {
-		entry++;
-		freq = entry->frequency;
-	} while (freq >= target_freq && freq != CPUFREQ_TABLE_END);
-	entry--;
+	index = cpufreq_frequency_table_target(policy, target_freq,
+					       CPUFREQ_RELATION_L);

This adds a function call to the fast path...

author maintainer
#4 lore raw

Re: [PATCH V2 2/2] cpufreq: Optimize cpufreq_frequency_table_target()

Quoted 8 lines · Steve Muckle
Quoted 8 lines
drivers/cpufreq/acpi-cpufreq.c +0 -5
 	/*
 	 * Find the closest frequency above target_freq.
-	 *
-	 * The table is sorted in the reverse order with respect to the
-	 * frequency and all of the entries are valid (see the initialization).
 	 */
-	entry = policy->freq_table;
-	do {
Quoted 8 lines

drivers/cpufreq/acpi-cpufreq.c unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy, · line 477

Quoted 6 lines
drivers/cpufreq/acpi-cpufreq.c +2 -4
-		entry++;
-		freq = entry->frequency;
-	} while (freq >= target_freq && freq != CPUFREQ_TABLE_END);
-	entry--;
+	index = cpufreq_frequency_table_target(policy, target_freq,
+					       CPUFREQ_RELATION_L);

This adds a function call to the fast path...

I understand that, but I am not sure how far should we go to avoid that. Open coding routines to save on that isn't a good idea surely.

I have at least kept this routine in cpufreq.h to avoid a call, but eventually we will have at least a call somewhere within it. :(

Signature · 1 line
viresh
#5 lore raw

Re: [PATCH V2 2/2] cpufreq: Optimize cpufreq_frequency_table_target()

Quoted 18 lines · Viresh Kumar
Quoted 8 lines · Steve Muckle
Quoted 8 lines
drivers/cpufreq/acpi-cpufreq.c +0 -5
 	/*
 	 * Find the closest frequency above target_freq.
-	 *
-	 * The table is sorted in the reverse order with respect to the
-	 * frequency and all of the entries are valid (see the initialization).
 	 */
-	entry = policy->freq_table;
-	do {
Quoted 8 lines

drivers/cpufreq/acpi-cpufreq.c unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy, · line 477

Quoted 6 lines
drivers/cpufreq/acpi-cpufreq.c +2 -4
-		entry++;
-		freq = entry->frequency;
-	} while (freq >= target_freq && freq != CPUFREQ_TABLE_END);
-	entry--;
+	index = cpufreq_frequency_table_target(policy, target_freq,
+					       CPUFREQ_RELATION_L);

This adds a function call to the fast path...

Quoted 5 lines

I understand that, but I am not sure how far should we go to avoid that. Open coding routines to save on that isn't a good idea surely.

I have at least kept this routine in cpufreq.h to avoid a call, but eventually we will have at least a call somewhere within it. :(

Shouldn't we be able to avoid extra function calls through the use of macros/inlines? Otherwise this is making things slower for schedutil than it is today.

Actually cpufreq_driver_resolve_freq() shouldn't require any calls from schedutil when a freq_table is available - the whole thing could be run inline.

author maintainer
#6 lore raw

Re: [PATCH V2 2/2] cpufreq: Optimize cpufreq_frequency_table_target()

Quoted 7 lines · Steve Muckle

Shouldn't we be able to avoid extra function calls through the use of macros/inlines? Otherwise this is making things slower for schedutil than it is today.

Actually cpufreq_driver_resolve_freq() shouldn't require any calls from schedutil when a freq_table is available - the whole thing could be run inline.

I will see what I can do on that. Thanks.

Signature · 1 line
viresh