ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/swap_stats.c
Revision: 1.26
Committed: Sun Oct 3 18:35:58 2010 UTC (13 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.25: +25 -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.10 /*
2 tdb 1.19 * i-scream libstatgrab
3 tdb 1.5 * http://www.i-scream.org
4 tdb 1.10 * Copyright (C) 2000-2004 i-scream
5 pajs 1.1 *
6 tdb 1.10 * 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.10 * 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.10 * 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.10 * 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.11 *
21 tdb 1.26 * $Id: swap_stats.c,v 1.25 2007/07/05 16:46:06 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.6 #include "tools.h"
30 pajs 1.1 #ifdef SOLARIS
31     #include <sys/stat.h>
32     #include <sys/swap.h>
33     #include <unistd.h>
34     #endif
35 ats 1.9 #if defined(LINUX) || defined(CYGWIN)
36 pajs 1.3 #include <stdio.h>
37 ats 1.8 #include <string.h>
38 pajs 1.3 #endif
39 tdb 1.16 #if defined(FREEBSD) || defined(DFBSD)
40 tdb 1.15 #ifdef FREEBSD5
41     #include <sys/param.h>
42     #include <sys/sysctl.h>
43     #include <sys/user.h>
44     #else
45 pajs 1.4 #include <sys/types.h>
46     #include <kvm.h>
47     #endif
48 tdb 1.17 #include <unistd.h>
49 tdb 1.15 #endif
50 tdb 1.13 #if defined(NETBSD) || defined(OPENBSD)
51 tdb 1.12 #include <sys/param.h>
52 ats 1.14 #include <sys/time.h>
53 tdb 1.12 #include <uvm/uvm.h>
54 tdb 1.17 #include <unistd.h>
55 tdb 1.12 #endif
56 tdb 1.22 #ifdef HPUX
57     #include <sys/param.h>
58     #include <sys/pstat.h>
59     #include <unistd.h>
60 ats 1.23 #define SWAP_BATCH 5
61 tdb 1.22 #endif
62 tdb 1.26 #ifdef AIX
63     #include <unistd.h>
64     #include <libperfstat.h>
65     #endif
66 tdb 1.24 #ifdef WIN32
67     #include <windows.h>
68     #endif
69 pajs 1.1
70 ats 1.18 sg_swap_stats *sg_get_swap_stats(){
71 pajs 1.1
72 ats 1.18 static sg_swap_stats swap_stat;
73 pajs 1.1
74 tdb 1.22 #ifdef HPUX
75 ats 1.23 struct pst_swapinfo pstat_swapinfo[SWAP_BATCH];
76 tdb 1.22 int swapidx = 0;
77 ats 1.23 int num, i;
78 tdb 1.22 #endif
79 pajs 1.3 #ifdef SOLARIS
80 pajs 1.1 struct anoninfo ai;
81     int pagesize;
82 pajs 1.3 #endif
83 ats 1.9 #if defined(LINUX) || defined(CYGWIN)
84 pajs 1.3 FILE *f;
85     char *line_ptr;
86 ats 1.8 unsigned long long value;
87 pajs 1.3 #endif
88 tdb 1.16 #if defined(FREEBSD) || defined(DFBSD)
89 tdb 1.15 int pagesize;
90     #ifdef FREEBSD5
91     struct xswdev xsw;
92     int mib[16], n;
93     size_t mibsize, size;
94     #else
95 pajs 1.4 struct kvm_swap swapinfo;
96 ats 1.6 kvm_t *kvmd;
97 pajs 1.4 #endif
98 tdb 1.15 #endif
99 tdb 1.12 #if defined(NETBSD) || defined(OPENBSD)
100 ats 1.7 struct uvmexp *uvm;
101     #endif
102 tdb 1.26 #ifdef AIX
103     perfstat_memory_total_t mem;
104     long long pagesize;
105     #endif
106 tdb 1.24 #ifdef WIN32
107     MEMORYSTATUSEX memstats;
108     #endif
109 pajs 1.1
110 tdb 1.22 #ifdef HPUX
111     swap_stat.total = 0;
112     swap_stat.used = 0;
113     swap_stat.free = 0;
114    
115 ats 1.23 while (1) {
116     num = pstat_getswap(pstat_swapinfo, sizeof pstat_swapinfo[0],
117     SWAP_BATCH, swapidx);
118     if (num == -1) {
119     sg_set_error_with_errno(SG_ERROR_PSTAT,
120     "pstat_getswap");
121     return NULL;
122     } else if (num == 0) {
123 tdb 1.22 break;
124     }
125    
126 ats 1.23 for (i = 0; i < num; i++) {
127     struct pst_swapinfo *si = &pstat_swapinfo[i];
128 tdb 1.22
129 ats 1.23 if ((si->pss_flags & SW_ENABLED) != SW_ENABLED) {
130     continue;
131     }
132    
133     if ((si->pss_flags & SW_BLOCK) == SW_BLOCK) {
134     swap_stat.total += ((long long) si->pss_nblksavail) * 1024LL;
135     swap_stat.used += ((long long) si->pss_nfpgs) * 1024LL;
136     swap_stat.free = swap_stat.total - swap_stat.used;
137     }
138     if ((si->pss_flags & SW_FS) == SW_FS) {
139     swap_stat.total += ((long long) si->pss_limit) * 1024LL;
140     swap_stat.used += ((long long) si->pss_allocated) * 1024LL;
141     swap_stat.free = swap_stat.total - swap_stat.used;
142     }
143 tdb 1.22 }
144 ats 1.23 swapidx = pstat_swapinfo[num - 1].pss_idx + 1;
145 tdb 1.22 }
146     #endif
147 tdb 1.26 #ifdef AIX
148     if ((pagesize = sysconf(_SC_PAGESIZE)) == -1) {
149     sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
150     return NULL;
151     }
152    
153     /* return code is number of structures returned */
154     if(perfstat_memory_total(NULL, &mem, sizeof(perfstat_memory_total_t), 1) != 1) {
155     sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "perfstat_memory_total");
156     return NULL;
157     }
158    
159     swap_stat.total = ((long long)mem.pgsp_total) * pagesize;
160     swap_stat.free = ((long long)mem.pgsp_free) * pagesize;
161     swap_stat.used = swap_stat.total - swap_stat.free;
162     #endif
163 pajs 1.3 #ifdef SOLARIS
164 pajs 1.1 if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
165 ats 1.21 sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
166 pajs 1.1 return NULL;
167     }
168     if (swapctl(SC_AINFO, &ai) == -1) {
169 ats 1.21 sg_set_error_with_errno(SG_ERROR_SWAPCTL, NULL);
170 pajs 1.1 return NULL;
171     }
172     swap_stat.total = (long long)ai.ani_max * (long long)pagesize;
173     swap_stat.used = (long long)ai.ani_resv * (long long)pagesize;
174     swap_stat.free = swap_stat.total - swap_stat.used;
175 pajs 1.3 #endif
176 ats 1.9 #if defined(LINUX) || defined(CYGWIN)
177 ats 1.8 if ((f = fopen("/proc/meminfo", "r")) == NULL) {
178 ats 1.21 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo");
179 pajs 1.3 return NULL;
180     }
181 ats 1.8
182 ats 1.18 while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
183 ats 1.8 if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) {
184     continue;
185     }
186     value *= 1024;
187    
188     if (strncmp(line_ptr, "SwapTotal:", 10) == 0) {
189     swap_stat.total = value;
190     } else if (strncmp(line_ptr, "SwapFree:", 9) == 0) {
191     swap_stat.free = value;
192     }
193 pajs 1.3 }
194 ats 1.8
195 pajs 1.3 fclose(f);
196 ats 1.8 swap_stat.used = swap_stat.total - swap_stat.free;
197 pajs 1.3 #endif
198 tdb 1.16 #if defined(FREEBSD) || defined(DFBSD)
199 tdb 1.15 pagesize=getpagesize();
200    
201     #ifdef FREEBSD5
202     swap_stat.total = 0;
203     swap_stat.used = 0;
204    
205     mibsize = sizeof mib / sizeof mib[0];
206     if (sysctlnametomib("vm.swap_info", mib, &mibsize) < 0) {
207 ats 1.21 sg_set_error_with_errno(SG_ERROR_SYSCTLNAMETOMIB,
208     "vm.swap_info");
209 tdb 1.15 return NULL;
210     }
211     for (n = 0; ; ++n) {
212     mib[mibsize] = n;
213     size = sizeof xsw;
214 tdb 1.25 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) < 0) {
215 tdb 1.15 break;
216     }
217     if (xsw.xsw_version != XSWDEV_VERSION) {
218 tdb 1.20 sg_set_error(SG_ERROR_XSW_VER_MISMATCH, NULL);
219 tdb 1.15 return NULL;
220     }
221     swap_stat.total += (long long) xsw.xsw_nblks;
222     swap_stat.used += (long long) xsw.xsw_used;
223     }
224     if (errno != ENOENT) {
225 ats 1.21 sg_set_error_with_errno(SG_ERROR_SYSCTL, "vm.swap_info");
226 tdb 1.15 return NULL;
227     }
228     #else
229 ats 1.18 if((kvmd = sg_get_kvm()) == NULL){
230 pajs 1.4 return NULL;
231     }
232     if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){
233 tdb 1.20 sg_set_error(SG_ERROR_KVM_GETSWAPINFO, NULL);
234 pajs 1.4 return NULL;
235     }
236 pajs 1.1
237 tdb 1.15 swap_stat.total = (long long)swapinfo.ksw_total;
238     swap_stat.used = (long long)swapinfo.ksw_used;
239     #endif
240     swap_stat.total *= pagesize;
241     swap_stat.used *= pagesize;
242     swap_stat.free = swap_stat.total - swap_stat.used;
243 ats 1.7 #endif
244 tdb 1.13 #if defined(NETBSD) || defined(OPENBSD)
245 ats 1.18 if ((uvm = sg_get_uvmexp()) == NULL) {
246 ats 1.7 return NULL;
247     }
248    
249     swap_stat.total = (long long)uvm->pagesize * (long long)uvm->swpages;
250     swap_stat.used = (long long)uvm->pagesize * (long long)uvm->swpginuse;
251     swap_stat.free = swap_stat.total - swap_stat.used;
252 tdb 1.24 #endif
253     #ifdef WIN32
254     memstats.dwLength = sizeof(memstats);
255     if (!GlobalMemoryStatusEx(&memstats)) {
256     sg_set_error_with_errno(SG_ERROR_MEMSTATUS,
257     "GloblaMemoryStatusEx");
258     return NULL;
259     }
260     /* the PageFile stats include Phys memory "minus an overhead".
261     * Due to this unknown "overhead" there's no way to extract just page
262     * file use from these numbers */
263     swap_stat.total = memstats.ullTotalPageFile;
264     swap_stat.free = memstats.ullAvailPageFile;
265     swap_stat.used = swap_stat.total - swap_stat.free;
266 tdb 1.12 #endif
267    
268 pajs 1.1 return &swap_stat;
269    
270     }