Mercurial > hg > graal-compiler
annotate src/share/vm/memory/genMarkSweep.cpp @ 1039:987e948ebbc8
6815692: method handle code needs some cleanup (post-6655638)
Summary: correctly raise exceptions, support safe bitwise "raw" conversions, fix bugs revealed by VerifyMethodHandles, remove dead code, improve debugging support
Reviewed-by: never, twisti
author | jrose |
---|---|
date | Sat, 17 Oct 2009 19:51:05 -0700 |
parents | a1423fe86a18 |
children | 2a1472c30599 |
rev | line source |
---|---|
0 | 1 /* |
948 | 2 * Copyright 2001-2009 Sun Microsystems, Inc. All Rights Reserved. |
0 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
20 * CA 95054 USA or visit www.sun.com if you need additional information or | |
21 * have any questions. | |
22 * | |
23 */ | |
24 | |
25 #include "incls/_precompiled.incl" | |
26 #include "incls/_genMarkSweep.cpp.incl" | |
27 | |
28 void GenMarkSweep::invoke_at_safepoint(int level, ReferenceProcessor* rp, | |
29 bool clear_all_softrefs) { | |
30 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); | |
31 | |
32 // hook up weak ref data so it can be used during Mark-Sweep | |
33 assert(ref_processor() == NULL, "no stomping"); | |
453
c96030fff130
6684579: SoftReference processing can be made more efficient
ysr
parents:
356
diff
changeset
|
34 assert(rp != NULL, "should be non-NULL"); |
0 | 35 _ref_processor = rp; |
457
27a80744a83b
6778647: snap(), snap_policy() should be renamed setup(), setup_policy()
ysr
parents:
453
diff
changeset
|
36 rp->setup_policy(clear_all_softrefs); |
0 | 37 |
38 TraceTime t1("Full GC", PrintGC && !PrintGCDetails, true, gclog_or_tty); | |
39 | |
40 // When collecting the permanent generation methodOops may be moving, | |
41 // so we either have to flush all bcp data or convert it into bci. | |
42 CodeCache::gc_prologue(); | |
43 Threads::gc_prologue(); | |
44 | |
45 // Increment the invocation count for the permanent generation, since it is | |
46 // implicitly collected whenever we do a full mark sweep collection. | |
47 GenCollectedHeap* gch = GenCollectedHeap::heap(); | |
48 gch->perm_gen()->stat_record()->invocations++; | |
49 | |
50 // Capture heap size before collection for printing. | |
51 size_t gch_prev_used = gch->used(); | |
52 | |
53 // Some of the card table updates below assume that the perm gen is | |
54 // also being collected. | |
55 assert(level == gch->n_gens() - 1, | |
56 "All generations are being collected, ergo perm gen too."); | |
57 | |
58 // Capture used regions for each generation that will be | |
59 // subject to collection, so that card table adjustments can | |
60 // be made intelligently (see clear / invalidate further below). | |
61 gch->save_used_regions(level, true /* perm */); | |
62 | |
63 allocate_stacks(); | |
64 | |
65 mark_sweep_phase1(level, clear_all_softrefs); | |
66 | |
67 mark_sweep_phase2(); | |
68 | |
69 // Don't add any more derived pointers during phase3 | |
70 COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity")); | |
71 COMPILER2_PRESENT(DerivedPointerTable::set_active(false)); | |
72 | |
73 mark_sweep_phase3(level); | |
74 | |
75 VALIDATE_MARK_SWEEP_ONLY( | |
76 if (ValidateMarkSweep) { | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
77 guarantee(_root_refs_stack->length() == 0, "should be empty by now"); |
0 | 78 } |
79 ) | |
80 | |
81 mark_sweep_phase4(); | |
82 | |
83 VALIDATE_MARK_SWEEP_ONLY( | |
84 if (ValidateMarkSweep) { | |
85 guarantee(_live_oops->length() == _live_oops_moved_to->length(), | |
86 "should be the same size"); | |
87 } | |
88 ) | |
89 | |
90 restore_marks(); | |
91 | |
92 // Set saved marks for allocation profiler (and other things? -- dld) | |
93 // (Should this be in general part?) | |
94 gch->save_marks(); | |
95 | |
96 deallocate_stacks(); | |
97 | |
98 // If compaction completely evacuated all generations younger than this | |
99 // one, then we can clear the card table. Otherwise, we must invalidate | |
100 // it (consider all cards dirty). In the future, we might consider doing | |
101 // compaction within generations only, and doing card-table sliding. | |
102 bool all_empty = true; | |
103 for (int i = 0; all_empty && i < level; i++) { | |
104 Generation* g = gch->get_gen(i); | |
105 all_empty = all_empty && gch->get_gen(i)->used() == 0; | |
106 } | |
107 GenRemSet* rs = gch->rem_set(); | |
108 // Clear/invalidate below make use of the "prev_used_regions" saved earlier. | |
109 if (all_empty) { | |
110 // We've evacuated all generations below us. | |
111 Generation* g = gch->get_gen(level); | |
112 rs->clear_into_younger(g, true /* perm */); | |
113 } else { | |
114 // Invalidate the cards corresponding to the currently used | |
115 // region and clear those corresponding to the evacuated region | |
116 // of all generations just collected (i.e. level and younger). | |
117 rs->invalidate_or_clear(gch->get_gen(level), | |
118 true /* younger */, | |
119 true /* perm */); | |
120 } | |
121 | |
122 Threads::gc_epilogue(); | |
123 CodeCache::gc_epilogue(); | |
124 | |
125 if (PrintGC && !PrintGCDetails) { | |
126 gch->print_heap_change(gch_prev_used); | |
127 } | |
128 | |
129 // refs processing: clean slate | |
130 _ref_processor = NULL; | |
131 | |
132 // Update heap occupancy information which is used as | |
133 // input to soft ref clearing policy at the next gc. | |
134 Universe::update_heap_info_at_gc(); | |
135 | |
136 // Update time of last gc for all generations we collected | |
137 // (which curently is all the generations in the heap). | |
138 gch->update_time_of_last_gc(os::javaTimeMillis()); | |
139 } | |
140 | |
141 void GenMarkSweep::allocate_stacks() { | |
142 GenCollectedHeap* gch = GenCollectedHeap::heap(); | |
143 // Scratch request on behalf of oldest generation; will do no | |
144 // allocation. | |
145 ScratchBlock* scratch = gch->gather_scratch(gch->_gens[gch->_n_gens-1], 0); | |
146 | |
147 // $$$ To cut a corner, we'll only use the first scratch block, and then | |
148 // revert to malloc. | |
149 if (scratch != NULL) { | |
150 _preserved_count_max = | |
151 scratch->num_words * HeapWordSize / sizeof(PreservedMark); | |
152 } else { | |
153 _preserved_count_max = 0; | |
154 } | |
155 | |
156 _preserved_marks = (PreservedMark*)scratch; | |
157 _preserved_count = 0; | |
158 _preserved_mark_stack = NULL; | |
159 _preserved_oop_stack = NULL; | |
160 | |
161 _marking_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(4000, true); | |
162 | |
163 int size = SystemDictionary::number_of_classes() * 2; | |
164 _revisit_klass_stack = new (ResourceObj::C_HEAP) GrowableArray<Klass*>(size, true); | |
941 | 165 // (#klass/k)^2 for k ~ 10 appears to be a better fit, but this will have to do for |
166 // now until we have had a chance to investigate a more optimal setting. | |
167 _revisit_mdo_stack = new (ResourceObj::C_HEAP) GrowableArray<DataLayout*>(2*size, true); | |
0 | 168 |
169 #ifdef VALIDATE_MARK_SWEEP | |
170 if (ValidateMarkSweep) { | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
171 _root_refs_stack = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true); |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
172 _other_refs_stack = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true); |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
173 _adjusted_pointers = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true); |
0 | 174 _live_oops = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true); |
175 _live_oops_moved_to = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true); | |
176 _live_oops_size = new (ResourceObj::C_HEAP) GrowableArray<size_t>(100, true); | |
177 } | |
178 if (RecordMarkSweepCompaction) { | |
179 if (_cur_gc_live_oops == NULL) { | |
180 _cur_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true); | |
181 _cur_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true); | |
182 _cur_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true); | |
183 _last_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true); | |
184 _last_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true); | |
185 _last_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true); | |
186 } else { | |
187 _cur_gc_live_oops->clear(); | |
188 _cur_gc_live_oops_moved_to->clear(); | |
189 _cur_gc_live_oops_size->clear(); | |
190 } | |
191 } | |
192 #endif | |
193 } | |
194 | |
195 | |
196 void GenMarkSweep::deallocate_stacks() { | |
263
12eea04c8b06
6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents:
113
diff
changeset
|
197 |
356 | 198 if (!UseG1GC) { |
199 GenCollectedHeap* gch = GenCollectedHeap::heap(); | |
200 gch->release_scratch(); | |
201 } | |
263
12eea04c8b06
6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents:
113
diff
changeset
|
202 |
0 | 203 if (_preserved_oop_stack) { |
204 delete _preserved_mark_stack; | |
205 _preserved_mark_stack = NULL; | |
206 delete _preserved_oop_stack; | |
207 _preserved_oop_stack = NULL; | |
208 } | |
209 | |
210 delete _marking_stack; | |
211 delete _revisit_klass_stack; | |
941 | 212 delete _revisit_mdo_stack; |
0 | 213 |
214 #ifdef VALIDATE_MARK_SWEEP | |
215 if (ValidateMarkSweep) { | |
216 delete _root_refs_stack; | |
217 delete _other_refs_stack; | |
218 delete _adjusted_pointers; | |
219 delete _live_oops; | |
220 delete _live_oops_size; | |
221 delete _live_oops_moved_to; | |
222 _live_oops_index = 0; | |
223 _live_oops_index_at_perm = 0; | |
224 } | |
225 #endif | |
226 } | |
227 | |
228 void GenMarkSweep::mark_sweep_phase1(int level, | |
229 bool clear_all_softrefs) { | |
230 // Recursively traverse all live objects and mark them | |
231 EventMark m("1 mark object"); | |
232 TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty); | |
233 trace(" 1"); | |
234 | |
235 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false)); | |
236 | |
237 GenCollectedHeap* gch = GenCollectedHeap::heap(); | |
238 | |
239 // Because follow_root_closure is created statically, cannot | |
240 // use OopsInGenClosure constructor which takes a generation, | |
241 // as the Universe has not been created when the static constructors | |
242 // are run. | |
243 follow_root_closure.set_orig_generation(gch->get_gen(level)); | |
244 | |
245 gch->gen_process_strong_roots(level, | |
246 false, // Younger gens are not roots. | |
989
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
457
diff
changeset
|
247 true, // activate StrongRootsScope |
0 | 248 true, // Collecting permanent generation. |
249 SharedHeap::SO_SystemClasses, | |
989
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
457
diff
changeset
|
250 &follow_root_closure, |
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
457
diff
changeset
|
251 true, // walk code active on stacks |
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
457
diff
changeset
|
252 &follow_root_closure); |
0 | 253 |
254 // Process reference objects found during marking | |
255 { | |
457
27a80744a83b
6778647: snap(), snap_policy() should be renamed setup(), setup_policy()
ysr
parents:
453
diff
changeset
|
256 ref_processor()->setup_policy(clear_all_softrefs); |
0 | 257 ref_processor()->process_discovered_references( |
453
c96030fff130
6684579: SoftReference processing can be made more efficient
ysr
parents:
356
diff
changeset
|
258 &is_alive, &keep_alive, &follow_stack_closure, NULL); |
0 | 259 } |
260 | |
261 // Follow system dictionary roots and unload classes | |
262 bool purged_class = SystemDictionary::do_unloading(&is_alive); | |
263 | |
264 // Follow code cache roots | |
265 CodeCache::do_unloading(&is_alive, &keep_alive, purged_class); | |
266 follow_stack(); // Flush marking stack | |
267 | |
268 // Update subklass/sibling/implementor links of live klasses | |
269 follow_weak_klass_links(); | |
270 assert(_marking_stack->is_empty(), "just drained"); | |
271 | |
941 | 272 // Visit memoized MDO's and clear any unmarked weak refs |
273 follow_mdo_weak_refs(); | |
274 assert(_marking_stack->is_empty(), "just drained"); | |
275 | |
0 | 276 // Visit symbol and interned string tables and delete unmarked oops |
277 SymbolTable::unlink(&is_alive); | |
278 StringTable::unlink(&is_alive); | |
279 | |
280 assert(_marking_stack->is_empty(), "stack should be empty by now"); | |
281 } | |
282 | |
283 | |
284 void GenMarkSweep::mark_sweep_phase2() { | |
285 // Now all live objects are marked, compute the new object addresses. | |
286 | |
287 // It is imperative that we traverse perm_gen LAST. If dead space is | |
288 // allowed a range of dead object may get overwritten by a dead int | |
289 // array. If perm_gen is not traversed last a klassOop may get | |
290 // overwritten. This is fine since it is dead, but if the class has dead | |
291 // instances we have to skip them, and in order to find their size we | |
292 // need the klassOop! | |
293 // | |
294 // It is not required that we traverse spaces in the same order in | |
295 // phase2, phase3 and phase4, but the ValidateMarkSweep live oops | |
296 // tracking expects us to do so. See comment under phase4. | |
297 | |
298 GenCollectedHeap* gch = GenCollectedHeap::heap(); | |
299 Generation* pg = gch->perm_gen(); | |
300 | |
301 EventMark m("2 compute new addresses"); | |
302 TraceTime tm("phase 2", PrintGC && Verbose, true, gclog_or_tty); | |
303 trace("2"); | |
304 | |
305 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false)); | |
306 | |
307 gch->prepare_for_compaction(); | |
308 | |
309 VALIDATE_MARK_SWEEP_ONLY(_live_oops_index_at_perm = _live_oops_index); | |
310 CompactPoint perm_cp(pg, NULL, NULL); | |
311 pg->prepare_for_compaction(&perm_cp); | |
312 } | |
313 | |
314 class GenAdjustPointersClosure: public GenCollectedHeap::GenClosure { | |
315 public: | |
316 void do_generation(Generation* gen) { | |
317 gen->adjust_pointers(); | |
318 } | |
319 }; | |
320 | |
321 void GenMarkSweep::mark_sweep_phase3(int level) { | |
322 GenCollectedHeap* gch = GenCollectedHeap::heap(); | |
323 Generation* pg = gch->perm_gen(); | |
324 | |
325 // Adjust the pointers to reflect the new locations | |
326 EventMark m("3 adjust pointers"); | |
327 TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty); | |
328 trace("3"); | |
329 | |
330 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false)); | |
331 | |
332 // Needs to be done before the system dictionary is adjusted. | |
333 pg->pre_adjust_pointers(); | |
334 | |
335 // Because the two closures below are created statically, cannot | |
336 // use OopsInGenClosure constructor which takes a generation, | |
337 // as the Universe has not been created when the static constructors | |
338 // are run. | |
339 adjust_root_pointer_closure.set_orig_generation(gch->get_gen(level)); | |
340 adjust_pointer_closure.set_orig_generation(gch->get_gen(level)); | |
341 | |
342 gch->gen_process_strong_roots(level, | |
343 false, // Younger gens are not roots. | |
989
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
457
diff
changeset
|
344 true, // activate StrongRootsScope |
0 | 345 true, // Collecting permanent generation. |
346 SharedHeap::SO_AllClasses, | |
347 &adjust_root_pointer_closure, | |
989
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
457
diff
changeset
|
348 false, // do not walk code |
0 | 349 &adjust_root_pointer_closure); |
350 | |
351 // Now adjust pointers in remaining weak roots. (All of which should | |
352 // have been cleared if they pointed to non-surviving objects.) | |
989
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
457
diff
changeset
|
353 CodeBlobToOopClosure adjust_code_pointer_closure(&adjust_pointer_closure, |
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
457
diff
changeset
|
354 /*do_marking=*/ false); |
0 | 355 gch->gen_process_weak_roots(&adjust_root_pointer_closure, |
989
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
457
diff
changeset
|
356 &adjust_code_pointer_closure, |
0 | 357 &adjust_pointer_closure); |
358 | |
359 adjust_marks(); | |
360 GenAdjustPointersClosure blk; | |
361 gch->generation_iterate(&blk, true); | |
362 pg->adjust_pointers(); | |
363 } | |
364 | |
365 class GenCompactClosure: public GenCollectedHeap::GenClosure { | |
366 public: | |
367 void do_generation(Generation* gen) { | |
368 gen->compact(); | |
369 } | |
370 }; | |
371 | |
372 void GenMarkSweep::mark_sweep_phase4() { | |
373 // All pointers are now adjusted, move objects accordingly | |
374 | |
375 // It is imperative that we traverse perm_gen first in phase4. All | |
376 // classes must be allocated earlier than their instances, and traversing | |
377 // perm_gen first makes sure that all klassOops have moved to their new | |
378 // location before any instance does a dispatch through it's klass! | |
379 | |
380 // The ValidateMarkSweep live oops tracking expects us to traverse spaces | |
381 // in the same order in phase2, phase3 and phase4. We don't quite do that | |
382 // here (perm_gen first rather than last), so we tell the validate code | |
383 // to use a higher index (saved from phase2) when verifying perm_gen. | |
384 GenCollectedHeap* gch = GenCollectedHeap::heap(); | |
385 Generation* pg = gch->perm_gen(); | |
386 | |
387 EventMark m("4 compact heap"); | |
388 TraceTime tm("phase 4", PrintGC && Verbose, true, gclog_or_tty); | |
389 trace("4"); | |
390 | |
391 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(true)); | |
392 | |
393 pg->compact(); | |
394 | |
395 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false)); | |
396 | |
397 GenCompactClosure blk; | |
398 gch->generation_iterate(&blk, true); | |
399 | |
400 VALIDATE_MARK_SWEEP_ONLY(compaction_complete()); | |
401 | |
402 pg->post_compact(); // Shared spaces verification. | |
403 } |