]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/physmem.c
[Ada] doc/Makefile: automatically create doc/share/_static
[thirdparty/gcc.git] / libiberty / physmem.c
CommitLineData
5cf67f1b 1/* Calculate the size of physical memory.
fbd26352 2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
5cf67f1b 3
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)
7 any later version.
8
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.
13
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,
95b8d1bc 16 Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5cf67f1b 17
4195ec6a 18/* Written by Paul Eggert. */
5cf67f1b 19
20#if HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#if HAVE_UNISTD_H
25# include <unistd.h>
26#endif
27
28#if HAVE_SYS_PSTAT_H
29# include <sys/pstat.h>
30#endif
31
8f3b8386 32#if HAVE_SYS_SYSMP_H
4195ec6a 33# include <sys/sysmp.h>
8f3b8386 34#endif
35
622d92fb 36#if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H
4195ec6a 37# include <sys/sysinfo.h>
38# include <machine/hal_sysinfo.h>
622d92fb 39#endif
40
41#if HAVE_SYS_TABLE_H
4195ec6a 42# include <sys/table.h>
622d92fb 43#endif
44
0991d38e 45#include <sys/types.h>
46
47#if HAVE_SYS_PARAM_H
4195ec6a 48# include <sys/param.h>
0991d38e 49#endif
50
51#if HAVE_SYS_SYSCTL_H
4195ec6a 52# include <sys/sysctl.h>
53#endif
54
55#if HAVE_SYS_SYSTEMCFG_H
56# include <sys/systemcfg.h>
0991d38e 57#endif
58
eafe92ea 59#ifdef _WIN32
df4b17bf 60# define WIN32_LEAN_AND_MEAN
61# include <windows.h>
eafe92ea 62/* MEMORYSTATUSEX is missing from older windows headers, so define
df4b17bf 63 a local replacement. */
64typedef struct
65{
66 DWORD dwLength;
67 DWORD dwMemoryLoad;
68 DWORDLONG ullTotalPhys;
69 DWORDLONG ullAvailPhys;
70 DWORDLONG ullTotalPageFile;
71 DWORDLONG ullAvailPageFile;
72 DWORDLONG ullTotalVirtual;
73 DWORDLONG ullAvailVirtual;
74 DWORDLONG ullAvailExtendedVirtual;
eafe92ea 75} lMEMORYSTATUSEX;
76typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*);
77#endif
78
8f3b8386 79#include "libiberty.h"
80
5cf67f1b 81/* Return the total amount of physical memory. */
82double
f8d9d6a0 83physmem_total (void)
5cf67f1b 84{
85#if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
df4b17bf 86 { /* This works on linux-gnu, solaris2 and cygwin. */
5cf67f1b 87 double pages = sysconf (_SC_PHYS_PAGES);
88 double pagesize = sysconf (_SC_PAGESIZE);
89 if (0 <= pages && 0 <= pagesize)
90 return pages * pagesize;
91 }
92#endif
93
94#if HAVE_PSTAT_GETSTATIC
8f3b8386 95 { /* This works on hpux11. */
5cf67f1b 96 struct pst_static pss;
97 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0))
98 {
99 double pages = pss.physical_memory;
100 double pagesize = pss.page_size;
101 if (0 <= pages && 0 <= pagesize)
102 return pages * pagesize;
103 }
104 }
105#endif
106
8f3b8386 107#if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
108 { /* This works on irix6. */
109 struct rminfo realmem;
4195ec6a 110 if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0)
8f3b8386 111 {
112 double pagesize = sysconf (_SC_PAGESIZE);
113 double pages = realmem.physmem;
114 if (0 <= pages && 0 <= pagesize)
df4b17bf 115 return pages * pagesize;
8f3b8386 116 }
117 }
118#endif
119
5833978f 120#if HAVE_GETSYSINFO && defined GSI_PHYSMEM
622d92fb 121 { /* This works on Tru64 UNIX V4/5. */
122 int physmem;
123
124 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
125 NULL, NULL, NULL) == 1)
126 {
127 double kbytes = physmem;
128
129 if (0 <= kbytes)
130 return kbytes * 1024.0;
131 }
132 }
133#endif
134
0991d38e 135#if HAVE_SYSCTL && defined HW_PHYSMEM
136 { /* This works on *bsd and darwin. */
137 unsigned int physmem;
df4b17bf 138 size_t len = sizeof physmem;
139 static int mib[2] = { CTL_HW, HW_PHYSMEM };
0991d38e 140
df4b17bf 141 if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0
0991d38e 142 && len == sizeof (physmem))
df4b17bf 143 return (double) physmem;
0991d38e 144 }
145#endif
146
4195ec6a 147#if HAVE__SYSTEM_CONFIGURATION
5e90539e 148 /* This works on AIX 4.3.3+. */
4195ec6a 149 return _system_configuration.physmem;
150#endif
151
eafe92ea 152#if defined _WIN32
153 { /* this works on windows */
df4b17bf 154 PFN_MS_EX pfnex;
155 HMODULE h = GetModuleHandle ("kernel32.dll");
eafe92ea 156
df4b17bf 157 if (!h)
eafe92ea 158 return 0.0;
159
df4b17bf 160 /* Use GlobalMemoryStatusEx if available. */
eafe92ea 161 if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx")))
162 {
163 lMEMORYSTATUSEX lms_ex;
164 lms_ex.dwLength = sizeof lms_ex;
165 if (!pfnex (&lms_ex))
166 return 0.0;
df4b17bf 167 return (double) lms_ex.ullTotalPhys;
eafe92ea 168 }
169
170 /* Fall back to GlobalMemoryStatus which is always available.
df4b17bf 171 but returns wrong results for physical memory > 4GB. */
eafe92ea 172 else
173 {
df4b17bf 174 MEMORYSTATUS ms;
175 GlobalMemoryStatus (&ms);
176 return (double) ms.dwTotalPhys;
eafe92ea 177 }
df4b17bf 178 }
eafe92ea 179#endif
180
5cf67f1b 181 /* Return 0 if we can't determine the value. */
182 return 0;
183}
184
185/* Return the amount of physical memory available. */
186double
f8d9d6a0 187physmem_available (void)
5cf67f1b 188{
189#if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
df4b17bf 190 { /* This works on linux-gnu, solaris2 and cygwin. */
5cf67f1b 191 double pages = sysconf (_SC_AVPHYS_PAGES);
192 double pagesize = sysconf (_SC_PAGESIZE);
193 if (0 <= pages && 0 <= pagesize)
194 return pages * pagesize;
195 }
196#endif
197
198#if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
8f3b8386 199 { /* This works on hpux11. */
5cf67f1b 200 struct pst_static pss;
201 struct pst_dynamic psd;
202 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)
203 && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0))
204 {
205 double pages = psd.psd_free;
206 double pagesize = pss.page_size;
207 if (0 <= pages && 0 <= pagesize)
208 return pages * pagesize;
209 }
210 }
211#endif
212
8f3b8386 213#if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
214 { /* This works on irix6. */
215 struct rminfo realmem;
4195ec6a 216 if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0)
8f3b8386 217 {
218 double pagesize = sysconf (_SC_PAGESIZE);
219 double pages = realmem.availrmem;
220 if (0 <= pages && 0 <= pagesize)
df4b17bf 221 return pages * pagesize;
8f3b8386 222 }
223 }
224#endif
225
5833978f 226#if HAVE_TABLE && defined TBL_VMSTATS
622d92fb 227 { /* This works on Tru64 UNIX V4/5. */
228 struct tbl_vmstats vmstats;
229
230 if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
231 {
232 double pages = vmstats.free_count;
233 double pagesize = vmstats.pagesize;
234
235 if (0 <= pages && 0 <= pagesize)
236 return pages * pagesize;
237 }
238 }
239#endif
240
0991d38e 241#if HAVE_SYSCTL && defined HW_USERMEM
242 { /* This works on *bsd and darwin. */
243 unsigned int usermem;
df4b17bf 244 size_t len = sizeof usermem;
245 static int mib[2] = { CTL_HW, HW_USERMEM };
0991d38e 246
df4b17bf 247 if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0
0991d38e 248 && len == sizeof (usermem))
df4b17bf 249 return (double) usermem;
0991d38e 250 }
251#endif
252
eafe92ea 253#if defined _WIN32
254 { /* this works on windows */
df4b17bf 255 PFN_MS_EX pfnex;
eafe92ea 256 HMODULE h = GetModuleHandle ("kernel32.dll");
257
258 if (!h)
259 return 0.0;
260
df4b17bf 261 /* Use GlobalMemoryStatusEx if available. */
eafe92ea 262 if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx")))
263 {
264 lMEMORYSTATUSEX lms_ex;
265 lms_ex.dwLength = sizeof lms_ex;
266 if (!pfnex (&lms_ex))
267 return 0.0;
268 return (double) lms_ex.ullAvailPhys;
269 }
270
271 /* Fall back to GlobalMemoryStatus which is always available.
df4b17bf 272 but returns wrong results for physical memory > 4GB */
eafe92ea 273 else
274 {
275 MEMORYSTATUS ms;
276 GlobalMemoryStatus (&ms);
df4b17bf 277 return (double) ms.dwAvailPhys;
eafe92ea 278 }
279 }
280#endif
281
5cf67f1b 282 /* Guess 25% of physical memory. */
283 return physmem_total () / 4;
284}
8f3b8386 285
286
287#if DEBUG
288
289# include <stdio.h>
290# include <stdlib.h>
291
292int
f8d9d6a0 293main (void)
8f3b8386 294{
295 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
296 exit (0);
297}
298
299#endif /* DEBUG */
300
301/*
302Local Variables:
303compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"
304End:
305*/