ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/page_stats.c
Revision: 1.23
Committed: Sat Sep 24 13:29:22 2005 UTC (18 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_17, LIBSTATGRAB_0_16, LIBSTATGRAB_0_15, LIBSTATGRAB_0_14, LIBSTATGRAB_0_13
Changes since 1.22: +25 -3 lines
Log Message:
Add WIN32 support via MINGW. We'll need to add stuff to the README file
about what this requires to build.

All the hard work done by: skel

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.22 2004/07/18 21:30:11 ats 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 WIN32
49 #include "win32.h"
50 #endif
51
52 static sg_page_stats page_stats;
53 #ifndef WIN32
54 static int page_stats_uninit=1;
55 #endif
56
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 page_stats.systime = time(NULL);
75 page_stats.pages_pagein=0;
76 page_stats.pages_pageout=0;
77
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
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 }