1 /* Calculate the size of physical memory.
2 Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Eggert. */
31 # include <sys/pstat.h>
35 # include <sys/sysmp.h>
38 #if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H
39 # include <sys/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_PSTAT_GETSTATIC
97 { /* This works on hpux11. */
98 struct pst_static pss
;
99 if (0 <= pstat_getstatic (&pss
, sizeof pss
, 1, 0))
101 double pages
= pss
.physical_memory
;
102 double pagesize
= pss
.page_size
;
103 if (0 <= pages
&& 0 <= pagesize
)
104 return pages
* pagesize
;
109 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
110 { /* This works on irix6. */
111 struct rminfo realmem
;
112 if (sysmp (MP_SAGET
, MPSA_RMINFO
, &realmem
, sizeof realmem
) == 0)
114 double pagesize
= sysconf (_SC_PAGESIZE
);
115 double pages
= realmem
.physmem
;
116 if (0 <= pages
&& 0 <= pagesize
)
117 return pages
* pagesize
;
122 #if HAVE_GETSYSINFO && defined GSI_PHYSMEM
123 { /* This works on Tru64 UNIX V4/5. */
126 if (getsysinfo (GSI_PHYSMEM
, (caddr_t
) &physmem
, sizeof (physmem
),
127 NULL
, NULL
, NULL
) == 1)
129 double kbytes
= physmem
;
132 return kbytes
* 1024.0;
137 #if HAVE_SYSCTL && defined HW_PHYSMEM
138 { /* This works on *bsd and darwin. */
139 unsigned int physmem
;
140 size_t len
= sizeof physmem
;
141 static int mib
[2] = { CTL_HW
, HW_PHYSMEM
};
143 if (sysctl (mib
, ARRAY_SIZE (mib
), &physmem
, &len
, NULL
, 0) == 0
144 && len
== sizeof (physmem
))
145 return (double) physmem
;
149 #if HAVE__SYSTEM_CONFIGURATION
150 /* This works on AIX. */
151 return _system_configuration
.physmem
;
155 { /* this works on windows */
157 HMODULE h
= GetModuleHandle ("kernel32.dll");
162 /* Use GlobalMemoryStatusEx if available. */
163 if ((pfnex
= (PFN_MS_EX
) GetProcAddress (h
, "GlobalMemoryStatusEx")))
165 lMEMORYSTATUSEX lms_ex
;
166 lms_ex
.dwLength
= sizeof lms_ex
;
167 if (!pfnex (&lms_ex
))
169 return (double) lms_ex
.ullTotalPhys
;
172 /* Fall back to GlobalMemoryStatus which is always available.
173 but returns wrong results for physical memory > 4GB. */
177 GlobalMemoryStatus (&ms
);
178 return (double) ms
.dwTotalPhys
;
183 /* Guess 64 MB. It's probably an older host, so guess small. */
184 return 64 * 1024 * 1024;
187 /* Return the amount of physical memory available. */
189 physmem_available (void)
191 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
192 { /* This works on linux-gnu, solaris2 and cygwin. */
193 double pages
= sysconf (_SC_AVPHYS_PAGES
);
194 double pagesize
= sysconf (_SC_PAGESIZE
);
195 if (0 <= pages
&& 0 <= pagesize
)
196 return pages
* pagesize
;
200 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
201 { /* This works on hpux11. */
202 struct pst_static pss
;
203 struct pst_dynamic psd
;
204 if (0 <= pstat_getstatic (&pss
, sizeof pss
, 1, 0)
205 && 0 <= pstat_getdynamic (&psd
, sizeof psd
, 1, 0))
207 double pages
= psd
.psd_free
;
208 double pagesize
= pss
.page_size
;
209 if (0 <= pages
&& 0 <= pagesize
)
210 return pages
* pagesize
;
215 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
216 { /* This works on irix6. */
217 struct rminfo realmem
;
218 if (sysmp (MP_SAGET
, MPSA_RMINFO
, &realmem
, sizeof realmem
) == 0)
220 double pagesize
= sysconf (_SC_PAGESIZE
);
221 double pages
= realmem
.availrmem
;
222 if (0 <= pages
&& 0 <= pagesize
)
223 return pages
* pagesize
;
228 #if HAVE_TABLE && defined TBL_VMSTATS
229 { /* This works on Tru64 UNIX V4/5. */
230 struct tbl_vmstats vmstats
;
232 if (table (TBL_VMSTATS
, 0, &vmstats
, 1, sizeof (vmstats
)) == 1)
234 double pages
= vmstats
.free_count
;
235 double pagesize
= vmstats
.pagesize
;
237 if (0 <= pages
&& 0 <= pagesize
)
238 return pages
* pagesize
;
243 #if HAVE_SYSCTL && defined HW_USERMEM
244 { /* This works on *bsd and darwin. */
245 unsigned int usermem
;
246 size_t len
= sizeof usermem
;
247 static int mib
[2] = { CTL_HW
, HW_USERMEM
};
249 if (sysctl (mib
, ARRAY_SIZE (mib
), &usermem
, &len
, NULL
, 0) == 0
250 && len
== sizeof (usermem
))
251 return (double) usermem
;
256 { /* this works on windows */
258 HMODULE h
= GetModuleHandle ("kernel32.dll");
263 /* Use GlobalMemoryStatusEx if available. */
264 if ((pfnex
= (PFN_MS_EX
) GetProcAddress (h
, "GlobalMemoryStatusEx")))
266 lMEMORYSTATUSEX lms_ex
;
267 lms_ex
.dwLength
= sizeof lms_ex
;
268 if (!pfnex (&lms_ex
))
270 return (double) lms_ex
.ullAvailPhys
;
273 /* Fall back to GlobalMemoryStatus which is always available.
274 but returns wrong results for physical memory > 4GB */
278 GlobalMemoryStatus (&ms
);
279 return (double) ms
.dwAvailPhys
;
284 /* Guess 25% of physical memory. */
285 return physmem_total () / 4;
297 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
305 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"