ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/swap_stats.c
Revision: 1.8
Committed: Fri Oct 24 17:26:43 2003 UTC (20 years, 6 months ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.7: +17 -8 lines
Log Message:
Support memory, swap and paging stats on Linux 2.6. For memory and swap,
this just means reading the new-style data in /proc/meminfo (since it's
present in 2.2 and 2.4 too); for paging, this means trying to read
/proc/vmstat if it's available, else reading /proc/stat (for 2.2).

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3 tdb 1.5 * http://www.i-scream.org
4     * Copyright (C) 2000-2003 i-scream
5 pajs 1.1 *
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 ats 1.6 #include "tools.h"
27 pajs 1.1 #ifdef SOLARIS
28     #include <sys/stat.h>
29     #include <sys/swap.h>
30     #include <unistd.h>
31     #endif
32 pajs 1.3 #ifdef LINUX
33     #include <stdio.h>
34 ats 1.8 #include <string.h>
35 pajs 1.3 #endif
36 pajs 1.4 #ifdef FREEBSD
37     #include <unistd.h>
38     #include <sys/types.h>
39     #include <kvm.h>
40     #endif
41 pajs 1.1
42     swap_stat_t *get_swap_stats(){
43    
44     static swap_stat_t swap_stat;
45    
46 pajs 1.3 #ifdef SOLARIS
47 pajs 1.1 struct anoninfo ai;
48     int pagesize;
49 pajs 1.3 #endif
50     #ifdef LINUX
51     FILE *f;
52     char *line_ptr;
53 ats 1.8 unsigned long long value;
54 pajs 1.3 #endif
55 pajs 1.4 #ifdef FREEBSD
56     struct kvm_swap swapinfo;
57     int pagesize;
58 ats 1.6 kvm_t *kvmd;
59 pajs 1.4 #endif
60 ats 1.7 #ifdef NETBSD
61     struct uvmexp *uvm;
62     #endif
63 pajs 1.1
64 pajs 1.3 #ifdef SOLARIS
65 pajs 1.1 if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
66     return NULL;
67     }
68     if (swapctl(SC_AINFO, &ai) == -1) {
69     return NULL;
70     }
71     swap_stat.total = (long long)ai.ani_max * (long long)pagesize;
72     swap_stat.used = (long long)ai.ani_resv * (long long)pagesize;
73     swap_stat.free = swap_stat.total - swap_stat.used;
74 pajs 1.3 #endif
75     #ifdef LINUX
76 ats 1.8 if ((f = fopen("/proc/meminfo", "r")) == NULL) {
77 pajs 1.3 return NULL;
78     }
79 ats 1.8
80     while ((line_ptr = f_read_line(f, "")) != NULL) {
81     if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) {
82     continue;
83     }
84     value *= 1024;
85    
86     if (strncmp(line_ptr, "SwapTotal:", 10) == 0) {
87     swap_stat.total = value;
88     } else if (strncmp(line_ptr, "SwapFree:", 9) == 0) {
89     swap_stat.free = value;
90     }
91 pajs 1.3 }
92 ats 1.8
93 pajs 1.3 fclose(f);
94 ats 1.8 swap_stat.used = swap_stat.total - swap_stat.free;
95 pajs 1.3 #endif
96 pajs 1.4 #ifdef FREEBSD
97 ats 1.6 if((kvmd = get_kvm()) == NULL){
98 pajs 1.4 return NULL;
99     }
100     if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){
101     return NULL;
102     }
103     pagesize=getpagesize();
104 pajs 1.1
105 pajs 1.4 swap_stat.total= (long long)swapinfo.ksw_total * (long long)pagesize;
106     swap_stat.used = (long long)swapinfo.ksw_used * (long long)pagesize;
107     swap_stat.free = swap_stat.total-swap_stat.used;
108 ats 1.7 #endif
109     #ifdef NETBSD
110     if ((uvm = get_uvmexp()) == NULL) {
111     return NULL;
112     }
113    
114     swap_stat.total = (long long)uvm->pagesize * (long long)uvm->swpages;
115     swap_stat.used = (long long)uvm->pagesize * (long long)uvm->swpginuse;
116     swap_stat.free = swap_stat.total - swap_stat.used;
117 pajs 1.4 #endif
118 pajs 1.1 return &swap_stat;
119    
120     }