Commit | Line | Data |
---|---|---|
94d7c01a | 1 | /* Functions for memory limit warnings. |
ba318903 | 2 | Copyright (C) 1990, 1992, 2001-2014 Free Software Foundation, Inc. |
94d7c01a JA |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
9ec0b715 | 6 | GNU Emacs is free software: you can redistribute it and/or modify |
94d7c01a | 7 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
8 | the Free Software Foundation, either version 3 of the License, or |
9 | (at your option) any later version. | |
94d7c01a JA |
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 | |
9ec0b715 | 17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
94d7c01a | 18 | |
18160b98 | 19 | #include <config.h> |
b69a6d22 | 20 | #include <unistd.h> /* for 'environ', on AIX */ |
94d7c01a | 21 | #include "lisp.h" |
1ddc2bd6 PE |
22 | |
23 | #ifdef MSDOS | |
24 | #include <dpmi.h> | |
25 | extern int etext; | |
26 | #endif | |
27 | ||
28 | /* Some systems need this before <sys/resource.h>. */ | |
29 | #include <sys/types.h> | |
30 | ||
31 | #ifdef HAVE_SYS_RESOURCE_H | |
32 | # include <sys/time.h> | |
33 | # include <sys/resource.h> | |
34 | #else | |
35 | # if HAVE_SYS_VLIMIT_H | |
36 | # include <sys/vlimit.h> /* Obsolete, says glibc */ | |
37 | # endif | |
38 | #endif | |
39 | ||
40 | /* Start of data. It is OK if this is approximate; it's used only as | |
41 | a heuristic. */ | |
42 | #ifdef DATA_START | |
43 | # define data_start ((char *) DATA_START) | |
44 | #else | |
45 | extern char data_start[]; | |
46 | # ifndef HAVE_DATA_START | |
47 | /* Initialize to nonzero, so that it's put into data and not bss. | |
48 | Link this file's object code first, so that this symbol is near the | |
49 | start of data. */ | |
50 | char data_start[1] = { 1 }; | |
51 | # endif | |
52 | #endif | |
94d7c01a JA |
53 | |
54 | /* | |
55 | Level number of warnings already issued. | |
56 | 0 -- no warnings issued. | |
57 | 1 -- 75% warning already issued. | |
58 | 2 -- 85% warning already issued. | |
3b672b8f | 59 | 3 -- 95% warning issued; keep warning frequently. |
94d7c01a | 60 | */ |
395d3972 | 61 | enum warnlevel { not_warned, warned_75, warned_85, warned_95 }; |
395d3972 | 62 | static enum warnlevel warnlevel; |
94d7c01a JA |
63 | |
64 | /* Function to call to issue a warning; | |
65 | 0 means don't issue them. */ | |
a8fe7202 | 66 | static void (*warn_function) (const char *); |
94d7c01a | 67 | |
1ddc2bd6 PE |
68 | /* Start of data space; can be changed by calling memory_warnings. */ |
69 | static char *data_space_start; | |
395d3972 RS |
70 | |
71 | /* Number of bytes of writable memory we can expect to be able to get. */ | |
62aba0d4 | 72 | static size_t lim_data; |
395d3972 | 73 | \f |
7be78020 | 74 | #ifdef HAVE_GETRLIMIT |
51757187 | 75 | |
7be78020 PE |
76 | # ifndef RLIMIT_AS |
77 | # define RLIMIT_AS RLIMIT_DATA | |
78 | # endif | |
395d3972 RS |
79 | |
80 | static void | |
7c3320d8 | 81 | get_lim_data (void) |
395d3972 | 82 | { |
7be78020 PE |
83 | /* Set LIM_DATA to the minimum of the maximum object size and the |
84 | maximum address space. Don't bother to check for values like | |
85 | RLIM_INFINITY since in practice they are not much less than SIZE_MAX. */ | |
86 | struct rlimit rlimit; | |
87 | lim_data | |
88 | = (getrlimit (RLIMIT_AS, &rlimit) == 0 && rlimit.rlim_cur <= SIZE_MAX | |
89 | ? rlimit.rlim_cur | |
90 | : SIZE_MAX); | |
395d3972 RS |
91 | } |
92 | ||
7be78020 | 93 | #elif defined WINDOWSNT |
395d3972 | 94 | |
a68089e4 EZ |
95 | #include "w32heap.h" |
96 | ||
395d3972 | 97 | static void |
7c3320d8 | 98 | get_lim_data (void) |
395d3972 | 99 | { |
62aba0d4 | 100 | extern size_t reserved_heap_size; |
395d3972 RS |
101 | lim_data = reserved_heap_size; |
102 | } | |
103 | ||
7be78020 | 104 | #elif defined MSDOS |
395d3972 | 105 | |
395d3972 | 106 | void |
7c3320d8 | 107 | get_lim_data (void) |
395d3972 RS |
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 | |
7c3320d8 | 138 | ret_lim_data (void) |
ec06ec19 EZ |
139 | { |
140 | get_lim_data (); | |
141 | return lim_data; | |
142 | } | |
395d3972 | 143 | #else |
7be78020 | 144 | # error "get_lim_data not implemented on this machine" |
395d3972 | 145 | #endif |
395d3972 RS |
146 | \f |
147 | /* Verify amount of memory available, complaining if we're near the end. */ | |
94d7c01a | 148 | |
fd065466 | 149 | static void |
d3da34e0 | 150 | check_memory_limits (void) |
94d7c01a | 151 | { |
968e9c04 | 152 | #ifdef REL_ALLOC |
fcee5028 | 153 | extern void *(*real_morecore) (ptrdiff_t); |
1ddc2bd6 PE |
154 | #else |
155 | void *(*real_morecore) (ptrdiff_t) = 0; | |
968e9c04 | 156 | #endif |
fcee5028 | 157 | extern void *(*__morecore) (ptrdiff_t); |
134994ae | 158 | |
1ddc2bd6 | 159 | char *cp; |
62aba0d4 FP |
160 | size_t five_percent; |
161 | size_t data_size; | |
395d3972 | 162 | enum warnlevel new_warnlevel; |
94d7c01a JA |
163 | |
164 | if (lim_data == 0) | |
165 | get_lim_data (); | |
3b672b8f | 166 | five_percent = lim_data / 20; |
94d7c01a JA |
167 | |
168 | /* Find current end of memory and issue warning if getting near max */ | |
1ddc2bd6 PE |
169 | cp = (real_morecore ? real_morecore : __morecore) (0); |
170 | data_size = cp - data_space_start; | |
94d7c01a | 171 | |
395d3972 RS |
172 | if (!warn_function) |
173 | return; | |
174 | ||
175 | /* What level of warning does current memory usage demand? */ | |
21da04c4 CY |
176 | new_warnlevel |
177 | = (data_size > five_percent * 19) ? warned_95 | |
178 | : (data_size > five_percent * 17) ? warned_85 | |
179 | : (data_size > five_percent * 15) ? warned_75 | |
180 | : not_warned; | |
395d3972 RS |
181 | |
182 | /* If we have gone up a level, give the appropriate warning. */ | |
183 | if (new_warnlevel > warnlevel || new_warnlevel == warned_95) | |
184 | { | |
185 | warnlevel = new_warnlevel; | |
186 | switch (warnlevel) | |
187 | { | |
188 | case warned_75: | |
189 | (*warn_function) ("Warning: past 75% of memory limit"); | |
190 | break; | |
191 | ||
192 | case warned_85: | |
193 | (*warn_function) ("Warning: past 85% of memory limit"); | |
194 | break; | |
195 | ||
196 | case warned_95: | |
197 | (*warn_function) ("Warning: past 95% of memory limit"); | |
198 | } | |
199 | } | |
200 | /* Handle going down in usage levels, with some hysteresis. */ | |
201 | else | |
202 | { | |
203 | /* If we go down below 70% full, issue another 75% warning | |
204 | when we go up again. */ | |
205 | if (data_size < five_percent * 14) | |
206 | warnlevel = not_warned; | |
207 | /* If we go down below 80% full, issue another 85% warning | |
208 | when we go up again. */ | |
209 | else if (warnlevel > warned_75 && data_size < five_percent * 16) | |
210 | warnlevel = warned_75; | |
211 | /* If we go down below 90% full, issue another 95% warning | |
212 | when we go up again. */ | |
213 | else if (warnlevel > warned_85 && data_size < five_percent * 18) | |
214 | warnlevel = warned_85; | |
215 | } | |
94d7c01a | 216 | } |
395d3972 RS |
217 | \f |
218 | /* Enable memory usage warnings. | |
219 | START says where the end of pure storage is. | |
220 | WARNFUN specifies the function to call to issue a warning. */ | |
94d7c01a JA |
221 | |
222 | void | |
fcee5028 | 223 | memory_warnings (void *start, void (*warnfun) (const char *)) |
94d7c01a | 224 | { |
d2aa42f8 | 225 | extern void (* __after_morecore_hook) (void); /* From gmalloc.c */ |
94d7c01a | 226 | |
1ddc2bd6 | 227 | data_space_start = start ? start : data_start; |
3b672b8f | 228 | |
da396c5e | 229 | warn_function = warnfun; |
fd065466 | 230 | __after_morecore_hook = check_memory_limits; |
b78e8d0a | 231 | |
b78e8d0a AI |
232 | /* Force data limit to be recalculated on each run. */ |
233 | lim_data = 0; | |
94d7c01a | 234 | } |