ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/cpu_stats.c
(Generate patch)

Comparing projects/cms/source/ihost/libstatgrab/cpu_stats.c (file contents):
Revision 1.1 by pajs, Mon May 13 17:10:09 2002 UTC vs.
Revision 1.2 by pajs, Tue May 14 12:01:16 2002 UTC

# Line 1 | Line 1
1   #include <stdio.h>
2 < #include <local/ukcprog.h>
2 > #include <ukcprog.h>
3   #include <unistd.h>
4 <
4 > #ifdef SOLARIS
5 > #include <kstat.h>
6 > #include <sys/sysinfo.h>
7 > #endif
8 > #ifdef FREEBSD
9 > #include <sys/sysctl.h>
10 > #include <sys/dkstat.h>
11 > #endif
12   #define WAIT_TIME_IN_SECS 1
13   char *get_cpu_stats(){
14 <                double u_p, i_p, k_p;
14 >                char *xml_cpu_stats;
15 >                double u_p=0.00;
16 >                double i_p=0.00;
17 >                double k_p=0.00;
18 >                double io_p=0.00;
19 >                double s_p=0.00;
20   #ifdef LINUX
21          long total, user, idle, kernel, nice;
22          long cpu_states[4][2];
23          FILE *f;
24   #endif
25 + #ifdef SOLARIS
26 +        kstat_ctl_t *kc;
27 +        kstat_t *ksp;
28 +        uint_t cpu_states[5][2];
29 +        uint_t user, kernel, idle, iowait, swap, total;
30 +        cpu_stat_t cs;
31 +        
32 + #endif
33 + #ifdef FREEBSD
34 +        long cp_time[CPUSTATES];
35 +        long total, user, idle, kernel, nice;
36 +        size_t size;    
37 + #endif
38  
39   #ifdef LINUX
40          if ((f=fopen("/proc/stat", "r" ))==NULL) {      
# Line 54 | Line 79 | char *get_cpu_stats(){
79          i_p=((double)idle/(double)total)*100.00;
80  
81   #endif
82 + #ifdef SOLARIS
83 +        if ((kc = kstat_open()) == NULL) {
84 +                errf("kstat_open failure (%m)");
85 +                return NULL;
86 +        }
87 +        for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
88 +                if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
89 +                if (kstat_read(kc, ksp, &cs) == -1) {
90 +                        errf("kstat read failure");
91 +                        continue;
92 +                }
93 +        }
94 +        cpu_states[0][0]=cs.cpu_sysinfo.cpu[CPU_USER];
95 +        cpu_states[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT];
96 +        cpu_states[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
97 +        cpu_states[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE];
98 +        cpu_states[4][0]=cs.cpu_sysinfo.cpu[CPU_STATES];
99 +
100 +        sleep(WAIT_TIME_IN_SECS);
101          
102 <        return "jibble";
103 < }
102 >        if (kstat_chain_update(kc) == -1) {
103 >                errf("Kstat update 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][1]=cs.cpu_sysinfo.cpu[CPU_USER];
114 >        cpu_states[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT];
115 >        cpu_states[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
116 >        cpu_states[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE];
117 >        cpu_states[4][1]=cs.cpu_sysinfo.cpu[CPU_STATES];
118  
119 < int main(){
120 <        get_cpu_stats();
121 <        exit(0);        
119 >        if((kstat_close(kc)) != 0){
120 >                errf("Failed to close kstat control structure (%m)");
121 >                return NULL;
122 >        }
123 >
124 >        user=cpu_states[0][1]-cpu_states[0][0];
125 >        iowait=cpu_states[1][1]-cpu_states[1][0];
126 >        kernel=cpu_states[2][1]-cpu_states[2][0];
127 >        idle=cpu_states[3][1]-cpu_states[3][0];
128 >        swap=cpu_states[4][1]-cpu_states[4][0];
129 >        total=user+kernel+idle+iowait+swap;
130 >
131 >        u_p=((double)user/(double)total)*100.00;
132 >        k_p=((double)kernel/(double)total)*100.00;
133 >        i_p=((double)idle/(double)total)*100.00;
134 >        io_p=((double)iowait/(double)total)*100.00;
135 >        s_p=((double)swap/(double)total)*100.00;
136 >
137 > #endif  
138 > #ifdef FREEBSD
139 >
140 > #define CPU_STAT_NAME "kern.cp_time"
141 >
142 >        if (sysctlbyname(CPU_STAT_NAME, NULL, &size, NULL, NULL) < 0){  
143 >                errf("sysctrlbyname failed (%m)");
144 >                return NULL;
145 >        }
146 >        if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){
147 >                errf("Failed to get cpu stats (%m)");
148 >                return NULL;
149 >        }
150 >
151 >        user=cp_time[CP_USER];
152 >        nice=cp_time[CP_NICE];
153 >        kernel=cp_time[CP_SYS];
154 >        idle=cp_time[CP_IDLE];
155 >
156 >        sleep(WAIT_TIME_IN_SECS);
157 >
158 >        if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){
159 >                errf("Failed to get cpu stats (%m)");
160 >                return NULL;
161 >        }
162 >
163 >        user=cp_time[CP_USER]-user;
164 >        nice=cp_time[CP_NICE]-nice;
165 >        kernel=cp_time[CP_SYS]-kernel;
166 >        idle=cp_time[CP_IDLE]-idle;
167 >        total=user+nice+kernel+idle;
168 >
169 >        u_p=((float)(user+nice)/(float)total)*100.0;
170 >        k_p=((float)(kernel)/(float)total)*100.0;
171 >        i_p=((float)(idle)/(float)total)*100.0;
172 >        
173 > #endif
174 >
175 >
176 >        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){
177 >                errf("strf failed (%m)");
178 >                return NULL;
179 >        }
180 >
181 >        return xml_cpu_stats;
182   }
183 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines