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.30 by tdb, Mon Nov 1 18:30:17 2004 UTC vs.
Revision 1.34 by tdb, Mon Oct 30 08:20:05 2006 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 WIN32
60 + #include <windows.h>
61 + #include "win32.h"
62 + #endif
63  
64   sg_mem_stats *sg_get_mem_stats(){
65  
66          static sg_mem_stats mem_stat;
67  
68   #ifdef HPUX
69 <        struct pst_static pstat_static;
69 >        struct pst_static *pstat_static;
70          struct pst_dynamic pstat_dynamic;
71          long long pagesize;
72   #endif
# Line 81 | 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 +        static int pagesize, pageshift;
102 + #endif
103 + #ifdef WIN32
104 +        MEMORYSTATUSEX memstats;
105 + #endif
106  
107   #ifdef HPUX
108          if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
# Line 95 | Line 114 | sg_mem_stats *sg_get_mem_stats(){
114                  sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic");
115                  return NULL;
116          }
117 <        if (pstat_getstatic(&pstat_static, sizeof(pstat_static), 1, 0) == -1) {
118 <                sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_static");
117 >        pstat_static = sg_get_pstat_static();
118 >        if (pstat_static == NULL) {
119                  return NULL;
120          }
121  
122 <        mem_stat.total = ((long long) pstat_static.physical_memory) * pagesize;
122 >        /* FIXME Does this include swap? */
123 >        mem_stat.total = ((long long) pstat_static->physical_memory) * pagesize;
124          mem_stat.free = ((long long) pstat_dynamic.psd_free) * pagesize;
125          mem_stat.used = mem_stat.total - mem_stat.free;
126   #endif
# Line 210 | 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;
219 #ifdef NETBSD
239          mem_stat.cache = uvm->pagesize * (uvm->filepages + uvm->execpages);
221 #else
222        /* Can't find cache memory on OpenBSD */
223        mem_stat.cache = 0;
224 #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 +        /* get the page size with "getpagesize" and calculate pageshift
246 +         * from it
247 +         */
248 +        pagesize = getpagesize();
249 +        pageshift = 0;
250 +        while (pagesize > 1) {
251 +                pageshift++;
252 +                pagesize >>= 1;
253 +        }
254 +
255 +        /* we only need the amount of log(2)1024 for our conversion */
256 +        pageshift -= 10;        /* Log base 2 of 1024 is 10 (2^10 == 1024) */
257 + #define pagetok(size) ((size) << pageshift)
258 +        mib[0] = CTL_VM;
259 +        mib[1] = VM_METER;
260 +        size = sizeof(vmtotal);
261 +        if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
262 +                sg_set_error_with_errno(SG_ERROR_SYSCTL,
263 +                                        "CTL_VM.VM_METER");
264 +                return NULL;
265 +        }
266 +        /* convert memory stats to Kbytes */
267 +        mem_stat.used = pagetok(vmtotal.t_rm);          /* total real mem in use */
268 +        mem_stat.cache = 0;                             /* ? */
269 +        mem_stat.free = pagetok(vmtotal.t_free);        /* free memory pages */
270 +        mem_stat.total = (mem_stat.used + mem_stat.free);
271 + #endif
272 +
273 + #ifdef WIN32
274 +        memstats.dwLength = sizeof(memstats);
275 +        if (!GlobalMemoryStatusEx(&memstats)) {
276 +                sg_set_error_with_errno(SG_ERROR_MEMSTATUS, NULL);
277 +                return NULL;
278 +        }
279 +        mem_stat.free = memstats.ullAvailPhys;
280 +        mem_stat.total = memstats.ullTotalPhys;
281 +        mem_stat.used = mem_stat.total - mem_stat.free;
282 +        if(read_counter_large(SG_WIN32_MEM_CACHE, &mem_stat.cache)) {
283 +                mem_stat.cache = 0;
284 +        }
285 + #endif
286          return &mem_stat;
287   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines