80fdd7975e929acc27e446b8cffabb7ae0209875
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. */
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 #if HAVE_SYS_SYSTEMCFG_H
56 # include <sys/systemcfg.h>
59 #include "libiberty.h"
61 /* Return the total amount of physical memory. */
65 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
67 double pages
= sysconf (_SC_PHYS_PAGES
);
68 double pagesize
= sysconf (_SC_PAGESIZE
);
69 if (0 <= pages
&& 0 <= pagesize
)
70 return pages
* pagesize
;
74 #if HAVE_PSTAT_GETSTATIC
75 { /* This works on hpux11. */
76 struct pst_static pss
;
77 if (0 <= pstat_getstatic (&pss
, sizeof pss
, 1, 0))
79 double pages
= pss
.physical_memory
;
80 double pagesize
= pss
.page_size
;
81 if (0 <= pages
&& 0 <= pagesize
)
82 return pages
* pagesize
;
87 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
88 { /* This works on irix6. */
89 struct rminfo realmem
;
90 if (sysmp (MP_SAGET
, MPSA_RMINFO
, &realmem
, sizeof realmem
) == 0)
92 double pagesize
= sysconf (_SC_PAGESIZE
);
93 double pages
= realmem
.physmem
;
94 if (0 <= pages
&& 0 <= pagesize
)
95 return pages
* pagesize
;
101 { /* This works on Tru64 UNIX V4/5. */
104 if (getsysinfo (GSI_PHYSMEM
, (caddr_t
) &physmem
, sizeof (physmem
),
105 NULL
, NULL
, NULL
) == 1)
107 double kbytes
= physmem
;
110 return kbytes
* 1024.0;
115 #if HAVE_SYSCTL && defined HW_PHYSMEM
116 { /* This works on *bsd and darwin. */
117 unsigned int physmem
;
118 size_t len
= sizeof(physmem
);
119 static int mib
[2] = {CTL_HW
, HW_PHYSMEM
};
121 if (sysctl(mib
, ARRAY_SIZE(mib
), &physmem
, &len
, NULL
, 0) == 0
122 && len
== sizeof (physmem
))
123 return (double)physmem
;
127 #if HAVE__SYSTEM_CONFIGURATION
128 /* This works on AIX. */
129 return _system_configuration
.physmem
;
132 /* Return 0 if we can't determine the value. */
136 /* Return the amount of physical memory available. */
140 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
142 double pages
= sysconf (_SC_AVPHYS_PAGES
);
143 double pagesize
= sysconf (_SC_PAGESIZE
);
144 if (0 <= pages
&& 0 <= pagesize
)
145 return pages
* pagesize
;
149 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
150 { /* This works on hpux11. */
151 struct pst_static pss
;
152 struct pst_dynamic psd
;
153 if (0 <= pstat_getstatic (&pss
, sizeof pss
, 1, 0)
154 && 0 <= pstat_getdynamic (&psd
, sizeof psd
, 1, 0))
156 double pages
= psd
.psd_free
;
157 double pagesize
= pss
.page_size
;
158 if (0 <= pages
&& 0 <= pagesize
)
159 return pages
* pagesize
;
164 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
165 { /* This works on irix6. */
166 struct rminfo realmem
;
167 if (sysmp (MP_SAGET
, MPSA_RMINFO
, &realmem
, sizeof realmem
) == 0)
169 double pagesize
= sysconf (_SC_PAGESIZE
);
170 double pages
= realmem
.availrmem
;
171 if (0 <= pages
&& 0 <= pagesize
)
172 return pages
* pagesize
;
177 #if HAVE_TABLE && HAVE_SYS_TABLE_H
178 { /* This works on Tru64 UNIX V4/5. */
179 struct tbl_vmstats vmstats
;
181 if (table (TBL_VMSTATS
, 0, &vmstats
, 1, sizeof (vmstats
)) == 1)
183 double pages
= vmstats
.free_count
;
184 double pagesize
= vmstats
.pagesize
;
186 if (0 <= pages
&& 0 <= pagesize
)
187 return pages
* pagesize
;
192 #if HAVE_SYSCTL && defined HW_USERMEM
193 { /* This works on *bsd and darwin. */
194 unsigned int usermem
;
195 size_t len
= sizeof(usermem
);
196 static int mib
[2] = {CTL_HW
, HW_USERMEM
};
198 if (sysctl(mib
, ARRAY_SIZE(mib
), &usermem
, &len
, NULL
, 0) == 0
199 && len
== sizeof (usermem
))
200 return (double)usermem
;
204 /* Guess 25% of physical memory. */
205 return physmem_total () / 4;
217 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
225 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"
This page took 0.051781 seconds and 4 git commands to generate.