1 /* Simple garbage collection for the GNU compiler. 2 Copyright (C) 1999-2013 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 6 GCC is free software; you can redistribute it and/or modify it under 7 the terms of the GNU General Public License as published by the Free 8 Software Foundation; either version 3, or (at your option) any later 9 version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GCC; see the file COPYING3. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 /* Generic garbage collection (GC) functions and data, not specific to 21 any particular GC implementation. */ 22 23 #include "config.h" 24 #include "system.h" 25 #include "coretypes.h" 26 #include "hashtab.h" 27 #include "ggc.h" 28 #include "ggc-internal.h" 29 #include "diagnostic-core.h" 30 #include "params.h" 31 #include "hosthooks.h" 32 #include "hosthooks-def.h" 33 #include "plugin.h" 34 #include "vec.h" 35 #include "timevar.h" 36 37 /* When set, ggc_collect will do collection. */ 38 bool ggc_force_collect; 39 40 /* When true, protect the contents of the identifier hash table. */ 41 bool ggc_protect_identifiers = true; 42 43 /* Statistics about the allocation. */ 44 static ggc_statistics *ggc_stats; 45 46 struct traversal_state; 47 48 static int ggc_htab_delete (void **, void *); 49 static hashval_t saving_htab_hash (const void *); 50 static int saving_htab_eq (const void *, const void *); 51 static int call_count (void **, void *); 52 static int call_alloc (void **, void *); 53 static int compare_ptr_data (const void *, const void *); 54 static void relocate_ptrs (void *, void *); 55 static void write_pch_globals (const struct ggc_root_tab * const *tab, 56 struct traversal_state *state); 57 58 /* Maintain global roots that are preserved during GC. */ 59 60 /* Process a slot of an htab by deleting it if it has not been marked. */ 61 62 static int 63 ggc_htab_delete (void **slot, void *info) 64 { 65 const struct ggc_cache_tab *r = (const struct ggc_cache_tab *) info; 66 67 if (! (*r->marked_p) (*slot)) 68 htab_clear_slot (*r->base, slot); 69 else 70 (*r->cb) (*slot); 71 72 return 1; 73 } 74 75 76 /* This extra vector of dynamically registered root_tab-s is used by 77 ggc_mark_roots and gives the ability to dynamically add new GGC root 78 tables, for instance from some plugins; this vector is on the heap 79 since it is used by GGC internally. */ 80 typedef const struct ggc_root_tab *const_ggc_root_tab_t; 81 static vec<const_ggc_root_tab_t> extra_root_vec; 82 83 /* Dynamically register a new GGC root table RT. This is useful for 84 plugins. */ 85 86 void 87 ggc_register_root_tab (const struct ggc_root_tab* rt) 88 { 89 if (rt) 90 extra_root_vec.safe_push (rt); 91 } 92 93 /* This extra vector of dynamically registered cache_tab-s is used by 94 ggc_mark_roots and gives the ability to dynamically add new GGC cache 95 tables, for instance from some plugins; this vector is on the heap 96 since it is used by GGC internally. */ 97 typedef const struct ggc_cache_tab *const_ggc_cache_tab_t; 98 static vec<const_ggc_cache_tab_t> extra_cache_vec; 99 100 /* Dynamically register a new GGC cache table CT. This is useful for 101 plugins. */ 102 103 void 104 ggc_register_cache_tab (const struct ggc_cache_tab* ct) 105 { 106 if (ct) 107 extra_cache_vec.safe_push (ct); 108 } 109 110 /* Scan a hash table that has objects which are to be deleted if they are not 111 already marked. */ 112 113 static void 114 ggc_scan_cache_tab (const_ggc_cache_tab_t ctp) 115 { 116 const struct ggc_cache_tab *cti; 117 118 for (cti = ctp; cti->base != NULL; cti++) 119 if (*cti->base) 120 { 121 ggc_set_mark (*cti->base); 122 htab_traverse_noresize (*cti->base, ggc_htab_delete, 123 CONST_CAST (void *, (const void *)cti)); 124 ggc_set_mark ((*cti->base)->entries); 125 } 126 } 127 128 /* Mark all the roots in the table RT. */ 129 130 static void 131 ggc_mark_root_tab (const_ggc_root_tab_t rt) 132 { 133 size_t i; 134 135 for ( ; rt->base != NULL; rt++) 136 for (i = 0; i < rt->nelt; i++) 137 (*rt->cb) (*(void **) ((char *)rt->base + rt->stride * i)); 138 } 139 140 /* Iterate through all registered roots and mark each element. */ 141 142 void 143 ggc_mark_roots (void) 144 { 145 const struct ggc_root_tab *const *rt; 146 const_ggc_root_tab_t rtp, rti; 147 const struct ggc_cache_tab *const *ct; 148 const_ggc_cache_tab_t ctp; 149 size_t i; 150 151 for (rt = gt_ggc_deletable_rtab; *rt; rt++) 152 for (rti = *rt; rti->base != NULL; rti++) 153 memset (rti->base, 0, rti->stride); 154 155 for (rt = gt_ggc_rtab; *rt; rt++) 156 ggc_mark_root_tab (*rt); 157 158 FOR_EACH_VEC_ELT (extra_root_vec, i, rtp) 159 ggc_mark_root_tab (rtp); 160 161 if (ggc_protect_identifiers) 162 ggc_mark_stringpool (); 163 164 /* Now scan all hash tables that have objects which are to be deleted if 165 they are not already marked. */ 166 for (ct = gt_ggc_cache_rtab; *ct; ct++) 167 ggc_scan_cache_tab (*ct); 168 169 FOR_EACH_VEC_ELT (extra_cache_vec, i, ctp) 170 ggc_scan_cache_tab (ctp); 171 172 if (! ggc_protect_identifiers) 173 ggc_purge_stringpool (); 174 175 /* Some plugins may call ggc_set_mark from here. */ 176 invoke_plugin_callbacks (PLUGIN_GGC_MARKING, NULL); 177 } 178 179 /* Allocate a block of memory, then clear it. */ 180 void * 181 ggc_internal_cleared_alloc_stat (size_t size MEM_STAT_DECL) 182 { 183 void *buf = ggc_internal_alloc_stat (size PASS_MEM_STAT); 184 memset (buf, 0, size); 185 return buf; 186 } 187 188 /* Resize a block of memory, possibly re-allocating it. */ 189 void * 190 ggc_realloc_stat (void *x, size_t size MEM_STAT_DECL) 191 { 192 void *r; 193 size_t old_size; 194 195 if (x == NULL) 196 return ggc_internal_alloc_stat (size PASS_MEM_STAT); 197 198 old_size = ggc_get_size (x); 199 200 if (size <= old_size) 201 { 202 /* Mark the unwanted memory as unaccessible. We also need to make 203 the "new" size accessible, since ggc_get_size returns the size of 204 the pool, not the size of the individually allocated object, the 205 size which was previously made accessible. Unfortunately, we 206 don't know that previously allocated size. Without that 207 knowledge we have to lose some initialization-tracking for the 208 old parts of the object. An alternative is to mark the whole 209 old_size as reachable, but that would lose tracking of writes 210 after the end of the object (by small offsets). Discard the 211 handle to avoid handle leak. */ 212 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size, 213 old_size - size)); 214 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size)); 215 return x; 216 } 217 218 r = ggc_internal_alloc_stat (size PASS_MEM_STAT); 219 220 /* Since ggc_get_size returns the size of the pool, not the size of the 221 individually allocated object, we'd access parts of the old object 222 that were marked invalid with the memcpy below. We lose a bit of the 223 initialization-tracking since some of it may be uninitialized. */ 224 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size)); 225 226 memcpy (r, x, old_size); 227 228 /* The old object is not supposed to be used anymore. */ 229 ggc_free (x); 230 231 return r; 232 } 233 234 void * 235 ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED, 236 size_t n ATTRIBUTE_UNUSED) 237 { 238 gcc_assert (c * n == sizeof (struct htab)); 239 return ggc_alloc_cleared_htab (); 240 } 241 242 /* TODO: once we actually use type information in GGC, create a new tag 243 gt_gcc_ptr_array and use it for pointer arrays. */ 244 void * 245 ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n) 246 { 247 gcc_assert (sizeof (PTR *) == n); 248 return ggc_internal_cleared_vec_alloc (sizeof (PTR *), c); 249 } 250 251 /* These are for splay_tree_new_ggc. */ 252 void * 253 ggc_splay_alloc (int sz, void *nl) 254 { 255 gcc_assert (!nl); 256 return ggc_internal_alloc (sz); 257 } 258 259 void 260 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl) 261 { 262 gcc_assert (!nl); 263 } 264 265 /* Print statistics that are independent of the collector in use. */ 266 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \ 267 ? (x) \ 268 : ((x) < 1024*1024*10 \ 269 ? (x) / 1024 \ 270 : (x) / (1024*1024)))) 271 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M')) 272 273 void 274 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED, 275 ggc_statistics *stats) 276 { 277 /* Set the pointer so that during collection we will actually gather 278 the statistics. */ 279 ggc_stats = stats; 280 281 /* Then do one collection to fill in the statistics. */ 282 ggc_collect (); 283 284 /* At present, we don't really gather any interesting statistics. */ 285 286 /* Don't gather statistics any more. */ 287 ggc_stats = NULL; 288 } 289 290 /* Functions for saving and restoring GCable memory to disk. */ 291 292 static htab_t saving_htab; 293 294 struct ptr_data 295 { 296 void *obj; 297 void *note_ptr_cookie; 298 gt_note_pointers note_ptr_fn; 299 gt_handle_reorder reorder_fn; 300 size_t size; 301 void *new_addr; 302 }; 303 304 #define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3) 305 306 /* Register an object in the hash table. */ 307 308 int 309 gt_pch_note_object (void *obj, void *note_ptr_cookie, 310 gt_note_pointers note_ptr_fn) 311 { 312 struct ptr_data **slot; 313 314 if (obj == NULL || obj == (void *) 1) 315 return 0; 316 317 slot = (struct ptr_data **) 318 htab_find_slot_with_hash (saving_htab, obj, POINTER_HASH (obj), 319 INSERT); 320 if (*slot != NULL) 321 { 322 gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn 323 && (*slot)->note_ptr_cookie == note_ptr_cookie); 324 return 0; 325 } 326 327 *slot = XCNEW (struct ptr_data); 328 (*slot)->obj = obj; 329 (*slot)->note_ptr_fn = note_ptr_fn; 330 (*slot)->note_ptr_cookie = note_ptr_cookie; 331 if (note_ptr_fn == gt_pch_p_S) 332 (*slot)->size = strlen ((const char *)obj) + 1; 333 else 334 (*slot)->size = ggc_get_size (obj); 335 return 1; 336 } 337 338 /* Register an object in the hash table. */ 339 340 void 341 gt_pch_note_reorder (void *obj, void *note_ptr_cookie, 342 gt_handle_reorder reorder_fn) 343 { 344 struct ptr_data *data; 345 346 if (obj == NULL || obj == (void *) 1) 347 return; 348 349 data = (struct ptr_data *) 350 htab_find_with_hash (saving_htab, obj, POINTER_HASH (obj)); 351 gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie); 352 353 data->reorder_fn = reorder_fn; 354 } 355 356 /* Hash and equality functions for saving_htab, callbacks for htab_create. */ 357 358 static hashval_t 359 saving_htab_hash (const void *p) 360 { 361 return POINTER_HASH (((const struct ptr_data *)p)->obj); 362 } 363 364 static int 365 saving_htab_eq (const void *p1, const void *p2) 366 { 367 return ((const struct ptr_data *)p1)->obj == p2; 368 } 369 370 /* Handy state for the traversal functions. */ 371 372 struct traversal_state 373 { 374 FILE *f; 375 struct ggc_pch_data *d; 376 size_t count; 377 struct ptr_data **ptrs; 378 size_t ptrs_i; 379 }; 380 381 /* Callbacks for htab_traverse. */ 382 383 static int 384 call_count (void **slot, void *state_p) 385 { 386 struct ptr_data *d = (struct ptr_data *)*slot; 387 struct traversal_state *state = (struct traversal_state *)state_p; 388 389 ggc_pch_count_object (state->d, d->obj, d->size, 390 d->note_ptr_fn == gt_pch_p_S); 391 state->count++; 392 return 1; 393 } 394 395 static int 396 call_alloc (void **slot, void *state_p) 397 { 398 struct ptr_data *d = (struct ptr_data *)*slot; 399 struct traversal_state *state = (struct traversal_state *)state_p; 400 401 d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size, 402 d->note_ptr_fn == gt_pch_p_S); 403 state->ptrs[state->ptrs_i++] = d; 404 return 1; 405 } 406 407 /* Callback for qsort. */ 408 409 static int 410 compare_ptr_data (const void *p1_p, const void *p2_p) 411 { 412 const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p; 413 const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p; 414 return (((size_t)p1->new_addr > (size_t)p2->new_addr) 415 - ((size_t)p1->new_addr < (size_t)p2->new_addr)); 416 } 417 418 /* Callbacks for note_ptr_fn. */ 419 420 static void 421 relocate_ptrs (void *ptr_p, void *state_p) 422 { 423 void **ptr = (void **)ptr_p; 424 struct traversal_state *state ATTRIBUTE_UNUSED 425 = (struct traversal_state *)state_p; 426 struct ptr_data *result; 427 428 if (*ptr == NULL || *ptr == (void *)1) 429 return; 430 431 result = (struct ptr_data *) 432 htab_find_with_hash (saving_htab, *ptr, POINTER_HASH (*ptr)); 433 gcc_assert (result); 434 *ptr = result->new_addr; 435 } 436 437 /* Write out, after relocation, the pointers in TAB. */ 438 static void 439 write_pch_globals (const struct ggc_root_tab * const *tab, 440 struct traversal_state *state) 441 { 442 const struct ggc_root_tab *const *rt; 443 const struct ggc_root_tab *rti; 444 size_t i; 445 446 for (rt = tab; *rt; rt++) 447 for (rti = *rt; rti->base != NULL; rti++) 448 for (i = 0; i < rti->nelt; i++) 449 { 450 void *ptr = *(void **)((char *)rti->base + rti->stride * i); 451 struct ptr_data *new_ptr; 452 if (ptr == NULL || ptr == (void *)1) 453 { 454 if (fwrite (&ptr, sizeof (void *), 1, state->f) 455 != 1) 456 fatal_error ("can%'t write PCH file: %m"); 457 } 458 else 459 { 460 new_ptr = (struct ptr_data *) 461 htab_find_with_hash (saving_htab, ptr, POINTER_HASH (ptr)); 462 if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f) 463 != 1) 464 fatal_error ("can%'t write PCH file: %m"); 465 } 466 } 467 } 468 469 /* Hold the information we need to mmap the file back in. */ 470 471 struct mmap_info 472 { 473 size_t offset; 474 size_t size; 475 void *preferred_base; 476 }; 477 478 /* Write out the state of the compiler to F. */ 479 480 void 481 gt_pch_save (FILE *f) 482 { 483 const struct ggc_root_tab *const *rt; 484 const struct ggc_root_tab *rti; 485 size_t i; 486 struct traversal_state state; 487 char *this_object = NULL; 488 size_t this_object_size = 0; 489 struct mmap_info mmi; 490 const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity(); 491 492 gt_pch_save_stringpool (); 493 494 timevar_push (TV_PCH_PTR_REALLOC); 495 saving_htab = htab_create (50000, saving_htab_hash, saving_htab_eq, free); 496 497 for (rt = gt_ggc_rtab; *rt; rt++) 498 for (rti = *rt; rti->base != NULL; rti++) 499 for (i = 0; i < rti->nelt; i++) 500 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i)); 501 502 for (rt = gt_pch_cache_rtab; *rt; rt++) 503 for (rti = *rt; rti->base != NULL; rti++) 504 for (i = 0; i < rti->nelt; i++) 505 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i)); 506 507 /* Prepare the objects for writing, determine addresses and such. */ 508 state.f = f; 509 state.d = init_ggc_pch (); 510 state.count = 0; 511 htab_traverse (saving_htab, call_count, &state); 512 513 mmi.size = ggc_pch_total_size (state.d); 514 515 /* Try to arrange things so that no relocation is necessary, but 516 don't try very hard. On most platforms, this will always work, 517 and on the rest it's a lot of work to do better. 518 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and 519 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */ 520 mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f)); 521 522 ggc_pch_this_base (state.d, mmi.preferred_base); 523 524 state.ptrs = XNEWVEC (struct ptr_data *, state.count); 525 state.ptrs_i = 0; 526 527 htab_traverse (saving_htab, call_alloc, &state); 528 timevar_pop (TV_PCH_PTR_REALLOC); 529 530 timevar_push (TV_PCH_PTR_SORT); 531 qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data); 532 timevar_pop (TV_PCH_PTR_SORT); 533 534 /* Write out all the scalar variables. */ 535 for (rt = gt_pch_scalar_rtab; *rt; rt++) 536 for (rti = *rt; rti->base != NULL; rti++) 537 if (fwrite (rti->base, rti->stride, 1, f) != 1) 538 fatal_error ("can%'t write PCH file: %m"); 539 540 /* Write out all the global pointers, after translation. */ 541 write_pch_globals (gt_ggc_rtab, &state); 542 write_pch_globals (gt_pch_cache_rtab, &state); 543 544 /* Pad the PCH file so that the mmapped area starts on an allocation 545 granularity (usually page) boundary. */ 546 { 547 long o; 548 o = ftell (state.f) + sizeof (mmi); 549 if (o == -1) 550 fatal_error ("can%'t get position in PCH file: %m"); 551 mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment; 552 if (mmi.offset == mmap_offset_alignment) 553 mmi.offset = 0; 554 mmi.offset += o; 555 } 556 if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1) 557 fatal_error ("can%'t write PCH file: %m"); 558 if (mmi.offset != 0 559 && fseek (state.f, mmi.offset, SEEK_SET) != 0) 560 fatal_error ("can%'t write padding to PCH file: %m"); 561 562 ggc_pch_prepare_write (state.d, state.f); 563 564 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS 565 vec<char> vbits = vNULL; 566 #endif 567 568 /* Actually write out the objects. */ 569 for (i = 0; i < state.count; i++) 570 { 571 if (this_object_size < state.ptrs[i]->size) 572 { 573 this_object_size = state.ptrs[i]->size; 574 this_object = XRESIZEVAR (char, this_object, this_object_size); 575 } 576 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS 577 /* obj might contain uninitialized bytes, e.g. in the trailing 578 padding of the object. Avoid warnings by making the memory 579 temporarily defined and then restoring previous state. */ 580 int get_vbits = 0; 581 size_t valid_size = state.ptrs[i]->size; 582 if (__builtin_expect (RUNNING_ON_VALGRIND, 0)) 583 { 584 if (vbits.length () < valid_size) 585 vbits.safe_grow (valid_size); 586 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj, 587 vbits.address (), valid_size); 588 if (get_vbits == 3) 589 { 590 /* We assume that first part of obj is addressable, and 591 the rest is unaddressable. Find out where the boundary is 592 using binary search. */ 593 size_t lo = 0, hi = valid_size; 594 while (hi > lo) 595 { 596 size_t mid = (lo + hi) / 2; 597 get_vbits = VALGRIND_GET_VBITS ((char *) state.ptrs[i]->obj 598 + mid, vbits.address (), 599 1); 600 if (get_vbits == 3) 601 hi = mid; 602 else if (get_vbits == 1) 603 lo = mid + 1; 604 else 605 break; 606 } 607 if (get_vbits == 1 || get_vbits == 3) 608 { 609 valid_size = lo; 610 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj, 611 vbits.address (), 612 valid_size); 613 } 614 } 615 if (get_vbits == 1) 616 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state.ptrs[i]->obj, 617 state.ptrs[i]->size)); 618 } 619 #endif 620 memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size); 621 if (state.ptrs[i]->reorder_fn != NULL) 622 state.ptrs[i]->reorder_fn (state.ptrs[i]->obj, 623 state.ptrs[i]->note_ptr_cookie, 624 relocate_ptrs, &state); 625 state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj, 626 state.ptrs[i]->note_ptr_cookie, 627 relocate_ptrs, &state); 628 ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj, 629 state.ptrs[i]->new_addr, state.ptrs[i]->size, 630 state.ptrs[i]->note_ptr_fn == gt_pch_p_S); 631 if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S) 632 memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size); 633 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS 634 if (__builtin_expect (get_vbits == 1, 0)) 635 { 636 (void) VALGRIND_SET_VBITS (state.ptrs[i]->obj, vbits.address (), 637 valid_size); 638 if (valid_size != state.ptrs[i]->size) 639 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) 640 state.ptrs[i]->obj 641 + valid_size, 642 state.ptrs[i]->size 643 - valid_size)); 644 } 645 #endif 646 } 647 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS 648 vbits.release (); 649 #endif 650 651 ggc_pch_finish (state.d, state.f); 652 gt_pch_fixup_stringpool (); 653 654 XDELETE (state.ptrs); 655 XDELETE (this_object); 656 htab_delete (saving_htab); 657 } 658 659 /* Read the state of the compiler back in from F. */ 660 661 void 662 gt_pch_restore (FILE *f) 663 { 664 const struct ggc_root_tab *const *rt; 665 const struct ggc_root_tab *rti; 666 size_t i; 667 struct mmap_info mmi; 668 int result; 669 struct line_maps * old_line_table = line_table; 670 location_t old_input_loc = input_location; 671 672 /* Delete any deletable objects. This makes ggc_pch_read much 673 faster, as it can be sure that no GCable objects remain other 674 than the ones just read in. */ 675 for (rt = gt_ggc_deletable_rtab; *rt; rt++) 676 for (rti = *rt; rti->base != NULL; rti++) 677 memset (rti->base, 0, rti->stride); 678 679 /* Read in all the scalar variables. */ 680 for (rt = gt_pch_scalar_rtab; *rt; rt++) 681 for (rti = *rt; rti->base != NULL; rti++) 682 if (fread (rti->base, rti->stride, 1, f) != 1) { 683 line_table = old_line_table; 684 input_location = old_input_loc; 685 fatal_error ("can%'t read PCH file: %m"); 686 } 687 688 /* Read in all the global pointers, in 6 easy loops. */ 689 for (rt = gt_ggc_rtab; *rt; rt++) 690 for (rti = *rt; rti->base != NULL; rti++) 691 for (i = 0; i < rti->nelt; i++) 692 if (fread ((char *)rti->base + rti->stride * i, 693 sizeof (void *), 1, f) != 1) { 694 line_table = old_line_table; 695 input_location = old_input_loc; 696 fatal_error ("can%'t read PCH file: %m"); 697 } 698 699 for (rt = gt_pch_cache_rtab; *rt; rt++) 700 for (rti = *rt; rti->base != NULL; rti++) 701 for (i = 0; i < rti->nelt; i++) 702 if (fread ((char *)rti->base + rti->stride * i, 703 sizeof (void *), 1, f) != 1) { 704 line_table = old_line_table; 705 input_location = old_input_loc; 706 fatal_error ("can%'t read PCH file: %m"); 707 } 708 709 if (fread (&mmi, sizeof (mmi), 1, f) != 1) { 710 line_table = old_line_table; 711 input_location = old_input_loc; 712 fatal_error ("can%'t read PCH file: %m"); 713 } 714 715 result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size, 716 fileno (f), mmi.offset); 717 if (result < 0) { 718 line_table = old_line_table; 719 input_location = old_input_loc; 720 fatal_error ("had to relocate PCH"); 721 } 722 if (result == 0) 723 { 724 if (fseek (f, mmi.offset, SEEK_SET) != 0 725 || fread (mmi.preferred_base, mmi.size, 1, f) != 1) { 726 line_table = old_line_table; 727 input_location = old_input_loc; 728 fatal_error ("can%'t read PCH file: %m"); 729 } 730 } 731 else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0) { 732 line_table = old_line_table; 733 input_location = old_input_loc; 734 fatal_error ("can%'t read PCH file: %m"); 735 } 736 737 ggc_pch_read (f, mmi.preferred_base); 738 739 gt_pch_restore_stringpool (); 740 } 741 742 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present. 743 Select no address whatsoever, and let gt_pch_save choose what it will with 744 malloc, presumably. */ 745 746 void * 747 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED, 748 int fd ATTRIBUTE_UNUSED) 749 { 750 return NULL; 751 } 752 753 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present. 754 Allocate SIZE bytes with malloc. Return 0 if the address we got is the 755 same as base, indicating that the memory has been allocated but needs to 756 be read in from the file. Return -1 if the address differs, to relocation 757 of the PCH file would be required. */ 758 759 int 760 default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED, 761 size_t offset ATTRIBUTE_UNUSED) 762 { 763 void *addr = xmalloc (size); 764 return (addr == base) - 1; 765 } 766 767 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the 768 alignment required for allocating virtual memory. Usually this is the 769 same as pagesize. */ 770 771 size_t 772 default_gt_pch_alloc_granularity (void) 773 { 774 return getpagesize(); 775 } 776 777 #if HAVE_MMAP_FILE 778 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present. 779 We temporarily allocate SIZE bytes, and let the kernel place the data 780 wherever it will. If it worked, that's our spot, if not we're likely 781 to be in trouble. */ 782 783 void * 784 mmap_gt_pch_get_address (size_t size, int fd) 785 { 786 void *ret; 787 788 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 789 if (ret == (void *) MAP_FAILED) 790 ret = NULL; 791 else 792 munmap ((caddr_t) ret, size); 793 794 return ret; 795 } 796 797 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present. 798 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at 799 mapping the data at BASE, -1 if we couldn't. 800 801 This version assumes that the kernel honors the START operand of mmap 802 even without MAP_FIXED if START through START+SIZE are not currently 803 mapped with something. */ 804 805 int 806 mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset) 807 { 808 void *addr; 809 810 /* We're called with size == 0 if we're not planning to load a PCH 811 file at all. This allows the hook to free any static space that 812 we might have allocated at link time. */ 813 if (size == 0) 814 return -1; 815 816 addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, 817 fd, offset); 818 819 return addr == base ? 1 : -1; 820 } 821 #endif /* HAVE_MMAP_FILE */ 822 823 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT 824 825 /* Modify the bound based on rlimits. */ 826 static double 827 ggc_rlimit_bound (double limit) 828 { 829 #if defined(HAVE_GETRLIMIT) 830 struct rlimit rlim; 831 # if defined (RLIMIT_AS) 832 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably 833 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */ 834 if (getrlimit (RLIMIT_AS, &rlim) == 0 835 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY 836 && rlim.rlim_cur < limit) 837 limit = rlim.rlim_cur; 838 # elif defined (RLIMIT_DATA) 839 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we 840 might be on an OS that has a broken mmap. (Others don't bound 841 mmap at all, apparently.) */ 842 if (getrlimit (RLIMIT_DATA, &rlim) == 0 843 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY 844 && rlim.rlim_cur < limit 845 /* Darwin has this horribly bogus default setting of 846 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA 847 appears to be ignored. Ignore such silliness. If a limit 848 this small was actually effective for mmap, GCC wouldn't even 849 start up. */ 850 && rlim.rlim_cur >= 8 * 1024 * 1024) 851 limit = rlim.rlim_cur; 852 # endif /* RLIMIT_AS or RLIMIT_DATA */ 853 #endif /* HAVE_GETRLIMIT */ 854 855 return limit; 856 } 857 858 /* Heuristic to set a default for GGC_MIN_EXPAND. */ 859 static int 860 ggc_min_expand_heuristic (void) 861 { 862 double min_expand = physmem_total(); 863 864 /* Adjust for rlimits. */ 865 min_expand = ggc_rlimit_bound (min_expand); 866 867 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding 868 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */ 869 min_expand /= 1024*1024*1024; 870 min_expand *= 70; 871 min_expand = MIN (min_expand, 70); 872 min_expand += 30; 873 874 return min_expand; 875 } 876 877 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */ 878 static int 879 ggc_min_heapsize_heuristic (void) 880 { 881 double phys_kbytes = physmem_total(); 882 double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2); 883 884 phys_kbytes /= 1024; /* Convert to Kbytes. */ 885 limit_kbytes /= 1024; 886 887 /* The heuristic is RAM/8, with a lower bound of 4M and an upper 888 bound of 128M (when RAM >= 1GB). */ 889 phys_kbytes /= 8; 890 891 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS) 892 /* Try not to overrun the RSS limit while doing garbage collection. 893 The RSS limit is only advisory, so no margin is subtracted. */ 894 { 895 struct rlimit rlim; 896 if (getrlimit (RLIMIT_RSS, &rlim) == 0 897 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY) 898 phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / 1024); 899 } 900 # endif 901 902 /* Don't blindly run over our data limit; do GC at least when the 903 *next* GC would be within 20Mb of the limit or within a quarter of 904 the limit, whichever is larger. If GCC does hit the data limit, 905 compilation will fail, so this tries to be conservative. */ 906 limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * 1024)); 907 limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic ()); 908 phys_kbytes = MIN (phys_kbytes, limit_kbytes); 909 910 phys_kbytes = MAX (phys_kbytes, 4 * 1024); 911 phys_kbytes = MIN (phys_kbytes, 128 * 1024); 912 913 return phys_kbytes; 914 } 915 #endif 916 917 void 918 init_ggc_heuristics (void) 919 { 920 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT 921 set_default_param_value (GGC_MIN_EXPAND, ggc_min_expand_heuristic ()); 922 set_default_param_value (GGC_MIN_HEAPSIZE, ggc_min_heapsize_heuristic ()); 923 #endif 924 } 925 926 /* Datastructure used to store per-call-site statistics. */ 927 struct loc_descriptor 928 { 929 const char *file; 930 int line; 931 const char *function; 932 int times; 933 size_t allocated; 934 size_t overhead; 935 size_t freed; 936 size_t collected; 937 }; 938 939 /* Hashtable used for statistics. */ 940 static htab_t loc_hash; 941 942 /* Hash table helpers functions. */ 943 static hashval_t 944 hash_descriptor (const void *p) 945 { 946 const struct loc_descriptor *const d = (const struct loc_descriptor *) p; 947 948 return htab_hash_pointer (d->function) | d->line; 949 } 950 951 static int 952 eq_descriptor (const void *p1, const void *p2) 953 { 954 const struct loc_descriptor *const d = (const struct loc_descriptor *) p1; 955 const struct loc_descriptor *const d2 = (const struct loc_descriptor *) p2; 956 957 return (d->file == d2->file && d->line == d2->line 958 && d->function == d2->function); 959 } 960 961 /* Hashtable converting address of allocated field to loc descriptor. */ 962 static htab_t ptr_hash; 963 struct ptr_hash_entry 964 { 965 void *ptr; 966 struct loc_descriptor *loc; 967 size_t size; 968 }; 969 970 /* Hash table helpers functions. */ 971 static hashval_t 972 hash_ptr (const void *p) 973 { 974 const struct ptr_hash_entry *const d = (const struct ptr_hash_entry *) p; 975 976 return htab_hash_pointer (d->ptr); 977 } 978 979 static int 980 eq_ptr (const void *p1, const void *p2) 981 { 982 const struct ptr_hash_entry *const p = (const struct ptr_hash_entry *) p1; 983 984 return (p->ptr == p2); 985 } 986 987 /* Return descriptor for given call site, create new one if needed. */ 988 static struct loc_descriptor * 989 loc_descriptor (const char *name, int line, const char *function) 990 { 991 struct loc_descriptor loc; 992 struct loc_descriptor **slot; 993 994 loc.file = name; 995 loc.line = line; 996 loc.function = function; 997 if (!loc_hash) 998 loc_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL); 999 1000 slot = (struct loc_descriptor **) htab_find_slot (loc_hash, &loc, INSERT); 1001 if (*slot) 1002 return *slot; 1003 *slot = XCNEW (struct loc_descriptor); 1004 (*slot)->file = name; 1005 (*slot)->line = line; 1006 (*slot)->function = function; 1007 return *slot; 1008 } 1009 1010 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */ 1011 void 1012 ggc_record_overhead (size_t allocated, size_t overhead, void *ptr, 1013 const char *name, int line, const char *function) 1014 { 1015 struct loc_descriptor *loc = loc_descriptor (name, line, function); 1016 struct ptr_hash_entry *p = XNEW (struct ptr_hash_entry); 1017 PTR *slot; 1018 1019 p->ptr = ptr; 1020 p->loc = loc; 1021 p->size = allocated + overhead; 1022 if (!ptr_hash) 1023 ptr_hash = htab_create (10, hash_ptr, eq_ptr, NULL); 1024 slot = htab_find_slot_with_hash (ptr_hash, ptr, htab_hash_pointer (ptr), INSERT); 1025 gcc_assert (!*slot); 1026 *slot = p; 1027 1028 loc->times++; 1029 loc->allocated+=allocated; 1030 loc->overhead+=overhead; 1031 } 1032 1033 /* Helper function for prune_overhead_list. See if SLOT is still marked and 1034 remove it from hashtable if it is not. */ 1035 static int 1036 ggc_prune_ptr (void **slot, void *b ATTRIBUTE_UNUSED) 1037 { 1038 struct ptr_hash_entry *p = (struct ptr_hash_entry *) *slot; 1039 if (!ggc_marked_p (p->ptr)) 1040 { 1041 p->loc->collected += p->size; 1042 htab_clear_slot (ptr_hash, slot); 1043 free (p); 1044 } 1045 return 1; 1046 } 1047 1048 /* After live values has been marked, walk all recorded pointers and see if 1049 they are still live. */ 1050 void 1051 ggc_prune_overhead_list (void) 1052 { 1053 htab_traverse (ptr_hash, ggc_prune_ptr, NULL); 1054 } 1055 1056 /* Notice that the pointer has been freed. */ 1057 void 1058 ggc_free_overhead (void *ptr) 1059 { 1060 PTR *slot = htab_find_slot_with_hash (ptr_hash, ptr, htab_hash_pointer (ptr), 1061 NO_INSERT); 1062 struct ptr_hash_entry *p; 1063 /* The pointer might be not found if a PCH read happened between allocation 1064 and ggc_free () call. FIXME: account memory properly in the presence of 1065 PCH. */ 1066 if (!slot) 1067 return; 1068 p = (struct ptr_hash_entry *) *slot; 1069 p->loc->freed += p->size; 1070 htab_clear_slot (ptr_hash, slot); 1071 free (p); 1072 } 1073 1074 /* Helper for qsort; sort descriptors by amount of memory consumed. */ 1075 static int 1076 final_cmp_statistic (const void *loc1, const void *loc2) 1077 { 1078 const struct loc_descriptor *const l1 = 1079 *(const struct loc_descriptor *const *) loc1; 1080 const struct loc_descriptor *const l2 = 1081 *(const struct loc_descriptor *const *) loc2; 1082 long diff; 1083 diff = ((long)(l1->allocated + l1->overhead - l1->freed) - 1084 (l2->allocated + l2->overhead - l2->freed)); 1085 return diff > 0 ? 1 : diff < 0 ? -1 : 0; 1086 } 1087 1088 /* Helper for qsort; sort descriptors by amount of memory consumed. */ 1089 static int 1090 cmp_statistic (const void *loc1, const void *loc2) 1091 { 1092 const struct loc_descriptor *const l1 = 1093 *(const struct loc_descriptor *const *) loc1; 1094 const struct loc_descriptor *const l2 = 1095 *(const struct loc_descriptor *const *) loc2; 1096 long diff; 1097 1098 diff = ((long)(l1->allocated + l1->overhead - l1->freed - l1->collected) - 1099 (l2->allocated + l2->overhead - l2->freed - l2->collected)); 1100 if (diff) 1101 return diff > 0 ? 1 : diff < 0 ? -1 : 0; 1102 diff = ((long)(l1->allocated + l1->overhead - l1->freed) - 1103 (l2->allocated + l2->overhead - l2->freed)); 1104 return diff > 0 ? 1 : diff < 0 ? -1 : 0; 1105 } 1106 1107 /* Collect array of the descriptors from hashtable. */ 1108 static struct loc_descriptor **loc_array; 1109 static int 1110 add_statistics (void **slot, void *b) 1111 { 1112 int *n = (int *)b; 1113 loc_array[*n] = (struct loc_descriptor *) *slot; 1114 (*n)++; 1115 return 1; 1116 } 1117 1118 /* Dump per-site memory statistics. */ 1119 1120 void 1121 dump_ggc_loc_statistics (bool final) 1122 { 1123 int nentries = 0; 1124 char s[4096]; 1125 size_t collected = 0, freed = 0, allocated = 0, overhead = 0, times = 0; 1126 int i; 1127 1128 if (! GATHER_STATISTICS) 1129 return; 1130 1131 ggc_force_collect = true; 1132 ggc_collect (); 1133 1134 loc_array = XCNEWVEC (struct loc_descriptor *, loc_hash->n_elements); 1135 fprintf (stderr, "-------------------------------------------------------\n"); 1136 fprintf (stderr, "\n%-48s %10s %10s %10s %10s %10s\n", 1137 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times"); 1138 fprintf (stderr, "-------------------------------------------------------\n"); 1139 htab_traverse (loc_hash, add_statistics, &nentries); 1140 qsort (loc_array, nentries, sizeof (*loc_array), 1141 final ? final_cmp_statistic : cmp_statistic); 1142 for (i = 0; i < nentries; i++) 1143 { 1144 struct loc_descriptor *d = loc_array[i]; 1145 allocated += d->allocated; 1146 times += d->times; 1147 freed += d->freed; 1148 collected += d->collected; 1149 overhead += d->overhead; 1150 } 1151 for (i = 0; i < nentries; i++) 1152 { 1153 struct loc_descriptor *d = loc_array[i]; 1154 if (d->allocated) 1155 { 1156 const char *s1 = d->file; 1157 const char *s2; 1158 while ((s2 = strstr (s1, "gcc/"))) 1159 s1 = s2 + 4; 1160 sprintf (s, "%s:%i (%s)", s1, d->line, d->function); 1161 s[48] = 0; 1162 fprintf (stderr, "%-48s %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li\n", s, 1163 (long)d->collected, 1164 (d->collected) * 100.0 / collected, 1165 (long)d->freed, 1166 (d->freed) * 100.0 / freed, 1167 (long)(d->allocated + d->overhead - d->freed - d->collected), 1168 (d->allocated + d->overhead - d->freed - d->collected) * 100.0 1169 / (allocated + overhead - freed - collected), 1170 (long)d->overhead, 1171 d->overhead * 100.0 / overhead, 1172 (long)d->times); 1173 } 1174 } 1175 fprintf (stderr, "%-48s %10ld %10ld %10ld %10ld %10ld\n", 1176 "Total", (long)collected, (long)freed, 1177 (long)(allocated + overhead - freed - collected), (long)overhead, 1178 (long)times); 1179 fprintf (stderr, "%-48s %10s %10s %10s %10s %10s\n", 1180 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times"); 1181 fprintf (stderr, "-------------------------------------------------------\n"); 1182 ggc_force_collect = false; 1183 } 1184