merge from gcc
[deliverable/binutils-gdb.git] / libiberty / physmem.c
CommitLineData
4938384a 1/* Calculate the size of physical memory.
192cbe66 2 Copyright 2000, 2001, 2003 Free Software Foundation, Inc.
4938384a
DD
3
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.
8
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.
13
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. */
17
192cbe66 18/* Written by Paul Eggert and Jim Meyering. */
4938384a
DD
19
20#if HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#if HAVE_UNISTD_H
25# include <unistd.h>
26#endif
27
28#if HAVE_SYS_PSTAT_H
29# include <sys/pstat.h>
30#endif
31
192cbe66
DD
32#if HAVE_SYS_SYSMP_H
33#include <sys/sysmp.h>
34#endif
35
36#if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H
37# include <sys/sysinfo.h>
38# include <machine/hal_sysinfo.h>
39#endif
40
41#if HAVE_SYS_TABLE_H
42# include <sys/table.h>
43#endif
44
45#include "libiberty.h"
46
4938384a
DD
47/* Return the total amount of physical memory. */
48double
900d2082 49physmem_total ()
4938384a
DD
50{
51#if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
52 {
53 double pages = sysconf (_SC_PHYS_PAGES);
54 double pagesize = sysconf (_SC_PAGESIZE);
55 if (0 <= pages && 0 <= pagesize)
56 return pages * pagesize;
57 }
58#endif
59
60#if HAVE_PSTAT_GETSTATIC
192cbe66 61 { /* This works on hpux11. */
4938384a
DD
62 struct pst_static pss;
63 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0))
64 {
65 double pages = pss.physical_memory;
66 double pagesize = pss.page_size;
67 if (0 <= pages && 0 <= pagesize)
68 return pages * pagesize;
69 }
70 }
71#endif
72
192cbe66
DD
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)
77 {
78 double pagesize = sysconf (_SC_PAGESIZE);
79 double pages = realmem.physmem;
80 if (0 <= pages && 0 <= pagesize)
81 return pages * pagesize;
82 }
83 }
84#endif
85
86#if HAVE_GETSYSINFO
87 { /* This works on Tru64 UNIX V4/5. */
88 int physmem;
89
90 if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
91 NULL, NULL, NULL) == 1)
92 {
93 double kbytes = physmem;
94
95 if (0 <= kbytes)
96 return kbytes * 1024.0;
97 }
98 }
99#endif
100
4938384a
DD
101 /* Return 0 if we can't determine the value. */
102 return 0;
103}
104
105/* Return the amount of physical memory available. */
106double
900d2082 107physmem_available ()
4938384a
DD
108{
109#if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
110 {
111 double pages = sysconf (_SC_AVPHYS_PAGES);
112 double pagesize = sysconf (_SC_PAGESIZE);
113 if (0 <= pages && 0 <= pagesize)
114 return pages * pagesize;
115 }
116#endif
117
118#if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
192cbe66 119 { /* This works on hpux11. */
4938384a
DD
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))
124 {
125 double pages = psd.psd_free;
126 double pagesize = pss.page_size;
127 if (0 <= pages && 0 <= pagesize)
128 return pages * pagesize;
129 }
130 }
131#endif
132
192cbe66
DD
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)
137 {
138 double pagesize = sysconf (_SC_PAGESIZE);
139 double pages = realmem.availrmem;
140 if (0 <= pages && 0 <= pagesize)
141 return pages * pagesize;
142 }
143 }
144#endif
145
146#if HAVE_TABLE && HAVE_SYS_TABLE_H
147 { /* This works on Tru64 UNIX V4/5. */
148 struct tbl_vmstats vmstats;
149
150 if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
151 {
152 double pages = vmstats.free_count;
153 double pagesize = vmstats.pagesize;
154
155 if (0 <= pages && 0 <= pagesize)
156 return pages * pagesize;
157 }
158 }
159#endif
160
4938384a
DD
161 /* Guess 25% of physical memory. */
162 return physmem_total () / 4;
163}
192cbe66
DD
164
165
166#if DEBUG
167
168# include <stdio.h>
169# include <stdlib.h>
170
171int
172main ()
173{
174 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
175 exit (0);
176}
177
178#endif /* DEBUG */
179
180/*
181Local Variables:
182compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"
183End:
184*/
This page took 0.029925 seconds and 4 git commands to generate.