ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/page_stats.c
Revision: 1.24
Committed: Sun Oct 3 18:35:58 2010 UTC (13 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.23: +32 -1 lines
Log Message:
Add support for AIX 5.x - 9.x.

Many thanks to Jens Rehsack <rehsack@googlemail.com> for providing the
patch for this work. Thanks!

File Contents

# Content
1 /*
2 * i-scream libstatgrab
3 * http://www.i-scream.org
4 * Copyright (C) 2000-2004 i-scream
5 *
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 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 GNU
14 * Lesser General Public License for more details.
15 *
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: page_stats.c,v 1.23 2005/09/24 13:29:22 tdb Exp $
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
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 AIX
49 #include <libperfstat.h>
50 #endif
51 #ifdef HPUX
52 #include <sys/pstat.h>
53 #endif
54 #ifdef WIN32
55 #include "win32.h"
56 #endif
57
58 static sg_page_stats page_stats;
59 #ifndef WIN32
60 static int page_stats_uninit=1;
61 #endif
62
63 sg_page_stats *sg_get_page_stats(){
64 #ifdef SOLARIS
65 kstat_ctl_t *kc;
66 kstat_t *ksp;
67 cpu_stat_t cs;
68 #endif
69 #if defined(LINUX) || defined(CYGWIN)
70 FILE *f;
71 char *line_ptr;
72 #endif
73 #if defined(FREEBSD) || defined(DFBSD)
74 size_t size;
75 #endif
76 #if defined(NETBSD) || defined(OPENBSD)
77 struct uvmexp *uvm;
78 #endif
79 #ifdef AIX
80 perfstat_memory_total_t mem;
81 #endif
82 #ifdef HPUX
83 struct pst_vminfo vminfo;
84 #endif
85
86 page_stats.systime = time(NULL);
87 page_stats.pages_pagein=0;
88 page_stats.pages_pageout=0;
89
90 #ifdef SOLARIS
91 if ((kc = kstat_open()) == NULL) {
92 sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
93 return NULL;
94 }
95 for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
96 if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
97 if (kstat_read(kc, ksp, &cs) == -1) {
98 continue;
99 }
100
101 page_stats.pages_pagein+=(long long)cs.cpu_vminfo.pgpgin;
102 page_stats.pages_pageout+=(long long)cs.cpu_vminfo.pgpgout;
103 }
104
105 kstat_close(kc);
106 #endif
107 #if defined(LINUX) || defined(CYGWIN)
108 if ((f = fopen("/proc/vmstat", "r")) != NULL) {
109 while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
110 long long value;
111
112 if (sscanf(line_ptr, "%*s %lld", &value) != 1) {
113 continue;
114 }
115
116 if (strncmp(line_ptr, "pgpgin ", 7) == 0) {
117 page_stats.pages_pagein = value;
118 } else if (strncmp(line_ptr, "pgpgout ", 8) == 0) {
119 page_stats.pages_pageout = value;
120 }
121 }
122
123 fclose(f);
124 } else if ((f = fopen("/proc/stat", "r")) != NULL) {
125 if ((line_ptr = sg_f_read_line(f, "page")) == NULL) {
126 sg_set_error(SG_ERROR_PARSE, "page");
127 fclose(f);
128 return NULL;
129 }
130
131 if (sscanf(line_ptr, "page %lld %lld", &page_stats.pages_pagein, &page_stats.pages_pageout) != 2) {
132 sg_set_error(SG_ERROR_PARSE, "page");
133 fclose(f);
134 return NULL;
135 }
136
137 fclose(f);
138 } else {
139 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat");
140 return NULL;
141 }
142 #endif
143 #if defined(FREEBSD) || defined(DFBSD)
144 size = sizeof page_stats.pages_pagein;
145 if (sysctlbyname("vm.stats.vm.v_swappgsin", &page_stats.pages_pagein, &size, NULL, 0) < 0){
146 sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
147 "vm.stats.vm.v_swappgsin");
148 return NULL;
149 }
150 size = sizeof page_stats.pages_pageout;
151 if (sysctlbyname("vm.stats.vm.v_swappgsout", &page_stats.pages_pageout, &size, NULL, 0) < 0){
152 sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME,
153 "vm.stats.vm.v_swappgsout");
154 return NULL;
155 }
156 #endif
157 #if defined(NETBSD) || defined(OPENBSD)
158 if ((uvm = sg_get_uvmexp()) == NULL) {
159 return NULL;
160 }
161
162 page_stats.pages_pagein = uvm->pgswapin;
163 page_stats.pages_pageout = uvm->pgswapout;
164 #endif
165 #ifdef AIX
166 /* return code is number of structures returned */
167 if(perfstat_memory_total(NULL, &mem, sizeof(perfstat_memory_total_t), 1) != 1) {
168 sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "perfstat_memory_total");
169 return NULL;
170 }
171
172 page_stats.pages_pagein = mem.pgins;
173 page_stats.pages_pageout = mem.pgouts;
174 #endif
175 #ifdef HPUX
176 if( pstat_getvminfo( &vminfo, sizeof(vminfo), 1, 0 ) == -1 ) {
177 sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "pstat_getswap");
178 return NULL;
179 };
180
181 page_stats.pages_pagein = vminfo.psv_spgin;
182 page_stats.pages_pageout = vminfo.psv_spgout;
183 #endif
184 #ifdef WIN32
185 sg_set_error(SG_ERROR_UNSUPPORTED, "Win32");
186 return NULL;
187 #endif
188
189 return &page_stats;
190 }
191
192 sg_page_stats *sg_get_page_stats_diff(){
193 static sg_page_stats page_stats_diff;
194 #ifndef WIN32
195 sg_page_stats *page_ptr;
196
197 if(page_stats_uninit){
198 page_ptr=sg_get_page_stats();
199 if(page_ptr==NULL){
200 return NULL;
201 }
202 page_stats_uninit=0;
203 return page_ptr;
204 }
205
206 page_stats_diff.pages_pagein=page_stats.pages_pagein;
207 page_stats_diff.pages_pageout=page_stats.pages_pageout;
208 page_stats_diff.systime=page_stats.systime;
209
210 page_ptr=sg_get_page_stats();
211 if(page_ptr==NULL){
212 return NULL;
213 }
214
215 page_stats_diff.pages_pagein=page_stats.pages_pagein-page_stats_diff.pages_pagein;
216 page_stats_diff.pages_pageout=page_stats.pages_pageout-page_stats_diff.pages_pageout;
217 page_stats_diff.systime=page_stats.systime-page_stats_diff.systime;
218
219 #else /* WIN32 */
220 if(read_counter_large(SG_WIN32_PAGEIN, &page_stats_diff.pages_pagein)) {
221 sg_set_error(SG_ERROR_PDHREAD, PDH_PAGEIN);
222 return NULL;
223 }
224 if(read_counter_large(SG_WIN32_PAGEOUT, &page_stats_diff.pages_pageout)) {
225 sg_set_error(SG_ERROR_PDHREAD, PDH_PAGEIN);
226 return NULL;
227 }
228 page_stats_diff.systime = 0;
229 #endif /* WIN32 */
230
231 return &page_stats_diff;
232 }