1*5e39b809Smartijn /* $OpenBSD: timer.c,v 1.1.1.1 2022/09/01 14:20:33 martijn Exp $ */
2*5e39b809Smartijn
3*5e39b809Smartijn /*
4*5e39b809Smartijn * Copyright (c) 2008 Reyk Floeter <reyk@openbsd.org>
5*5e39b809Smartijn *
6*5e39b809Smartijn * Permission to use, copy, modify, and distribute this software for any
7*5e39b809Smartijn * purpose with or without fee is hereby granted, provided that the above
8*5e39b809Smartijn * copyright notice and this permission notice appear in all copies.
9*5e39b809Smartijn *
10*5e39b809Smartijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*5e39b809Smartijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*5e39b809Smartijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*5e39b809Smartijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*5e39b809Smartijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*5e39b809Smartijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*5e39b809Smartijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*5e39b809Smartijn */
18*5e39b809Smartijn
19*5e39b809Smartijn #include <sys/queue.h>
20*5e39b809Smartijn #include <sys/types.h>
21*5e39b809Smartijn #include <sys/time.h>
22*5e39b809Smartijn #include <sys/sched.h>
23*5e39b809Smartijn #include <sys/socket.h>
24*5e39b809Smartijn #include <sys/sysctl.h>
25*5e39b809Smartijn
26*5e39b809Smartijn #include <net/if.h>
27*5e39b809Smartijn #include <net/if_types.h>
28*5e39b809Smartijn #include <netinet/in.h>
29*5e39b809Smartijn #include <netinet/ip.h>
30*5e39b809Smartijn #include <netinet/ip_var.h>
31*5e39b809Smartijn #include <arpa/inet.h>
32*5e39b809Smartijn
33*5e39b809Smartijn #include <stdlib.h>
34*5e39b809Smartijn #include <stdio.h>
35*5e39b809Smartijn #include <errno.h>
36*5e39b809Smartijn #include <event.h>
37*5e39b809Smartijn #include <fcntl.h>
38*5e39b809Smartijn #include <string.h>
39*5e39b809Smartijn #include <unistd.h>
40*5e39b809Smartijn #include <pwd.h>
41*5e39b809Smartijn
42*5e39b809Smartijn #include "snmpd.h"
43*5e39b809Smartijn #include "mib.h"
44*5e39b809Smartijn
45*5e39b809Smartijn void timer_cpu(int, short, void *);
46*5e39b809Smartijn int percentages(int, int64_t *, int64_t *, int64_t *, int64_t *);
47*5e39b809Smartijn
48*5e39b809Smartijn static int64_t **cp_time;
49*5e39b809Smartijn static int64_t **cp_old;
50*5e39b809Smartijn static int64_t **cp_diff;
51*5e39b809Smartijn struct event cpu_ev;
52*5e39b809Smartijn
53*5e39b809Smartijn void
timer_cpu(int fd,short event,void * arg)54*5e39b809Smartijn timer_cpu(int fd, short event, void *arg)
55*5e39b809Smartijn {
56*5e39b809Smartijn struct event *ev = (struct event *)arg;
57*5e39b809Smartijn struct timeval tv = { 60, 0 }; /* every 60 seconds */
58*5e39b809Smartijn int mib[3] = { CTL_KERN, KERN_CPTIME2, 0 }, n;
59*5e39b809Smartijn size_t len;
60*5e39b809Smartijn int64_t *cptime2;
61*5e39b809Smartijn
62*5e39b809Smartijn len = CPUSTATES * sizeof(int64_t);
63*5e39b809Smartijn for (n = 0; n < snmpd_env->sc_ncpu; n++) {
64*5e39b809Smartijn mib[2] = n;
65*5e39b809Smartijn cptime2 = snmpd_env->sc_cpustates + (CPUSTATES * n);
66*5e39b809Smartijn if (sysctl(mib, 3, cp_time[n], &len, NULL, 0) == -1)
67*5e39b809Smartijn continue;
68*5e39b809Smartijn (void)percentages(CPUSTATES, cptime2, cp_time[n],
69*5e39b809Smartijn cp_old[n], cp_diff[n]);
70*5e39b809Smartijn #ifdef DEBUG
71*5e39b809Smartijn log_debug("timer_cpu: cpu%d %lld%% idle in %llds", n,
72*5e39b809Smartijn (cptime2[CP_IDLE] > 1000 ?
73*5e39b809Smartijn 1000 : (cptime2[CP_IDLE] / 10)), (long long) tv.tv_sec);
74*5e39b809Smartijn #endif
75*5e39b809Smartijn }
76*5e39b809Smartijn
77*5e39b809Smartijn evtimer_add(ev, &tv);
78*5e39b809Smartijn }
79*5e39b809Smartijn
80*5e39b809Smartijn void
timer_init(void)81*5e39b809Smartijn timer_init(void)
82*5e39b809Smartijn {
83*5e39b809Smartijn int mib[] = { CTL_HW, HW_NCPU }, i;
84*5e39b809Smartijn size_t len;
85*5e39b809Smartijn
86*5e39b809Smartijn len = sizeof(snmpd_env->sc_ncpu);
87*5e39b809Smartijn if (sysctl(mib, 2, &snmpd_env->sc_ncpu, &len, NULL, 0) == -1)
88*5e39b809Smartijn fatal("sysctl");
89*5e39b809Smartijn
90*5e39b809Smartijn snmpd_env->sc_cpustates = calloc(snmpd_env->sc_ncpu,
91*5e39b809Smartijn CPUSTATES * sizeof(int64_t));
92*5e39b809Smartijn cp_time = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *));
93*5e39b809Smartijn cp_old = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *));
94*5e39b809Smartijn cp_diff = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *));
95*5e39b809Smartijn if (snmpd_env->sc_cpustates == NULL ||
96*5e39b809Smartijn cp_time == NULL || cp_old == NULL || cp_diff == NULL)
97*5e39b809Smartijn fatal("calloc");
98*5e39b809Smartijn for (i = 0; i < snmpd_env->sc_ncpu; i++) {
99*5e39b809Smartijn cp_time[i] = calloc(CPUSTATES, sizeof(int64_t));
100*5e39b809Smartijn cp_old[i] = calloc(CPUSTATES, sizeof(int64_t));
101*5e39b809Smartijn cp_diff[i] = calloc(CPUSTATES, sizeof(int64_t));
102*5e39b809Smartijn if (cp_time[i] == NULL || cp_old[i] == NULL ||
103*5e39b809Smartijn cp_diff[i] == NULL)
104*5e39b809Smartijn fatal("calloc");
105*5e39b809Smartijn }
106*5e39b809Smartijn
107*5e39b809Smartijn evtimer_set(&cpu_ev, timer_cpu, &cpu_ev);
108*5e39b809Smartijn timer_cpu(0, EV_TIMEOUT, &cpu_ev);
109*5e39b809Smartijn }
110*5e39b809Smartijn
111*5e39b809Smartijn /*
112*5e39b809Smartijn * percentages() function to calculate CPU utilization.
113*5e39b809Smartijn * Source code derived from the top(1) utility:
114*5e39b809Smartijn *
115*5e39b809Smartijn * Copyright (c) 1984, 1989, William LeFebvre, Rice University
116*5e39b809Smartijn * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
117*5e39b809Smartijn *
118*5e39b809Smartijn * Redistribution and use in source and binary forms, with or without
119*5e39b809Smartijn * modification, are permitted provided that the following conditions
120*5e39b809Smartijn * are met:
121*5e39b809Smartijn * 1. Redistributions of source code must retain the above copyright
122*5e39b809Smartijn * notice, this list of conditions and the following disclaimer.
123*5e39b809Smartijn * 2. Redistributions in binary form must reproduce the above copyright
124*5e39b809Smartijn * notice, this list of conditions and the following disclaimer in the
125*5e39b809Smartijn * documentation and/or other materials provided with the distribution.
126*5e39b809Smartijn *
127*5e39b809Smartijn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
128*5e39b809Smartijn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
129*5e39b809Smartijn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
130*5e39b809Smartijn * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
131*5e39b809Smartijn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
132*5e39b809Smartijn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
133*5e39b809Smartijn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
134*5e39b809Smartijn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
135*5e39b809Smartijn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
136*5e39b809Smartijn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
137*5e39b809Smartijn */
138*5e39b809Smartijn int
percentages(int cnt,int64_t * out,int64_t * new,int64_t * old,int64_t * diffs)139*5e39b809Smartijn percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs)
140*5e39b809Smartijn {
141*5e39b809Smartijn int64_t change, total_change, *dp, half_total;
142*5e39b809Smartijn int i;
143*5e39b809Smartijn
144*5e39b809Smartijn /* initialization */
145*5e39b809Smartijn total_change = 0;
146*5e39b809Smartijn dp = diffs;
147*5e39b809Smartijn
148*5e39b809Smartijn /* calculate changes for each state and the overall change */
149*5e39b809Smartijn for (i = 0; i < cnt; i++) {
150*5e39b809Smartijn if ((change = *new - *old) < 0) {
151*5e39b809Smartijn /* this only happens when the counter wraps */
152*5e39b809Smartijn change = (*new - *old);
153*5e39b809Smartijn }
154*5e39b809Smartijn total_change += (*dp++ = change);
155*5e39b809Smartijn *old++ = *new++;
156*5e39b809Smartijn }
157*5e39b809Smartijn
158*5e39b809Smartijn /* avoid divide by zero potential */
159*5e39b809Smartijn if (total_change == 0)
160*5e39b809Smartijn total_change = 1;
161*5e39b809Smartijn
162*5e39b809Smartijn /* calculate percentages based on overall change, rounding up */
163*5e39b809Smartijn half_total = total_change / 2l;
164*5e39b809Smartijn for (i = 0; i < cnt; i++)
165*5e39b809Smartijn *out++ = ((*diffs++ * 1000 + half_total) / total_change);
166*5e39b809Smartijn
167*5e39b809Smartijn /* return the total in case the caller wants to use it */
168*5e39b809Smartijn return (total_change);
169*5e39b809Smartijn }
170