1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-15 17:47: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:
Brad Sawatzky 2018-01-19 09:02:32 -05:00 committed by Brenden Matthews
parent 61c0dfaf60
commit c712d67884

View File

@ -801,8 +801,11 @@ void get_cpu_count(void)
{
FILE *stat_fp;
static int rep = 0;
int highest_cpu_index;
char buf[256];
char *str1, *str2, *token, *subtoken;
char *saveptr1, *saveptr2;
int subtoken1=-1;
int subtoken2=-1;
if (info.cpu_usage) {
return;
@ -819,11 +822,30 @@ void get_cpu_count(void)
break;
}
if (sscanf(buf, "%*d-%d", &highest_cpu_index) == 1) {
info.cpu_count = highest_cpu_index;
// Do some parsing here to handle skipped cpu numbers. For example,
// 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));
fclose(stat_fp);
@ -897,7 +919,7 @@ int update_stat(void)
} else if (strncmp(buf, "cpu", 3) == 0) {
double delta;
if (isdigit(buf[3])) {
idx = atoi(&buf[3]) + 1;
idx++; //just increment here since the CPU index can skip numbers
} else {
idx = 0;
}