#include <sys/queue.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/sched.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include "snmpd.h"
#include "mib.h"
void timer_cpu(int, short, void *);
int percentages(int, int64_t *, int64_t *, int64_t *, int64_t *);
static int64_t **cp_time;
static int64_t **cp_old;
static int64_t **cp_diff;
struct event cpu_ev;
void
timer_cpu(int fd, short event, void *arg)
{
struct event *ev = (struct event *)arg;
struct timeval tv = { 60, 0 };
int mib[3] = { CTL_KERN, KERN_CPTIME2, 0 }, n;
size_t len;
int64_t *cptime2;
len = CPUSTATES * sizeof(int64_t);
for (n = 0; n < snmpd_env->sc_ncpu; n++) {
mib[2] = n;
cptime2 = snmpd_env->sc_cpustates + (CPUSTATES * n);
if (sysctl(mib, 3, cp_time[n], &len, NULL, 0) == -1)
continue;
(void)percentages(CPUSTATES, cptime2, cp_time[n],
cp_old[n], cp_diff[n]);
#ifdef DEBUG
log_debug("timer_cpu: cpu%d %lld%% idle in %llds", n,
(cptime2[CP_IDLE] > 1000 ?
1000 : (cptime2[CP_IDLE] / 10)), (long long) tv.tv_sec);
#endif
}
evtimer_add(ev, &tv);
}
void
timer_init(void)
{
int mib[] = { CTL_HW, HW_NCPU }, i;
size_t len;
len = sizeof(snmpd_env->sc_ncpu);
if (sysctl(mib, 2, &snmpd_env->sc_ncpu, &len, NULL, 0) == -1)
fatal("sysctl");
snmpd_env->sc_cpustates = calloc(snmpd_env->sc_ncpu,
CPUSTATES * sizeof(int64_t));
cp_time = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *));
cp_old = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *));
cp_diff = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *));
if (snmpd_env->sc_cpustates == NULL ||
cp_time == NULL || cp_old == NULL || cp_diff == NULL)
fatal("calloc");
for (i = 0; i < snmpd_env->sc_ncpu; i++) {
cp_time[i] = calloc(CPUSTATES, sizeof(int64_t));
cp_old[i] = calloc(CPUSTATES, sizeof(int64_t));
cp_diff[i] = calloc(CPUSTATES, sizeof(int64_t));
if (cp_time[i] == NULL || cp_old[i] == NULL ||
cp_diff[i] == NULL)
fatal("calloc");
}
evtimer_set(&cpu_ev, timer_cpu, &cpu_ev);
timer_cpu(0, EV_TIMEOUT, &cpu_ev);
}
int
percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs)
{
int64_t change, total_change, *dp, half_total;
int i;
total_change = 0;
dp = diffs;
for (i = 0; i < cnt; i++) {
if ((change = *new - *old) < 0) {
change = (*new - *old);
}
total_change += (*dp++ = change);
*old++ = *new++;
}
if (total_change == 0)
total_change = 1;
half_total = total_change / 2l;
for (i = 0; i < cnt; i++)
*out++ = ((*diffs++ * 1000 + half_total) / total_change);
return (total_change);
}