src/share/vm/gc_implementation/g1/vm_operations_g1.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/vm_operations_g1.hpp"
aoqi@0 31 #include "gc_implementation/shared/gcTimer.hpp"
aoqi@0 32 #include "gc_implementation/shared/gcTraceTime.hpp"
aoqi@0 33 #include "gc_implementation/shared/isGCActiveMark.hpp"
aoqi@0 34 #include "gc_implementation/g1/vm_operations_g1.hpp"
aoqi@0 35 #include "runtime/interfaceSupport.hpp"
aoqi@0 36
aoqi@0 37 VM_G1CollectForAllocation::VM_G1CollectForAllocation(
aoqi@0 38 unsigned int gc_count_before,
aoqi@0 39 size_t word_size)
aoqi@0 40 : VM_G1OperationWithAllocRequest(gc_count_before, word_size,
aoqi@0 41 GCCause::_allocation_failure) {
aoqi@0 42 guarantee(word_size > 0, "an allocation should always be requested");
aoqi@0 43 }
aoqi@0 44
aoqi@0 45 void VM_G1CollectForAllocation::doit() {
aoqi@0 46 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 47 GCCauseSetter x(g1h, _gc_cause);
aoqi@0 48 _result = g1h->satisfy_failed_allocation(_word_size, &_pause_succeeded);
aoqi@0 49 assert(_result == NULL || _pause_succeeded,
aoqi@0 50 "if we get back a result, the pause should have succeeded");
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 void VM_G1CollectFull::doit() {
aoqi@0 54 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 55 GCCauseSetter x(g1h, _gc_cause);
aoqi@0 56 g1h->do_full_collection(false /* clear_all_soft_refs */);
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 VM_G1IncCollectionPause::VM_G1IncCollectionPause(
aoqi@0 60 unsigned int gc_count_before,
aoqi@0 61 size_t word_size,
aoqi@0 62 bool should_initiate_conc_mark,
aoqi@0 63 double target_pause_time_ms,
aoqi@0 64 GCCause::Cause gc_cause)
aoqi@0 65 : VM_G1OperationWithAllocRequest(gc_count_before, word_size, gc_cause),
aoqi@0 66 _should_initiate_conc_mark(should_initiate_conc_mark),
aoqi@0 67 _target_pause_time_ms(target_pause_time_ms),
aoqi@0 68 _should_retry_gc(false),
aoqi@0 69 _old_marking_cycles_completed_before(0) {
aoqi@0 70 guarantee(target_pause_time_ms > 0.0,
aoqi@0 71 err_msg("target_pause_time_ms = %1.6lf should be positive",
aoqi@0 72 target_pause_time_ms));
aoqi@0 73 _gc_cause = gc_cause;
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 bool VM_G1IncCollectionPause::doit_prologue() {
aoqi@0 77 bool res = VM_GC_Operation::doit_prologue();
aoqi@0 78 if (!res) {
aoqi@0 79 if (_should_initiate_conc_mark) {
aoqi@0 80 // The prologue can fail for a couple of reasons. The first is that another GC
aoqi@0 81 // got scheduled and prevented the scheduling of the initial mark GC. The
aoqi@0 82 // second is that the GC locker may be active and the heap can't be expanded.
aoqi@0 83 // In both cases we want to retry the GC so that the initial mark pause is
aoqi@0 84 // actually scheduled. In the second case, however, we should stall until
aoqi@0 85 // until the GC locker is no longer active and then retry the initial mark GC.
aoqi@0 86 _should_retry_gc = true;
aoqi@0 87 }
aoqi@0 88 }
aoqi@0 89 return res;
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 void VM_G1IncCollectionPause::doit() {
aoqi@0 93 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 94 assert(!_should_initiate_conc_mark ||
aoqi@0 95 ((_gc_cause == GCCause::_gc_locker && GCLockerInvokesConcurrent) ||
aoqi@0 96 (_gc_cause == GCCause::_java_lang_system_gc && ExplicitGCInvokesConcurrent) ||
aoqi@0 97 _gc_cause == GCCause::_g1_humongous_allocation),
aoqi@0 98 "only a GC locker, a System.gc() or a hum allocation induced GC should start a cycle");
aoqi@0 99
aoqi@0 100 if (_word_size > 0) {
aoqi@0 101 // An allocation has been requested. So, try to do that first.
aoqi@0 102 _result = g1h->attempt_allocation_at_safepoint(_word_size,
aoqi@0 103 false /* expect_null_cur_alloc_region */);
aoqi@0 104 if (_result != NULL) {
aoqi@0 105 // If we can successfully allocate before we actually do the
aoqi@0 106 // pause then we will consider this pause successful.
aoqi@0 107 _pause_succeeded = true;
aoqi@0 108 return;
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 GCCauseSetter x(g1h, _gc_cause);
aoqi@0 113 if (_should_initiate_conc_mark) {
aoqi@0 114 // It's safer to read old_marking_cycles_completed() here, given
aoqi@0 115 // that noone else will be updating it concurrently. Since we'll
aoqi@0 116 // only need it if we're initiating a marking cycle, no point in
aoqi@0 117 // setting it earlier.
aoqi@0 118 _old_marking_cycles_completed_before = g1h->old_marking_cycles_completed();
aoqi@0 119
aoqi@0 120 // At this point we are supposed to start a concurrent cycle. We
aoqi@0 121 // will do so if one is not already in progress.
aoqi@0 122 bool res = g1h->g1_policy()->force_initial_mark_if_outside_cycle(_gc_cause);
aoqi@0 123
aoqi@0 124 // The above routine returns true if we were able to force the
aoqi@0 125 // next GC pause to be an initial mark; it returns false if a
aoqi@0 126 // marking cycle is already in progress.
aoqi@0 127 //
aoqi@0 128 // If a marking cycle is already in progress just return and skip the
aoqi@0 129 // pause below - if the reason for requesting this initial mark pause
aoqi@0 130 // was due to a System.gc() then the requesting thread should block in
aoqi@0 131 // doit_epilogue() until the marking cycle is complete.
aoqi@0 132 //
aoqi@0 133 // If this initial mark pause was requested as part of a humongous
aoqi@0 134 // allocation then we know that the marking cycle must just have
aoqi@0 135 // been started by another thread (possibly also allocating a humongous
aoqi@0 136 // object) as there was no active marking cycle when the requesting
aoqi@0 137 // thread checked before calling collect() in
aoqi@0 138 // attempt_allocation_humongous(). Retrying the GC, in this case,
aoqi@0 139 // will cause the requesting thread to spin inside collect() until the
aoqi@0 140 // just started marking cycle is complete - which may be a while. So
aoqi@0 141 // we do NOT retry the GC.
aoqi@0 142 if (!res) {
aoqi@0 143 assert(_word_size == 0, "Concurrent Full GC/Humongous Object IM shouldn't be allocating");
aoqi@0 144 if (_gc_cause != GCCause::_g1_humongous_allocation) {
aoqi@0 145 _should_retry_gc = true;
aoqi@0 146 }
aoqi@0 147 return;
aoqi@0 148 }
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 _pause_succeeded =
aoqi@0 152 g1h->do_collection_pause_at_safepoint(_target_pause_time_ms);
aoqi@0 153 if (_pause_succeeded && _word_size > 0) {
aoqi@0 154 // An allocation had been requested.
aoqi@0 155 _result = g1h->attempt_allocation_at_safepoint(_word_size,
aoqi@0 156 true /* expect_null_cur_alloc_region */);
aoqi@0 157 } else {
aoqi@0 158 assert(_result == NULL, "invariant");
aoqi@0 159 if (!_pause_succeeded) {
aoqi@0 160 // Another possible reason reason for the pause to not be successful
aoqi@0 161 // is that, again, the GC locker is active (and has become active
aoqi@0 162 // since the prologue was executed). In this case we should retry
aoqi@0 163 // the pause after waiting for the GC locker to become inactive.
aoqi@0 164 _should_retry_gc = true;
aoqi@0 165 }
aoqi@0 166 }
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 void VM_G1IncCollectionPause::doit_epilogue() {
aoqi@0 170 VM_GC_Operation::doit_epilogue();
aoqi@0 171
aoqi@0 172 // If the pause was initiated by a System.gc() and
aoqi@0 173 // +ExplicitGCInvokesConcurrent, we have to wait here for the cycle
aoqi@0 174 // that just started (or maybe one that was already in progress) to
aoqi@0 175 // finish.
aoqi@0 176 if (_gc_cause == GCCause::_java_lang_system_gc &&
aoqi@0 177 _should_initiate_conc_mark) {
aoqi@0 178 assert(ExplicitGCInvokesConcurrent,
aoqi@0 179 "the only way to be here is if ExplicitGCInvokesConcurrent is set");
aoqi@0 180
aoqi@0 181 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 182
aoqi@0 183 // In the doit() method we saved g1h->old_marking_cycles_completed()
aoqi@0 184 // in the _old_marking_cycles_completed_before field. We have to
aoqi@0 185 // wait until we observe that g1h->old_marking_cycles_completed()
aoqi@0 186 // has increased by at least one. This can happen if a) we started
aoqi@0 187 // a cycle and it completes, b) a cycle already in progress
aoqi@0 188 // completes, or c) a Full GC happens.
aoqi@0 189
aoqi@0 190 // If the condition has already been reached, there's no point in
aoqi@0 191 // actually taking the lock and doing the wait.
aoqi@0 192 if (g1h->old_marking_cycles_completed() <=
aoqi@0 193 _old_marking_cycles_completed_before) {
aoqi@0 194 // The following is largely copied from CMS
aoqi@0 195
aoqi@0 196 Thread* thr = Thread::current();
aoqi@0 197 assert(thr->is_Java_thread(), "invariant");
aoqi@0 198 JavaThread* jt = (JavaThread*)thr;
aoqi@0 199 ThreadToNativeFromVM native(jt);
aoqi@0 200
aoqi@0 201 MutexLockerEx x(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 202 while (g1h->old_marking_cycles_completed() <=
aoqi@0 203 _old_marking_cycles_completed_before) {
aoqi@0 204 FullGCCount_lock->wait(Mutex::_no_safepoint_check_flag);
aoqi@0 205 }
aoqi@0 206 }
aoqi@0 207 }
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 void VM_CGC_Operation::acquire_pending_list_lock() {
aoqi@0 211 assert(_needs_pll, "don't call this otherwise");
aoqi@0 212 // The caller may block while communicating
aoqi@0 213 // with the SLT thread in order to acquire/release the PLL.
aoqi@0 214 ConcurrentMarkThread::slt()->
aoqi@0 215 manipulatePLL(SurrogateLockerThread::acquirePLL);
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 void VM_CGC_Operation::release_and_notify_pending_list_lock() {
aoqi@0 219 assert(_needs_pll, "don't call this otherwise");
aoqi@0 220 // The caller may block while communicating
aoqi@0 221 // with the SLT thread in order to acquire/release the PLL.
aoqi@0 222 ConcurrentMarkThread::slt()->
aoqi@0 223 manipulatePLL(SurrogateLockerThread::releaseAndNotifyPLL);
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 void VM_CGC_Operation::doit() {
aoqi@0 227 gclog_or_tty->date_stamp(G1Log::fine() && PrintGCDateStamps);
aoqi@0 228 TraceCPUTime tcpu(G1Log::finer(), true, gclog_or_tty);
aoqi@0 229 GCTraceTime t(_printGCMessage, G1Log::fine(), true, G1CollectedHeap::heap()->gc_timer_cm());
aoqi@0 230 SharedHeap* sh = SharedHeap::heap();
aoqi@0 231 // This could go away if CollectedHeap gave access to _gc_is_active...
aoqi@0 232 if (sh != NULL) {
aoqi@0 233 IsGCActiveMark x;
aoqi@0 234 _cl->do_void();
aoqi@0 235 } else {
aoqi@0 236 _cl->do_void();
aoqi@0 237 }
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 bool VM_CGC_Operation::doit_prologue() {
aoqi@0 241 // Note the relative order of the locks must match that in
aoqi@0 242 // VM_GC_Operation::doit_prologue() or deadlocks can occur
aoqi@0 243 if (_needs_pll) {
aoqi@0 244 acquire_pending_list_lock();
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 Heap_lock->lock();
aoqi@0 248 SharedHeap::heap()->_thread_holds_heap_lock_for_gc = true;
aoqi@0 249 return true;
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 void VM_CGC_Operation::doit_epilogue() {
aoqi@0 253 // Note the relative order of the unlocks must match that in
aoqi@0 254 // VM_GC_Operation::doit_epilogue()
aoqi@0 255 SharedHeap::heap()->_thread_holds_heap_lock_for_gc = false;
aoqi@0 256 Heap_lock->unlock();
aoqi@0 257 if (_needs_pll) {
aoqi@0 258 release_and_notify_pending_list_lock();
aoqi@0 259 }
aoqi@0 260 }

mercurial