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

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial