1 /* Calculate the size of physical memory.
3 Copyright (C) 2000-2001, 2003, 2005-2006, 2009-2017 Free Software
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert. */
28 # include <sys/pstat.h>
32 # include <sys/sysmp.h>
35 #if HAVE_SYS_SYSINFO_H
36 # include <sys/sysinfo.h>
39 #if HAVE_MACHINE_HAL_SYSINFO_H
40 # include <machine/hal_sysinfo.h>
44 # include <sys/table.h>
47 #include <sys/types.h>
50 # include <sys/param.h>
54 # include <sys/sysctl.h>
57 #if HAVE_SYS_SYSTEMCFG_H
58 # include <sys/systemcfg.h>
62 # define WIN32_LEAN_AND_MEAN
64 /* MEMORYSTATUSEX is missing from older windows headers, so define
65 a local replacement. */
70 DWORDLONG ullTotalPhys
;
71 DWORDLONG ullAvailPhys
;
72 DWORDLONG ullTotalPageFile
;
73 DWORDLONG ullAvailPageFile
;
74 DWORDLONG ullTotalVirtual
;
75 DWORDLONG ullAvailVirtual
;
76 DWORDLONG ullAvailExtendedVirtual
;
78 typedef WINBOOL (WINAPI
*PFN_MS_EX
) (lMEMORYSTATUSEX
*);
81 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
83 /* Return the total amount of physical memory. */
87 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
88 { /* This works on linux-gnu, solaris2 and cygwin. */
89 double pages
= sysconf (_SC_PHYS_PAGES
);
90 double pagesize
= sysconf (_SC_PAGESIZE
);
91 if (0 <= pages
&& 0 <= pagesize
)
92 return pages
* pagesize
;
96 #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
97 { /* This works on linux. */
99 if (sysinfo(&si
) == 0)
100 return (double) si
.totalram
* si
.mem_unit
;
104 #if HAVE_PSTAT_GETSTATIC
105 { /* This works on hpux11. */
106 struct pst_static pss
;
107 if (0 <= pstat_getstatic (&pss
, sizeof pss
, 1, 0))
109 double pages
= pss
.physical_memory
;
110 double pagesize
= pss
.page_size
;
111 if (0 <= pages
&& 0 <= pagesize
)
112 return pages
* pagesize
;
117 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
118 { /* This works on irix6. */
119 struct rminfo realmem
;
120 if (sysmp (MP_SAGET
, MPSA_RMINFO
, &realmem
, sizeof realmem
) == 0)
122 double pagesize
= sysconf (_SC_PAGESIZE
);
123 double pages
= realmem
.physmem
;
124 if (0 <= pages
&& 0 <= pagesize
)
125 return pages
* pagesize
;
130 #if HAVE_GETSYSINFO && defined GSI_PHYSMEM
131 { /* This works on Tru64 UNIX V4/5. */
134 if (getsysinfo (GSI_PHYSMEM
, (caddr_t
) &physmem
, sizeof (physmem
),
135 NULL
, NULL
, NULL
) == 1)
137 double kbytes
= physmem
;
140 return kbytes
* 1024.0;
145 #if HAVE_SYSCTL && defined HW_PHYSMEM
146 { /* This works on *bsd and darwin. */
147 unsigned int physmem
;
148 size_t len
= sizeof physmem
;
149 static int mib
[2] = { CTL_HW
, HW_PHYSMEM
};
151 if (sysctl (mib
, ARRAY_SIZE (mib
), &physmem
, &len
, NULL
, 0) == 0
152 && len
== sizeof (physmem
))
153 return (double) physmem
;
157 #if HAVE__SYSTEM_CONFIGURATION
158 /* This works on AIX. */
159 return _system_configuration
.physmem
;
163 { /* this works on windows */
165 HMODULE h
= GetModuleHandle ("kernel32.dll");
170 /* Use GlobalMemoryStatusEx if available. */
171 if ((pfnex
= (PFN_MS_EX
) GetProcAddress (h
, "GlobalMemoryStatusEx")))
173 lMEMORYSTATUSEX lms_ex
;
174 lms_ex
.dwLength
= sizeof lms_ex
;
175 if (!pfnex (&lms_ex
))
177 return (double) lms_ex
.ullTotalPhys
;
180 /* Fall back to GlobalMemoryStatus which is always available.
181 but returns wrong results for physical memory > 4GB. */
185 GlobalMemoryStatus (&ms
);
186 return (double) ms
.dwTotalPhys
;
191 /* Guess 64 MB. It's probably an older host, so guess small. */
192 return 64 * 1024 * 1024;
195 /* Return the amount of physical memory available. */
197 physmem_available (void)
199 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
200 { /* This works on linux-gnu, solaris2 and cygwin. */
201 double pages
= sysconf (_SC_AVPHYS_PAGES
);
202 double pagesize
= sysconf (_SC_PAGESIZE
);
203 if (0 <= pages
&& 0 <= pagesize
)
204 return pages
* pagesize
;
208 #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
209 { /* This works on linux. */
211 if (sysinfo(&si
) == 0)
212 return ((double) si
.freeram
+ si
.bufferram
) * si
.mem_unit
;
216 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
217 { /* This works on hpux11. */
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))
223 double pages
= psd
.psd_free
;
224 double pagesize
= pss
.page_size
;
225 if (0 <= pages
&& 0 <= pagesize
)
226 return pages
* pagesize
;
231 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
232 { /* This works on irix6. */
233 struct rminfo realmem
;
234 if (sysmp (MP_SAGET
, MPSA_RMINFO
, &realmem
, sizeof realmem
) == 0)
236 double pagesize
= sysconf (_SC_PAGESIZE
);
237 double pages
= realmem
.availrmem
;
238 if (0 <= pages
&& 0 <= pagesize
)
239 return pages
* pagesize
;
244 #if HAVE_TABLE && defined TBL_VMSTATS
245 { /* This works on Tru64 UNIX V4/5. */
246 struct tbl_vmstats vmstats
;
248 if (table (TBL_VMSTATS
, 0, &vmstats
, 1, sizeof (vmstats
)) == 1)
250 double pages
= vmstats
.free_count
;
251 double pagesize
= vmstats
.pagesize
;
253 if (0 <= pages
&& 0 <= pagesize
)
254 return pages
* pagesize
;
259 #if HAVE_SYSCTL && defined HW_USERMEM
260 { /* This works on *bsd and darwin. */
261 unsigned int usermem
;
262 size_t len
= sizeof usermem
;
263 static int mib
[2] = { CTL_HW
, HW_USERMEM
};
265 if (sysctl (mib
, ARRAY_SIZE (mib
), &usermem
, &len
, NULL
, 0) == 0
266 && len
== sizeof (usermem
))
267 return (double) usermem
;
272 { /* this works on windows */
274 HMODULE h
= GetModuleHandle ("kernel32.dll");
279 /* Use GlobalMemoryStatusEx if available. */
280 if ((pfnex
= (PFN_MS_EX
) GetProcAddress (h
, "GlobalMemoryStatusEx")))
282 lMEMORYSTATUSEX lms_ex
;
283 lms_ex
.dwLength
= sizeof lms_ex
;
284 if (!pfnex (&lms_ex
))
286 return (double) lms_ex
.ullAvailPhys
;
289 /* Fall back to GlobalMemoryStatus which is always available.
290 but returns wrong results for physical memory > 4GB */
294 GlobalMemoryStatus (&ms
);
295 return (double) ms
.dwAvailPhys
;
300 /* Guess 25% of physical memory. */
301 return physmem_total () / 4;
313 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
321 compile-command: "gcc -DDEBUG -g -O -Wall -W physmem.c"