ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
Revision: 1.23
Committed: Wed Apr 7 14:53:40 2004 UTC (20 years, 1 month ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_10
Changes since 1.22: +0 -0 lines
Log Message:
Whitespace tidyup - change spaces to tabs.

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.22 * $Id: cpu_stats.c,v 1.21 2004/04/06 14:52:58 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 pajs 1.1
54 ats 1.20 static sg_cpu_stats cpu_now;
55 pajs 1.1 static int cpu_now_uninit=1;
56    
57 ats 1.20 sg_cpu_stats *sg_get_cpu_stats(){
58 pajs 1.1
59     #ifdef SOLARIS
60 tdb 1.22 kstat_ctl_t *kc;
61     kstat_t *ksp;
62 pajs 1.7 cpu_stat_t cs;
63     #endif
64 tdb 1.15 #if defined(LINUX) || defined(CYGWIN)
65 pajs 1.7 FILE *f;
66     #endif
67 ats 1.13 #ifdef ALLBSD
68 tdb 1.19 #if defined(NETBSD) || defined(OPENBSD)
69 ats 1.13 int mib[2];
70     #endif
71     #ifdef NETBSD
72     u_int64_t cp_time[CPUSTATES];
73     #else
74 pajs 1.9 long cp_time[CPUSTATES];
75 ats 1.13 #endif
76 pajs 1.9 size_t size;
77     #endif
78 tdb 1.22
79 pajs 1.1 cpu_now.user=0;
80 pajs 1.9 /* Not stored in linux or freebsd */
81 pajs 1.1 cpu_now.iowait=0;
82     cpu_now.kernel=0;
83     cpu_now.idle=0;
84 pajs 1.9 /* Not stored in linux or freebsd */
85 pajs 1.1 cpu_now.swap=0;
86     cpu_now.total=0;
87     /* Not stored in solaris */
88     cpu_now.nice=0;
89    
90 pajs 1.7 #ifdef SOLARIS
91 tdb 1.22 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 pajs 1.5 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];
103     cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
104     cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
105 pajs 1.1 }
106    
107     cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
108 pajs 1.7
109 tdb 1.22 kstat_close(kc);
110 pajs 1.7 #endif
111 ats 1.14 #if defined(LINUX) || defined(CYGWIN)
112 pajs 1.7 if ((f=fopen("/proc/stat", "r" ))==NULL) {
113 tdb 1.22 sg_set_error(SG_ERROR_OPEN, "/proc/stat");
114 pajs 1.7 return NULL;
115     }
116     /* The very first line should be cpu */
117     if((fscanf(f, "cpu %lld %lld %lld %lld", \
118     &cpu_now.user, \
119     &cpu_now.nice, \
120     &cpu_now.kernel, \
121     &cpu_now.idle)) != 4){
122 tdb 1.22 sg_set_error(SG_ERROR_PARSE, "cpu");
123 pajs 1.8 fclose(f);
124 pajs 1.7 return NULL;
125     }
126    
127     fclose(f);
128 pajs 1.1
129 pajs 1.7 cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
130 pajs 1.9 #endif
131 ats 1.13 #ifdef ALLBSD
132 tdb 1.19 #if defined(FREEBSD) || defined(DFBSD)
133 ats 1.12 size = sizeof cp_time;
134 ats 1.11 if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
135 tdb 1.22 sg_set_error(SG_ERROR_SYSCTLBYNAME, "kern.cp_time");
136 pajs 1.9 return NULL;
137     }
138 ats 1.13 #else
139     mib[0] = CTL_KERN;
140 tdb 1.18 #ifdef NETBSD
141 ats 1.13 mib[1] = KERN_CP_TIME;
142 tdb 1.18 #else
143     mib[1] = KERN_CPTIME;
144     #endif
145 ats 1.13 size = sizeof cp_time;
146     if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) {
147 tdb 1.22 #ifdef NETBSD
148     sg_set_error(SG_ERROR_SYSCTL, "CTL_KERN.KERN_CP_TIME");
149     #else
150     sg_set_error(SG_ERROR_SYSCTL, "CTL_KERN.KERN_CPTIME");
151     #endif
152 ats 1.13 return NULL;
153     }
154     #endif
155 pajs 1.9
156     cpu_now.user=cp_time[CP_USER];
157     cpu_now.nice=cp_time[CP_NICE];
158     cpu_now.kernel=cp_time[CP_SYS];
159     cpu_now.idle=cp_time[CP_IDLE];
160    
161     cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
162    
163 pajs 1.1 #endif
164    
165     cpu_now.systime=time(NULL);
166 pajs 1.7 cpu_now_uninit=0;
167    
168 pajs 1.1
169     return &cpu_now;
170     }
171    
172 ats 1.20 sg_cpu_stats *sg_get_cpu_stats_diff(){
173     static sg_cpu_stats cpu_diff;
174     sg_cpu_stats cpu_then, *cpu_tmp;
175 pajs 1.1
176     if (cpu_now_uninit){
177 ats 1.20 if((cpu_tmp=sg_get_cpu_stats())==NULL){
178 tdb 1.22 /* Should sg_get_cpu_stats fail */
179 pajs 1.1 return NULL;
180     }
181     return cpu_tmp;
182     }
183    
184    
185 tdb 1.22 cpu_then.user=cpu_now.user;
186     cpu_then.kernel=cpu_now.kernel;
187     cpu_then.idle=cpu_now.idle;
188     cpu_then.iowait=cpu_now.iowait;
189     cpu_then.swap=cpu_now.swap;
190     cpu_then.nice=cpu_now.nice;
191     cpu_then.total=cpu_now.total;
192     cpu_then.systime=cpu_now.systime;
193 pajs 1.1
194 ats 1.20 if((cpu_tmp=sg_get_cpu_stats())==NULL){
195 pajs 1.1 return NULL;
196     }
197    
198     cpu_diff.user = cpu_now.user - cpu_then.user;
199     cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel;
200     cpu_diff.idle = cpu_now.idle - cpu_then.idle;
201     cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait;
202     cpu_diff.swap = cpu_now.swap - cpu_then.swap;
203     cpu_diff.nice = cpu_now.nice - cpu_then.nice;
204     cpu_diff.total = cpu_now.total - cpu_then.total;
205     cpu_diff.systime = cpu_now.systime - cpu_then.systime;
206    
207     return &cpu_diff;
208     }
209    
210 ats 1.20 sg_cpu_percents *sg_get_cpu_percents(){
211     static sg_cpu_percents cpu_usage;
212     sg_cpu_stats *cs_ptr;
213 pajs 1.1
214 ats 1.20 cs_ptr=sg_get_cpu_stats_diff();
215 pajs 1.1 if(cs_ptr==NULL){
216     return NULL;
217     }
218    
219     cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100;
220     cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100;
221     cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100;
222     cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100;
223     cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100;
224     cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100;
225     cpu_usage.time_taken = cs_ptr->systime;
226    
227     return &cpu_usage;
228    
229     }
230