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.7 by pajs, Mon Mar 3 15:26:23 2003 UTC vs.
Revision 1.24 by ats, Sun Jul 18 21:30:11 2004 UTC

# Line 1 | Line 1
1 < /*
2 < * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
1 > /*
2 > * i-scream libstatgrab
3 > * http://www.i-scream.org
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>
33   #include <string.h>
34   #endif
35 < #ifdef LINUX
35 > #if defined(LINUX) || defined(CYGWIN)
36   #include <stdio.h>
37   #endif
38 + #if defined(FREEBSD) || defined(DFBSD)
39 + #include <sys/sysctl.h>
40 + #include <sys/dkstat.h>
41 + #endif
42 + #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 + #ifdef OPENBSD
49 + #include <sys/param.h>
50 + #include <sys/sysctl.h>
51 + #include <sys/dkstat.h>
52 + #endif
53  
54 < static cpu_states_t cpu_now;
54 > static sg_cpu_stats cpu_now;
55   static int cpu_now_uninit=1;
56  
57 < cpu_states_t *get_cpu_totals(){
57 > sg_cpu_stats *sg_get_cpu_stats(){
58  
59   #ifdef SOLARIS
60 <        kstat_ctl_t *kc;
61 <        kstat_t *ksp;
60 >        kstat_ctl_t *kc;
61 >        kstat_t *ksp;
62          cpu_stat_t cs;
63   #endif
64 < #ifdef LINUX
64 > #if defined(LINUX) || defined(CYGWIN)
65          FILE *f;
66   #endif
67 <        
67 > #ifdef ALLBSD
68 > #if defined(NETBSD) || defined(OPENBSD)
69 >        int mib[2];
70 > #endif
71 > #ifdef NETBSD
72 >        u_int64_t cp_time[CPUSTATES];
73 > #else
74 >        long cp_time[CPUSTATES];
75 > #endif
76 >        size_t size;
77 > #endif
78 >
79          cpu_now.user=0;
80 <        /* Not stored in linux */
80 >        /* Not stored in linux or freebsd */
81          cpu_now.iowait=0;
82          cpu_now.kernel=0;
83          cpu_now.idle=0;
84 <        /* Not stored in linux */
84 >        /* Not stored in linux or freebsd */
85          cpu_now.swap=0;
86          cpu_now.total=0;
87          /* Not stored in solaris */
88          cpu_now.nice=0;
89  
90   #ifdef SOLARIS
91 <        if ((kc = kstat_open()) == NULL) {
92 <                return NULL;
93 <        }
94 <        for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
95 <                if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
96 <                if (kstat_read(kc, ksp, &cs) == -1) {
97 <                        continue;
98 <                }
91 >        if ((kc = kstat_open()) == NULL) {
92 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
93 >                return NULL;
94 >        }
95 >        for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
96 >                if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
97 >                if (kstat_read(kc, ksp, &cs) == -1) {
98 >                        continue;
99 >                }
100                  cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
101                  cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
102                  cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
# Line 76 | Line 106 | cpu_states_t *get_cpu_totals(){
106  
107          cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
108          
109 <        kstat_close(kc);
109 >        kstat_close(kc);
110   #endif
111 < #ifdef LINUX
111 > #if defined(LINUX) || defined(CYGWIN)
112          if ((f=fopen("/proc/stat", "r" ))==NULL) {
113 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat");
114                  return NULL;
115          }
116          /* The very first line should be cpu */
# Line 88 | Line 119 | cpu_states_t *get_cpu_totals(){
119                  &cpu_now.nice, \
120                  &cpu_now.kernel, \
121                  &cpu_now.idle)) != 4){
122 +                sg_set_error(SG_ERROR_PARSE, "cpu");
123 +                fclose(f);
124                  return NULL;
125          }
126  
# Line 95 | Line 128 | cpu_states_t *get_cpu_totals(){
128  
129          cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
130   #endif
131 + #ifdef ALLBSD
132 + #if defined(FREEBSD) || defined(DFBSD)
133 +        size = sizeof cp_time;
134 +        if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
135 +                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "kern.cp_time");
136 +                return NULL;
137 +        }
138 + #else
139 +        mib[0] = CTL_KERN;
140 + #ifdef NETBSD
141 +        mib[1] = KERN_CP_TIME;
142 + #else
143 +        mib[1] = KERN_CPTIME;
144 + #endif
145 +        size = sizeof cp_time;
146 +        if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) {
147 + #ifdef NETBSD
148 +                sg_set_error_with_errno(SG_ERROR_SYSCTL,
149 +                                        "CTL_KERN.KERN_CP_TIME");
150 + #else
151 +                sg_set_error_with_errno(SG_ERROR_SYSCTL,
152 +                                        "CTL_KERN.KERN_CPTIME");
153 + #endif
154 +                return NULL;
155 +        }
156 + #endif
157  
158 +        cpu_now.user=cp_time[CP_USER];
159 +        cpu_now.nice=cp_time[CP_NICE];
160 +        cpu_now.kernel=cp_time[CP_SYS];
161 +        cpu_now.idle=cp_time[CP_IDLE];
162 +        
163 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
164 +
165 + #endif
166 +
167          cpu_now.systime=time(NULL);
168          cpu_now_uninit=0;
169  
# Line 103 | Line 171 | cpu_states_t *get_cpu_totals(){
171          return &cpu_now;
172   }
173  
174 < cpu_states_t *get_cpu_diff(){
175 <        static cpu_states_t cpu_diff;
176 <        cpu_states_t cpu_then, *cpu_tmp;
174 > sg_cpu_stats *sg_get_cpu_stats_diff(){
175 >        static sg_cpu_stats cpu_diff;
176 >        sg_cpu_stats cpu_then, *cpu_tmp;
177  
178          if (cpu_now_uninit){
179 <                if((cpu_tmp=get_cpu_totals())==NULL){
180 <                /* Should get_cpu_totals fail */
179 >                if((cpu_tmp=sg_get_cpu_stats())==NULL){
180 >                        /* Should sg_get_cpu_stats fail */
181                          return NULL;
182                  }
183                  return cpu_tmp;
184          }
185  
186  
187 <        cpu_then.user=cpu_now.user;
188 <        cpu_then.kernel=cpu_now.kernel;
189 <        cpu_then.idle=cpu_now.idle;
190 <        cpu_then.iowait=cpu_now.iowait;
191 <        cpu_then.swap=cpu_now.swap;
192 <        cpu_then.nice=cpu_now.nice;
193 <        cpu_then.total=cpu_now.total;
194 <        cpu_then.systime=cpu_now.systime;
187 >        cpu_then.user=cpu_now.user;
188 >        cpu_then.kernel=cpu_now.kernel;
189 >        cpu_then.idle=cpu_now.idle;
190 >        cpu_then.iowait=cpu_now.iowait;
191 >        cpu_then.swap=cpu_now.swap;
192 >        cpu_then.nice=cpu_now.nice;
193 >        cpu_then.total=cpu_now.total;
194 >        cpu_then.systime=cpu_now.systime;
195  
196 <        if((cpu_tmp=get_cpu_totals())==NULL){
196 >        if((cpu_tmp=sg_get_cpu_stats())==NULL){
197                  return NULL;
198          }
199  
# Line 141 | Line 209 | cpu_states_t *get_cpu_diff(){
209          return &cpu_diff;
210   }
211  
212 < cpu_percent_t *cpu_percent_usage(){
213 <        static cpu_percent_t cpu_usage;
214 <        cpu_states_t *cs_ptr;
212 > sg_cpu_percents *sg_get_cpu_percents(){
213 >        static sg_cpu_percents cpu_usage;
214 >        sg_cpu_stats *cs_ptr;
215  
216 <        cs_ptr=get_cpu_diff();
216 >        cs_ptr=sg_get_cpu_stats_diff();
217          if(cs_ptr==NULL){
218                  return NULL;
219          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines