ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/cpu_stats.c
Revision: 1.4
Committed: Tue May 21 16:47:12 2002 UTC (22 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.3: +1 -0 lines
Log Message:
Added URL to GPL headers.

File Contents

# User Rev Content
1 tdb 1.3 /*
2     * i-scream central monitoring system
3 tdb 1.4 * http://www.i-scream.org.uk
4 tdb 1.3 * Copyright (C) 2000-2002 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.
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 pajs 1.1 #include <stdio.h>
22 pajs 1.2 #include <ukcprog.h>
23 pajs 1.1 #include <unistd.h>
24 pajs 1.2 #ifdef SOLARIS
25     #include <kstat.h>
26     #include <sys/sysinfo.h>
27     #endif
28     #ifdef FREEBSD
29     #include <sys/sysctl.h>
30     #include <sys/dkstat.h>
31     #endif
32 pajs 1.1 #define WAIT_TIME_IN_SECS 1
33     char *get_cpu_stats(){
34 pajs 1.2 char *xml_cpu_stats;
35     double u_p=0.00;
36     double i_p=0.00;
37     double k_p=0.00;
38     double io_p=0.00;
39     double s_p=0.00;
40 pajs 1.1 #ifdef LINUX
41     long total, user, idle, kernel, nice;
42     long cpu_states[4][2];
43     FILE *f;
44     #endif
45 pajs 1.2 #ifdef SOLARIS
46     kstat_ctl_t *kc;
47     kstat_t *ksp;
48     uint_t cpu_states[5][2];
49     uint_t user, kernel, idle, iowait, swap, total;
50     cpu_stat_t cs;
51    
52     #endif
53     #ifdef FREEBSD
54     long cp_time[CPUSTATES];
55     long total, user, idle, kernel, nice;
56     size_t size;
57     #endif
58 pajs 1.1
59     #ifdef LINUX
60     if ((f=fopen("/proc/stat", "r" ))==NULL) {
61     errf("Failed to open /proc/stat (%m)");
62     return NULL;
63     }
64    
65     /* cpu stats should be the first line.. */
66     if((fscanf(f,"%*s %ld %ld %ld %ld", &cpu_states[0][0], &cpu_states[1][0], &cpu_states[2][0] ,&cpu_states[3][0])) != 4){
67     errf("Failed to read in correct number of CPU stats (%m)");
68     return NULL;
69     }
70    
71     if ((fclose(f)) != 0) {
72     errf("Failed to close file (%m)");
73     return NULL;
74     }
75    
76     sleep(WAIT_TIME_IN_SECS);
77    
78     if ((f=fopen("/proc/stat", "r" ))==NULL) {
79     errf("Failed to open /proc/stat (%m)");
80     return NULL;
81     }
82     if((fscanf(f,"%*s %ld %ld %ld %ld", &cpu_states[0][1], &cpu_states[1][1], &cpu_states[2][1] ,&cpu_states[3][1])) != 4){
83     errf("Failed to read in correct number of CPU stats (%m)");
84     return NULL;
85     }
86    
87     if ((fclose(f)) != 0) {
88     errf("Failed to close file (%m)");
89     return NULL;
90     }
91    
92     user=cpu_states[0][1]-cpu_states[0][0]+cpu_states[1][1]-cpu_states[1][0];
93     kernel=cpu_states[2][1]-cpu_states[2][0];
94     idle=cpu_states[3][1]-cpu_states[3][0];
95     total=user+kernel+idle;
96    
97     u_p=((double)user/(double)total)*100.00;
98     k_p=((double)kernel/(double)total)*100.00;
99     i_p=((double)idle/(double)total)*100.00;
100    
101     #endif
102 pajs 1.2 #ifdef SOLARIS
103     if ((kc = kstat_open()) == NULL) {
104     errf("kstat_open failure (%m)");
105     return NULL;
106     }
107     for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
108     if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
109     if (kstat_read(kc, ksp, &cs) == -1) {
110     errf("kstat read failure");
111     continue;
112     }
113     }
114     cpu_states[0][0]=cs.cpu_sysinfo.cpu[CPU_USER];
115     cpu_states[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT];
116     cpu_states[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
117     cpu_states[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE];
118     cpu_states[4][0]=cs.cpu_sysinfo.cpu[CPU_STATES];
119    
120     sleep(WAIT_TIME_IN_SECS);
121    
122     if (kstat_chain_update(kc) == -1) {
123     errf("Kstat update failure (%m)");
124     return NULL;
125     }
126     for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
127     if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
128     if (kstat_read(kc, ksp, &cs) == -1) {
129     errf("kstat read failure");
130     continue;
131     }
132     }
133     cpu_states[0][1]=cs.cpu_sysinfo.cpu[CPU_USER];
134     cpu_states[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT];
135     cpu_states[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
136     cpu_states[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE];
137     cpu_states[4][1]=cs.cpu_sysinfo.cpu[CPU_STATES];
138    
139     if((kstat_close(kc)) != 0){
140     errf("Failed to close kstat control structure (%m)");
141     return NULL;
142     }
143    
144     user=cpu_states[0][1]-cpu_states[0][0];
145     iowait=cpu_states[1][1]-cpu_states[1][0];
146     kernel=cpu_states[2][1]-cpu_states[2][0];
147     idle=cpu_states[3][1]-cpu_states[3][0];
148     swap=cpu_states[4][1]-cpu_states[4][0];
149     total=user+kernel+idle+iowait+swap;
150    
151     u_p=((double)user/(double)total)*100.00;
152     k_p=((double)kernel/(double)total)*100.00;
153     i_p=((double)idle/(double)total)*100.00;
154     io_p=((double)iowait/(double)total)*100.00;
155     s_p=((double)swap/(double)total)*100.00;
156    
157     #endif
158     #ifdef FREEBSD
159    
160     #define CPU_STAT_NAME "kern.cp_time"
161    
162     if (sysctlbyname(CPU_STAT_NAME, NULL, &size, NULL, NULL) < 0){
163     errf("sysctrlbyname failed (%m)");
164     return NULL;
165     }
166     if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){
167     errf("Failed to get cpu stats (%m)");
168     return NULL;
169     }
170    
171     user=cp_time[CP_USER];
172     nice=cp_time[CP_NICE];
173     kernel=cp_time[CP_SYS];
174     idle=cp_time[CP_IDLE];
175    
176     sleep(WAIT_TIME_IN_SECS);
177    
178     if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){
179     errf("Failed to get cpu stats (%m)");
180     return NULL;
181     }
182    
183     user=cp_time[CP_USER]-user;
184     nice=cp_time[CP_NICE]-nice;
185     kernel=cp_time[CP_SYS]-kernel;
186     idle=cp_time[CP_IDLE]-idle;
187     total=user+nice+kernel+idle;
188    
189     u_p=((float)(user+nice)/(float)total)*100.0;
190     k_p=((float)(kernel)/(float)total)*100.0;
191     i_p=((float)(idle)/(float)total)*100.0;
192 pajs 1.1
193 pajs 1.2 #endif
194    
195    
196     if((xml_cpu_stats=strf("<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", u_p, k_p, i_p, io_p, s_p)) == NULL){
197     errf("strf failed (%m)");
198     return NULL;
199     }
200    
201     return xml_cpu_stats;
202 pajs 1.1 }
203