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" |
90 |
|
int mib[2]; |
91 |
|
u_long physmem; |
92 |
|
size_t size; |
93 |
< |
u_int free_count; |
94 |
< |
u_int cache_count; |
95 |
< |
u_int inactive_count; |
93 |
> |
u_long free_count; |
94 |
> |
u_long cache_count; |
95 |
> |
u_long inactive_count; |
96 |
|
int pagesize; |
97 |
|
#endif |
98 |
|
#if defined(NETBSD) |
102 |
|
int mib[2]; |
103 |
|
struct vmtotal vmtotal; |
104 |
|
size_t size; |
105 |
< |
static int pagesize, pageshift; |
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 |
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"); |
267 |
|
#endif |
268 |
|
|
269 |
|
#if defined(OPENBSD) |
270 |
< |
/* get the page size with "getpagesize" and calculate pageshift |
271 |
< |
* from it |
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 |
< |
pagesize = getpagesize(); |
278 |
< |
pageshift = 0; |
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 |
< |
pageshift++; |
292 |
> |
page_multiplier++; |
293 |
|
pagesize >>= 1; |
294 |
|
} |
295 |
|
|
296 |
< |
/* we only need the amount of log(2)1024 for our conversion */ |
297 |
< |
pageshift -= 10; /* Log base 2 of 1024 is 10 (2^10 == 1024) */ |
298 |
< |
#define pagetok(size) ((size) << pageshift) |
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 |
< |
sg_set_error_with_errno(SG_ERROR_SYSCTL, |
305 |
< |
"CTL_VM.VM_METER"); |
304 |
> |
bzero(&vmtotal, sizeof(vmtotal)); |
305 |
> |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_METER"); |
306 |
|
return NULL; |
307 |
|
} |
308 |
< |
/* convert memory stats to Kbytes */ |
309 |
< |
mem_stat.used = pagetok(vmtotal.t_rm); /* total real mem in use */ |
310 |
< |
mem_stat.cache = 0; /* ? */ |
311 |
< |
mem_stat.free = pagetok(vmtotal.t_free); /* free memory pages */ |
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 |
|
|