ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/cpu_stats.c (file contents):
Revision 1.15 by tdb, Tue Nov 11 10:27:04 2003 UTC vs.
Revision 1.28 by tdb, Tue Jun 17 15:52:52 2008 UTC

# Line 1 | Line 1
1 < /*
2 < * i-scream central monitoring system
1 > /*
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 i-scream
5   *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU General Public License
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
6 > * 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   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 < * GNU General Public License for more details.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser General Public License for more details.
15   *
16 < * You should have received a copy of the GNU General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 > *
20 > * $Id$
21   */
22  
23   #ifdef HAVE_CONFIG_H
# Line 24 | Line 26
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>
# Line 32 | Line 35
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
# Line 42 | Line 45
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 + #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)
78          FILE *f;
79 +        int proc_stat_cpu;
80   #endif
81   #ifdef ALLBSD
82 < #ifndef FREEBSD
82 > #if defined(NETBSD) || defined(OPENBSD)
83          int mib[2];
84   #endif
85   #ifdef NETBSD
# Line 67 | Line 89 | cpu_states_t *get_cpu_totals(){
89   #endif
90          size_t size;
91   #endif
92 <        
92 >
93          cpu_now.user=0;
94          /* Not stored in linux or freebsd */
95          cpu_now.iowait=0;
96          cpu_now.kernel=0;
97          cpu_now.idle=0;
98 <        /* Not stored in linux or freebsd */
98 >        /* Not stored in linux, freebsd, hpux or windows */
99          cpu_now.swap=0;
100          cpu_now.total=0;
101 <        /* Not stored in solaris */
101 >        /* Not stored in solaris or windows */
102          cpu_now.nice=0;
103  
104 + #ifdef HPUX
105 +        if (pstat_getdynamic(&pstat_dynamic, sizeof(pstat_dynamic), 1, 0) == -1) {
106 +                sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic");
107 +                return NULL;
108 +        }
109 +        cpu_now.user   = pstat_dynamic.psd_cpu_time[CP_USER];
110 +        cpu_now.iowait = pstat_dynamic.psd_cpu_time[CP_WAIT];
111 +        cpu_now.kernel = pstat_dynamic.psd_cpu_time[CP_SSYS] + pstat_dynamic.psd_cpu_time[CP_SYS];
112 +        cpu_now.idle   = pstat_dynamic.psd_cpu_time[CP_IDLE];
113 +        cpu_now.nice   = pstat_dynamic.psd_cpu_time[CP_NICE];
114 +        for (i = 0; i < PST_MAX_CPUSTATES; i++) {
115 +                cpu_now.total += pstat_dynamic.psd_cpu_time[i];
116 +        }
117 + #endif
118   #ifdef SOLARIS
119 <        if ((kc = 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 <                }
119 >        if ((kc = kstat_open()) == NULL) {
120 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
121 >                return NULL;
122 >        }
123 >        for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
124 >                if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
125 >                if (kstat_read(kc, ksp, &cs) == -1) {
126 >                        continue;
127 >                }
128                  cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
92                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];
131 >                cpu_now.iowait+=(long long)cs.cpu_sysinfo.wait[W_IO]+(long long)cs.cpu_sysinfo.wait[W_PIO];
132 >                cpu_now.swap+=(long long)cs.cpu_sysinfo.wait[W_SWAP];
133          }
134  
135          cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
136          
137 <        kstat_close(kc);
137 >        kstat_close(kc);
138   #endif
139   #if defined(LINUX) || defined(CYGWIN)
140          if ((f=fopen("/proc/stat", "r" ))==NULL) {
141 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat");
142                  return NULL;
143          }
144 +
145          /* The very first line should be cpu */
146 <        if((fscanf(f, "cpu %lld %lld %lld %lld", \
146 >        proc_stat_cpu = fscanf(f, "cpu %lld %lld %lld %lld %lld", \
147                  &cpu_now.user, \
148                  &cpu_now.nice, \
149                  &cpu_now.kernel, \
150 <                &cpu_now.idle)) != 4){
151 <                fclose(f);
150 >                &cpu_now.idle, \
151 >                &cpu_now.iowait);
152 >
153 >        fclose(f);
154 >
155 >        if (proc_stat_cpu < 4 || proc_stat_cpu > 5) {
156 >                sg_set_error(SG_ERROR_PARSE, "cpu");
157                  return NULL;
158          }
159  
160 <        fclose(f);
160 >        /* older linux doesn't give iowait */
161 >        if (proc_stat_cpu == 4) {
162 >                cpu_now.iowait = 0;
163 >        }
164  
165          cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
166   #endif
167   #ifdef ALLBSD
168 < #ifdef FREEBSD
168 > #if defined(FREEBSD) || defined(DFBSD)
169          size = sizeof cp_time;
170          if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
171 +                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "kern.cp_time");
172                  return NULL;
173          }
174   #else
175          mib[0] = CTL_KERN;
176 + #ifdef NETBSD
177          mib[1] = KERN_CP_TIME;
178 + #else
179 +        mib[1] = KERN_CPTIME;
180 + #endif
181          size = sizeof cp_time;
182          if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) {
183 + #ifdef NETBSD
184 +                sg_set_error_with_errno(SG_ERROR_SYSCTL,
185 +                                        "CTL_KERN.KERN_CP_TIME");
186 + #else
187 +                sg_set_error_with_errno(SG_ERROR_SYSCTL,
188 +                                        "CTL_KERN.KERN_CPTIME");
189 + #endif
190                  return NULL;
191          }
192   #endif
# Line 140 | Line 199 | cpu_states_t *get_cpu_totals(){
199          cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
200  
201   #endif
202 + #ifdef WIN32
203 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Win32");
204 +        return NULL;
205 + #endif
206  
207          cpu_now.systime=time(NULL);
208          cpu_now_uninit=0;
# Line 148 | Line 211 | cpu_states_t *get_cpu_totals(){
211          return &cpu_now;
212   }
213  
214 < cpu_states_t *get_cpu_diff(){
215 <        static cpu_states_t cpu_diff;
216 <        cpu_states_t cpu_then, *cpu_tmp;
214 > sg_cpu_stats *sg_get_cpu_stats_diff(){
215 >        static sg_cpu_stats cpu_diff;
216 >        sg_cpu_stats cpu_then, *cpu_tmp;
217  
218          if (cpu_now_uninit){
219 <                if((cpu_tmp=get_cpu_totals())==NULL){
220 <                /* Should get_cpu_totals fail */
219 >                if((cpu_tmp=sg_get_cpu_stats())==NULL){
220 >                        /* Should sg_get_cpu_stats fail */
221                          return NULL;
222                  }
223                  return cpu_tmp;
224          }
225  
226  
227 <        cpu_then.user=cpu_now.user;
228 <        cpu_then.kernel=cpu_now.kernel;
229 <        cpu_then.idle=cpu_now.idle;
230 <        cpu_then.iowait=cpu_now.iowait;
231 <        cpu_then.swap=cpu_now.swap;
232 <        cpu_then.nice=cpu_now.nice;
233 <        cpu_then.total=cpu_now.total;
234 <        cpu_then.systime=cpu_now.systime;
227 >        cpu_then.user=cpu_now.user;
228 >        cpu_then.kernel=cpu_now.kernel;
229 >        cpu_then.idle=cpu_now.idle;
230 >        cpu_then.iowait=cpu_now.iowait;
231 >        cpu_then.swap=cpu_now.swap;
232 >        cpu_then.nice=cpu_now.nice;
233 >        cpu_then.total=cpu_now.total;
234 >        cpu_then.systime=cpu_now.systime;
235  
236 <        if((cpu_tmp=get_cpu_totals())==NULL){
236 >        if((cpu_tmp=sg_get_cpu_stats())==NULL){
237                  return NULL;
238          }
239  
# Line 186 | Line 249 | cpu_states_t *get_cpu_diff(){
249          return &cpu_diff;
250   }
251  
252 < cpu_percent_t *cpu_percent_usage(){
253 <        static cpu_percent_t cpu_usage;
254 <        cpu_states_t *cs_ptr;
252 > sg_cpu_percents *sg_get_cpu_percents(){
253 >        static sg_cpu_percents cpu_usage;
254 > #ifndef WIN32
255 >        sg_cpu_stats *cs_ptr;
256  
257 <        cs_ptr=get_cpu_diff();
257 >        cs_ptr=sg_get_cpu_stats_diff();
258          if(cs_ptr==NULL){
259                  return NULL;
260          }
# Line 202 | Line 266 | cpu_percent_t *cpu_percent_usage(){
266          cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100;
267          cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100;
268          cpu_usage.time_taken = cs_ptr->systime;
269 + #else
270 +        double result;
271  
272 <        return &cpu_usage;
272 >        if(read_counter_double(SG_WIN32_PROC_USER, &result)) {
273 >                sg_set_error(SG_ERROR_PDHREAD, PDH_USER);
274 >                return NULL;
275 >        }
276 >        cpu_usage.user = (float)result;
277 >        if(read_counter_double(SG_WIN32_PROC_PRIV, &result)) {
278 >                sg_set_error(SG_ERROR_PDHREAD, PDH_PRIV);
279 >                return NULL;
280 >        }
281 >        cpu_usage.kernel = (float)result;
282 >        if(read_counter_double(SG_WIN32_PROC_IDLE, &result)) {
283 >                sg_set_error(SG_ERROR_PDHREAD, PDH_IDLE);
284 >                return NULL;
285 >        }
286 >        /* win2000 does not have an idle counter, but does have %activity
287 >         * so convert it to idle */
288 >        cpu_usage.idle = 100 - (float)result;
289 >        if(read_counter_double(SG_WIN32_PROC_INT, &result)) {
290 >                sg_set_error(SG_ERROR_PDHREAD, PDH_INTER);
291 >                return NULL;
292 >        }
293 >        cpu_usage.iowait = (float)result;
294 > #endif
295  
296 +        return &cpu_usage;
297   }
298  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines