ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/page_stats.c
Revision: 1.13
Committed: Fri Jan 16 15:54:54 2004 UTC (20 years, 4 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.12: +13 -12 lines
Log Message:
Alter the licensing of libstatgrab. The library part is now under the
LGPL, whilst the tools/examples are under the GPL. Both licenses are
included in the distribution (and are both now in CVS). Also made a
minor alteration to the webpage where it said everything was licensed
under the GPL.

File Contents

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