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.3
Committed: Sat May 18 18:15:56 2002 UTC (22 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.2: +19 -0 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * Copyright (C) 2000-2002 i-scream
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #include <stdio.h>
21 #include <ukcprog.h>
22 #include <unistd.h>
23 #ifdef SOLARIS
24 #include <kstat.h>
25 #include <sys/sysinfo.h>
26 #endif
27 #ifdef FREEBSD
28 #include <sys/sysctl.h>
29 #include <sys/dkstat.h>
30 #endif
31 #define WAIT_TIME_IN_SECS 1
32 char *get_cpu_stats(){
33 char *xml_cpu_stats;
34 double u_p=0.00;
35 double i_p=0.00;
36 double k_p=0.00;
37 double io_p=0.00;
38 double s_p=0.00;
39 #ifdef LINUX
40 long total, user, idle, kernel, nice;
41 long cpu_states[4][2];
42 FILE *f;
43 #endif
44 #ifdef SOLARIS
45 kstat_ctl_t *kc;
46 kstat_t *ksp;
47 uint_t cpu_states[5][2];
48 uint_t user, kernel, idle, iowait, swap, total;
49 cpu_stat_t cs;
50
51 #endif
52 #ifdef FREEBSD
53 long cp_time[CPUSTATES];
54 long total, user, idle, kernel, nice;
55 size_t size;
56 #endif
57
58 #ifdef LINUX
59 if ((f=fopen("/proc/stat", "r" ))==NULL) {
60 errf("Failed to open /proc/stat (%m)");
61 return NULL;
62 }
63
64 /* cpu stats should be the first line.. */
65 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){
66 errf("Failed to read in correct number of CPU stats (%m)");
67 return NULL;
68 }
69
70 if ((fclose(f)) != 0) {
71 errf("Failed to close file (%m)");
72 return NULL;
73 }
74
75 sleep(WAIT_TIME_IN_SECS);
76
77 if ((f=fopen("/proc/stat", "r" ))==NULL) {
78 errf("Failed to open /proc/stat (%m)");
79 return NULL;
80 }
81 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){
82 errf("Failed to read in correct number of CPU stats (%m)");
83 return NULL;
84 }
85
86 if ((fclose(f)) != 0) {
87 errf("Failed to close file (%m)");
88 return NULL;
89 }
90
91 user=cpu_states[0][1]-cpu_states[0][0]+cpu_states[1][1]-cpu_states[1][0];
92 kernel=cpu_states[2][1]-cpu_states[2][0];
93 idle=cpu_states[3][1]-cpu_states[3][0];
94 total=user+kernel+idle;
95
96 u_p=((double)user/(double)total)*100.00;
97 k_p=((double)kernel/(double)total)*100.00;
98 i_p=((double)idle/(double)total)*100.00;
99
100 #endif
101 #ifdef SOLARIS
102 if ((kc = kstat_open()) == NULL) {
103 errf("kstat_open failure (%m)");
104 return NULL;
105 }
106 for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
107 if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
108 if (kstat_read(kc, ksp, &cs) == -1) {
109 errf("kstat read failure");
110 continue;
111 }
112 }
113 cpu_states[0][0]=cs.cpu_sysinfo.cpu[CPU_USER];
114 cpu_states[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT];
115 cpu_states[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
116 cpu_states[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE];
117 cpu_states[4][0]=cs.cpu_sysinfo.cpu[CPU_STATES];
118
119 sleep(WAIT_TIME_IN_SECS);
120
121 if (kstat_chain_update(kc) == -1) {
122 errf("Kstat update failure (%m)");
123 return NULL;
124 }
125 for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
126 if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
127 if (kstat_read(kc, ksp, &cs) == -1) {
128 errf("kstat read failure");
129 continue;
130 }
131 }
132 cpu_states[0][1]=cs.cpu_sysinfo.cpu[CPU_USER];
133 cpu_states[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT];
134 cpu_states[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
135 cpu_states[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE];
136 cpu_states[4][1]=cs.cpu_sysinfo.cpu[CPU_STATES];
137
138 if((kstat_close(kc)) != 0){
139 errf("Failed to close kstat control structure (%m)");
140 return NULL;
141 }
142
143 user=cpu_states[0][1]-cpu_states[0][0];
144 iowait=cpu_states[1][1]-cpu_states[1][0];
145 kernel=cpu_states[2][1]-cpu_states[2][0];
146 idle=cpu_states[3][1]-cpu_states[3][0];
147 swap=cpu_states[4][1]-cpu_states[4][0];
148 total=user+kernel+idle+iowait+swap;
149
150 u_p=((double)user/(double)total)*100.00;
151 k_p=((double)kernel/(double)total)*100.00;
152 i_p=((double)idle/(double)total)*100.00;
153 io_p=((double)iowait/(double)total)*100.00;
154 s_p=((double)swap/(double)total)*100.00;
155
156 #endif
157 #ifdef FREEBSD
158
159 #define CPU_STAT_NAME "kern.cp_time"
160
161 if (sysctlbyname(CPU_STAT_NAME, NULL, &size, NULL, NULL) < 0){
162 errf("sysctrlbyname failed (%m)");
163 return NULL;
164 }
165 if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){
166 errf("Failed to get cpu stats (%m)");
167 return NULL;
168 }
169
170 user=cp_time[CP_USER];
171 nice=cp_time[CP_NICE];
172 kernel=cp_time[CP_SYS];
173 idle=cp_time[CP_IDLE];
174
175 sleep(WAIT_TIME_IN_SECS);
176
177 if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){
178 errf("Failed to get cpu stats (%m)");
179 return NULL;
180 }
181
182 user=cp_time[CP_USER]-user;
183 nice=cp_time[CP_NICE]-nice;
184 kernel=cp_time[CP_SYS]-kernel;
185 idle=cp_time[CP_IDLE]-idle;
186 total=user+nice+kernel+idle;
187
188 u_p=((float)(user+nice)/(float)total)*100.0;
189 k_p=((float)(kernel)/(float)total)*100.0;
190 i_p=((float)(idle)/(float)total)*100.0;
191
192 #endif
193
194
195 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){
196 errf("strf failed (%m)");
197 return NULL;
198 }
199
200 return xml_cpu_stats;
201 }
202