98 |
|
int mib[2]; |
99 |
|
struct vmtotal vmtotal; |
100 |
|
size_t size; |
101 |
< |
static int pagesize, pageshift; |
101 |
> |
int pagesize, page_multiplier; |
102 |
|
#endif |
103 |
|
#ifdef WIN32 |
104 |
|
MEMORYSTATUSEX memstats; |
242 |
|
#endif |
243 |
|
|
244 |
|
#if defined(OPENBSD) |
245 |
< |
/* get the page size with "getpagesize" and calculate pageshift |
246 |
< |
* from it |
245 |
> |
/* The code in this section is based on the code in the OpenBSD |
246 |
> |
* top utility, located at src/usr.bin/top/machine.c in the |
247 |
> |
* OpenBSD source tree. |
248 |
> |
* |
249 |
> |
* For fun, and like OpenBSD top, we will do the multiplication |
250 |
> |
* converting the memory stats in pages to bytes in base 2. |
251 |
|
*/ |
252 |
< |
pagesize = getpagesize(); |
253 |
< |
pageshift = 0; |
252 |
> |
|
253 |
> |
/* All memory stats in OpenBSD are returned as the number of pages. |
254 |
> |
* To convert this into the number of bytes we need to know the |
255 |
> |
* page size on this system. |
256 |
> |
*/ |
257 |
> |
pagesize = sysconf(_SC_PAGESIZE); |
258 |
> |
|
259 |
> |
/* The pagesize gives us the base 10 multiplier, so we need to work |
260 |
> |
* out what the base 2 multiplier is. This means dividing |
261 |
> |
* pagesize by 2 until we reach unity, and counting the number of |
262 |
> |
* divisions required. |
263 |
> |
*/ |
264 |
> |
page_multiplier = 0; |
265 |
> |
|
266 |
|
while (pagesize > 1) { |
267 |
< |
pageshift++; |
267 |
> |
page_multiplier++; |
268 |
|
pagesize >>= 1; |
269 |
|
} |
270 |
|
|
271 |
< |
/* we only need the amount of log(2)1024 for our conversion */ |
272 |
< |
pageshift -= 10; /* Log base 2 of 1024 is 10 (2^10 == 1024) */ |
273 |
< |
#define pagetok(size) ((size) << pageshift) |
271 |
> |
/* We can now ret the the raw VM stats (in pages) using the |
272 |
> |
* sysctl interface. |
273 |
> |
*/ |
274 |
|
mib[0] = CTL_VM; |
275 |
|
mib[1] = VM_METER; |
276 |
|
size = sizeof(vmtotal); |
277 |
+ |
|
278 |
|
if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) { |
279 |
< |
sg_set_error_with_errno(SG_ERROR_SYSCTL, |
280 |
< |
"CTL_VM.VM_METER"); |
279 |
> |
bzero(&vmtotal, sizeof(vmtotal)); |
280 |
> |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_METER"); |
281 |
|
return NULL; |
282 |
|
} |
283 |
< |
/* convert memory stats to Kbytes */ |
284 |
< |
mem_stat.used = pagetok(vmtotal.t_rm); /* total real mem in use */ |
285 |
< |
mem_stat.cache = 0; /* ? */ |
286 |
< |
mem_stat.free = pagetok(vmtotal.t_free); /* free memory pages */ |
283 |
> |
|
284 |
> |
/* Convert the raw stats to bytes, and return these to the caller |
285 |
> |
*/ |
286 |
> |
mem_stat.used = (vmtotal.t_rm << page_multiplier); /* total real mem in use */ |
287 |
> |
mem_stat.cache = 0; /* no cache stats */ |
288 |
> |
mem_stat.free = (vmtotal.t_free << page_multiplier); /* free memory pages */ |
289 |
|
mem_stat.total = (mem_stat.used + mem_stat.free); |
290 |
|
#endif |
291 |
|
|