Skip to contentSkip to conversation

cpufreq: stats: Improve the performance of cpufreq_stats_create_table()

PATCH Applied Liao Chang 4 participants 4 messages Aug 18, 2023 – Aug 21, 2023 CPU frequency scaling framework

lore
Share & tools
lore

Liao Chang

author
#0 lore raw

PATCH 1/1 cpufreq: stats: Improve the performance of cpufreq_stats_create_table()

In the worst case, the freq_table of policy data is not sorted and contains duplicate frequencies, this means that it needs to iterate through the entire freq_table of policy to ensure each frequency is unique in the freq_table of stats data, this has a time complexity of O(N^2), where N is the number of frequencies in the freq_table of policy.

However, if the policy.freq_table is already sorted and contains no duplicate frequencices, it can reduce the time complexity of creating stats.freq_table to O(N), the 'freq_table_sorted' field of policy data can be used to indicate whether the policy.freq_table is sorted.

---
 drivers/cpufreq/cpufreq_stats.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cpufreq/cpufreq_stats.c +2 -1
index 55c7ffd37d1c..fcb74050711a 100644
@@ -243,7 +243,8 @@ void cpufreq_stats_create_table(struct cpufreq_policy *policy)
 
 	/* Find valid-unique entries */
 	cpufreq_for_each_valid_entry(pos, policy->freq_table)
-		if (freq_table_get_index(stats, pos->frequency) == -1)
+		if ((policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED) ||
+		    (freq_table_get_index(stats, pos->frequency) == -1))
 			stats->freq_table[i++] = pos->frequency;
 
 	stats->state_num = i;
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 22 lines · Liao Chang

In the worst case, the freq_table of policy data is not sorted and contains duplicate frequencies, this means that it needs to iterate through the entire freq_table of policy to ensure each frequency is unique in the freq_table of stats data, this has a time complexity of O(N^2), where N is the number of frequencies in the freq_table of policy.

However, if the policy.freq_table is already sorted and contains no duplicate frequencices, it can reduce the time complexity of creating stats.freq_table to O(N), the 'freq_table_sorted' field of policy data can be used to indicate whether the policy.freq_table is sorted.

---
 drivers/cpufreq/cpufreq_stats.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cpufreq/cpufreq_stats.c +0 -0
index 55c7ffd37d1c..fcb74050711a 100644

drivers/cpufreq/cpufreq_stats.c void cpufreq_stats_create_table(struct cpufreq_policy *policy) · line 250

Quoted 9 lines
drivers/cpufreq/cpufreq_stats.c +2 -1
@@ -243,7 +243,8 @@ void cpufreq_stats_create_table(struct cpufreq_policy *policy)
 
 	/* Find valid-unique entries */
 	cpufreq_for_each_valid_entry(pos, policy->freq_table)
-		if (freq_table_get_index(stats, pos->frequency) == -1)
+		if ((policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED) ||
+		    (freq_table_get_index(stats, pos->frequency) == -1))
 			stats->freq_table[i++] = pos->frequency;
 
 	stats->state_num = i;
Signature · 1 line
viresh
Reviewed-by on v1 1/1
#2 lore raw
In reply to Liao Chang
Quoted 9 lines · Liao Chang

In the worst case, the freq_table of policy data is not sorted and contains duplicate frequencies, this means that it needs to iterate through the entire freq_table of policy to ensure each frequency is unique in the freq_table of stats data, this has a time complexity of O(N^2), where N is the number of frequencies in the freq_table of policy.

However, if the policy.freq_table is already sorted and contains no duplicate frequencices, it can reduce the time complexity of creating

s/frequencices/frequencies?

Quoted 13 lines

stats.freq_table to O(N), the 'freq_table_sorted' field of policy data can be used to indicate whether the policy.freq_table is sorted.

---
 drivers/cpufreq/cpufreq_stats.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cpufreq/cpufreq_stats.c +0 -0
index 55c7ffd37d1c..fcb74050711a 100644

drivers/cpufreq/cpufreq_stats.c void cpufreq_stats_create_table(struct cpufreq_policy *policy) · line 246

Quoted 5 lines
drivers/cpufreq/cpufreq_stats.c +1 -1
@@ -243,7 +243,8 @@ void cpufreq_stats_create_table(struct cpufreq_policy *policy)
 
 	/* Find valid-unique entries */
 	cpufreq_for_each_valid_entry(pos, policy->freq_table)
-		if (freq_table_get_index(stats, pos->frequency) == -1)
+		if ((policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED) ||

[...]

Otherwise looks okay to me,

Signature · 2 lines
Best regards,
Dhruva Gole <[email protected]>
maintainer Applied
#3 lore raw
Quoted 9 lines · Dhruva Gole
Quoted 7 lines · Liao Chang

In the worst case, the freq_table of policy data is not sorted and contains duplicate frequencies, this means that it needs to iterate through the entire freq_table of policy to ensure each frequency is unique in the freq_table of stats data, this has a time complexity of O(N^2), where N is the number of frequencies in the freq_table of policy.

Quoted 4 lines
Quoted 2 lines

However, if the policy.freq_table is already sorted and contains no duplicate frequencices, it can reduce the time complexity of creating

s/frequencices/frequencies?

I've fixed this when applying the patch.

Quoted 14 lines
Quoted 14 lines

stats.freq_table to O(N), the 'freq_table_sorted' field of policy data can be used to indicate whether the policy.freq_table is sorted.

---
 drivers/cpufreq/cpufreq_stats.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cpufreq/cpufreq_stats.c +0 -0
index 55c7ffd37d1c..fcb74050711a 100644
@@ -243,7 +243,8 @@ void cpufreq_stats_create_table(struct cpufreq_policy *policy)
 
Quoted 4 lines

drivers/cpufreq/cpufreq_stats.c void cpufreq_stats_create_table(struct cpufreq_policy *policy) · line 246

Quoted 4 lines
drivers/cpufreq/cpufreq_stats.c +1 -1
      /* Find valid-unique entries */
      cpufreq_for_each_valid_entry(pos, policy->freq_table)
-             if (freq_table_get_index(stats, pos->frequency) == -1)
+             if ((policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED) ||

I've also removed the redundant parens from this check.

Quoted 6 lines

[...]

Otherwise looks okay to me,

Applied as 6.6 material (with the changes mentioned above), thanks!