src/share/vm/gc_implementation/g1/concurrentMarkThread.cpp

Mon, 31 Jan 2011 16:28:40 -0500

author
tonyp
date
Mon, 31 Jan 2011 16:28:40 -0500
changeset 2501
b7a938236e43
parent 2492
a672e43650cc
child 2643
1216415d8e35
permissions
-rw-r--r--

7014679: G1: deadlock during concurrent cleanup
Summary: There's a potential deadlock between the concurrent cleanup thread and the GC workers that are trying to allocate and waiting for more free regions to be made available.
Reviewed-by: iveresov, jcoomes

ysr@777 1 /*
tonyp@2472 2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
ysr@777 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ysr@777 4 *
ysr@777 5 * This code is free software; you can redistribute it and/or modify it
ysr@777 6 * under the terms of the GNU General Public License version 2 only, as
ysr@777 7 * published by the Free Software Foundation.
ysr@777 8 *
ysr@777 9 * This code is distributed in the hope that it will be useful, but WITHOUT
ysr@777 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ysr@777 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ysr@777 12 * version 2 for more details (a copy is included in the LICENSE file that
ysr@777 13 * accompanied this code).
ysr@777 14 *
ysr@777 15 * You should have received a copy of the GNU General Public License version
ysr@777 16 * 2 along with this work; if not, write to the Free Software Foundation,
ysr@777 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ysr@777 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.
ysr@777 22 *
ysr@777 23 */
ysr@777 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"
stefank@2314 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
stefank@2314 28 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
stefank@2314 29 #include "gc_implementation/g1/g1MMUTracker.hpp"
stefank@2314 30 #include "gc_implementation/g1/vm_operations_g1.hpp"
stefank@2314 31 #include "memory/resourceArea.hpp"
stefank@2314 32 #include "runtime/vmThread.hpp"
ysr@777 33
ysr@777 34 // ======= Concurrent Mark Thread ========
ysr@777 35
ysr@777 36 // The CM thread is created when the G1 garbage collector is used
ysr@777 37
ysr@777 38 SurrogateLockerThread*
ysr@777 39 ConcurrentMarkThread::_slt = NULL;
ysr@777 40
ysr@777 41 ConcurrentMarkThread::ConcurrentMarkThread(ConcurrentMark* cm) :
ysr@777 42 ConcurrentGCThread(),
ysr@777 43 _cm(cm),
ysr@777 44 _started(false),
ysr@777 45 _in_progress(false),
ysr@777 46 _vtime_accum(0.0),
ysr@777 47 _vtime_mark_accum(0.0),
ysr@777 48 _vtime_count_accum(0.0)
ysr@777 49 {
ysr@777 50 create_and_start();
ysr@777 51 }
ysr@777 52
ysr@777 53 class CMCheckpointRootsInitialClosure: public VoidClosure {
ysr@777 54
ysr@777 55 ConcurrentMark* _cm;
ysr@777 56 public:
ysr@777 57
ysr@777 58 CMCheckpointRootsInitialClosure(ConcurrentMark* cm) :
ysr@777 59 _cm(cm) {}
ysr@777 60
ysr@777 61 void do_void(){
ysr@777 62 _cm->checkpointRootsInitial();
ysr@777 63 }
ysr@777 64 };
ysr@777 65
ysr@777 66 class CMCheckpointRootsFinalClosure: public VoidClosure {
ysr@777 67
ysr@777 68 ConcurrentMark* _cm;
ysr@777 69 public:
ysr@777 70
ysr@777 71 CMCheckpointRootsFinalClosure(ConcurrentMark* cm) :
ysr@777 72 _cm(cm) {}
ysr@777 73
ysr@777 74 void do_void(){
ysr@777 75 _cm->checkpointRootsFinal(false); // !clear_all_soft_refs
ysr@777 76 }
ysr@777 77 };
ysr@777 78
ysr@777 79 class CMCleanUp: public VoidClosure {
ysr@777 80 ConcurrentMark* _cm;
ysr@777 81 public:
ysr@777 82
ysr@777 83 CMCleanUp(ConcurrentMark* cm) :
ysr@777 84 _cm(cm) {}
ysr@777 85
ysr@777 86 void do_void(){
ysr@777 87 _cm->cleanup();
ysr@777 88 }
ysr@777 89 };
ysr@777 90
ysr@777 91
ysr@777 92
ysr@777 93 void ConcurrentMarkThread::run() {
ysr@777 94 initialize_in_thread();
ysr@777 95 _vtime_start = os::elapsedVTime();
ysr@777 96 wait_for_universe_init();
ysr@777 97
tonyp@2472 98 G1CollectedHeap* g1h = G1CollectedHeap::heap();
tonyp@2472 99 G1CollectorPolicy* g1_policy = g1h->g1_policy();
ysr@777 100 G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker();
ysr@777 101 Thread *current_thread = Thread::current();
ysr@777 102
ysr@777 103 while (!_should_terminate) {
ysr@777 104 // wait until started is set.
ysr@777 105 sleepBeforeNextCycle();
ysr@777 106 {
ysr@777 107 ResourceMark rm;
ysr@777 108 HandleMark hm;
ysr@777 109 double cycle_start = os::elapsedVTime();
ysr@777 110 double mark_start_sec = os::elapsedTime();
ysr@777 111 char verbose_str[128];
ysr@777 112
ysr@777 113 if (PrintGC) {
ysr@777 114 gclog_or_tty->date_stamp(PrintGCDateStamps);
ysr@777 115 gclog_or_tty->stamp(PrintGCTimeStamps);
tonyp@1054 116 gclog_or_tty->print_cr("[GC concurrent-mark-start]");
ysr@777 117 }
ysr@777 118
ysr@777 119 if (!g1_policy->in_young_gc_mode()) {
ysr@777 120 // this ensures the flag is not set if we bail out of the marking
ysr@777 121 // cycle; normally the flag is cleared immediately after cleanup
tonyp@2472 122 g1h->set_marking_complete();
ysr@777 123
ysr@777 124 if (g1_policy->adaptive_young_list_length()) {
ysr@777 125 double now = os::elapsedTime();
ysr@777 126 double init_prediction_ms = g1_policy->predict_init_time_ms();
ysr@777 127 jlong sleep_time_ms = mmu_tracker->when_ms(now, init_prediction_ms);
ysr@777 128 os::sleep(current_thread, sleep_time_ms, false);
ysr@777 129 }
ysr@777 130
ysr@777 131 // We don't have to skip here if we've been asked to restart, because
ysr@777 132 // in the worst case we just enqueue a new VM operation to start a
ysr@777 133 // marking. Note that the init operation resets has_aborted()
ysr@777 134 CMCheckpointRootsInitialClosure init_cl(_cm);
ysr@777 135 strcpy(verbose_str, "GC initial-mark");
ysr@777 136 VM_CGC_Operation op(&init_cl, verbose_str);
ysr@777 137 VMThread::execute(&op);
ysr@777 138 }
ysr@777 139
ysr@777 140 int iter = 0;
ysr@777 141 do {
ysr@777 142 iter++;
ysr@777 143 if (!cm()->has_aborted()) {
ysr@777 144 _cm->markFromRoots();
ysr@777 145 }
ysr@777 146
ysr@777 147 double mark_end_time = os::elapsedVTime();
ysr@777 148 double mark_end_sec = os::elapsedTime();
ysr@777 149 _vtime_mark_accum += (mark_end_time - cycle_start);
ysr@777 150 if (!cm()->has_aborted()) {
ysr@777 151 if (g1_policy->adaptive_young_list_length()) {
ysr@777 152 double now = os::elapsedTime();
ysr@777 153 double remark_prediction_ms = g1_policy->predict_remark_time_ms();
ysr@777 154 jlong sleep_time_ms = mmu_tracker->when_ms(now, remark_prediction_ms);
ysr@777 155 os::sleep(current_thread, sleep_time_ms, false);
ysr@777 156 }
ysr@777 157
ysr@777 158 if (PrintGC) {
ysr@777 159 gclog_or_tty->date_stamp(PrintGCDateStamps);
ysr@777 160 gclog_or_tty->stamp(PrintGCTimeStamps);
ysr@777 161 gclog_or_tty->print_cr("[GC concurrent-mark-end, %1.7lf sec]",
ysr@777 162 mark_end_sec - mark_start_sec);
ysr@777 163 }
ysr@777 164
ysr@777 165 CMCheckpointRootsFinalClosure final_cl(_cm);
ysr@777 166 sprintf(verbose_str, "GC remark");
ysr@777 167 VM_CGC_Operation op(&final_cl, verbose_str);
ysr@777 168 VMThread::execute(&op);
ysr@777 169 }
ysr@777 170 if (cm()->restart_for_overflow() &&
ysr@777 171 G1TraceMarkStackOverflow) {
ysr@777 172 gclog_or_tty->print_cr("Restarting conc marking because of MS overflow "
ysr@777 173 "in remark (restart #%d).", iter);
ysr@777 174 }
ysr@777 175
ysr@777 176 if (cm()->restart_for_overflow()) {
ysr@777 177 if (PrintGC) {
ysr@777 178 gclog_or_tty->date_stamp(PrintGCDateStamps);
ysr@777 179 gclog_or_tty->stamp(PrintGCTimeStamps);
ysr@777 180 gclog_or_tty->print_cr("[GC concurrent-mark-restart-for-overflow]");
ysr@777 181 }
ysr@777 182 }
ysr@777 183 } while (cm()->restart_for_overflow());
ysr@777 184 double counting_start_time = os::elapsedVTime();
ysr@777 185
ysr@777 186 // YSR: These look dubious (i.e. redundant) !!! FIX ME
ysr@777 187 slt()->manipulatePLL(SurrogateLockerThread::acquirePLL);
ysr@777 188 slt()->manipulatePLL(SurrogateLockerThread::releaseAndNotifyPLL);
ysr@777 189
ysr@777 190 if (!cm()->has_aborted()) {
ysr@777 191 double count_start_sec = os::elapsedTime();
ysr@777 192 if (PrintGC) {
ysr@777 193 gclog_or_tty->date_stamp(PrintGCDateStamps);
ysr@777 194 gclog_or_tty->stamp(PrintGCTimeStamps);
ysr@777 195 gclog_or_tty->print_cr("[GC concurrent-count-start]");
ysr@777 196 }
ysr@777 197
ysr@777 198 _sts.join();
ysr@777 199 _cm->calcDesiredRegions();
ysr@777 200 _sts.leave();
ysr@777 201
ysr@777 202 if (!cm()->has_aborted()) {
ysr@777 203 double count_end_sec = os::elapsedTime();
ysr@777 204 if (PrintGC) {
ysr@777 205 gclog_or_tty->date_stamp(PrintGCDateStamps);
ysr@777 206 gclog_or_tty->stamp(PrintGCTimeStamps);
ysr@777 207 gclog_or_tty->print_cr("[GC concurrent-count-end, %1.7lf]",
ysr@777 208 count_end_sec - count_start_sec);
ysr@777 209 }
ysr@777 210 }
ysr@777 211 }
ysr@777 212 double end_time = os::elapsedVTime();
ysr@777 213 _vtime_count_accum += (end_time - counting_start_time);
ysr@777 214 // Update the total virtual time before doing this, since it will try
ysr@777 215 // to measure it to get the vtime for this marking. We purposely
ysr@777 216 // neglect the presumably-short "completeCleanup" phase here.
ysr@777 217 _vtime_accum = (end_time - _vtime_start);
ysr@777 218 if (!cm()->has_aborted()) {
ysr@777 219 if (g1_policy->adaptive_young_list_length()) {
ysr@777 220 double now = os::elapsedTime();
ysr@777 221 double cleanup_prediction_ms = g1_policy->predict_cleanup_time_ms();
ysr@777 222 jlong sleep_time_ms = mmu_tracker->when_ms(now, cleanup_prediction_ms);
ysr@777 223 os::sleep(current_thread, sleep_time_ms, false);
ysr@777 224 }
ysr@777 225
ysr@777 226 CMCleanUp cl_cl(_cm);
ysr@777 227 sprintf(verbose_str, "GC cleanup");
ysr@777 228 VM_CGC_Operation op(&cl_cl, verbose_str);
ysr@777 229 VMThread::execute(&op);
ysr@777 230 } else {
tonyp@2472 231 g1h->set_marking_complete();
ysr@777 232 }
ysr@777 233
tonyp@2472 234 // Check if cleanup set the free_regions_coming flag. If it
tonyp@2472 235 // hasn't, we can just skip the next step.
tonyp@2472 236 if (g1h->free_regions_coming()) {
tonyp@2472 237 // The following will finish freeing up any regions that we
tonyp@2472 238 // found to be empty during cleanup. We'll do this part
tonyp@2472 239 // without joining the suspendible set. If an evacuation pause
tonyp@2472 240 // takes places, then we would carry on freeing regions in
tonyp@2472 241 // case they are needed by the pause. If a Full GC takes
tonyp@2472 242 // places, it would wait for us to process the regions
tonyp@2472 243 // reclaimed by cleanup.
tonyp@2472 244
ysr@777 245 double cleanup_start_sec = os::elapsedTime();
ysr@777 246 if (PrintGC) {
ysr@777 247 gclog_or_tty->date_stamp(PrintGCDateStamps);
ysr@777 248 gclog_or_tty->stamp(PrintGCTimeStamps);
ysr@777 249 gclog_or_tty->print_cr("[GC concurrent-cleanup-start]");
ysr@777 250 }
ysr@777 251
ysr@777 252 // Now do the remainder of the cleanup operation.
ysr@777 253 _cm->completeCleanup();
tonyp@2501 254 // Notify anyone who's waiting that there are no more free
tonyp@2501 255 // regions coming. We have to do this before we join the STS,
tonyp@2501 256 // otherwise we might deadlock: a GC worker could be blocked
tonyp@2501 257 // waiting for the notification whereas this thread will be
tonyp@2501 258 // blocked for the pause to finish while it's trying to join
tonyp@2501 259 // the STS, which is conditional on the GC workers finishing.
tonyp@2501 260 g1h->reset_free_regions_coming();
tonyp@2501 261
tonyp@2492 262 _sts.join();
tonyp@2472 263 g1_policy->record_concurrent_mark_cleanup_completed();
tonyp@2492 264 _sts.leave();
ysr@777 265
tonyp@2472 266 double cleanup_end_sec = os::elapsedTime();
tonyp@2472 267 if (PrintGC) {
tonyp@2472 268 gclog_or_tty->date_stamp(PrintGCDateStamps);
tonyp@2472 269 gclog_or_tty->stamp(PrintGCTimeStamps);
tonyp@2472 270 gclog_or_tty->print_cr("[GC concurrent-cleanup-end, %1.7lf]",
tonyp@2472 271 cleanup_end_sec - cleanup_start_sec);
ysr@777 272 }
ysr@777 273 }
tonyp@2472 274 guarantee(cm()->cleanup_list_is_empty(),
tonyp@2472 275 "at this point there should be no regions on the cleanup list");
ysr@777 276
ysr@777 277 if (cm()->has_aborted()) {
ysr@777 278 if (PrintGC) {
ysr@777 279 gclog_or_tty->date_stamp(PrintGCDateStamps);
ysr@777 280 gclog_or_tty->stamp(PrintGCTimeStamps);
ysr@777 281 gclog_or_tty->print_cr("[GC concurrent-mark-abort]");
ysr@777 282 }
ysr@777 283 }
ysr@777 284
ysr@777 285 // we now want to allow clearing of the marking bitmap to be
ysr@777 286 // suspended by a collection pause.
ysr@777 287 _sts.join();
ysr@777 288 _cm->clearNextBitmap();
ysr@777 289 _sts.leave();
ysr@777 290 }
tonyp@2011 291
tonyp@2011 292 // Update the number of full collections that have been
tonyp@2011 293 // completed. This will also notify the FullGCCount_lock in case a
tonyp@2011 294 // Java thread is waiting for a full GC to happen (e.g., it
tonyp@2011 295 // called System.gc() with +ExplicitGCInvokesConcurrent).
tonyp@2372 296 _sts.join();
tonyp@2472 297 g1h->increment_full_collections_completed(true /* concurrent */);
tonyp@2372 298 _sts.leave();
ysr@777 299 }
ysr@777 300 assert(_should_terminate, "just checking");
ysr@777 301
ysr@777 302 terminate();
ysr@777 303 }
ysr@777 304
ysr@777 305
ysr@777 306 void ConcurrentMarkThread::yield() {
ysr@777 307 _sts.yield("Concurrent Mark");
ysr@777 308 }
ysr@777 309
ysr@777 310 void ConcurrentMarkThread::stop() {
ysr@777 311 // it is ok to take late safepoints here, if needed
ysr@777 312 MutexLockerEx mu(Terminator_lock);
ysr@777 313 _should_terminate = true;
ysr@777 314 while (!_has_terminated) {
ysr@777 315 Terminator_lock->wait();
ysr@777 316 }
ysr@777 317 }
ysr@777 318
tonyp@1454 319 void ConcurrentMarkThread::print() const {
tonyp@1454 320 print_on(tty);
tonyp@1454 321 }
tonyp@1454 322
tonyp@1454 323 void ConcurrentMarkThread::print_on(outputStream* st) const {
tonyp@1454 324 st->print("\"G1 Main Concurrent Mark GC Thread\" ");
tonyp@1454 325 Thread::print_on(st);
tonyp@1454 326 st->cr();
ysr@777 327 }
ysr@777 328
ysr@777 329 void ConcurrentMarkThread::sleepBeforeNextCycle() {
ysr@777 330 // We join here because we don't want to do the "shouldConcurrentMark()"
ysr@777 331 // below while the world is otherwise stopped.
johnc@2195 332 assert(!in_progress(), "should have been cleared");
johnc@2195 333
ysr@777 334 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
ysr@777 335 while (!started()) {
ysr@777 336 CGC_lock->wait(Mutex::_no_safepoint_check_flag);
ysr@777 337 }
ysr@777 338 set_in_progress();
ysr@777 339 clear_started();
ysr@777 340 }
ysr@777 341
ysr@777 342 // Note: this method, although exported by the ConcurrentMarkSweepThread,
ysr@777 343 // which is a non-JavaThread, can only be called by a JavaThread.
ysr@777 344 // Currently this is done at vm creation time (post-vm-init) by the
ysr@777 345 // main/Primordial (Java)Thread.
ysr@777 346 // XXX Consider changing this in the future to allow the CMS thread
ysr@777 347 // itself to create this thread?
ysr@777 348 void ConcurrentMarkThread::makeSurrogateLockerThread(TRAPS) {
ysr@777 349 assert(_slt == NULL, "SLT already created");
ysr@777 350 _slt = SurrogateLockerThread::make(THREAD);
ysr@777 351 }

mercurial