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.23 2004/11/06 15:41:30 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 |
#define SWAP_BATCH 5 |
61 |
#endif |
62 |
#ifdef WIN32 |
63 |
#include <windows.h> |
64 |
#endif |
65 |
|
66 |
sg_swap_stats *sg_get_swap_stats(){ |
67 |
|
68 |
static sg_swap_stats swap_stat; |
69 |
|
70 |
#ifdef HPUX |
71 |
struct pst_swapinfo pstat_swapinfo[SWAP_BATCH]; |
72 |
int swapidx = 0; |
73 |
int num, i; |
74 |
#endif |
75 |
#ifdef SOLARIS |
76 |
struct anoninfo ai; |
77 |
int pagesize; |
78 |
#endif |
79 |
#if defined(LINUX) || defined(CYGWIN) |
80 |
FILE *f; |
81 |
char *line_ptr; |
82 |
unsigned long long value; |
83 |
#endif |
84 |
#if defined(FREEBSD) || defined(DFBSD) |
85 |
int pagesize; |
86 |
#ifdef FREEBSD5 |
87 |
struct xswdev xsw; |
88 |
int mib[16], n; |
89 |
size_t mibsize, size; |
90 |
#else |
91 |
struct kvm_swap swapinfo; |
92 |
kvm_t *kvmd; |
93 |
#endif |
94 |
#endif |
95 |
#if defined(NETBSD) || defined(OPENBSD) |
96 |
struct uvmexp *uvm; |
97 |
#endif |
98 |
#ifdef WIN32 |
99 |
MEMORYSTATUSEX memstats; |
100 |
#endif |
101 |
|
102 |
#ifdef HPUX |
103 |
swap_stat.total = 0; |
104 |
swap_stat.used = 0; |
105 |
swap_stat.free = 0; |
106 |
|
107 |
while (1) { |
108 |
num = pstat_getswap(pstat_swapinfo, sizeof pstat_swapinfo[0], |
109 |
SWAP_BATCH, swapidx); |
110 |
if (num == -1) { |
111 |
sg_set_error_with_errno(SG_ERROR_PSTAT, |
112 |
"pstat_getswap"); |
113 |
return NULL; |
114 |
} else if (num == 0) { |
115 |
break; |
116 |
} |
117 |
|
118 |
for (i = 0; i < num; i++) { |
119 |
struct pst_swapinfo *si = &pstat_swapinfo[i]; |
120 |
|
121 |
if ((si->pss_flags & SW_ENABLED) != SW_ENABLED) { |
122 |
continue; |
123 |
} |
124 |
|
125 |
if ((si->pss_flags & SW_BLOCK) == SW_BLOCK) { |
126 |
swap_stat.total += ((long long) si->pss_nblksavail) * 1024LL; |
127 |
swap_stat.used += ((long long) si->pss_nfpgs) * 1024LL; |
128 |
swap_stat.free = swap_stat.total - swap_stat.used; |
129 |
} |
130 |
if ((si->pss_flags & SW_FS) == SW_FS) { |
131 |
swap_stat.total += ((long long) si->pss_limit) * 1024LL; |
132 |
swap_stat.used += ((long long) si->pss_allocated) * 1024LL; |
133 |
swap_stat.free = swap_stat.total - swap_stat.used; |
134 |
} |
135 |
} |
136 |
swapidx = pstat_swapinfo[num - 1].pss_idx + 1; |
137 |
} |
138 |
#endif |
139 |
#ifdef SOLARIS |
140 |
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
141 |
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE"); |
142 |
return NULL; |
143 |
} |
144 |
if (swapctl(SC_AINFO, &ai) == -1) { |
145 |
sg_set_error_with_errno(SG_ERROR_SWAPCTL, NULL); |
146 |
return NULL; |
147 |
} |
148 |
swap_stat.total = (long long)ai.ani_max * (long long)pagesize; |
149 |
swap_stat.used = (long long)ai.ani_resv * (long long)pagesize; |
150 |
swap_stat.free = swap_stat.total - swap_stat.used; |
151 |
#endif |
152 |
#if defined(LINUX) || defined(CYGWIN) |
153 |
if ((f = fopen("/proc/meminfo", "r")) == NULL) { |
154 |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo"); |
155 |
return NULL; |
156 |
} |
157 |
|
158 |
while ((line_ptr = sg_f_read_line(f, "")) != NULL) { |
159 |
if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) { |
160 |
continue; |
161 |
} |
162 |
value *= 1024; |
163 |
|
164 |
if (strncmp(line_ptr, "SwapTotal:", 10) == 0) { |
165 |
swap_stat.total = value; |
166 |
} else if (strncmp(line_ptr, "SwapFree:", 9) == 0) { |
167 |
swap_stat.free = value; |
168 |
} |
169 |
} |
170 |
|
171 |
fclose(f); |
172 |
swap_stat.used = swap_stat.total - swap_stat.free; |
173 |
#endif |
174 |
#if defined(FREEBSD) || defined(DFBSD) |
175 |
pagesize=getpagesize(); |
176 |
|
177 |
#ifdef FREEBSD5 |
178 |
swap_stat.total = 0; |
179 |
swap_stat.used = 0; |
180 |
|
181 |
mibsize = sizeof mib / sizeof mib[0]; |
182 |
if (sysctlnametomib("vm.swap_info", mib, &mibsize) < 0) { |
183 |
sg_set_error_with_errno(SG_ERROR_SYSCTLNAMETOMIB, |
184 |
"vm.swap_info"); |
185 |
return NULL; |
186 |
} |
187 |
for (n = 0; ; ++n) { |
188 |
mib[mibsize] = n; |
189 |
size = sizeof xsw; |
190 |
if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, NULL) < 0) { |
191 |
break; |
192 |
} |
193 |
if (xsw.xsw_version != XSWDEV_VERSION) { |
194 |
sg_set_error(SG_ERROR_XSW_VER_MISMATCH, NULL); |
195 |
return NULL; |
196 |
} |
197 |
swap_stat.total += (long long) xsw.xsw_nblks; |
198 |
swap_stat.used += (long long) xsw.xsw_used; |
199 |
} |
200 |
if (errno != ENOENT) { |
201 |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "vm.swap_info"); |
202 |
return NULL; |
203 |
} |
204 |
#else |
205 |
if((kvmd = sg_get_kvm()) == NULL){ |
206 |
return NULL; |
207 |
} |
208 |
if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){ |
209 |
sg_set_error(SG_ERROR_KVM_GETSWAPINFO, NULL); |
210 |
return NULL; |
211 |
} |
212 |
|
213 |
swap_stat.total = (long long)swapinfo.ksw_total; |
214 |
swap_stat.used = (long long)swapinfo.ksw_used; |
215 |
#endif |
216 |
swap_stat.total *= pagesize; |
217 |
swap_stat.used *= pagesize; |
218 |
swap_stat.free = swap_stat.total - swap_stat.used; |
219 |
#endif |
220 |
#if defined(NETBSD) || defined(OPENBSD) |
221 |
if ((uvm = sg_get_uvmexp()) == NULL) { |
222 |
return NULL; |
223 |
} |
224 |
|
225 |
swap_stat.total = (long long)uvm->pagesize * (long long)uvm->swpages; |
226 |
swap_stat.used = (long long)uvm->pagesize * (long long)uvm->swpginuse; |
227 |
swap_stat.free = swap_stat.total - swap_stat.used; |
228 |
#endif |
229 |
#ifdef WIN32 |
230 |
memstats.dwLength = sizeof(memstats); |
231 |
if (!GlobalMemoryStatusEx(&memstats)) { |
232 |
sg_set_error_with_errno(SG_ERROR_MEMSTATUS, |
233 |
"GloblaMemoryStatusEx"); |
234 |
return NULL; |
235 |
} |
236 |
/* the PageFile stats include Phys memory "minus an overhead". |
237 |
* Due to this unknown "overhead" there's no way to extract just page |
238 |
* file use from these numbers */ |
239 |
swap_stat.total = memstats.ullTotalPageFile; |
240 |
swap_stat.free = memstats.ullAvailPageFile; |
241 |
swap_stat.used = swap_stat.total - swap_stat.free; |
242 |
#endif |
243 |
|
244 |
return &swap_stat; |
245 |
|
246 |
} |