Mon, 07 Jul 2014 10:12:40 +0200
8049421: G1 Class Unloading after completing a concurrent mark cycle
Reviewed-by: tschatzl, ehelin, brutisso, coleenp, roland, iveresov
Contributed-by: stefan.karlsson@oracle.com, mikael.gerdin@oracle.com
duke@435 | 1 | /* |
acorn@4497 | 2 | * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "classfile/classLoader.hpp" |
stefank@2314 | 27 | #include "classfile/javaClasses.hpp" |
stefank@2314 | 28 | #include "gc_implementation/shared/vmGCOperations.hpp" |
stefank@2314 | 29 | #include "memory/gcLocker.inline.hpp" |
stefank@2314 | 30 | #include "memory/genCollectedHeap.hpp" |
stefank@2314 | 31 | #include "memory/oopFactory.hpp" |
stefank@2314 | 32 | #include "oops/instanceKlass.hpp" |
stefank@2314 | 33 | #include "oops/instanceRefKlass.hpp" |
stefank@2314 | 34 | #include "runtime/handles.inline.hpp" |
stefank@2314 | 35 | #include "runtime/init.hpp" |
stefank@2314 | 36 | #include "runtime/interfaceSupport.hpp" |
stefank@2314 | 37 | #include "utilities/dtrace.hpp" |
stefank@2314 | 38 | #include "utilities/preserveException.hpp" |
jprovino@4542 | 39 | #include "utilities/macros.hpp" |
jprovino@4542 | 40 | #if INCLUDE_ALL_GCS |
stefank@2314 | 41 | #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" |
jprovino@4542 | 42 | #endif // INCLUDE_ALL_GCS |
kamg@2445 | 43 | |
dcubed@3202 | 44 | #ifndef USDT2 |
duke@435 | 45 | HS_DTRACE_PROBE_DECL1(hotspot, gc__begin, bool); |
duke@435 | 46 | HS_DTRACE_PROBE_DECL(hotspot, gc__end); |
dcubed@3202 | 47 | #endif /* !USDT2 */ |
duke@435 | 48 | |
duke@435 | 49 | // The same dtrace probe can't be inserted in two different files, so we |
duke@435 | 50 | // have to call it here, so it's only in one file. Can't create new probes |
duke@435 | 51 | // for the other file anymore. The dtrace probes have to remain stable. |
duke@435 | 52 | void VM_GC_Operation::notify_gc_begin(bool full) { |
dcubed@3202 | 53 | #ifndef USDT2 |
duke@435 | 54 | HS_DTRACE_PROBE1(hotspot, gc__begin, full); |
jcoomes@1902 | 55 | HS_DTRACE_WORKAROUND_TAIL_CALL_BUG(); |
dcubed@3202 | 56 | #else /* USDT2 */ |
dcubed@3202 | 57 | HOTSPOT_GC_BEGIN( |
dcubed@3202 | 58 | full); |
dcubed@3202 | 59 | #endif /* USDT2 */ |
duke@435 | 60 | } |
duke@435 | 61 | |
duke@435 | 62 | void VM_GC_Operation::notify_gc_end() { |
dcubed@3202 | 63 | #ifndef USDT2 |
duke@435 | 64 | HS_DTRACE_PROBE(hotspot, gc__end); |
jcoomes@1902 | 65 | HS_DTRACE_WORKAROUND_TAIL_CALL_BUG(); |
dcubed@3202 | 66 | #else /* USDT2 */ |
dcubed@3202 | 67 | HOTSPOT_GC_END( |
dcubed@3202 | 68 | ); |
dcubed@3202 | 69 | #endif /* USDT2 */ |
duke@435 | 70 | } |
duke@435 | 71 | |
duke@435 | 72 | void VM_GC_Operation::acquire_pending_list_lock() { |
duke@435 | 73 | // we may enter this with pending exception set |
coleenp@4047 | 74 | InstanceRefKlass::acquire_pending_list_lock(&_pending_list_basic_lock); |
duke@435 | 75 | } |
duke@435 | 76 | |
duke@435 | 77 | |
duke@435 | 78 | void VM_GC_Operation::release_and_notify_pending_list_lock() { |
duke@435 | 79 | |
coleenp@4047 | 80 | InstanceRefKlass::release_and_notify_pending_list_lock(&_pending_list_basic_lock); |
duke@435 | 81 | } |
duke@435 | 82 | |
duke@435 | 83 | // Allocations may fail in several threads at about the same time, |
duke@435 | 84 | // resulting in multiple gc requests. We only want to do one of them. |
duke@435 | 85 | // In case a GC locker is active and the need for a GC is already signalled, |
duke@435 | 86 | // we want to skip this GC attempt altogether, without doing a futile |
duke@435 | 87 | // safepoint operation. |
duke@435 | 88 | bool VM_GC_Operation::skip_operation() const { |
duke@435 | 89 | bool skip = (_gc_count_before != Universe::heap()->total_collections()); |
duke@435 | 90 | if (_full && skip) { |
duke@435 | 91 | skip = (_full_gc_count_before != Universe::heap()->total_full_collections()); |
duke@435 | 92 | } |
duke@435 | 93 | if (!skip && GC_locker::is_active_and_needs_gc()) { |
duke@435 | 94 | skip = Universe::heap()->is_maximal_no_gc(); |
duke@435 | 95 | assert(!(skip && (_gc_cause == GCCause::_gc_locker)), |
duke@435 | 96 | "GC_locker cannot be active when initiating GC"); |
duke@435 | 97 | } |
duke@435 | 98 | return skip; |
duke@435 | 99 | } |
duke@435 | 100 | |
duke@435 | 101 | bool VM_GC_Operation::doit_prologue() { |
duke@435 | 102 | assert(Thread::current()->is_Java_thread(), "just checking"); |
brutisso@2532 | 103 | assert(((_gc_cause != GCCause::_no_gc) && |
brutisso@2532 | 104 | (_gc_cause != GCCause::_no_cause_specified)), "Illegal GCCause"); |
duke@435 | 105 | |
sjohanss@6619 | 106 | // To be able to handle a GC the VM initialization needs to be completed. |
sjohanss@6619 | 107 | if (!is_init_completed()) { |
sjohanss@6619 | 108 | vm_exit_during_initialization( |
sjohanss@6619 | 109 | err_msg("GC triggered before VM initialization completed. Try increasing " |
sjohanss@6619 | 110 | "NewSize, current value " UINTX_FORMAT "%s.", |
sjohanss@6619 | 111 | byte_size_in_proper_unit(NewSize), |
sjohanss@6619 | 112 | proper_unit_for_byte_size(NewSize))); |
sjohanss@6619 | 113 | } |
sjohanss@6619 | 114 | |
duke@435 | 115 | acquire_pending_list_lock(); |
duke@435 | 116 | // If the GC count has changed someone beat us to the collection |
duke@435 | 117 | // Get the Heap_lock after the pending_list_lock. |
duke@435 | 118 | Heap_lock->lock(); |
ysr@777 | 119 | |
duke@435 | 120 | // Check invocations |
duke@435 | 121 | if (skip_operation()) { |
duke@435 | 122 | // skip collection |
duke@435 | 123 | Heap_lock->unlock(); |
duke@435 | 124 | release_and_notify_pending_list_lock(); |
duke@435 | 125 | _prologue_succeeded = false; |
duke@435 | 126 | } else { |
duke@435 | 127 | _prologue_succeeded = true; |
ysr@777 | 128 | SharedHeap* sh = SharedHeap::heap(); |
ysr@777 | 129 | if (sh != NULL) sh->_thread_holds_heap_lock_for_gc = true; |
duke@435 | 130 | } |
duke@435 | 131 | return _prologue_succeeded; |
duke@435 | 132 | } |
duke@435 | 133 | |
duke@435 | 134 | |
duke@435 | 135 | void VM_GC_Operation::doit_epilogue() { |
duke@435 | 136 | assert(Thread::current()->is_Java_thread(), "just checking"); |
duke@435 | 137 | // Release the Heap_lock first. |
ysr@777 | 138 | SharedHeap* sh = SharedHeap::heap(); |
ysr@777 | 139 | if (sh != NULL) sh->_thread_holds_heap_lock_for_gc = false; |
duke@435 | 140 | Heap_lock->unlock(); |
duke@435 | 141 | release_and_notify_pending_list_lock(); |
duke@435 | 142 | } |
duke@435 | 143 | |
duke@435 | 144 | bool VM_GC_HeapInspection::doit_prologue() { |
duke@435 | 145 | if (Universe::heap()->supports_heap_inspection()) { |
duke@435 | 146 | return VM_GC_Operation::doit_prologue(); |
duke@435 | 147 | } else { |
duke@435 | 148 | return false; |
duke@435 | 149 | } |
duke@435 | 150 | } |
duke@435 | 151 | |
duke@435 | 152 | bool VM_GC_HeapInspection::skip_operation() const { |
duke@435 | 153 | assert(Universe::heap()->supports_heap_inspection(), "huh?"); |
duke@435 | 154 | return false; |
duke@435 | 155 | } |
duke@435 | 156 | |
sla@5237 | 157 | bool VM_GC_HeapInspection::collect() { |
sla@5237 | 158 | if (GC_locker::is_active()) { |
sla@5237 | 159 | return false; |
sla@5237 | 160 | } |
sla@5237 | 161 | Universe::heap()->collect_as_vm_thread(GCCause::_heap_inspection); |
sla@5237 | 162 | return true; |
sla@5237 | 163 | } |
sla@5237 | 164 | |
duke@435 | 165 | void VM_GC_HeapInspection::doit() { |
duke@435 | 166 | HandleMark hm; |
sla@5237 | 167 | Universe::heap()->ensure_parsability(false); // must happen, even if collection does |
sla@5237 | 168 | // not happen (e.g. due to GC_locker) |
sla@5237 | 169 | // or _full_gc being false |
duke@435 | 170 | if (_full_gc) { |
sla@5237 | 171 | if (!collect()) { |
sla@5237 | 172 | // The collection attempt was skipped because the gc locker is held. |
sla@5237 | 173 | // The following dump may then be a tad misleading to someone expecting |
sla@5237 | 174 | // only live objects to show up in the dump (see CR 6944195). Just issue |
sla@5237 | 175 | // a suitable warning in that case and do not attempt to do a collection. |
sla@5237 | 176 | // The latter is a subtle point, because even a failed attempt |
sla@5237 | 177 | // to GC will, in fact, induce one in the future, which we |
sla@5237 | 178 | // probably want to avoid in this case because the GC that we may |
sla@5237 | 179 | // be about to attempt holds value for us only |
sla@5237 | 180 | // if it happens now and not if it happens in the eventual |
sla@5237 | 181 | // future. |
kevinw@1827 | 182 | warning("GC locker is held; pre-dump GC was skipped"); |
kevinw@1827 | 183 | } |
duke@435 | 184 | } |
acorn@4497 | 185 | HeapInspection inspect(_csv_format, _print_help, _print_class_stats, |
acorn@4497 | 186 | _columns); |
sla@5237 | 187 | inspect.heap_inspection(_out); |
duke@435 | 188 | } |
duke@435 | 189 | |
duke@435 | 190 | |
duke@435 | 191 | void VM_GenCollectForAllocation::doit() { |
kamg@2445 | 192 | SvcGCMarker sgcm(SvcGCMarker::MINOR); |
duke@435 | 193 | |
duke@435 | 194 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
duke@435 | 195 | GCCauseSetter gccs(gch, _gc_cause); |
duke@435 | 196 | _res = gch->satisfy_failed_allocation(_size, _tlab); |
duke@435 | 197 | assert(gch->is_in_reserved_or_null(_res), "result not in heap"); |
duke@435 | 198 | |
duke@435 | 199 | if (_res == NULL && GC_locker::is_active_and_needs_gc()) { |
duke@435 | 200 | set_gc_locked(); |
duke@435 | 201 | } |
duke@435 | 202 | } |
duke@435 | 203 | |
duke@435 | 204 | void VM_GenCollectFull::doit() { |
kamg@2445 | 205 | SvcGCMarker sgcm(SvcGCMarker::FULL); |
duke@435 | 206 | |
duke@435 | 207 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
duke@435 | 208 | GCCauseSetter gccs(gch, _gc_cause); |
duke@435 | 209 | gch->do_full_collection(gch->must_clear_all_soft_refs(), _max_level); |
duke@435 | 210 | } |
apetrusenko@574 | 211 | |
stefank@6992 | 212 | bool VM_CollectForMetadataAllocation::initiate_concurrent_GC() { |
stefank@6992 | 213 | #if INCLUDE_ALL_GCS |
stefank@6992 | 214 | if (UseConcMarkSweepGC || UseG1GC) { |
stefank@6992 | 215 | if (UseConcMarkSweepGC && CMSClassUnloadingEnabled) { |
stefank@6992 | 216 | MetaspaceGC::set_should_concurrent_collect(true); |
stefank@6992 | 217 | } else if (UseG1GC) { |
stefank@6992 | 218 | G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
stefank@6992 | 219 | g1h->g1_policy()->set_initiate_conc_mark_if_possible(); |
stefank@6992 | 220 | |
stefank@6992 | 221 | GCCauseSetter x(g1h, _gc_cause); |
stefank@6992 | 222 | |
stefank@6992 | 223 | // At this point we are supposed to start a concurrent cycle. We |
stefank@6992 | 224 | // will do so if one is not already in progress. |
stefank@6992 | 225 | bool should_start = g1h->g1_policy()->force_initial_mark_if_outside_cycle(_gc_cause); |
stefank@6992 | 226 | |
stefank@6992 | 227 | if (should_start) { |
stefank@6992 | 228 | double pause_target = g1h->g1_policy()->max_pause_time_ms(); |
stefank@6992 | 229 | g1h->do_collection_pause_at_safepoint(pause_target); |
stefank@6992 | 230 | } |
stefank@6992 | 231 | } |
stefank@6992 | 232 | |
stefank@6992 | 233 | return true; |
stefank@6992 | 234 | } |
stefank@6992 | 235 | #endif |
stefank@6992 | 236 | return false; |
stefank@6992 | 237 | } |
stefank@6992 | 238 | |
stefank@6992 | 239 | static void log_metaspace_alloc_failure_for_concurrent_GC() { |
stefank@6992 | 240 | if (Verbose && PrintGCDetails) { |
stefank@6992 | 241 | if (UseConcMarkSweepGC) { |
stefank@6992 | 242 | gclog_or_tty->print_cr("\nCMS full GC for Metaspace"); |
stefank@6992 | 243 | } else if (UseG1GC) { |
stefank@6992 | 244 | gclog_or_tty->print_cr("\nG1 full GC for Metaspace"); |
stefank@6992 | 245 | } |
stefank@6992 | 246 | } |
stefank@6992 | 247 | } |
stefank@6992 | 248 | |
coleenp@4037 | 249 | void VM_CollectForMetadataAllocation::doit() { |
kamg@2445 | 250 | SvcGCMarker sgcm(SvcGCMarker::FULL); |
kamg@2445 | 251 | |
coleenp@4037 | 252 | CollectedHeap* heap = Universe::heap(); |
ysr@777 | 253 | GCCauseSetter gccs(heap, _gc_cause); |
coleenp@4037 | 254 | |
coleenp@4037 | 255 | // Check again if the space is available. Another thread |
coleenp@4037 | 256 | // may have similarly failed a metadata allocation and induced |
coleenp@4037 | 257 | // a GC that freed space for the allocation. |
coleenp@4037 | 258 | if (!MetadataAllocationFailALot) { |
coleenp@4037 | 259 | _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype); |
stefank@6992 | 260 | if (_result != NULL) { |
stefank@6992 | 261 | return; |
coleenp@4037 | 262 | } |
coleenp@4037 | 263 | } |
coleenp@4037 | 264 | |
stefank@6992 | 265 | if (initiate_concurrent_GC()) { |
stefank@6992 | 266 | // For CMS and G1 expand since the collection is going to be concurrent. |
stefank@6992 | 267 | _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype); |
stefank@6992 | 268 | if (_result != NULL) { |
stefank@6992 | 269 | return; |
stefank@6992 | 270 | } |
stefank@6992 | 271 | |
stefank@6992 | 272 | log_metaspace_alloc_failure_for_concurrent_GC(); |
stefank@6992 | 273 | } |
stefank@6992 | 274 | |
stefank@6992 | 275 | // Don't clear the soft refs yet. |
stefank@6992 | 276 | heap->collect_as_vm_thread(GCCause::_metadata_GC_threshold); |
stefank@6992 | 277 | // After a GC try to allocate without expanding. Could fail |
stefank@6992 | 278 | // and expansion will be tried below. |
stefank@6992 | 279 | _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype); |
stefank@6992 | 280 | if (_result != NULL) { |
stefank@6992 | 281 | return; |
stefank@6992 | 282 | } |
stefank@6992 | 283 | |
stefank@6992 | 284 | // If still failing, allow the Metaspace to expand. |
stefank@6992 | 285 | // See delta_capacity_until_GC() for explanation of the |
stefank@6992 | 286 | // amount of the expansion. |
stefank@6992 | 287 | // This should work unless there really is no more space |
stefank@6992 | 288 | // or a MaxMetaspaceSize has been specified on the command line. |
stefank@6992 | 289 | _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype); |
stefank@6992 | 290 | if (_result != NULL) { |
stefank@6992 | 291 | return; |
stefank@6992 | 292 | } |
stefank@6992 | 293 | |
stefank@6992 | 294 | // If expansion failed, do a last-ditch collection and try allocating |
stefank@6992 | 295 | // again. A last-ditch collection will clear softrefs. This |
stefank@6992 | 296 | // behavior is similar to the last-ditch collection done for perm |
stefank@6992 | 297 | // gen when it was full and a collection for failed allocation |
stefank@6992 | 298 | // did not free perm gen space. |
stefank@6992 | 299 | heap->collect_as_vm_thread(GCCause::_last_ditch_collection); |
stefank@6992 | 300 | _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype); |
stefank@6992 | 301 | if (_result != NULL) { |
stefank@6992 | 302 | return; |
stefank@6992 | 303 | } |
stefank@6992 | 304 | |
stefank@6992 | 305 | if (Verbose && PrintGCDetails) { |
stefank@6992 | 306 | gclog_or_tty->print_cr("\nAfter Metaspace GC failed to allocate size " |
stefank@6992 | 307 | SIZE_FORMAT, _size); |
stefank@6992 | 308 | } |
stefank@6992 | 309 | |
stefank@6992 | 310 | if (GC_locker::is_active_and_needs_gc()) { |
apetrusenko@574 | 311 | set_gc_locked(); |
apetrusenko@574 | 312 | } |
apetrusenko@574 | 313 | } |