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