2a71e88695ac324e9065b69ad397c339194df35f
[bpt/emacs.git] / src / vm-limit.c
1 /* Functions for memory limit warnings.
2 Copyright (C) 1990, 1992, 2001-2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <unistd.h> /* for 'environ', on AIX */
21 #include "lisp.h"
22 #include "mem-limits.h"
23
24 /*
25 Level number of warnings already issued.
26 0 -- no warnings issued.
27 1 -- 75% warning already issued.
28 2 -- 85% warning already issued.
29 3 -- 95% warning issued; keep warning frequently.
30 */
31 enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
32 static enum warnlevel warnlevel;
33
34 typedef void *POINTER;
35
36 /* Function to call to issue a warning;
37 0 means don't issue them. */
38 static void (*warn_function) (const char *);
39
40 /* Start of data space; can be changed by calling malloc_init. */
41 static POINTER data_space_start;
42
43 /* Number of bytes of writable memory we can expect to be able to get. */
44 static size_t lim_data;
45 \f
46
47 #if defined (HAVE_GETRLIMIT) && defined (RLIMIT_AS)
48 static void
49 get_lim_data (void)
50 {
51 struct rlimit rlimit;
52
53 getrlimit (RLIMIT_AS, &rlimit);
54 if (rlimit.rlim_cur == RLIM_INFINITY)
55 lim_data = -1;
56 else
57 lim_data = rlimit.rlim_cur;
58 }
59
60 #else /* not HAVE_GETRLIMIT */
61
62 #ifdef USG
63
64 static void
65 get_lim_data (void)
66 {
67 extern long ulimit ();
68
69 lim_data = -1;
70
71 /* Use the ulimit call, if we seem to have it. */
72 #if !defined (ULIMIT_BREAK_VALUE) || defined (GNU_LINUX)
73 lim_data = ulimit (3, 0);
74 #endif
75
76 /* If that didn't work, just use the macro's value. */
77 #ifdef ULIMIT_BREAK_VALUE
78 if (lim_data == -1)
79 lim_data = ULIMIT_BREAK_VALUE;
80 #endif
81
82 lim_data -= (long) data_space_start;
83 }
84
85 #else /* not USG */
86 #ifdef WINDOWSNT
87
88 #include "w32heap.h"
89
90 static void
91 get_lim_data (void)
92 {
93 extern size_t reserved_heap_size;
94 lim_data = reserved_heap_size;
95 }
96
97 #else
98 #if !defined (BSD4_2) && !defined (CYGWIN)
99
100 #ifdef MSDOS
101 void
102 get_lim_data (void)
103 {
104 _go32_dpmi_meminfo info;
105 unsigned long lim1, lim2;
106
107 _go32_dpmi_get_free_memory_information (&info);
108 /* DPMI server of Windows NT and its descendants reports in
109 info.available_memory a much lower amount that is really
110 available, which causes bogus "past 95% of memory limit"
111 warnings. Try to overcome that via circumstantial evidence. */
112 lim1 = info.available_memory;
113 lim2 = info.available_physical_pages;
114 /* DPMI Spec: "Fields that are unavailable will hold -1." */
115 if ((long)lim1 == -1L)
116 lim1 = 0;
117 if ((long)lim2 == -1L)
118 lim2 = 0;
119 else
120 lim2 *= 4096;
121 /* Surely, the available memory is at least what we have physically
122 available, right? */
123 if (lim1 >= lim2)
124 lim_data = lim1;
125 else
126 lim_data = lim2;
127 /* Don't believe they will give us more that 0.5 GB. */
128 if (lim_data > 512U * 1024U * 1024U)
129 lim_data = 512U * 1024U * 1024U;
130 }
131
132 unsigned long
133 ret_lim_data (void)
134 {
135 get_lim_data ();
136 return lim_data;
137 }
138 #else /* not MSDOS */
139 static void
140 get_lim_data (void)
141 {
142 lim_data = vlimit (LIM_DATA, -1);
143 }
144 #endif /* not MSDOS */
145
146 #else /* BSD4_2 || CYGWIN */
147
148 static void
149 get_lim_data (void)
150 {
151 struct rlimit XXrlimit;
152
153 getrlimit (RLIMIT_DATA, &XXrlimit);
154 #ifdef RLIM_INFINITY
155 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
156 #else
157 lim_data = XXrlimit.rlim_cur; /* soft limit */
158 #endif
159 }
160 #endif /* BSD4_2 */
161 #endif /* not WINDOWSNT */
162 #endif /* not USG */
163 #endif /* not HAVE_GETRLIMIT */
164 \f
165 /* Verify amount of memory available, complaining if we're near the end. */
166
167 static void
168 check_memory_limits (void)
169 {
170 #ifdef REL_ALLOC
171 extern POINTER (*real_morecore) (ptrdiff_t);
172 #endif
173 extern POINTER (*__morecore) (ptrdiff_t);
174
175 register POINTER cp;
176 size_t five_percent;
177 size_t data_size;
178 enum warnlevel new_warnlevel;
179
180 if (lim_data == 0)
181 get_lim_data ();
182 five_percent = lim_data / 20;
183
184 /* Find current end of memory and issue warning if getting near max */
185 #ifdef REL_ALLOC
186 if (real_morecore)
187 cp = (char *) (*real_morecore) (0);
188 else
189 #endif
190 cp = (char *) (*__morecore) (0);
191 data_size = (char *) cp - (char *) data_space_start;
192
193 if (!warn_function)
194 return;
195
196 /* What level of warning does current memory usage demand? */
197 new_warnlevel
198 = (data_size > five_percent * 19) ? warned_95
199 : (data_size > five_percent * 17) ? warned_85
200 : (data_size > five_percent * 15) ? warned_75
201 : not_warned;
202
203 /* If we have gone up a level, give the appropriate warning. */
204 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
205 {
206 warnlevel = new_warnlevel;
207 switch (warnlevel)
208 {
209 case warned_75:
210 (*warn_function) ("Warning: past 75% of memory limit");
211 break;
212
213 case warned_85:
214 (*warn_function) ("Warning: past 85% of memory limit");
215 break;
216
217 case warned_95:
218 (*warn_function) ("Warning: past 95% of memory limit");
219 }
220 }
221 /* Handle going down in usage levels, with some hysteresis. */
222 else
223 {
224 /* If we go down below 70% full, issue another 75% warning
225 when we go up again. */
226 if (data_size < five_percent * 14)
227 warnlevel = not_warned;
228 /* If we go down below 80% full, issue another 85% warning
229 when we go up again. */
230 else if (warnlevel > warned_75 && data_size < five_percent * 16)
231 warnlevel = warned_75;
232 /* If we go down below 90% full, issue another 95% warning
233 when we go up again. */
234 else if (warnlevel > warned_85 && data_size < five_percent * 18)
235 warnlevel = warned_85;
236 }
237
238 if (EXCEEDS_LISP_PTR (cp))
239 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
240 }
241 \f
242 #if !defined (CANNOT_DUMP) || !defined (SYSTEM_MALLOC)
243 /* Some systems that cannot dump also cannot implement these. */
244
245 /*
246 * Return the address of the start of the data segment prior to
247 * doing an unexec. After unexec the return value is undefined.
248 * See crt0.c for further information and definition of data_start.
249 *
250 * Apparently, on BSD systems this is etext at startup. On
251 * USG systems (swapping) this is highly mmu dependent and
252 * is also dependent on whether or not the program is running
253 * with shared text. Generally there is a (possibly large)
254 * gap between end of text and start of data with shared text.
255 *
256 */
257
258 char *
259 start_of_data (void)
260 {
261 #ifdef BSD_SYSTEM
262 extern char etext;
263 return (POINTER)(&etext);
264 #elif defined DATA_START
265 return ((POINTER) DATA_START);
266 #elif defined ORDINARY_LINK
267 /*
268 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
269 * data_start isn't defined. We take the address of environ, which
270 * is known to live at or near the start of the system crt0.c, and
271 * we don't sweat the handful of bytes that might lose.
272 */
273 return ((POINTER) &environ);
274 #else
275 extern int data_start;
276 return ((POINTER) &data_start);
277 #endif
278 }
279 #endif /* (not CANNOT_DUMP or not SYSTEM_MALLOC) */
280 \f
281 /* Enable memory usage warnings.
282 START says where the end of pure storage is.
283 WARNFUN specifies the function to call to issue a warning. */
284
285 void
286 memory_warnings (POINTER start, void (*warnfun) (const char *))
287 {
288 extern void (* __after_morecore_hook) (void); /* From gmalloc.c */
289
290 if (start)
291 data_space_start = start;
292 else
293 data_space_start = start_of_data ();
294
295 warn_function = warnfun;
296 __after_morecore_hook = check_memory_limits;
297
298 /* Force data limit to be recalculated on each run. */
299 lim_data = 0;
300 }