55 |
|
#include <sys/pstat.h> |
56 |
|
#include <sys/dk.h> |
57 |
|
#endif |
58 |
+ |
#ifdef AIX |
59 |
+ |
#include <strings.h> |
60 |
+ |
#include <sys/proc.h> |
61 |
+ |
#include <libperfstat.h> |
62 |
+ |
#endif |
63 |
+ |
#ifdef WIN32 |
64 |
+ |
#include <pdh.h> |
65 |
+ |
#include "win32.h" |
66 |
+ |
#endif |
67 |
|
|
68 |
|
static sg_cpu_stats cpu_now; |
69 |
|
static int cpu_now_uninit=1; |
74 |
|
struct pst_dynamic pstat_dynamic; |
75 |
|
int i; |
76 |
|
#endif |
77 |
+ |
#ifdef AIX |
78 |
+ |
perfstat_cpu_total_t all_cpu_info; |
79 |
+ |
int rc; |
80 |
+ |
#endif |
81 |
|
#ifdef SOLARIS |
82 |
|
kstat_ctl_t *kc; |
83 |
|
kstat_t *ksp; |
85 |
|
#endif |
86 |
|
#if defined(LINUX) || defined(CYGWIN) |
87 |
|
FILE *f; |
88 |
+ |
int proc_stat_cpu; |
89 |
|
#endif |
90 |
|
#ifdef ALLBSD |
91 |
|
#if defined(NETBSD) || defined(OPENBSD) |
104 |
|
cpu_now.iowait=0; |
105 |
|
cpu_now.kernel=0; |
106 |
|
cpu_now.idle=0; |
107 |
< |
/* Not stored in linux, freebsd, or hpux */ |
107 |
> |
/* Not stored in linux, freebsd, hpux, aix or windows */ |
108 |
|
cpu_now.swap=0; |
109 |
|
cpu_now.total=0; |
110 |
< |
/* Not stored in solaris */ |
110 |
> |
/* Not stored in solaris or windows */ |
111 |
|
cpu_now.nice=0; |
112 |
|
|
113 |
|
#ifdef HPUX |
124 |
|
cpu_now.total += pstat_dynamic.psd_cpu_time[i]; |
125 |
|
} |
126 |
|
#endif |
127 |
+ |
#ifdef AIX |
128 |
+ |
rc = perfstat_cpu_total( NULL, &all_cpu_info, sizeof(all_cpu_info), 1); |
129 |
+ |
if( -1 == rc ) { |
130 |
+ |
sg_set_error_with_errno(SG_ERROR_PSTAT, "perfstat_cpu_total"); |
131 |
+ |
return NULL; |
132 |
+ |
} |
133 |
+ |
|
134 |
+ |
cpu_now.user = all_cpu_info.user; |
135 |
+ |
cpu_now.iowait = all_cpu_info.wait; |
136 |
+ |
cpu_now.kernel = all_cpu_info.sys; |
137 |
+ |
cpu_now.idle = all_cpu_info.idle; |
138 |
+ |
cpu_now.total = all_cpu_info.user + all_cpu_info.wait + all_cpu_info.sys + all_cpu_info.idle; |
139 |
+ |
#endif |
140 |
|
#ifdef SOLARIS |
141 |
|
if ((kc = kstat_open()) == NULL) { |
142 |
|
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
148 |
|
continue; |
149 |
|
} |
150 |
|
cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER]; |
124 |
– |
cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT]; |
151 |
|
cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
152 |
|
cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE]; |
153 |
< |
cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES]; |
153 |
> |
cpu_now.iowait+=(long long)cs.cpu_sysinfo.wait[W_IO]+(long long)cs.cpu_sysinfo.wait[W_PIO]; |
154 |
> |
cpu_now.swap+=(long long)cs.cpu_sysinfo.wait[W_SWAP]; |
155 |
|
} |
156 |
|
|
157 |
|
cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap; |
163 |
|
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat"); |
164 |
|
return NULL; |
165 |
|
} |
166 |
+ |
|
167 |
|
/* The very first line should be cpu */ |
168 |
< |
if((fscanf(f, "cpu %lld %lld %lld %lld", \ |
168 |
> |
proc_stat_cpu = fscanf(f, "cpu %lld %lld %lld %lld %lld", \ |
169 |
|
&cpu_now.user, \ |
170 |
|
&cpu_now.nice, \ |
171 |
|
&cpu_now.kernel, \ |
172 |
< |
&cpu_now.idle)) != 4){ |
172 |
> |
&cpu_now.idle, \ |
173 |
> |
&cpu_now.iowait); |
174 |
> |
|
175 |
> |
fclose(f); |
176 |
> |
|
177 |
> |
if (proc_stat_cpu < 4 || proc_stat_cpu > 5) { |
178 |
|
sg_set_error(SG_ERROR_PARSE, "cpu"); |
146 |
– |
fclose(f); |
179 |
|
return NULL; |
180 |
|
} |
181 |
|
|
182 |
< |
fclose(f); |
182 |
> |
/* older linux doesn't give iowait */ |
183 |
> |
if (proc_stat_cpu == 4) { |
184 |
> |
cpu_now.iowait = 0; |
185 |
> |
} |
186 |
|
|
187 |
|
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
188 |
|
#endif |
221 |
|
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
222 |
|
|
223 |
|
#endif |
224 |
+ |
#ifdef WIN32 |
225 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "Win32"); |
226 |
+ |
return NULL; |
227 |
+ |
#endif |
228 |
|
|
229 |
|
cpu_now.systime=time(NULL); |
230 |
|
cpu_now_uninit=0; |
273 |
|
|
274 |
|
sg_cpu_percents *sg_get_cpu_percents(){ |
275 |
|
static sg_cpu_percents cpu_usage; |
276 |
+ |
#ifndef WIN32 |
277 |
|
sg_cpu_stats *cs_ptr; |
278 |
|
|
279 |
|
cs_ptr=sg_get_cpu_stats_diff(); |
288 |
|
cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100; |
289 |
|
cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100; |
290 |
|
cpu_usage.time_taken = cs_ptr->systime; |
291 |
+ |
#else |
292 |
+ |
double result; |
293 |
|
|
294 |
< |
return &cpu_usage; |
294 |
> |
if(read_counter_double(SG_WIN32_PROC_USER, &result)) { |
295 |
> |
sg_set_error(SG_ERROR_PDHREAD, PDH_USER); |
296 |
> |
return NULL; |
297 |
> |
} |
298 |
> |
cpu_usage.user = (float)result; |
299 |
> |
if(read_counter_double(SG_WIN32_PROC_PRIV, &result)) { |
300 |
> |
sg_set_error(SG_ERROR_PDHREAD, PDH_PRIV); |
301 |
> |
return NULL; |
302 |
> |
} |
303 |
> |
cpu_usage.kernel = (float)result; |
304 |
> |
if(read_counter_double(SG_WIN32_PROC_IDLE, &result)) { |
305 |
> |
sg_set_error(SG_ERROR_PDHREAD, PDH_IDLE); |
306 |
> |
return NULL; |
307 |
> |
} |
308 |
> |
/* win2000 does not have an idle counter, but does have %activity |
309 |
> |
* so convert it to idle */ |
310 |
> |
cpu_usage.idle = 100 - (float)result; |
311 |
> |
if(read_counter_double(SG_WIN32_PROC_INT, &result)) { |
312 |
> |
sg_set_error(SG_ERROR_PDHREAD, PDH_INTER); |
313 |
> |
return NULL; |
314 |
> |
} |
315 |
> |
cpu_usage.iowait = (float)result; |
316 |
> |
#endif |
317 |
|
|
318 |
+ |
return &cpu_usage; |
319 |
|
} |
320 |
|
|