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 "libiberty.h"
47 /* Return the total amount of physical memory. */
51 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
53 double pages = sysconf (_SC_PHYS_PAGES);
54 double pagesize = sysconf (_SC_PAGESIZE);
55 if (0 <= pages && 0 <= pagesize)
56 return pages * pagesize;
60 #if HAVE_PSTAT_GETSTATIC
61 { /* This works on hpux11. */
62 struct pst_static pss;
63 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0))
65 double pages = pss.physical_memory;
66 double pagesize = pss.page_size;
67 if (0 <= pages && 0 <= pagesize)
68 return pages * pagesize;
73 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
74 { /* This works on irix6. */
75 struct rminfo realmem;
76 if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == 0)
78 double pagesize = sysconf (_SC_PAGESIZE);
79 double pages = realmem.physmem;
80 if (0 <= pages && 0 <= pagesize)
81 return pages * pagesize;
87 { /* This works on Tru64 UNIX V4/5. */
90 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
91 NULL, NULL, NULL) == 1)
93 double kbytes = physmem;
96 return kbytes * 1024.0;
101 /* Return 0 if we can't determine the value. */
105 /* Return the amount of physical memory available. */
109 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
111 double pages = sysconf (_SC_AVPHYS_PAGES);
112 double pagesize = sysconf (_SC_PAGESIZE);
113 if (0 <= pages && 0 <= pagesize)
114 return pages * pagesize;
118 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
119 { /* This works on hpux11. */
120 struct pst_static pss;
121 struct pst_dynamic psd;
122 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)
123 && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0))
125 double pages = psd.psd_free;
126 double pagesize = pss.page_size;
127 if (0 <= pages && 0 <= pagesize)
128 return pages * pagesize;
133 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
134 { /* This works on irix6. */
135 struct rminfo realmem;
136 if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == 0)
138 double pagesize = sysconf (_SC_PAGESIZE);
139 double pages = realmem.availrmem;
140 if (0 <= pages && 0 <= pagesize)
141 return pages * pagesize;
146 #if HAVE_TABLE && HAVE_SYS_TABLE_H
147 { /* This works on Tru64 UNIX V4/5. */
148 struct tbl_vmstats vmstats;
150 if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
152 double pages = vmstats.free_count;
153 double pagesize = vmstats.pagesize;
155 if (0 <= pages && 0 <= pagesize)
156 return pages * pagesize;
161 /* Guess 25% of physical memory. */
162 return physmem_total () / 4;
174 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
182 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"