1 |
tdb |
1.16 |
/* |
2 |
pajs |
1.1 |
* i-scream central monitoring system |
3 |
tdb |
1.10 |
* http://www.i-scream.org |
4 |
tdb |
1.16 |
* Copyright (C) 2000-2004 i-scream |
5 |
pajs |
1.1 |
* |
6 |
tdb |
1.16 |
* This library is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public |
8 |
|
|
* License as published by the Free Software Foundation; either |
9 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
10 |
pajs |
1.1 |
* |
11 |
tdb |
1.16 |
* This library is distributed in the hope that it will be useful, |
12 |
pajs |
1.1 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
tdb |
1.16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* Lesser General Public License for more details. |
15 |
pajs |
1.1 |
* |
16 |
tdb |
1.16 |
* You should have received a copy of the GNU Lesser General Public |
17 |
|
|
* License along with this library; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
tdb |
1.17 |
* |
20 |
|
|
* $Id$ |
21 |
pajs |
1.1 |
*/ |
22 |
|
|
|
23 |
|
|
#ifdef HAVE_CONFIG_H |
24 |
|
|
#include "config.h" |
25 |
|
|
#endif |
26 |
|
|
|
27 |
|
|
#include <time.h> |
28 |
|
|
#include "statgrab.h" |
29 |
|
|
#ifdef SOLARIS |
30 |
|
|
#include <kstat.h> |
31 |
|
|
#include <sys/sysinfo.h> |
32 |
pajs |
1.6 |
#include <string.h> |
33 |
pajs |
1.1 |
#endif |
34 |
ats |
1.14 |
#if defined(LINUX) || defined(CYGWIN) |
35 |
pajs |
1.7 |
#include <stdio.h> |
36 |
|
|
#endif |
37 |
pajs |
1.9 |
#ifdef FREEBSD |
38 |
|
|
#include <sys/sysctl.h> |
39 |
|
|
#include <sys/dkstat.h> |
40 |
|
|
#endif |
41 |
ats |
1.13 |
#ifdef NETBSD |
42 |
|
|
#include <sys/types.h> |
43 |
|
|
#include <sys/param.h> |
44 |
|
|
#include <sys/sysctl.h> |
45 |
|
|
#include <sys/sched.h> |
46 |
|
|
#endif |
47 |
pajs |
1.1 |
|
48 |
|
|
static cpu_states_t cpu_now; |
49 |
|
|
static int cpu_now_uninit=1; |
50 |
|
|
|
51 |
|
|
cpu_states_t *get_cpu_totals(){ |
52 |
|
|
|
53 |
|
|
#ifdef SOLARIS |
54 |
|
|
kstat_ctl_t *kc; |
55 |
|
|
kstat_t *ksp; |
56 |
pajs |
1.7 |
cpu_stat_t cs; |
57 |
|
|
#endif |
58 |
tdb |
1.15 |
#if defined(LINUX) || defined(CYGWIN) |
59 |
pajs |
1.7 |
FILE *f; |
60 |
|
|
#endif |
61 |
ats |
1.13 |
#ifdef ALLBSD |
62 |
|
|
#ifndef FREEBSD |
63 |
|
|
int mib[2]; |
64 |
|
|
#endif |
65 |
|
|
#ifdef NETBSD |
66 |
|
|
u_int64_t cp_time[CPUSTATES]; |
67 |
|
|
#else |
68 |
pajs |
1.9 |
long cp_time[CPUSTATES]; |
69 |
ats |
1.13 |
#endif |
70 |
pajs |
1.9 |
size_t size; |
71 |
|
|
#endif |
72 |
pajs |
1.7 |
|
73 |
pajs |
1.1 |
cpu_now.user=0; |
74 |
pajs |
1.9 |
/* Not stored in linux or freebsd */ |
75 |
pajs |
1.1 |
cpu_now.iowait=0; |
76 |
|
|
cpu_now.kernel=0; |
77 |
|
|
cpu_now.idle=0; |
78 |
pajs |
1.9 |
/* Not stored in linux or freebsd */ |
79 |
pajs |
1.1 |
cpu_now.swap=0; |
80 |
|
|
cpu_now.total=0; |
81 |
|
|
/* Not stored in solaris */ |
82 |
|
|
cpu_now.nice=0; |
83 |
|
|
|
84 |
pajs |
1.7 |
#ifdef SOLARIS |
85 |
pajs |
1.1 |
if ((kc = kstat_open()) == NULL) { |
86 |
|
|
return NULL; |
87 |
|
|
} |
88 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
89 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
90 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
91 |
|
|
continue; |
92 |
|
|
} |
93 |
pajs |
1.5 |
cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER]; |
94 |
|
|
cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT]; |
95 |
|
|
cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
96 |
|
|
cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE]; |
97 |
|
|
cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES]; |
98 |
pajs |
1.1 |
} |
99 |
|
|
|
100 |
|
|
cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap; |
101 |
pajs |
1.7 |
|
102 |
|
|
kstat_close(kc); |
103 |
|
|
#endif |
104 |
ats |
1.14 |
#if defined(LINUX) || defined(CYGWIN) |
105 |
pajs |
1.7 |
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
106 |
|
|
return NULL; |
107 |
|
|
} |
108 |
|
|
/* The very first line should be cpu */ |
109 |
|
|
if((fscanf(f, "cpu %lld %lld %lld %lld", \ |
110 |
|
|
&cpu_now.user, \ |
111 |
|
|
&cpu_now.nice, \ |
112 |
|
|
&cpu_now.kernel, \ |
113 |
|
|
&cpu_now.idle)) != 4){ |
114 |
pajs |
1.8 |
fclose(f); |
115 |
pajs |
1.7 |
return NULL; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
fclose(f); |
119 |
pajs |
1.1 |
|
120 |
pajs |
1.7 |
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
121 |
pajs |
1.9 |
#endif |
122 |
ats |
1.13 |
#ifdef ALLBSD |
123 |
pajs |
1.9 |
#ifdef FREEBSD |
124 |
ats |
1.12 |
size = sizeof cp_time; |
125 |
ats |
1.11 |
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){ |
126 |
pajs |
1.9 |
return NULL; |
127 |
|
|
} |
128 |
ats |
1.13 |
#else |
129 |
|
|
mib[0] = CTL_KERN; |
130 |
|
|
mib[1] = KERN_CP_TIME; |
131 |
|
|
size = sizeof cp_time; |
132 |
|
|
if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) { |
133 |
|
|
return NULL; |
134 |
|
|
} |
135 |
|
|
#endif |
136 |
pajs |
1.9 |
|
137 |
|
|
cpu_now.user=cp_time[CP_USER]; |
138 |
|
|
cpu_now.nice=cp_time[CP_NICE]; |
139 |
|
|
cpu_now.kernel=cp_time[CP_SYS]; |
140 |
|
|
cpu_now.idle=cp_time[CP_IDLE]; |
141 |
|
|
|
142 |
|
|
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
143 |
|
|
|
144 |
pajs |
1.1 |
#endif |
145 |
|
|
|
146 |
|
|
cpu_now.systime=time(NULL); |
147 |
pajs |
1.7 |
cpu_now_uninit=0; |
148 |
|
|
|
149 |
pajs |
1.1 |
|
150 |
|
|
return &cpu_now; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
cpu_states_t *get_cpu_diff(){ |
154 |
|
|
static cpu_states_t cpu_diff; |
155 |
|
|
cpu_states_t cpu_then, *cpu_tmp; |
156 |
|
|
|
157 |
|
|
if (cpu_now_uninit){ |
158 |
|
|
if((cpu_tmp=get_cpu_totals())==NULL){ |
159 |
|
|
/* Should get_cpu_totals fail */ |
160 |
|
|
return NULL; |
161 |
|
|
} |
162 |
|
|
return cpu_tmp; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
|
166 |
|
|
cpu_then.user=cpu_now.user; |
167 |
|
|
cpu_then.kernel=cpu_now.kernel; |
168 |
|
|
cpu_then.idle=cpu_now.idle; |
169 |
|
|
cpu_then.iowait=cpu_now.iowait; |
170 |
|
|
cpu_then.swap=cpu_now.swap; |
171 |
|
|
cpu_then.nice=cpu_now.nice; |
172 |
|
|
cpu_then.total=cpu_now.total; |
173 |
|
|
cpu_then.systime=cpu_now.systime; |
174 |
|
|
|
175 |
|
|
if((cpu_tmp=get_cpu_totals())==NULL){ |
176 |
|
|
return NULL; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
cpu_diff.user = cpu_now.user - cpu_then.user; |
180 |
|
|
cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel; |
181 |
|
|
cpu_diff.idle = cpu_now.idle - cpu_then.idle; |
182 |
|
|
cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait; |
183 |
|
|
cpu_diff.swap = cpu_now.swap - cpu_then.swap; |
184 |
|
|
cpu_diff.nice = cpu_now.nice - cpu_then.nice; |
185 |
|
|
cpu_diff.total = cpu_now.total - cpu_then.total; |
186 |
|
|
cpu_diff.systime = cpu_now.systime - cpu_then.systime; |
187 |
|
|
|
188 |
|
|
return &cpu_diff; |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
cpu_percent_t *cpu_percent_usage(){ |
192 |
|
|
static cpu_percent_t cpu_usage; |
193 |
|
|
cpu_states_t *cs_ptr; |
194 |
|
|
|
195 |
|
|
cs_ptr=get_cpu_diff(); |
196 |
|
|
if(cs_ptr==NULL){ |
197 |
|
|
return NULL; |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100; |
201 |
|
|
cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100; |
202 |
|
|
cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100; |
203 |
|
|
cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100; |
204 |
|
|
cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100; |
205 |
|
|
cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100; |
206 |
|
|
cpu_usage.time_taken = cs_ptr->systime; |
207 |
|
|
|
208 |
|
|
return &cpu_usage; |
209 |
|
|
|
210 |
|
|
} |
211 |
|
|
|