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.37 by tdb, Sun Oct 3 18:35:57 2010 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>
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"
# Line 80 | Line 90 | sg_mem_stats *sg_get_mem_stats(){
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) || defined(OPENBSD)
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
# Line 112 | Line 132 | sg_mem_stats *sg_get_mem_stats(){
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");
# Line 218 | Line 255 | sg_mem_stats *sg_get_mem_stats(){
255          mem_stat.used=physmem-mem_stat.free;
256   #endif
257  
258 < #if defined(NETBSD) || defined(OPENBSD)
258 > #if defined(NETBSD)
259          if ((uvm = sg_get_uvmexp()) == NULL) {
260                  return NULL;
261          }
262  
263          mem_stat.total = uvm->pagesize * uvm->npages;
227 #ifdef NETBSD
264          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
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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines