1 /* Getter for RLIMIT_DATA.
2 Copyright (C) 2011-2018 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2011.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 #include "resource-ext.h"
23 /* The "data segment size" is defined as the virtual memory area of the
24 current process that contains malloc()ed memory.
26 There are two ways of retrieving the current data segment size:
27 a) by trying setrlimit with various values and observing whether the
28 kernel allows additional sbrk() calls,
29 b) by using system dependent APIs that allow to iterate over the list
30 of virtual memory areas.
31 We define two functions
32 get_rusage_data_via_setrlimit(),
33 get_rusage_data_via_iterator().
36 a') by trying setrlimit with various values and observing whether
37 additional malloc() calls succeed
38 is not as good as a), because a malloc() call can be served by already
39 allocated memory or through mmap(), and because a malloc() of 1 page may
42 Discussion per platform:
45 a) setrlimit with RLIMIT_DATA works.
46 b) The /proc/self/maps file contains a list of the virtual memory areas.
47 get_rusage_data_via_setrlimit() returns the sum of the length of the
48 executable's data segment plus the heap VMA (an anonymous memory area),
49 whereas get_rusage_data_via_iterator() returns only the latter.
50 Note that malloc() falls back on mmap() for large allocations and also
51 for small allocations if there is not enough room in the data segment.
54 a) setrlimit with RLIMIT_DATA succeeds but does not really work: The OS
55 ignores RLIMIT_DATA. Therefore get_rusage_data_via_setrlimit() is
57 b) The Mach based API works.
58 Note that malloc() falls back on mmap() for large allocations.
61 a) setrlimit with RLIMIT_DATA works.
62 b) The /proc/self/maps file contains a list of the virtual memory areas.
65 a) setrlimit with RLIMIT_DATA works.
66 b) The /proc/self/maps file contains a list of the virtual memory areas.
68 Note that malloc() uses mmap() for large allocations.
71 a) setrlimit with RLIMIT_DATA works.
72 b) mquery() can be used to find out about the virtual memory areas.
73 get_rusage_data_via_setrlimit() works much better than
74 get_rusage_data_via_iterator().
75 Note that malloc() appears to use mmap() for both large and small
79 a) setrlimit with RLIMIT_DATA works.
80 b) No VMA iteration API exists.
83 a) setrlimit with RLIMIT_DATA works, except on HP-UX 11.00, where it
84 cannot restore the previous limits, and except on HP-UX 11.11, where
85 it sometimes has no effect.
86 b) pstat_getprocvm() can be used to find out about the virtual memory
88 Both methods agree, except that the value of get_rusage_data_via_iterator()
89 is sometimes 4 KB larger than get_rusage_data_via_setrlimit().
92 a) setrlimit with RLIMIT_DATA works.
93 b) The /proc/$pid file supports ioctls PIOCNMAP and PIOCMAP.
94 get_rusage_data_via_setrlimit() works slightly better than
95 get_rusage_data_via_iterator() before the first malloc() call.
98 a) setrlimit with RLIMIT_DATA works.
99 b) The /proc/$pid file supports ioctls PIOCNMAP and PIOCMAP.
103 a) setrlimit with RLIMIT_DATA works.
104 b) The /proc/$pid file supports ioctls PIOCNMAP and PIOCMAP, and the
105 /proc/self/maps file contains a list of the virtual memory areas.
106 get_rusage_data_via_setrlimit() ignores the data segment of the executable,
107 whereas get_rusage_data_via_iterator() includes it.
110 a) setrlimit with RLIMIT_DATA always fails.
111 get_rusage_data_via_setrlimit() therefore produces a wrong value.
112 b) The /proc/$pid/maps file lists only the memory areas belonging to
113 the executable and shared libraries, not the anonymous memory.
114 But the native Windows API works.
115 Note that malloc() apparently falls back on mmap() for large allocations.
118 a) There is no setrlimit function.
119 b) There is no sbrk() function.
120 Note that malloc() falls back on VirtualAlloc() for large allocations.
123 a) On BeOS, there is no setrlimit function.
124 On Haiku, setrlimit exists. RLIMIT_DATA is defined but setrlimit fails.
125 b) There is a specific BeOS API: get_next_area_info().
129 #include <errno.h> /* errno */
130 #include <stdlib.h> /* size_t, abort, malloc, free, sbrk */
131 #include <fcntl.h> /* open, O_RDONLY */
132 #include <unistd.h> /* getpagesize, read, close */
135 /* System support for get_rusage_data_via_setrlimit(). */
138 # include <sys/time.h>
139 # include <sys/resource.h> /* getrlimit, setrlimit */
140 # include <sys/utsname.h>
141 # include <string.h> /* strlen, strcmp */
145 /* System support for get_rusage_data_via_iterator(). */
147 #include "vma-iter.h"
150 #if !(defined __APPLE__ && defined __MACH__) || defined TEST
151 /* Implement get_rusage_data_via_setrlimit(). */
153 # if HAVE_SETRLIMIT && defined RLIMIT_DATA && !defined __HAIKU__
156 # define errno_expected() (errno == EINVAL || errno == EFAULT)
158 # define errno_expected() (errno == EINVAL)
162 get_rusage_data_via_setrlimit (void)
166 struct rlimit orig_limit
;
169 /* On HP-UX 11.00, setrlimit() RLIMIT_DATA of does not work: It cannot
170 restore the previous limits.
171 On HP-UX 11.11, setrlimit() RLIMIT_DATA of does not work: It sometimes
172 has no effect on the next sbrk() call. */
176 if (uname (&buf
) == 0
177 && strlen (buf
.release
) >= 5
178 && (strcmp (buf
.release
+ strlen (buf
.release
) - 5, "11.00") == 0
179 || strcmp (buf
.release
+ strlen (buf
.release
) - 5, "11.11") == 0))
184 /* Record the original limit. */
185 if (getrlimit (RLIMIT_DATA
, &orig_limit
) < 0)
188 if (orig_limit
.rlim_max
!= RLIM_INFINITY
189 && (orig_limit
.rlim_cur
== RLIM_INFINITY
190 || orig_limit
.rlim_cur
> orig_limit
.rlim_max
))
191 /* We may not be able to restore the current rlim_cur value.
196 /* The granularity is a single page. */
197 const intptr_t pagesize
= getpagesize ();
199 uintptr_t low_bound
= 0;
200 uintptr_t high_bound
;
204 /* Here we know that the data segment size is >= low_bound. */
205 struct rlimit try_limit
;
206 uintptr_t try_next
= 2 * low_bound
+ pagesize
;
208 if (try_next
< low_bound
)
210 try_next
= ((uintptr_t) (~ 0) / pagesize
) * pagesize
;
212 /* There's no point in trying a value > orig_limit.rlim_max, as
213 setrlimit would fail anyway. */
214 if (orig_limit
.rlim_max
!= RLIM_INFINITY
215 && orig_limit
.rlim_max
< try_next
)
216 try_next
= orig_limit
.rlim_max
;
218 /* Avoid endless loop. */
219 if (try_next
== low_bound
)
221 /* try_next could not be increased. */
226 try_limit
.rlim_max
= orig_limit
.rlim_max
;
227 try_limit
.rlim_cur
= try_next
;
228 if (setrlimit (RLIMIT_DATA
, &try_limit
) == 0)
230 /* Allocate a page of memory, to compare the current data segment
231 size with try_limit.rlim_cur. */
232 void *new_page
= sbrk (pagesize
);
234 if (new_page
!= (void *)(-1))
236 /* The page could be added successfully. Free it. */
238 /* We know that the data segment size is
239 < try_limit.rlim_cur. */
240 high_bound
= try_next
;
245 /* We know that the data segment size is
246 >= try_limit.rlim_cur. */
247 low_bound
= try_next
;
252 /* Here we expect only EINVAL or (on AIX) EFAULT, not EPERM. */
253 if (! errno_expected ())
255 /* We know that the data segment size is
256 >= try_limit.rlim_cur. */
257 low_bound
= try_next
;
261 /* Here we know that the data segment size is
262 >= low_bound and < high_bound. */
263 while (high_bound
- low_bound
> pagesize
)
265 struct rlimit try_limit
;
267 low_bound
+ (((high_bound
- low_bound
) / 2) / pagesize
) * pagesize
;
269 /* Here low_bound <= try_next < high_bound. */
270 try_limit
.rlim_max
= orig_limit
.rlim_max
;
271 try_limit
.rlim_cur
= try_next
;
272 if (setrlimit (RLIMIT_DATA
, &try_limit
) == 0)
274 /* Allocate a page of memory, to compare the current data segment
275 size with try_limit.rlim_cur. */
276 void *new_page
= sbrk (pagesize
);
278 if (new_page
!= (void *)(-1))
280 /* The page could be added successfully. Free it. */
282 /* We know that the data segment size is
283 < try_limit.rlim_cur. */
284 high_bound
= try_next
;
288 /* We know that the data segment size is
289 >= try_limit.rlim_cur. */
290 low_bound
= try_next
;
295 /* Here we expect only EINVAL or (on AIX) EFAULT, not EPERM. */
296 if (! errno_expected ())
298 /* We know that the data segment size is
299 >= try_limit.rlim_cur. */
300 low_bound
= try_next
;
308 /* Restore the original rlim_cur value. */
309 if (setrlimit (RLIMIT_DATA
, &orig_limit
) < 0)
318 get_rusage_data_via_setrlimit (void)
328 #if !(defined __APPLE__ && defined __MACH__) || defined TEST
329 /* Implement get_rusage_data_via_iterator(). */
331 # if VMA_ITERATE_SUPPORTED
336 uintptr_t data_segment_size
;
340 vma_iterate_callback (void *data
, uintptr_t start
, uintptr_t end
,
343 struct locals
*lp
= (struct locals
*) data
;
345 if (start
<= lp
->brk_value
&& lp
->brk_value
- 1 <= end
- 1)
347 lp
->data_segment_size
= end
- start
;
354 get_rusage_data_via_iterator (void)
356 # if (defined _WIN32 && !defined __CYGWIN__) || defined __BEOS__ || defined __HAIKU__
357 /* On native Windows, there is no sbrk() function.
358 On Haiku, sbrk(0) always returns 0. */
359 static void *brk_value
;
361 if (brk_value
== NULL
)
363 brk_value
= malloc (1);
364 if (brk_value
== NULL
)
370 brk_value
= sbrk (0);
371 if (brk_value
== (void *)-1)
378 l
.brk_value
= (uintptr_t) brk_value
;
379 l
.data_segment_size
= 0;
380 vma_iterate (vma_iterate_callback
, &l
);
382 return l
.data_segment_size
;
389 get_rusage_data_via_iterator (void)
400 get_rusage_data (void)
402 #if (defined __APPLE__ && defined __MACH__) /* Mac OS X */
403 /* get_rusage_data_via_setrlimit() does not work: it always returns 0.
404 get_rusage_data_via_iterator() does not work: it always returns 0x400000.
405 And sbrk() is deprecated. */
407 #elif defined __minix /* Minix */
408 /* get_rusage_data_via_setrlimit() does not work: it always returns 0.
409 get_rusage_data_via_iterator() does not work: it shrinks upon malloc. */
411 #elif defined __CYGWIN__ /* Cygwin */
412 /* get_rusage_data_via_setrlimit() does not work.
413 Prefer get_rusage_data_via_iterator(). */
414 return get_rusage_data_via_iterator ();
415 #elif HAVE_SETRLIMIT && defined RLIMIT_DATA && !defined __HAIKU__
416 # if defined __linux__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined _AIX || defined __hpux || defined __sgi || defined __osf__ || defined __sun /* Linux, FreeBSD, NetBSD, OpenBSD, AIX, HP-UX, IRIX, OSF/1, Solaris */
417 /* get_rusage_data_via_setrlimit() works. */
418 return get_rusage_data_via_setrlimit ();
420 /* Prefer get_rusage_data_via_setrlimit() if it succeeds,
421 because the caller may want to use the result with setrlimit(). */
424 result
= get_rusage_data_via_setrlimit ();
426 result
= get_rusage_data_via_iterator ();
430 return get_rusage_data_via_iterator ();
442 printf ("Initially: 0x%08lX 0x%08lX 0x%08lX\n",
443 get_rusage_data_via_setrlimit (), get_rusage_data_via_iterator (),
446 printf ("After small malloc: 0x%08lX 0x%08lX 0x%08lX\n",
447 get_rusage_data_via_setrlimit (), get_rusage_data_via_iterator (),
450 printf ("After medium malloc: 0x%08lX 0x%08lX 0x%08lX\n",
451 get_rusage_data_via_setrlimit (), get_rusage_data_via_iterator (),
454 printf ("After large malloc: 0x%08lX 0x%08lX 0x%08lX\n",
455 get_rusage_data_via_setrlimit (), get_rusage_data_via_iterator (),