Thu, 27 Jan 2011 16:11:27 -0800
6990754: Use native memory and reference counting to implement SymbolTable
Summary: move symbols from permgen into C heap and reference count them
Reviewed-by: never, acorn, jmasa, stefank
1 /*
2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "classfile/symbolTable.hpp"
28 #include "classfile/systemDictionary.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "code/codeCache.hpp"
31 #include "code/icBuffer.hpp"
32 #include "gc_interface/collectedHeap.inline.hpp"
33 #include "memory/genCollectedHeap.hpp"
34 #include "memory/genMarkSweep.hpp"
35 #include "memory/genOopClosures.inline.hpp"
36 #include "memory/generation.inline.hpp"
37 #include "memory/modRefBarrierSet.hpp"
38 #include "memory/referencePolicy.hpp"
39 #include "memory/space.hpp"
40 #include "oops/instanceRefKlass.hpp"
41 #include "oops/oop.inline.hpp"
42 #include "prims/jvmtiExport.hpp"
43 #include "runtime/fprofiler.hpp"
44 #include "runtime/handles.inline.hpp"
45 #include "runtime/synchronizer.hpp"
46 #include "runtime/vmThread.hpp"
47 #include "utilities/copy.hpp"
48 #include "utilities/events.hpp"
49 #ifdef TARGET_OS_FAMILY_linux
50 # include "thread_linux.inline.hpp"
51 #endif
52 #ifdef TARGET_OS_FAMILY_solaris
53 # include "thread_solaris.inline.hpp"
54 #endif
55 #ifdef TARGET_OS_FAMILY_windows
56 # include "thread_windows.inline.hpp"
57 #endif
59 void GenMarkSweep::invoke_at_safepoint(int level, ReferenceProcessor* rp,
60 bool clear_all_softrefs) {
61 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
63 GenCollectedHeap* gch = GenCollectedHeap::heap();
64 #ifdef ASSERT
65 if (gch->collector_policy()->should_clear_all_soft_refs()) {
66 assert(clear_all_softrefs, "Policy should have been checked earlier");
67 }
68 #endif
70 // hook up weak ref data so it can be used during Mark-Sweep
71 assert(ref_processor() == NULL, "no stomping");
72 assert(rp != NULL, "should be non-NULL");
73 _ref_processor = rp;
74 rp->setup_policy(clear_all_softrefs);
76 TraceTime t1("Full GC", PrintGC && !PrintGCDetails, true, gclog_or_tty);
78 // When collecting the permanent generation methodOops may be moving,
79 // so we either have to flush all bcp data or convert it into bci.
80 CodeCache::gc_prologue();
81 Threads::gc_prologue();
83 // Increment the invocation count for the permanent generation, since it is
84 // implicitly collected whenever we do a full mark sweep collection.
85 gch->perm_gen()->stat_record()->invocations++;
87 // Capture heap size before collection for printing.
88 size_t gch_prev_used = gch->used();
90 // Some of the card table updates below assume that the perm gen is
91 // also being collected.
92 assert(level == gch->n_gens() - 1,
93 "All generations are being collected, ergo perm gen too.");
95 // Capture used regions for each generation that will be
96 // subject to collection, so that card table adjustments can
97 // be made intelligently (see clear / invalidate further below).
98 gch->save_used_regions(level, true /* perm */);
100 allocate_stacks();
102 mark_sweep_phase1(level, clear_all_softrefs);
104 mark_sweep_phase2();
106 // Don't add any more derived pointers during phase3
107 COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity"));
108 COMPILER2_PRESENT(DerivedPointerTable::set_active(false));
110 mark_sweep_phase3(level);
112 VALIDATE_MARK_SWEEP_ONLY(
113 if (ValidateMarkSweep) {
114 guarantee(_root_refs_stack->length() == 0, "should be empty by now");
115 }
116 )
118 mark_sweep_phase4();
120 VALIDATE_MARK_SWEEP_ONLY(
121 if (ValidateMarkSweep) {
122 guarantee(_live_oops->length() == _live_oops_moved_to->length(),
123 "should be the same size");
124 }
125 )
127 restore_marks();
129 // Set saved marks for allocation profiler (and other things? -- dld)
130 // (Should this be in general part?)
131 gch->save_marks();
133 deallocate_stacks();
135 // If compaction completely evacuated all generations younger than this
136 // one, then we can clear the card table. Otherwise, we must invalidate
137 // it (consider all cards dirty). In the future, we might consider doing
138 // compaction within generations only, and doing card-table sliding.
139 bool all_empty = true;
140 for (int i = 0; all_empty && i < level; i++) {
141 Generation* g = gch->get_gen(i);
142 all_empty = all_empty && gch->get_gen(i)->used() == 0;
143 }
144 GenRemSet* rs = gch->rem_set();
145 // Clear/invalidate below make use of the "prev_used_regions" saved earlier.
146 if (all_empty) {
147 // We've evacuated all generations below us.
148 Generation* g = gch->get_gen(level);
149 rs->clear_into_younger(g, true /* perm */);
150 } else {
151 // Invalidate the cards corresponding to the currently used
152 // region and clear those corresponding to the evacuated region
153 // of all generations just collected (i.e. level and younger).
154 rs->invalidate_or_clear(gch->get_gen(level),
155 true /* younger */,
156 true /* perm */);
157 }
159 Threads::gc_epilogue();
160 CodeCache::gc_epilogue();
161 JvmtiExport::gc_epilogue();
163 if (PrintGC && !PrintGCDetails) {
164 gch->print_heap_change(gch_prev_used);
165 }
167 // refs processing: clean slate
168 _ref_processor = NULL;
170 // Update heap occupancy information which is used as
171 // input to soft ref clearing policy at the next gc.
172 Universe::update_heap_info_at_gc();
174 // Update time of last gc for all generations we collected
175 // (which curently is all the generations in the heap).
176 gch->update_time_of_last_gc(os::javaTimeMillis());
177 }
179 void GenMarkSweep::allocate_stacks() {
180 GenCollectedHeap* gch = GenCollectedHeap::heap();
181 // Scratch request on behalf of oldest generation; will do no
182 // allocation.
183 ScratchBlock* scratch = gch->gather_scratch(gch->_gens[gch->_n_gens-1], 0);
185 // $$$ To cut a corner, we'll only use the first scratch block, and then
186 // revert to malloc.
187 if (scratch != NULL) {
188 _preserved_count_max =
189 scratch->num_words * HeapWordSize / sizeof(PreservedMark);
190 } else {
191 _preserved_count_max = 0;
192 }
194 _preserved_marks = (PreservedMark*)scratch;
195 _preserved_count = 0;
197 #ifdef VALIDATE_MARK_SWEEP
198 if (ValidateMarkSweep) {
199 _root_refs_stack = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
200 _other_refs_stack = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
201 _adjusted_pointers = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
202 _live_oops = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true);
203 _live_oops_moved_to = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true);
204 _live_oops_size = new (ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
205 }
206 if (RecordMarkSweepCompaction) {
207 if (_cur_gc_live_oops == NULL) {
208 _cur_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
209 _cur_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
210 _cur_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
211 _last_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
212 _last_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
213 _last_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
214 } else {
215 _cur_gc_live_oops->clear();
216 _cur_gc_live_oops_moved_to->clear();
217 _cur_gc_live_oops_size->clear();
218 }
219 }
220 #endif
221 }
224 void GenMarkSweep::deallocate_stacks() {
225 if (!UseG1GC) {
226 GenCollectedHeap* gch = GenCollectedHeap::heap();
227 gch->release_scratch();
228 }
230 _preserved_mark_stack.clear(true);
231 _preserved_oop_stack.clear(true);
232 _marking_stack.clear();
233 _objarray_stack.clear(true);
234 _revisit_klass_stack.clear(true);
235 _revisit_mdo_stack.clear(true);
237 #ifdef VALIDATE_MARK_SWEEP
238 if (ValidateMarkSweep) {
239 delete _root_refs_stack;
240 delete _other_refs_stack;
241 delete _adjusted_pointers;
242 delete _live_oops;
243 delete _live_oops_size;
244 delete _live_oops_moved_to;
245 _live_oops_index = 0;
246 _live_oops_index_at_perm = 0;
247 }
248 #endif
249 }
251 void GenMarkSweep::mark_sweep_phase1(int level,
252 bool clear_all_softrefs) {
253 // Recursively traverse all live objects and mark them
254 EventMark m("1 mark object");
255 TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty);
256 trace(" 1");
258 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
260 GenCollectedHeap* gch = GenCollectedHeap::heap();
262 // Because follow_root_closure is created statically, cannot
263 // use OopsInGenClosure constructor which takes a generation,
264 // as the Universe has not been created when the static constructors
265 // are run.
266 follow_root_closure.set_orig_generation(gch->get_gen(level));
268 gch->gen_process_strong_roots(level,
269 false, // Younger gens are not roots.
270 true, // activate StrongRootsScope
271 true, // Collecting permanent generation.
272 SharedHeap::SO_SystemClasses,
273 &follow_root_closure,
274 true, // walk code active on stacks
275 &follow_root_closure);
277 // Process reference objects found during marking
278 {
279 ref_processor()->setup_policy(clear_all_softrefs);
280 ref_processor()->process_discovered_references(
281 &is_alive, &keep_alive, &follow_stack_closure, NULL);
282 }
284 // Follow system dictionary roots and unload classes
285 bool purged_class = SystemDictionary::do_unloading(&is_alive);
287 // Follow code cache roots
288 CodeCache::do_unloading(&is_alive, &keep_alive, purged_class);
289 follow_stack(); // Flush marking stack
291 // Update subklass/sibling/implementor links of live klasses
292 follow_weak_klass_links();
293 assert(_marking_stack.is_empty(), "just drained");
295 // Visit memoized MDO's and clear any unmarked weak refs
296 follow_mdo_weak_refs();
297 assert(_marking_stack.is_empty(), "just drained");
299 // Visit interned string tables and delete unmarked oops
300 StringTable::unlink(&is_alive);
301 // Clean up unreferenced symbols in symbol table.
302 SymbolTable::unlink();
304 assert(_marking_stack.is_empty(), "stack should be empty by now");
305 }
308 void GenMarkSweep::mark_sweep_phase2() {
309 // Now all live objects are marked, compute the new object addresses.
311 // It is imperative that we traverse perm_gen LAST. If dead space is
312 // allowed a range of dead object may get overwritten by a dead int
313 // array. If perm_gen is not traversed last a klassOop may get
314 // overwritten. This is fine since it is dead, but if the class has dead
315 // instances we have to skip them, and in order to find their size we
316 // need the klassOop!
317 //
318 // It is not required that we traverse spaces in the same order in
319 // phase2, phase3 and phase4, but the ValidateMarkSweep live oops
320 // tracking expects us to do so. See comment under phase4.
322 GenCollectedHeap* gch = GenCollectedHeap::heap();
323 Generation* pg = gch->perm_gen();
325 EventMark m("2 compute new addresses");
326 TraceTime tm("phase 2", PrintGC && Verbose, true, gclog_or_tty);
327 trace("2");
329 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
331 gch->prepare_for_compaction();
333 VALIDATE_MARK_SWEEP_ONLY(_live_oops_index_at_perm = _live_oops_index);
334 CompactPoint perm_cp(pg, NULL, NULL);
335 pg->prepare_for_compaction(&perm_cp);
336 }
338 class GenAdjustPointersClosure: public GenCollectedHeap::GenClosure {
339 public:
340 void do_generation(Generation* gen) {
341 gen->adjust_pointers();
342 }
343 };
345 void GenMarkSweep::mark_sweep_phase3(int level) {
346 GenCollectedHeap* gch = GenCollectedHeap::heap();
347 Generation* pg = gch->perm_gen();
349 // Adjust the pointers to reflect the new locations
350 EventMark m("3 adjust pointers");
351 TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty);
352 trace("3");
354 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
356 // Needs to be done before the system dictionary is adjusted.
357 pg->pre_adjust_pointers();
359 // Because the two closures below are created statically, cannot
360 // use OopsInGenClosure constructor which takes a generation,
361 // as the Universe has not been created when the static constructors
362 // are run.
363 adjust_root_pointer_closure.set_orig_generation(gch->get_gen(level));
364 adjust_pointer_closure.set_orig_generation(gch->get_gen(level));
366 gch->gen_process_strong_roots(level,
367 false, // Younger gens are not roots.
368 true, // activate StrongRootsScope
369 true, // Collecting permanent generation.
370 SharedHeap::SO_AllClasses,
371 &adjust_root_pointer_closure,
372 false, // do not walk code
373 &adjust_root_pointer_closure);
375 // Now adjust pointers in remaining weak roots. (All of which should
376 // have been cleared if they pointed to non-surviving objects.)
377 CodeBlobToOopClosure adjust_code_pointer_closure(&adjust_pointer_closure,
378 /*do_marking=*/ false);
379 gch->gen_process_weak_roots(&adjust_root_pointer_closure,
380 &adjust_code_pointer_closure,
381 &adjust_pointer_closure);
383 adjust_marks();
384 GenAdjustPointersClosure blk;
385 gch->generation_iterate(&blk, true);
386 pg->adjust_pointers();
387 }
389 class GenCompactClosure: public GenCollectedHeap::GenClosure {
390 public:
391 void do_generation(Generation* gen) {
392 gen->compact();
393 }
394 };
396 void GenMarkSweep::mark_sweep_phase4() {
397 // All pointers are now adjusted, move objects accordingly
399 // It is imperative that we traverse perm_gen first in phase4. All
400 // classes must be allocated earlier than their instances, and traversing
401 // perm_gen first makes sure that all klassOops have moved to their new
402 // location before any instance does a dispatch through it's klass!
404 // The ValidateMarkSweep live oops tracking expects us to traverse spaces
405 // in the same order in phase2, phase3 and phase4. We don't quite do that
406 // here (perm_gen first rather than last), so we tell the validate code
407 // to use a higher index (saved from phase2) when verifying perm_gen.
408 GenCollectedHeap* gch = GenCollectedHeap::heap();
409 Generation* pg = gch->perm_gen();
411 EventMark m("4 compact heap");
412 TraceTime tm("phase 4", PrintGC && Verbose, true, gclog_or_tty);
413 trace("4");
415 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(true));
417 pg->compact();
419 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
421 GenCompactClosure blk;
422 gch->generation_iterate(&blk, true);
424 VALIDATE_MARK_SWEEP_ONLY(compaction_complete());
426 pg->post_compact(); // Shared spaces verification.
427 }