1 /* Calculate the size of physical memory.
3 Copyright (C) 2000-2001, 2003, 2005-2006, 2009-2018 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>
63 # define WIN32_LEAN_AND_MEAN
66 /* Avoid warnings from gcc -Wcast-function-type. */
67 # define GetProcAddress \
68 (void *) GetProcAddress
70 /* MEMORYSTATUSEX is missing from older windows headers, so define
71 a local replacement. */
76 DWORDLONG ullTotalPhys
;
77 DWORDLONG ullAvailPhys
;
78 DWORDLONG ullTotalPageFile
;
79 DWORDLONG ullAvailPageFile
;
80 DWORDLONG ullTotalVirtual
;
81 DWORDLONG ullAvailVirtual
;
82 DWORDLONG ullAvailExtendedVirtual
;
84 typedef WINBOOL (WINAPI
*PFN_MS_EX
) (lMEMORYSTATUSEX
*);
88 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
90 /* Return the total amount of physical memory. */
94 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
95 { /* This works on linux-gnu, solaris2 and cygwin. */
96 double pages
= sysconf (_SC_PHYS_PAGES
);
97 double pagesize
= sysconf (_SC_PAGESIZE
);
98 if (0 <= pages
&& 0 <= pagesize
)
99 return pages
* pagesize
;
103 #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
104 { /* This works on linux. */
106 if (sysinfo(&si
) == 0)
107 return (double) si
.totalram
* si
.mem_unit
;
111 #if HAVE_PSTAT_GETSTATIC
112 { /* This works on hpux11. */
113 struct pst_static pss
;
114 if (0 <= pstat_getstatic (&pss
, sizeof pss
, 1, 0))
116 double pages
= pss
.physical_memory
;
117 double pagesize
= pss
.page_size
;
118 if (0 <= pages
&& 0 <= pagesize
)
119 return pages
* pagesize
;
124 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
125 { /* This works on irix6. */
126 struct rminfo realmem
;
127 if (sysmp (MP_SAGET
, MPSA_RMINFO
, &realmem
, sizeof realmem
) == 0)
129 double pagesize
= sysconf (_SC_PAGESIZE
);
130 double pages
= realmem
.physmem
;
131 if (0 <= pages
&& 0 <= pagesize
)
132 return pages
* pagesize
;
137 #if HAVE_GETSYSINFO && defined GSI_PHYSMEM
138 { /* This works on Tru64 UNIX V4/5. */
141 if (getsysinfo (GSI_PHYSMEM
, (caddr_t
) &physmem
, sizeof (physmem
),
142 NULL
, NULL
, NULL
) == 1)
144 double kbytes
= physmem
;
147 return kbytes
* 1024.0;
152 #if HAVE_SYSCTL && defined HW_PHYSMEM
153 { /* This works on *bsd and darwin. */
154 unsigned int physmem
;
155 size_t len
= sizeof physmem
;
156 static int mib
[2] = { CTL_HW
, HW_PHYSMEM
};
158 if (sysctl (mib
, ARRAY_SIZE (mib
), &physmem
, &len
, NULL
, 0) == 0
159 && len
== sizeof (physmem
))
160 return (double) physmem
;
164 #if HAVE__SYSTEM_CONFIGURATION
165 /* This works on AIX. */
166 return _system_configuration
.physmem
;
170 { /* this works on windows */
172 HMODULE h
= GetModuleHandle ("kernel32.dll");
177 /* Use GlobalMemoryStatusEx if available. */
178 if ((pfnex
= (PFN_MS_EX
) GetProcAddress (h
, "GlobalMemoryStatusEx")))
180 lMEMORYSTATUSEX lms_ex
;
181 lms_ex
.dwLength
= sizeof lms_ex
;
182 if (!pfnex (&lms_ex
))
184 return (double) lms_ex
.ullTotalPhys
;
187 /* Fall back to GlobalMemoryStatus which is always available.
188 but returns wrong results for physical memory > 4GB. */
192 GlobalMemoryStatus (&ms
);
193 return (double) ms
.dwTotalPhys
;
198 /* Guess 64 MB. It's probably an older host, so guess small. */
199 return 64 * 1024 * 1024;
202 /* Return the amount of physical memory available. */
204 physmem_available (void)
206 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
207 { /* This works on linux-gnu, solaris2 and cygwin. */
208 double pages
= sysconf (_SC_AVPHYS_PAGES
);
209 double pagesize
= sysconf (_SC_PAGESIZE
);
210 if (0 <= pages
&& 0 <= pagesize
)
211 return pages
* pagesize
;
215 #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
216 { /* This works on linux. */
218 if (sysinfo(&si
) == 0)
219 return ((double) si
.freeram
+ si
.bufferram
) * si
.mem_unit
;
223 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
224 { /* This works on hpux11. */
225 struct pst_static pss
;
226 struct pst_dynamic psd
;
227 if (0 <= pstat_getstatic (&pss
, sizeof pss
, 1, 0)
228 && 0 <= pstat_getdynamic (&psd
, sizeof psd
, 1, 0))
230 double pages
= psd
.psd_free
;
231 double pagesize
= pss
.page_size
;
232 if (0 <= pages
&& 0 <= pagesize
)
233 return pages
* pagesize
;
238 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
239 { /* This works on irix6. */
240 struct rminfo realmem
;
241 if (sysmp (MP_SAGET
, MPSA_RMINFO
, &realmem
, sizeof realmem
) == 0)
243 double pagesize
= sysconf (_SC_PAGESIZE
);
244 double pages
= realmem
.availrmem
;
245 if (0 <= pages
&& 0 <= pagesize
)
246 return pages
* pagesize
;
251 #if HAVE_TABLE && defined TBL_VMSTATS
252 { /* This works on Tru64 UNIX V4/5. */
253 struct tbl_vmstats vmstats
;
255 if (table (TBL_VMSTATS
, 0, &vmstats
, 1, sizeof (vmstats
)) == 1)
257 double pages
= vmstats
.free_count
;
258 double pagesize
= vmstats
.pagesize
;
260 if (0 <= pages
&& 0 <= pagesize
)
261 return pages
* pagesize
;
266 #if HAVE_SYSCTL && defined HW_USERMEM
267 { /* This works on *bsd and darwin. */
268 unsigned int usermem
;
269 size_t len
= sizeof usermem
;
270 static int mib
[2] = { CTL_HW
, HW_USERMEM
};
272 if (sysctl (mib
, ARRAY_SIZE (mib
), &usermem
, &len
, NULL
, 0) == 0
273 && len
== sizeof (usermem
))
274 return (double) usermem
;
279 { /* this works on windows */
281 HMODULE h
= GetModuleHandle ("kernel32.dll");
286 /* Use GlobalMemoryStatusEx if available. */
287 if ((pfnex
= (PFN_MS_EX
) GetProcAddress (h
, "GlobalMemoryStatusEx")))
289 lMEMORYSTATUSEX lms_ex
;
290 lms_ex
.dwLength
= sizeof lms_ex
;
291 if (!pfnex (&lms_ex
))
293 return (double) lms_ex
.ullAvailPhys
;
296 /* Fall back to GlobalMemoryStatus which is always available.
297 but returns wrong results for physical memory > 4GB */
301 GlobalMemoryStatus (&ms
);
302 return (double) ms
.dwAvailPhys
;
307 /* Guess 25% of physical memory. */
308 return physmem_total () / 4;
320 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
328 compile-command: "gcc -DDEBUG -g -O -Wall -W physmem.c"