1 |
|
/* |
2 |
< |
* i-scream central monitoring system |
2 |
> |
* i-scream libstatgrab |
3 |
|
* http://www.i-scream.org |
4 |
|
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
26 |
|
|
27 |
|
#include <time.h> |
28 |
|
#include "statgrab.h" |
29 |
+ |
#include "tools.h" |
30 |
|
#ifdef SOLARIS |
31 |
|
#include <kstat.h> |
32 |
|
#include <sys/sysinfo.h> |
50 |
|
#include <sys/sysctl.h> |
51 |
|
#include <sys/dkstat.h> |
52 |
|
#endif |
53 |
+ |
#ifdef HPUX |
54 |
+ |
#include <sys/param.h> |
55 |
+ |
#include <sys/pstat.h> |
56 |
+ |
#include <sys/dk.h> |
57 |
+ |
#endif |
58 |
+ |
#ifdef WIN32 |
59 |
+ |
#include <pdh.h> |
60 |
+ |
#include "win32.h" |
61 |
+ |
#endif |
62 |
|
|
63 |
< |
static cpu_states_t cpu_now; |
63 |
> |
static sg_cpu_stats cpu_now; |
64 |
|
static int cpu_now_uninit=1; |
65 |
|
|
66 |
< |
cpu_states_t *get_cpu_totals(){ |
66 |
> |
sg_cpu_stats *sg_get_cpu_stats(){ |
67 |
|
|
68 |
+ |
#ifdef HPUX |
69 |
+ |
struct pst_dynamic pstat_dynamic; |
70 |
+ |
int i; |
71 |
+ |
#endif |
72 |
|
#ifdef SOLARIS |
73 |
< |
kstat_ctl_t *kc; |
74 |
< |
kstat_t *ksp; |
73 |
> |
kstat_ctl_t *kc; |
74 |
> |
kstat_t *ksp; |
75 |
|
cpu_stat_t cs; |
76 |
|
#endif |
77 |
|
#if defined(LINUX) || defined(CYGWIN) |
88 |
|
#endif |
89 |
|
size_t size; |
90 |
|
#endif |
91 |
< |
|
91 |
> |
|
92 |
|
cpu_now.user=0; |
93 |
|
/* Not stored in linux or freebsd */ |
94 |
|
cpu_now.iowait=0; |
95 |
|
cpu_now.kernel=0; |
96 |
|
cpu_now.idle=0; |
97 |
< |
/* Not stored in linux or freebsd */ |
97 |
> |
/* Not stored in linux, freebsd, hpux or windows */ |
98 |
|
cpu_now.swap=0; |
99 |
|
cpu_now.total=0; |
100 |
< |
/* Not stored in solaris */ |
100 |
> |
/* Not stored in solaris or windows */ |
101 |
|
cpu_now.nice=0; |
102 |
|
|
103 |
+ |
#ifdef HPUX |
104 |
+ |
if (pstat_getdynamic(&pstat_dynamic, sizeof(pstat_dynamic), 1, 0) == -1) { |
105 |
+ |
sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic"); |
106 |
+ |
return NULL; |
107 |
+ |
} |
108 |
+ |
cpu_now.user = pstat_dynamic.psd_cpu_time[CP_USER]; |
109 |
+ |
cpu_now.iowait = pstat_dynamic.psd_cpu_time[CP_WAIT]; |
110 |
+ |
cpu_now.kernel = pstat_dynamic.psd_cpu_time[CP_SSYS] + pstat_dynamic.psd_cpu_time[CP_SYS]; |
111 |
+ |
cpu_now.idle = pstat_dynamic.psd_cpu_time[CP_IDLE]; |
112 |
+ |
cpu_now.nice = pstat_dynamic.psd_cpu_time[CP_NICE]; |
113 |
+ |
for (i = 0; i < PST_MAX_CPUSTATES; i++) { |
114 |
+ |
cpu_now.total += pstat_dynamic.psd_cpu_time[i]; |
115 |
+ |
} |
116 |
+ |
#endif |
117 |
|
#ifdef SOLARIS |
118 |
< |
if ((kc = kstat_open()) == NULL) { |
119 |
< |
return NULL; |
120 |
< |
} |
121 |
< |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
122 |
< |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
123 |
< |
if (kstat_read(kc, ksp, &cs) == -1) { |
124 |
< |
continue; |
125 |
< |
} |
118 |
> |
if ((kc = kstat_open()) == NULL) { |
119 |
> |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
120 |
> |
return NULL; |
121 |
> |
} |
122 |
> |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
123 |
> |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
124 |
> |
if (kstat_read(kc, ksp, &cs) == -1) { |
125 |
> |
continue; |
126 |
> |
} |
127 |
|
cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER]; |
99 |
– |
cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT]; |
128 |
|
cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
129 |
|
cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE]; |
130 |
< |
cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES]; |
130 |
> |
cpu_now.iowait+=(long long)cs.cpu_sysinfo.wait[W_IO]+(long long)cs.cpu_sysinfo.wait[W_PIO]; |
131 |
> |
cpu_now.swap+=(long long)cs.cpu_sysinfo.wait[W_SWAP]; |
132 |
|
} |
133 |
|
|
134 |
|
cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap; |
135 |
|
|
136 |
< |
kstat_close(kc); |
136 |
> |
kstat_close(kc); |
137 |
|
#endif |
138 |
|
#if defined(LINUX) || defined(CYGWIN) |
139 |
|
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
140 |
+ |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat"); |
141 |
|
return NULL; |
142 |
|
} |
143 |
|
/* The very first line should be cpu */ |
146 |
|
&cpu_now.nice, \ |
147 |
|
&cpu_now.kernel, \ |
148 |
|
&cpu_now.idle)) != 4){ |
149 |
+ |
sg_set_error(SG_ERROR_PARSE, "cpu"); |
150 |
|
fclose(f); |
151 |
|
return NULL; |
152 |
|
} |
159 |
|
#if defined(FREEBSD) || defined(DFBSD) |
160 |
|
size = sizeof cp_time; |
161 |
|
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){ |
162 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "kern.cp_time"); |
163 |
|
return NULL; |
164 |
|
} |
165 |
|
#else |
171 |
|
#endif |
172 |
|
size = sizeof cp_time; |
173 |
|
if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) { |
174 |
+ |
#ifdef NETBSD |
175 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, |
176 |
+ |
"CTL_KERN.KERN_CP_TIME"); |
177 |
+ |
#else |
178 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, |
179 |
+ |
"CTL_KERN.KERN_CPTIME"); |
180 |
+ |
#endif |
181 |
|
return NULL; |
182 |
|
} |
183 |
|
#endif |
190 |
|
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
191 |
|
|
192 |
|
#endif |
193 |
+ |
#ifdef WIN32 |
194 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "Win32"); |
195 |
+ |
return NULL; |
196 |
+ |
#endif |
197 |
|
|
198 |
|
cpu_now.systime=time(NULL); |
199 |
|
cpu_now_uninit=0; |
202 |
|
return &cpu_now; |
203 |
|
} |
204 |
|
|
205 |
< |
cpu_states_t *get_cpu_diff(){ |
206 |
< |
static cpu_states_t cpu_diff; |
207 |
< |
cpu_states_t cpu_then, *cpu_tmp; |
205 |
> |
sg_cpu_stats *sg_get_cpu_stats_diff(){ |
206 |
> |
static sg_cpu_stats cpu_diff; |
207 |
> |
sg_cpu_stats cpu_then, *cpu_tmp; |
208 |
|
|
209 |
|
if (cpu_now_uninit){ |
210 |
< |
if((cpu_tmp=get_cpu_totals())==NULL){ |
211 |
< |
/* Should get_cpu_totals fail */ |
210 |
> |
if((cpu_tmp=sg_get_cpu_stats())==NULL){ |
211 |
> |
/* Should sg_get_cpu_stats fail */ |
212 |
|
return NULL; |
213 |
|
} |
214 |
|
return cpu_tmp; |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
< |
cpu_then.user=cpu_now.user; |
219 |
< |
cpu_then.kernel=cpu_now.kernel; |
220 |
< |
cpu_then.idle=cpu_now.idle; |
221 |
< |
cpu_then.iowait=cpu_now.iowait; |
222 |
< |
cpu_then.swap=cpu_now.swap; |
223 |
< |
cpu_then.nice=cpu_now.nice; |
224 |
< |
cpu_then.total=cpu_now.total; |
225 |
< |
cpu_then.systime=cpu_now.systime; |
218 |
> |
cpu_then.user=cpu_now.user; |
219 |
> |
cpu_then.kernel=cpu_now.kernel; |
220 |
> |
cpu_then.idle=cpu_now.idle; |
221 |
> |
cpu_then.iowait=cpu_now.iowait; |
222 |
> |
cpu_then.swap=cpu_now.swap; |
223 |
> |
cpu_then.nice=cpu_now.nice; |
224 |
> |
cpu_then.total=cpu_now.total; |
225 |
> |
cpu_then.systime=cpu_now.systime; |
226 |
|
|
227 |
< |
if((cpu_tmp=get_cpu_totals())==NULL){ |
227 |
> |
if((cpu_tmp=sg_get_cpu_stats())==NULL){ |
228 |
|
return NULL; |
229 |
|
} |
230 |
|
|
240 |
|
return &cpu_diff; |
241 |
|
} |
242 |
|
|
243 |
< |
cpu_percent_t *cpu_percent_usage(){ |
244 |
< |
static cpu_percent_t cpu_usage; |
245 |
< |
cpu_states_t *cs_ptr; |
243 |
> |
sg_cpu_percents *sg_get_cpu_percents(){ |
244 |
> |
static sg_cpu_percents cpu_usage; |
245 |
> |
#ifndef WIN32 |
246 |
> |
sg_cpu_stats *cs_ptr; |
247 |
|
|
248 |
< |
cs_ptr=get_cpu_diff(); |
248 |
> |
cs_ptr=sg_get_cpu_stats_diff(); |
249 |
|
if(cs_ptr==NULL){ |
250 |
|
return NULL; |
251 |
|
} |
257 |
|
cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100; |
258 |
|
cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100; |
259 |
|
cpu_usage.time_taken = cs_ptr->systime; |
260 |
+ |
#else |
261 |
+ |
double result; |
262 |
|
|
263 |
< |
return &cpu_usage; |
263 |
> |
if(read_counter_double(SG_WIN32_PROC_USER, &result)) { |
264 |
> |
sg_set_error(SG_ERROR_PDHREAD, PDH_USER); |
265 |
> |
return NULL; |
266 |
> |
} |
267 |
> |
cpu_usage.user = (float)result; |
268 |
> |
if(read_counter_double(SG_WIN32_PROC_PRIV, &result)) { |
269 |
> |
sg_set_error(SG_ERROR_PDHREAD, PDH_PRIV); |
270 |
> |
return NULL; |
271 |
> |
} |
272 |
> |
cpu_usage.kernel = (float)result; |
273 |
> |
if(read_counter_double(SG_WIN32_PROC_IDLE, &result)) { |
274 |
> |
sg_set_error(SG_ERROR_PDHREAD, PDH_IDLE); |
275 |
> |
return NULL; |
276 |
> |
} |
277 |
> |
/* win2000 does not have an idle counter, but does have %activity |
278 |
> |
* so convert it to idle */ |
279 |
> |
cpu_usage.idle = 100 - (float)result; |
280 |
> |
if(read_counter_double(SG_WIN32_PROC_INT, &result)) { |
281 |
> |
sg_set_error(SG_ERROR_PDHREAD, PDH_INTER); |
282 |
> |
return NULL; |
283 |
> |
} |
284 |
> |
cpu_usage.iowait = (float)result; |
285 |
> |
#endif |
286 |
|
|
287 |
+ |
return &cpu_usage; |
288 |
|
} |
289 |
|
|