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

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

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 "gc_implementation/g1/concurrentMarkThread.inline.hpp"
aoqi@0 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
aoqi@0 28 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
aoqi@0 29 #include "gc_implementation/g1/g1Log.hpp"
aoqi@0 30 #include "gc_implementation/g1/g1MMUTracker.hpp"
aoqi@0 31 #include "gc_implementation/g1/vm_operations_g1.hpp"
aoqi@0 32 #include "memory/resourceArea.hpp"
aoqi@0 33 #include "runtime/vmThread.hpp"
aoqi@0 34
aoqi@0 35 // ======= Concurrent Mark Thread ========
aoqi@0 36
aoqi@0 37 // The CM thread is created when the G1 garbage collector is used
aoqi@0 38
aoqi@0 39 SurrogateLockerThread*
aoqi@0 40 ConcurrentMarkThread::_slt = NULL;
aoqi@0 41
aoqi@0 42 ConcurrentMarkThread::ConcurrentMarkThread(ConcurrentMark* cm) :
aoqi@0 43 ConcurrentGCThread(),
aoqi@0 44 _cm(cm),
aoqi@0 45 _started(false),
aoqi@0 46 _in_progress(false),
aoqi@0 47 _vtime_accum(0.0),
aoqi@0 48 _vtime_mark_accum(0.0) {
aoqi@0 49 create_and_start();
aoqi@0 50 }
aoqi@0 51
aoqi@0 52 class CMCheckpointRootsFinalClosure: public VoidClosure {
aoqi@0 53
aoqi@0 54 ConcurrentMark* _cm;
aoqi@0 55 public:
aoqi@0 56
aoqi@0 57 CMCheckpointRootsFinalClosure(ConcurrentMark* cm) :
aoqi@0 58 _cm(cm) {}
aoqi@0 59
aoqi@0 60 void do_void(){
aoqi@0 61 _cm->checkpointRootsFinal(false); // !clear_all_soft_refs
aoqi@0 62 }
aoqi@0 63 };
aoqi@0 64
aoqi@0 65 class CMCleanUp: public VoidClosure {
aoqi@0 66 ConcurrentMark* _cm;
aoqi@0 67 public:
aoqi@0 68
aoqi@0 69 CMCleanUp(ConcurrentMark* cm) :
aoqi@0 70 _cm(cm) {}
aoqi@0 71
aoqi@0 72 void do_void(){
aoqi@0 73 _cm->cleanup();
aoqi@0 74 }
aoqi@0 75 };
aoqi@0 76
aoqi@0 77
aoqi@0 78
aoqi@0 79 void ConcurrentMarkThread::run() {
aoqi@0 80 initialize_in_thread();
aoqi@0 81 _vtime_start = os::elapsedVTime();
aoqi@0 82 wait_for_universe_init();
aoqi@0 83
aoqi@0 84 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 85 G1CollectorPolicy* g1_policy = g1h->g1_policy();
aoqi@0 86 G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker();
aoqi@0 87 Thread *current_thread = Thread::current();
aoqi@0 88
aoqi@0 89 while (!_should_terminate) {
aoqi@0 90 // wait until started is set.
aoqi@0 91 sleepBeforeNextCycle();
aoqi@0 92 if (_should_terminate) {
aoqi@0 93 break;
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 {
aoqi@0 97 ResourceMark rm;
aoqi@0 98 HandleMark hm;
aoqi@0 99 double cycle_start = os::elapsedVTime();
aoqi@0 100
aoqi@0 101 // We have to ensure that we finish scanning the root regions
aoqi@0 102 // before the next GC takes place. To ensure this we have to
aoqi@0 103 // make sure that we do not join the STS until the root regions
aoqi@0 104 // have been scanned. If we did then it's possible that a
aoqi@0 105 // subsequent GC could block us from joining the STS and proceed
aoqi@0 106 // without the root regions have been scanned which would be a
aoqi@0 107 // correctness issue.
aoqi@0 108
aoqi@0 109 double scan_start = os::elapsedTime();
aoqi@0 110 if (!cm()->has_aborted()) {
aoqi@0 111 if (G1Log::fine()) {
aoqi@0 112 gclog_or_tty->date_stamp(PrintGCDateStamps);
aoqi@0 113 gclog_or_tty->stamp(PrintGCTimeStamps);
aoqi@0 114 gclog_or_tty->print_cr("[GC concurrent-root-region-scan-start]");
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 _cm->scanRootRegions();
aoqi@0 118
aoqi@0 119 double scan_end = os::elapsedTime();
aoqi@0 120 if (G1Log::fine()) {
aoqi@0 121 gclog_or_tty->date_stamp(PrintGCDateStamps);
aoqi@0 122 gclog_or_tty->stamp(PrintGCTimeStamps);
aoqi@0 123 gclog_or_tty->print_cr("[GC concurrent-root-region-scan-end, %1.7lf secs]",
aoqi@0 124 scan_end - scan_start);
aoqi@0 125 }
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 double mark_start_sec = os::elapsedTime();
aoqi@0 129 if (G1Log::fine()) {
aoqi@0 130 gclog_or_tty->date_stamp(PrintGCDateStamps);
aoqi@0 131 gclog_or_tty->stamp(PrintGCTimeStamps);
aoqi@0 132 gclog_or_tty->print_cr("[GC concurrent-mark-start]");
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 int iter = 0;
aoqi@0 136 do {
aoqi@0 137 iter++;
aoqi@0 138 if (!cm()->has_aborted()) {
aoqi@0 139 _cm->markFromRoots();
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 double mark_end_time = os::elapsedVTime();
aoqi@0 143 double mark_end_sec = os::elapsedTime();
aoqi@0 144 _vtime_mark_accum += (mark_end_time - cycle_start);
aoqi@0 145 if (!cm()->has_aborted()) {
aoqi@0 146 if (g1_policy->adaptive_young_list_length()) {
aoqi@0 147 double now = os::elapsedTime();
aoqi@0 148 double remark_prediction_ms = g1_policy->predict_remark_time_ms();
aoqi@0 149 jlong sleep_time_ms = mmu_tracker->when_ms(now, remark_prediction_ms);
aoqi@0 150 os::sleep(current_thread, sleep_time_ms, false);
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 if (G1Log::fine()) {
aoqi@0 154 gclog_or_tty->date_stamp(PrintGCDateStamps);
aoqi@0 155 gclog_or_tty->stamp(PrintGCTimeStamps);
aoqi@0 156 gclog_or_tty->print_cr("[GC concurrent-mark-end, %1.7lf secs]",
aoqi@0 157 mark_end_sec - mark_start_sec);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 CMCheckpointRootsFinalClosure final_cl(_cm);
aoqi@0 161 VM_CGC_Operation op(&final_cl, "GC remark", true /* needs_pll */);
aoqi@0 162 VMThread::execute(&op);
aoqi@0 163 }
aoqi@0 164 if (cm()->restart_for_overflow()) {
aoqi@0 165 if (G1TraceMarkStackOverflow) {
aoqi@0 166 gclog_or_tty->print_cr("Restarting conc marking because of MS overflow "
aoqi@0 167 "in remark (restart #%d).", iter);
aoqi@0 168 }
aoqi@0 169 if (G1Log::fine()) {
aoqi@0 170 gclog_or_tty->date_stamp(PrintGCDateStamps);
aoqi@0 171 gclog_or_tty->stamp(PrintGCTimeStamps);
aoqi@0 172 gclog_or_tty->print_cr("[GC concurrent-mark-restart-for-overflow]");
aoqi@0 173 }
aoqi@0 174 }
aoqi@0 175 } while (cm()->restart_for_overflow());
aoqi@0 176
aoqi@0 177 double end_time = os::elapsedVTime();
aoqi@0 178 // Update the total virtual time before doing this, since it will try
aoqi@0 179 // to measure it to get the vtime for this marking. We purposely
aoqi@0 180 // neglect the presumably-short "completeCleanup" phase here.
aoqi@0 181 _vtime_accum = (end_time - _vtime_start);
aoqi@0 182
aoqi@0 183 if (!cm()->has_aborted()) {
aoqi@0 184 if (g1_policy->adaptive_young_list_length()) {
aoqi@0 185 double now = os::elapsedTime();
aoqi@0 186 double cleanup_prediction_ms = g1_policy->predict_cleanup_time_ms();
aoqi@0 187 jlong sleep_time_ms = mmu_tracker->when_ms(now, cleanup_prediction_ms);
aoqi@0 188 os::sleep(current_thread, sleep_time_ms, false);
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 CMCleanUp cl_cl(_cm);
aoqi@0 192 VM_CGC_Operation op(&cl_cl, "GC cleanup", false /* needs_pll */);
aoqi@0 193 VMThread::execute(&op);
aoqi@0 194 } else {
aoqi@0 195 // We don't want to update the marking status if a GC pause
aoqi@0 196 // is already underway.
aoqi@0 197 _sts.join();
aoqi@0 198 g1h->set_marking_complete();
aoqi@0 199 _sts.leave();
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 // Check if cleanup set the free_regions_coming flag. If it
aoqi@0 203 // hasn't, we can just skip the next step.
aoqi@0 204 if (g1h->free_regions_coming()) {
aoqi@0 205 // The following will finish freeing up any regions that we
aoqi@0 206 // found to be empty during cleanup. We'll do this part
aoqi@0 207 // without joining the suspendible set. If an evacuation pause
aoqi@0 208 // takes place, then we would carry on freeing regions in
aoqi@0 209 // case they are needed by the pause. If a Full GC takes
aoqi@0 210 // place, it would wait for us to process the regions
aoqi@0 211 // reclaimed by cleanup.
aoqi@0 212
aoqi@0 213 double cleanup_start_sec = os::elapsedTime();
aoqi@0 214 if (G1Log::fine()) {
aoqi@0 215 gclog_or_tty->date_stamp(PrintGCDateStamps);
aoqi@0 216 gclog_or_tty->stamp(PrintGCTimeStamps);
aoqi@0 217 gclog_or_tty->print_cr("[GC concurrent-cleanup-start]");
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 // Now do the concurrent cleanup operation.
aoqi@0 221 _cm->completeCleanup();
aoqi@0 222
aoqi@0 223 // Notify anyone who's waiting that there are no more free
aoqi@0 224 // regions coming. We have to do this before we join the STS
aoqi@0 225 // (in fact, we should not attempt to join the STS in the
aoqi@0 226 // interval between finishing the cleanup pause and clearing
aoqi@0 227 // the free_regions_coming flag) otherwise we might deadlock:
aoqi@0 228 // a GC worker could be blocked waiting for the notification
aoqi@0 229 // whereas this thread will be blocked for the pause to finish
aoqi@0 230 // while it's trying to join the STS, which is conditional on
aoqi@0 231 // the GC workers finishing.
aoqi@0 232 g1h->reset_free_regions_coming();
aoqi@0 233
aoqi@0 234 double cleanup_end_sec = os::elapsedTime();
aoqi@0 235 if (G1Log::fine()) {
aoqi@0 236 gclog_or_tty->date_stamp(PrintGCDateStamps);
aoqi@0 237 gclog_or_tty->stamp(PrintGCTimeStamps);
aoqi@0 238 gclog_or_tty->print_cr("[GC concurrent-cleanup-end, %1.7lf secs]",
aoqi@0 239 cleanup_end_sec - cleanup_start_sec);
aoqi@0 240 }
aoqi@0 241 }
aoqi@0 242 guarantee(cm()->cleanup_list_is_empty(),
aoqi@0 243 "at this point there should be no regions on the cleanup list");
aoqi@0 244
aoqi@0 245 // There is a tricky race before recording that the concurrent
aoqi@0 246 // cleanup has completed and a potential Full GC starting around
aoqi@0 247 // the same time. We want to make sure that the Full GC calls
aoqi@0 248 // abort() on concurrent mark after
aoqi@0 249 // record_concurrent_mark_cleanup_completed(), since abort() is
aoqi@0 250 // the method that will reset the concurrent mark state. If we
aoqi@0 251 // end up calling record_concurrent_mark_cleanup_completed()
aoqi@0 252 // after abort() then we might incorrectly undo some of the work
aoqi@0 253 // abort() did. Checking the has_aborted() flag after joining
aoqi@0 254 // the STS allows the correct ordering of the two methods. There
aoqi@0 255 // are two scenarios:
aoqi@0 256 //
aoqi@0 257 // a) If we reach here before the Full GC, the fact that we have
aoqi@0 258 // joined the STS means that the Full GC cannot start until we
aoqi@0 259 // leave the STS, so record_concurrent_mark_cleanup_completed()
aoqi@0 260 // will complete before abort() is called.
aoqi@0 261 //
aoqi@0 262 // b) If we reach here during the Full GC, we'll be held up from
aoqi@0 263 // joining the STS until the Full GC is done, which means that
aoqi@0 264 // abort() will have completed and has_aborted() will return
aoqi@0 265 // true to prevent us from calling
aoqi@0 266 // record_concurrent_mark_cleanup_completed() (and, in fact, it's
aoqi@0 267 // not needed any more as the concurrent mark state has been
aoqi@0 268 // already reset).
aoqi@0 269 _sts.join();
aoqi@0 270 if (!cm()->has_aborted()) {
aoqi@0 271 g1_policy->record_concurrent_mark_cleanup_completed();
aoqi@0 272 }
aoqi@0 273 _sts.leave();
aoqi@0 274
aoqi@0 275 if (cm()->has_aborted()) {
aoqi@0 276 if (G1Log::fine()) {
aoqi@0 277 gclog_or_tty->date_stamp(PrintGCDateStamps);
aoqi@0 278 gclog_or_tty->stamp(PrintGCTimeStamps);
aoqi@0 279 gclog_or_tty->print_cr("[GC concurrent-mark-abort]");
aoqi@0 280 }
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 // We now want to allow clearing of the marking bitmap to be
aoqi@0 284 // suspended by a collection pause.
aoqi@0 285 _sts.join();
aoqi@0 286 _cm->clearNextBitmap();
aoqi@0 287 _sts.leave();
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 // Update the number of full collections that have been
aoqi@0 291 // completed. This will also notify the FullGCCount_lock in case a
aoqi@0 292 // Java thread is waiting for a full GC to happen (e.g., it
aoqi@0 293 // called System.gc() with +ExplicitGCInvokesConcurrent).
aoqi@0 294 _sts.join();
aoqi@0 295 g1h->increment_old_marking_cycles_completed(true /* concurrent */);
aoqi@0 296 g1h->register_concurrent_cycle_end();
aoqi@0 297 _sts.leave();
aoqi@0 298 }
aoqi@0 299 assert(_should_terminate, "just checking");
aoqi@0 300
aoqi@0 301 terminate();
aoqi@0 302 }
aoqi@0 303
aoqi@0 304
aoqi@0 305 void ConcurrentMarkThread::yield() {
aoqi@0 306 _sts.yield("Concurrent Mark");
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 void ConcurrentMarkThread::stop() {
aoqi@0 310 {
aoqi@0 311 MutexLockerEx ml(Terminator_lock);
aoqi@0 312 _should_terminate = true;
aoqi@0 313 }
aoqi@0 314
aoqi@0 315 {
aoqi@0 316 MutexLockerEx ml(CGC_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 317 CGC_lock->notify_all();
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 {
aoqi@0 321 MutexLockerEx ml(Terminator_lock);
aoqi@0 322 while (!_has_terminated) {
aoqi@0 323 Terminator_lock->wait();
aoqi@0 324 }
aoqi@0 325 }
aoqi@0 326 }
aoqi@0 327
aoqi@0 328 void ConcurrentMarkThread::print() const {
aoqi@0 329 print_on(tty);
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 void ConcurrentMarkThread::print_on(outputStream* st) const {
aoqi@0 333 st->print("\"G1 Main Concurrent Mark GC Thread\" ");
aoqi@0 334 Thread::print_on(st);
aoqi@0 335 st->cr();
aoqi@0 336 }
aoqi@0 337
aoqi@0 338 void ConcurrentMarkThread::sleepBeforeNextCycle() {
aoqi@0 339 // We join here because we don't want to do the "shouldConcurrentMark()"
aoqi@0 340 // below while the world is otherwise stopped.
aoqi@0 341 assert(!in_progress(), "should have been cleared");
aoqi@0 342
aoqi@0 343 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 344 while (!started() && !_should_terminate) {
aoqi@0 345 CGC_lock->wait(Mutex::_no_safepoint_check_flag);
aoqi@0 346 }
aoqi@0 347
aoqi@0 348 if (started()) {
aoqi@0 349 set_in_progress();
aoqi@0 350 clear_started();
aoqi@0 351 }
aoqi@0 352 }
aoqi@0 353
aoqi@0 354 // Note: As is the case with CMS - this method, although exported
aoqi@0 355 // by the ConcurrentMarkThread, which is a non-JavaThread, can only
aoqi@0 356 // be called by a JavaThread. Currently this is done at vm creation
aoqi@0 357 // time (post-vm-init) by the main/Primordial (Java)Thread.
aoqi@0 358 // XXX Consider changing this in the future to allow the CM thread
aoqi@0 359 // itself to create this thread?
aoqi@0 360 void ConcurrentMarkThread::makeSurrogateLockerThread(TRAPS) {
aoqi@0 361 assert(UseG1GC, "SLT thread needed only for concurrent GC");
aoqi@0 362 assert(THREAD->is_Java_thread(), "must be a Java thread");
aoqi@0 363 assert(_slt == NULL, "SLT already created");
aoqi@0 364 _slt = SurrogateLockerThread::make(THREAD);
aoqi@0 365 }

mercurial