26 |
|
|
27 |
|
#include <time.h> |
28 |
|
#include "statgrab.h" |
29 |
+ |
#include "tools.h" |
30 |
|
#ifdef SOLARIS |
31 |
|
#include <kstat.h> |
32 |
|
#include <sys/sysinfo.h> |
57 |
|
sg_cpu_stats *sg_get_cpu_stats(){ |
58 |
|
|
59 |
|
#ifdef SOLARIS |
60 |
< |
kstat_ctl_t *kc; |
61 |
< |
kstat_t *ksp; |
60 |
> |
kstat_ctl_t *kc; |
61 |
> |
kstat_t *ksp; |
62 |
|
cpu_stat_t cs; |
63 |
|
#endif |
64 |
|
#if defined(LINUX) || defined(CYGWIN) |
75 |
|
#endif |
76 |
|
size_t size; |
77 |
|
#endif |
78 |
< |
|
78 |
> |
|
79 |
|
cpu_now.user=0; |
80 |
|
/* Not stored in linux or freebsd */ |
81 |
|
cpu_now.iowait=0; |
88 |
|
cpu_now.nice=0; |
89 |
|
|
90 |
|
#ifdef SOLARIS |
91 |
< |
if ((kc = kstat_open()) == NULL) { |
92 |
< |
return NULL; |
93 |
< |
} |
94 |
< |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
95 |
< |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
96 |
< |
if (kstat_read(kc, ksp, &cs) == -1) { |
97 |
< |
continue; |
98 |
< |
} |
91 |
> |
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 |
|
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]; |
106 |
|
|
107 |
|
cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap; |
108 |
|
|
109 |
< |
kstat_close(kc); |
109 |
> |
kstat_close(kc); |
110 |
|
#endif |
111 |
|
#if defined(LINUX) || defined(CYGWIN) |
112 |
|
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
113 |
+ |
sg_set_error(SG_ERROR_OPEN, "/proc/stat"); |
114 |
|
return NULL; |
115 |
|
} |
116 |
|
/* The very first line should be cpu */ |
119 |
|
&cpu_now.nice, \ |
120 |
|
&cpu_now.kernel, \ |
121 |
|
&cpu_now.idle)) != 4){ |
122 |
+ |
sg_set_error(SG_ERROR_PARSE, "cpu"); |
123 |
|
fclose(f); |
124 |
|
return NULL; |
125 |
|
} |
132 |
|
#if defined(FREEBSD) || defined(DFBSD) |
133 |
|
size = sizeof cp_time; |
134 |
|
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){ |
135 |
+ |
sg_set_error(SG_ERROR_SYSCTLBYNAME, "kern.cp_time"); |
136 |
|
return NULL; |
137 |
|
} |
138 |
|
#else |
144 |
|
#endif |
145 |
|
size = sizeof cp_time; |
146 |
|
if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) { |
147 |
+ |
#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 |
|
return NULL; |
153 |
|
} |
154 |
|
#endif |
175 |
|
|
176 |
|
if (cpu_now_uninit){ |
177 |
|
if((cpu_tmp=sg_get_cpu_stats())==NULL){ |
178 |
< |
/* Should sg_get_cpu_stats fail */ |
178 |
> |
/* Should sg_get_cpu_stats fail */ |
179 |
|
return NULL; |
180 |
|
} |
181 |
|
return cpu_tmp; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
< |
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; |
185 |
> |
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 |
|
|
194 |
|
if((cpu_tmp=sg_get_cpu_stats())==NULL){ |
195 |
|
return NULL; |