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