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.1 by pajs, Tue Feb 18 19:28:30 2003 UTC vs.
Revision 1.20 by ats, Mon Apr 5 15:40:14 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
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
24   #include "config.h"
25   #endif
26  
25 #include <stdio.h>
26 #include <ukcprog.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
27   #include <time.h>
31 #include <string.h>
28   #include "statgrab.h"
29   #ifdef SOLARIS
30   #include <kstat.h>
31   #include <sys/sysinfo.h>
32 + #include <string.h>
33   #endif
34 < #ifdef FREEBSD
34 > #if defined(LINUX) || defined(CYGWIN)
35 > #include <stdio.h>
36 > #endif
37 > #if defined(FREEBSD) || defined(DFBSD)
38   #include <sys/sysctl.h>
39   #include <sys/dkstat.h>
40   #endif
41 + #ifdef NETBSD
42 + #include <sys/types.h>
43 + #include <sys/param.h>
44 + #include <sys/sysctl.h>
45 + #include <sys/sched.h>
46 + #endif
47 + #ifdef OPENBSD
48 + #include <sys/param.h>
49 + #include <sys/sysctl.h>
50 + #include <sys/dkstat.h>
51 + #endif
52  
53 < static cpu_states_t cpu_now;
53 > static sg_cpu_stats cpu_now;
54   static int cpu_now_uninit=1;
55  
56 + sg_cpu_stats *sg_get_cpu_stats(){
57  
46 cpu_states_t *get_cpu_totals(){
47
58   #ifdef SOLARIS
59          kstat_ctl_t *kc;
60          kstat_t *ksp;
61 <        cpu_stat_t cs;
62 <
61 >        cpu_stat_t cs;
62 > #endif
63 > #if defined(LINUX) || defined(CYGWIN)
64 >        FILE *f;
65 > #endif
66 > #ifdef ALLBSD
67 > #if defined(NETBSD) || defined(OPENBSD)
68 >        int mib[2];
69 > #endif
70 > #ifdef NETBSD
71 >        u_int64_t cp_time[CPUSTATES];
72 > #else
73 >        long cp_time[CPUSTATES];
74 > #endif
75 >        size_t size;
76 > #endif
77 >        
78          cpu_now.user=0;
79 +        /* Not stored in linux or freebsd */
80          cpu_now.iowait=0;
81          cpu_now.kernel=0;
82          cpu_now.idle=0;
83 +        /* Not stored in linux or freebsd */
84          cpu_now.swap=0;
85          cpu_now.total=0;
86          /* Not stored in solaris */
87          cpu_now.nice=0;
88  
89 + #ifdef SOLARIS
90          if ((kc = kstat_open()) == NULL) {
91                  return NULL;
92          }
# Line 67 | Line 95 | cpu_states_t *get_cpu_totals(){
95                  if (kstat_read(kc, ksp, &cs) == -1) {
96                          continue;
97                  }
98 <                cpu_now.user+=cs.cpu_sysinfo.cpu[CPU_USER];
99 <                cpu_now.iowait+=cs.cpu_sysinfo.cpu[CPU_WAIT];
100 <                cpu_now.kernel+=cs.cpu_sysinfo.cpu[CPU_KERNEL];
101 <                cpu_now.idle+=cs.cpu_sysinfo.cpu[CPU_IDLE];
102 <                cpu_now.swap+=cs.cpu_sysinfo.cpu[CPU_STATES];
98 >                cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
99 >                cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
100 >                cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
101 >                cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
102 >                cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
103          }
104  
105          cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
106 <        cpu_now_uninit=0;
106 >        
107 >        kstat_close(kc);
108 > #endif
109 > #if defined(LINUX) || defined(CYGWIN)
110 >        if ((f=fopen("/proc/stat", "r" ))==NULL) {
111 >                return NULL;
112 >        }
113 >        /* The very first line should be cpu */
114 >        if((fscanf(f, "cpu %lld %lld %lld %lld", \
115 >                &cpu_now.user, \
116 >                &cpu_now.nice, \
117 >                &cpu_now.kernel, \
118 >                &cpu_now.idle)) != 4){
119 >                fclose(f);
120 >                return NULL;
121 >        }
122  
123 <        if((kstat_close(kc)) != 0){
81 <                return NULL;
82 <        }
123 >        fclose(f);
124  
125 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
126   #endif
127 + #ifdef ALLBSD
128 + #if defined(FREEBSD) || defined(DFBSD)
129 +        size = sizeof cp_time;
130 +        if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
131 +                return NULL;
132 +        }
133 + #else
134 +        mib[0] = CTL_KERN;
135 + #ifdef NETBSD
136 +        mib[1] = KERN_CP_TIME;
137 + #else
138 +        mib[1] = KERN_CPTIME;
139 + #endif
140 +        size = sizeof cp_time;
141 +        if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) {
142 +                return NULL;
143 +        }
144 + #endif
145  
146 +        cpu_now.user=cp_time[CP_USER];
147 +        cpu_now.nice=cp_time[CP_NICE];
148 +        cpu_now.kernel=cp_time[CP_SYS];
149 +        cpu_now.idle=cp_time[CP_IDLE];
150 +        
151 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
152 +
153 + #endif
154 +
155          cpu_now.systime=time(NULL);
156 +        cpu_now_uninit=0;
157  
158 +
159          return &cpu_now;
160   }
161  
162 < cpu_states_t *get_cpu_diff(){
163 <        static cpu_states_t cpu_diff;
164 <        cpu_states_t cpu_then, *cpu_tmp;
162 > sg_cpu_stats *sg_get_cpu_stats_diff(){
163 >        static sg_cpu_stats cpu_diff;
164 >        sg_cpu_stats cpu_then, *cpu_tmp;
165  
166          if (cpu_now_uninit){
167 <                if((cpu_tmp=get_cpu_totals())==NULL){
168 <                /* Should get_cpu_totals fail */
167 >                if((cpu_tmp=sg_get_cpu_stats())==NULL){
168 >                /* Should sg_get_cpu_stats fail */
169                          return NULL;
170                  }
171                  return cpu_tmp;
# Line 110 | Line 181 | cpu_states_t *get_cpu_diff(){
181          cpu_then.total=cpu_now.total;
182          cpu_then.systime=cpu_now.systime;
183  
184 <        if((cpu_tmp=get_cpu_totals())==NULL){
184 >        if((cpu_tmp=sg_get_cpu_stats())==NULL){
185                  return NULL;
186          }
187  
# Line 126 | Line 197 | cpu_states_t *get_cpu_diff(){
197          return &cpu_diff;
198   }
199  
200 < cpu_percent_t *cpu_percent_usage(){
201 <        static cpu_percent_t cpu_usage;
202 <        cpu_states_t *cs_ptr;
200 > sg_cpu_percents *sg_get_cpu_percents(){
201 >        static sg_cpu_percents cpu_usage;
202 >        sg_cpu_stats *cs_ptr;
203  
204 <        cs_ptr=get_cpu_diff();
204 >        cs_ptr=sg_get_cpu_stats_diff();
205          if(cs_ptr==NULL){
206                  return NULL;
207          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines