ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/page_stats.c
Revision: 1.7
Committed: Sat Oct 18 22:15:35 2003 UTC (20 years, 7 months ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.6: +2 -8 lines
Log Message:
You only need to call sysctl{,byname}() twice if you're actually going
  to do something with the size the first call returns.
The last argument to sysctl{,byname}() is a size_t, not a pointer, so it
  should be 0, not NULL.

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 pajs 1.4 #include <time.h>
27 pajs 1.1 #ifdef SOLARIS
28     #include <kstat.h>
29     #include <sys/sysinfo.h>
30 pajs 1.2 #include <string.h>
31 pajs 1.1 #endif
32 pajs 1.4 #ifdef LINUX
33     #include <stdio.h>
34     #include "tools.h"
35     #endif
36 pajs 1.5 #ifdef FREEBSD
37     #include <sys/types.h>
38     #include <sys/sysctl.h>
39     #endif
40 pajs 1.3
41     static page_stat_t page_stats;
42     static int page_stats_uninit=1;
43    
44 pajs 1.1 page_stat_t *get_page_stats(){
45 pajs 1.4 #ifdef SOLARIS
46 pajs 1.1 kstat_ctl_t *kc;
47     kstat_t *ksp;
48     cpu_stat_t cs;
49 pajs 1.4 #endif
50     #ifdef LINUX
51     FILE *f;
52     char *line_ptr;
53     #endif
54 pajs 1.5 #ifdef FREEBSD
55     size_t size;
56     #endif
57 pajs 1.2
58     page_stats.pages_pagein=0;
59     page_stats.pages_pageout=0;
60 pajs 1.1
61 pajs 1.4 #ifdef SOLARIS
62 pajs 1.1 if ((kc = kstat_open()) == NULL) {
63     return NULL;
64     }
65     for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
66     if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
67     if (kstat_read(kc, ksp, &cs) == -1) {
68     continue;
69     }
70    
71 pajs 1.2 page_stats.pages_pagein+=(long long)cs.cpu_vminfo.pgpgin;
72     page_stats.pages_pageout+=(long long)cs.cpu_vminfo.pgpgout;
73     }
74    
75     page_stats.systime=time(NULL);
76    
77     kstat_close(kc);
78 pajs 1.4 #endif
79     #ifdef LINUX
80     if((f=fopen("/proc/stat", "r"))==NULL){
81     return NULL;
82     }
83     if((line_ptr=f_read_line(f, "page"))==NULL){
84     fclose(f);
85     return NULL;
86     }
87     if((sscanf(line_ptr, "page %lld %lld", &page_stats.pages_pagein, &page_stats.pages_pageout))!=2){
88     return NULL;
89     }
90     page_stats.systime=time(NULL);
91     fclose(f);
92 pajs 1.5
93     #endif
94     #ifdef FREEBSD
95 ats 1.7 if (sysctlbyname("vm.stats.vm.v_swappgsin", &page_stats.pages_pagein, &size, NULL, 0) < 0){
96 pajs 1.5 return NULL;
97     }
98 ats 1.7 if (sysctlbyname("vm.stats.vm.v_swappgsout", &page_stats.pages_pageout, &size, NULL, 0) < 0){
99 pajs 1.5 return NULL;
100     }
101 pajs 1.4
102     #endif
103 pajs 1.1
104 pajs 1.2 return &page_stats;
105 pajs 1.3 }
106    
107     page_stat_t *get_page_stats_diff(){
108     page_stat_t *page_ptr;
109     static page_stat_t page_stats_diff;
110    
111     if(page_stats_uninit){
112     page_ptr=get_page_stats();
113     if(page_ptr==NULL){
114     return NULL;
115     }
116     page_stats_uninit=0;
117     return page_ptr;
118     }
119    
120     page_stats_diff.pages_pagein=page_stats.pages_pagein;
121     page_stats_diff.pages_pageout=page_stats.pages_pageout;
122     page_stats_diff.systime=page_stats.systime;
123    
124     page_ptr=get_page_stats();
125     if(page_ptr==NULL){
126     return NULL;
127     }
128    
129     page_stats_diff.pages_pagein=page_stats.pages_pagein-page_stats_diff.pages_pagein;
130     page_stats_diff.pages_pageout=page_stats.pages_pageout-page_stats_diff.pages_pageout;
131     page_stats_diff.systime=page_stats.systime-page_stats_diff.systime;
132    
133     return &page_stats_diff;
134 pajs 1.1 }