1 |
/* |
2 |
* i-scream central monitoring system |
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 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include "config.h" |
24 |
#endif |
25 |
|
26 |
#include "statgrab.h" |
27 |
#include "tools.h" |
28 |
#include <time.h> |
29 |
#ifdef SOLARIS |
30 |
#include <kstat.h> |
31 |
#include <sys/sysinfo.h> |
32 |
#include <string.h> |
33 |
#endif |
34 |
#if defined(LINUX) || defined(CYGWIN) |
35 |
#include <stdio.h> |
36 |
#include <string.h> |
37 |
#endif |
38 |
#ifdef FREEBSD |
39 |
#include <sys/types.h> |
40 |
#include <sys/sysctl.h> |
41 |
#endif |
42 |
|
43 |
static page_stat_t page_stats; |
44 |
static int page_stats_uninit=1; |
45 |
|
46 |
page_stat_t *get_page_stats(){ |
47 |
#ifdef SOLARIS |
48 |
kstat_ctl_t *kc; |
49 |
kstat_t *ksp; |
50 |
cpu_stat_t cs; |
51 |
#endif |
52 |
#if defined(LINUX) || defined(CYGWIN) |
53 |
FILE *f; |
54 |
char *line_ptr; |
55 |
#endif |
56 |
#ifdef FREEBSD |
57 |
size_t size; |
58 |
#endif |
59 |
#ifdef NETBSD |
60 |
struct uvmexp *uvm; |
61 |
#endif |
62 |
|
63 |
page_stats.systime = time(NULL); |
64 |
page_stats.pages_pagein=0; |
65 |
page_stats.pages_pageout=0; |
66 |
|
67 |
#ifdef SOLARIS |
68 |
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 |
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 |
#endif |
83 |
#if defined(LINUX) || defined(CYGWIN) |
84 |
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 |
fclose(f); |
108 |
return NULL; |
109 |
} |
110 |
|
111 |
fclose(f); |
112 |
} else { |
113 |
return NULL; |
114 |
} |
115 |
#endif |
116 |
#ifdef FREEBSD |
117 |
size = sizeof page_stats.pages_pagein; |
118 |
if (sysctlbyname("vm.stats.vm.v_swappgsin", &page_stats.pages_pagein, &size, NULL, 0) < 0){ |
119 |
return NULL; |
120 |
} |
121 |
size = sizeof page_stats.pages_pageout; |
122 |
if (sysctlbyname("vm.stats.vm.v_swappgsout", &page_stats.pages_pageout, &size, NULL, 0) < 0){ |
123 |
return NULL; |
124 |
} |
125 |
#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 |
#endif |
133 |
|
134 |
return &page_stats; |
135 |
} |
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 |
} |