OpenBSD: Unified CPU polling API.

Further testing proves we can use one approach for both SP and MP
machines.
This commit is contained in:
Alastair Poole 2019-05-25 16:24:55 +01:00
parent 95f81498c0
commit 56f2667451
2 changed files with 18 additions and 80 deletions

View File

@ -253,39 +253,9 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
memset(&cpu_times, 0, CPU_STATES * sizeof(struct cpustats));
if (!ncpu)
return;
if (ncpu == 1)
for (i = 0; i < ncpu; i++)
{
core = cores[0];
int cpu_time_mib[] = { CTL_KERN, KERN_CPTIME };
size = sizeof(struct cpustats);
if (sysctl(cpu_time_mib, 2, &cpu_times[0], &size, NULL, 0) < 0)
return;
total = 0;
for (j = 0; j < CPU_STATES; j++)
total += cpu_times[0].cs_time[j];
idle = cpu_times[0].cs_time[CP_IDLE];
diff_total = total - core->total;
diff_idle = idle - core->idle;
if (diff_total == 0) diff_total = 1;
ratio = diff_total / 100.0;
used = diff_total - diff_idle;
percent = used / ratio;
if (percent > 100) percent = 100;
else if (percent < 0)
percent = 0;
core->percent = percent;
core->total = total;
core->idle = idle;
}
else if (ncpu > 1)
{
for (i = 0; i < ncpu; i++) {
core = cores[i];
int cpu_time_mib[] = { CTL_KERN, KERN_CPUSTATS, 0 };
size = sizeof(struct cpustats);
@ -315,7 +285,6 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->total = total;
core->idle = idle;
}
}
#elif defined(__linux__)
char *buf, name[128];
int i;

View File

@ -322,39 +322,9 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
memset(&cpu_times, 0, CPU_STATES * sizeof(struct cpustats));
if (!ncpu)
return;
if (ncpu == 1)
for (i = 0; i < ncpu; i++)
{
core = cores[0];
int cpu_time_mib[] = { CTL_KERN, KERN_CPTIME };
size = sizeof(struct cpustats);
if (sysctl(cpu_time_mib, 2, &cpu_times[0], &size, NULL, 0) < 0)
return;
total = 0;
for (j = 0; j < CPU_STATES; j++)
total += cpu_times[0].cs_time[j];
idle = cpu_times[0].cs_time[CP_IDLE];
diff_total = total - core->total;
diff_idle = idle - core->idle;
if (diff_total == 0) diff_total = 1;
ratio = diff_total / 100.0;
used = diff_total - diff_idle;
percent = used / ratio;
if (percent > 100) percent = 100;
else if (percent < 0)
percent = 0;
core->percent = percent;
core->total = total;
core->idle = idle;
}
else if (ncpu > 1)
{
for (i = 0; i < ncpu; i++) {
core = cores[i];
int cpu_time_mib[] = { CTL_KERN, KERN_CPUSTATS, 0 };
size = sizeof(struct cpustats);
@ -384,7 +354,6 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->total = total;
core->idle = idle;
}
}
#elif defined(__linux__)
char *buf, name[128];
int i;