1 |
tdb |
1.18 |
/* |
2 |
tdb |
1.25 |
* i-scream libstatgrab |
3 |
tdb |
1.7 |
* http://www.i-scream.org |
4 |
tdb |
1.18 |
* Copyright (C) 2000-2004 i-scream |
5 |
pajs |
1.1 |
* |
6 |
tdb |
1.18 |
* 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.18 |
* 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.18 |
* 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.18 |
* 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 |
tdb |
1.19 |
* |
21 |
tdb |
1.34 |
* $Id: memory_stats.c,v 1.33 2006/10/29 23:01:16 tdb Exp $ |
22 |
pajs |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
#ifdef HAVE_CONFIG_H |
25 |
|
|
#include "config.h" |
26 |
|
|
#endif |
27 |
|
|
|
28 |
|
|
#include "statgrab.h" |
29 |
ats |
1.12 |
#include "tools.h" |
30 |
pajs |
1.1 |
#ifdef SOLARIS |
31 |
|
|
#include <unistd.h> |
32 |
|
|
#include <kstat.h> |
33 |
|
|
#endif |
34 |
ats |
1.17 |
#if defined(LINUX) || defined(CYGWIN) |
35 |
pajs |
1.5 |
#include <stdio.h> |
36 |
|
|
#include <string.h> |
37 |
|
|
#endif |
38 |
tdb |
1.23 |
#if defined(FREEBSD) || defined(DFBSD) |
39 |
pajs |
1.6 |
#include <sys/types.h> |
40 |
|
|
#include <sys/sysctl.h> |
41 |
|
|
#include <unistd.h> |
42 |
|
|
#endif |
43 |
tdb |
1.33 |
#if defined(NETBSD) |
44 |
tdb |
1.20 |
#include <sys/param.h> |
45 |
ats |
1.22 |
#include <sys/time.h> |
46 |
tdb |
1.20 |
#include <uvm/uvm.h> |
47 |
|
|
#endif |
48 |
tdb |
1.33 |
#if defined(OPENBSD) |
49 |
|
|
#include <sys/param.h> |
50 |
|
|
#include <sys/types.h> |
51 |
|
|
#include <sys/sysctl.h> |
52 |
|
|
#include <sys/unistd.h> |
53 |
|
|
#endif |
54 |
tdb |
1.30 |
#ifdef HPUX |
55 |
|
|
#include <sys/param.h> |
56 |
|
|
#include <sys/pstat.h> |
57 |
|
|
#include <unistd.h> |
58 |
|
|
#endif |
59 |
tdb |
1.32 |
#ifdef WIN32 |
60 |
|
|
#include <windows.h> |
61 |
|
|
#include "win32.h" |
62 |
|
|
#endif |
63 |
pajs |
1.1 |
|
64 |
ats |
1.24 |
sg_mem_stats *sg_get_mem_stats(){ |
65 |
pajs |
1.1 |
|
66 |
ats |
1.24 |
static sg_mem_stats mem_stat; |
67 |
pajs |
1.1 |
|
68 |
tdb |
1.30 |
#ifdef HPUX |
69 |
ats |
1.31 |
struct pst_static *pstat_static; |
70 |
tdb |
1.30 |
struct pst_dynamic pstat_dynamic; |
71 |
|
|
long long pagesize; |
72 |
|
|
#endif |
73 |
pajs |
1.1 |
#ifdef SOLARIS |
74 |
|
|
kstat_ctl_t *kc; |
75 |
|
|
kstat_t *ksp; |
76 |
|
|
kstat_named_t *kn; |
77 |
|
|
long totalmem; |
78 |
|
|
int pagesize; |
79 |
|
|
#endif |
80 |
ats |
1.17 |
#if defined(LINUX) || defined(CYGWIN) |
81 |
pajs |
1.5 |
char *line_ptr; |
82 |
ats |
1.15 |
unsigned long long value; |
83 |
pajs |
1.5 |
FILE *f; |
84 |
|
|
#endif |
85 |
tdb |
1.23 |
#if defined(FREEBSD) || defined(DFBSD) |
86 |
ats |
1.12 |
int mib[2]; |
87 |
ats |
1.14 |
u_long physmem; |
88 |
|
|
size_t size; |
89 |
tdb |
1.8 |
u_int free_count; |
90 |
|
|
u_int cache_count; |
91 |
|
|
u_int inactive_count; |
92 |
ats |
1.12 |
int pagesize; |
93 |
|
|
#endif |
94 |
tdb |
1.33 |
#if defined(NETBSD) |
95 |
ats |
1.12 |
struct uvmexp *uvm; |
96 |
|
|
#endif |
97 |
tdb |
1.33 |
#if defined(OPENBSD) |
98 |
|
|
int mib[2]; |
99 |
|
|
struct vmtotal vmtotal; |
100 |
|
|
size_t size; |
101 |
|
|
static int pagesize, pageshift; |
102 |
|
|
#endif |
103 |
tdb |
1.32 |
#ifdef WIN32 |
104 |
|
|
MEMORYSTATUSEX memstats; |
105 |
|
|
#endif |
106 |
pajs |
1.1 |
|
107 |
tdb |
1.30 |
#ifdef HPUX |
108 |
|
|
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
109 |
|
|
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE"); |
110 |
|
|
return NULL; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
if (pstat_getdynamic(&pstat_dynamic, sizeof(pstat_dynamic), 1, 0) == -1) { |
114 |
|
|
sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic"); |
115 |
|
|
return NULL; |
116 |
|
|
} |
117 |
ats |
1.31 |
pstat_static = sg_get_pstat_static(); |
118 |
|
|
if (pstat_static == NULL) { |
119 |
tdb |
1.30 |
return NULL; |
120 |
|
|
} |
121 |
|
|
|
122 |
ats |
1.31 |
/* FIXME Does this include swap? */ |
123 |
|
|
mem_stat.total = ((long long) pstat_static->physical_memory) * pagesize; |
124 |
tdb |
1.30 |
mem_stat.free = ((long long) pstat_dynamic.psd_free) * pagesize; |
125 |
|
|
mem_stat.used = mem_stat.total - mem_stat.free; |
126 |
|
|
#endif |
127 |
pajs |
1.1 |
#ifdef SOLARIS |
128 |
|
|
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
129 |
ats |
1.28 |
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE"); |
130 |
pajs |
1.1 |
return NULL; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){ |
134 |
ats |
1.28 |
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PHYS_PAGES"); |
135 |
pajs |
1.1 |
return NULL; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
if ((kc = kstat_open()) == NULL) { |
139 |
tdb |
1.27 |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
140 |
pajs |
1.1 |
return NULL; |
141 |
|
|
} |
142 |
|
|
if((ksp=kstat_lookup(kc, "unix", 0, "system_pages")) == NULL){ |
143 |
tdb |
1.27 |
sg_set_error(SG_ERROR_KSTAT_LOOKUP, "unix,0,system_pages"); |
144 |
pajs |
1.1 |
return NULL; |
145 |
|
|
} |
146 |
|
|
if (kstat_read(kc, ksp, 0) == -1) { |
147 |
tdb |
1.27 |
sg_set_error(SG_ERROR_KSTAT_READ, NULL); |
148 |
pajs |
1.1 |
return NULL; |
149 |
|
|
} |
150 |
|
|
if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){ |
151 |
tdb |
1.27 |
sg_set_error(SG_ERROR_KSTAT_DATA_LOOKUP, "freemem"); |
152 |
pajs |
1.1 |
return NULL; |
153 |
|
|
} |
154 |
tdb |
1.26 |
kstat_close(kc); |
155 |
pajs |
1.2 |
|
156 |
pajs |
1.1 |
mem_stat.total = (long long)totalmem * (long long)pagesize; |
157 |
|
|
mem_stat.free = ((long long)kn->value.ul) * (long long)pagesize; |
158 |
|
|
mem_stat.used = mem_stat.total - mem_stat.free; |
159 |
pajs |
1.5 |
#endif |
160 |
|
|
|
161 |
ats |
1.17 |
#if defined(LINUX) || defined(CYGWIN) |
162 |
ats |
1.16 |
if ((f = fopen("/proc/meminfo", "r")) == NULL) { |
163 |
ats |
1.28 |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo"); |
164 |
pajs |
1.5 |
return NULL; |
165 |
|
|
} |
166 |
|
|
|
167 |
ats |
1.24 |
while ((line_ptr = sg_f_read_line(f, "")) != NULL) { |
168 |
ats |
1.16 |
if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) { |
169 |
ats |
1.15 |
continue; |
170 |
|
|
} |
171 |
|
|
value *= 1024; |
172 |
|
|
|
173 |
|
|
if (strncmp(line_ptr, "MemTotal:", 9) == 0) { |
174 |
|
|
mem_stat.total = value; |
175 |
|
|
} else if (strncmp(line_ptr, "MemFree:", 8) == 0) { |
176 |
|
|
mem_stat.free = value; |
177 |
|
|
} else if (strncmp(line_ptr, "Cached:", 7) == 0) { |
178 |
|
|
mem_stat.cache = value; |
179 |
|
|
} |
180 |
pajs |
1.5 |
} |
181 |
|
|
|
182 |
|
|
fclose(f); |
183 |
ats |
1.15 |
mem_stat.used = mem_stat.total - mem_stat.free; |
184 |
pajs |
1.6 |
#endif |
185 |
|
|
|
186 |
tdb |
1.23 |
#if defined(FREEBSD) || defined(DFBSD) |
187 |
tdb |
1.8 |
/* Returns bytes */ |
188 |
ats |
1.12 |
mib[0] = CTL_HW; |
189 |
|
|
mib[1] = HW_PHYSMEM; |
190 |
ats |
1.11 |
size = sizeof physmem; |
191 |
ats |
1.12 |
if (sysctl(mib, 2, &physmem, &size, NULL, 0) < 0) { |
192 |
ats |
1.28 |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_PHYSMEM"); |
193 |
pajs |
1.6 |
return NULL; |
194 |
ats |
1.12 |
} |
195 |
|
|
mem_stat.total = physmem; |
196 |
pajs |
1.6 |
|
197 |
|
|
/*returns pages*/ |
198 |
ats |
1.11 |
size = sizeof free_count; |
199 |
tdb |
1.27 |
if (sysctlbyname("vm.stats.vm.v_free_count", &free_count, &size, NULL, 0) < 0){ |
200 |
ats |
1.28 |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, |
201 |
|
|
"vm.stats.vm.v_free_count"); |
202 |
pajs |
1.6 |
return NULL; |
203 |
tdb |
1.27 |
} |
204 |
pajs |
1.6 |
|
205 |
ats |
1.11 |
size = sizeof inactive_count; |
206 |
tdb |
1.27 |
if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count , &size, NULL, 0) < 0){ |
207 |
ats |
1.28 |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, |
208 |
|
|
"vm.stats.vm.v_inactive_count"); |
209 |
pajs |
1.6 |
return NULL; |
210 |
tdb |
1.27 |
} |
211 |
pajs |
1.6 |
|
212 |
ats |
1.11 |
size = sizeof cache_count; |
213 |
tdb |
1.27 |
if (sysctlbyname("vm.stats.vm.v_cache_count", &cache_count, &size, NULL, 0) < 0){ |
214 |
ats |
1.28 |
sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, |
215 |
|
|
"vm.stats.vm.v_cache_count"); |
216 |
pajs |
1.6 |
return NULL; |
217 |
tdb |
1.27 |
} |
218 |
pajs |
1.6 |
|
219 |
tdb |
1.8 |
/* Because all the vm.stats returns pages, I need to get the page size. |
220 |
tdb |
1.27 |
* After that I then need to multiple the anything that used vm.stats to |
221 |
tdb |
1.8 |
* get the system statistics by pagesize |
222 |
pajs |
1.6 |
*/ |
223 |
ats |
1.29 |
pagesize = getpagesize(); |
224 |
tdb |
1.8 |
mem_stat.cache=cache_count*pagesize; |
225 |
|
|
|
226 |
|
|
/* Of couse nothing is ever that simple :) And I have inactive pages to |
227 |
|
|
* deal with too. So I'm going to add them to free memory :) |
228 |
pajs |
1.6 |
*/ |
229 |
tdb |
1.8 |
mem_stat.free=(free_count*pagesize)+(inactive_count*pagesize); |
230 |
|
|
mem_stat.used=physmem-mem_stat.free; |
231 |
ats |
1.12 |
#endif |
232 |
ats |
1.14 |
|
233 |
tdb |
1.33 |
#if defined(NETBSD) |
234 |
ats |
1.24 |
if ((uvm = sg_get_uvmexp()) == NULL) { |
235 |
ats |
1.12 |
return NULL; |
236 |
|
|
} |
237 |
tdb |
1.20 |
|
238 |
ats |
1.14 |
mem_stat.total = uvm->pagesize * uvm->npages; |
239 |
ats |
1.12 |
mem_stat.cache = uvm->pagesize * (uvm->filepages + uvm->execpages); |
240 |
ats |
1.14 |
mem_stat.free = uvm->pagesize * (uvm->free + uvm->inactive); |
241 |
|
|
mem_stat.used = mem_stat.total - mem_stat.free; |
242 |
pajs |
1.1 |
#endif |
243 |
|
|
|
244 |
tdb |
1.33 |
#if defined(OPENBSD) |
245 |
|
|
/* get the page size with "getpagesize" and calculate pageshift |
246 |
|
|
* from it |
247 |
|
|
*/ |
248 |
|
|
pagesize = getpagesize(); |
249 |
|
|
pageshift = 0; |
250 |
|
|
while (pagesize > 1) { |
251 |
|
|
pageshift++; |
252 |
|
|
pagesize >>= 1; |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
/* we only need the amount of log(2)1024 for our conversion */ |
256 |
|
|
pageshift -= 10; /* Log base 2 of 1024 is 10 (2^10 == 1024) */ |
257 |
|
|
#define pagetok(size) ((size) << pageshift) |
258 |
|
|
mib[0] = CTL_VM; |
259 |
|
|
mib[1] = VM_METER; |
260 |
|
|
size = sizeof(vmtotal); |
261 |
tdb |
1.34 |
if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) { |
262 |
tdb |
1.33 |
sg_set_error_with_errno(SG_ERROR_SYSCTL, |
263 |
|
|
"CTL_VM.VM_METER"); |
264 |
|
|
return NULL; |
265 |
|
|
} |
266 |
|
|
/* convert memory stats to Kbytes */ |
267 |
|
|
mem_stat.used = pagetok(vmtotal.t_rm); /* total real mem in use */ |
268 |
|
|
mem_stat.cache = 0; /* ? */ |
269 |
|
|
mem_stat.free = pagetok(vmtotal.t_free); /* free memory pages */ |
270 |
|
|
mem_stat.total = (mem_stat.used + mem_stat.free); |
271 |
|
|
#endif |
272 |
|
|
|
273 |
tdb |
1.32 |
#ifdef WIN32 |
274 |
|
|
memstats.dwLength = sizeof(memstats); |
275 |
|
|
if (!GlobalMemoryStatusEx(&memstats)) { |
276 |
|
|
sg_set_error_with_errno(SG_ERROR_MEMSTATUS, NULL); |
277 |
|
|
return NULL; |
278 |
|
|
} |
279 |
|
|
mem_stat.free = memstats.ullAvailPhys; |
280 |
|
|
mem_stat.total = memstats.ullTotalPhys; |
281 |
|
|
mem_stat.used = mem_stat.total - mem_stat.free; |
282 |
|
|
if(read_counter_large(SG_WIN32_MEM_CACHE, &mem_stat.cache)) { |
283 |
|
|
mem_stat.cache = 0; |
284 |
|
|
} |
285 |
|
|
#endif |
286 |
pajs |
1.1 |
return &mem_stat; |
287 |
|
|
} |