summaryrefslogtreecommitdiffstats
path: root/usr.sbin/snmpd/timer.c
blob: 26d22b8d9f01eac20125548537a1b12e6812bc93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*	$OpenBSD: timer.c,v 1.7 2016/10/28 08:01:53 rzalamena Exp $	*/

/*
 * Copyright (c) 2008 Reyk Floeter <reyk@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#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 };	/* every 60 seconds */
	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);
}

/*
 * percentages() function to calculate CPU utilization.
 * Source code derived from the top(1) utility:
 *
 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
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;

	/* initialization */
	total_change = 0;
	dp = diffs;

	/* calculate changes for each state and the overall change */
	for (i = 0; i < cnt; i++) {
		if ((change = *new - *old) < 0) {
			/* this only happens when the counter wraps */
			change = (*new - *old);
		}
		total_change += (*dp++ = change);
		*old++ = *new++;
	}

	/* avoid divide by zero potential */
	if (total_change == 0)
		total_change = 1;

	/* calculate percentages based on overall change, rounding up */
	half_total = total_change / 2l;
	for (i = 0; i < cnt; i++)
		*out++ = ((*diffs++ * 1000 + half_total) / total_change);

	/* return the total in case the caller wants to use it */
	return (total_change);
}