1 /*
2  * Copyright (c) 2001-2007 Willem Dijkstra
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  *    - Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *    - Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials provided
14  *      with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 
31 /*
32  * Get current cpu statistics in percentages (total of all counts = 100.0)
33  * and returns them in symon_buf as
34  *
35  * user : nice : system : interrupt : idle
36  *
37  * This module uses the sysctl interface and can run as any user.
38  */
39 
40 #include "conf.h"
41 
42 #include <sys/dkstat.h>
43 #include <sys/param.h>
44 #include <sys/sysctl.h>
45 
46 #include <errno.h>
47 #include <limits.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "error.h"
52 #include "percentages.h"
53 #include "symon.h"
54 
55 void
init_cpu(struct stream * st)56 init_cpu(struct stream *st)
57 {
58     char buf[SYMON_MAX_OBJSIZE];
59 #ifdef HAS_KERN_CPTIME2
60     const char *errstr;
61     int mib[2] = {CTL_HW, HW_NCPU};
62     int ncpu;
63     long num;
64     size_t size = sizeof(ncpu);
65 #endif
66 
67     st->parg.cp.mib[0] = CTL_KERN;
68     st->parg.cp.mib[1] = KERN_CPTIME;
69     st->parg.cp.miblen = 2;
70 
71 #ifdef HAS_KERN_CPTIME2
72     if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1) {
73         warning("could not determine number of cpus: %.200s", strerror(errno));
74         ncpu = 1;
75     }
76 
77     num = strtonum(st->arg, 0, SYMON_MAXCPUID - 1, &errstr);
78     if (errstr != NULL) {
79         fatal("cpu(%.200s) is invalid: %.200s", st->arg, errstr);
80     }
81 
82     if (ncpu > 1) {
83         st->parg.cp.mib[0] = CTL_KERN;
84         st->parg.cp.mib[1] = KERN_CPTIME2;
85         st->parg.cp.mib[2] = num;
86         st->parg.cp.miblen = 3;
87         if (st->parg.cp.mib[2] >= ncpu) {
88             fatal("cpu(%d) is not present", st->parg.cp.mib[2]);
89         }
90     }
91 #endif
92 
93     /* Call get_cpu once to fill the cp_old structure */
94     get_cpu(buf, sizeof(buf), st);
95 
96     info("started module cpu(%.200s)", st->arg);
97 }
98 
99 void
gets_cpu()100 gets_cpu()
101 {
102     /* EMPTY */
103 }
104 
105 int
get_cpu(char * symon_buf,int maxlen,struct stream * st)106 get_cpu(char *symon_buf, int maxlen, struct stream *st)
107 {
108     int i, total;
109     size_t len;
110 
111     len = sizeof(st->parg.cp.time2);
112     if (sysctl(st->parg.cp.mib, st->parg.cp.miblen, &st->parg.cp.time2, &len, NULL, 0) < 0) {
113         warning("%s:%d: sysctl kern.cp_time* for cpu%d failed", __FILE__, __LINE__,
114           st->parg.cp.mib[2]);
115         return 0;
116     }
117 
118     /* sysctl will return 32 bit longs for CPTIME and 64 bit longs for CPTIME2 */
119     if (st->parg.cp.mib[1] == KERN_CPTIME) {
120         bcopy(st->parg.cp.time2, st->parg.cp.time1, sizeof(st->parg.cp.time1));
121         for (i = 0; i < CPUSTATES; i++) {
122             st->parg.cp.time2[i] = (int64_t) st->parg.cp.time1[i];
123         }
124     }
125 
126     /* convert cp_time counts to percentages */
127     total = percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time2, st->parg.cp.old, st->parg.cp.diff);
128 
129     return snpack(symon_buf, maxlen, st->arg, MT_CPU,
130                   (double) (st->parg.cp.states[CP_USER] / 10.0),
131                   (double) (st->parg.cp.states[CP_NICE] / 10.0),
132                   (double) (st->parg.cp.states[CP_SYS] / 10.0),
133                   (double) (st->parg.cp.states[CP_INTR] / 10.0),
134                   (double) (st->parg.cp.states[CP_IDLE] / 10.0));
135 }
136