ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/swap_stats.c
Revision: 1.22
Committed: Mon Nov 1 18:30:17 2004 UTC (19 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.21: +42 -1 lines
Log Message:
Merge in patch to provide support for HP-UX 11.11.

Contributed by Roy Keene - thanks Roy!

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.22 * $Id: swap_stats.c,v 1.21 2004/07/18 21:30:11 ats 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     #endif
61 pajs 1.1
62 ats 1.18 sg_swap_stats *sg_get_swap_stats(){
63 pajs 1.1
64 ats 1.18 static sg_swap_stats swap_stat;
65 pajs 1.1
66 tdb 1.22 #ifdef HPUX
67     struct pst_swapinfo pstat_swapinfo;
68     int swapidx = 0;
69     #endif
70 pajs 1.3 #ifdef SOLARIS
71 pajs 1.1 struct anoninfo ai;
72     int pagesize;
73 pajs 1.3 #endif
74 ats 1.9 #if defined(LINUX) || defined(CYGWIN)
75 pajs 1.3 FILE *f;
76     char *line_ptr;
77 ats 1.8 unsigned long long value;
78 pajs 1.3 #endif
79 tdb 1.16 #if defined(FREEBSD) || defined(DFBSD)
80 tdb 1.15 int pagesize;
81     #ifdef FREEBSD5
82     struct xswdev xsw;
83     int mib[16], n;
84     size_t mibsize, size;
85     #else
86 pajs 1.4 struct kvm_swap swapinfo;
87 ats 1.6 kvm_t *kvmd;
88 pajs 1.4 #endif
89 tdb 1.15 #endif
90 tdb 1.12 #if defined(NETBSD) || defined(OPENBSD)
91 ats 1.7 struct uvmexp *uvm;
92     #endif
93 pajs 1.1
94 tdb 1.22 #ifdef HPUX
95     swap_stat.total = 0;
96     swap_stat.used = 0;
97     swap_stat.free = 0;
98    
99     /* The 128 here is arbitrary, it can be increased at the expense
100     of more system calls to pstat(). */
101     for (swapidx = 0; swapidx < 128; swapidx++) {
102     if (pstat_getswap(&pstat_swapinfo, sizeof(pstat_swapinfo), 1, swapidx) == -1) {
103     break;
104     }
105    
106     if (pstat_swapinfo.pss_idx != swapidx) {
107     continue;
108     }
109    
110     if ((pstat_swapinfo.pss_flags & SW_ENABLED) != SW_ENABLED) {
111     continue;
112     }
113    
114     if ((pstat_swapinfo.pss_flags & SW_BLOCK) == SW_BLOCK) {
115     swap_stat.total += ((long long) pstat_swapinfo.pss_nblksavail) * 1024LL;
116     swap_stat.used += ((long long) pstat_swapinfo.pss_nfpgs) * 1024LL;
117     swap_stat.free = swap_stat.total - swap_stat.used;
118     }
119     if ((pstat_swapinfo.pss_flags & SW_FS) == SW_FS) {
120     swap_stat.total += ((long long) pstat_swapinfo.pss_limit) * 1024LL;
121     swap_stat.used += ((long long) pstat_swapinfo.pss_allocated) * 1024LL;
122     swap_stat.free = swap_stat.total - swap_stat.used;
123     }
124     }
125     #endif
126 pajs 1.3 #ifdef SOLARIS
127 pajs 1.1 if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
128 ats 1.21 sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
129 pajs 1.1 return NULL;
130     }
131     if (swapctl(SC_AINFO, &ai) == -1) {
132 ats 1.21 sg_set_error_with_errno(SG_ERROR_SWAPCTL, NULL);
133 pajs 1.1 return NULL;
134     }
135     swap_stat.total = (long long)ai.ani_max * (long long)pagesize;
136     swap_stat.used = (long long)ai.ani_resv * (long long)pagesize;
137     swap_stat.free = swap_stat.total - swap_stat.used;
138 pajs 1.3 #endif
139 ats 1.9 #if defined(LINUX) || defined(CYGWIN)
140 ats 1.8 if ((f = fopen("/proc/meminfo", "r")) == NULL) {
141 ats 1.21 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo");
142 pajs 1.3 return NULL;
143     }
144 ats 1.8
145 ats 1.18 while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
146 ats 1.8 if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) {
147     continue;
148     }
149     value *= 1024;
150    
151     if (strncmp(line_ptr, "SwapTotal:", 10) == 0) {
152     swap_stat.total = value;
153     } else if (strncmp(line_ptr, "SwapFree:", 9) == 0) {
154     swap_stat.free = value;
155     }
156 pajs 1.3 }
157 ats 1.8
158 pajs 1.3 fclose(f);
159 ats 1.8 swap_stat.used = swap_stat.total - swap_stat.free;
160 pajs 1.3 #endif
161 tdb 1.16 #if defined(FREEBSD) || defined(DFBSD)
162 tdb 1.15 pagesize=getpagesize();
163    
164     #ifdef FREEBSD5
165     swap_stat.total = 0;
166     swap_stat.used = 0;
167    
168     mibsize = sizeof mib / sizeof mib[0];
169     if (sysctlnametomib("vm.swap_info", mib, &mibsize) < 0) {
170 ats 1.21 sg_set_error_with_errno(SG_ERROR_SYSCTLNAMETOMIB,
171     "vm.swap_info");
172 tdb 1.15 return NULL;
173     }
174     for (n = 0; ; ++n) {
175     mib[mibsize] = n;
176     size = sizeof xsw;
177     if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, NULL) < 0) {
178     break;
179     }
180     if (xsw.xsw_version != XSWDEV_VERSION) {
181 tdb 1.20 sg_set_error(SG_ERROR_XSW_VER_MISMATCH, NULL);
182 tdb 1.15 return NULL;
183     }
184     swap_stat.total += (long long) xsw.xsw_nblks;
185     swap_stat.used += (long long) xsw.xsw_used;
186     }
187     if (errno != ENOENT) {
188 ats 1.21 sg_set_error_with_errno(SG_ERROR_SYSCTL, "vm.swap_info");
189 tdb 1.15 return NULL;
190     }
191     #else
192 ats 1.18 if((kvmd = sg_get_kvm()) == NULL){
193 pajs 1.4 return NULL;
194     }
195     if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){
196 tdb 1.20 sg_set_error(SG_ERROR_KVM_GETSWAPINFO, NULL);
197 pajs 1.4 return NULL;
198     }
199 pajs 1.1
200 tdb 1.15 swap_stat.total = (long long)swapinfo.ksw_total;
201     swap_stat.used = (long long)swapinfo.ksw_used;
202     #endif
203     swap_stat.total *= pagesize;
204     swap_stat.used *= pagesize;
205     swap_stat.free = swap_stat.total - swap_stat.used;
206 ats 1.7 #endif
207 tdb 1.13 #if defined(NETBSD) || defined(OPENBSD)
208 ats 1.18 if ((uvm = sg_get_uvmexp()) == NULL) {
209 ats 1.7 return NULL;
210     }
211    
212     swap_stat.total = (long long)uvm->pagesize * (long long)uvm->swpages;
213     swap_stat.used = (long long)uvm->pagesize * (long long)uvm->swpginuse;
214     swap_stat.free = swap_stat.total - swap_stat.used;
215 tdb 1.12 #endif
216    
217 pajs 1.1 return &swap_stat;
218    
219     }