ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
Revision: 1.29
Committed: Sun Oct 3 18:35:57 2010 UTC (13 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.28: +24 -2 lines
Log Message:
Add support for AIX 5.x - 9.x.

Many thanks to Jens Rehsack <rehsack@googlemail.com> for providing the
patch for this work. Thanks!

File Contents

# User Rev Content
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.29 * $Id: cpu_stats.c,v 1.28 2008/06/17 15:52:52 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.29 #ifdef AIX
59     #include <strings.h>
60     #include <sys/proc.h>
61     #include <libperfstat.h>
62     #endif
63 tdb 1.26 #ifdef WIN32
64     #include <pdh.h>
65     #include "win32.h"
66     #endif
67 pajs 1.1
68 ats 1.20 static sg_cpu_stats cpu_now;
69 pajs 1.1 static int cpu_now_uninit=1;
70    
71 ats 1.20 sg_cpu_stats *sg_get_cpu_stats(){
72 pajs 1.1
73 tdb 1.25 #ifdef HPUX
74     struct pst_dynamic pstat_dynamic;
75     int i;
76     #endif
77 tdb 1.29 #ifdef AIX
78     perfstat_cpu_total_t all_cpu_info;
79     int rc;
80     #endif
81 pajs 1.1 #ifdef SOLARIS
82 tdb 1.22 kstat_ctl_t *kc;
83     kstat_t *ksp;
84 pajs 1.7 cpu_stat_t cs;
85     #endif
86 tdb 1.15 #if defined(LINUX) || defined(CYGWIN)
87 pajs 1.7 FILE *f;
88 tdb 1.28 int proc_stat_cpu;
89 pajs 1.7 #endif
90 ats 1.13 #ifdef ALLBSD
91 tdb 1.19 #if defined(NETBSD) || defined(OPENBSD)
92 ats 1.13 int mib[2];
93     #endif
94     #ifdef NETBSD
95     u_int64_t cp_time[CPUSTATES];
96     #else
97 pajs 1.9 long cp_time[CPUSTATES];
98 ats 1.13 #endif
99 pajs 1.9 size_t size;
100     #endif
101 tdb 1.22
102 pajs 1.1 cpu_now.user=0;
103 pajs 1.9 /* Not stored in linux or freebsd */
104 pajs 1.1 cpu_now.iowait=0;
105     cpu_now.kernel=0;
106     cpu_now.idle=0;
107 tdb 1.29 /* Not stored in linux, freebsd, hpux, aix or windows */
108 pajs 1.1 cpu_now.swap=0;
109     cpu_now.total=0;
110 tdb 1.26 /* Not stored in solaris or windows */
111 pajs 1.1 cpu_now.nice=0;
112    
113 tdb 1.25 #ifdef HPUX
114     if (pstat_getdynamic(&pstat_dynamic, sizeof(pstat_dynamic), 1, 0) == -1) {
115     sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic");
116     return NULL;
117     }
118     cpu_now.user = pstat_dynamic.psd_cpu_time[CP_USER];
119     cpu_now.iowait = pstat_dynamic.psd_cpu_time[CP_WAIT];
120     cpu_now.kernel = pstat_dynamic.psd_cpu_time[CP_SSYS] + pstat_dynamic.psd_cpu_time[CP_SYS];
121     cpu_now.idle = pstat_dynamic.psd_cpu_time[CP_IDLE];
122     cpu_now.nice = pstat_dynamic.psd_cpu_time[CP_NICE];
123     for (i = 0; i < PST_MAX_CPUSTATES; i++) {
124     cpu_now.total += pstat_dynamic.psd_cpu_time[i];
125     }
126     #endif
127 tdb 1.29 #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 pajs 1.7 #ifdef SOLARIS
141 tdb 1.22 if ((kc = kstat_open()) == NULL) {
142     sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
143     return NULL;
144     }
145     for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
146     if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
147     if (kstat_read(kc, ksp, &cs) == -1) {
148     continue;
149     }
150 pajs 1.5 cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
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 tdb 1.27 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 pajs 1.1 }
156    
157     cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
158 pajs 1.7
159 tdb 1.22 kstat_close(kc);
160 pajs 1.7 #endif
161 ats 1.14 #if defined(LINUX) || defined(CYGWIN)
162 pajs 1.7 if ((f=fopen("/proc/stat", "r" ))==NULL) {
163 ats 1.24 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat");
164 pajs 1.7 return NULL;
165     }
166 tdb 1.28
167 pajs 1.7 /* The very first line should be cpu */
168 tdb 1.28 proc_stat_cpu = fscanf(f, "cpu %lld %lld %lld %lld %lld", \
169 pajs 1.7 &cpu_now.user, \
170     &cpu_now.nice, \
171     &cpu_now.kernel, \
172 tdb 1.28 &cpu_now.idle, \
173     &cpu_now.iowait);
174    
175     fclose(f);
176    
177     if (proc_stat_cpu < 4 || proc_stat_cpu > 5) {
178 tdb 1.22 sg_set_error(SG_ERROR_PARSE, "cpu");
179 pajs 1.7 return NULL;
180     }
181    
182 tdb 1.28 /* older linux doesn't give iowait */
183     if (proc_stat_cpu == 4) {
184     cpu_now.iowait = 0;
185     }
186 pajs 1.1
187 pajs 1.7 cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
188 pajs 1.9 #endif
189 ats 1.13 #ifdef ALLBSD
190 tdb 1.19 #if defined(FREEBSD) || defined(DFBSD)
191 ats 1.12 size = sizeof cp_time;
192 ats 1.11 if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
193 ats 1.24 sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "kern.cp_time");
194 pajs 1.9 return NULL;
195     }
196 ats 1.13 #else
197     mib[0] = CTL_KERN;
198 tdb 1.18 #ifdef NETBSD
199 ats 1.13 mib[1] = KERN_CP_TIME;
200 tdb 1.18 #else
201     mib[1] = KERN_CPTIME;
202     #endif
203 ats 1.13 size = sizeof cp_time;
204     if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) {
205 tdb 1.22 #ifdef NETBSD
206 ats 1.24 sg_set_error_with_errno(SG_ERROR_SYSCTL,
207     "CTL_KERN.KERN_CP_TIME");
208 tdb 1.22 #else
209 ats 1.24 sg_set_error_with_errno(SG_ERROR_SYSCTL,
210     "CTL_KERN.KERN_CPTIME");
211 tdb 1.22 #endif
212 ats 1.13 return NULL;
213     }
214     #endif
215 pajs 1.9
216     cpu_now.user=cp_time[CP_USER];
217     cpu_now.nice=cp_time[CP_NICE];
218     cpu_now.kernel=cp_time[CP_SYS];
219     cpu_now.idle=cp_time[CP_IDLE];
220    
221     cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
222    
223 pajs 1.1 #endif
224 tdb 1.26 #ifdef WIN32
225     sg_set_error(SG_ERROR_UNSUPPORTED, "Win32");
226     return NULL;
227     #endif
228 pajs 1.1
229     cpu_now.systime=time(NULL);
230 pajs 1.7 cpu_now_uninit=0;
231    
232 pajs 1.1
233     return &cpu_now;
234     }
235    
236 ats 1.20 sg_cpu_stats *sg_get_cpu_stats_diff(){
237     static sg_cpu_stats cpu_diff;
238     sg_cpu_stats cpu_then, *cpu_tmp;
239 pajs 1.1
240     if (cpu_now_uninit){
241 ats 1.20 if((cpu_tmp=sg_get_cpu_stats())==NULL){
242 tdb 1.22 /* Should sg_get_cpu_stats fail */
243 pajs 1.1 return NULL;
244     }
245     return cpu_tmp;
246     }
247    
248    
249 tdb 1.22 cpu_then.user=cpu_now.user;
250     cpu_then.kernel=cpu_now.kernel;
251     cpu_then.idle=cpu_now.idle;
252     cpu_then.iowait=cpu_now.iowait;
253     cpu_then.swap=cpu_now.swap;
254     cpu_then.nice=cpu_now.nice;
255     cpu_then.total=cpu_now.total;
256     cpu_then.systime=cpu_now.systime;
257 pajs 1.1
258 ats 1.20 if((cpu_tmp=sg_get_cpu_stats())==NULL){
259 pajs 1.1 return NULL;
260     }
261    
262     cpu_diff.user = cpu_now.user - cpu_then.user;
263     cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel;
264     cpu_diff.idle = cpu_now.idle - cpu_then.idle;
265     cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait;
266     cpu_diff.swap = cpu_now.swap - cpu_then.swap;
267     cpu_diff.nice = cpu_now.nice - cpu_then.nice;
268     cpu_diff.total = cpu_now.total - cpu_then.total;
269     cpu_diff.systime = cpu_now.systime - cpu_then.systime;
270    
271     return &cpu_diff;
272     }
273    
274 ats 1.20 sg_cpu_percents *sg_get_cpu_percents(){
275     static sg_cpu_percents cpu_usage;
276 tdb 1.26 #ifndef WIN32
277 ats 1.20 sg_cpu_stats *cs_ptr;
278 pajs 1.1
279 ats 1.20 cs_ptr=sg_get_cpu_stats_diff();
280 pajs 1.1 if(cs_ptr==NULL){
281     return NULL;
282     }
283    
284     cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100;
285     cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100;
286     cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100;
287     cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100;
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 tdb 1.26 #else
292     double result;
293    
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 pajs 1.1
318     return &cpu_usage;
319     }
320