OSDN Git Service

* configure.in: Check for sys/sysctl.h and sysctl.
[pf3gnuchains/gcc-fork.git] / libiberty / physmem.c
1 /* Calculate the size of physical memory.
2    Copyright 2000, 2001, 2003 Free Software Foundation, Inc.
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
18 /* Written by Paul Eggert and Jim Meyering.  */
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
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 <sys/types.h>
46
47 #if HAVE_SYS_PARAM_H
48 #include <sys/param.h>
49 #endif
50
51 #if HAVE_SYS_SYSCTL_H
52 #include <sys/sysctl.h>
53 #endif
54
55 #include "libiberty.h"
56
57 /* Return the total amount of physical memory.  */
58 double
59 physmem_total ()
60 {
61 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
62   {
63     double pages = sysconf (_SC_PHYS_PAGES);
64     double pagesize = sysconf (_SC_PAGESIZE);
65     if (0 <= pages && 0 <= pagesize)
66       return pages * pagesize;
67   }
68 #endif
69
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))
74       {
75         double pages = pss.physical_memory;
76         double pagesize = pss.page_size;
77         if (0 <= pages && 0 <= pagesize)
78           return pages * pagesize;
79       }
80   }
81 #endif
82
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)
87       {
88         double pagesize = sysconf (_SC_PAGESIZE);
89         double pages = realmem.physmem;
90         if (0 <= pages && 0 <= pagesize)
91           return pages * pagesize;
92       }
93   }
94 #endif
95
96 #if HAVE_GETSYSINFO
97   { /* This works on Tru64 UNIX V4/5.  */
98     int physmem;
99
100     if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem),
101                     NULL, NULL, NULL) == 1)
102       {
103         double kbytes = physmem;
104
105         if (0 <= kbytes)
106           return kbytes * 1024.0;
107       }
108   }
109 #endif
110
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};
116
117     if (sysctl(mib, ARRAY_SIZE(mib), &physmem, &len, NULL, 0) == 0
118         && len == sizeof (physmem))
119       return (double)physmem;
120   }
121 #endif
122
123   /* Return 0 if we can't determine the value.  */
124   return 0;
125 }
126
127 /* Return the amount of physical memory available.  */
128 double
129 physmem_available ()
130 {
131 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
132   {
133     double pages = sysconf (_SC_AVPHYS_PAGES);
134     double pagesize = sysconf (_SC_PAGESIZE);
135     if (0 <= pages && 0 <= pagesize)
136       return pages * pagesize;
137   }
138 #endif
139
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))
146       {
147         double pages = psd.psd_free;
148         double pagesize = pss.page_size;
149         if (0 <= pages && 0 <= pagesize)
150           return pages * pagesize;
151       }
152   }
153 #endif
154
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)
159       {
160         double pagesize = sysconf (_SC_PAGESIZE);
161         double pages = realmem.availrmem;
162         if (0 <= pages && 0 <= pagesize)
163           return pages * pagesize;
164       }
165   }
166 #endif
167
168 #if HAVE_TABLE && HAVE_SYS_TABLE_H
169   { /* This works on Tru64 UNIX V4/5.  */
170     struct tbl_vmstats vmstats;
171
172     if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1)
173       {
174         double pages = vmstats.free_count;
175         double pagesize = vmstats.pagesize;
176
177         if (0 <= pages && 0 <= pagesize)
178           return pages * pagesize;
179       }
180   }
181 #endif
182
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};
188
189     if (sysctl(mib, ARRAY_SIZE(mib), &usermem, &len, NULL, 0) == 0
190         && len == sizeof (usermem))
191       return (double)usermem;
192   }
193 #endif
194
195   /* Guess 25% of physical memory.  */
196   return physmem_total () / 4;
197 }
198
199
200 #if DEBUG
201
202 # include <stdio.h>
203 # include <stdlib.h>
204
205 int
206 main ()
207 {
208   printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
209   exit (0);
210 }
211
212 #endif /* DEBUG */
213
214 /*
215 Local Variables:
216 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"
217 End:
218 */