1 |
< |
/* |
2 |
< |
* i-scream central monitoring system |
1 |
> |
/* |
2 |
> |
* i-scream libstatgrab |
3 |
|
* http://www.i-scream.org |
4 |
< |
* Copyright (C) 2000-2003 i-scream |
4 |
> |
* Copyright (C) 2000-2004 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. |
6 |
> |
* This library is free software; you can redistribute it and/or |
7 |
> |
* modify it under the terms of the GNU Lesser General Public |
8 |
> |
* License as published by the Free Software Foundation; either |
9 |
> |
* version 2.1 of the License, or (at your option) any later version. |
10 |
|
* |
11 |
< |
* This program is distributed in the hope that it will be useful, |
11 |
> |
* This library 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. |
13 |
> |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
> |
* Lesser 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. |
16 |
> |
* You should have received a copy of the GNU Lesser General Public |
17 |
> |
* License along with this library; if not, write to the Free Software |
18 |
> |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
> |
* 02111-1307 USA |
20 |
> |
* |
21 |
> |
* $Id$ |
22 |
|
*/ |
23 |
|
|
24 |
|
#ifdef HAVE_CONFIG_H |
26 |
|
#endif |
27 |
|
|
28 |
|
#include "statgrab.h" |
29 |
+ |
#include "tools.h" |
30 |
|
#ifdef SOLARIS |
31 |
|
#include <unistd.h> |
32 |
|
#include <kstat.h> |
33 |
|
#endif |
34 |
< |
#ifdef LINUX |
34 |
> |
#if defined(LINUX) || defined(CYGWIN) |
35 |
|
#include <stdio.h> |
36 |
|
#include <string.h> |
33 |
– |
#include "tools.h" |
37 |
|
#endif |
38 |
< |
#ifdef FREEBSD |
38 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
39 |
|
#include <sys/types.h> |
40 |
|
#include <sys/sysctl.h> |
41 |
|
#include <unistd.h> |
42 |
|
#endif |
43 |
+ |
#if defined(NETBSD) |
44 |
+ |
#include <sys/param.h> |
45 |
+ |
#include <sys/time.h> |
46 |
+ |
#include <uvm/uvm.h> |
47 |
+ |
#endif |
48 |
+ |
#if defined(OPENBSD) |
49 |
+ |
#include <sys/param.h> |
50 |
+ |
#include <sys/types.h> |
51 |
+ |
#include <sys/sysctl.h> |
52 |
+ |
#include <sys/unistd.h> |
53 |
+ |
#endif |
54 |
+ |
#ifdef HPUX |
55 |
+ |
#include <sys/param.h> |
56 |
+ |
#include <sys/pstat.h> |
57 |
+ |
#include <unistd.h> |
58 |
+ |
#endif |
59 |
+ |
#ifdef AIX |
60 |
+ |
#include <unistd.h> |
61 |
+ |
#include <libperfstat.h> |
62 |
+ |
#endif |
63 |
+ |
#ifdef WIN32 |
64 |
+ |
#include <windows.h> |
65 |
+ |
#include "win32.h" |
66 |
+ |
#endif |
67 |
|
|
68 |
< |
mem_stat_t *get_memory_stats(){ |
68 |
> |
sg_mem_stats *sg_get_mem_stats(){ |
69 |
|
|
70 |
< |
static mem_stat_t mem_stat; |
70 |
> |
static sg_mem_stats mem_stat; |
71 |
|
|
72 |
+ |
#ifdef HPUX |
73 |
+ |
struct pst_static *pstat_static; |
74 |
+ |
struct pst_dynamic pstat_dynamic; |
75 |
+ |
long long pagesize; |
76 |
+ |
#endif |
77 |
|
#ifdef SOLARIS |
78 |
|
kstat_ctl_t *kc; |
79 |
|
kstat_t *ksp; |
81 |
|
long totalmem; |
82 |
|
int pagesize; |
83 |
|
#endif |
84 |
< |
#ifdef LINUX |
84 |
> |
#if defined(LINUX) || defined(CYGWIN) |
85 |
|
char *line_ptr; |
86 |
+ |
unsigned long long value; |
87 |
|
FILE *f; |
88 |
|
#endif |
89 |
< |
#ifdef FREEBSD |
90 |
< |
u_int free_count; |
91 |
< |
u_int cache_count; |
59 |
< |
u_int inactive_count; |
60 |
< |
u_int physmem; |
89 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
90 |
> |
int mib[2]; |
91 |
> |
u_long physmem; |
92 |
|
size_t size; |
93 |
+ |
u_long free_count; |
94 |
+ |
u_long cache_count; |
95 |
+ |
u_long inactive_count; |
96 |
|
int pagesize; |
97 |
|
#endif |
98 |
+ |
#if defined(NETBSD) |
99 |
+ |
struct uvmexp *uvm; |
100 |
+ |
#endif |
101 |
+ |
#if defined(OPENBSD) |
102 |
+ |
int mib[2]; |
103 |
+ |
struct vmtotal vmtotal; |
104 |
+ |
size_t size; |
105 |
+ |
int pagesize, page_multiplier; |
106 |
+ |
#endif |
107 |
+ |
#ifdef AIX |
108 |
+ |
perfstat_memory_total_t mem; |
109 |
+ |
long long pagesize; |
110 |
+ |
#endif |
111 |
+ |
#ifdef WIN32 |
112 |
+ |
MEMORYSTATUSEX memstats; |
113 |
+ |
#endif |
114 |
|
|
115 |
+ |
#ifdef HPUX |
116 |
+ |
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
117 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE"); |
118 |
+ |
return NULL; |
119 |
+ |
} |
120 |
+ |
|
121 |
+ |
if (pstat_getdynamic(&pstat_dynamic, sizeof(pstat_dynamic), 1, 0) == -1) { |
122 |
+ |
sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic"); |
123 |
+ |
return NULL; |
124 |
+ |
} |
125 |
+ |
pstat_static = sg_get_pstat_static(); |
126 |
+ |
if (pstat_static == NULL) { |
127 |
+ |
return NULL; |
128 |
+ |
} |
129 |
+ |
|
130 |
+ |
/* FIXME Does this include swap? */ |
131 |
+ |
mem_stat.total = ((long long) pstat_static->physical_memory) * pagesize; |
132 |
+ |
mem_stat.free = ((long long) pstat_dynamic.psd_free) * pagesize; |
133 |
+ |
mem_stat.used = mem_stat.total - mem_stat.free; |
134 |
+ |
#endif |
135 |
+ |
#ifdef AIX |
136 |
+ |
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
137 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE"); |
138 |
+ |
return NULL; |
139 |
+ |
} |
140 |
+ |
|
141 |
+ |
/* return code is number of structures returned */ |
142 |
+ |
if(perfstat_memory_total(NULL, &mem, sizeof(perfstat_memory_total_t), 1) != 1) { |
143 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "perfstat_memory_total"); |
144 |
+ |
return NULL; |
145 |
+ |
} |
146 |
+ |
|
147 |
+ |
mem_stat.total = ((long long) mem.real_total) * pagesize; |
148 |
+ |
mem_stat.free = ((long long) mem.real_free) * pagesize; |
149 |
+ |
mem_stat.used = ((long long) mem.real_inuse) * pagesize; |
150 |
+ |
mem_stat.cache = ((long long) mem.numperm) * pagesize; |
151 |
+ |
#endif |
152 |
|
#ifdef SOLARIS |
153 |
|
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
154 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE"); |
155 |
|
return NULL; |
156 |
|
} |
157 |
|
|
158 |
|
if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){ |
159 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PHYS_PAGES"); |
160 |
|
return NULL; |
161 |
|
} |
162 |
|
|
163 |
|
if ((kc = kstat_open()) == NULL) { |
164 |
+ |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
165 |
|
return NULL; |
166 |
|
} |
167 |
|
if((ksp=kstat_lookup(kc, "unix", 0, "system_pages")) == NULL){ |
168 |
+ |
sg_set_error(SG_ERROR_KSTAT_LOOKUP, "unix,0,system_pages"); |
169 |
|
return NULL; |
170 |
|
} |
171 |
|
if (kstat_read(kc, ksp, 0) == -1) { |
172 |
+ |
sg_set_error(SG_ERROR_KSTAT_READ, NULL); |
173 |
|
return NULL; |
174 |
|
} |
175 |
|
if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){ |
176 |
+ |
sg_set_error(SG_ERROR_KSTAT_DATA_LOOKUP, "freemem"); |
177 |
|
return NULL; |
178 |
|
} |
179 |
< |
kstat_close(kc); |
179 |
> |
kstat_close(kc); |
180 |
|
|
181 |
|
mem_stat.total = (long long)totalmem * (long long)pagesize; |
182 |
|
mem_stat.free = ((long long)kn->value.ul) * (long long)pagesize; |
183 |
|
mem_stat.used = mem_stat.total - mem_stat.free; |
184 |
|
#endif |
185 |
|
|
186 |
< |
#ifdef LINUX |
187 |
< |
f=fopen("/proc/meminfo", "r"); |
188 |
< |
if(f==NULL){ |
186 |
> |
#if defined(LINUX) || defined(CYGWIN) |
187 |
> |
if ((f = fopen("/proc/meminfo", "r")) == NULL) { |
188 |
> |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo"); |
189 |
|
return NULL; |
190 |
|
} |
191 |
|
|
192 |
< |
if((line_ptr=f_read_line(f, "Mem:"))==NULL){ |
193 |
< |
fclose(f); |
194 |
< |
return NULL; |
195 |
< |
} |
192 |
> |
while ((line_ptr = sg_f_read_line(f, "")) != NULL) { |
193 |
> |
if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) { |
194 |
> |
continue; |
195 |
> |
} |
196 |
> |
value *= 1024; |
197 |
|
|
198 |
< |
fclose(f); |
199 |
< |
|
200 |
< |
/* Linux actually stores this as a unsigned long long, but |
201 |
< |
* our structures are just long longs. This shouldn't be a |
202 |
< |
* problem for sometime yet :) |
203 |
< |
*/ |
204 |
< |
if((sscanf(line_ptr,"Mem: %lld %lld %lld %*d %*d %lld", \ |
111 |
< |
&mem_stat.total, \ |
112 |
< |
&mem_stat.used, \ |
113 |
< |
&mem_stat.free, \ |
114 |
< |
&mem_stat.cache))!=4){ |
115 |
< |
return NULL; |
198 |
> |
if (strncmp(line_ptr, "MemTotal:", 9) == 0) { |
199 |
> |
mem_stat.total = value; |
200 |
> |
} else if (strncmp(line_ptr, "MemFree:", 8) == 0) { |
201 |
> |
mem_stat.free = value; |
202 |
> |
} else if (strncmp(line_ptr, "Cached:", 7) == 0) { |
203 |
> |
mem_stat.cache = value; |
204 |
> |
} |
205 |
|
} |
206 |
|
|
207 |
+ |
fclose(f); |
208 |
+ |
mem_stat.used = mem_stat.total - mem_stat.free; |
209 |
|
#endif |
210 |
|
|
211 |
< |
#ifdef FREEBSD |
211 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
212 |
|
/* Returns bytes */ |
213 |
< |
if (sysctlbyname("hw.physmem", NULL, &size, NULL, NULL) < 0){ |
213 |
> |
mib[0] = CTL_HW; |
214 |
> |
mib[1] = HW_PHYSMEM; |
215 |
> |
size = sizeof physmem; |
216 |
> |
if (sysctl(mib, 2, &physmem, &size, NULL, 0) < 0) { |
217 |
> |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_PHYSMEM"); |
218 |
|
return NULL; |
219 |
< |
} |
220 |
< |
if (sysctlbyname("hw.physmem", &physmem, &size, NULL, NULL) < 0){ |
126 |
< |
return NULL; |
127 |
< |
} |
219 |
> |
} |
220 |
> |
mem_stat.total = physmem; |
221 |
|
|
222 |
|
/*returns pages*/ |
223 |
< |
if (sysctlbyname("vm.stats.vm.v_free_count", NULL, &size, NULL, NULL) < 0){ |
223 |
> |
size = sizeof free_count; |
224 |
> |
if (sysctlbyname("vm.stats.vm.v_free_count", &free_count, &size, NULL, 0) < 0){ |
225 |
> |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, |
226 |
> |
"vm.stats.vm.v_free_count"); |
227 |
|
return NULL; |
228 |
< |
} |
133 |
< |
if (sysctlbyname("vm.stats.vm.v_free_count", &free_count, &size, NULL, NULL) < 0){ |
134 |
< |
return NULL; |
135 |
< |
} |
228 |
> |
} |
229 |
|
|
230 |
< |
if (sysctlbyname("vm.stats.vm.v_inactive_count", NULL, &size, NULL, NULL) < 0){ |
230 |
> |
size = sizeof inactive_count; |
231 |
> |
if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count , &size, NULL, 0) < 0){ |
232 |
> |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, |
233 |
> |
"vm.stats.vm.v_inactive_count"); |
234 |
|
return NULL; |
235 |
< |
} |
140 |
< |
if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count , &size, NULL, NULL) < 0){ |
141 |
< |
return NULL; |
142 |
< |
} |
235 |
> |
} |
236 |
|
|
237 |
< |
if (sysctlbyname("vm.stats.vm.v_cache_count", NULL, &size, NULL, NULL) < 0){ |
237 |
> |
size = sizeof cache_count; |
238 |
> |
if (sysctlbyname("vm.stats.vm.v_cache_count", &cache_count, &size, NULL, 0) < 0){ |
239 |
> |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, |
240 |
> |
"vm.stats.vm.v_cache_count"); |
241 |
|
return NULL; |
242 |
< |
} |
147 |
< |
if (sysctlbyname("vm.stats.vm.v_cache_count", &cache_count, &size, NULL, NULL) < 0){ |
148 |
< |
return NULL; |
149 |
< |
} |
242 |
> |
} |
243 |
|
|
244 |
|
/* Because all the vm.stats returns pages, I need to get the page size. |
245 |
< |
* After that I then need to multiple the anything that used vm.stats to |
245 |
> |
* After that I then need to multiple the anything that used vm.stats to |
246 |
|
* get the system statistics by pagesize |
247 |
|
*/ |
248 |
< |
if ((pagesize=getpagesize()) == -1){ |
156 |
< |
return NULL; |
157 |
< |
} |
158 |
< |
|
159 |
< |
mem_stat.total=physmem; |
248 |
> |
pagesize = getpagesize(); |
249 |
|
mem_stat.cache=cache_count*pagesize; |
250 |
|
|
251 |
|
/* Of couse nothing is ever that simple :) And I have inactive pages to |
255 |
|
mem_stat.used=physmem-mem_stat.free; |
256 |
|
#endif |
257 |
|
|
258 |
< |
return &mem_stat; |
258 |
> |
#if defined(NETBSD) |
259 |
> |
if ((uvm = sg_get_uvmexp()) == NULL) { |
260 |
> |
return NULL; |
261 |
> |
} |
262 |
|
|
263 |
+ |
mem_stat.total = uvm->pagesize * uvm->npages; |
264 |
+ |
mem_stat.cache = uvm->pagesize * (uvm->filepages + uvm->execpages); |
265 |
+ |
mem_stat.free = uvm->pagesize * (uvm->free + uvm->inactive); |
266 |
+ |
mem_stat.used = mem_stat.total - mem_stat.free; |
267 |
+ |
#endif |
268 |
+ |
|
269 |
+ |
#if defined(OPENBSD) |
270 |
+ |
/* The code in this section is based on the code in the OpenBSD |
271 |
+ |
* top utility, located at src/usr.bin/top/machine.c in the |
272 |
+ |
* OpenBSD source tree. |
273 |
+ |
* |
274 |
+ |
* For fun, and like OpenBSD top, we will do the multiplication |
275 |
+ |
* converting the memory stats in pages to bytes in base 2. |
276 |
+ |
*/ |
277 |
+ |
|
278 |
+ |
/* All memory stats in OpenBSD are returned as the number of pages. |
279 |
+ |
* To convert this into the number of bytes we need to know the |
280 |
+ |
* page size on this system. |
281 |
+ |
*/ |
282 |
+ |
pagesize = sysconf(_SC_PAGESIZE); |
283 |
+ |
|
284 |
+ |
/* The pagesize gives us the base 10 multiplier, so we need to work |
285 |
+ |
* out what the base 2 multiplier is. This means dividing |
286 |
+ |
* pagesize by 2 until we reach unity, and counting the number of |
287 |
+ |
* divisions required. |
288 |
+ |
*/ |
289 |
+ |
page_multiplier = 0; |
290 |
+ |
|
291 |
+ |
while (pagesize > 1) { |
292 |
+ |
page_multiplier++; |
293 |
+ |
pagesize >>= 1; |
294 |
+ |
} |
295 |
+ |
|
296 |
+ |
/* We can now ret the the raw VM stats (in pages) using the |
297 |
+ |
* sysctl interface. |
298 |
+ |
*/ |
299 |
+ |
mib[0] = CTL_VM; |
300 |
+ |
mib[1] = VM_METER; |
301 |
+ |
size = sizeof(vmtotal); |
302 |
+ |
|
303 |
+ |
if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) { |
304 |
+ |
bzero(&vmtotal, sizeof(vmtotal)); |
305 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_METER"); |
306 |
+ |
return NULL; |
307 |
+ |
} |
308 |
+ |
|
309 |
+ |
/* Convert the raw stats to bytes, and return these to the caller |
310 |
+ |
*/ |
311 |
+ |
mem_stat.used = (vmtotal.t_rm << page_multiplier); /* total real mem in use */ |
312 |
+ |
mem_stat.cache = 0; /* no cache stats */ |
313 |
+ |
mem_stat.free = (vmtotal.t_free << page_multiplier); /* free memory pages */ |
314 |
+ |
mem_stat.total = (mem_stat.used + mem_stat.free); |
315 |
+ |
#endif |
316 |
+ |
|
317 |
+ |
#ifdef WIN32 |
318 |
+ |
memstats.dwLength = sizeof(memstats); |
319 |
+ |
if (!GlobalMemoryStatusEx(&memstats)) { |
320 |
+ |
sg_set_error_with_errno(SG_ERROR_MEMSTATUS, NULL); |
321 |
+ |
return NULL; |
322 |
+ |
} |
323 |
+ |
mem_stat.free = memstats.ullAvailPhys; |
324 |
+ |
mem_stat.total = memstats.ullTotalPhys; |
325 |
+ |
mem_stat.used = mem_stat.total - mem_stat.free; |
326 |
+ |
if(read_counter_large(SG_WIN32_MEM_CACHE, &mem_stat.cache)) { |
327 |
+ |
mem_stat.cache = 0; |
328 |
+ |
} |
329 |
+ |
#endif |
330 |
+ |
return &mem_stat; |
331 |
|
} |