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