ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/swap_stats.c
Revision: 1.23
Committed: Sat Nov 6 15:41:30 2004 UTC (19 years, 6 months ago) by ats
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_12, LIBSTATGRAB_0_11_1, LIBSTATGRAB_0_11
Changes since 1.22: +29 -22 lines
Log Message:
Rework the HP-UX swap stats code to fetch batches of swap devices.
(Untested.)

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