From patchwork Sat Apr 20 13:40:07 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 238154 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) by ozlabs.org (Postfix) with ESMTP id 88D982C015D for ; Sat, 20 Apr 2013 23:40:13 +1000 (EST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1UTY1S-0007eG-Pu; Sat, 20 Apr 2013 13:40:10 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1UTY1Q-0007e3-Jc for fwts-devel@lists.ubuntu.com; Sat, 20 Apr 2013 13:40:08 +0000 Received: from cpc3-craw6-2-0-cust180.croy.cable.virginmedia.com ([77.100.248.181] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1UTY1Q-0007Sd-F3 for fwts-devel@lists.ubuntu.com; Sat, 20 Apr 2013 13:40:08 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH] cpufreq: use qsort to sort frequencies Date: Sat, 20 Apr 2013 14:40:07 +0100 Message-Id: <1366465207-13534-1-git-send-email-colin.king@canonical.com> X-Mailer: git-send-email 1.8.1.2 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.14 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: fwts-devel-bounces@lists.ubuntu.com From: Colin Ian King 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 Acked-by: Alex Hung Acked-by: Ivan Hu --- 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++) {