ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
Revision: 1.11
Committed: Sat Oct 18 22:15:35 2003 UTC (20 years, 7 months ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.10: +1 -4 lines
Log Message:
You only need to call sysctl{,byname}() twice if you're actually going
  to do something with the size the first call returns.
The last argument to sysctl{,byname}() is a size_t, not a pointer, so it
  should be 0, not NULL.

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3 tdb 1.10 * http://www.i-scream.org
4     * Copyright (C) 2000-2003 i-scream
5 pajs 1.1 *
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.
10     *
11     * This program 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.
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.
19     */
20    
21     #ifdef HAVE_CONFIG_H
22     #include "config.h"
23     #endif
24    
25     #include <time.h>
26     #include "statgrab.h"
27     #ifdef SOLARIS
28     #include <kstat.h>
29     #include <sys/sysinfo.h>
30 pajs 1.6 #include <string.h>
31 pajs 1.1 #endif
32 pajs 1.7 #ifdef LINUX
33     #include <stdio.h>
34     #endif
35 pajs 1.9 #ifdef FREEBSD
36     #include <sys/sysctl.h>
37     #include <sys/dkstat.h>
38     #endif
39 pajs 1.1
40     static cpu_states_t cpu_now;
41     static int cpu_now_uninit=1;
42    
43     cpu_states_t *get_cpu_totals(){
44    
45     #ifdef SOLARIS
46     kstat_ctl_t *kc;
47     kstat_t *ksp;
48 pajs 1.7 cpu_stat_t cs;
49     #endif
50     #ifdef LINUX
51     FILE *f;
52     #endif
53 pajs 1.9 #ifdef FREEBSD
54     long cp_time[CPUSTATES];
55     size_t size;
56     #endif
57 pajs 1.7
58 pajs 1.1 cpu_now.user=0;
59 pajs 1.9 /* Not stored in linux or freebsd */
60 pajs 1.1 cpu_now.iowait=0;
61     cpu_now.kernel=0;
62     cpu_now.idle=0;
63 pajs 1.9 /* Not stored in linux or freebsd */
64 pajs 1.1 cpu_now.swap=0;
65     cpu_now.total=0;
66     /* Not stored in solaris */
67     cpu_now.nice=0;
68    
69 pajs 1.7 #ifdef SOLARIS
70 pajs 1.1 if ((kc = kstat_open()) == NULL) {
71     return NULL;
72     }
73     for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
74     if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
75     if (kstat_read(kc, ksp, &cs) == -1) {
76     continue;
77     }
78 pajs 1.5 cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
79     cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
80     cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
81     cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
82     cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
83 pajs 1.1 }
84    
85     cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
86 pajs 1.7
87     kstat_close(kc);
88     #endif
89     #ifdef LINUX
90     if ((f=fopen("/proc/stat", "r" ))==NULL) {
91     return NULL;
92     }
93     /* The very first line should be cpu */
94     if((fscanf(f, "cpu %lld %lld %lld %lld", \
95     &cpu_now.user, \
96     &cpu_now.nice, \
97     &cpu_now.kernel, \
98     &cpu_now.idle)) != 4){
99 pajs 1.8 fclose(f);
100 pajs 1.7 return NULL;
101     }
102    
103     fclose(f);
104 pajs 1.1
105 pajs 1.7 cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
106 pajs 1.9 #endif
107     #ifdef FREEBSD
108 ats 1.11 if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
109 pajs 1.9 return NULL;
110     }
111    
112     cpu_now.user=cp_time[CP_USER];
113     cpu_now.nice=cp_time[CP_NICE];
114     cpu_now.kernel=cp_time[CP_SYS];
115     cpu_now.idle=cp_time[CP_IDLE];
116    
117     cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
118    
119 pajs 1.1 #endif
120    
121     cpu_now.systime=time(NULL);
122 pajs 1.7 cpu_now_uninit=0;
123    
124 pajs 1.1
125     return &cpu_now;
126     }
127    
128     cpu_states_t *get_cpu_diff(){
129     static cpu_states_t cpu_diff;
130     cpu_states_t cpu_then, *cpu_tmp;
131    
132     if (cpu_now_uninit){
133     if((cpu_tmp=get_cpu_totals())==NULL){
134     /* Should get_cpu_totals fail */
135     return NULL;
136     }
137     return cpu_tmp;
138     }
139    
140    
141     cpu_then.user=cpu_now.user;
142     cpu_then.kernel=cpu_now.kernel;
143     cpu_then.idle=cpu_now.idle;
144     cpu_then.iowait=cpu_now.iowait;
145     cpu_then.swap=cpu_now.swap;
146     cpu_then.nice=cpu_now.nice;
147     cpu_then.total=cpu_now.total;
148     cpu_then.systime=cpu_now.systime;
149    
150     if((cpu_tmp=get_cpu_totals())==NULL){
151     return NULL;
152     }
153    
154     cpu_diff.user = cpu_now.user - cpu_then.user;
155     cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel;
156     cpu_diff.idle = cpu_now.idle - cpu_then.idle;
157     cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait;
158     cpu_diff.swap = cpu_now.swap - cpu_then.swap;
159     cpu_diff.nice = cpu_now.nice - cpu_then.nice;
160     cpu_diff.total = cpu_now.total - cpu_then.total;
161     cpu_diff.systime = cpu_now.systime - cpu_then.systime;
162    
163     return &cpu_diff;
164     }
165    
166     cpu_percent_t *cpu_percent_usage(){
167     static cpu_percent_t cpu_usage;
168     cpu_states_t *cs_ptr;
169    
170     cs_ptr=get_cpu_diff();
171     if(cs_ptr==NULL){
172     return NULL;
173     }
174    
175     cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100;
176     cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100;
177     cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100;
178     cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100;
179     cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100;
180     cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100;
181     cpu_usage.time_taken = cs_ptr->systime;
182    
183     return &cpu_usage;
184    
185     }
186