1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org.uk |
4 |
* 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 |
#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 |
#include <string.h> |
31 |
#endif |
32 |
#ifdef LINUX |
33 |
#include <stdio.h> |
34 |
#endif |
35 |
#ifdef FREEBSD |
36 |
#include <sys/sysctl.h> |
37 |
#include <sys/dkstat.h> |
38 |
#endif |
39 |
|
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 |
cpu_stat_t cs; |
49 |
#endif |
50 |
#ifdef LINUX |
51 |
FILE *f; |
52 |
#endif |
53 |
#ifdef FREEBSD |
54 |
long cp_time[CPUSTATES]; |
55 |
size_t size; |
56 |
#endif |
57 |
|
58 |
cpu_now.user=0; |
59 |
/* Not stored in linux or freebsd */ |
60 |
cpu_now.iowait=0; |
61 |
cpu_now.kernel=0; |
62 |
cpu_now.idle=0; |
63 |
/* Not stored in linux or freebsd */ |
64 |
cpu_now.swap=0; |
65 |
cpu_now.total=0; |
66 |
/* Not stored in solaris */ |
67 |
cpu_now.nice=0; |
68 |
|
69 |
#ifdef SOLARIS |
70 |
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 |
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 |
} |
84 |
|
85 |
cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap; |
86 |
|
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 |
fclose(f); |
100 |
return NULL; |
101 |
} |
102 |
|
103 |
fclose(f); |
104 |
|
105 |
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
106 |
#endif |
107 |
#ifdef FREEBSD |
108 |
if (sysctlbyname("kern.cp_time", NULL, &size, NULL, NULL) < 0){ |
109 |
return NULL; |
110 |
} |
111 |
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, NULL) < 0){ |
112 |
return NULL; |
113 |
} |
114 |
|
115 |
cpu_now.user=cp_time[CP_USER]; |
116 |
cpu_now.nice=cp_time[CP_NICE]; |
117 |
cpu_now.kernel=cp_time[CP_SYS]; |
118 |
cpu_now.idle=cp_time[CP_IDLE]; |
119 |
|
120 |
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
121 |
|
122 |
#endif |
123 |
|
124 |
cpu_now.systime=time(NULL); |
125 |
cpu_now_uninit=0; |
126 |
|
127 |
|
128 |
return &cpu_now; |
129 |
} |
130 |
|
131 |
cpu_states_t *get_cpu_diff(){ |
132 |
static cpu_states_t cpu_diff; |
133 |
cpu_states_t cpu_then, *cpu_tmp; |
134 |
|
135 |
if (cpu_now_uninit){ |
136 |
if((cpu_tmp=get_cpu_totals())==NULL){ |
137 |
/* Should get_cpu_totals fail */ |
138 |
return NULL; |
139 |
} |
140 |
return cpu_tmp; |
141 |
} |
142 |
|
143 |
|
144 |
cpu_then.user=cpu_now.user; |
145 |
cpu_then.kernel=cpu_now.kernel; |
146 |
cpu_then.idle=cpu_now.idle; |
147 |
cpu_then.iowait=cpu_now.iowait; |
148 |
cpu_then.swap=cpu_now.swap; |
149 |
cpu_then.nice=cpu_now.nice; |
150 |
cpu_then.total=cpu_now.total; |
151 |
cpu_then.systime=cpu_now.systime; |
152 |
|
153 |
if((cpu_tmp=get_cpu_totals())==NULL){ |
154 |
return NULL; |
155 |
} |
156 |
|
157 |
cpu_diff.user = cpu_now.user - cpu_then.user; |
158 |
cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel; |
159 |
cpu_diff.idle = cpu_now.idle - cpu_then.idle; |
160 |
cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait; |
161 |
cpu_diff.swap = cpu_now.swap - cpu_then.swap; |
162 |
cpu_diff.nice = cpu_now.nice - cpu_then.nice; |
163 |
cpu_diff.total = cpu_now.total - cpu_then.total; |
164 |
cpu_diff.systime = cpu_now.systime - cpu_then.systime; |
165 |
|
166 |
return &cpu_diff; |
167 |
} |
168 |
|
169 |
cpu_percent_t *cpu_percent_usage(){ |
170 |
static cpu_percent_t cpu_usage; |
171 |
cpu_states_t *cs_ptr; |
172 |
|
173 |
cs_ptr=get_cpu_diff(); |
174 |
if(cs_ptr==NULL){ |
175 |
return NULL; |
176 |
} |
177 |
|
178 |
cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100; |
179 |
cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100; |
180 |
cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100; |
181 |
cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100; |
182 |
cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100; |
183 |
cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100; |
184 |
cpu_usage.time_taken = cs_ptr->systime; |
185 |
|
186 |
return &cpu_usage; |
187 |
|
188 |
} |
189 |
|