src/share/vm/gc_implementation/shared/vmGCOperations.cpp

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

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

merge

aoqi@0 1 /*
stefank@6994 2 * Copyright (c) 2005, 2014, 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 "classfile/classLoader.hpp"
aoqi@0 27 #include "classfile/javaClasses.hpp"
aoqi@0 28 #include "gc_implementation/shared/vmGCOperations.hpp"
aoqi@0 29 #include "memory/gcLocker.inline.hpp"
aoqi@0 30 #include "memory/genCollectedHeap.hpp"
aoqi@0 31 #include "memory/oopFactory.hpp"
aoqi@0 32 #include "oops/instanceKlass.hpp"
aoqi@0 33 #include "oops/instanceRefKlass.hpp"
aoqi@0 34 #include "runtime/handles.inline.hpp"
aoqi@0 35 #include "runtime/init.hpp"
aoqi@0 36 #include "runtime/interfaceSupport.hpp"
aoqi@0 37 #include "utilities/dtrace.hpp"
aoqi@0 38 #include "utilities/preserveException.hpp"
aoqi@0 39 #include "utilities/macros.hpp"
aoqi@0 40 #if INCLUDE_ALL_GCS
aoqi@0 41 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
aoqi@0 42 #endif // INCLUDE_ALL_GCS
aoqi@0 43
aoqi@0 44 #ifndef USDT2
aoqi@0 45 HS_DTRACE_PROBE_DECL1(hotspot, gc__begin, bool);
aoqi@0 46 HS_DTRACE_PROBE_DECL(hotspot, gc__end);
aoqi@0 47 #endif /* !USDT2 */
aoqi@0 48
aoqi@0 49 // The same dtrace probe can't be inserted in two different files, so we
aoqi@0 50 // have to call it here, so it's only in one file. Can't create new probes
aoqi@0 51 // for the other file anymore. The dtrace probes have to remain stable.
aoqi@0 52 void VM_GC_Operation::notify_gc_begin(bool full) {
aoqi@0 53 #ifndef USDT2
aoqi@0 54 HS_DTRACE_PROBE1(hotspot, gc__begin, full);
aoqi@0 55 HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
aoqi@0 56 #else /* USDT2 */
aoqi@0 57 HOTSPOT_GC_BEGIN(
aoqi@0 58 full);
aoqi@0 59 #endif /* USDT2 */
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 void VM_GC_Operation::notify_gc_end() {
aoqi@0 63 #ifndef USDT2
aoqi@0 64 HS_DTRACE_PROBE(hotspot, gc__end);
aoqi@0 65 HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
aoqi@0 66 #else /* USDT2 */
aoqi@0 67 HOTSPOT_GC_END(
aoqi@0 68 );
aoqi@0 69 #endif /* USDT2 */
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 void VM_GC_Operation::acquire_pending_list_lock() {
aoqi@0 73 // we may enter this with pending exception set
aoqi@0 74 InstanceRefKlass::acquire_pending_list_lock(&_pending_list_basic_lock);
aoqi@0 75 }
aoqi@0 76
aoqi@0 77
aoqi@0 78 void VM_GC_Operation::release_and_notify_pending_list_lock() {
aoqi@0 79
aoqi@0 80 InstanceRefKlass::release_and_notify_pending_list_lock(&_pending_list_basic_lock);
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 // Allocations may fail in several threads at about the same time,
aoqi@0 84 // resulting in multiple gc requests. We only want to do one of them.
aoqi@0 85 // In case a GC locker is active and the need for a GC is already signalled,
aoqi@0 86 // we want to skip this GC attempt altogether, without doing a futile
aoqi@0 87 // safepoint operation.
aoqi@0 88 bool VM_GC_Operation::skip_operation() const {
aoqi@0 89 bool skip = (_gc_count_before != Universe::heap()->total_collections());
aoqi@0 90 if (_full && skip) {
aoqi@0 91 skip = (_full_gc_count_before != Universe::heap()->total_full_collections());
aoqi@0 92 }
aoqi@0 93 if (!skip && GC_locker::is_active_and_needs_gc()) {
aoqi@0 94 skip = Universe::heap()->is_maximal_no_gc();
aoqi@0 95 assert(!(skip && (_gc_cause == GCCause::_gc_locker)),
aoqi@0 96 "GC_locker cannot be active when initiating GC");
aoqi@0 97 }
aoqi@0 98 return skip;
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 bool VM_GC_Operation::doit_prologue() {
aoqi@0 102 assert(Thread::current()->is_Java_thread(), "just checking");
aoqi@0 103 assert(((_gc_cause != GCCause::_no_gc) &&
aoqi@0 104 (_gc_cause != GCCause::_no_cause_specified)), "Illegal GCCause");
aoqi@0 105
aoqi@0 106 // To be able to handle a GC the VM initialization needs to be completed.
aoqi@0 107 if (!is_init_completed()) {
aoqi@0 108 vm_exit_during_initialization(
aoqi@0 109 err_msg("GC triggered before VM initialization completed. Try increasing "
aoqi@0 110 "NewSize, current value " UINTX_FORMAT "%s.",
aoqi@0 111 byte_size_in_proper_unit(NewSize),
aoqi@0 112 proper_unit_for_byte_size(NewSize)));
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 acquire_pending_list_lock();
aoqi@0 116 // If the GC count has changed someone beat us to the collection
aoqi@0 117 // Get the Heap_lock after the pending_list_lock.
aoqi@0 118 Heap_lock->lock();
aoqi@0 119
aoqi@0 120 // Check invocations
aoqi@0 121 if (skip_operation()) {
aoqi@0 122 // skip collection
aoqi@0 123 Heap_lock->unlock();
aoqi@0 124 release_and_notify_pending_list_lock();
aoqi@0 125 _prologue_succeeded = false;
aoqi@0 126 } else {
aoqi@0 127 _prologue_succeeded = true;
aoqi@0 128 SharedHeap* sh = SharedHeap::heap();
aoqi@0 129 if (sh != NULL) sh->_thread_holds_heap_lock_for_gc = true;
aoqi@0 130 }
aoqi@0 131 return _prologue_succeeded;
aoqi@0 132 }
aoqi@0 133
aoqi@0 134
aoqi@0 135 void VM_GC_Operation::doit_epilogue() {
aoqi@0 136 assert(Thread::current()->is_Java_thread(), "just checking");
aoqi@0 137 // Release the Heap_lock first.
aoqi@0 138 SharedHeap* sh = SharedHeap::heap();
aoqi@0 139 if (sh != NULL) sh->_thread_holds_heap_lock_for_gc = false;
aoqi@0 140 Heap_lock->unlock();
aoqi@0 141 release_and_notify_pending_list_lock();
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 bool VM_GC_HeapInspection::doit_prologue() {
aoqi@0 145 if (Universe::heap()->supports_heap_inspection()) {
aoqi@0 146 return VM_GC_Operation::doit_prologue();
aoqi@0 147 } else {
aoqi@0 148 return false;
aoqi@0 149 }
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 bool VM_GC_HeapInspection::skip_operation() const {
aoqi@0 153 assert(Universe::heap()->supports_heap_inspection(), "huh?");
aoqi@0 154 return false;
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 bool VM_GC_HeapInspection::collect() {
aoqi@0 158 if (GC_locker::is_active()) {
aoqi@0 159 return false;
aoqi@0 160 }
aoqi@0 161 Universe::heap()->collect_as_vm_thread(GCCause::_heap_inspection);
aoqi@0 162 return true;
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 void VM_GC_HeapInspection::doit() {
aoqi@0 166 HandleMark hm;
aoqi@0 167 Universe::heap()->ensure_parsability(false); // must happen, even if collection does
aoqi@0 168 // not happen (e.g. due to GC_locker)
aoqi@0 169 // or _full_gc being false
aoqi@0 170 if (_full_gc) {
aoqi@0 171 if (!collect()) {
aoqi@0 172 // The collection attempt was skipped because the gc locker is held.
aoqi@0 173 // The following dump may then be a tad misleading to someone expecting
aoqi@0 174 // only live objects to show up in the dump (see CR 6944195). Just issue
aoqi@0 175 // a suitable warning in that case and do not attempt to do a collection.
aoqi@0 176 // The latter is a subtle point, because even a failed attempt
aoqi@0 177 // to GC will, in fact, induce one in the future, which we
aoqi@0 178 // probably want to avoid in this case because the GC that we may
aoqi@0 179 // be about to attempt holds value for us only
aoqi@0 180 // if it happens now and not if it happens in the eventual
aoqi@0 181 // future.
aoqi@0 182 warning("GC locker is held; pre-dump GC was skipped");
aoqi@0 183 }
aoqi@0 184 }
aoqi@0 185 HeapInspection inspect(_csv_format, _print_help, _print_class_stats,
aoqi@0 186 _columns);
aoqi@0 187 inspect.heap_inspection(_out);
aoqi@0 188 }
aoqi@0 189
aoqi@0 190
aoqi@0 191 void VM_GenCollectForAllocation::doit() {
aoqi@0 192 SvcGCMarker sgcm(SvcGCMarker::MINOR);
aoqi@0 193
aoqi@0 194 GenCollectedHeap* gch = GenCollectedHeap::heap();
aoqi@0 195 GCCauseSetter gccs(gch, _gc_cause);
mlarsson@7687 196 _result = gch->satisfy_failed_allocation(_word_size, _tlab);
mlarsson@7687 197 assert(gch->is_in_reserved_or_null(_result), "result not in heap");
aoqi@0 198
mlarsson@7687 199 if (_result == NULL && GC_locker::is_active_and_needs_gc()) {
aoqi@0 200 set_gc_locked();
aoqi@0 201 }
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 void VM_GenCollectFull::doit() {
aoqi@0 205 SvcGCMarker sgcm(SvcGCMarker::FULL);
aoqi@0 206
aoqi@0 207 GenCollectedHeap* gch = GenCollectedHeap::heap();
aoqi@0 208 GCCauseSetter gccs(gch, _gc_cause);
aoqi@0 209 gch->do_full_collection(gch->must_clear_all_soft_refs(), _max_level);
aoqi@0 210 }
aoqi@0 211
mlarsson@7688 212 VM_CollectForMetadataAllocation::VM_CollectForMetadataAllocation(ClassLoaderData* loader_data,
mlarsson@7688 213 size_t size,
mlarsson@7688 214 Metaspace::MetadataType mdtype,
mlarsson@7688 215 uint gc_count_before,
mlarsson@7688 216 uint full_gc_count_before,
mlarsson@7688 217 GCCause::Cause gc_cause)
mlarsson@7688 218 : VM_GC_Operation(gc_count_before, gc_cause, full_gc_count_before, true),
mlarsson@7688 219 _loader_data(loader_data), _size(size), _mdtype(mdtype), _result(NULL) {
mlarsson@7688 220 assert(_size != 0, "An allocation should always be requested with this operation.");
mlarsson@7688 221 AllocTracer::send_allocation_requiring_gc_event(_size * HeapWordSize, GCId::peek());
mlarsson@7688 222 }
mlarsson@7688 223
stefank@6996 224 // Returns true iff concurrent GCs unloads metadata.
stefank@6992 225 bool VM_CollectForMetadataAllocation::initiate_concurrent_GC() {
stefank@6992 226 #if INCLUDE_ALL_GCS
stefank@6994 227 if (UseConcMarkSweepGC && CMSClassUnloadingEnabled) {
stefank@6994 228 MetaspaceGC::set_should_concurrent_collect(true);
stefank@6994 229 return true;
stefank@6994 230 }
stefank@6992 231
stefank@6996 232 if (UseG1GC && ClassUnloadingWithConcurrentMark) {
stefank@6994 233 G1CollectedHeap* g1h = G1CollectedHeap::heap();
stefank@6994 234 g1h->g1_policy()->set_initiate_conc_mark_if_possible();
stefank@6992 235
stefank@6994 236 GCCauseSetter x(g1h, _gc_cause);
stefank@6992 237
stefank@6994 238 // At this point we are supposed to start a concurrent cycle. We
stefank@6994 239 // will do so if one is not already in progress.
stefank@6994 240 bool should_start = g1h->g1_policy()->force_initial_mark_if_outside_cycle(_gc_cause);
stefank@6994 241
stefank@6994 242 if (should_start) {
stefank@6994 243 double pause_target = g1h->g1_policy()->max_pause_time_ms();
stefank@6994 244 g1h->do_collection_pause_at_safepoint(pause_target);
stefank@6992 245 }
stefank@6992 246 return true;
stefank@6992 247 }
stefank@6992 248 #endif
stefank@6994 249
stefank@6992 250 return false;
stefank@6992 251 }
stefank@6992 252
stefank@6992 253 static void log_metaspace_alloc_failure_for_concurrent_GC() {
stefank@6992 254 if (Verbose && PrintGCDetails) {
stefank@6992 255 if (UseConcMarkSweepGC) {
stefank@6992 256 gclog_or_tty->print_cr("\nCMS full GC for Metaspace");
stefank@6992 257 } else if (UseG1GC) {
stefank@6992 258 gclog_or_tty->print_cr("\nG1 full GC for Metaspace");
stefank@6992 259 }
stefank@6992 260 }
stefank@6992 261 }
stefank@6992 262
aoqi@0 263 void VM_CollectForMetadataAllocation::doit() {
aoqi@0 264 SvcGCMarker sgcm(SvcGCMarker::FULL);
aoqi@0 265
aoqi@0 266 CollectedHeap* heap = Universe::heap();
aoqi@0 267 GCCauseSetter gccs(heap, _gc_cause);
aoqi@0 268
aoqi@0 269 // Check again if the space is available. Another thread
aoqi@0 270 // may have similarly failed a metadata allocation and induced
aoqi@0 271 // a GC that freed space for the allocation.
aoqi@0 272 if (!MetadataAllocationFailALot) {
aoqi@0 273 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
stefank@6992 274 if (_result != NULL) {
stefank@6992 275 return;
aoqi@0 276 }
aoqi@0 277 }
aoqi@0 278
stefank@6992 279 if (initiate_concurrent_GC()) {
stefank@6992 280 // For CMS and G1 expand since the collection is going to be concurrent.
stefank@6992 281 _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype);
stefank@6992 282 if (_result != NULL) {
stefank@6992 283 return;
stefank@6992 284 }
stefank@6992 285
stefank@6992 286 log_metaspace_alloc_failure_for_concurrent_GC();
stefank@6992 287 }
stefank@6992 288
stefank@6992 289 // Don't clear the soft refs yet.
stefank@6992 290 heap->collect_as_vm_thread(GCCause::_metadata_GC_threshold);
stefank@6992 291 // After a GC try to allocate without expanding. Could fail
stefank@6992 292 // and expansion will be tried below.
stefank@6992 293 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
stefank@6992 294 if (_result != NULL) {
stefank@6992 295 return;
stefank@6992 296 }
stefank@6992 297
stefank@6992 298 // If still failing, allow the Metaspace to expand.
stefank@6992 299 // See delta_capacity_until_GC() for explanation of the
stefank@6992 300 // amount of the expansion.
stefank@6992 301 // This should work unless there really is no more space
stefank@6992 302 // or a MaxMetaspaceSize has been specified on the command line.
stefank@6992 303 _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype);
stefank@6992 304 if (_result != NULL) {
stefank@6992 305 return;
stefank@6992 306 }
stefank@6992 307
stefank@6992 308 // If expansion failed, do a last-ditch collection and try allocating
stefank@6992 309 // again. A last-ditch collection will clear softrefs. This
stefank@6992 310 // behavior is similar to the last-ditch collection done for perm
stefank@6992 311 // gen when it was full and a collection for failed allocation
stefank@6992 312 // did not free perm gen space.
stefank@6992 313 heap->collect_as_vm_thread(GCCause::_last_ditch_collection);
stefank@6992 314 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
stefank@6992 315 if (_result != NULL) {
stefank@6992 316 return;
stefank@6992 317 }
stefank@6992 318
stefank@6992 319 if (Verbose && PrintGCDetails) {
stefank@6992 320 gclog_or_tty->print_cr("\nAfter Metaspace GC failed to allocate size "
stefank@6992 321 SIZE_FORMAT, _size);
stefank@6992 322 }
stefank@6992 323
stefank@6992 324 if (GC_locker::is_active_and_needs_gc()) {
aoqi@0 325 set_gc_locked();
aoqi@0 326 }
aoqi@0 327 }
mlarsson@7688 328
mlarsson@7688 329 VM_CollectForAllocation::VM_CollectForAllocation(size_t word_size, uint gc_count_before, GCCause::Cause cause)
mlarsson@7688 330 : VM_GC_Operation(gc_count_before, cause), _result(NULL), _word_size(word_size) {
mlarsson@7688 331 // Only report if operation was really caused by an allocation.
mlarsson@7688 332 if (_word_size != 0) {
mlarsson@7688 333 AllocTracer::send_allocation_requiring_gc_event(_word_size * HeapWordSize, GCId::peek());
mlarsson@7688 334 }
mlarsson@7688 335 }

mercurial