1 /* Calculate the size of physical memory.
2 Copyright 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 and Jim Meyering. */
29 # include <sys/pstat.h>
33 #include <sys/sysmp.h>
36 #if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H
37 # include <sys/sysinfo.h>
38 # include <machine/hal_sysinfo.h>
42 # include <sys/table.h>
45 #include <sys/types.h>
48 #include <sys/param.h>
52 #include <sys/sysctl.h>
55 #include "libiberty.h"
57 /* Return the total amount of physical memory. */
61 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
63 double pages = sysconf (_SC_PHYS_PAGES);
64 double pagesize = sysconf (_SC_PAGESIZE);
65 if (0 <= pages && 0 <= pagesize)
66 return pages * pagesize;
70 #if HAVE_PSTAT_GETSTATIC
71 { /* This works on hpux11. */
72 struct pst_static pss;
73 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0))
75 double pages = pss.physical_memory;
76 double pagesize = pss.page_size;
77 if (0 <= pages && 0 <= pagesize)
78 return pages * pagesize;
83 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
84 { /* This works on irix6. */
85 struct rminfo realmem;
86 if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == 0)
88 double pagesize = sysconf (_SC_PAGESIZE);
89 double pages = realmem.physmem;
90 if (0 <= pages && 0 <= pagesize)
91 return pages * pagesize;
97 { /* This works on Tru64 UNIX V4/5. */
100 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
101 NULL, NULL, NULL) == 1)
103 double kbytes = physmem;
106 return kbytes * 1024.0;
111 #if HAVE_SYSCTL && defined HW_PHYSMEM
112 { /* This works on *bsd and darwin. */
113 unsigned int physmem;
114 size_t len = sizeof(physmem);
115 static int mib[2] = {CTL_HW, HW_PHYSMEM};
117 if (sysctl(mib, ARRAY_SIZE(mib), &physmem, &len, NULL, 0) == 0
118 && len == sizeof (physmem))
119 return (double)physmem;
123 /* Return 0 if we can't determine the value. */
127 /* Return the amount of physical memory available. */
131 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
133 double pages = sysconf (_SC_AVPHYS_PAGES);
134 double pagesize = sysconf (_SC_PAGESIZE);
135 if (0 <= pages && 0 <= pagesize)
136 return pages * pagesize;
140 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
141 { /* This works on hpux11. */
142 struct pst_static pss;
143 struct pst_dynamic psd;
144 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)
145 && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0))
147 double pages = psd.psd_free;
148 double pagesize = pss.page_size;
149 if (0 <= pages && 0 <= pagesize)
150 return pages * pagesize;
155 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
156 { /* This works on irix6. */
157 struct rminfo realmem;
158 if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == 0)
160 double pagesize = sysconf (_SC_PAGESIZE);
161 double pages = realmem.availrmem;
162 if (0 <= pages && 0 <= pagesize)
163 return pages * pagesize;
168 #if HAVE_TABLE && HAVE_SYS_TABLE_H
169 { /* This works on Tru64 UNIX V4/5. */
170 struct tbl_vmstats vmstats;
172 if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
174 double pages = vmstats.free_count;
175 double pagesize = vmstats.pagesize;
177 if (0 <= pages && 0 <= pagesize)
178 return pages * pagesize;
183 #if HAVE_SYSCTL && defined HW_USERMEM
184 { /* This works on *bsd and darwin. */
185 unsigned int usermem;
186 size_t len = sizeof(usermem);
187 static int mib[2] = {CTL_HW, HW_USERMEM};
189 if (sysctl(mib, ARRAY_SIZE(mib), &usermem, &len, NULL, 0) == 0
190 && len == sizeof (usermem))
191 return (double)usermem;
195 /* Guess 25% of physical memory. */
196 return physmem_total () / 4;
208 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
216 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"