1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
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 2, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* Generic garbage collection (GC) functions and data, not specific to
23 any particular GC implementation. */
27 #include "coretypes.h"
32 #include "hosthooks.h"
33 #include "hosthooks-def.h"
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
40 # include <sys/mman.h>
42 /* This is on Solaris. */
43 # include <sys/types.h>
48 # define MAP_FAILED ((void *)-1)
51 #ifdef ENABLE_VALGRIND_CHECKING
52 # ifdef HAVE_VALGRIND_MEMCHECK_H
53 # include <valgrind/memcheck.h>
54 # elif defined HAVE_MEMCHECK_H
55 # include <memcheck.h>
57 # include <valgrind.h>
60 /* Avoid #ifdef:s when we can help it. */
61 #define VALGRIND_DISCARD(x)
64 /* Statistics about the allocation. */
65 static ggc_statistics
*ggc_stats
;
67 struct traversal_state
;
69 static int ggc_htab_delete (void **, void *);
70 static hashval_t
saving_htab_hash (const void *);
71 static int saving_htab_eq (const void *, const void *);
72 static int call_count (void **, void *);
73 static int call_alloc (void **, void *);
74 static int compare_ptr_data (const void *, const void *);
75 static void relocate_ptrs (void *, void *);
76 static void write_pch_globals (const struct ggc_root_tab
* const *tab
,
77 struct traversal_state
*state
);
78 static double ggc_rlimit_bound (double);
80 /* Maintain global roots that are preserved during GC. */
82 /* Process a slot of an htab by deleting it if it has not been marked. */
85 ggc_htab_delete (void **slot
, void *info
)
87 const struct ggc_cache_tab
*r
= (const struct ggc_cache_tab
*) info
;
89 if (! (*r
->marked_p
) (*slot
))
90 htab_clear_slot (*r
->base
, slot
);
97 /* Iterate through all registered roots and mark each element. */
100 ggc_mark_roots (void)
102 const struct ggc_root_tab
*const *rt
;
103 const struct ggc_root_tab
*rti
;
104 const struct ggc_cache_tab
*const *ct
;
105 const struct ggc_cache_tab
*cti
;
108 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
109 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
110 memset (rti
->base
, 0, rti
->stride
);
112 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
113 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
114 for (i
= 0; i
< rti
->nelt
; i
++)
115 (*rti
->cb
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
117 ggc_mark_stringpool ();
119 /* Now scan all hash tables that have objects which are to be deleted if
120 they are not already marked. */
121 for (ct
= gt_ggc_cache_rtab
; *ct
; ct
++)
122 for (cti
= *ct
; cti
->base
!= NULL
; cti
++)
125 ggc_set_mark (*cti
->base
);
126 htab_traverse_noresize (*cti
->base
, ggc_htab_delete
, (void *) cti
);
127 ggc_set_mark ((*cti
->base
)->entries
);
131 /* Allocate a block of memory, then clear it. */
133 ggc_alloc_cleared_stat (size_t size MEM_STAT_DECL
)
135 void *buf
= ggc_alloc_stat (size PASS_MEM_STAT
);
136 memset (buf
, 0, size
);
140 /* Resize a block of memory, possibly re-allocating it. */
142 ggc_realloc_stat (void *x
, size_t size MEM_STAT_DECL
)
148 return ggc_alloc_stat (size PASS_MEM_STAT
);
150 old_size
= ggc_get_size (x
);
152 if (size
<= old_size
)
154 /* Mark the unwanted memory as unaccessible. We also need to make
155 the "new" size accessible, since ggc_get_size returns the size of
156 the pool, not the size of the individually allocated object, the
157 size which was previously made accessible. Unfortunately, we
158 don't know that previously allocated size. Without that
159 knowledge we have to lose some initialization-tracking for the
160 old parts of the object. An alternative is to mark the whole
161 old_size as reachable, but that would lose tracking of writes
162 after the end of the object (by small offsets). Discard the
163 handle to avoid handle leak. */
164 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) x
+ size
,
166 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x
, size
));
170 r
= ggc_alloc_stat (size PASS_MEM_STAT
);
172 /* Since ggc_get_size returns the size of the pool, not the size of the
173 individually allocated object, we'd access parts of the old object
174 that were marked invalid with the memcpy below. We lose a bit of the
175 initialization-tracking since some of it may be uninitialized. */
176 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x
, old_size
));
178 memcpy (r
, x
, old_size
);
180 /* The old object is not supposed to be used anymore. */
186 /* Like ggc_alloc_cleared, but performs a multiplication. */
188 ggc_calloc (size_t s1
, size_t s2
)
190 return ggc_alloc_cleared (s1
* s2
);
193 /* These are for splay_tree_new_ggc. */
195 ggc_splay_alloc (int sz
, void *nl
)
199 return ggc_alloc (sz
);
203 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED
, void *nl
)
209 /* Print statistics that are independent of the collector in use. */
210 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
212 : ((x) < 1024*1024*10 \
214 : (x) / (1024*1024))))
215 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
218 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED
,
219 ggc_statistics
*stats
)
221 /* Set the pointer so that during collection we will actually gather
225 /* Then do one collection to fill in the statistics. */
228 /* At present, we don't really gather any interesting statistics. */
230 /* Don't gather statistics any more. */
234 /* Functions for saving and restoring GCable memory to disk. */
236 static htab_t saving_htab
;
241 void *note_ptr_cookie
;
242 gt_note_pointers note_ptr_fn
;
243 gt_handle_reorder reorder_fn
;
248 #define POINTER_HASH(x) (hashval_t)((long)x >> 3)
250 /* Register an object in the hash table. */
253 gt_pch_note_object (void *obj
, void *note_ptr_cookie
,
254 gt_note_pointers note_ptr_fn
)
256 struct ptr_data
**slot
;
258 if (obj
== NULL
|| obj
== (void *) 1)
261 slot
= (struct ptr_data
**)
262 htab_find_slot_with_hash (saving_htab
, obj
, POINTER_HASH (obj
),
266 if ((*slot
)->note_ptr_fn
!= note_ptr_fn
267 || (*slot
)->note_ptr_cookie
!= note_ptr_cookie
)
272 *slot
= xcalloc (sizeof (struct ptr_data
), 1);
274 (*slot
)->note_ptr_fn
= note_ptr_fn
;
275 (*slot
)->note_ptr_cookie
= note_ptr_cookie
;
276 if (note_ptr_fn
== gt_pch_p_S
)
277 (*slot
)->size
= strlen (obj
) + 1;
279 (*slot
)->size
= ggc_get_size (obj
);
283 /* Register an object in the hash table. */
286 gt_pch_note_reorder (void *obj
, void *note_ptr_cookie
,
287 gt_handle_reorder reorder_fn
)
289 struct ptr_data
*data
;
291 if (obj
== NULL
|| obj
== (void *) 1)
294 data
= htab_find_with_hash (saving_htab
, obj
, POINTER_HASH (obj
));
296 || data
->note_ptr_cookie
!= note_ptr_cookie
)
299 data
->reorder_fn
= reorder_fn
;
302 /* Hash and equality functions for saving_htab, callbacks for htab_create. */
305 saving_htab_hash (const void *p
)
307 return POINTER_HASH (((struct ptr_data
*)p
)->obj
);
311 saving_htab_eq (const void *p1
, const void *p2
)
313 return ((struct ptr_data
*)p1
)->obj
== p2
;
316 /* Handy state for the traversal functions. */
318 struct traversal_state
321 struct ggc_pch_data
*d
;
323 struct ptr_data
**ptrs
;
327 /* Callbacks for htab_traverse. */
330 call_count (void **slot
, void *state_p
)
332 struct ptr_data
*d
= (struct ptr_data
*)*slot
;
333 struct traversal_state
*state
= (struct traversal_state
*)state_p
;
335 ggc_pch_count_object (state
->d
, d
->obj
, d
->size
, d
->note_ptr_fn
== gt_pch_p_S
);
341 call_alloc (void **slot
, void *state_p
)
343 struct ptr_data
*d
= (struct ptr_data
*)*slot
;
344 struct traversal_state
*state
= (struct traversal_state
*)state_p
;
346 d
->new_addr
= ggc_pch_alloc_object (state
->d
, d
->obj
, d
->size
, d
->note_ptr_fn
== gt_pch_p_S
);
347 state
->ptrs
[state
->ptrs_i
++] = d
;
351 /* Callback for qsort. */
354 compare_ptr_data (const void *p1_p
, const void *p2_p
)
356 struct ptr_data
*p1
= *(struct ptr_data
*const *)p1_p
;
357 struct ptr_data
*p2
= *(struct ptr_data
*const *)p2_p
;
358 return (((size_t)p1
->new_addr
> (size_t)p2
->new_addr
)
359 - ((size_t)p1
->new_addr
< (size_t)p2
->new_addr
));
362 /* Callbacks for note_ptr_fn. */
365 relocate_ptrs (void *ptr_p
, void *state_p
)
367 void **ptr
= (void **)ptr_p
;
368 struct traversal_state
*state ATTRIBUTE_UNUSED
369 = (struct traversal_state
*)state_p
;
370 struct ptr_data
*result
;
372 if (*ptr
== NULL
|| *ptr
== (void *)1)
375 result
= htab_find_with_hash (saving_htab
, *ptr
, POINTER_HASH (*ptr
));
378 *ptr
= result
->new_addr
;
381 /* Write out, after relocation, the pointers in TAB. */
383 write_pch_globals (const struct ggc_root_tab
* const *tab
,
384 struct traversal_state
*state
)
386 const struct ggc_root_tab
*const *rt
;
387 const struct ggc_root_tab
*rti
;
390 for (rt
= tab
; *rt
; rt
++)
391 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
392 for (i
= 0; i
< rti
->nelt
; i
++)
394 void *ptr
= *(void **)((char *)rti
->base
+ rti
->stride
* i
);
395 struct ptr_data
*new_ptr
;
396 if (ptr
== NULL
|| ptr
== (void *)1)
398 if (fwrite (&ptr
, sizeof (void *), 1, state
->f
)
400 fatal_error ("can't write PCH file: %m");
404 new_ptr
= htab_find_with_hash (saving_htab
, ptr
,
406 if (fwrite (&new_ptr
->new_addr
, sizeof (void *), 1, state
->f
)
408 fatal_error ("can't write PCH file: %m");
413 /* Hold the information we need to mmap the file back in. */
419 void *preferred_base
;
422 /* Write out the state of the compiler to F. */
425 gt_pch_save (FILE *f
)
427 const struct ggc_root_tab
*const *rt
;
428 const struct ggc_root_tab
*rti
;
430 struct traversal_state state
;
431 char *this_object
= NULL
;
432 size_t this_object_size
= 0;
433 struct mmap_info mmi
;
434 const size_t mmap_offset_alignment
= host_hooks
.gt_pch_alloc_granularity();
436 gt_pch_save_stringpool ();
438 saving_htab
= htab_create (50000, saving_htab_hash
, saving_htab_eq
, free
);
440 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
441 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
442 for (i
= 0; i
< rti
->nelt
; i
++)
443 (*rti
->pchw
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
445 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
446 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
447 for (i
= 0; i
< rti
->nelt
; i
++)
448 (*rti
->pchw
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
450 /* Prepare the objects for writing, determine addresses and such. */
452 state
.d
= init_ggc_pch();
454 htab_traverse (saving_htab
, call_count
, &state
);
456 mmi
.size
= ggc_pch_total_size (state
.d
);
458 /* Try to arrange things so that no relocation is necessary, but
459 don't try very hard. On most platforms, this will always work,
460 and on the rest it's a lot of work to do better.
461 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
462 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
463 mmi
.preferred_base
= host_hooks
.gt_pch_get_address (mmi
.size
, fileno (f
));
465 ggc_pch_this_base (state
.d
, mmi
.preferred_base
);
467 state
.ptrs
= xmalloc (state
.count
* sizeof (*state
.ptrs
));
469 htab_traverse (saving_htab
, call_alloc
, &state
);
470 qsort (state
.ptrs
, state
.count
, sizeof (*state
.ptrs
), compare_ptr_data
);
472 /* Write out all the scalar variables. */
473 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
474 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
475 if (fwrite (rti
->base
, rti
->stride
, 1, f
) != 1)
476 fatal_error ("can't write PCH file: %m");
478 /* Write out all the global pointers, after translation. */
479 write_pch_globals (gt_ggc_rtab
, &state
);
480 write_pch_globals (gt_pch_cache_rtab
, &state
);
482 ggc_pch_prepare_write (state
.d
, state
.f
);
484 /* Pad the PCH file so that the mmapped area starts on an allocation
485 granularity (usually page) boundary. */
488 o
= ftell (state
.f
) + sizeof (mmi
);
490 fatal_error ("can't get position in PCH file: %m");
491 mmi
.offset
= mmap_offset_alignment
- o
% mmap_offset_alignment
;
492 if (mmi
.offset
== mmap_offset_alignment
)
496 if (fwrite (&mmi
, sizeof (mmi
), 1, state
.f
) != 1)
497 fatal_error ("can't write PCH file: %m");
499 && fseek (state
.f
, mmi
.offset
, SEEK_SET
) != 0)
500 fatal_error ("can't write padding to PCH file: %m");
502 /* Actually write out the objects. */
503 for (i
= 0; i
< state
.count
; i
++)
505 if (this_object_size
< state
.ptrs
[i
]->size
)
507 this_object_size
= state
.ptrs
[i
]->size
;
508 this_object
= xrealloc (this_object
, this_object_size
);
510 memcpy (this_object
, state
.ptrs
[i
]->obj
, state
.ptrs
[i
]->size
);
511 if (state
.ptrs
[i
]->reorder_fn
!= NULL
)
512 state
.ptrs
[i
]->reorder_fn (state
.ptrs
[i
]->obj
,
513 state
.ptrs
[i
]->note_ptr_cookie
,
514 relocate_ptrs
, &state
);
515 state
.ptrs
[i
]->note_ptr_fn (state
.ptrs
[i
]->obj
,
516 state
.ptrs
[i
]->note_ptr_cookie
,
517 relocate_ptrs
, &state
);
518 ggc_pch_write_object (state
.d
, state
.f
, state
.ptrs
[i
]->obj
,
519 state
.ptrs
[i
]->new_addr
, state
.ptrs
[i
]->size
,
520 state
.ptrs
[i
]->note_ptr_fn
== gt_pch_p_S
);
521 if (state
.ptrs
[i
]->note_ptr_fn
!= gt_pch_p_S
)
522 memcpy (state
.ptrs
[i
]->obj
, this_object
, state
.ptrs
[i
]->size
);
524 ggc_pch_finish (state
.d
, state
.f
);
525 gt_pch_fixup_stringpool ();
528 htab_delete (saving_htab
);
531 /* Read the state of the compiler back in from F. */
534 gt_pch_restore (FILE *f
)
536 const struct ggc_root_tab
*const *rt
;
537 const struct ggc_root_tab
*rti
;
539 struct mmap_info mmi
;
542 /* Delete any deletable objects. This makes ggc_pch_read much
543 faster, as it can be sure that no GCable objects remain other
544 than the ones just read in. */
545 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
546 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
547 memset (rti
->base
, 0, rti
->stride
);
549 /* Read in all the scalar variables. */
550 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
551 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
552 if (fread (rti
->base
, rti
->stride
, 1, f
) != 1)
553 fatal_error ("can't read PCH file: %m");
555 /* Read in all the global pointers, in 6 easy loops. */
556 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
557 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
558 for (i
= 0; i
< rti
->nelt
; i
++)
559 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
560 sizeof (void *), 1, f
) != 1)
561 fatal_error ("can't read PCH file: %m");
563 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
564 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
565 for (i
= 0; i
< rti
->nelt
; i
++)
566 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
567 sizeof (void *), 1, f
) != 1)
568 fatal_error ("can't read PCH file: %m");
570 if (fread (&mmi
, sizeof (mmi
), 1, f
) != 1)
571 fatal_error ("can't read PCH file: %m");
573 result
= host_hooks
.gt_pch_use_address (mmi
.preferred_base
, mmi
.size
,
574 fileno (f
), mmi
.offset
);
576 fatal_error ("had to relocate PCH");
579 if (fseek (f
, mmi
.offset
, SEEK_SET
) != 0
580 || fread (mmi
.preferred_base
, mmi
.size
, 1, f
) != 1)
581 fatal_error ("can't read PCH file: %m");
583 else if (fseek (f
, mmi
.offset
+ mmi
.size
, SEEK_SET
) != 0)
584 fatal_error ("can't read PCH file: %m");
586 ggc_pch_read (f
, mmi
.preferred_base
);
588 gt_pch_restore_stringpool ();
591 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
592 Select no address whatsoever, and let gt_pch_save choose what it will with
593 malloc, presumably. */
596 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED
,
597 int fd ATTRIBUTE_UNUSED
)
602 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
603 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
604 same as base, indicating that the memory has been allocated but needs to
605 be read in from the file. Return -1 if the address differs, to relocation
606 of the PCH file would be required. */
609 default_gt_pch_use_address (void *base
, size_t size
, int fd ATTRIBUTE_UNUSED
,
610 size_t offset ATTRIBUTE_UNUSED
)
612 void *addr
= xmalloc (size
);
613 return (addr
== base
) - 1;
616 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
617 alignment required for allocating virtual memory. Usually this is the
621 default_gt_pch_alloc_granularity (void)
623 return getpagesize();
627 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
628 We temporarily allocate SIZE bytes, and let the kernel place the data
629 wherever it will. If it worked, that's our spot, if not we're likely
633 mmap_gt_pch_get_address (size_t size
, int fd
)
637 ret
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
638 if (ret
== (void *) MAP_FAILED
)
646 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
647 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
648 mapping the data at BASE, -1 if we couldn't.
650 This version assumes that the kernel honors the START operand of mmap
651 even without MAP_FIXED if START through START+SIZE are not currently
652 mapped with something. */
655 mmap_gt_pch_use_address (void *base
, size_t size
, int fd
, size_t offset
)
659 /* We're called with size == 0 if we're not planning to load a PCH
660 file at all. This allows the hook to free any static space that
661 we might have allocated at link time. */
665 addr
= mmap (base
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
668 return addr
== base
? 1 : -1;
670 #endif /* HAVE_MMAP_FILE */
672 /* Modify the bound based on rlimits. */
674 ggc_rlimit_bound (double limit
)
676 #if defined(HAVE_GETRLIMIT)
678 # if defined (RLIMIT_AS)
679 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
680 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
681 if (getrlimit (RLIMIT_AS
, &rlim
) == 0
682 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
683 && rlim
.rlim_cur
< limit
)
684 limit
= rlim
.rlim_cur
;
685 # elif defined (RLIMIT_DATA)
686 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
687 might be on an OS that has a broken mmap. (Others don't bound
688 mmap at all, apparently.) */
689 if (getrlimit (RLIMIT_DATA
, &rlim
) == 0
690 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
691 && rlim
.rlim_cur
< limit
692 /* Darwin has this horribly bogus default setting of
693 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
694 appears to be ignored. Ignore such silliness. If a limit
695 this small was actually effective for mmap, GCC wouldn't even
697 && rlim
.rlim_cur
>= 8 * 1024 * 1024)
698 limit
= rlim
.rlim_cur
;
699 # endif /* RLIMIT_AS or RLIMIT_DATA */
700 #endif /* HAVE_GETRLIMIT */
705 /* Heuristic to set a default for GGC_MIN_EXPAND. */
707 ggc_min_expand_heuristic (void)
709 double min_expand
= physmem_total();
711 /* Adjust for rlimits. */
712 min_expand
= ggc_rlimit_bound (min_expand
);
714 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
715 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
716 min_expand
/= 1024*1024*1024;
718 min_expand
= MIN (min_expand
, 70);
724 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
726 ggc_min_heapsize_heuristic (void)
728 double phys_kbytes
= physmem_total();
729 double limit_kbytes
= ggc_rlimit_bound (phys_kbytes
* 2);
731 phys_kbytes
/= 1024; /* Convert to Kbytes. */
732 limit_kbytes
/= 1024;
734 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
735 bound of 128M (when RAM >= 1GB). */
738 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
739 /* Try not to overrun the RSS limit while doing garbage collection.
740 The RSS limit is only advisory, so no margin is subtracted. */
743 if (getrlimit (RLIMIT_RSS
, &rlim
) == 0
744 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
)
745 phys_kbytes
= MIN (phys_kbytes
, rlim
.rlim_cur
/ 1024);
749 /* Don't blindly run over our data limit; do GC at least when the
750 *next* GC would be within 16Mb of the limit. If GCC does hit the
751 data limit, compilation will fail, so this tries to be
753 limit_kbytes
= MAX (0, limit_kbytes
- 16 * 1024);
754 limit_kbytes
= (limit_kbytes
* 100) / (110 + ggc_min_expand_heuristic());
755 phys_kbytes
= MIN (phys_kbytes
, limit_kbytes
);
757 phys_kbytes
= MAX (phys_kbytes
, 4 * 1024);
758 phys_kbytes
= MIN (phys_kbytes
, 128 * 1024);
764 init_ggc_heuristics (void)
766 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
767 set_param_value ("ggc-min-expand", ggc_min_expand_heuristic());
768 set_param_value ("ggc-min-heapsize", ggc_min_heapsize_heuristic());
772 #ifdef GATHER_STATISTICS
774 /* Datastructure used to store per-call-site statistics. */
775 struct loc_descriptor
779 const char *function
;
785 /* Hashtable used for statistics. */
786 static htab_t loc_hash
;
788 /* Hash table helpers functions. */
790 hash_descriptor (const void *p
)
792 const struct loc_descriptor
*d
= p
;
794 return htab_hash_pointer (d
->function
) | d
->line
;
798 eq_descriptor (const void *p1
, const void *p2
)
800 const struct loc_descriptor
*d
= p1
;
801 const struct loc_descriptor
*d2
= p2
;
803 return (d
->file
== d2
->file
&& d
->line
== d2
->line
804 && d
->function
== d2
->function
);
807 /* Return descriptor for given call site, create new one if needed. */
808 static struct loc_descriptor
*
809 loc_descriptor (const char *name
, int line
, const char *function
)
811 struct loc_descriptor loc
;
812 struct loc_descriptor
**slot
;
816 loc
.function
= function
;
818 loc_hash
= htab_create (10, hash_descriptor
, eq_descriptor
, NULL
);
820 slot
= (struct loc_descriptor
**) htab_find_slot (loc_hash
, &loc
, 1);
823 *slot
= xcalloc (sizeof (**slot
), 1);
824 (*slot
)->file
= name
;
825 (*slot
)->line
= line
;
826 (*slot
)->function
= function
;
830 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
832 ggc_record_overhead (size_t allocated
, size_t overhead
,
833 const char *name
, int line
, const char *function
)
835 struct loc_descriptor
*loc
= loc_descriptor (name
, line
, function
);
838 loc
->allocated
+=allocated
;
839 loc
->overhead
+=overhead
;
842 /* Helper for qsort; sort descriptors by amount of memory consumed. */
844 cmp_statistic (const void *loc1
, const void *loc2
)
846 struct loc_descriptor
*l1
= *(struct loc_descriptor
**) loc1
;
847 struct loc_descriptor
*l2
= *(struct loc_descriptor
**) loc2
;
848 return (l1
->allocated
+ l1
->overhead
) - (l2
->allocated
+ l2
->overhead
);
851 /* Collect array of the descriptors from hashtable. */
852 struct loc_descriptor
**loc_array
;
854 add_statistics (void **slot
, void *b
)
857 loc_array
[*n
] = (struct loc_descriptor
*) *slot
;
862 /* Dump per-site memory statistics. */
864 void dump_ggc_loc_statistics (void)
866 #ifdef GATHER_STATISTICS
869 size_t count
, size
, overhead
;
872 loc_array
= xcalloc (sizeof (*loc_array
), loc_hash
->n_elements
);
873 fprintf (stderr
, "-------------------------------------------------------\n");
874 fprintf (stderr
, "\n%-60s %10s %10s %10s\n",
875 "source location", "Times", "Allocated", "Overhead");
876 fprintf (stderr
, "-------------------------------------------------------\n");
880 htab_traverse (loc_hash
, add_statistics
, &nentries
);
881 qsort (loc_array
, nentries
, sizeof (*loc_array
), cmp_statistic
);
882 for (i
= 0; i
< nentries
; i
++)
884 struct loc_descriptor
*d
= loc_array
[i
];
885 size
+= d
->allocated
;
887 overhead
+= d
->overhead
;
889 for (i
= 0; i
< nentries
; i
++)
891 struct loc_descriptor
*d
= loc_array
[i
];
894 const char *s1
= d
->file
;
896 while ((s2
= strstr (s1
, "gcc/")))
898 sprintf (s
, "%s:%i (%s)", s1
, d
->line
, d
->function
);
899 fprintf (stderr
, "%-60s %10i %10li %10li:%.3f%%\n", s
,
900 d
->times
, (long)d
->allocated
, (long)d
->overhead
,
901 (d
->allocated
+ d
->overhead
) *100.0 / (size
+ overhead
));
904 fprintf (stderr
, "%-60s %10ld %10ld %10ld\n",
905 "Total", (long)count
, (long)size
, (long)overhead
);
906 fprintf (stderr
, "-------------------------------------------------------\n");