ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/page_stats.c
Revision: 1.12
Committed: Fri Jan 9 10:06:01 2004 UTC (20 years, 4 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.11: +1 -0 lines
Log Message:
Fixed leaking fd in diskio_stats code. Fixed a potential leak in page_stats.

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3 tdb 1.6 * 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 pajs 1.2 #include "statgrab.h"
26 ats 1.9 #include "tools.h"
27 pajs 1.4 #include <time.h>
28 pajs 1.1 #ifdef SOLARIS
29     #include <kstat.h>
30     #include <sys/sysinfo.h>
31 pajs 1.2 #include <string.h>
32 pajs 1.1 #endif
33 ats 1.11 #if defined(LINUX) || defined(CYGWIN)
34 pajs 1.4 #include <stdio.h>
35 ats 1.10 #include <string.h>
36 pajs 1.4 #endif
37 pajs 1.5 #ifdef FREEBSD
38     #include <sys/types.h>
39     #include <sys/sysctl.h>
40     #endif
41 pajs 1.3
42     static page_stat_t page_stats;
43     static int page_stats_uninit=1;
44    
45 pajs 1.1 page_stat_t *get_page_stats(){
46 pajs 1.4 #ifdef SOLARIS
47 pajs 1.1 kstat_ctl_t *kc;
48     kstat_t *ksp;
49     cpu_stat_t cs;
50 pajs 1.4 #endif
51 ats 1.11 #if defined(LINUX) || defined(CYGWIN)
52 pajs 1.4 FILE *f;
53     char *line_ptr;
54     #endif
55 pajs 1.5 #ifdef FREEBSD
56     size_t size;
57     #endif
58 ats 1.9 #ifdef NETBSD
59     struct uvmexp *uvm;
60     #endif
61 pajs 1.2
62 ats 1.9 page_stats.systime = time(NULL);
63 pajs 1.2 page_stats.pages_pagein=0;
64     page_stats.pages_pageout=0;
65 pajs 1.1
66 pajs 1.4 #ifdef SOLARIS
67 pajs 1.1 if ((kc = kstat_open()) == NULL) {
68     return NULL;
69     }
70     for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
71     if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
72     if (kstat_read(kc, ksp, &cs) == -1) {
73     continue;
74     }
75    
76 pajs 1.2 page_stats.pages_pagein+=(long long)cs.cpu_vminfo.pgpgin;
77     page_stats.pages_pageout+=(long long)cs.cpu_vminfo.pgpgout;
78     }
79    
80     kstat_close(kc);
81 pajs 1.4 #endif
82 ats 1.11 #if defined(LINUX) || defined(CYGWIN)
83 ats 1.10 if ((f = fopen("/proc/vmstat", "r")) != NULL) {
84     while ((line_ptr = f_read_line(f, "")) != NULL) {
85     long long value;
86    
87     if (sscanf(line_ptr, "%*s %lld", &value) != 1) {
88     continue;
89     }
90    
91     if (strncmp(line_ptr, "pgpgin ", 7) == 0) {
92     page_stats.pages_pagein = value;
93     } else if (strncmp(line_ptr, "pgpgout ", 8) == 0) {
94     page_stats.pages_pageout = value;
95     }
96     }
97    
98     fclose(f);
99     } else if ((f = fopen("/proc/stat", "r")) != NULL) {
100     if ((line_ptr = f_read_line(f, "page")) == NULL) {
101     fclose(f);
102     return NULL;
103     }
104    
105     if (sscanf(line_ptr, "page %lld %lld", &page_stats.pages_pagein, &page_stats.pages_pageout) != 2) {
106 pajs 1.12 fclose(f);
107 ats 1.10 return NULL;
108     }
109    
110 pajs 1.4 fclose(f);
111 ats 1.10 } else {
112 pajs 1.4 return NULL;
113     }
114 pajs 1.5 #endif
115     #ifdef FREEBSD
116 ats 1.8 size = sizeof page_stats.pages_pagein;
117 ats 1.7 if (sysctlbyname("vm.stats.vm.v_swappgsin", &page_stats.pages_pagein, &size, NULL, 0) < 0){
118 pajs 1.5 return NULL;
119     }
120 ats 1.8 size = sizeof page_stats.pages_pageout;
121 ats 1.7 if (sysctlbyname("vm.stats.vm.v_swappgsout", &page_stats.pages_pageout, &size, NULL, 0) < 0){
122 pajs 1.5 return NULL;
123     }
124 ats 1.9 #endif
125     #ifdef NETBSD
126     if ((uvm = get_uvmexp()) == NULL) {
127     return NULL;
128     }
129     page_stats.pages_pagein = uvm->pgswapin;
130     page_stats.pages_pageout = uvm->pgswapout;
131 pajs 1.4 #endif
132 pajs 1.1
133 pajs 1.2 return &page_stats;
134 pajs 1.3 }
135    
136     page_stat_t *get_page_stats_diff(){
137     page_stat_t *page_ptr;
138     static page_stat_t page_stats_diff;
139    
140     if(page_stats_uninit){
141     page_ptr=get_page_stats();
142     if(page_ptr==NULL){
143     return NULL;
144     }
145     page_stats_uninit=0;
146     return page_ptr;
147     }
148    
149     page_stats_diff.pages_pagein=page_stats.pages_pagein;
150     page_stats_diff.pages_pageout=page_stats.pages_pageout;
151     page_stats_diff.systime=page_stats.systime;
152    
153     page_ptr=get_page_stats();
154     if(page_ptr==NULL){
155     return NULL;
156     }
157    
158     page_stats_diff.pages_pagein=page_stats.pages_pagein-page_stats_diff.pages_pagein;
159     page_stats_diff.pages_pageout=page_stats.pages_pageout-page_stats_diff.pages_pageout;
160     page_stats_diff.systime=page_stats.systime-page_stats_diff.systime;
161    
162     return &page_stats_diff;
163 pajs 1.1 }