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

Comparing projects/libstatgrab/src/libstatgrab/page_stats.c (file contents):
Revision 1.2 by pajs, Tue Feb 18 23:12:08 2003 UTC vs.
Revision 1.23 by tdb, Sat Sep 24 13:29:22 2005 UTC

# Line 1 | Line 1
1 < /*
2 < * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
1 > /*
2 > * i-scream libstatgrab
3 > * http://www.i-scream.org
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
25   #include "config.h"
26   #endif
27  
25 #include <stdio.h>
28   #include "statgrab.h"
29 + #include "tools.h"
30 + #include <time.h>
31   #ifdef SOLARIS
32   #include <kstat.h>
33   #include <sys/sysinfo.h>
34   #include <string.h>
35   #endif
36 + #if defined(LINUX) || defined(CYGWIN)
37 + #include <stdio.h>
38 + #include <string.h>
39 + #endif
40 + #if defined(FREEBSD) || defined(DFBSD)
41 + #include <sys/types.h>
42 + #include <sys/sysctl.h>
43 + #endif
44 + #if defined(NETBSD) || defined(OPENBSD)
45 + #include <sys/param.h>
46 + #include <uvm/uvm.h>
47 + #endif
48 + #ifdef WIN32
49 + #include "win32.h"
50 + #endif
51  
52 < page_stat_t *get_page_stats(){
53 <        static page_stat_t page_stats;
54 <        kstat_ctl_t *kc;
55 <        kstat_t *ksp;
37 <        cpu_stat_t cs;
52 > static sg_page_stats page_stats;
53 > #ifndef WIN32
54 > static int page_stats_uninit=1;
55 > #endif
56  
57 <        page_stats.num_pagein=0;
58 <        page_stats.num_pageout=0;
59 <        page_stats.pages_pagein=0;
60 <        page_stats.pages_pageout=0;
57 > sg_page_stats *sg_get_page_stats(){
58 > #ifdef SOLARIS
59 >        kstat_ctl_t *kc;
60 >        kstat_t *ksp;
61 >        cpu_stat_t cs;
62 > #endif
63 > #if defined(LINUX) || defined(CYGWIN)
64 >        FILE *f;
65 >        char *line_ptr;
66 > #endif
67 > #if defined(FREEBSD) || defined(DFBSD)
68 >        size_t size;
69 > #endif
70 > #if defined(NETBSD) || defined(OPENBSD)
71 >        struct uvmexp *uvm;
72 > #endif
73  
74 <        if ((kc = kstat_open()) == NULL) {
75 <                return NULL;
76 <        }
47 <        for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
48 <                if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
49 <                if (kstat_read(kc, ksp, &cs) == -1) {
50 <                        continue;
51 <                }
74 >        page_stats.systime = time(NULL);
75 >        page_stats.pages_pagein=0;
76 >        page_stats.pages_pageout=0;
77  
78 <                page_stats.num_pagein+=(long long)cs.cpu_vminfo.pgin;
79 <                page_stats.num_pageout+=(long long)cs.cpu_vminfo.pgout;
78 > #ifdef SOLARIS
79 >        if ((kc = kstat_open()) == NULL) {
80 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
81 >                return NULL;
82 >        }
83 >        for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
84 >                if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
85 >                if (kstat_read(kc, ksp, &cs) == -1) {
86 >                        continue;
87 >                }
88 >
89                  page_stats.pages_pagein+=(long long)cs.cpu_vminfo.pgpgin;
90                  page_stats.pages_pageout+=(long long)cs.cpu_vminfo.pgpgout;
91          }
92  
59        page_stats.systime=time(NULL);
60
93          kstat_close(kc);
94 + #endif
95 + #if defined(LINUX) || defined(CYGWIN)
96 +        if ((f = fopen("/proc/vmstat", "r")) != NULL) {
97 +                while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
98 +                        long long value;
99  
100 +                        if (sscanf(line_ptr, "%*s %lld", &value) != 1) {
101 +                                continue;
102 +                        }
103 +
104 +                        if (strncmp(line_ptr, "pgpgin ", 7) == 0) {
105 +                                page_stats.pages_pagein = value;
106 +                        } else if (strncmp(line_ptr, "pgpgout ", 8) == 0) {
107 +                                page_stats.pages_pageout = value;
108 +                        }
109 +                }
110 +
111 +                fclose(f);
112 +        } else if ((f = fopen("/proc/stat", "r")) != NULL) {
113 +                if ((line_ptr = sg_f_read_line(f, "page")) == NULL) {
114 +                        sg_set_error(SG_ERROR_PARSE, "page");
115 +                        fclose(f);
116 +                        return NULL;
117 +                }
118 +
119 +                if (sscanf(line_ptr, "page %lld %lld", &page_stats.pages_pagein, &page_stats.pages_pageout) != 2) {
120 +                        sg_set_error(SG_ERROR_PARSE, "page");
121 +                        fclose(f);
122 +                        return NULL;
123 +                }
124 +
125 +                fclose(f);
126 +        } else {
127 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat");
128 +                return NULL;
129 +        }
130 + #endif
131 + #if defined(FREEBSD) || defined(DFBSD)
132 +        size = sizeof page_stats.pages_pagein;
133 +        if (sysctlbyname("vm.stats.vm.v_swappgsin", &page_stats.pages_pagein, &size, NULL, 0) < 0){
134 +                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
135 +                                        "vm.stats.vm.v_swappgsin");
136 +                return NULL;
137 +        }
138 +        size = sizeof page_stats.pages_pageout;
139 +        if (sysctlbyname("vm.stats.vm.v_swappgsout", &page_stats.pages_pageout, &size, NULL, 0) < 0){
140 +                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
141 +                                        "vm.stats.vm.v_swappgsout");
142 +                return NULL;
143 +        }
144 + #endif
145 + #if defined(NETBSD) || defined(OPENBSD)
146 +        if ((uvm = sg_get_uvmexp()) == NULL) {
147 +                return NULL;
148 +        }
149 +
150 +        page_stats.pages_pagein = uvm->pgswapin;
151 +        page_stats.pages_pageout = uvm->pgswapout;
152 + #endif
153 + #ifdef WIN32
154 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Win32");
155 +        return NULL;
156 + #endif
157 +
158          return &page_stats;
159 + }
160 +
161 + sg_page_stats *sg_get_page_stats_diff(){
162 +        static sg_page_stats page_stats_diff;
163 + #ifndef WIN32
164 +        sg_page_stats *page_ptr;
165 +
166 +        if(page_stats_uninit){
167 +                page_ptr=sg_get_page_stats();
168 +                if(page_ptr==NULL){
169 +                        return NULL;
170 +                }
171 +                page_stats_uninit=0;
172 +                return page_ptr;
173 +        }
174 +
175 +        page_stats_diff.pages_pagein=page_stats.pages_pagein;
176 +        page_stats_diff.pages_pageout=page_stats.pages_pageout;
177 +        page_stats_diff.systime=page_stats.systime;
178 +
179 +        page_ptr=sg_get_page_stats();
180 +        if(page_ptr==NULL){
181 +                return NULL;
182 +        }
183 +
184 +        page_stats_diff.pages_pagein=page_stats.pages_pagein-page_stats_diff.pages_pagein;
185 +        page_stats_diff.pages_pageout=page_stats.pages_pageout-page_stats_diff.pages_pageout;
186 +        page_stats_diff.systime=page_stats.systime-page_stats_diff.systime;
187 +
188 + #else /* WIN32 */
189 +        if(read_counter_large(SG_WIN32_PAGEIN, &page_stats_diff.pages_pagein)) {
190 +                sg_set_error(SG_ERROR_PDHREAD, PDH_PAGEIN);
191 +                return NULL;
192 +        }
193 +        if(read_counter_large(SG_WIN32_PAGEOUT, &page_stats_diff.pages_pageout)) {
194 +                sg_set_error(SG_ERROR_PDHREAD, PDH_PAGEIN);
195 +                return NULL;
196 +        }
197 +        page_stats_diff.systime = 0;
198 + #endif /* WIN32 */
199 +
200 +        return &page_stats_diff;
201   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines