2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ggc-common.c
blob17c1f50b608fc82d81f3a9e0eb028da3283b1a61
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Generic garbage collection (GC) functions and data, not specific to
22 any particular GC implementation. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "hashtab.h"
28 #include "ggc.h"
29 #include "toplev.h"
30 #include "params.h"
31 #include "hosthooks.h"
32 #include "hosthooks-def.h"
34 #ifdef HAVE_SYS_RESOURCE_H
35 # include <sys/resource.h>
36 #endif
38 #ifdef HAVE_MMAP_FILE
39 # include <sys/mman.h>
40 # ifdef HAVE_MINCORE
41 /* This is on Solaris. */
42 # include <sys/types.h>
43 # endif
44 #endif
46 #ifndef MAP_FAILED
47 # define MAP_FAILED ((void *)-1)
48 #endif
50 /* When set, ggc_collect will do collection. */
51 bool ggc_force_collect;
53 /* When true, protect the contents of the identifier hash table. */
54 bool ggc_protect_identifiers = true;
56 /* Statistics about the allocation. */
57 static ggc_statistics *ggc_stats;
59 struct traversal_state;
61 static int ggc_htab_delete (void **, void *);
62 static hashval_t saving_htab_hash (const void *);
63 static int saving_htab_eq (const void *, const void *);
64 static int call_count (void **, void *);
65 static int call_alloc (void **, void *);
66 static int compare_ptr_data (const void *, const void *);
67 static void relocate_ptrs (void *, void *);
68 static void write_pch_globals (const struct ggc_root_tab * const *tab,
69 struct traversal_state *state);
70 static double ggc_rlimit_bound (double);
72 /* Maintain global roots that are preserved during GC. */
74 /* Process a slot of an htab by deleting it if it has not been marked. */
76 static int
77 ggc_htab_delete (void **slot, void *info)
79 const struct ggc_cache_tab *r = (const struct ggc_cache_tab *) info;
81 if (! (*r->marked_p) (*slot))
82 htab_clear_slot (*r->base, slot);
83 else
84 (*r->cb) (*slot);
86 return 1;
89 /* Iterate through all registered roots and mark each element. */
91 void
92 ggc_mark_roots (void)
94 const struct ggc_root_tab *const *rt;
95 const struct ggc_root_tab *rti;
96 const struct ggc_cache_tab *const *ct;
97 const struct ggc_cache_tab *cti;
98 size_t i;
100 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
101 for (rti = *rt; rti->base != NULL; rti++)
102 memset (rti->base, 0, rti->stride);
104 for (rt = gt_ggc_rtab; *rt; rt++)
105 for (rti = *rt; rti->base != NULL; rti++)
106 for (i = 0; i < rti->nelt; i++)
107 (*rti->cb)(*(void **)((char *)rti->base + rti->stride * i));
109 if (ggc_protect_identifiers)
110 ggc_mark_stringpool ();
112 /* Now scan all hash tables that have objects which are to be deleted if
113 they are not already marked. */
114 for (ct = gt_ggc_cache_rtab; *ct; ct++)
115 for (cti = *ct; cti->base != NULL; cti++)
116 if (*cti->base)
118 ggc_set_mark (*cti->base);
119 htab_traverse_noresize (*cti->base, ggc_htab_delete, (void *) cti);
120 ggc_set_mark ((*cti->base)->entries);
123 if (! ggc_protect_identifiers)
124 ggc_purge_stringpool ();
127 /* Allocate a block of memory, then clear it. */
128 void *
129 ggc_alloc_cleared_stat (size_t size MEM_STAT_DECL)
131 void *buf = ggc_alloc_stat (size PASS_MEM_STAT);
132 memset (buf, 0, size);
133 return buf;
136 /* Resize a block of memory, possibly re-allocating it. */
137 void *
138 ggc_realloc_stat (void *x, size_t size MEM_STAT_DECL)
140 void *r;
141 size_t old_size;
143 if (x == NULL)
144 return ggc_alloc_stat (size PASS_MEM_STAT);
146 old_size = ggc_get_size (x);
148 if (size <= old_size)
150 /* Mark the unwanted memory as unaccessible. We also need to make
151 the "new" size accessible, since ggc_get_size returns the size of
152 the pool, not the size of the individually allocated object, the
153 size which was previously made accessible. Unfortunately, we
154 don't know that previously allocated size. Without that
155 knowledge we have to lose some initialization-tracking for the
156 old parts of the object. An alternative is to mark the whole
157 old_size as reachable, but that would lose tracking of writes
158 after the end of the object (by small offsets). Discard the
159 handle to avoid handle leak. */
160 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size,
161 old_size - size));
162 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size));
163 return x;
166 r = ggc_alloc_stat (size PASS_MEM_STAT);
168 /* Since ggc_get_size returns the size of the pool, not the size of the
169 individually allocated object, we'd access parts of the old object
170 that were marked invalid with the memcpy below. We lose a bit of the
171 initialization-tracking since some of it may be uninitialized. */
172 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size));
174 memcpy (r, x, old_size);
176 /* The old object is not supposed to be used anymore. */
177 ggc_free (x);
179 return r;
182 /* Like ggc_alloc_cleared, but performs a multiplication. */
183 void *
184 ggc_calloc (size_t s1, size_t s2)
186 return ggc_alloc_cleared (s1 * s2);
189 /* These are for splay_tree_new_ggc. */
190 void *
191 ggc_splay_alloc (int sz, void *nl)
193 gcc_assert (!nl);
194 return ggc_alloc (sz);
197 void
198 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl)
200 gcc_assert (!nl);
203 /* Print statistics that are independent of the collector in use. */
204 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
205 ? (x) \
206 : ((x) < 1024*1024*10 \
207 ? (x) / 1024 \
208 : (x) / (1024*1024))))
209 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
211 void
212 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED,
213 ggc_statistics *stats)
215 /* Set the pointer so that during collection we will actually gather
216 the statistics. */
217 ggc_stats = stats;
219 /* Then do one collection to fill in the statistics. */
220 ggc_collect ();
222 /* At present, we don't really gather any interesting statistics. */
224 /* Don't gather statistics any more. */
225 ggc_stats = NULL;
228 /* Functions for saving and restoring GCable memory to disk. */
230 static htab_t saving_htab;
232 struct ptr_data
234 void *obj;
235 void *note_ptr_cookie;
236 gt_note_pointers note_ptr_fn;
237 gt_handle_reorder reorder_fn;
238 size_t size;
239 void *new_addr;
240 enum gt_types_enum type;
243 #define POINTER_HASH(x) (hashval_t)((long)x >> 3)
245 /* Register an object in the hash table. */
248 gt_pch_note_object (void *obj, void *note_ptr_cookie,
249 gt_note_pointers note_ptr_fn,
250 enum gt_types_enum type)
252 struct ptr_data **slot;
254 if (obj == NULL || obj == (void *) 1)
255 return 0;
257 slot = (struct ptr_data **)
258 htab_find_slot_with_hash (saving_htab, obj, POINTER_HASH (obj),
259 INSERT);
260 if (*slot != NULL)
262 gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn
263 && (*slot)->note_ptr_cookie == note_ptr_cookie);
264 return 0;
267 *slot = xcalloc (sizeof (struct ptr_data), 1);
268 (*slot)->obj = obj;
269 (*slot)->note_ptr_fn = note_ptr_fn;
270 (*slot)->note_ptr_cookie = note_ptr_cookie;
271 if (note_ptr_fn == gt_pch_p_S)
272 (*slot)->size = strlen (obj) + 1;
273 else
274 (*slot)->size = ggc_get_size (obj);
275 (*slot)->type = type;
276 return 1;
279 /* Register an object in the hash table. */
281 void
282 gt_pch_note_reorder (void *obj, void *note_ptr_cookie,
283 gt_handle_reorder reorder_fn)
285 struct ptr_data *data;
287 if (obj == NULL || obj == (void *) 1)
288 return;
290 data = htab_find_with_hash (saving_htab, obj, POINTER_HASH (obj));
291 gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie);
293 data->reorder_fn = reorder_fn;
296 /* Hash and equality functions for saving_htab, callbacks for htab_create. */
298 static hashval_t
299 saving_htab_hash (const void *p)
301 return POINTER_HASH (((const struct ptr_data *)p)->obj);
304 static int
305 saving_htab_eq (const void *p1, const void *p2)
307 return ((const struct ptr_data *)p1)->obj == p2;
310 /* Handy state for the traversal functions. */
312 struct traversal_state
314 FILE *f;
315 struct ggc_pch_data *d;
316 size_t count;
317 struct ptr_data **ptrs;
318 size_t ptrs_i;
321 /* Callbacks for htab_traverse. */
323 static int
324 call_count (void **slot, void *state_p)
326 struct ptr_data *d = (struct ptr_data *)*slot;
327 struct traversal_state *state = (struct traversal_state *)state_p;
329 ggc_pch_count_object (state->d, d->obj, d->size,
330 d->note_ptr_fn == gt_pch_p_S,
331 d->type);
332 state->count++;
333 return 1;
336 static int
337 call_alloc (void **slot, void *state_p)
339 struct ptr_data *d = (struct ptr_data *)*slot;
340 struct traversal_state *state = (struct traversal_state *)state_p;
342 d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size,
343 d->note_ptr_fn == gt_pch_p_S,
344 d->type);
345 state->ptrs[state->ptrs_i++] = d;
346 return 1;
349 /* Callback for qsort. */
351 static int
352 compare_ptr_data (const void *p1_p, const void *p2_p)
354 const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p;
355 const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p;
356 return (((size_t)p1->new_addr > (size_t)p2->new_addr)
357 - ((size_t)p1->new_addr < (size_t)p2->new_addr));
360 /* Callbacks for note_ptr_fn. */
362 static void
363 relocate_ptrs (void *ptr_p, void *state_p)
365 void **ptr = (void **)ptr_p;
366 struct traversal_state *state ATTRIBUTE_UNUSED
367 = (struct traversal_state *)state_p;
368 struct ptr_data *result;
370 if (*ptr == NULL || *ptr == (void *)1)
371 return;
373 result = htab_find_with_hash (saving_htab, *ptr, POINTER_HASH (*ptr));
374 gcc_assert (result);
375 *ptr = result->new_addr;
378 /* Write out, after relocation, the pointers in TAB. */
379 static void
380 write_pch_globals (const struct ggc_root_tab * const *tab,
381 struct traversal_state *state)
383 const struct ggc_root_tab *const *rt;
384 const struct ggc_root_tab *rti;
385 size_t i;
387 for (rt = tab; *rt; rt++)
388 for (rti = *rt; rti->base != NULL; rti++)
389 for (i = 0; i < rti->nelt; i++)
391 void *ptr = *(void **)((char *)rti->base + rti->stride * i);
392 struct ptr_data *new_ptr;
393 if (ptr == NULL || ptr == (void *)1)
395 if (fwrite (&ptr, sizeof (void *), 1, state->f)
396 != 1)
397 fatal_error ("can't write PCH file: %m");
399 else
401 new_ptr = htab_find_with_hash (saving_htab, ptr,
402 POINTER_HASH (ptr));
403 if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f)
404 != 1)
405 fatal_error ("can't write PCH file: %m");
410 /* Hold the information we need to mmap the file back in. */
412 struct mmap_info
414 size_t offset;
415 size_t size;
416 void *preferred_base;
419 /* Write out the state of the compiler to F. */
421 void
422 gt_pch_save (FILE *f)
424 const struct ggc_root_tab *const *rt;
425 const struct ggc_root_tab *rti;
426 size_t i;
427 struct traversal_state state;
428 char *this_object = NULL;
429 size_t this_object_size = 0;
430 struct mmap_info mmi;
431 const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity();
433 gt_pch_save_stringpool ();
435 saving_htab = htab_create (50000, saving_htab_hash, saving_htab_eq, free);
437 for (rt = gt_ggc_rtab; *rt; rt++)
438 for (rti = *rt; rti->base != NULL; rti++)
439 for (i = 0; i < rti->nelt; i++)
440 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
442 for (rt = gt_pch_cache_rtab; *rt; rt++)
443 for (rti = *rt; rti->base != NULL; rti++)
444 for (i = 0; i < rti->nelt; i++)
445 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
447 /* Prepare the objects for writing, determine addresses and such. */
448 state.f = f;
449 state.d = init_ggc_pch();
450 state.count = 0;
451 htab_traverse (saving_htab, call_count, &state);
453 mmi.size = ggc_pch_total_size (state.d);
455 /* Try to arrange things so that no relocation is necessary, but
456 don't try very hard. On most platforms, this will always work,
457 and on the rest it's a lot of work to do better.
458 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
459 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
460 mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f));
462 ggc_pch_this_base (state.d, mmi.preferred_base);
464 state.ptrs = XNEWVEC (struct ptr_data *, state.count);
465 state.ptrs_i = 0;
466 htab_traverse (saving_htab, call_alloc, &state);
467 qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data);
469 /* Write out all the scalar variables. */
470 for (rt = gt_pch_scalar_rtab; *rt; rt++)
471 for (rti = *rt; rti->base != NULL; rti++)
472 if (fwrite (rti->base, rti->stride, 1, f) != 1)
473 fatal_error ("can't write PCH file: %m");
475 /* Write out all the global pointers, after translation. */
476 write_pch_globals (gt_ggc_rtab, &state);
477 write_pch_globals (gt_pch_cache_rtab, &state);
479 /* Pad the PCH file so that the mmapped area starts on an allocation
480 granularity (usually page) boundary. */
482 long o;
483 o = ftell (state.f) + sizeof (mmi);
484 if (o == -1)
485 fatal_error ("can't get position in PCH file: %m");
486 mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment;
487 if (mmi.offset == mmap_offset_alignment)
488 mmi.offset = 0;
489 mmi.offset += o;
491 if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1)
492 fatal_error ("can't write PCH file: %m");
493 if (mmi.offset != 0
494 && fseek (state.f, mmi.offset, SEEK_SET) != 0)
495 fatal_error ("can't write padding to PCH file: %m");
497 ggc_pch_prepare_write (state.d, state.f);
499 /* Actually write out the objects. */
500 for (i = 0; i < state.count; i++)
502 if (this_object_size < state.ptrs[i]->size)
504 this_object_size = state.ptrs[i]->size;
505 this_object = xrealloc (this_object, this_object_size);
507 memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size);
508 if (state.ptrs[i]->reorder_fn != NULL)
509 state.ptrs[i]->reorder_fn (state.ptrs[i]->obj,
510 state.ptrs[i]->note_ptr_cookie,
511 relocate_ptrs, &state);
512 state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj,
513 state.ptrs[i]->note_ptr_cookie,
514 relocate_ptrs, &state);
515 ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj,
516 state.ptrs[i]->new_addr, state.ptrs[i]->size,
517 state.ptrs[i]->note_ptr_fn == gt_pch_p_S);
518 if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S)
519 memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size);
521 ggc_pch_finish (state.d, state.f);
522 gt_pch_fixup_stringpool ();
524 free (state.ptrs);
525 htab_delete (saving_htab);
528 /* Read the state of the compiler back in from F. */
530 void
531 gt_pch_restore (FILE *f)
533 const struct ggc_root_tab *const *rt;
534 const struct ggc_root_tab *rti;
535 size_t i;
536 struct mmap_info mmi;
537 int result;
539 /* Delete any deletable objects. This makes ggc_pch_read much
540 faster, as it can be sure that no GCable objects remain other
541 than the ones just read in. */
542 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
543 for (rti = *rt; rti->base != NULL; rti++)
544 memset (rti->base, 0, rti->stride);
546 /* Read in all the scalar variables. */
547 for (rt = gt_pch_scalar_rtab; *rt; rt++)
548 for (rti = *rt; rti->base != NULL; rti++)
549 if (fread (rti->base, rti->stride, 1, f) != 1)
550 fatal_error ("can't read PCH file: %m");
552 /* Read in all the global pointers, in 6 easy loops. */
553 for (rt = gt_ggc_rtab; *rt; rt++)
554 for (rti = *rt; rti->base != NULL; rti++)
555 for (i = 0; i < rti->nelt; i++)
556 if (fread ((char *)rti->base + rti->stride * i,
557 sizeof (void *), 1, f) != 1)
558 fatal_error ("can't read PCH file: %m");
560 for (rt = gt_pch_cache_rtab; *rt; rt++)
561 for (rti = *rt; rti->base != NULL; rti++)
562 for (i = 0; i < rti->nelt; i++)
563 if (fread ((char *)rti->base + rti->stride * i,
564 sizeof (void *), 1, f) != 1)
565 fatal_error ("can't read PCH file: %m");
567 if (fread (&mmi, sizeof (mmi), 1, f) != 1)
568 fatal_error ("can't read PCH file: %m");
570 result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size,
571 fileno (f), mmi.offset);
572 if (result < 0)
573 fatal_error ("had to relocate PCH");
574 if (result == 0)
576 if (fseek (f, mmi.offset, SEEK_SET) != 0
577 || fread (mmi.preferred_base, mmi.size, 1, f) != 1)
578 fatal_error ("can't read PCH file: %m");
580 else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0)
581 fatal_error ("can't read PCH file: %m");
583 ggc_pch_read (f, mmi.preferred_base);
585 gt_pch_restore_stringpool ();
588 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
589 Select no address whatsoever, and let gt_pch_save choose what it will with
590 malloc, presumably. */
592 void *
593 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED,
594 int fd ATTRIBUTE_UNUSED)
596 return NULL;
599 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
600 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
601 same as base, indicating that the memory has been allocated but needs to
602 be read in from the file. Return -1 if the address differs, to relocation
603 of the PCH file would be required. */
606 default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED,
607 size_t offset ATTRIBUTE_UNUSED)
609 void *addr = xmalloc (size);
610 return (addr == base) - 1;
613 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
614 alignment required for allocating virtual memory. Usually this is the
615 same as pagesize. */
617 size_t
618 default_gt_pch_alloc_granularity (void)
620 return getpagesize();
623 #if HAVE_MMAP_FILE
624 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
625 We temporarily allocate SIZE bytes, and let the kernel place the data
626 wherever it will. If it worked, that's our spot, if not we're likely
627 to be in trouble. */
629 void *
630 mmap_gt_pch_get_address (size_t size, int fd)
632 void *ret;
634 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
635 if (ret == (void *) MAP_FAILED)
636 ret = NULL;
637 else
638 munmap (ret, size);
640 return ret;
643 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
644 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
645 mapping the data at BASE, -1 if we couldn't.
647 This version assumes that the kernel honors the START operand of mmap
648 even without MAP_FIXED if START through START+SIZE are not currently
649 mapped with something. */
652 mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
654 void *addr;
656 /* We're called with size == 0 if we're not planning to load a PCH
657 file at all. This allows the hook to free any static space that
658 we might have allocated at link time. */
659 if (size == 0)
660 return -1;
662 addr = mmap (base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
663 fd, offset);
665 return addr == base ? 1 : -1;
667 #endif /* HAVE_MMAP_FILE */
669 /* Modify the bound based on rlimits. */
670 static double
671 ggc_rlimit_bound (double limit)
673 #if defined(HAVE_GETRLIMIT)
674 struct rlimit rlim;
675 # if defined (RLIMIT_AS)
676 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
677 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
678 if (getrlimit (RLIMIT_AS, &rlim) == 0
679 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
680 && rlim.rlim_cur < limit)
681 limit = rlim.rlim_cur;
682 # elif defined (RLIMIT_DATA)
683 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
684 might be on an OS that has a broken mmap. (Others don't bound
685 mmap at all, apparently.) */
686 if (getrlimit (RLIMIT_DATA, &rlim) == 0
687 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
688 && rlim.rlim_cur < limit
689 /* Darwin has this horribly bogus default setting of
690 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
691 appears to be ignored. Ignore such silliness. If a limit
692 this small was actually effective for mmap, GCC wouldn't even
693 start up. */
694 && rlim.rlim_cur >= 8 * 1024 * 1024)
695 limit = rlim.rlim_cur;
696 # endif /* RLIMIT_AS or RLIMIT_DATA */
697 #endif /* HAVE_GETRLIMIT */
699 return limit;
702 /* Heuristic to set a default for GGC_MIN_EXPAND. */
704 ggc_min_expand_heuristic (void)
706 double min_expand = physmem_total();
708 /* Adjust for rlimits. */
709 min_expand = ggc_rlimit_bound (min_expand);
711 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
712 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
713 min_expand /= 1024*1024*1024;
714 min_expand *= 70;
715 min_expand = MIN (min_expand, 70);
716 min_expand += 30;
718 return min_expand;
721 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
723 ggc_min_heapsize_heuristic (void)
725 double phys_kbytes = physmem_total();
726 double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2);
728 phys_kbytes /= 1024; /* Convert to Kbytes. */
729 limit_kbytes /= 1024;
731 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
732 bound of 128M (when RAM >= 1GB). */
733 phys_kbytes /= 8;
735 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
736 /* Try not to overrun the RSS limit while doing garbage collection.
737 The RSS limit is only advisory, so no margin is subtracted. */
739 struct rlimit rlim;
740 if (getrlimit (RLIMIT_RSS, &rlim) == 0
741 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
742 phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / 1024);
744 # endif
746 /* Don't blindly run over our data limit; do GC at least when the
747 *next* GC would be within 20Mb of the limit or within a quarter of
748 the limit, whichever is larger. If GCC does hit the data limit,
749 compilation will fail, so this tries to be conservative. */
750 limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * 1024));
751 limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic());
752 phys_kbytes = MIN (phys_kbytes, limit_kbytes);
754 phys_kbytes = MAX (phys_kbytes, 4 * 1024);
755 phys_kbytes = MIN (phys_kbytes, 128 * 1024);
757 return phys_kbytes;
760 void
761 init_ggc_heuristics (void)
763 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
764 set_param_value ("ggc-min-expand", ggc_min_expand_heuristic());
765 set_param_value ("ggc-min-heapsize", ggc_min_heapsize_heuristic());
766 #endif
769 #ifdef GATHER_STATISTICS
771 /* Datastructure used to store per-call-site statistics. */
772 struct loc_descriptor
774 const char *file;
775 int line;
776 const char *function;
777 int times;
778 size_t allocated;
779 size_t overhead;
780 size_t freed;
781 size_t collected;
784 /* Hashtable used for statistics. */
785 static htab_t loc_hash;
787 /* Hash table helpers functions. */
788 static hashval_t
789 hash_descriptor (const void *p)
791 const struct loc_descriptor *const d = p;
793 return htab_hash_pointer (d->function) | d->line;
796 static int
797 eq_descriptor (const void *p1, const void *p2)
799 const struct loc_descriptor *const d = p1;
800 const struct loc_descriptor *const d2 = p2;
802 return (d->file == d2->file && d->line == d2->line
803 && d->function == d2->function);
806 /* Hashtable converting address of allocated field to loc descriptor. */
807 static htab_t ptr_hash;
808 struct ptr_hash_entry
810 void *ptr;
811 struct loc_descriptor *loc;
812 size_t size;
815 /* Hash table helpers functions. */
816 static hashval_t
817 hash_ptr (const void *p)
819 const struct ptr_hash_entry *const d = p;
821 return htab_hash_pointer (d->ptr);
824 static int
825 eq_ptr (const void *p1, const void *p2)
827 const struct ptr_hash_entry *const p = p1;
829 return (p->ptr == p2);
832 /* Return descriptor for given call site, create new one if needed. */
833 static struct loc_descriptor *
834 loc_descriptor (const char *name, int line, const char *function)
836 struct loc_descriptor loc;
837 struct loc_descriptor **slot;
839 loc.file = name;
840 loc.line = line;
841 loc.function = function;
842 if (!loc_hash)
843 loc_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
845 slot = (struct loc_descriptor **) htab_find_slot (loc_hash, &loc, 1);
846 if (*slot)
847 return *slot;
848 *slot = xcalloc (sizeof (**slot), 1);
849 (*slot)->file = name;
850 (*slot)->line = line;
851 (*slot)->function = function;
852 return *slot;
855 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
856 void
857 ggc_record_overhead (size_t allocated, size_t overhead, void *ptr,
858 const char *name, int line, const char *function)
860 struct loc_descriptor *loc = loc_descriptor (name, line, function);
861 struct ptr_hash_entry *p = XNEW (struct ptr_hash_entry);
862 PTR *slot;
864 p->ptr = ptr;
865 p->loc = loc;
866 p->size = allocated + overhead;
867 if (!ptr_hash)
868 ptr_hash = htab_create (10, hash_ptr, eq_ptr, NULL);
869 slot = htab_find_slot_with_hash (ptr_hash, ptr, htab_hash_pointer (ptr), INSERT);
870 gcc_assert (!*slot);
871 *slot = p;
873 loc->times++;
874 loc->allocated+=allocated;
875 loc->overhead+=overhead;
878 /* Helper function for prune_overhead_list. See if SLOT is still marked and
879 remove it from hashtable if it is not. */
880 static int
881 ggc_prune_ptr (void **slot, void *b ATTRIBUTE_UNUSED)
883 struct ptr_hash_entry *p = *slot;
884 if (!ggc_marked_p (p->ptr))
886 p->loc->collected += p->size;
887 htab_clear_slot (ptr_hash, slot);
888 free (p);
890 return 1;
893 /* After live values has been marked, walk all recorded pointers and see if
894 they are still live. */
895 void
896 ggc_prune_overhead_list (void)
898 htab_traverse (ptr_hash, ggc_prune_ptr, NULL);
901 /* Notice that the pointer has been freed. */
902 void
903 ggc_free_overhead (void *ptr)
905 PTR *slot = htab_find_slot_with_hash (ptr_hash, ptr, htab_hash_pointer (ptr),
906 NO_INSERT);
907 struct ptr_hash_entry *p = *slot;
908 p->loc->freed += p->size;
909 htab_clear_slot (ptr_hash, slot);
910 free (p);
913 /* Helper for qsort; sort descriptors by amount of memory consumed. */
914 static int
915 final_cmp_statistic (const void *loc1, const void *loc2)
917 struct loc_descriptor *l1 = *(struct loc_descriptor **) loc1;
918 struct loc_descriptor *l2 = *(struct loc_descriptor **) loc2;
919 long diff;
920 diff = ((long)(l1->allocated + l1->overhead - l1->freed) -
921 (l2->allocated + l2->overhead - l2->freed));
922 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
925 /* Helper for qsort; sort descriptors by amount of memory consumed. */
926 static int
927 cmp_statistic (const void *loc1, const void *loc2)
929 struct loc_descriptor *l1 = *(struct loc_descriptor **) loc1;
930 struct loc_descriptor *l2 = *(struct loc_descriptor **) loc2;
931 long diff;
933 diff = ((long)(l1->allocated + l1->overhead - l1->freed - l1->collected) -
934 (l2->allocated + l2->overhead - l2->freed - l2->collected));
935 if (diff)
936 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
937 diff = ((long)(l1->allocated + l1->overhead - l1->freed) -
938 (l2->allocated + l2->overhead - l2->freed));
939 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
942 /* Collect array of the descriptors from hashtable. */
943 struct loc_descriptor **loc_array;
944 static int
945 add_statistics (void **slot, void *b)
947 int *n = (int *)b;
948 loc_array[*n] = (struct loc_descriptor *) *slot;
949 (*n)++;
950 return 1;
953 /* Dump per-site memory statistics. */
954 #endif
955 void
956 dump_ggc_loc_statistics (bool final ATTRIBUTE_UNUSED)
958 #ifdef GATHER_STATISTICS
959 int nentries = 0;
960 char s[4096];
961 size_t collected = 0, freed = 0, allocated = 0, overhead = 0, times = 0;
962 int i;
964 ggc_force_collect = true;
965 ggc_collect ();
967 loc_array = xcalloc (sizeof (*loc_array), loc_hash->n_elements);
968 fprintf (stderr, "-------------------------------------------------------\n");
969 fprintf (stderr, "\n%-48s %10s %10s %10s %10s %10s\n",
970 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times");
971 fprintf (stderr, "-------------------------------------------------------\n");
972 htab_traverse (loc_hash, add_statistics, &nentries);
973 qsort (loc_array, nentries, sizeof (*loc_array),
974 final ? final_cmp_statistic : cmp_statistic);
975 for (i = 0; i < nentries; i++)
977 struct loc_descriptor *d = loc_array[i];
978 allocated += d->allocated;
979 times += d->times;
980 freed += d->freed;
981 collected += d->collected;
982 overhead += d->overhead;
984 for (i = 0; i < nentries; i++)
986 struct loc_descriptor *d = loc_array[i];
987 if (d->allocated)
989 const char *s1 = d->file;
990 const char *s2;
991 while ((s2 = strstr (s1, "gcc/")))
992 s1 = s2 + 4;
993 sprintf (s, "%s:%i (%s)", s1, d->line, d->function);
994 s[48] = 0;
995 fprintf (stderr, "%-48s %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li\n", s,
996 (long)d->collected,
997 (d->collected) * 100.0 / collected,
998 (long)d->freed,
999 (d->freed) * 100.0 / freed,
1000 (long)(d->allocated + d->overhead - d->freed - d->collected),
1001 (d->allocated + d->overhead - d->freed - d->collected) * 100.0
1002 / (allocated + overhead - freed - collected),
1003 (long)d->overhead,
1004 d->overhead * 100.0 / overhead,
1005 (long)d->times);
1008 fprintf (stderr, "%-48s %10ld %10ld %10ld %10ld %10ld\n",
1009 "Total", (long)collected, (long)freed,
1010 (long)(allocated + overhead - freed - collected), (long)overhead,
1011 (long)times);
1012 fprintf (stderr, "%-48s %10s %10s %10s %10s %10s\n",
1013 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times");
1014 fprintf (stderr, "-------------------------------------------------------\n");
1015 ggc_force_collect = false;
1016 #endif