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.6 by pajs, Thu Apr 3 20:05:10 2003 UTC vs.
Revision 1.13 by ats, Sun Oct 19 02:11:50 2003 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
3 > * http://www.i-scream.org
4 > * Copyright (C) 2000-2003 i-scream
5   *
6   * This program is free software; you can redistribute it and/or
7   * modify it under the terms of the GNU General Public License
# Line 23 | Line 23
23   #endif
24  
25   #include "statgrab.h"
26 + #include "tools.h"
27   #ifdef SOLARIS
28   #include <unistd.h>
29   #include <kstat.h>
# Line 30 | Line 31
31   #ifdef LINUX
32   #include <stdio.h>
33   #include <string.h>
33 #include "tools.h"
34   #endif
35 + #ifdef ALLBSD
36   #ifdef FREEBSD
37   #include <sys/types.h>
38   #include <sys/sysctl.h>
39 + #else
40 + #include <sys/param.h>
41 + #include <sys/sysctl.h>
42 + #endif
43   #include <unistd.h>
44   #endif
45  
# Line 53 | Line 58 | mem_stat_t *get_memory_stats(){
58          char *line_ptr;
59          FILE *f;
60   #endif
61 + #ifdef ALLBSD
62 +        int mib[2];
63   #ifdef FREEBSD
64 <        long inactive;
65 <        size_t size;
64 >        u_int free_count;
65 >        u_int cache_count;
66 >        u_int inactive_count;
67          int pagesize;
68   #endif
69 + #ifdef NETBSD
70 +        struct uvmexp *uvm;
71 + #endif
72 +        u_long physmem;
73 +        size_t size;
74 + #endif
75  
76   #ifdef SOLARIS
77          if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
# Line 114 | Line 128 | mem_stat_t *get_memory_stats(){
128  
129   #endif
130  
131 < #ifdef FREEBSD
132 <        /* Returns byes */
133 <        if (sysctlbyname("hw.physmem", NULL, &size, NULL, NULL) < 0){
131 > #ifdef ALLBSD
132 >        /* Returns bytes */
133 >        mib[0] = CTL_HW;
134 >        mib[1] = HW_PHYSMEM;
135 >        size = sizeof physmem;
136 >        if (sysctl(mib, 2, &physmem, &size, NULL, 0) < 0) {
137                  return NULL;
138 <        }
139 <        if (sysctlbyname("hw.physmem", &mem_stat.total, &size, NULL, NULL) < 0){
123 <                return NULL;
124 <        }
138 >        }
139 >        mem_stat.total = physmem;
140  
141          /*returns pages*/
142 <        if (sysctlbyname("vm.stats.vm.v_free_count", NULL, &size, NULL, NULL) < 0){
142 > #ifdef FREEBSD
143 >        size = sizeof free_count;
144 >        if (sysctlbyname("vm.stats.vm.v_free_count", &free_count, &size, NULL, 0) < 0){
145                  return NULL;
129        }
130        if (sysctlbyname("vm.stats.vm.v_free_count", &mem_stat.free, &size, NULL, NULL) < 0){
131                return NULL;
146          }
147  
148 <        if (sysctlbyname("vm.stats.vm.v_inactive_count", NULL, &size, NULL, NULL) < 0){
148 >        size = sizeof inactive_count;
149 >        if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count , &size, NULL, 0) < 0){
150                  return NULL;
136        }
137        if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive , &size, NULL, NULL) < 0){
138                return NULL;
151          }
152  
153 <        if (sysctlbyname("vm.stats.vm.v_cache_count", NULL, &size, NULL, NULL) < 0){
153 >        size = sizeof cache_count;
154 >        if (sysctlbyname("vm.stats.vm.v_cache_count", &cache_count, &size, NULL, 0) < 0){
155                  return NULL;
143        }
144        if (sysctlbyname("vm.stats.vm.v_cache_count", &mem_stat.cache, &size, NULL, NULL) < 0){
145                return NULL;
156          }
157  
158 <        /* Because all the vm.stats returns pages, i need to get the page size.
159 <         * After that i then need to multiple the anything that used vm.stats to get
160 <         * the system statistics by pagesize
158 >        /* Because all the vm.stats returns pages, I need to get the page size.
159 >         * After that I then need to multiple the anything that used vm.stats to
160 >         * get the system statistics by pagesize
161           */
162          if ((pagesize=getpagesize()) == -1){
163                  return NULL;
164          }
165  
166 <        mem_stat.cache=mem_stat.cache*pagesize;
157 <        /* Of couse nothing is ever that simple :) And i have inactive pages to deal
158 <         * with too. So im goingto add them to free memory :)
159 <         */
160 <        mem_stat.free=(mem_stat.free*pagesize)+(inactive*pagesize);
161 <        
162 <        mem_stat.used=mem_stat.total-mem_stat.free;
166 >        mem_stat.cache=cache_count*pagesize;
167  
168 +        /* Of couse nothing is ever that simple :) And I have inactive pages to
169 +         * deal with too. So I'm going to add them to free memory :)
170 +         */
171 +        mem_stat.free=(free_count*pagesize)+(inactive_count*pagesize);
172 +        mem_stat.used=physmem-mem_stat.free;
173 + #endif
174 + #ifdef NETBSD
175 +        /* FIXME This is not consistent with the FreeBSD logic above. */
176 +        if ((uvm = get_uvmexp()) == NULL) {
177 +                return NULL;
178 +        }
179 +        mem_stat.cache = uvm->pagesize * (uvm->filepages + uvm->execpages);
180 +        mem_stat.free = uvm->pagesize * uvm->free;
181 +        mem_stat.used = uvm->pagesize * (uvm->npages - uvm->free);
182 + #endif
183   #endif
184  
185          return &mem_stat;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines