ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/memory_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/memory_stats.c (file contents):
Revision 1.32 by tdb, Sat Sep 24 13:29:22 2005 UTC vs.
Revision 1.35 by tdb, Mon Jun 18 20:56:22 2007 UTC

# Line 40 | Line 40
40   #include <sys/sysctl.h>
41   #include <unistd.h>
42   #endif
43 < #if defined(NETBSD) || defined(OPENBSD)
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>
# Line 85 | Line 91 | sg_mem_stats *sg_get_mem_stats(){
91          u_int inactive_count;
92          int pagesize;
93   #endif
94 < #if defined(NETBSD) || defined(OPENBSD)
94 > #if defined(NETBSD)
95          struct uvmexp *uvm;
96   #endif
97 + #if defined(OPENBSD)
98 +        int mib[2];
99 +        struct vmtotal vmtotal;
100 +        size_t size;
101 +        int pagesize, page_multiplier;
102 + #endif
103   #ifdef WIN32
104          MEMORYSTATUSEX memstats;
105   #endif
# Line 218 | Line 230 | sg_mem_stats *sg_get_mem_stats(){
230          mem_stat.used=physmem-mem_stat.free;
231   #endif
232  
233 < #if defined(NETBSD) || defined(OPENBSD)
233 > #if defined(NETBSD)
234          if ((uvm = sg_get_uvmexp()) == NULL) {
235                  return NULL;
236          }
237  
238          mem_stat.total = uvm->pagesize * uvm->npages;
227 #ifdef NETBSD
239          mem_stat.cache = uvm->pagesize * (uvm->filepages + uvm->execpages);
229 #else
230        /* Can't find cache memory on OpenBSD */
231        mem_stat.cache = 0;
232 #endif
240          mem_stat.free = uvm->pagesize * (uvm->free + uvm->inactive);
241          mem_stat.used = mem_stat.total - mem_stat.free;
242 + #endif
243 +
244 + #if defined(OPENBSD)
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 +
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 +                page_multiplier++;
268 +                pagesize >>= 1;
269 +        }
270 +
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 +                bzero(&vmtotal, sizeof(vmtotal));
280 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_METER");
281 +                return NULL;
282 +        }
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  
292   #ifdef WIN32

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines