ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/memory_stats.c
Revision: 1.37
Committed: Sun Oct 3 18:35:57 2010 UTC (13 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.36: +26 -1 lines
Log Message:
Add support for AIX 5.x - 9.x.

Many thanks to Jens Rehsack <rehsack@googlemail.com> for providing the
patch for this work. Thanks!

File Contents

# User Rev Content
1 tdb 1.18 /*
2 tdb 1.25 * i-scream libstatgrab
3 tdb 1.7 * http://www.i-scream.org
4 tdb 1.18 * Copyright (C) 2000-2004 i-scream
5 pajs 1.1 *
6 tdb 1.18 * This library is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public
8     * License as published by the Free Software Foundation; either
9     * version 2.1 of the License, or (at your option) any later version.
10 pajs 1.1 *
11 tdb 1.18 * This library is distributed in the hope that it will be useful,
12 pajs 1.1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 tdb 1.18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15 pajs 1.1 *
16 tdb 1.18 * You should have received a copy of the GNU Lesser General Public
17     * License along with this library; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19     * 02111-1307 USA
20 tdb 1.19 *
21 tdb 1.37 * $Id: memory_stats.c,v 1.36 2010/02/21 10:04:26 tdb Exp $
22 pajs 1.1 */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27    
28     #include "statgrab.h"
29 ats 1.12 #include "tools.h"
30 pajs 1.1 #ifdef SOLARIS
31     #include <unistd.h>
32     #include <kstat.h>
33     #endif
34 ats 1.17 #if defined(LINUX) || defined(CYGWIN)
35 pajs 1.5 #include <stdio.h>
36     #include <string.h>
37     #endif
38 tdb 1.23 #if defined(FREEBSD) || defined(DFBSD)
39 pajs 1.6 #include <sys/types.h>
40     #include <sys/sysctl.h>
41     #include <unistd.h>
42     #endif
43 tdb 1.33 #if defined(NETBSD)
44 tdb 1.20 #include <sys/param.h>
45 ats 1.22 #include <sys/time.h>
46 tdb 1.20 #include <uvm/uvm.h>
47     #endif
48 tdb 1.33 #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 tdb 1.30 #ifdef HPUX
55     #include <sys/param.h>
56     #include <sys/pstat.h>
57     #include <unistd.h>
58     #endif
59 tdb 1.37 #ifdef AIX
60     #include <unistd.h>
61     #include <libperfstat.h>
62     #endif
63 tdb 1.32 #ifdef WIN32
64     #include <windows.h>
65     #include "win32.h"
66     #endif
67 pajs 1.1
68 ats 1.24 sg_mem_stats *sg_get_mem_stats(){
69 pajs 1.1
70 ats 1.24 static sg_mem_stats mem_stat;
71 pajs 1.1
72 tdb 1.30 #ifdef HPUX
73 ats 1.31 struct pst_static *pstat_static;
74 tdb 1.30 struct pst_dynamic pstat_dynamic;
75     long long pagesize;
76     #endif
77 pajs 1.1 #ifdef SOLARIS
78     kstat_ctl_t *kc;
79     kstat_t *ksp;
80     kstat_named_t *kn;
81     long totalmem;
82     int pagesize;
83     #endif
84 ats 1.17 #if defined(LINUX) || defined(CYGWIN)
85 pajs 1.5 char *line_ptr;
86 ats 1.15 unsigned long long value;
87 pajs 1.5 FILE *f;
88     #endif
89 tdb 1.23 #if defined(FREEBSD) || defined(DFBSD)
90 ats 1.12 int mib[2];
91 ats 1.14 u_long physmem;
92     size_t size;
93 tdb 1.36 u_long free_count;
94     u_long cache_count;
95     u_long inactive_count;
96 ats 1.12 int pagesize;
97     #endif
98 tdb 1.33 #if defined(NETBSD)
99 ats 1.12 struct uvmexp *uvm;
100     #endif
101 tdb 1.33 #if defined(OPENBSD)
102     int mib[2];
103     struct vmtotal vmtotal;
104     size_t size;
105 tdb 1.35 int pagesize, page_multiplier;
106 tdb 1.33 #endif
107 tdb 1.37 #ifdef AIX
108     perfstat_memory_total_t mem;
109     long long pagesize;
110     #endif
111 tdb 1.32 #ifdef WIN32
112     MEMORYSTATUSEX memstats;
113     #endif
114 pajs 1.1
115 tdb 1.30 #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 ats 1.31 pstat_static = sg_get_pstat_static();
126     if (pstat_static == NULL) {
127 tdb 1.30 return NULL;
128     }
129    
130 ats 1.31 /* FIXME Does this include swap? */
131     mem_stat.total = ((long long) pstat_static->physical_memory) * pagesize;
132 tdb 1.30 mem_stat.free = ((long long) pstat_dynamic.psd_free) * pagesize;
133     mem_stat.used = mem_stat.total - mem_stat.free;
134     #endif
135 tdb 1.37 #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 pajs 1.1 #ifdef SOLARIS
153     if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
154 ats 1.28 sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
155 pajs 1.1 return NULL;
156     }
157    
158     if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){
159 ats 1.28 sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PHYS_PAGES");
160 pajs 1.1 return NULL;
161     }
162    
163     if ((kc = kstat_open()) == NULL) {
164 tdb 1.27 sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
165 pajs 1.1 return NULL;
166     }
167     if((ksp=kstat_lookup(kc, "unix", 0, "system_pages")) == NULL){
168 tdb 1.27 sg_set_error(SG_ERROR_KSTAT_LOOKUP, "unix,0,system_pages");
169 pajs 1.1 return NULL;
170     }
171     if (kstat_read(kc, ksp, 0) == -1) {
172 tdb 1.27 sg_set_error(SG_ERROR_KSTAT_READ, NULL);
173 pajs 1.1 return NULL;
174     }
175     if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
176 tdb 1.27 sg_set_error(SG_ERROR_KSTAT_DATA_LOOKUP, "freemem");
177 pajs 1.1 return NULL;
178     }
179 tdb 1.26 kstat_close(kc);
180 pajs 1.2
181 pajs 1.1 mem_stat.total = (long long)totalmem * (long long)pagesize;
182     mem_stat.free = ((long long)kn->value.ul) * (long long)pagesize;
183     mem_stat.used = mem_stat.total - mem_stat.free;
184 pajs 1.5 #endif
185    
186 ats 1.17 #if defined(LINUX) || defined(CYGWIN)
187 ats 1.16 if ((f = fopen("/proc/meminfo", "r")) == NULL) {
188 ats 1.28 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo");
189 pajs 1.5 return NULL;
190     }
191    
192 ats 1.24 while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
193 ats 1.16 if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) {
194 ats 1.15 continue;
195     }
196     value *= 1024;
197    
198     if (strncmp(line_ptr, "MemTotal:", 9) == 0) {
199     mem_stat.total = value;
200     } else if (strncmp(line_ptr, "MemFree:", 8) == 0) {
201     mem_stat.free = value;
202     } else if (strncmp(line_ptr, "Cached:", 7) == 0) {
203     mem_stat.cache = value;
204     }
205 pajs 1.5 }
206    
207     fclose(f);
208 ats 1.15 mem_stat.used = mem_stat.total - mem_stat.free;
209 pajs 1.6 #endif
210    
211 tdb 1.23 #if defined(FREEBSD) || defined(DFBSD)
212 tdb 1.8 /* Returns bytes */
213 ats 1.12 mib[0] = CTL_HW;
214     mib[1] = HW_PHYSMEM;
215 ats 1.11 size = sizeof physmem;
216 ats 1.12 if (sysctl(mib, 2, &physmem, &size, NULL, 0) < 0) {
217 ats 1.28 sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_PHYSMEM");
218 pajs 1.6 return NULL;
219 ats 1.12 }
220     mem_stat.total = physmem;
221 pajs 1.6
222     /*returns pages*/
223 ats 1.11 size = sizeof free_count;
224 tdb 1.27 if (sysctlbyname("vm.stats.vm.v_free_count", &free_count, &size, NULL, 0) < 0){
225 ats 1.28 sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
226     "vm.stats.vm.v_free_count");
227 pajs 1.6 return NULL;
228 tdb 1.27 }
229 pajs 1.6
230 ats 1.11 size = sizeof inactive_count;
231 tdb 1.27 if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count , &size, NULL, 0) < 0){
232 ats 1.28 sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
233     "vm.stats.vm.v_inactive_count");
234 pajs 1.6 return NULL;
235 tdb 1.27 }
236 pajs 1.6
237 ats 1.11 size = sizeof cache_count;
238 tdb 1.27 if (sysctlbyname("vm.stats.vm.v_cache_count", &cache_count, &size, NULL, 0) < 0){
239 ats 1.28 sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
240     "vm.stats.vm.v_cache_count");
241 pajs 1.6 return NULL;
242 tdb 1.27 }
243 pajs 1.6
244 tdb 1.8 /* Because all the vm.stats returns pages, I need to get the page size.
245 tdb 1.27 * After that I then need to multiple the anything that used vm.stats to
246 tdb 1.8 * get the system statistics by pagesize
247 pajs 1.6 */
248 ats 1.29 pagesize = getpagesize();
249 tdb 1.8 mem_stat.cache=cache_count*pagesize;
250    
251     /* Of couse nothing is ever that simple :) And I have inactive pages to
252     * deal with too. So I'm going to add them to free memory :)
253 pajs 1.6 */
254 tdb 1.8 mem_stat.free=(free_count*pagesize)+(inactive_count*pagesize);
255     mem_stat.used=physmem-mem_stat.free;
256 ats 1.12 #endif
257 ats 1.14
258 tdb 1.33 #if defined(NETBSD)
259 ats 1.24 if ((uvm = sg_get_uvmexp()) == NULL) {
260 ats 1.12 return NULL;
261     }
262 tdb 1.20
263 ats 1.14 mem_stat.total = uvm->pagesize * uvm->npages;
264 ats 1.12 mem_stat.cache = uvm->pagesize * (uvm->filepages + uvm->execpages);
265 ats 1.14 mem_stat.free = uvm->pagesize * (uvm->free + uvm->inactive);
266     mem_stat.used = mem_stat.total - mem_stat.free;
267 pajs 1.1 #endif
268    
269 tdb 1.33 #if defined(OPENBSD)
270 tdb 1.35 /* 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 tdb 1.33 */
277 tdb 1.35
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 tdb 1.33 while (pagesize > 1) {
292 tdb 1.35 page_multiplier++;
293 tdb 1.33 pagesize >>= 1;
294     }
295    
296 tdb 1.35 /* We can now ret the the raw VM stats (in pages) using the
297     * sysctl interface.
298     */
299 tdb 1.33 mib[0] = CTL_VM;
300     mib[1] = VM_METER;
301     size = sizeof(vmtotal);
302 tdb 1.35
303 tdb 1.34 if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
304 tdb 1.35 bzero(&vmtotal, sizeof(vmtotal));
305     sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_METER");
306 tdb 1.33 return NULL;
307     }
308 tdb 1.35
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 tdb 1.33 mem_stat.total = (mem_stat.used + mem_stat.free);
315     #endif
316    
317 tdb 1.32 #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 pajs 1.1 return &mem_stat;
331     }