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

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

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6996
f3aeae1f9fc5
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
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);
aoqi@0 196 _res = gch->satisfy_failed_allocation(_size, _tlab);
aoqi@0 197 assert(gch->is_in_reserved_or_null(_res), "result not in heap");
aoqi@0 198
aoqi@0 199 if (_res == 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
stefank@6996 212 // Returns true iff concurrent GCs unloads metadata.
stefank@6992 213 bool VM_CollectForMetadataAllocation::initiate_concurrent_GC() {
stefank@6992 214 #if INCLUDE_ALL_GCS
stefank@6994 215 if (UseConcMarkSweepGC && CMSClassUnloadingEnabled) {
stefank@6994 216 MetaspaceGC::set_should_concurrent_collect(true);
stefank@6994 217 return true;
stefank@6994 218 }
stefank@6992 219
stefank@6996 220 if (UseG1GC && ClassUnloadingWithConcurrentMark) {
stefank@6994 221 G1CollectedHeap* g1h = G1CollectedHeap::heap();
stefank@6994 222 g1h->g1_policy()->set_initiate_conc_mark_if_possible();
stefank@6992 223
stefank@6994 224 GCCauseSetter x(g1h, _gc_cause);
stefank@6992 225
stefank@6994 226 // At this point we are supposed to start a concurrent cycle. We
stefank@6994 227 // will do so if one is not already in progress.
stefank@6994 228 bool should_start = g1h->g1_policy()->force_initial_mark_if_outside_cycle(_gc_cause);
stefank@6994 229
stefank@6994 230 if (should_start) {
stefank@6994 231 double pause_target = g1h->g1_policy()->max_pause_time_ms();
stefank@6994 232 g1h->do_collection_pause_at_safepoint(pause_target);
stefank@6992 233 }
stefank@6992 234 return true;
stefank@6992 235 }
stefank@6992 236 #endif
stefank@6994 237
stefank@6992 238 return false;
stefank@6992 239 }
stefank@6992 240
stefank@6992 241 static void log_metaspace_alloc_failure_for_concurrent_GC() {
stefank@6992 242 if (Verbose && PrintGCDetails) {
stefank@6992 243 if (UseConcMarkSweepGC) {
stefank@6992 244 gclog_or_tty->print_cr("\nCMS full GC for Metaspace");
stefank@6992 245 } else if (UseG1GC) {
stefank@6992 246 gclog_or_tty->print_cr("\nG1 full GC for Metaspace");
stefank@6992 247 }
stefank@6992 248 }
stefank@6992 249 }
stefank@6992 250
aoqi@0 251 void VM_CollectForMetadataAllocation::doit() {
aoqi@0 252 SvcGCMarker sgcm(SvcGCMarker::FULL);
aoqi@0 253
aoqi@0 254 CollectedHeap* heap = Universe::heap();
aoqi@0 255 GCCauseSetter gccs(heap, _gc_cause);
aoqi@0 256
aoqi@0 257 // Check again if the space is available. Another thread
aoqi@0 258 // may have similarly failed a metadata allocation and induced
aoqi@0 259 // a GC that freed space for the allocation.
aoqi@0 260 if (!MetadataAllocationFailALot) {
aoqi@0 261 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
stefank@6992 262 if (_result != NULL) {
stefank@6992 263 return;
aoqi@0 264 }
aoqi@0 265 }
aoqi@0 266
stefank@6992 267 if (initiate_concurrent_GC()) {
stefank@6992 268 // For CMS and G1 expand since the collection is going to be concurrent.
stefank@6992 269 _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype);
stefank@6992 270 if (_result != NULL) {
stefank@6992 271 return;
stefank@6992 272 }
stefank@6992 273
stefank@6992 274 log_metaspace_alloc_failure_for_concurrent_GC();
stefank@6992 275 }
stefank@6992 276
stefank@6992 277 // Don't clear the soft refs yet.
stefank@6992 278 heap->collect_as_vm_thread(GCCause::_metadata_GC_threshold);
stefank@6992 279 // After a GC try to allocate without expanding. Could fail
stefank@6992 280 // and expansion will be tried below.
stefank@6992 281 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
stefank@6992 282 if (_result != NULL) {
stefank@6992 283 return;
stefank@6992 284 }
stefank@6992 285
stefank@6992 286 // If still failing, allow the Metaspace to expand.
stefank@6992 287 // See delta_capacity_until_GC() for explanation of the
stefank@6992 288 // amount of the expansion.
stefank@6992 289 // This should work unless there really is no more space
stefank@6992 290 // or a MaxMetaspaceSize has been specified on the command line.
stefank@6992 291 _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype);
stefank@6992 292 if (_result != NULL) {
stefank@6992 293 return;
stefank@6992 294 }
stefank@6992 295
stefank@6992 296 // If expansion failed, do a last-ditch collection and try allocating
stefank@6992 297 // again. A last-ditch collection will clear softrefs. This
stefank@6992 298 // behavior is similar to the last-ditch collection done for perm
stefank@6992 299 // gen when it was full and a collection for failed allocation
stefank@6992 300 // did not free perm gen space.
stefank@6992 301 heap->collect_as_vm_thread(GCCause::_last_ditch_collection);
stefank@6992 302 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
stefank@6992 303 if (_result != NULL) {
stefank@6992 304 return;
stefank@6992 305 }
stefank@6992 306
stefank@6992 307 if (Verbose && PrintGCDetails) {
stefank@6992 308 gclog_or_tty->print_cr("\nAfter Metaspace GC failed to allocate size "
stefank@6992 309 SIZE_FORMAT, _size);
stefank@6992 310 }
stefank@6992 311
stefank@6992 312 if (GC_locker::is_active_and_needs_gc()) {
aoqi@0 313 set_gc_locked();
aoqi@0 314 }
aoqi@0 315 }

mercurial