(SIGAR-39) Use kern.cp_times in cpu_list on FreeBSD 7.x+

This commit is contained in:
Doug MacEachern 2009-04-20 18:39:36 -07:00
parent 68cf08cf4d
commit 039080f722
1 changed files with 52 additions and 1 deletions

View File

@ -783,6 +783,52 @@ int sigar_cpu_get(sigar_t *sigar, sigar_cpu_t *cpu)
return SIGAR_OK;
}
#if defined(__FreeBSD__) && (__FreeBSD_version >= 700000)
#define HAVE_KERN_CP_TIMES /* kern.cp_times came later than 7.0, not sure exactly when */
static int sigar_cp_times_get(sigar_t *sigar, sigar_cpu_list_t *cpulist)
{
int maxcpu, status;
size_t len = sizeof(maxcpu), size;
long *times;
if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &len, NULL, 0) == -1) {
return errno;
}
size = sizeof(long) * maxcpu * CPUSTATES;
times = malloc(size);
if (sysctlbyname("kern.cp_times", times, &size, NULL, 0) == -1) {
status = errno;
}
else {
int i, maxid = (size / CPUSTATES / sizeof(long));
long *cp_time = times;
status = SIGAR_OK;
for (i=0; i<maxid; i++) {
sigar_cpu_t *cpu;
SIGAR_CPU_LIST_GROW(cpulist);
cpu = &cpulist->data[cpulist->number++];
cpu->user = SIGAR_TICK2MSEC(cp_time[CP_USER]);
cpu->nice = SIGAR_TICK2MSEC(cp_time[CP_NICE]);
cpu->sys = SIGAR_TICK2MSEC(cp_time[CP_SYS]);
cpu->idle = SIGAR_TICK2MSEC(cp_time[CP_IDLE]);
cpu->wait = 0; /*N/A*/
cpu->irq = SIGAR_TICK2MSEC(cp_time[CP_INTR]);
cpu->soft_irq = 0; /*N/A*/
cpu->stolen = 0; /*N/A*/
cpu->total = cpu->user + cpu->nice + cpu->sys + cpu->idle + cpu->irq;
cp_time += CPUSTATES;
}
}
free(times);
return status;
}
#endif
int sigar_cpu_list_get(sigar_t *sigar, sigar_cpu_list_t *cpulist)
{
#ifdef DARWIN
@ -830,7 +876,12 @@ int sigar_cpu_list_get(sigar_t *sigar, sigar_cpu_list_t *cpulist)
sigar_cpu_list_create(cpulist);
/* XXX howto multi cpu in freebsd?
#ifdef HAVE_KERN_CP_TIMES
if ((status = sigar_cp_times_get(sigar, cpulist)) == SIGAR_OK) {
return SIGAR_OK;
}
#endif
/* XXX no multi cpu in freebsd < 7.0, howbout others?
* for now just report all metrics on the 1st cpu
* 0's for the rest
*/