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

# Content
1 /*
2 * i-scream libstatgrab
3 * http://www.i-scream.org
4 * Copyright (C) 2000-2004 i-scream
5 *
6 * 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 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * 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 *
21 * $Id: swap_stats.c,v 1.21 2004/07/18 21:30:11 ats Exp $
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include "statgrab.h"
29 #include "tools.h"
30 #ifdef SOLARIS
31 #include <sys/stat.h>
32 #include <sys/swap.h>
33 #include <unistd.h>
34 #endif
35 #if defined(LINUX) || defined(CYGWIN)
36 #include <stdio.h>
37 #include <string.h>
38 #endif
39 #if defined(FREEBSD) || defined(DFBSD)
40 #ifdef FREEBSD5
41 #include <sys/param.h>
42 #include <sys/sysctl.h>
43 #include <sys/user.h>
44 #else
45 #include <sys/types.h>
46 #include <kvm.h>
47 #endif
48 #include <unistd.h>
49 #endif
50 #if defined(NETBSD) || defined(OPENBSD)
51 #include <sys/param.h>
52 #include <sys/time.h>
53 #include <uvm/uvm.h>
54 #include <unistd.h>
55 #endif
56 #ifdef HPUX
57 #include <sys/param.h>
58 #include <sys/pstat.h>
59 #include <unistd.h>
60 #endif
61
62 sg_swap_stats *sg_get_swap_stats(){
63
64 static sg_swap_stats swap_stat;
65
66 #ifdef HPUX
67 struct pst_swapinfo pstat_swapinfo;
68 int swapidx = 0;
69 #endif
70 #ifdef SOLARIS
71 struct anoninfo ai;
72 int pagesize;
73 #endif
74 #if defined(LINUX) || defined(CYGWIN)
75 FILE *f;
76 char *line_ptr;
77 unsigned long long value;
78 #endif
79 #if defined(FREEBSD) || defined(DFBSD)
80 int pagesize;
81 #ifdef FREEBSD5
82 struct xswdev xsw;
83 int mib[16], n;
84 size_t mibsize, size;
85 #else
86 struct kvm_swap swapinfo;
87 kvm_t *kvmd;
88 #endif
89 #endif
90 #if defined(NETBSD) || defined(OPENBSD)
91 struct uvmexp *uvm;
92 #endif
93
94 #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 #ifdef SOLARIS
127 if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
128 sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
129 return NULL;
130 }
131 if (swapctl(SC_AINFO, &ai) == -1) {
132 sg_set_error_with_errno(SG_ERROR_SWAPCTL, NULL);
133 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 #endif
139 #if defined(LINUX) || defined(CYGWIN)
140 if ((f = fopen("/proc/meminfo", "r")) == NULL) {
141 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo");
142 return NULL;
143 }
144
145 while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
146 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 }
157
158 fclose(f);
159 swap_stat.used = swap_stat.total - swap_stat.free;
160 #endif
161 #if defined(FREEBSD) || defined(DFBSD)
162 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 sg_set_error_with_errno(SG_ERROR_SYSCTLNAMETOMIB,
171 "vm.swap_info");
172 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 sg_set_error(SG_ERROR_XSW_VER_MISMATCH, NULL);
182 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 sg_set_error_with_errno(SG_ERROR_SYSCTL, "vm.swap_info");
189 return NULL;
190 }
191 #else
192 if((kvmd = sg_get_kvm()) == NULL){
193 return NULL;
194 }
195 if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){
196 sg_set_error(SG_ERROR_KVM_GETSWAPINFO, NULL);
197 return NULL;
198 }
199
200 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 #endif
207 #if defined(NETBSD) || defined(OPENBSD)
208 if ((uvm = sg_get_uvmexp()) == NULL) {
209 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 #endif
216
217 return &swap_stat;
218
219 }