From bc10e59aa18f2ad628a157cc03f79a6740b28d23 Mon Sep 17 00:00:00 2001
From: Amit Daniel Kachhap <amit.kachhap@linaro.org>
Date: Fri, 10 Jun 2011 19:07:52 +0530
Subject: [PATCH] Bug fix: Read the frequency levels from
/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state. This will be more
generic for all platforms.
Signed-off-by: Amit Daniel Kachhap <amit.kachhap@linaro.org>
---
Source/headdefine.h | 2 +
Source/update.c | 64 ++++++++++++++++++++++++---------------------------
2 files changed, 32 insertions(+), 34 deletions(-)
@@ -37,10 +37,12 @@
/* CPU information. Everything fixed on cpu0 right now */
#define MAXCPUFREQ0 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
#define SET_AVAILFREQ0 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies"
+#define SET_FREQSTAT0 "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state"
/* type definitions */
#define MAX_FILECHARSIZE 80
#define MAX_VALUES 20
+#define MAX_LINECHARSIZE 1024
/* get_state return definitions */
#define SAFE_STATE 100
@@ -19,49 +19,45 @@
int available_freq (void)
{
FILE *avail_freq_file;
- char *validfreq; /* A single available freq */
- int ch;
- int i,j,k;
+ char *freqend; /* A single available freq */
+ int j, lenfreq;
+ char *line;
+
+ avail_freq_file = fopen(SET_FREQSTAT0, "r");
+ if (avail_freq_file == NULL)
+ err_fileopen(SET_FREQSTAT0);
/* Initial memory */
- if ((validfreq = calloc(MAX_FILECHARSIZE, sizeof(char))) == NULL)
+ line = calloc(MAX_LINECHARSIZE, sizeof(char));
+ if (line == NULL)
err_allocatemem();
-
- if ((avail_freq_file = fopen (SET_AVAILFREQ0, "r")) == NULL)
- err_fileopen (SET_AVAILFREQ0);
- i = 0;
j = 0;
- k = 0;
- while ((ch = fgetc(avail_freq_file)) != '\n') {
- if (ch == ' ') {
- if ((avail_freq[j].s_validfreq = calloc(strlen(validfreq),
- sizeof(char))) == NULL) {
- fclose(avail_freq_file);
- err_allocatemem();
- }
- strcpy (avail_freq[j].s_validfreq, validfreq);
- memset (validfreq, 0x00, MAX_FILECHARSIZE);
- j++;
- if (j > MAX_VALUES) {
- fclose(avail_freq_file);
- err_maxvalues();
- }
- k = 0;
- }
- else {
- validfreq[k] = (char)ch;
- k++;
+
+ while (!feof(avail_freq_file)) {
+ if (fgets(line, MAX_LINECHARSIZE-1, avail_freq_file) == NULL)
+ break;
+
+ freqend = strchr(line, ' ');
+ if (freqend == NULL) {
+ fclose(avail_freq_file);
+ free(line);
+ err_filedata(SET_FREQSTAT0);
}
- i++;
- if (i > MAX_FILECHARSIZE) {
+ lenfreq = freqend - line;
+
+ avail_freq[j].s_validfreq = calloc(lenfreq + 1,
+ sizeof(char));
+ if (avail_freq[j].s_validfreq == NULL) {
fclose(avail_freq_file);
- err_filedata (SET_AVAILFREQ0);
+ free(line);
+ err_allocatemem();
}
+ strncpy(avail_freq[j].s_validfreq, line, lenfreq);
+ j++;
}
- free (validfreq);
-
- fclose(avail_freq_file);
+ free(line);
+ fclose(avail_freq_file);
return j; /* Number of freq values */
}
--
1.7.1