Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | #include <config.h> | - |
22 | | - |
23 | #include "physmem.h" | - |
24 | | - |
25 | #include <unistd.h> | - |
26 | | - |
27 | #if HAVE_SYS_PSTAT_H | - |
28 | # include <sys/pstat.h> | - |
29 | #endif | - |
30 | | - |
31 | #if HAVE_SYS_SYSMP_H | - |
32 | # include <sys/sysmp.h> | - |
33 | #endif | - |
34 | | - |
35 | #if HAVE_SYS_SYSINFO_H | - |
36 | # include <sys/sysinfo.h> | - |
37 | #endif | - |
38 | | - |
39 | #if HAVE_MACHINE_HAL_SYSINFO_H | - |
40 | # include <machine/hal_sysinfo.h> | - |
41 | #endif | - |
42 | | - |
43 | #if HAVE_SYS_TABLE_H | - |
44 | # include <sys/table.h> | - |
45 | #endif | - |
46 | | - |
47 | #include <sys/types.h> | - |
48 | | - |
49 | #if HAVE_SYS_PARAM_H | - |
50 | # include <sys/param.h> | - |
51 | #endif | - |
52 | | - |
53 | #if HAVE_SYS_SYSCTL_H | - |
54 | # include <sys/sysctl.h> | - |
55 | #endif | - |
56 | | - |
57 | #if HAVE_SYS_SYSTEMCFG_H | - |
58 | # include <sys/systemcfg.h> | - |
59 | #endif | - |
60 | | - |
61 | #ifdef _WIN32 | - |
62 | # define WIN32_LEAN_AND_MEAN | - |
63 | # include <windows.h> | - |
64 | | - |
65 | | - |
66 | typedef struct | - |
67 | { | - |
68 | DWORD dwLength; | - |
69 | DWORD dwMemoryLoad; | - |
70 | DWORDLONG ullTotalPhys; | - |
71 | DWORDLONG ullAvailPhys; | - |
72 | DWORDLONG ullTotalPageFile; | - |
73 | DWORDLONG ullAvailPageFile; | - |
74 | DWORDLONG ullTotalVirtual; | - |
75 | DWORDLONG ullAvailVirtual; | - |
76 | DWORDLONG ullAvailExtendedVirtual; | - |
77 | } lMEMORYSTATUSEX; | - |
78 | typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*); | - |
79 | #endif | - |
80 | | - |
81 | #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) | - |
82 | | - |
83 | | - |
84 | double | - |
85 | physmem_total (void) | - |
86 | { | - |
87 | #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE | - |
88 | { | - |
89 | double pages = sysconf (_SC_PHYS_PAGES); | - |
90 | double pagesize = sysconf (_SC_PAGESIZE); | - |
91 | if (0 <= pages && 0 <= pagesize)TRUE | evaluated 685 times by 1 test | FALSE | never evaluated |
TRUE | evaluated 685 times by 1 test | FALSE | never evaluated |
| 0-685 |
92 | return pages * pagesize;executed 685 times by 1 test: return pages * pagesize; | 685 |
93 | } | - |
94 | #endif | - |
95 | | - |
96 | #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT | - |
97 | { | - |
98 | struct sysinfo si; | - |
99 | if (sysinfo(&si) == 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
100 | return (double) si.totalram * si.mem_unit; never executed: return (double) si.totalram * si.mem_unit; | 0 |
101 | } | - |
102 | #endif | - |
103 | | - |
104 | #if HAVE_PSTAT_GETSTATIC | - |
105 | { | - |
106 | struct pst_static pss; | - |
107 | if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)) | - |
108 | { | - |
109 | double pages = pss.physical_memory; | - |
110 | double pagesize = pss.page_size; | - |
111 | if (0 <= pages && 0 <= pagesize) | - |
112 | return pages * pagesize; | - |
113 | } | - |
114 | } | - |
115 | #endif | - |
116 | | - |
117 | #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE | - |
118 | { | - |
119 | struct rminfo realmem; | - |
120 | if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) | - |
121 | { | - |
122 | double pagesize = sysconf (_SC_PAGESIZE); | - |
123 | double pages = realmem.physmem; | - |
124 | if (0 <= pages && 0 <= pagesize) | - |
125 | return pages * pagesize; | - |
126 | } | - |
127 | } | - |
128 | #endif | - |
129 | | - |
130 | #if HAVE_GETSYSINFO && defined GSI_PHYSMEM | - |
131 | { | - |
132 | int physmem; | - |
133 | | - |
134 | if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem), | - |
135 | NULL, NULL, NULL) == 1) | - |
136 | { | - |
137 | double kbytes = physmem; | - |
138 | | - |
139 | if (0 <= kbytes) | - |
140 | return kbytes * 1024.0; | - |
141 | } | - |
142 | } | - |
143 | #endif | - |
144 | | - |
145 | #if HAVE_SYSCTL && defined HW_PHYSMEM | - |
146 | { | - |
147 | unsigned int physmem; | - |
148 | size_t len = sizeof physmem; | - |
149 | static int mib[2] = { CTL_HW, HW_PHYSMEM }; | - |
150 | | - |
151 | if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0 | - |
152 | && len == sizeof (physmem)) | - |
153 | return (double) physmem; | - |
154 | } | - |
155 | #endif | - |
156 | | - |
157 | #if HAVE__SYSTEM_CONFIGURATION | - |
158 | | - |
159 | return _system_configuration.physmem; | - |
160 | #endif | - |
161 | | - |
162 | #if defined _WIN32 | - |
163 | { | - |
164 | PFN_MS_EX pfnex; | - |
165 | HMODULE h = GetModuleHandle ("kernel32.dll"); | - |
166 | | - |
167 | if (!h) | - |
168 | return 0.0; | - |
169 | | - |
170 | | - |
171 | if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx"))) | - |
172 | { | - |
173 | lMEMORYSTATUSEX lms_ex; | - |
174 | lms_ex.dwLength = sizeof lms_ex; | - |
175 | if (!pfnex (&lms_ex)) | - |
176 | return 0.0; | - |
177 | return (double) lms_ex.ullTotalPhys; | - |
178 | } | - |
179 | | - |
180 | | - |
181 | | - |
182 | else | - |
183 | { | - |
184 | MEMORYSTATUS ms; | - |
185 | GlobalMemoryStatus (&ms); | - |
186 | return (double) ms.dwTotalPhys; | - |
187 | } | - |
188 | } | - |
189 | #endif | - |
190 | | - |
191 | | - |
192 | return 64 * 1024 * 1024; never executed: return 64 * 1024 * 1024; | 0 |
193 | } | - |
194 | | - |
195 | | - |
196 | double | - |
197 | physmem_available (void) | - |
198 | { | - |
199 | #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE | - |
200 | { | - |
201 | double pages = sysconf (_SC_AVPHYS_PAGES); | - |
202 | double pagesize = sysconf (_SC_PAGESIZE); | - |
203 | if (0 <= pages && 0 <= pagesize)TRUE | evaluated 696 times by 2 tests | FALSE | never evaluated |
TRUE | evaluated 696 times by 2 tests | FALSE | never evaluated |
| 0-696 |
204 | return pages * pagesize;executed 696 times by 2 tests: return pages * pagesize; | 696 |
205 | } | - |
206 | #endif | - |
207 | | - |
208 | #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT | - |
209 | { | - |
210 | struct sysinfo si; | - |
211 | if (sysinfo(&si) == 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
212 | return ((double) si.freeram + si.bufferram) * si.mem_unit; never executed: return ((double) si.freeram + si.bufferram) * si.mem_unit; | 0 |
213 | } | - |
214 | #endif | - |
215 | | - |
216 | #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC | - |
217 | { | - |
218 | struct pst_static pss; | - |
219 | struct pst_dynamic psd; | - |
220 | if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0) | - |
221 | && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0)) | - |
222 | { | - |
223 | double pages = psd.psd_free; | - |
224 | double pagesize = pss.page_size; | - |
225 | if (0 <= pages && 0 <= pagesize) | - |
226 | return pages * pagesize; | - |
227 | } | - |
228 | } | - |
229 | #endif | - |
230 | | - |
231 | #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE | - |
232 | { | - |
233 | struct rminfo realmem; | - |
234 | if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) | - |
235 | { | - |
236 | double pagesize = sysconf (_SC_PAGESIZE); | - |
237 | double pages = realmem.availrmem; | - |
238 | if (0 <= pages && 0 <= pagesize) | - |
239 | return pages * pagesize; | - |
240 | } | - |
241 | } | - |
242 | #endif | - |
243 | | - |
244 | #if HAVE_TABLE && defined TBL_VMSTATS | - |
245 | { | - |
246 | struct tbl_vmstats vmstats; | - |
247 | | - |
248 | if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1) | - |
249 | { | - |
250 | double pages = vmstats.free_count; | - |
251 | double pagesize = vmstats.pagesize; | - |
252 | | - |
253 | if (0 <= pages && 0 <= pagesize) | - |
254 | return pages * pagesize; | - |
255 | } | - |
256 | } | - |
257 | #endif | - |
258 | | - |
259 | #if HAVE_SYSCTL && defined HW_USERMEM | - |
260 | { | - |
261 | unsigned int usermem; | - |
262 | size_t len = sizeof usermem; | - |
263 | static int mib[2] = { CTL_HW, HW_USERMEM }; | - |
264 | | - |
265 | if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0 | - |
266 | && len == sizeof (usermem)) | - |
267 | return (double) usermem; | - |
268 | } | - |
269 | #endif | - |
270 | | - |
271 | #if defined _WIN32 | - |
272 | { | - |
273 | PFN_MS_EX pfnex; | - |
274 | HMODULE h = GetModuleHandle ("kernel32.dll"); | - |
275 | | - |
276 | if (!h) | - |
277 | return 0.0; | - |
278 | | - |
279 | | - |
280 | if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx"))) | - |
281 | { | - |
282 | lMEMORYSTATUSEX lms_ex; | - |
283 | lms_ex.dwLength = sizeof lms_ex; | - |
284 | if (!pfnex (&lms_ex)) | - |
285 | return 0.0; | - |
286 | return (double) lms_ex.ullAvailPhys; | - |
287 | } | - |
288 | | - |
289 | | - |
290 | | - |
291 | else | - |
292 | { | - |
293 | MEMORYSTATUS ms; | - |
294 | GlobalMemoryStatus (&ms); | - |
295 | return (double) ms.dwAvailPhys; | - |
296 | } | - |
297 | } | - |
298 | #endif | - |
299 | | - |
300 | | - |
301 | return physmem_total () / 4; never executed: return physmem_total () / 4; | 0 |
302 | } | - |
303 | | - |
304 | | - |
305 | #if DEBUG | - |
306 | | - |
307 | # include <stdio.h> | - |
308 | # include <stdlib.h> | - |
309 | | - |
310 | int | - |
311 | main (void) | - |
312 | { | - |
313 | printf ("%12.f %12.f\n", physmem_total (), physmem_available ()); | - |
314 | exit (0); | - |
315 | } | - |
316 | | - |
317 | #endif /* DEBUG */ | - |
318 | | - |
319 | | - |
320 | | - |
321 | | - |
322 | | - |
323 | | - |
| | |