Not really needed, as memory wasted is not much.
The array could also simply get increased if there are CPUs
with more frequencies.
Unfortunately the array max (16) is used hardcoded at some
places which at least should get adjusted to a constant.
This would not be needed with this patch anymore, though.

Signed-off-by: Thomas Renninger <[email protected]>
CC: Amit Arora <[email protected]>
CC: [email protected]
CC: [email protected]
---
 cpufreqstats.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/cpufreqstats.c b/cpufreqstats.c
index b4df006..7ae6ecb 100644
--- a/cpufreqstats.c
+++ b/cpufreqstats.c
@@ -38,10 +38,9 @@ struct cpufreqdata {
        uint64_t        count;
 };
 
-struct cpufreqdata freqs[16];
-struct cpufreqdata oldfreqs[16];
-
-struct cpufreqdata delta[16];
+static struct cpufreqdata *freqs;
+static struct cpufreqdata *oldfreqs;
+static struct cpufreqdata *delta;
 
 char cpufreqstrings[6][80];
 int topfreq = -1;
@@ -98,6 +97,31 @@ static char *HzToHuman(unsigned long hz)
        return buffer;
 }
 
+/* Get amount of supported frequencies, if CPUs have different
+   amount of supported frequencies, return max
+   -> would be nice to have this in cpufreqlibs as well
+*/
+static int get_frequency_count(void) {
+       int cpu = 0, cpu_freq_count = 0, max_freq_count = 0;
+       struct cpufreq_available_frequencies *tmp, *avail_freqs =
+               cpufreq_get_available_frequencies(cpu);
+
+       while (avail_freqs) {
+               tmp = avail_freqs->first;
+               while (avail_freqs) {
+                       avail_freqs = avail_freqs->next;
+                       cpu_freq_count++;
+               }
+               if (max_freq_count < cpu_freq_count)
+                       max_freq_count = cpu_freq_count;
+               cpu_freq_count = 0;
+
+               cpu++;
+               cpufreq_put_available_frequencies(tmp);
+               avail_freqs = cpufreq_get_available_frequencies(cpu);
+       }
+       return max_freq_count;
+}
 
 void  do_cpufreq_stats(void)
 {
@@ -107,11 +131,24 @@ void  do_cpufreq_stats(void)
        uint64_t total_time = 0;
        unsigned long long time_dummy = 0;
 
-       memcpy(&oldfreqs, &freqs, sizeof(freqs));
+       maxfreq = get_frequency_count();
+       if(maxfreq == 0)
+               return;
+
+       if (!freqs)
+               freqs = calloc(sizeof(struct cpufreqdata), maxfreq + 1);
+       if (!oldfreqs)
+               oldfreqs = calloc(sizeof(struct cpufreqdata), maxfreq + 1);
+       if (!delta)
+               delta = calloc(sizeof(struct cpufreqdata), maxfreq + 1);
+       if (!freqs || !oldfreqs || !delta)
+               return;
+
+       memcpy(oldfreqs, freqs, sizeof(struct cpufreqdata) * (maxfreq + 1));
        memset(&cpufreqstrings, 0, sizeof(cpufreqstrings));
        sprintf(cpufreqstrings[0], _("P-states (frequencies)\n"));
 
-       for (ret = 0; ret < 16; ret++)
+       for (ret = 0; ret < maxfreq; ret++)
                freqs[ret].count = 0;
 
        freq_stats = cpufreq_get_stats(0, &time_dummy);
@@ -141,7 +178,7 @@ void  do_cpufreq_stats(void)
                        if (f && maxfreq < i)
                                maxfreq = i;
                        i++;
-                       if (i > 15)
+                       if (i >= maxfreq)
                                break;
                }
                cpu++;
@@ -161,10 +198,10 @@ void  do_cpufreq_stats(void)
        if (!total_time)
                return;
 
-       qsort(&delta, maxfreq + 1, sizeof(struct cpufreqdata), sort_by_count);
+       qsort(delta, maxfreq + 1, sizeof(struct cpufreqdata), sort_by_count);
        if (maxfreq > 4)
                maxfreq = 4;
-       qsort(&delta, maxfreq + 1, sizeof(struct cpufreqdata), sort_by_freq);
+       qsort(delta, maxfreq + 1, sizeof(struct cpufreqdata), sort_by_freq);
 
        if (delta[0].frequency == delta[1].frequency + 1000)
                turbo_hz = delta[0].frequency;
-- 
1.6.4.2

_______________________________________________
Power mailing list
[email protected]
http://www.bughost.org/mailman/listinfo/power

Reply via email to