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