getprogname: Work around program name truncation when possible.
[gnulib.git] / lib / physmem.c
blobc89c0f06f77376176d11475a8a425e810a640037
1 /* Calculate the size of physical memory.
3 Copyright (C) 2000-2001, 2003, 2005-2006, 2009-2018 Free Software
4 Foundation, Inc.
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. */
21 #include <config.h>
23 #include "physmem.h"
25 #include <unistd.h>
27 #if HAVE_SYS_PSTAT_H
28 # include <sys/pstat.h>
29 #endif
31 #if HAVE_SYS_SYSMP_H
32 # include <sys/sysmp.h>
33 #endif
35 #if HAVE_SYS_SYSINFO_H
36 # include <sys/sysinfo.h>
37 #endif
39 #if HAVE_MACHINE_HAL_SYSINFO_H
40 # include <machine/hal_sysinfo.h>
41 #endif
43 #if HAVE_SYS_TABLE_H
44 # include <sys/table.h>
45 #endif
47 #include <sys/types.h>
49 #if HAVE_SYS_PARAM_H
50 # include <sys/param.h>
51 #endif
53 #if HAVE_SYS_SYSCTL_H
54 # include <sys/sysctl.h>
55 #endif
57 #if HAVE_SYS_SYSTEMCFG_H
58 # include <sys/systemcfg.h>
59 #endif
61 #ifdef _WIN32
63 # define WIN32_LEAN_AND_MEAN
64 # include <windows.h>
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. */
72 typedef struct
74 DWORD dwLength;
75 DWORD dwMemoryLoad;
76 DWORDLONG ullTotalPhys;
77 DWORDLONG ullAvailPhys;
78 DWORDLONG ullTotalPageFile;
79 DWORDLONG ullAvailPageFile;
80 DWORDLONG ullTotalVirtual;
81 DWORDLONG ullAvailVirtual;
82 DWORDLONG ullAvailExtendedVirtual;
83 } lMEMORYSTATUSEX;
84 typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*);
86 #endif
88 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
90 /* Return the total amount of physical memory. */
91 double
92 physmem_total (void)
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;
101 #endif
103 #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
104 { /* This works on linux. */
105 struct sysinfo si;
106 if (sysinfo(&si) == 0)
107 return (double) si.totalram * si.mem_unit;
109 #endif
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;
122 #endif
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;
135 #endif
137 #if HAVE_GETSYSINFO && defined GSI_PHYSMEM
138 { /* This works on Tru64 UNIX V4/5. */
139 int physmem;
141 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
142 NULL, NULL, NULL) == 1)
144 double kbytes = physmem;
146 if (0 <= kbytes)
147 return kbytes * 1024.0;
150 #endif
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;
162 #endif
164 #if HAVE__SYSTEM_CONFIGURATION
165 /* This works on AIX. */
166 return _system_configuration.physmem;
167 #endif
169 #if defined _WIN32
170 { /* this works on windows */
171 PFN_MS_EX pfnex;
172 HMODULE h = GetModuleHandle ("kernel32.dll");
174 if (!h)
175 return 0.0;
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))
183 return 0.0;
184 return (double) lms_ex.ullTotalPhys;
187 /* Fall back to GlobalMemoryStatus which is always available.
188 but returns wrong results for physical memory > 4GB. */
189 else
191 MEMORYSTATUS ms;
192 GlobalMemoryStatus (&ms);
193 return (double) ms.dwTotalPhys;
196 #endif
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. */
203 double
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;
213 #endif
215 #if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
216 { /* This works on linux. */
217 struct sysinfo si;
218 if (sysinfo(&si) == 0)
219 return ((double) si.freeram + si.bufferram) * si.mem_unit;
221 #endif
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;
236 #endif
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;
249 #endif
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;
264 #endif
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;
276 #endif
278 #if defined _WIN32
279 { /* this works on windows */
280 PFN_MS_EX pfnex;
281 HMODULE h = GetModuleHandle ("kernel32.dll");
283 if (!h)
284 return 0.0;
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))
292 return 0.0;
293 return (double) lms_ex.ullAvailPhys;
296 /* Fall back to GlobalMemoryStatus which is always available.
297 but returns wrong results for physical memory > 4GB */
298 else
300 MEMORYSTATUS ms;
301 GlobalMemoryStatus (&ms);
302 return (double) ms.dwAvailPhys;
305 #endif
307 /* Guess 25% of physical memory. */
308 return physmem_total () / 4;
312 #if DEBUG
314 # include <stdio.h>
315 # include <stdlib.h>
318 main (void)
320 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
321 exit (0);
324 #endif /* DEBUG */
327 Local Variables:
328 compile-command: "gcc -DDEBUG -g -O -Wall -W physmem.c"
329 End: