1 |
tdb |
1.16 |
/* |
2 |
tdb |
1.21 |
* i-scream libstatgrab |
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 |
tdb |
1.26 |
* $Id: cpu_stats.c,v 1.25 2004/11/01 18:30:17 tdb Exp $ |
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 |
tdb |
1.22 |
#include "tools.h" |
30 |
pajs |
1.1 |
#ifdef SOLARIS |
31 |
|
|
#include <kstat.h> |
32 |
|
|
#include <sys/sysinfo.h> |
33 |
pajs |
1.6 |
#include <string.h> |
34 |
pajs |
1.1 |
#endif |
35 |
ats |
1.14 |
#if defined(LINUX) || defined(CYGWIN) |
36 |
pajs |
1.7 |
#include <stdio.h> |
37 |
|
|
#endif |
38 |
tdb |
1.19 |
#if defined(FREEBSD) || defined(DFBSD) |
39 |
pajs |
1.9 |
#include <sys/sysctl.h> |
40 |
|
|
#include <sys/dkstat.h> |
41 |
|
|
#endif |
42 |
ats |
1.13 |
#ifdef NETBSD |
43 |
|
|
#include <sys/types.h> |
44 |
|
|
#include <sys/param.h> |
45 |
|
|
#include <sys/sysctl.h> |
46 |
|
|
#include <sys/sched.h> |
47 |
|
|
#endif |
48 |
tdb |
1.18 |
#ifdef OPENBSD |
49 |
|
|
#include <sys/param.h> |
50 |
|
|
#include <sys/sysctl.h> |
51 |
|
|
#include <sys/dkstat.h> |
52 |
|
|
#endif |
53 |
tdb |
1.25 |
#ifdef HPUX |
54 |
|
|
#include <sys/param.h> |
55 |
|
|
#include <sys/pstat.h> |
56 |
|
|
#include <sys/dk.h> |
57 |
|
|
#endif |
58 |
tdb |
1.26 |
#ifdef WIN32 |
59 |
|
|
#include <pdh.h> |
60 |
|
|
#include "win32.h" |
61 |
|
|
#endif |
62 |
pajs |
1.1 |
|
63 |
ats |
1.20 |
static sg_cpu_stats cpu_now; |
64 |
pajs |
1.1 |
static int cpu_now_uninit=1; |
65 |
|
|
|
66 |
ats |
1.20 |
sg_cpu_stats *sg_get_cpu_stats(){ |
67 |
pajs |
1.1 |
|
68 |
tdb |
1.25 |
#ifdef HPUX |
69 |
|
|
struct pst_dynamic pstat_dynamic; |
70 |
|
|
int i; |
71 |
|
|
#endif |
72 |
pajs |
1.1 |
#ifdef SOLARIS |
73 |
tdb |
1.22 |
kstat_ctl_t *kc; |
74 |
|
|
kstat_t *ksp; |
75 |
pajs |
1.7 |
cpu_stat_t cs; |
76 |
|
|
#endif |
77 |
tdb |
1.15 |
#if defined(LINUX) || defined(CYGWIN) |
78 |
pajs |
1.7 |
FILE *f; |
79 |
|
|
#endif |
80 |
ats |
1.13 |
#ifdef ALLBSD |
81 |
tdb |
1.19 |
#if defined(NETBSD) || defined(OPENBSD) |
82 |
ats |
1.13 |
int mib[2]; |
83 |
|
|
#endif |
84 |
|
|
#ifdef NETBSD |
85 |
|
|
u_int64_t cp_time[CPUSTATES]; |
86 |
|
|
#else |
87 |
pajs |
1.9 |
long cp_time[CPUSTATES]; |
88 |
ats |
1.13 |
#endif |
89 |
pajs |
1.9 |
size_t size; |
90 |
|
|
#endif |
91 |
tdb |
1.22 |
|
92 |
pajs |
1.1 |
cpu_now.user=0; |
93 |
pajs |
1.9 |
/* Not stored in linux or freebsd */ |
94 |
pajs |
1.1 |
cpu_now.iowait=0; |
95 |
|
|
cpu_now.kernel=0; |
96 |
|
|
cpu_now.idle=0; |
97 |
tdb |
1.26 |
/* Not stored in linux, freebsd, hpux or windows */ |
98 |
pajs |
1.1 |
cpu_now.swap=0; |
99 |
|
|
cpu_now.total=0; |
100 |
tdb |
1.26 |
/* Not stored in solaris or windows */ |
101 |
pajs |
1.1 |
cpu_now.nice=0; |
102 |
|
|
|
103 |
tdb |
1.25 |
#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 |
pajs |
1.7 |
#ifdef SOLARIS |
118 |
tdb |
1.22 |
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 |
pajs |
1.5 |
cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER]; |
128 |
|
|
cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT]; |
129 |
|
|
cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
130 |
|
|
cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE]; |
131 |
|
|
cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES]; |
132 |
pajs |
1.1 |
} |
133 |
|
|
|
134 |
|
|
cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap; |
135 |
pajs |
1.7 |
|
136 |
tdb |
1.22 |
kstat_close(kc); |
137 |
pajs |
1.7 |
#endif |
138 |
ats |
1.14 |
#if defined(LINUX) || defined(CYGWIN) |
139 |
pajs |
1.7 |
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
140 |
ats |
1.24 |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat"); |
141 |
pajs |
1.7 |
return NULL; |
142 |
|
|
} |
143 |
|
|
/* The very first line should be cpu */ |
144 |
|
|
if((fscanf(f, "cpu %lld %lld %lld %lld", \ |
145 |
|
|
&cpu_now.user, \ |
146 |
|
|
&cpu_now.nice, \ |
147 |
|
|
&cpu_now.kernel, \ |
148 |
|
|
&cpu_now.idle)) != 4){ |
149 |
tdb |
1.22 |
sg_set_error(SG_ERROR_PARSE, "cpu"); |
150 |
pajs |
1.8 |
fclose(f); |
151 |
pajs |
1.7 |
return NULL; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
fclose(f); |
155 |
pajs |
1.1 |
|
156 |
pajs |
1.7 |
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
157 |
pajs |
1.9 |
#endif |
158 |
ats |
1.13 |
#ifdef ALLBSD |
159 |
tdb |
1.19 |
#if defined(FREEBSD) || defined(DFBSD) |
160 |
ats |
1.12 |
size = sizeof cp_time; |
161 |
ats |
1.11 |
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){ |
162 |
ats |
1.24 |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "kern.cp_time"); |
163 |
pajs |
1.9 |
return NULL; |
164 |
|
|
} |
165 |
ats |
1.13 |
#else |
166 |
|
|
mib[0] = CTL_KERN; |
167 |
tdb |
1.18 |
#ifdef NETBSD |
168 |
ats |
1.13 |
mib[1] = KERN_CP_TIME; |
169 |
tdb |
1.18 |
#else |
170 |
|
|
mib[1] = KERN_CPTIME; |
171 |
|
|
#endif |
172 |
ats |
1.13 |
size = sizeof cp_time; |
173 |
|
|
if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) { |
174 |
tdb |
1.22 |
#ifdef NETBSD |
175 |
ats |
1.24 |
sg_set_error_with_errno(SG_ERROR_SYSCTL, |
176 |
|
|
"CTL_KERN.KERN_CP_TIME"); |
177 |
tdb |
1.22 |
#else |
178 |
ats |
1.24 |
sg_set_error_with_errno(SG_ERROR_SYSCTL, |
179 |
|
|
"CTL_KERN.KERN_CPTIME"); |
180 |
tdb |
1.22 |
#endif |
181 |
ats |
1.13 |
return NULL; |
182 |
|
|
} |
183 |
|
|
#endif |
184 |
pajs |
1.9 |
|
185 |
|
|
cpu_now.user=cp_time[CP_USER]; |
186 |
|
|
cpu_now.nice=cp_time[CP_NICE]; |
187 |
|
|
cpu_now.kernel=cp_time[CP_SYS]; |
188 |
|
|
cpu_now.idle=cp_time[CP_IDLE]; |
189 |
|
|
|
190 |
|
|
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
191 |
|
|
|
192 |
pajs |
1.1 |
#endif |
193 |
tdb |
1.26 |
#ifdef WIN32 |
194 |
|
|
sg_set_error(SG_ERROR_UNSUPPORTED, "Win32"); |
195 |
|
|
return NULL; |
196 |
|
|
#endif |
197 |
pajs |
1.1 |
|
198 |
|
|
cpu_now.systime=time(NULL); |
199 |
pajs |
1.7 |
cpu_now_uninit=0; |
200 |
|
|
|
201 |
pajs |
1.1 |
|
202 |
|
|
return &cpu_now; |
203 |
|
|
} |
204 |
|
|
|
205 |
ats |
1.20 |
sg_cpu_stats *sg_get_cpu_stats_diff(){ |
206 |
|
|
static sg_cpu_stats cpu_diff; |
207 |
|
|
sg_cpu_stats cpu_then, *cpu_tmp; |
208 |
pajs |
1.1 |
|
209 |
|
|
if (cpu_now_uninit){ |
210 |
ats |
1.20 |
if((cpu_tmp=sg_get_cpu_stats())==NULL){ |
211 |
tdb |
1.22 |
/* Should sg_get_cpu_stats fail */ |
212 |
pajs |
1.1 |
return NULL; |
213 |
|
|
} |
214 |
|
|
return cpu_tmp; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
|
218 |
tdb |
1.22 |
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 |
pajs |
1.1 |
|
227 |
ats |
1.20 |
if((cpu_tmp=sg_get_cpu_stats())==NULL){ |
228 |
pajs |
1.1 |
return NULL; |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
cpu_diff.user = cpu_now.user - cpu_then.user; |
232 |
|
|
cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel; |
233 |
|
|
cpu_diff.idle = cpu_now.idle - cpu_then.idle; |
234 |
|
|
cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait; |
235 |
|
|
cpu_diff.swap = cpu_now.swap - cpu_then.swap; |
236 |
|
|
cpu_diff.nice = cpu_now.nice - cpu_then.nice; |
237 |
|
|
cpu_diff.total = cpu_now.total - cpu_then.total; |
238 |
|
|
cpu_diff.systime = cpu_now.systime - cpu_then.systime; |
239 |
|
|
|
240 |
|
|
return &cpu_diff; |
241 |
|
|
} |
242 |
|
|
|
243 |
ats |
1.20 |
sg_cpu_percents *sg_get_cpu_percents(){ |
244 |
|
|
static sg_cpu_percents cpu_usage; |
245 |
tdb |
1.26 |
#ifndef WIN32 |
246 |
ats |
1.20 |
sg_cpu_stats *cs_ptr; |
247 |
pajs |
1.1 |
|
248 |
ats |
1.20 |
cs_ptr=sg_get_cpu_stats_diff(); |
249 |
pajs |
1.1 |
if(cs_ptr==NULL){ |
250 |
|
|
return NULL; |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100; |
254 |
|
|
cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100; |
255 |
|
|
cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100; |
256 |
|
|
cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100; |
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 |
tdb |
1.26 |
#else |
261 |
|
|
double result; |
262 |
|
|
|
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 |
pajs |
1.1 |
|
287 |
|
|
return &cpu_usage; |
288 |
|
|
} |
289 |
|
|
|