mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-11-16 01:57:09 +00:00
Fix segfault when kernel reports non-sequential SMP CPU indices (#416)
- For an AMD FX(tm)-6350 Six-Core Processor the file '/sys/.../present' reports "0,3-7". I assume that chip is really an 8-core die with two cores disabled... Presumably you could also get "0,3-4,6", and other combos too...
This commit is contained in:
parent
61c0dfaf60
commit
c712d67884
32
src/linux.cc
32
src/linux.cc
@ -801,8 +801,11 @@ void get_cpu_count(void)
|
|||||||
{
|
{
|
||||||
FILE *stat_fp;
|
FILE *stat_fp;
|
||||||
static int rep = 0;
|
static int rep = 0;
|
||||||
int highest_cpu_index;
|
|
||||||
char buf[256];
|
char buf[256];
|
||||||
|
char *str1, *str2, *token, *subtoken;
|
||||||
|
char *saveptr1, *saveptr2;
|
||||||
|
int subtoken1=-1;
|
||||||
|
int subtoken2=-1;
|
||||||
|
|
||||||
if (info.cpu_usage) {
|
if (info.cpu_usage) {
|
||||||
return;
|
return;
|
||||||
@ -819,11 +822,30 @@ void get_cpu_count(void)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sscanf(buf, "%*d-%d", &highest_cpu_index) == 1) {
|
// Do some parsing here to handle skipped cpu numbers. For example,
|
||||||
info.cpu_count = highest_cpu_index;
|
// for an AMD FX(tm)-6350 Six-Core Processor /sys/.../present reports
|
||||||
|
// "0,3-7". I assume that chip is really an 8-core die with two cores
|
||||||
|
// disabled... Presumably you could also get "0,3-4,6", and other
|
||||||
|
// combos too...
|
||||||
|
for (str1 = buf; ; str1 = NULL) {
|
||||||
|
token = strtok_r(str1, ",", &saveptr1);
|
||||||
|
if (token == NULL) break;
|
||||||
|
++info.cpu_count;
|
||||||
|
|
||||||
|
subtoken1=-1;
|
||||||
|
subtoken2=-1;
|
||||||
|
for (str2 = token; ; str2 = NULL) {
|
||||||
|
subtoken = strtok_r(str2, "-", &saveptr2);
|
||||||
|
if (subtoken == NULL) break;
|
||||||
|
if(subtoken1 < 0)
|
||||||
|
subtoken1=atoi(subtoken);
|
||||||
|
else
|
||||||
|
subtoken2=atoi(subtoken);
|
||||||
|
}
|
||||||
|
if(subtoken2 > 0)
|
||||||
|
info.cpu_count += subtoken2 - subtoken1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
++info.cpu_count;
|
|
||||||
info.cpu_usage = (float*)malloc((info.cpu_count + 1) * sizeof(float));
|
info.cpu_usage = (float*)malloc((info.cpu_count + 1) * sizeof(float));
|
||||||
|
|
||||||
fclose(stat_fp);
|
fclose(stat_fp);
|
||||||
@ -897,7 +919,7 @@ int update_stat(void)
|
|||||||
} else if (strncmp(buf, "cpu", 3) == 0) {
|
} else if (strncmp(buf, "cpu", 3) == 0) {
|
||||||
double delta;
|
double delta;
|
||||||
if (isdigit(buf[3])) {
|
if (isdigit(buf[3])) {
|
||||||
idx = atoi(&buf[3]) + 1;
|
idx++; //just increment here since the CPU index can skip numbers
|
||||||
} else {
|
} else {
|
||||||
idx = 0;
|
idx = 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user