Patchwork cpufreq: use qsort to sort frequencies

login
register
mail settings
Submitter Colin King
Date April 20, 2013, 1:40 p.m.
Message ID <1366465207-13534-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/238154/
State Accepted
Headers show

Comments

Colin King - April 20, 2013, 1:40 p.m.
From: Colin Ian King <colin.king@canonical.com>

Use qsort to sort frequencies, and set the maximum number of
frequencies to 256 to overly futureproof the code.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/cpu/cpufreq/cpufreq.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)
Alex Hung - April 22, 2013, 1:32 a.m.
On 04/20/2013 09:40 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Use qsort to sort frequencies, and set the maximum number of
> frequencies to 256 to overly futureproof the code.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/cpu/cpufreq/cpufreq.c | 33 +++++++++++++++------------------
>   1 file changed, 15 insertions(+), 18 deletions(-)
>
> diff --git a/src/cpu/cpufreq/cpufreq.c b/src/cpu/cpufreq/cpufreq.c
> index fd37b9d..fa10275 100644
> --- a/src/cpu/cpufreq/cpufreq.c
> +++ b/src/cpu/cpufreq/cpufreq.c
> @@ -41,6 +41,8 @@
>
>   #define FWTS_CPU_PATH	"/sys/devices/system/cpu"
>
> +#define MAX_FREQS	256
> +
>   typedef struct {
>   	uint32_t	Hz;
>   	uint64_t	speed;
> @@ -214,14 +216,22 @@ static uint32_t get_claimed_hz(const int cpu)
>   	return value;
>   }
>
> +static int cpu_freq_compare(const void *v1, const void *v2)
> +{
> +	const fwts_cpu_freq *cpu_freq1 = (fwts_cpu_freq *)v1;
> +	const fwts_cpu_freq *cpu_freq2 = (fwts_cpu_freq *)v2;
> +
> +	return cpu_freq1->Hz - cpu_freq2->Hz;
> +}
> +
>   static void do_cpu(fwts_framework *fw, int cpu)
>   {
>   	char path[PATH_MAX];
>   	char line[4096];
> -	fwts_cpu_freq freqs[32];
> +	fwts_cpu_freq freqs[MAX_FREQS];
>   	FILE *file;
>   	char *c, *c2;
> -	int i, delta;
> +	int i;
>   	int speedcount;
>   	static int warned=0;
>   	int warned_PSS = 0;
> @@ -250,7 +260,7 @@ static void do_cpu(fwts_framework *fw, int cpu)
>
>   	c = line;
>   	i = 0;
> -	while (c && strlen(c) > 1) {
> +	while ((i < MAX_FREQS) && c && strlen(c) > 1) {
>   		c2 = strchr(c, ' ');
>   		if (c2) {
>   			*c2 = 0;
> @@ -302,21 +312,8 @@ static void do_cpu(fwts_framework *fw, int cpu)
>   	if (speedcount<2)
>   		return;
>
> -	/* Now.. sort the frequencies */
> -
> -	delta=1;
> -	while (delta) {
> -		fwts_cpu_freq tmp;
> -		delta = 0;
> -		for (i = 0; i < speedcount-1; i++) {
> -			if (freqs[i].Hz > freqs[i+1].Hz) {
> -				tmp = freqs[i];
> -				freqs[i] = freqs[i+1];
> -				freqs[i+1] = tmp;
> -				delta = 1;
> -			}
> -		}
> -	}
> +	/* Sort the frequencies */
> +	qsort(freqs, speedcount, sizeof(fwts_cpu_freq), cpu_freq_compare);
>
>   	/* now check for 1) increasing HZ and 2) increasing speed */
>   	for (i = 0; i < speedcount-1; i++) {
>
Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu - April 22, 2013, 3:47 a.m.
On 04/20/2013 09:40 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Use qsort to sort frequencies, and set the maximum number of
> frequencies to 256 to overly futureproof the code.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/cpu/cpufreq/cpufreq.c | 33 +++++++++++++++------------------
>   1 file changed, 15 insertions(+), 18 deletions(-)
>
> diff --git a/src/cpu/cpufreq/cpufreq.c b/src/cpu/cpufreq/cpufreq.c
> index fd37b9d..fa10275 100644
> --- a/src/cpu/cpufreq/cpufreq.c
> +++ b/src/cpu/cpufreq/cpufreq.c
> @@ -41,6 +41,8 @@
>
>   #define FWTS_CPU_PATH	"/sys/devices/system/cpu"
>
> +#define MAX_FREQS	256
> +
>   typedef struct {
>   	uint32_t	Hz;
>   	uint64_t	speed;
> @@ -214,14 +216,22 @@ static uint32_t get_claimed_hz(const int cpu)
>   	return value;
>   }
>
> +static int cpu_freq_compare(const void *v1, const void *v2)
> +{
> +	const fwts_cpu_freq *cpu_freq1 = (fwts_cpu_freq *)v1;
> +	const fwts_cpu_freq *cpu_freq2 = (fwts_cpu_freq *)v2;
> +
> +	return cpu_freq1->Hz - cpu_freq2->Hz;
> +}
> +
>   static void do_cpu(fwts_framework *fw, int cpu)
>   {
>   	char path[PATH_MAX];
>   	char line[4096];
> -	fwts_cpu_freq freqs[32];
> +	fwts_cpu_freq freqs[MAX_FREQS];
>   	FILE *file;
>   	char *c, *c2;
> -	int i, delta;
> +	int i;
>   	int speedcount;
>   	static int warned=0;
>   	int warned_PSS = 0;
> @@ -250,7 +260,7 @@ static void do_cpu(fwts_framework *fw, int cpu)
>
>   	c = line;
>   	i = 0;
> -	while (c && strlen(c) > 1) {
> +	while ((i < MAX_FREQS) && c && strlen(c) > 1) {
>   		c2 = strchr(c, ' ');
>   		if (c2) {
>   			*c2 = 0;
> @@ -302,21 +312,8 @@ static void do_cpu(fwts_framework *fw, int cpu)
>   	if (speedcount<2)
>   		return;
>
> -	/* Now.. sort the frequencies */
> -
> -	delta=1;
> -	while (delta) {
> -		fwts_cpu_freq tmp;
> -		delta = 0;
> -		for (i = 0; i < speedcount-1; i++) {
> -			if (freqs[i].Hz > freqs[i+1].Hz) {
> -				tmp = freqs[i];
> -				freqs[i] = freqs[i+1];
> -				freqs[i+1] = tmp;
> -				delta = 1;
> -			}
> -		}
> -	}
> +	/* Sort the frequencies */
> +	qsort(freqs, speedcount, sizeof(fwts_cpu_freq), cpu_freq_compare);
>
>   	/* now check for 1) increasing HZ and 2) increasing speed */
>   	for (i = 0; i < speedcount-1; i++) {
>

Acked-by: Ivan Hu <ivan.hu@canonical.com>

Patch

diff --git a/src/cpu/cpufreq/cpufreq.c b/src/cpu/cpufreq/cpufreq.c
index fd37b9d..fa10275 100644
--- a/src/cpu/cpufreq/cpufreq.c
+++ b/src/cpu/cpufreq/cpufreq.c
@@ -41,6 +41,8 @@ 
 
 #define FWTS_CPU_PATH	"/sys/devices/system/cpu"
 
+#define MAX_FREQS	256
+
 typedef struct {
 	uint32_t	Hz;
 	uint64_t	speed;
@@ -214,14 +216,22 @@  static uint32_t get_claimed_hz(const int cpu)
 	return value;
 }
 
+static int cpu_freq_compare(const void *v1, const void *v2)
+{
+	const fwts_cpu_freq *cpu_freq1 = (fwts_cpu_freq *)v1;
+	const fwts_cpu_freq *cpu_freq2 = (fwts_cpu_freq *)v2;
+
+	return cpu_freq1->Hz - cpu_freq2->Hz;
+}
+
 static void do_cpu(fwts_framework *fw, int cpu)
 {
 	char path[PATH_MAX];
 	char line[4096];
-	fwts_cpu_freq freqs[32];
+	fwts_cpu_freq freqs[MAX_FREQS];
 	FILE *file;
 	char *c, *c2;
-	int i, delta;
+	int i;
 	int speedcount;
 	static int warned=0;
 	int warned_PSS = 0;
@@ -250,7 +260,7 @@  static void do_cpu(fwts_framework *fw, int cpu)
 
 	c = line;
 	i = 0;
-	while (c && strlen(c) > 1) {
+	while ((i < MAX_FREQS) && c && strlen(c) > 1) {
 		c2 = strchr(c, ' ');
 		if (c2) {
 			*c2 = 0;
@@ -302,21 +312,8 @@  static void do_cpu(fwts_framework *fw, int cpu)
 	if (speedcount<2)
 		return;
 
-	/* Now.. sort the frequencies */
-
-	delta=1;
-	while (delta) {
-		fwts_cpu_freq tmp;
-		delta = 0;
-		for (i = 0; i < speedcount-1; i++) {
-			if (freqs[i].Hz > freqs[i+1].Hz) {
-				tmp = freqs[i];
-				freqs[i] = freqs[i+1];
-				freqs[i+1] = tmp;
-				delta = 1;
-			}
-		}
-	}
+	/* Sort the frequencies */
+	qsort(freqs, speedcount, sizeof(fwts_cpu_freq), cpu_freq_compare);
 
 	/* now check for 1) increasing HZ and 2) increasing speed */
 	for (i = 0; i < speedcount-1; i++) {