ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/swap_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/swap_stats.c (file contents):
Revision 1.6 by ats, Sun Oct 19 00:25:30 2003 UTC vs.
Revision 1.26 by tdb, Sun Oct 3 18:35:58 2010 UTC

# Line 1 | Line 1
1 < /*
2 < * i-scream central monitoring system
1 > /*
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 i-scream
5   *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU General Public License
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
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 program is distributed in the hope that it will be useful,
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
14 < * GNU General Public License for more details.
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 General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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$
22   */
23  
24   #ifdef HAVE_CONFIG_H
# Line 29 | Line 32
32   #include <sys/swap.h>
33   #include <unistd.h>
34   #endif
35 < #ifdef LINUX
35 > #if defined(LINUX) || defined(CYGWIN)
36   #include <stdio.h>
37 + #include <string.h>
38   #endif
39 < #ifdef FREEBSD
40 < #include <unistd.h>
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 AIX
63 + #include <unistd.h>
64 + #include <libperfstat.h>
65 + #endif
66 + #ifdef WIN32
67 + #include <windows.h>
68 + #endif
69  
70 < swap_stat_t *get_swap_stats(){
70 > sg_swap_stats *sg_get_swap_stats(){
71  
72 <        static swap_stat_t swap_stat;
72 >        static sg_swap_stats swap_stat;
73  
74 + #ifdef HPUX
75 +        struct pst_swapinfo pstat_swapinfo[SWAP_BATCH];
76 +        int swapidx = 0;
77 +        int num, i;
78 + #endif
79   #ifdef SOLARIS
80          struct anoninfo ai;
81          int pagesize;
82   #endif
83 < #ifdef LINUX
83 > #if defined(LINUX) || defined(CYGWIN)
84          FILE *f;
85          char *line_ptr;
86 +        unsigned long long value;
87   #endif
88 < #ifdef FREEBSD
54 <        struct kvm_swap swapinfo;
88 > #if defined(FREEBSD) || defined(DFBSD)
89          int pagesize;
90 + #ifdef FREEBSD5
91 +        struct xswdev xsw;
92 +        int mib[16], n;
93 +        size_t mibsize, size;
94 + #else
95 +        struct kvm_swap swapinfo;
96          kvm_t *kvmd;
97   #endif
98 + #endif
99 + #if defined(NETBSD) || defined(OPENBSD)
100 +        struct uvmexp *uvm;
101 + #endif
102 + #ifdef AIX
103 +        perfstat_memory_total_t mem;
104 +        long long pagesize;
105 + #endif
106 + #ifdef WIN32
107 +        MEMORYSTATUSEX memstats;
108 + #endif
109  
110 + #ifdef HPUX
111 +        swap_stat.total = 0;
112 +        swap_stat.used = 0;
113 +        swap_stat.free = 0;
114 +
115 +        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 +                        break;
124 +                }
125 +
126 +                for (i = 0; i < num; i++) {
127 +                        struct pst_swapinfo *si = &pstat_swapinfo[i];
128 +
129 +                        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 +                }
144 +                swapidx = pstat_swapinfo[num - 1].pss_idx + 1;
145 +        }
146 + #endif
147 + #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   #ifdef SOLARIS
164          if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
165 +                sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
166                  return NULL;
167          }
168          if (swapctl(SC_AINFO, &ai) == -1) {
169 +                sg_set_error_with_errno(SG_ERROR_SWAPCTL, NULL);
170                  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   #endif
176 < #ifdef LINUX
177 <        if ((f=fopen("/proc/meminfo", "r" ))==NULL) {
176 > #if defined(LINUX) || defined(CYGWIN)
177 >        if ((f = fopen("/proc/meminfo", "r")) == NULL) {
178 >                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo");
179                  return NULL;
180          }
181 <        if((line_ptr=f_read_line(f, "Swap:"))==NULL){
182 <                fclose(f);
181 >
182 >        while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
183 >                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 >        }
194 >
195 >        fclose(f);
196 >        swap_stat.used = swap_stat.total - swap_stat.free;
197 > #endif
198 > #if defined(FREEBSD) || defined(DFBSD)
199 >        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 >                sg_set_error_with_errno(SG_ERROR_SYSCTLNAMETOMIB,
208 >                                        "vm.swap_info");
209                  return NULL;
210          }
211 <        if((sscanf(line_ptr, "Swap: %lld %lld %lld", &swap_stat.total, &swap_stat.used, &swap_stat.free))!=3){
212 <                fclose(f);
211 >        for (n = 0; ; ++n) {
212 >                mib[mibsize] = n;
213 >                size = sizeof xsw;
214 >                if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) < 0) {
215 >                        break;
216 >                }
217 >                if (xsw.xsw_version != XSWDEV_VERSION) {
218 >                        sg_set_error(SG_ERROR_XSW_VER_MISMATCH, NULL);
219 >                        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 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "vm.swap_info");
226                  return NULL;
227          }
228 <        fclose(f);
229 < #endif
84 < #ifdef FREEBSD
85 <        if((kvmd = get_kvm()) == NULL){
228 > #else
229 >        if((kvmd = sg_get_kvm()) == NULL){
230                  return NULL;
231          }
232          if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){
233 +                sg_set_error(SG_ERROR_KVM_GETSWAPINFO, NULL);
234                  return NULL;
235          }
91        pagesize=getpagesize();
236  
237 <        swap_stat.total= (long long)swapinfo.ksw_total * (long long)pagesize;
238 <        swap_stat.used = (long long)swapinfo.ksw_used * (long long)pagesize;
95 <        swap_stat.free = swap_stat.total-swap_stat.used;
237 >        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 + #endif
244 + #if defined(NETBSD) || defined(OPENBSD)
245 +        if ((uvm = sg_get_uvmexp()) == NULL) {
246 +                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 + #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 + #endif
267 +
268          return &swap_stat;
269  
270   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines