]> git.ipfire.org Git - thirdparty/gcc.git/blob - libiberty/physmem.c
configure.in: Check for sys/sysmp.h and sysmp.
[thirdparty/gcc.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 #include "libiberty.h"
37
38 /* Return the total amount of physical memory. */
39 double
40 physmem_total ()
41 {
42 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
43 {
44 double pages = sysconf (_SC_PHYS_PAGES);
45 double pagesize = sysconf (_SC_PAGESIZE);
46 if (0 <= pages && 0 <= pagesize)
47 return pages * pagesize;
48 }
49 #endif
50
51 #if HAVE_PSTAT_GETSTATIC
52 { /* This works on hpux11. */
53 struct pst_static pss;
54 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0))
55 {
56 double pages = pss.physical_memory;
57 double pagesize = pss.page_size;
58 if (0 <= pages && 0 <= pagesize)
59 return pages * pagesize;
60 }
61 }
62 #endif
63
64 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
65 { /* This works on irix6. */
66 struct rminfo realmem;
67 if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == 0)
68 {
69 double pagesize = sysconf (_SC_PAGESIZE);
70 double pages = realmem.physmem;
71 if (0 <= pages && 0 <= pagesize)
72 return pages * pagesize;
73 }
74 }
75 #endif
76
77 /* Return 0 if we can't determine the value. */
78 return 0;
79 }
80
81 /* Return the amount of physical memory available. */
82 double
83 physmem_available ()
84 {
85 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
86 {
87 double pages = sysconf (_SC_AVPHYS_PAGES);
88 double pagesize = sysconf (_SC_PAGESIZE);
89 if (0 <= pages && 0 <= pagesize)
90 return pages * pagesize;
91 }
92 #endif
93
94 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
95 { /* This works on hpux11. */
96 struct pst_static pss;
97 struct pst_dynamic psd;
98 if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)
99 && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0))
100 {
101 double pages = psd.psd_free;
102 double pagesize = pss.page_size;
103 if (0 <= pages && 0 <= pagesize)
104 return pages * pagesize;
105 }
106 }
107 #endif
108
109 #if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE
110 { /* This works on irix6. */
111 struct rminfo realmem;
112 if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == 0)
113 {
114 double pagesize = sysconf (_SC_PAGESIZE);
115 double pages = realmem.availrmem;
116 if (0 <= pages && 0 <= pagesize)
117 return pages * pagesize;
118 }
119 }
120 #endif
121
122 /* Guess 25% of physical memory. */
123 return physmem_total () / 4;
124 }
125
126
127 #if DEBUG
128
129 # include <stdio.h>
130 # include <stdlib.h>
131
132 int
133 main ()
134 {
135 printf ("%12.f %12.f\n", physmem_total (), physmem_available ());
136 exit (0);
137 }
138
139 #endif /* DEBUG */
140
141 /*
142 Local Variables:
143 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W physmem.c"
144 End:
145 */