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