From 3ac35c199e59585bd3c9e8348c5f4b5a7137ff41 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 27 Sep 2014 13:03:09 +1200 Subject: Fixed bug affecting CPUs with multiple cores --- common.c | 11 +++++++++++ common.h | 1 + getfreq.c | 59 +++++++++++++++++++++++++++++++++++++---------------------- getfreq.h | 2 +- tray.c | 6 ++++-- 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/common.c b/common.c index 231ad50..a85efd3 100644 --- a/common.c +++ b/common.c @@ -93,3 +93,14 @@ int get_int(const char* string) return atoi(first_num); } + + +/*********************************************************************** + * Replace occurrences of \n from end of string with \0 + **********************************************************************/ +void chomp(char* string) +{ + int i; + for (i = strlen(string)-1; (i >= 0 && string[i] == '\n'); i--) + string[i] = '\0'; +} diff --git a/common.h b/common.h index 350f724..74036bb 100644 --- a/common.h +++ b/common.h @@ -26,6 +26,7 @@ int get_int_value_from_file(const char* filename); int get_int(const char* string); bool file_has_line(const char *filename, const char *line); FILE* check_for_file(char* path); +void chomp(char* string); // Stringification of line number #define STRING2(x) #x diff --git a/getfreq.c b/getfreq.c index d9507c2..87d7bc3 100644 --- a/getfreq.c +++ b/getfreq.c @@ -25,9 +25,12 @@ #include #include -/* [CORE][FREQUENCY NUMBER] */ -char freqs[999][50][13]; -int total_freqs; +#define MAX_CORES 1000 +#define MAX_FREQS 50 +#define FREQ_LENGTH 14 + +char freqs[MAX_CORES][MAX_FREQS][FREQ_LENGTH]; +int total_freqs; // Number of freqs for core 0 /*********************************************************************** @@ -35,10 +38,14 @@ int total_freqs; **********************************************************************/ void gf_init() { - char freq_string[500]; + char freq_string[4096]; // POSIX suggested line length. Source of error for CPUs with a huge number of freqs + char *next_token; unsigned int i; - for(i = 0; i < gc_number(); i++) + memset(freqs, '\0', sizeof(freqs)); + + + for(i = 0; (i < gc_number() && i < MAX_CORES); i++) { memset(freq_string, '\0', sizeof(freq_string) ); @@ -49,21 +56,25 @@ void gf_init() continue; } - // freq_string is a space separated list of freqs so - // iterate over each frequency in freq_string - char* curr = &freq_string[0]; - char* end_of_curr = g_strstr_len(curr, strlen(curr), " "); - while(end_of_curr) - { - // TO DO : get rid of magic constants - memset(freqs[i][total_freqs], '\0', 13); // TO DO: get rid of magic constant 13 - memmove(freqs[i][total_freqs], curr, end_of_curr - curr); + *strchrnul(freq_string, '\n') = '\0'; - curr = end_of_curr+1; - end_of_curr = g_strstr_len(curr, strlen(curr), " "); + // freq_string is a space separated list of freqs + // Use strtok to find each + next_token = strtok(freq_string, " \n"); + total_freqs = 0; + do + { + chomp(next_token); + debug("Found frequency #%d (%s KHz)\n",freq,next_token); + strncpy(freqs[i][total_freqs], next_token, FREQ_LENGTH); total_freqs++; - } + } while((next_token = strtok(NULL, " ")) != NULL); } + + // Hit the limit of storage of cores' frequencies + if (i == MAX_CORES) + info("Unable to add more than %d cores\n", MAX_CORES); + debug("Found %d frequencies\n",total_freqs); } @@ -122,15 +133,19 @@ int gf_available(int core, char* out, int size) } /*********************************************************************** - * Populate `out` with a formatted, units-added freq label for freq + * Populate `out` with a formatted, units-added freq label for `freq` **********************************************************************/ char* gf_get_frequency_label(int freq) { char *string; - if(freq >= 1000000) // >= 1 million KHz (1GHz) - asprintf(&string, "%.2f GHz", ((float)freq/1000000) ); - else - asprintf(&string, "%.2d MHz", freq/1000); + if(freq >= 1000000000) // >= 1 billion KHz (1 THz) This, ladies and gentlement, is future-proofing ;) + asprintf(&string, "%.2f THz", (float)freq/1000000000 ); + else if(freq >= 1000000) // >= 1 million KHz (1 GHz) + asprintf(&string, "%.2f GHz", (float)freq/1000000 ); + else if (freq >= 1000) // >= 1 thousand KHz (1 MHz) + asprintf(&string, "%.2f MHz", (float)freq/1000 ); + else // < 1000 KHz (1 MHz) + asprintf(&string, "%.2f KHz", (float)freq); debug("Prepared freq label '%s' for freq %d\n",string,freq); diff --git a/getfreq.h b/getfreq.h index a8257eb..6699cd5 100644 --- a/getfreq.h +++ b/getfreq.h @@ -23,7 +23,7 @@ void gf_init(); int gf_current(int core); -bool gf_available(int core, char* out, int size); +int gf_available(int core, char* out, int size); char* gf_get_frequency_label(int freq); char* gf_freqa(int core, int index); int gf_freqi(int core, int index); diff --git a/tray.c b/tray.c index afc78c7..110e26b 100644 --- a/tray.c +++ b/tray.c @@ -340,8 +340,10 @@ void tray_update_icon_percent() char* file; gulong max_frequency = gf_freqi(0, 0); gint adjusted_percent = 0; - // If no governor, set percentage to 0. This if statement fixes an FPE a few lines down - if (gg_number() == 0) + + // If max_frequency is 0, we don't want to divide by it, + // so give up, call it a day, and have a simple icon + if (max_frequency == 0) { adjusted_percent = 0; } else { -- cgit v1.1