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

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7360
4e4ebe50c8e3
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial