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> |
35 |
|
#if defined(LINUX) || defined(CYGWIN) |
36 |
|
#include <stdio.h> |
37 |
|
#endif |
38 |
< |
#ifdef FREEBSD |
38 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
39 |
|
#include <sys/sysctl.h> |
40 |
|
#include <sys/dkstat.h> |
41 |
|
#endif |
45 |
|
#include <sys/sysctl.h> |
46 |
|
#include <sys/sched.h> |
47 |
|
#endif |
48 |
+ |
#ifdef OPENBSD |
49 |
+ |
#include <sys/param.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 |
|
|
59 |
< |
static cpu_states_t cpu_now; |
59 |
> |
static sg_cpu_stats cpu_now; |
60 |
|
static int cpu_now_uninit=1; |
61 |
|
|
62 |
< |
cpu_states_t *get_cpu_totals(){ |
62 |
> |
sg_cpu_stats *sg_get_cpu_stats(){ |
63 |
|
|
64 |
+ |
#ifdef HPUX |
65 |
+ |
struct pst_dynamic pstat_dynamic; |
66 |
+ |
int i; |
67 |
+ |
#endif |
68 |
|
#ifdef SOLARIS |
69 |
< |
kstat_ctl_t *kc; |
70 |
< |
kstat_t *ksp; |
69 |
> |
kstat_ctl_t *kc; |
70 |
> |
kstat_t *ksp; |
71 |
|
cpu_stat_t cs; |
72 |
|
#endif |
73 |
|
#if defined(LINUX) || defined(CYGWIN) |
74 |
|
FILE *f; |
75 |
|
#endif |
76 |
|
#ifdef ALLBSD |
77 |
< |
#ifndef FREEBSD |
77 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
78 |
|
int mib[2]; |
79 |
|
#endif |
80 |
|
#ifdef NETBSD |
84 |
|
#endif |
85 |
|
size_t size; |
86 |
|
#endif |
87 |
< |
|
87 |
> |
|
88 |
|
cpu_now.user=0; |
89 |
|
/* Not stored in linux or freebsd */ |
90 |
|
cpu_now.iowait=0; |
91 |
|
cpu_now.kernel=0; |
92 |
|
cpu_now.idle=0; |
93 |
< |
/* Not stored in linux or freebsd */ |
93 |
> |
/* Not stored in linux, freebsd, or hpux */ |
94 |
|
cpu_now.swap=0; |
95 |
|
cpu_now.total=0; |
96 |
|
/* Not stored in solaris */ |
97 |
|
cpu_now.nice=0; |
98 |
|
|
99 |
+ |
#ifdef HPUX |
100 |
+ |
if (pstat_getdynamic(&pstat_dynamic, sizeof(pstat_dynamic), 1, 0) == -1) { |
101 |
+ |
sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic"); |
102 |
+ |
return NULL; |
103 |
+ |
} |
104 |
+ |
cpu_now.user = pstat_dynamic.psd_cpu_time[CP_USER]; |
105 |
+ |
cpu_now.iowait = pstat_dynamic.psd_cpu_time[CP_WAIT]; |
106 |
+ |
cpu_now.kernel = pstat_dynamic.psd_cpu_time[CP_SSYS] + pstat_dynamic.psd_cpu_time[CP_SYS]; |
107 |
+ |
cpu_now.idle = pstat_dynamic.psd_cpu_time[CP_IDLE]; |
108 |
+ |
cpu_now.nice = pstat_dynamic.psd_cpu_time[CP_NICE]; |
109 |
+ |
for (i = 0; i < PST_MAX_CPUSTATES; i++) { |
110 |
+ |
cpu_now.total += pstat_dynamic.psd_cpu_time[i]; |
111 |
+ |
} |
112 |
+ |
#endif |
113 |
|
#ifdef SOLARIS |
114 |
< |
if ((kc = kstat_open()) == NULL) { |
115 |
< |
return NULL; |
116 |
< |
} |
117 |
< |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
118 |
< |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
119 |
< |
if (kstat_read(kc, ksp, &cs) == -1) { |
120 |
< |
continue; |
121 |
< |
} |
114 |
> |
if ((kc = kstat_open()) == NULL) { |
115 |
> |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
116 |
> |
return NULL; |
117 |
> |
} |
118 |
> |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
119 |
> |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
120 |
> |
if (kstat_read(kc, ksp, &cs) == -1) { |
121 |
> |
continue; |
122 |
> |
} |
123 |
|
cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER]; |
124 |
|
cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT]; |
125 |
|
cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
129 |
|
|
130 |
|
cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap; |
131 |
|
|
132 |
< |
kstat_close(kc); |
132 |
> |
kstat_close(kc); |
133 |
|
#endif |
134 |
|
#if defined(LINUX) || defined(CYGWIN) |
135 |
|
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
136 |
+ |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat"); |
137 |
|
return NULL; |
138 |
|
} |
139 |
|
/* The very first line should be cpu */ |
142 |
|
&cpu_now.nice, \ |
143 |
|
&cpu_now.kernel, \ |
144 |
|
&cpu_now.idle)) != 4){ |
145 |
+ |
sg_set_error(SG_ERROR_PARSE, "cpu"); |
146 |
|
fclose(f); |
147 |
|
return NULL; |
148 |
|
} |
152 |
|
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
153 |
|
#endif |
154 |
|
#ifdef ALLBSD |
155 |
< |
#ifdef FREEBSD |
155 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
156 |
|
size = sizeof cp_time; |
157 |
|
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){ |
158 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "kern.cp_time"); |
159 |
|
return NULL; |
160 |
|
} |
161 |
|
#else |
162 |
|
mib[0] = CTL_KERN; |
163 |
+ |
#ifdef NETBSD |
164 |
|
mib[1] = KERN_CP_TIME; |
165 |
+ |
#else |
166 |
+ |
mib[1] = KERN_CPTIME; |
167 |
+ |
#endif |
168 |
|
size = sizeof cp_time; |
169 |
|
if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) { |
170 |
+ |
#ifdef NETBSD |
171 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, |
172 |
+ |
"CTL_KERN.KERN_CP_TIME"); |
173 |
+ |
#else |
174 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, |
175 |
+ |
"CTL_KERN.KERN_CPTIME"); |
176 |
+ |
#endif |
177 |
|
return NULL; |
178 |
|
} |
179 |
|
#endif |
194 |
|
return &cpu_now; |
195 |
|
} |
196 |
|
|
197 |
< |
cpu_states_t *get_cpu_diff(){ |
198 |
< |
static cpu_states_t cpu_diff; |
199 |
< |
cpu_states_t cpu_then, *cpu_tmp; |
197 |
> |
sg_cpu_stats *sg_get_cpu_stats_diff(){ |
198 |
> |
static sg_cpu_stats cpu_diff; |
199 |
> |
sg_cpu_stats cpu_then, *cpu_tmp; |
200 |
|
|
201 |
|
if (cpu_now_uninit){ |
202 |
< |
if((cpu_tmp=get_cpu_totals())==NULL){ |
203 |
< |
/* Should get_cpu_totals fail */ |
202 |
> |
if((cpu_tmp=sg_get_cpu_stats())==NULL){ |
203 |
> |
/* Should sg_get_cpu_stats fail */ |
204 |
|
return NULL; |
205 |
|
} |
206 |
|
return cpu_tmp; |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
< |
cpu_then.user=cpu_now.user; |
211 |
< |
cpu_then.kernel=cpu_now.kernel; |
212 |
< |
cpu_then.idle=cpu_now.idle; |
213 |
< |
cpu_then.iowait=cpu_now.iowait; |
214 |
< |
cpu_then.swap=cpu_now.swap; |
215 |
< |
cpu_then.nice=cpu_now.nice; |
216 |
< |
cpu_then.total=cpu_now.total; |
217 |
< |
cpu_then.systime=cpu_now.systime; |
210 |
> |
cpu_then.user=cpu_now.user; |
211 |
> |
cpu_then.kernel=cpu_now.kernel; |
212 |
> |
cpu_then.idle=cpu_now.idle; |
213 |
> |
cpu_then.iowait=cpu_now.iowait; |
214 |
> |
cpu_then.swap=cpu_now.swap; |
215 |
> |
cpu_then.nice=cpu_now.nice; |
216 |
> |
cpu_then.total=cpu_now.total; |
217 |
> |
cpu_then.systime=cpu_now.systime; |
218 |
|
|
219 |
< |
if((cpu_tmp=get_cpu_totals())==NULL){ |
219 |
> |
if((cpu_tmp=sg_get_cpu_stats())==NULL){ |
220 |
|
return NULL; |
221 |
|
} |
222 |
|
|
232 |
|
return &cpu_diff; |
233 |
|
} |
234 |
|
|
235 |
< |
cpu_percent_t *cpu_percent_usage(){ |
236 |
< |
static cpu_percent_t cpu_usage; |
237 |
< |
cpu_states_t *cs_ptr; |
235 |
> |
sg_cpu_percents *sg_get_cpu_percents(){ |
236 |
> |
static sg_cpu_percents cpu_usage; |
237 |
> |
sg_cpu_stats *cs_ptr; |
238 |
|
|
239 |
< |
cs_ptr=get_cpu_diff(); |
239 |
> |
cs_ptr=sg_get_cpu_stats_diff(); |
240 |
|
if(cs_ptr==NULL){ |
241 |
|
return NULL; |
242 |
|
} |