src/share/vm/memory/genMarkSweep.cpp

Fri, 20 Sep 2013 10:53:28 +0200

author
stefank
date
Fri, 20 Sep 2013 10:53:28 +0200
changeset 5769
2c022e432e10
parent 5519
bd902affe102
child 6876
710a3c8b516e
child 6904
0982ec23da03
permissions
-rw-r--r--

8024974: Incorrect use of GC_locker::is_active()
Summary: SymbolTable and StringTable can make calls to GC_locker::is_active() outside a safepoint. This isn't safe because the GC_locker active state (lock count) is only updated at a safepoint and only remains valid as long as _needs_gc is true. However, outside a safepoint_needs_gc can change to false at any time, which makes it impossible to do a correct call to is_active() in that context. In this case these calls can just be removed since the input argument to basic_add() should never be on the heap and so there's no need to check the GC_locker state. This change also adjusts the assert() in is_active() to makes sure all calls to this function are always done under a safepoint.
Reviewed-by: brutisso, dcubed
Contributed-by: per.liden@oracle.com

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

mercurial