ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/page_stats.c
Revision: 1.9
Committed: Sun Oct 19 02:03:02 2003 UTC (20 years, 7 months ago) by ats
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_7
Changes since 1.8: +12 -5 lines
Log Message:
Initial support for NetBSD. This adds NetBSD support for everything
except diskio stats (since they're even more disturbingly complex to get
at on NetBSD than the three OSs we already support). Tested against
NetBSD 1.6 on i386.

File Contents

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