ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/swap_stats.c
Revision: 1.6
Committed: Sun Oct 19 00:25:30 2003 UTC (20 years, 7 months ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.5: +3 -6 lines
Log Message:
Add a helper function for opening the kvm handle. This removes a little
bit of duplicated code, and means that we don't open and close kvm
(which can potentially be quite expensive) each time we use it. This
will also be useful for NetBSD.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org
4 * Copyright (C) 2000-2003 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.
10 *
11 * This program 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.
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "statgrab.h"
26 #include "tools.h"
27 #ifdef SOLARIS
28 #include <sys/stat.h>
29 #include <sys/swap.h>
30 #include <unistd.h>
31 #endif
32 #ifdef LINUX
33 #include <stdio.h>
34 #endif
35 #ifdef FREEBSD
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <kvm.h>
39 #endif
40
41 swap_stat_t *get_swap_stats(){
42
43 static swap_stat_t swap_stat;
44
45 #ifdef SOLARIS
46 struct anoninfo ai;
47 int pagesize;
48 #endif
49 #ifdef LINUX
50 FILE *f;
51 char *line_ptr;
52 #endif
53 #ifdef FREEBSD
54 struct kvm_swap swapinfo;
55 int pagesize;
56 kvm_t *kvmd;
57 #endif
58
59 #ifdef SOLARIS
60 if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
61 return NULL;
62 }
63 if (swapctl(SC_AINFO, &ai) == -1) {
64 return NULL;
65 }
66 swap_stat.total = (long long)ai.ani_max * (long long)pagesize;
67 swap_stat.used = (long long)ai.ani_resv * (long long)pagesize;
68 swap_stat.free = swap_stat.total - swap_stat.used;
69 #endif
70 #ifdef LINUX
71 if ((f=fopen("/proc/meminfo", "r" ))==NULL) {
72 return NULL;
73 }
74 if((line_ptr=f_read_line(f, "Swap:"))==NULL){
75 fclose(f);
76 return NULL;
77 }
78 if((sscanf(line_ptr, "Swap: %lld %lld %lld", &swap_stat.total, &swap_stat.used, &swap_stat.free))!=3){
79 fclose(f);
80 return NULL;
81 }
82 fclose(f);
83 #endif
84 #ifdef FREEBSD
85 if((kvmd = get_kvm()) == NULL){
86 return NULL;
87 }
88 if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){
89 return NULL;
90 }
91 pagesize=getpagesize();
92
93 swap_stat.total= (long long)swapinfo.ksw_total * (long long)pagesize;
94 swap_stat.used = (long long)swapinfo.ksw_used * (long long)pagesize;
95 swap_stat.free = swap_stat.total-swap_stat.used;
96 #endif
97 return &swap_stat;
98
99 }