Wed, 27 Apr 2016 01:25:04 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #include "precompiled.hpp" |
aoqi@0 | 26 | #include "classfile/javaClasses.hpp" |
aoqi@0 | 27 | #include "classfile/symbolTable.hpp" |
aoqi@0 | 28 | #include "classfile/systemDictionary.hpp" |
aoqi@0 | 29 | #include "classfile/vmSymbols.hpp" |
aoqi@0 | 30 | #include "code/codeCache.hpp" |
aoqi@0 | 31 | #include "code/icBuffer.hpp" |
aoqi@0 | 32 | #include "gc_implementation/shared/gcHeapSummary.hpp" |
aoqi@0 | 33 | #include "gc_implementation/shared/gcTimer.hpp" |
aoqi@0 | 34 | #include "gc_implementation/shared/gcTrace.hpp" |
aoqi@0 | 35 | #include "gc_implementation/shared/gcTraceTime.hpp" |
aoqi@0 | 36 | #include "gc_interface/collectedHeap.inline.hpp" |
aoqi@0 | 37 | #include "memory/genCollectedHeap.hpp" |
aoqi@0 | 38 | #include "memory/genMarkSweep.hpp" |
aoqi@0 | 39 | #include "memory/genOopClosures.inline.hpp" |
aoqi@0 | 40 | #include "memory/generation.inline.hpp" |
aoqi@0 | 41 | #include "memory/modRefBarrierSet.hpp" |
aoqi@0 | 42 | #include "memory/referencePolicy.hpp" |
aoqi@0 | 43 | #include "memory/space.hpp" |
aoqi@0 | 44 | #include "oops/instanceRefKlass.hpp" |
aoqi@0 | 45 | #include "oops/oop.inline.hpp" |
aoqi@0 | 46 | #include "prims/jvmtiExport.hpp" |
aoqi@0 | 47 | #include "runtime/fprofiler.hpp" |
aoqi@0 | 48 | #include "runtime/handles.inline.hpp" |
aoqi@0 | 49 | #include "runtime/synchronizer.hpp" |
aoqi@0 | 50 | #include "runtime/thread.inline.hpp" |
aoqi@0 | 51 | #include "runtime/vmThread.hpp" |
aoqi@0 | 52 | #include "utilities/copy.hpp" |
aoqi@0 | 53 | #include "utilities/events.hpp" |
aoqi@0 | 54 | |
aoqi@0 | 55 | void GenMarkSweep::invoke_at_safepoint(int level, ReferenceProcessor* rp, bool clear_all_softrefs) { |
aoqi@0 | 56 | guarantee(level == 1, "We always collect both old and young."); |
aoqi@0 | 57 | assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); |
aoqi@0 | 58 | |
aoqi@0 | 59 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 60 | #ifdef ASSERT |
aoqi@0 | 61 | if (gch->collector_policy()->should_clear_all_soft_refs()) { |
aoqi@0 | 62 | assert(clear_all_softrefs, "Policy should have been checked earlier"); |
aoqi@0 | 63 | } |
aoqi@0 | 64 | #endif |
aoqi@0 | 65 | |
aoqi@0 | 66 | // hook up weak ref data so it can be used during Mark-Sweep |
aoqi@0 | 67 | assert(ref_processor() == NULL, "no stomping"); |
aoqi@0 | 68 | assert(rp != NULL, "should be non-NULL"); |
aoqi@0 | 69 | _ref_processor = rp; |
aoqi@0 | 70 | rp->setup_policy(clear_all_softrefs); |
aoqi@0 | 71 | |
aoqi@0 | 72 | GCTraceTime t1(GCCauseString("Full GC", gch->gc_cause()), PrintGC && !PrintGCDetails, true, NULL); |
aoqi@0 | 73 | |
aoqi@0 | 74 | gch->trace_heap_before_gc(_gc_tracer); |
aoqi@0 | 75 | |
aoqi@0 | 76 | // When collecting the permanent generation Method*s may be moving, |
aoqi@0 | 77 | // so we either have to flush all bcp data or convert it into bci. |
aoqi@0 | 78 | CodeCache::gc_prologue(); |
aoqi@0 | 79 | Threads::gc_prologue(); |
aoqi@0 | 80 | |
aoqi@0 | 81 | // Increment the invocation count |
aoqi@0 | 82 | _total_invocations++; |
aoqi@0 | 83 | |
aoqi@0 | 84 | // Capture heap size before collection for printing. |
aoqi@0 | 85 | size_t gch_prev_used = gch->used(); |
aoqi@0 | 86 | |
aoqi@0 | 87 | // Capture used regions for each generation that will be |
aoqi@0 | 88 | // subject to collection, so that card table adjustments can |
aoqi@0 | 89 | // be made intelligently (see clear / invalidate further below). |
aoqi@0 | 90 | gch->save_used_regions(level); |
aoqi@0 | 91 | |
aoqi@0 | 92 | allocate_stacks(); |
aoqi@0 | 93 | |
aoqi@0 | 94 | mark_sweep_phase1(level, clear_all_softrefs); |
aoqi@0 | 95 | |
aoqi@0 | 96 | mark_sweep_phase2(); |
aoqi@0 | 97 | |
aoqi@0 | 98 | // Don't add any more derived pointers during phase3 |
aoqi@0 | 99 | COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity")); |
aoqi@0 | 100 | COMPILER2_PRESENT(DerivedPointerTable::set_active(false)); |
aoqi@0 | 101 | |
aoqi@0 | 102 | mark_sweep_phase3(level); |
aoqi@0 | 103 | |
aoqi@0 | 104 | mark_sweep_phase4(); |
aoqi@0 | 105 | |
aoqi@0 | 106 | restore_marks(); |
aoqi@0 | 107 | |
aoqi@0 | 108 | // Set saved marks for allocation profiler (and other things? -- dld) |
aoqi@0 | 109 | // (Should this be in general part?) |
aoqi@0 | 110 | gch->save_marks(); |
aoqi@0 | 111 | |
aoqi@0 | 112 | deallocate_stacks(); |
aoqi@0 | 113 | |
aoqi@0 | 114 | // If compaction completely evacuated all generations younger than this |
aoqi@0 | 115 | // one, then we can clear the card table. Otherwise, we must invalidate |
aoqi@0 | 116 | // it (consider all cards dirty). In the future, we might consider doing |
aoqi@0 | 117 | // compaction within generations only, and doing card-table sliding. |
aoqi@0 | 118 | bool all_empty = true; |
aoqi@0 | 119 | for (int i = 0; all_empty && i < level; i++) { |
aoqi@0 | 120 | Generation* g = gch->get_gen(i); |
aoqi@0 | 121 | all_empty = all_empty && gch->get_gen(i)->used() == 0; |
aoqi@0 | 122 | } |
aoqi@0 | 123 | GenRemSet* rs = gch->rem_set(); |
aoqi@0 | 124 | Generation* old_gen = gch->get_gen(level); |
aoqi@0 | 125 | // Clear/invalidate below make use of the "prev_used_regions" saved earlier. |
aoqi@0 | 126 | if (all_empty) { |
aoqi@0 | 127 | // We've evacuated all generations below us. |
aoqi@0 | 128 | rs->clear_into_younger(old_gen); |
aoqi@0 | 129 | } else { |
aoqi@0 | 130 | // Invalidate the cards corresponding to the currently used |
aoqi@0 | 131 | // region and clear those corresponding to the evacuated region. |
aoqi@0 | 132 | rs->invalidate_or_clear(old_gen); |
aoqi@0 | 133 | } |
aoqi@0 | 134 | |
aoqi@0 | 135 | Threads::gc_epilogue(); |
aoqi@0 | 136 | CodeCache::gc_epilogue(); |
aoqi@0 | 137 | JvmtiExport::gc_epilogue(); |
aoqi@0 | 138 | |
aoqi@0 | 139 | if (PrintGC && !PrintGCDetails) { |
aoqi@0 | 140 | gch->print_heap_change(gch_prev_used); |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | // refs processing: clean slate |
aoqi@0 | 144 | _ref_processor = NULL; |
aoqi@0 | 145 | |
aoqi@0 | 146 | // Update heap occupancy information which is used as |
aoqi@0 | 147 | // input to soft ref clearing policy at the next gc. |
aoqi@0 | 148 | Universe::update_heap_info_at_gc(); |
aoqi@0 | 149 | |
aoqi@0 | 150 | // Update time of last gc for all generations we collected |
aoqi@0 | 151 | // (which curently is all the generations in the heap). |
aoqi@0 | 152 | // We need to use a monotonically non-deccreasing time in ms |
aoqi@0 | 153 | // or we will see time-warp warnings and os::javaTimeMillis() |
aoqi@0 | 154 | // does not guarantee monotonicity. |
aoqi@0 | 155 | jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC; |
aoqi@0 | 156 | gch->update_time_of_last_gc(now); |
aoqi@0 | 157 | |
aoqi@0 | 158 | gch->trace_heap_after_gc(_gc_tracer); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | |
aoqi@0 | 161 | void GenMarkSweep::allocate_stacks() { |
aoqi@0 | 162 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 163 | // Scratch request on behalf of oldest generation; will do no |
aoqi@0 | 164 | // allocation. |
aoqi@0 | 165 | ScratchBlock* scratch = gch->gather_scratch(gch->_gens[gch->_n_gens-1], 0); |
aoqi@0 | 166 | |
aoqi@0 | 167 | // $$$ To cut a corner, we'll only use the first scratch block, and then |
aoqi@0 | 168 | // revert to malloc. |
aoqi@0 | 169 | if (scratch != NULL) { |
aoqi@0 | 170 | _preserved_count_max = |
aoqi@0 | 171 | scratch->num_words * HeapWordSize / sizeof(PreservedMark); |
aoqi@0 | 172 | } else { |
aoqi@0 | 173 | _preserved_count_max = 0; |
aoqi@0 | 174 | } |
aoqi@0 | 175 | |
aoqi@0 | 176 | _preserved_marks = (PreservedMark*)scratch; |
aoqi@0 | 177 | _preserved_count = 0; |
aoqi@0 | 178 | } |
aoqi@0 | 179 | |
aoqi@0 | 180 | |
aoqi@0 | 181 | void GenMarkSweep::deallocate_stacks() { |
aoqi@0 | 182 | if (!UseG1GC) { |
aoqi@0 | 183 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 184 | gch->release_scratch(); |
aoqi@0 | 185 | } |
aoqi@0 | 186 | |
aoqi@0 | 187 | _preserved_mark_stack.clear(true); |
aoqi@0 | 188 | _preserved_oop_stack.clear(true); |
aoqi@0 | 189 | _marking_stack.clear(); |
aoqi@0 | 190 | _objarray_stack.clear(true); |
aoqi@0 | 191 | } |
aoqi@0 | 192 | |
aoqi@0 | 193 | void GenMarkSweep::mark_sweep_phase1(int level, |
aoqi@0 | 194 | bool clear_all_softrefs) { |
aoqi@0 | 195 | // Recursively traverse all live objects and mark them |
aoqi@0 | 196 | GCTraceTime tm("phase 1", PrintGC && Verbose, true, _gc_timer); |
aoqi@0 | 197 | trace(" 1"); |
aoqi@0 | 198 | |
aoqi@0 | 199 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 200 | |
aoqi@0 | 201 | // Because follow_root_closure is created statically, cannot |
aoqi@0 | 202 | // use OopsInGenClosure constructor which takes a generation, |
aoqi@0 | 203 | // as the Universe has not been created when the static constructors |
aoqi@0 | 204 | // are run. |
aoqi@0 | 205 | follow_root_closure.set_orig_generation(gch->get_gen(level)); |
aoqi@0 | 206 | |
aoqi@0 | 207 | // Need new claim bits before marking starts. |
aoqi@0 | 208 | ClassLoaderDataGraph::clear_claimed_marks(); |
aoqi@0 | 209 | |
aoqi@0 | 210 | gch->gen_process_strong_roots(level, |
aoqi@0 | 211 | false, // Younger gens are not roots. |
aoqi@0 | 212 | true, // activate StrongRootsScope |
aoqi@0 | 213 | false, // not scavenging |
aoqi@0 | 214 | SharedHeap::SO_SystemClasses, |
aoqi@0 | 215 | &follow_root_closure, |
aoqi@0 | 216 | true, // walk code active on stacks |
aoqi@0 | 217 | &follow_root_closure, |
aoqi@0 | 218 | &follow_klass_closure); |
aoqi@0 | 219 | |
aoqi@0 | 220 | // Process reference objects found during marking |
aoqi@0 | 221 | { |
aoqi@0 | 222 | ref_processor()->setup_policy(clear_all_softrefs); |
aoqi@0 | 223 | const ReferenceProcessorStats& stats = |
aoqi@0 | 224 | ref_processor()->process_discovered_references( |
aoqi@0 | 225 | &is_alive, &keep_alive, &follow_stack_closure, NULL, _gc_timer); |
aoqi@0 | 226 | gc_tracer()->report_gc_reference_stats(stats); |
aoqi@0 | 227 | } |
aoqi@0 | 228 | |
aoqi@0 | 229 | // This is the point where the entire marking should have completed. |
aoqi@0 | 230 | assert(_marking_stack.is_empty(), "Marking should have completed"); |
aoqi@0 | 231 | |
aoqi@0 | 232 | // Unload classes and purge the SystemDictionary. |
aoqi@0 | 233 | bool purged_class = SystemDictionary::do_unloading(&is_alive); |
aoqi@0 | 234 | |
aoqi@0 | 235 | // Unload nmethods. |
aoqi@0 | 236 | CodeCache::do_unloading(&is_alive, purged_class); |
aoqi@0 | 237 | |
aoqi@0 | 238 | // Prune dead klasses from subklass/sibling/implementor lists. |
aoqi@0 | 239 | Klass::clean_weak_klass_links(&is_alive); |
aoqi@0 | 240 | |
aoqi@0 | 241 | // Delete entries for dead interned strings. |
aoqi@0 | 242 | StringTable::unlink(&is_alive); |
aoqi@0 | 243 | |
aoqi@0 | 244 | // Clean up unreferenced symbols in symbol table. |
aoqi@0 | 245 | SymbolTable::unlink(); |
aoqi@0 | 246 | |
aoqi@0 | 247 | gc_tracer()->report_object_count_after_gc(&is_alive); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | |
aoqi@0 | 251 | void GenMarkSweep::mark_sweep_phase2() { |
aoqi@0 | 252 | // Now all live objects are marked, compute the new object addresses. |
aoqi@0 | 253 | |
aoqi@0 | 254 | // It is imperative that we traverse perm_gen LAST. If dead space is |
aoqi@0 | 255 | // allowed a range of dead object may get overwritten by a dead int |
aoqi@0 | 256 | // array. If perm_gen is not traversed last a Klass* may get |
aoqi@0 | 257 | // overwritten. This is fine since it is dead, but if the class has dead |
aoqi@0 | 258 | // instances we have to skip them, and in order to find their size we |
aoqi@0 | 259 | // need the Klass*! |
aoqi@0 | 260 | // |
aoqi@0 | 261 | // It is not required that we traverse spaces in the same order in |
aoqi@0 | 262 | // phase2, phase3 and phase4, but the ValidateMarkSweep live oops |
aoqi@0 | 263 | // tracking expects us to do so. See comment under phase4. |
aoqi@0 | 264 | |
aoqi@0 | 265 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 266 | |
aoqi@0 | 267 | GCTraceTime tm("phase 2", PrintGC && Verbose, true, _gc_timer); |
aoqi@0 | 268 | trace("2"); |
aoqi@0 | 269 | |
aoqi@0 | 270 | gch->prepare_for_compaction(); |
aoqi@0 | 271 | } |
aoqi@0 | 272 | |
aoqi@0 | 273 | class GenAdjustPointersClosure: public GenCollectedHeap::GenClosure { |
aoqi@0 | 274 | public: |
aoqi@0 | 275 | void do_generation(Generation* gen) { |
aoqi@0 | 276 | gen->adjust_pointers(); |
aoqi@0 | 277 | } |
aoqi@0 | 278 | }; |
aoqi@0 | 279 | |
aoqi@0 | 280 | void GenMarkSweep::mark_sweep_phase3(int level) { |
aoqi@0 | 281 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 282 | |
aoqi@0 | 283 | // Adjust the pointers to reflect the new locations |
aoqi@0 | 284 | GCTraceTime tm("phase 3", PrintGC && Verbose, true, _gc_timer); |
aoqi@0 | 285 | trace("3"); |
aoqi@0 | 286 | |
aoqi@0 | 287 | // Need new claim bits for the pointer adjustment tracing. |
aoqi@0 | 288 | ClassLoaderDataGraph::clear_claimed_marks(); |
aoqi@0 | 289 | |
aoqi@0 | 290 | // Because the closure below is created statically, we cannot |
aoqi@0 | 291 | // use OopsInGenClosure constructor which takes a generation, |
aoqi@0 | 292 | // as the Universe has not been created when the static constructors |
aoqi@0 | 293 | // are run. |
aoqi@0 | 294 | adjust_pointer_closure.set_orig_generation(gch->get_gen(level)); |
aoqi@0 | 295 | |
aoqi@0 | 296 | gch->gen_process_strong_roots(level, |
aoqi@0 | 297 | false, // Younger gens are not roots. |
aoqi@0 | 298 | true, // activate StrongRootsScope |
aoqi@0 | 299 | false, // not scavenging |
aoqi@0 | 300 | SharedHeap::SO_AllClasses, |
aoqi@0 | 301 | &adjust_pointer_closure, |
aoqi@0 | 302 | false, // do not walk code |
aoqi@0 | 303 | &adjust_pointer_closure, |
aoqi@0 | 304 | &adjust_klass_closure); |
aoqi@0 | 305 | |
aoqi@0 | 306 | // Now adjust pointers in remaining weak roots. (All of which should |
aoqi@0 | 307 | // have been cleared if they pointed to non-surviving objects.) |
aoqi@0 | 308 | CodeBlobToOopClosure adjust_code_pointer_closure(&adjust_pointer_closure, |
aoqi@0 | 309 | /*do_marking=*/ false); |
aoqi@0 | 310 | gch->gen_process_weak_roots(&adjust_pointer_closure, |
aoqi@0 | 311 | &adjust_code_pointer_closure); |
aoqi@0 | 312 | |
aoqi@0 | 313 | adjust_marks(); |
aoqi@0 | 314 | GenAdjustPointersClosure blk; |
aoqi@0 | 315 | gch->generation_iterate(&blk, true); |
aoqi@0 | 316 | } |
aoqi@0 | 317 | |
aoqi@0 | 318 | class GenCompactClosure: public GenCollectedHeap::GenClosure { |
aoqi@0 | 319 | public: |
aoqi@0 | 320 | void do_generation(Generation* gen) { |
aoqi@0 | 321 | gen->compact(); |
aoqi@0 | 322 | } |
aoqi@0 | 323 | }; |
aoqi@0 | 324 | |
aoqi@0 | 325 | void GenMarkSweep::mark_sweep_phase4() { |
aoqi@0 | 326 | // All pointers are now adjusted, move objects accordingly |
aoqi@0 | 327 | |
aoqi@0 | 328 | // It is imperative that we traverse perm_gen first in phase4. All |
aoqi@0 | 329 | // classes must be allocated earlier than their instances, and traversing |
aoqi@0 | 330 | // perm_gen first makes sure that all Klass*s have moved to their new |
aoqi@0 | 331 | // location before any instance does a dispatch through it's klass! |
aoqi@0 | 332 | |
aoqi@0 | 333 | // The ValidateMarkSweep live oops tracking expects us to traverse spaces |
aoqi@0 | 334 | // in the same order in phase2, phase3 and phase4. We don't quite do that |
aoqi@0 | 335 | // here (perm_gen first rather than last), so we tell the validate code |
aoqi@0 | 336 | // to use a higher index (saved from phase2) when verifying perm_gen. |
aoqi@0 | 337 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 338 | |
aoqi@0 | 339 | GCTraceTime tm("phase 4", PrintGC && Verbose, true, _gc_timer); |
aoqi@0 | 340 | trace("4"); |
aoqi@0 | 341 | |
aoqi@0 | 342 | GenCompactClosure blk; |
aoqi@0 | 343 | gch->generation_iterate(&blk, true); |
aoqi@0 | 344 | } |