freebsd cpu metrics

This commit is contained in:
Doug MacEachern 2005-02-09 03:54:57 +00:00
parent ac37c62faa
commit 0f4850670f
2 changed files with 62 additions and 6 deletions

View File

@ -8,6 +8,7 @@
#include <mach/message.h> #include <mach/message.h>
#include <mach/kern_return.h> #include <mach/kern_return.h>
#else #else
#include <sys/dkstat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/user.h> #include <sys/user.h>
@ -31,6 +32,43 @@
#define NMIB(mib) (sizeof(mib)/sizeof(mib[0])) #define NMIB(mib) (sizeof(mib)/sizeof(mib[0]))
#ifndef DARWIN
static int get_koffsets(sigar_t *sigar)
{
int i;
struct nlist klist[] = {
{ "_cp_time" },
{ NULL }
};
kvm_nlist(sigar->kp, klist);
if (klist[0].n_type == 0) {
return errno;
}
for (i=0; i<KOFFSET_MAX; i++) {
sigar->koffsets[i] = klist[i].n_value;
}
return SIGAR_OK;
}
static int kread(sigar_t *sigar, void *data, int size, long offset)
{
#if 0
if (sigar->kmem < 0) {
return SIGAR_EPERM_KMEM;
}
#endif
if (kvm_read(sigar->kp, offset, data, size) != size) {
return errno;
}
return SIGAR_OK;
}
#endif
int sigar_os_open(sigar_t **sigar) int sigar_os_open(sigar_t **sigar)
{ {
int mib[2]; int mib[2];
@ -60,6 +98,8 @@ int sigar_os_open(sigar_t **sigar)
(*sigar)->kp = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open"); (*sigar)->kp = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open");
#endif #endif
get_koffsets(*sigar);
(*sigar)->ncpu = ncpu; (*sigar)->ncpu = ncpu;
(*sigar)->boot_time = boottime.tv_sec; /* XXX seems off a bit */ (*sigar)->boot_time = boottime.tv_sec; /* XXX seems off a bit */
@ -239,12 +279,21 @@ int sigar_cpu_get(sigar_t *sigar, sigar_cpu_t *cpu)
cpu->total = cpu->user + cpu->nice + cpu->sys + cpu->idle; cpu->total = cpu->user + cpu->nice + cpu->sys + cpu->idle;
#else #else
/*XXX*/ int status;
cpu->user = 0; long cp_time[CPUSTATES];
cpu->nice = 0;
cpu->sys = 0; status = kread(sigar, &cp_time, sizeof(cp_time),
cpu->idle = 0; sigar->koffsets[KOFFSET_CPUINFO]);
cpu->wait = 0; /*N/A*/
if (status != SIGAR_OK) {
return status;
}
cpu->user = cp_time[CP_USER];
cpu->nice = cp_time[CP_NICE];
cpu->sys = cp_time[CP_SYS];
cpu->idle = cp_time[CP_IDLE];
cpu->wait = cp_time[CP_INTR];
cpu->total = cpu->user + cpu->nice + cpu->sys + cpu->idle; cpu->total = cpu->user + cpu->nice + cpu->sys + cpu->idle;
#endif #endif

View File

@ -10,6 +10,11 @@
#include <sys/sysctl.h> #include <sys/sysctl.h>
enum {
KOFFSET_CPUINFO,
KOFFSET_MAX
};
struct sigar_t { struct sigar_t {
SIGAR_T_BASE; SIGAR_T_BASE;
int pagesize; int pagesize;
@ -20,6 +25,8 @@ struct sigar_t {
mach_port_t mach_port; mach_port_t mach_port;
#else #else
kvm_t *kp; kvm_t *kp;
/* offsets for seeking on kmem */
unsigned long koffsets[KOFFSET_MAX];
#endif #endif
}; };