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.26 by tdb, Wed Apr 7 14:53:40 2004 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"
66 + #endif
67  
68   sg_mem_stats *sg_get_mem_stats(){
69  
70          static sg_mem_stats mem_stat;
71  
72 + #ifdef HPUX
73 +        struct pst_static *pstat_static;
74 +        struct pst_dynamic pstat_dynamic;
75 +        long long pagesize;
76 + #endif
77   #ifdef SOLARIS
78          kstat_ctl_t *kc;
79          kstat_t *ksp;
# Line 66 | 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
114  
115 + #ifdef HPUX
116 +        if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
117 +                sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
118 +                return NULL;
119 +        }
120 +
121 +        if (pstat_getdynamic(&pstat_dynamic, sizeof(pstat_dynamic), 1, 0) == -1) {
122 +                sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic");
123 +                return NULL;
124 +        }
125 +        pstat_static = sg_get_pstat_static();
126 +        if (pstat_static == NULL) {
127 +                return NULL;
128 +        }
129 +
130 +        /* FIXME Does this include swap? */
131 +        mem_stat.total = ((long long) pstat_static->physical_memory) * pagesize;
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");
155                  return NULL;    
156          }
157  
158          if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){
159 +                sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PHYS_PAGES");
160                  return NULL;
161          }
162  
163          if ((kc = kstat_open()) == NULL) {
164 +                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
165                  return NULL;
166          }
167          if((ksp=kstat_lookup(kc, "unix", 0, "system_pages")) == NULL){
168 +                sg_set_error(SG_ERROR_KSTAT_LOOKUP, "unix,0,system_pages");
169                  return NULL;
170          }
171          if (kstat_read(kc, ksp, 0) == -1) {
172 +                sg_set_error(SG_ERROR_KSTAT_READ, NULL);
173                  return NULL;
174          }
175          if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
176 +                sg_set_error(SG_ERROR_KSTAT_DATA_LOOKUP, "freemem");
177                  return NULL;
178          }
179          kstat_close(kc);
# Line 105 | Line 185 | sg_mem_stats *sg_get_mem_stats(){
185  
186   #if defined(LINUX) || defined(CYGWIN)
187          if ((f = fopen("/proc/meminfo", "r")) == NULL) {
188 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo");
189                  return NULL;
190          }
191  
# Line 133 | Line 214 | sg_mem_stats *sg_get_mem_stats(){
214          mib[1] = HW_PHYSMEM;
215          size = sizeof physmem;
216          if (sysctl(mib, 2, &physmem, &size, NULL, 0) < 0) {
217 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_PHYSMEM");
218                  return NULL;
219          }
220          mem_stat.total = physmem;
221  
222          /*returns pages*/
223          size = sizeof free_count;
224 <        if (sysctlbyname("vm.stats.vm.v_free_count", &free_count, &size, NULL, 0) < 0){
224 >        if (sysctlbyname("vm.stats.vm.v_free_count", &free_count, &size, NULL, 0) < 0){
225 >                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
226 >                                        "vm.stats.vm.v_free_count");
227                  return NULL;
228 <        }
228 >        }
229  
230          size = sizeof inactive_count;
231 <        if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count , &size, NULL, 0) < 0){
231 >        if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count , &size, NULL, 0) < 0){
232 >                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
233 >                                        "vm.stats.vm.v_inactive_count");
234                  return NULL;
235 <        }
235 >        }
236  
237          size = sizeof cache_count;
238 <        if (sysctlbyname("vm.stats.vm.v_cache_count", &cache_count, &size, NULL, 0) < 0){
238 >        if (sysctlbyname("vm.stats.vm.v_cache_count", &cache_count, &size, NULL, 0) < 0){
239 >                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
240 >                                        "vm.stats.vm.v_cache_count");
241                  return NULL;
242 <        }
242 >        }
243  
244          /* Because all the vm.stats returns pages, I need to get the page size.
245 <         * After that I then need to multiple the anything that used vm.stats to
245 >         * After that I then need to multiple the anything that used vm.stats to
246           * get the system statistics by pagesize
247           */
248 <        if ((pagesize=getpagesize()) == -1){
161 <                return NULL;
162 <        }
163 <
248 >        pagesize = getpagesize();
249          mem_stat.cache=cache_count*pagesize;
250  
251          /* Of couse nothing is ever that simple :) And I have inactive pages to
# Line 170 | 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;
179 #ifdef NETBSD
264          mem_stat.cache = uvm->pagesize * (uvm->filepages + uvm->execpages);
181 #else
182        /* Can't find cache memory on OpenBSD */
183        mem_stat.cache = 0;
184 #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
318 +        memstats.dwLength = sizeof(memstats);
319 +        if (!GlobalMemoryStatusEx(&memstats)) {
320 +                sg_set_error_with_errno(SG_ERROR_MEMSTATUS, NULL);
321 +                return NULL;
322 +        }
323 +        mem_stat.free = memstats.ullAvailPhys;
324 +        mem_stat.total = memstats.ullTotalPhys;
325 +        mem_stat.used = mem_stat.total - mem_stat.free;
326 +        if(read_counter_large(SG_WIN32_MEM_CACHE, &mem_stat.cache)) {
327 +                mem_stat.cache = 0;
328 +        }
329 + #endif
330          return &mem_stat;
331   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines