.
[coreutils.git] / lib / physmem.c
blobdc67b575cff577b28ec7a1de35fc66e7433137f8
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)
7 any later version.
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. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include "physmem.h"
26 #if HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
30 #if HAVE_SYS_PSTAT_H
31 # include <sys/pstat.h>
32 #endif
34 #if HAVE_SYS_SYSMP_H
35 # include <sys/sysmp.h>
36 #endif
38 #if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H
39 # include <sys/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
62 # define WIN32_LEAN_AND_MEAN
63 # include <windows.h>
64 /* MEMORYSTATUSEX is missing from older windows headers, so define
65 a local replacement. */
66 typedef struct
68 DWORD dwLength;
69 DWORD dwMemoryLoad;
70 DWORDLONG ullTotalPhys;
71 DWORDLONG ullAvailPhys;
72 DWORDLONG ullTotalPageFile;
73 DWORDLONG ullAvailPageFile;
74 DWORDLONG ullTotalVirtual;
75 DWORDLONG ullAvailVirtual;
76 DWORDLONG ullAvailExtendedVirtual;
77 } lMEMORYSTATUSEX;
78 typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*);
79 #endif
81 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
83 /* Return the total amount of physical memory. */
84 double
85 physmem_total (void)
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;
94 #endif
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;
107 #endif
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;
120 #endif
122 #if HAVE_GETSYSINFO && defined GSI_PHYSMEM
123 { /* This works on Tru64 UNIX V4/5. */
124 int physmem;
126 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
127 NULL, NULL, NULL) == 1)
129 double kbytes = physmem;
131 if (0 <= kbytes)
132 return kbytes * 1024.0;
135 #endif
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;
147 #endif
149 #if HAVE__SYSTEM_CONFIGURATION
150 /* This works on AIX. */
151 return _system_configuration.physmem;
152 #endif
154 #if defined _WIN32
155 { /* this works on windows */
156 PFN_MS_EX pfnex;
157 HMODULE h = GetModuleHandle ("kernel32.dll");
159 if (!h)
160 return 0.0;
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))
168 return 0.0;
169 return (double) lms_ex.ullTotalPhys;
172 /* Fall back to GlobalMemoryStatus which is always available.
173 but returns wrong results for physical memory > 4GB. */
174 else
176 MEMORYSTATUS ms;
177 GlobalMemoryStatus (&ms);
178 return (double) ms.dwTotalPhys;
181 #endif
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. */
188 double
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;
198 #endif
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;
213 #endif
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;
226 #endif
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;
241 #endif
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;
253 #endif
255 #if defined _WIN32
256 { /* this works on windows */
257 PFN_MS_EX pfnex;
258 HMODULE h = GetModuleHandle ("kernel32.dll");
260 if (!h)
261 return 0.0;
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))
269 return 0.0;
270 return (double) lms_ex.ullAvailPhys;
273 /* Fall back to GlobalMemoryStatus which is always available.
274 but returns wrong results for physical memory > 4GB */
275 else
277 MEMORYSTATUS ms;
278 GlobalMemoryStatus (&ms);
279 return (double) ms.dwAvailPhys;
282 #endif
284 /* Guess 25% of physical memory. */
285 return physmem_total () / 4;
289 #if DEBUG
291 # include <stdio.h>
292 # include <stdlib.h>
295 main (void)
297 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
298 exit (0);
301 #endif /* DEBUG */
304 Local Variables:
305 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"
306 End: