From 039080f722b66d825e557d941986eca0e3956ab4 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Mon, 20 Apr 2009 18:39:36 -0700 Subject: [PATCH] (SIGAR-39) Use kern.cp_times in cpu_list on FreeBSD 7.x+ --- src/os/darwin/darwin_sigar.c | 53 +++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/os/darwin/darwin_sigar.c b/src/os/darwin/darwin_sigar.c index 9f0bb8ed..fca70acd 100644 --- a/src/os/darwin/darwin_sigar.c +++ b/src/os/darwin/darwin_sigar.c @@ -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; idata[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 */