src/share/vm/memory/genCollectedHeap.cpp

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

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7990
1f646daf0d67
parent 7535
7ae4e26cb1e0
child 9703
2fdf635bcf28
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2000, 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/symbolTable.hpp"
aoqi@0 27 #include "classfile/systemDictionary.hpp"
aoqi@0 28 #include "classfile/vmSymbols.hpp"
mgerdin@7975 29 #include "code/codeCache.hpp"
aoqi@0 30 #include "code/icBuffer.hpp"
aoqi@0 31 #include "gc_implementation/shared/collectorCounters.hpp"
brutisso@6904 32 #include "gc_implementation/shared/gcTrace.hpp"
aoqi@0 33 #include "gc_implementation/shared/gcTraceTime.hpp"
aoqi@0 34 #include "gc_implementation/shared/vmGCOperations.hpp"
aoqi@0 35 #include "gc_interface/collectedHeap.inline.hpp"
aoqi@0 36 #include "memory/filemap.hpp"
aoqi@0 37 #include "memory/gcLocker.inline.hpp"
aoqi@0 38 #include "memory/genCollectedHeap.hpp"
aoqi@0 39 #include "memory/genOopClosures.inline.hpp"
aoqi@0 40 #include "memory/generation.inline.hpp"
aoqi@0 41 #include "memory/generationSpec.hpp"
aoqi@0 42 #include "memory/resourceArea.hpp"
aoqi@0 43 #include "memory/sharedHeap.hpp"
aoqi@0 44 #include "memory/space.hpp"
aoqi@0 45 #include "oops/oop.inline.hpp"
aoqi@0 46 #include "oops/oop.inline2.hpp"
aoqi@0 47 #include "runtime/biasedLocking.hpp"
aoqi@0 48 #include "runtime/fprofiler.hpp"
aoqi@0 49 #include "runtime/handles.hpp"
aoqi@0 50 #include "runtime/handles.inline.hpp"
aoqi@0 51 #include "runtime/java.hpp"
aoqi@0 52 #include "runtime/vmThread.hpp"
mgerdin@7975 53 #include "services/management.hpp"
aoqi@0 54 #include "services/memoryService.hpp"
aoqi@0 55 #include "utilities/vmError.hpp"
aoqi@0 56 #include "utilities/workgroup.hpp"
aoqi@0 57 #include "utilities/macros.hpp"
aoqi@0 58 #if INCLUDE_ALL_GCS
aoqi@0 59 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
aoqi@0 60 #include "gc_implementation/concurrentMarkSweep/vmCMSOperations.hpp"
aoqi@0 61 #endif // INCLUDE_ALL_GCS
aoqi@0 62
aoqi@0 63 GenCollectedHeap* GenCollectedHeap::_gch;
aoqi@0 64 NOT_PRODUCT(size_t GenCollectedHeap::_skip_header_HeapWords = 0;)
aoqi@0 65
stefank@6992 66 // The set of potentially parallel tasks in root scanning.
stefank@6992 67 enum GCH_strong_roots_tasks {
mgerdin@7975 68 GCH_PS_Universe_oops_do,
mgerdin@7975 69 GCH_PS_JNIHandles_oops_do,
mgerdin@7975 70 GCH_PS_ObjectSynchronizer_oops_do,
mgerdin@7975 71 GCH_PS_FlatProfiler_oops_do,
mgerdin@7975 72 GCH_PS_Management_oops_do,
mgerdin@7975 73 GCH_PS_SystemDictionary_oops_do,
mgerdin@7975 74 GCH_PS_ClassLoaderDataGraph_oops_do,
mgerdin@7975 75 GCH_PS_jvmti_oops_do,
mgerdin@7975 76 GCH_PS_CodeCache_oops_do,
aoqi@0 77 GCH_PS_younger_gens,
aoqi@0 78 // Leave this one last.
aoqi@0 79 GCH_PS_NumElements
aoqi@0 80 };
aoqi@0 81
aoqi@0 82 GenCollectedHeap::GenCollectedHeap(GenCollectorPolicy *policy) :
aoqi@0 83 SharedHeap(policy),
aoqi@0 84 _gen_policy(policy),
mgerdin@7975 85 _process_strong_tasks(new SubTasksDone(GCH_PS_NumElements)),
aoqi@0 86 _full_collections_completed(0)
aoqi@0 87 {
aoqi@0 88 assert(policy != NULL, "Sanity check");
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 jint GenCollectedHeap::initialize() {
aoqi@0 92 CollectedHeap::pre_initialize();
aoqi@0 93
aoqi@0 94 int i;
aoqi@0 95 _n_gens = gen_policy()->number_of_generations();
aoqi@0 96
aoqi@0 97 // While there are no constraints in the GC code that HeapWordSize
aoqi@0 98 // be any particular value, there are multiple other areas in the
aoqi@0 99 // system which believe this to be true (e.g. oop->object_size in some
aoqi@0 100 // cases incorrectly returns the size in wordSize units rather than
aoqi@0 101 // HeapWordSize).
aoqi@0 102 guarantee(HeapWordSize == wordSize, "HeapWordSize must equal wordSize");
aoqi@0 103
aoqi@0 104 // The heap must be at least as aligned as generations.
aoqi@0 105 size_t gen_alignment = Generation::GenGrain;
aoqi@0 106
aoqi@0 107 _gen_specs = gen_policy()->generations();
aoqi@0 108
aoqi@0 109 // Make sure the sizes are all aligned.
aoqi@0 110 for (i = 0; i < _n_gens; i++) {
aoqi@0 111 _gen_specs[i]->align(gen_alignment);
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 // Allocate space for the heap.
aoqi@0 115
aoqi@0 116 char* heap_address;
aoqi@0 117 size_t total_reserved = 0;
aoqi@0 118 int n_covered_regions = 0;
aoqi@0 119 ReservedSpace heap_rs;
aoqi@0 120
aoqi@0 121 size_t heap_alignment = collector_policy()->heap_alignment();
aoqi@0 122
aoqi@0 123 heap_address = allocate(heap_alignment, &total_reserved,
aoqi@0 124 &n_covered_regions, &heap_rs);
aoqi@0 125
aoqi@0 126 if (!heap_rs.is_reserved()) {
aoqi@0 127 vm_shutdown_during_initialization(
aoqi@0 128 "Could not reserve enough space for object heap");
aoqi@0 129 return JNI_ENOMEM;
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 _reserved = MemRegion((HeapWord*)heap_rs.base(),
aoqi@0 133 (HeapWord*)(heap_rs.base() + heap_rs.size()));
aoqi@0 134
aoqi@0 135 // It is important to do this in a way such that concurrent readers can't
aoqi@0 136 // temporarily think somethings in the heap. (Seen this happen in asserts.)
aoqi@0 137 _reserved.set_word_size(0);
aoqi@0 138 _reserved.set_start((HeapWord*)heap_rs.base());
aoqi@0 139 size_t actual_heap_size = heap_rs.size();
aoqi@0 140 _reserved.set_end((HeapWord*)(heap_rs.base() + actual_heap_size));
aoqi@0 141
aoqi@0 142 _rem_set = collector_policy()->create_rem_set(_reserved, n_covered_regions);
aoqi@0 143 set_barrier_set(rem_set()->bs());
aoqi@0 144
aoqi@0 145 _gch = this;
aoqi@0 146
aoqi@0 147 for (i = 0; i < _n_gens; i++) {
aoqi@0 148 ReservedSpace this_rs = heap_rs.first_part(_gen_specs[i]->max_size(), false, false);
aoqi@0 149 _gens[i] = _gen_specs[i]->init(this_rs, i, rem_set());
aoqi@0 150 heap_rs = heap_rs.last_part(_gen_specs[i]->max_size());
aoqi@0 151 }
aoqi@0 152 clear_incremental_collection_failed();
aoqi@0 153
aoqi@0 154 #if INCLUDE_ALL_GCS
aoqi@0 155 // If we are running CMS, create the collector responsible
aoqi@0 156 // for collecting the CMS generations.
aoqi@0 157 if (collector_policy()->is_concurrent_mark_sweep_policy()) {
aoqi@0 158 bool success = create_cms_collector();
aoqi@0 159 if (!success) return JNI_ENOMEM;
aoqi@0 160 }
aoqi@0 161 #endif // INCLUDE_ALL_GCS
aoqi@0 162
aoqi@0 163 return JNI_OK;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166
aoqi@0 167 char* GenCollectedHeap::allocate(size_t alignment,
aoqi@0 168 size_t* _total_reserved,
aoqi@0 169 int* _n_covered_regions,
aoqi@0 170 ReservedSpace* heap_rs){
aoqi@0 171 const char overflow_msg[] = "The size of the object heap + VM data exceeds "
aoqi@0 172 "the maximum representable size";
aoqi@0 173
aoqi@0 174 // Now figure out the total size.
aoqi@0 175 size_t total_reserved = 0;
aoqi@0 176 int n_covered_regions = 0;
aoqi@0 177 const size_t pageSize = UseLargePages ?
aoqi@0 178 os::large_page_size() : os::vm_page_size();
aoqi@0 179
aoqi@0 180 assert(alignment % pageSize == 0, "Must be");
aoqi@0 181
aoqi@0 182 for (int i = 0; i < _n_gens; i++) {
aoqi@0 183 total_reserved += _gen_specs[i]->max_size();
aoqi@0 184 if (total_reserved < _gen_specs[i]->max_size()) {
aoqi@0 185 vm_exit_during_initialization(overflow_msg);
aoqi@0 186 }
aoqi@0 187 n_covered_regions += _gen_specs[i]->n_covered_regions();
aoqi@0 188 }
aoqi@0 189 assert(total_reserved % alignment == 0,
aoqi@0 190 err_msg("Gen size; total_reserved=" SIZE_FORMAT ", alignment="
aoqi@0 191 SIZE_FORMAT, total_reserved, alignment));
aoqi@0 192
aoqi@0 193 // Needed until the cardtable is fixed to have the right number
aoqi@0 194 // of covered regions.
aoqi@0 195 n_covered_regions += 2;
aoqi@0 196
aoqi@0 197 *_total_reserved = total_reserved;
aoqi@0 198 *_n_covered_regions = n_covered_regions;
aoqi@0 199
aoqi@0 200 *heap_rs = Universe::reserve_heap(total_reserved, alignment);
aoqi@0 201 return heap_rs->base();
aoqi@0 202 }
aoqi@0 203
aoqi@0 204
aoqi@0 205 void GenCollectedHeap::post_initialize() {
aoqi@0 206 SharedHeap::post_initialize();
aoqi@0 207 TwoGenerationCollectorPolicy *policy =
aoqi@0 208 (TwoGenerationCollectorPolicy *)collector_policy();
aoqi@0 209 guarantee(policy->is_two_generation_policy(), "Illegal policy type");
aoqi@0 210 DefNewGeneration* def_new_gen = (DefNewGeneration*) get_gen(0);
aoqi@0 211 assert(def_new_gen->kind() == Generation::DefNew ||
aoqi@0 212 def_new_gen->kind() == Generation::ParNew ||
aoqi@0 213 def_new_gen->kind() == Generation::ASParNew,
aoqi@0 214 "Wrong generation kind");
aoqi@0 215
aoqi@0 216 Generation* old_gen = get_gen(1);
aoqi@0 217 assert(old_gen->kind() == Generation::ConcurrentMarkSweep ||
aoqi@0 218 old_gen->kind() == Generation::ASConcurrentMarkSweep ||
aoqi@0 219 old_gen->kind() == Generation::MarkSweepCompact,
aoqi@0 220 "Wrong generation kind");
aoqi@0 221
aoqi@0 222 policy->initialize_size_policy(def_new_gen->eden()->capacity(),
aoqi@0 223 old_gen->capacity(),
aoqi@0 224 def_new_gen->from()->capacity());
aoqi@0 225 policy->initialize_gc_policy_counters();
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 void GenCollectedHeap::ref_processing_init() {
aoqi@0 229 SharedHeap::ref_processing_init();
aoqi@0 230 for (int i = 0; i < _n_gens; i++) {
aoqi@0 231 _gens[i]->ref_processor_init();
aoqi@0 232 }
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 size_t GenCollectedHeap::capacity() const {
aoqi@0 236 size_t res = 0;
aoqi@0 237 for (int i = 0; i < _n_gens; i++) {
aoqi@0 238 res += _gens[i]->capacity();
aoqi@0 239 }
aoqi@0 240 return res;
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 size_t GenCollectedHeap::used() const {
aoqi@0 244 size_t res = 0;
aoqi@0 245 for (int i = 0; i < _n_gens; i++) {
aoqi@0 246 res += _gens[i]->used();
aoqi@0 247 }
aoqi@0 248 return res;
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 // Save the "used_region" for generations level and lower.
aoqi@0 252 void GenCollectedHeap::save_used_regions(int level) {
aoqi@0 253 assert(level < _n_gens, "Illegal level parameter");
aoqi@0 254 for (int i = level; i >= 0; i--) {
aoqi@0 255 _gens[i]->save_used_region();
aoqi@0 256 }
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 size_t GenCollectedHeap::max_capacity() const {
aoqi@0 260 size_t res = 0;
aoqi@0 261 for (int i = 0; i < _n_gens; i++) {
aoqi@0 262 res += _gens[i]->max_capacity();
aoqi@0 263 }
aoqi@0 264 return res;
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 // Update the _full_collections_completed counter
aoqi@0 268 // at the end of a stop-world full GC.
aoqi@0 269 unsigned int GenCollectedHeap::update_full_collections_completed() {
aoqi@0 270 MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 271 assert(_full_collections_completed <= _total_full_collections,
aoqi@0 272 "Can't complete more collections than were started");
aoqi@0 273 _full_collections_completed = _total_full_collections;
aoqi@0 274 ml.notify_all();
aoqi@0 275 return _full_collections_completed;
aoqi@0 276 }
aoqi@0 277
aoqi@0 278 // Update the _full_collections_completed counter, as appropriate,
aoqi@0 279 // at the end of a concurrent GC cycle. Note the conditional update
aoqi@0 280 // below to allow this method to be called by a concurrent collector
aoqi@0 281 // without synchronizing in any manner with the VM thread (which
aoqi@0 282 // may already have initiated a STW full collection "concurrently").
aoqi@0 283 unsigned int GenCollectedHeap::update_full_collections_completed(unsigned int count) {
aoqi@0 284 MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 285 assert((_full_collections_completed <= _total_full_collections) &&
aoqi@0 286 (count <= _total_full_collections),
aoqi@0 287 "Can't complete more collections than were started");
aoqi@0 288 if (count > _full_collections_completed) {
aoqi@0 289 _full_collections_completed = count;
aoqi@0 290 ml.notify_all();
aoqi@0 291 }
aoqi@0 292 return _full_collections_completed;
aoqi@0 293 }
aoqi@0 294
aoqi@0 295
aoqi@0 296 #ifndef PRODUCT
aoqi@0 297 // Override of memory state checking method in CollectedHeap:
aoqi@0 298 // Some collectors (CMS for example) can't have badHeapWordVal written
aoqi@0 299 // in the first two words of an object. (For instance , in the case of
aoqi@0 300 // CMS these words hold state used to synchronize between certain
aoqi@0 301 // (concurrent) GC steps and direct allocating mutators.)
aoqi@0 302 // The skip_header_HeapWords() method below, allows us to skip
aoqi@0 303 // over the requisite number of HeapWord's. Note that (for
aoqi@0 304 // generational collectors) this means that those many words are
aoqi@0 305 // skipped in each object, irrespective of the generation in which
aoqi@0 306 // that object lives. The resultant loss of precision seems to be
aoqi@0 307 // harmless and the pain of avoiding that imprecision appears somewhat
aoqi@0 308 // higher than we are prepared to pay for such rudimentary debugging
aoqi@0 309 // support.
aoqi@0 310 void GenCollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr,
aoqi@0 311 size_t size) {
aoqi@0 312 if (CheckMemoryInitialization && ZapUnusedHeapArea) {
aoqi@0 313 // We are asked to check a size in HeapWords,
aoqi@0 314 // but the memory is mangled in juint words.
aoqi@0 315 juint* start = (juint*) (addr + skip_header_HeapWords());
aoqi@0 316 juint* end = (juint*) (addr + size);
aoqi@0 317 for (juint* slot = start; slot < end; slot += 1) {
aoqi@0 318 assert(*slot == badHeapWordVal,
aoqi@0 319 "Found non badHeapWordValue in pre-allocation check");
aoqi@0 320 }
aoqi@0 321 }
aoqi@0 322 }
aoqi@0 323 #endif
aoqi@0 324
aoqi@0 325 HeapWord* GenCollectedHeap::attempt_allocation(size_t size,
aoqi@0 326 bool is_tlab,
aoqi@0 327 bool first_only) {
aoqi@0 328 HeapWord* res;
aoqi@0 329 for (int i = 0; i < _n_gens; i++) {
aoqi@0 330 if (_gens[i]->should_allocate(size, is_tlab)) {
aoqi@0 331 res = _gens[i]->allocate(size, is_tlab);
aoqi@0 332 if (res != NULL) return res;
aoqi@0 333 else if (first_only) break;
aoqi@0 334 }
aoqi@0 335 }
aoqi@0 336 // Otherwise...
aoqi@0 337 return NULL;
aoqi@0 338 }
aoqi@0 339
aoqi@0 340 HeapWord* GenCollectedHeap::mem_allocate(size_t size,
aoqi@0 341 bool* gc_overhead_limit_was_exceeded) {
aoqi@0 342 return collector_policy()->mem_allocate_work(size,
aoqi@0 343 false /* is_tlab */,
aoqi@0 344 gc_overhead_limit_was_exceeded);
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 bool GenCollectedHeap::must_clear_all_soft_refs() {
aoqi@0 348 return _gc_cause == GCCause::_last_ditch_collection;
aoqi@0 349 }
aoqi@0 350
aoqi@0 351 bool GenCollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) {
aoqi@0 352 return UseConcMarkSweepGC &&
aoqi@0 353 ((cause == GCCause::_gc_locker && GCLockerInvokesConcurrent) ||
aoqi@0 354 (cause == GCCause::_java_lang_system_gc && ExplicitGCInvokesConcurrent));
aoqi@0 355 }
aoqi@0 356
aoqi@0 357 void GenCollectedHeap::do_collection(bool full,
aoqi@0 358 bool clear_all_soft_refs,
aoqi@0 359 size_t size,
aoqi@0 360 bool is_tlab,
aoqi@0 361 int max_level) {
aoqi@0 362 bool prepared_for_verification = false;
aoqi@0 363 ResourceMark rm;
aoqi@0 364 DEBUG_ONLY(Thread* my_thread = Thread::current();)
aoqi@0 365
aoqi@0 366 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
aoqi@0 367 assert(my_thread->is_VM_thread() ||
aoqi@0 368 my_thread->is_ConcurrentGC_thread(),
aoqi@0 369 "incorrect thread type capability");
aoqi@0 370 assert(Heap_lock->is_locked(),
aoqi@0 371 "the requesting thread should have the Heap_lock");
aoqi@0 372 guarantee(!is_gc_active(), "collection is not reentrant");
aoqi@0 373 assert(max_level < n_gens(), "sanity check");
aoqi@0 374
aoqi@0 375 if (GC_locker::check_active_before_gc()) {
aoqi@0 376 return; // GC is disabled (e.g. JNI GetXXXCritical operation)
aoqi@0 377 }
aoqi@0 378
aoqi@0 379 const bool do_clear_all_soft_refs = clear_all_soft_refs ||
aoqi@0 380 collector_policy()->should_clear_all_soft_refs();
aoqi@0 381
aoqi@0 382 ClearedAllSoftRefs casr(do_clear_all_soft_refs, collector_policy());
aoqi@0 383
aoqi@0 384 const size_t metadata_prev_used = MetaspaceAux::used_bytes();
aoqi@0 385
aoqi@0 386 print_heap_before_gc();
aoqi@0 387
aoqi@0 388 {
aoqi@0 389 FlagSetting fl(_is_gc_active, true);
aoqi@0 390
aoqi@0 391 bool complete = full && (max_level == (n_gens()-1));
aoqi@0 392 const char* gc_cause_prefix = complete ? "Full GC" : "GC";
aoqi@0 393 TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
brutisso@6904 394 // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later
brutisso@6904 395 // so we can assume here that the next GC id is what we want.
brutisso@6904 396 GCTraceTime t(GCCauseString(gc_cause_prefix, gc_cause()), PrintGCDetails, false, NULL, GCId::peek());
aoqi@0 397
aoqi@0 398 gc_prologue(complete);
aoqi@0 399 increment_total_collections(complete);
aoqi@0 400
aoqi@0 401 size_t gch_prev_used = used();
aoqi@0 402
aoqi@0 403 int starting_level = 0;
aoqi@0 404 if (full) {
aoqi@0 405 // Search for the oldest generation which will collect all younger
aoqi@0 406 // generations, and start collection loop there.
aoqi@0 407 for (int i = max_level; i >= 0; i--) {
aoqi@0 408 if (_gens[i]->full_collects_younger_generations()) {
aoqi@0 409 starting_level = i;
aoqi@0 410 break;
aoqi@0 411 }
aoqi@0 412 }
aoqi@0 413 }
aoqi@0 414
aoqi@0 415 bool must_restore_marks_for_biased_locking = false;
aoqi@0 416
aoqi@0 417 int max_level_collected = starting_level;
aoqi@0 418 for (int i = starting_level; i <= max_level; i++) {
aoqi@0 419 if (_gens[i]->should_collect(full, size, is_tlab)) {
aoqi@0 420 if (i == n_gens() - 1) { // a major collection is to happen
aoqi@0 421 if (!complete) {
aoqi@0 422 // The full_collections increment was missed above.
aoqi@0 423 increment_total_full_collections();
aoqi@0 424 }
aoqi@0 425 pre_full_gc_dump(NULL); // do any pre full gc dumps
aoqi@0 426 }
aoqi@0 427 // Timer for individual generations. Last argument is false: no CR
aoqi@0 428 // FIXME: We should try to start the timing earlier to cover more of the GC pause
brutisso@6904 429 // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later
brutisso@6904 430 // so we can assume here that the next GC id is what we want.
brutisso@6904 431 GCTraceTime t1(_gens[i]->short_name(), PrintGCDetails, false, NULL, GCId::peek());
aoqi@0 432 TraceCollectorStats tcs(_gens[i]->counters());
aoqi@0 433 TraceMemoryManagerStats tmms(_gens[i]->kind(),gc_cause());
aoqi@0 434
aoqi@0 435 size_t prev_used = _gens[i]->used();
aoqi@0 436 _gens[i]->stat_record()->invocations++;
aoqi@0 437 _gens[i]->stat_record()->accumulated_time.start();
aoqi@0 438
aoqi@0 439 // Must be done anew before each collection because
aoqi@0 440 // a previous collection will do mangling and will
aoqi@0 441 // change top of some spaces.
aoqi@0 442 record_gen_tops_before_GC();
aoqi@0 443
aoqi@0 444 if (PrintGC && Verbose) {
aoqi@0 445 gclog_or_tty->print("level=%d invoke=%d size=" SIZE_FORMAT,
aoqi@0 446 i,
aoqi@0 447 _gens[i]->stat_record()->invocations,
aoqi@0 448 size*HeapWordSize);
aoqi@0 449 }
aoqi@0 450
aoqi@0 451 if (VerifyBeforeGC && i >= VerifyGCLevel &&
aoqi@0 452 total_collections() >= VerifyGCStartAt) {
aoqi@0 453 HandleMark hm; // Discard invalid handles created during verification
aoqi@0 454 if (!prepared_for_verification) {
aoqi@0 455 prepare_for_verify();
aoqi@0 456 prepared_for_verification = true;
aoqi@0 457 }
aoqi@0 458 Universe::verify(" VerifyBeforeGC:");
aoqi@0 459 }
aoqi@0 460 COMPILER2_PRESENT(DerivedPointerTable::clear());
aoqi@0 461
aoqi@0 462 if (!must_restore_marks_for_biased_locking &&
aoqi@0 463 _gens[i]->performs_in_place_marking()) {
aoqi@0 464 // We perform this mark word preservation work lazily
aoqi@0 465 // because it's only at this point that we know whether we
aoqi@0 466 // absolutely have to do it; we want to avoid doing it for
aoqi@0 467 // scavenge-only collections where it's unnecessary
aoqi@0 468 must_restore_marks_for_biased_locking = true;
aoqi@0 469 BiasedLocking::preserve_marks();
aoqi@0 470 }
aoqi@0 471
aoqi@0 472 // Do collection work
aoqi@0 473 {
aoqi@0 474 // Note on ref discovery: For what appear to be historical reasons,
aoqi@0 475 // GCH enables and disabled (by enqueing) refs discovery.
aoqi@0 476 // In the future this should be moved into the generation's
aoqi@0 477 // collect method so that ref discovery and enqueueing concerns
aoqi@0 478 // are local to a generation. The collect method could return
aoqi@0 479 // an appropriate indication in the case that notification on
aoqi@0 480 // the ref lock was needed. This will make the treatment of
aoqi@0 481 // weak refs more uniform (and indeed remove such concerns
aoqi@0 482 // from GCH). XXX
aoqi@0 483
aoqi@0 484 HandleMark hm; // Discard invalid handles created during gc
aoqi@0 485 save_marks(); // save marks for all gens
aoqi@0 486 // We want to discover references, but not process them yet.
aoqi@0 487 // This mode is disabled in process_discovered_references if the
aoqi@0 488 // generation does some collection work, or in
aoqi@0 489 // enqueue_discovered_references if the generation returns
aoqi@0 490 // without doing any work.
aoqi@0 491 ReferenceProcessor* rp = _gens[i]->ref_processor();
aoqi@0 492 // If the discovery of ("weak") refs in this generation is
aoqi@0 493 // atomic wrt other collectors in this configuration, we
aoqi@0 494 // are guaranteed to have empty discovered ref lists.
aoqi@0 495 if (rp->discovery_is_atomic()) {
aoqi@0 496 rp->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/);
aoqi@0 497 rp->setup_policy(do_clear_all_soft_refs);
aoqi@0 498 } else {
aoqi@0 499 // collect() below will enable discovery as appropriate
aoqi@0 500 }
aoqi@0 501 _gens[i]->collect(full, do_clear_all_soft_refs, size, is_tlab);
aoqi@0 502 if (!rp->enqueuing_is_done()) {
aoqi@0 503 rp->enqueue_discovered_references();
aoqi@0 504 } else {
aoqi@0 505 rp->set_enqueuing_is_done(false);
aoqi@0 506 }
aoqi@0 507 rp->verify_no_references_recorded();
aoqi@0 508 }
aoqi@0 509 max_level_collected = i;
aoqi@0 510
aoqi@0 511 // Determine if allocation request was met.
aoqi@0 512 if (size > 0) {
aoqi@0 513 if (!is_tlab || _gens[i]->supports_tlab_allocation()) {
aoqi@0 514 if (size*HeapWordSize <= _gens[i]->unsafe_max_alloc_nogc()) {
aoqi@0 515 size = 0;
aoqi@0 516 }
aoqi@0 517 }
aoqi@0 518 }
aoqi@0 519
aoqi@0 520 COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
aoqi@0 521
aoqi@0 522 _gens[i]->stat_record()->accumulated_time.stop();
aoqi@0 523
aoqi@0 524 update_gc_stats(i, full);
aoqi@0 525
aoqi@0 526 if (VerifyAfterGC && i >= VerifyGCLevel &&
aoqi@0 527 total_collections() >= VerifyGCStartAt) {
aoqi@0 528 HandleMark hm; // Discard invalid handles created during verification
aoqi@0 529 Universe::verify(" VerifyAfterGC:");
aoqi@0 530 }
aoqi@0 531
aoqi@0 532 if (PrintGCDetails) {
aoqi@0 533 gclog_or_tty->print(":");
aoqi@0 534 _gens[i]->print_heap_change(prev_used);
aoqi@0 535 }
aoqi@0 536 }
aoqi@0 537 }
aoqi@0 538
aoqi@0 539 // Update "complete" boolean wrt what actually transpired --
aoqi@0 540 // for instance, a promotion failure could have led to
aoqi@0 541 // a whole heap collection.
aoqi@0 542 complete = complete || (max_level_collected == n_gens() - 1);
aoqi@0 543
aoqi@0 544 if (complete) { // We did a "major" collection
aoqi@0 545 // FIXME: See comment at pre_full_gc_dump call
aoqi@0 546 post_full_gc_dump(NULL); // do any post full gc dumps
aoqi@0 547 }
aoqi@0 548
aoqi@0 549 if (PrintGCDetails) {
aoqi@0 550 print_heap_change(gch_prev_used);
aoqi@0 551
aoqi@0 552 // Print metaspace info for full GC with PrintGCDetails flag.
aoqi@0 553 if (complete) {
aoqi@0 554 MetaspaceAux::print_metaspace_change(metadata_prev_used);
aoqi@0 555 }
aoqi@0 556 }
aoqi@0 557
aoqi@0 558 for (int j = max_level_collected; j >= 0; j -= 1) {
aoqi@0 559 // Adjust generation sizes.
aoqi@0 560 _gens[j]->compute_new_size();
aoqi@0 561 }
aoqi@0 562
aoqi@0 563 if (complete) {
aoqi@0 564 // Delete metaspaces for unloaded class loaders and clean up loader_data graph
aoqi@0 565 ClassLoaderDataGraph::purge();
aoqi@0 566 MetaspaceAux::verify_metrics();
aoqi@0 567 // Resize the metaspace capacity after full collections
aoqi@0 568 MetaspaceGC::compute_new_size();
aoqi@0 569 update_full_collections_completed();
aoqi@0 570 }
aoqi@0 571
aoqi@0 572 // Track memory usage and detect low memory after GC finishes
aoqi@0 573 MemoryService::track_memory_usage();
aoqi@0 574
aoqi@0 575 gc_epilogue(complete);
aoqi@0 576
aoqi@0 577 if (must_restore_marks_for_biased_locking) {
aoqi@0 578 BiasedLocking::restore_marks();
aoqi@0 579 }
aoqi@0 580 }
aoqi@0 581
aoqi@0 582 AdaptiveSizePolicy* sp = gen_policy()->size_policy();
aoqi@0 583 AdaptiveSizePolicyOutput(sp, total_collections());
aoqi@0 584
aoqi@0 585 print_heap_after_gc();
aoqi@0 586
aoqi@0 587 #ifdef TRACESPINNING
aoqi@0 588 ParallelTaskTerminator::print_termination_counts();
aoqi@0 589 #endif
aoqi@0 590 }
aoqi@0 591
aoqi@0 592 HeapWord* GenCollectedHeap::satisfy_failed_allocation(size_t size, bool is_tlab) {
aoqi@0 593 return collector_policy()->satisfy_failed_allocation(size, is_tlab);
aoqi@0 594 }
aoqi@0 595
aoqi@0 596 void GenCollectedHeap::set_par_threads(uint t) {
aoqi@0 597 SharedHeap::set_par_threads(t);
mgerdin@7975 598 set_n_termination(t);
aoqi@0 599 }
aoqi@0 600
mgerdin@7975 601 void GenCollectedHeap::set_n_termination(uint t) {
mgerdin@7975 602 _process_strong_tasks->set_n_threads(t);
mgerdin@7975 603 }
mgerdin@7975 604
mgerdin@7975 605 #ifdef ASSERT
mgerdin@7975 606 class AssertNonScavengableClosure: public OopClosure {
mgerdin@7975 607 public:
mgerdin@7975 608 virtual void do_oop(oop* p) {
mgerdin@7975 609 assert(!Universe::heap()->is_in_partial_collection(*p),
mgerdin@7975 610 "Referent should not be scavengable."); }
mgerdin@7975 611 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
mgerdin@7975 612 };
mgerdin@7975 613 static AssertNonScavengableClosure assert_is_non_scavengable_closure;
mgerdin@7975 614 #endif
mgerdin@7975 615
mgerdin@7975 616 void GenCollectedHeap::process_roots(bool activate_scope,
mgerdin@7975 617 ScanningOption so,
mgerdin@7975 618 OopClosure* strong_roots,
mgerdin@7975 619 OopClosure* weak_roots,
mgerdin@7975 620 CLDClosure* strong_cld_closure,
mgerdin@7975 621 CLDClosure* weak_cld_closure,
mgerdin@7975 622 CodeBlobClosure* code_roots) {
mgerdin@7975 623 StrongRootsScope srs(this, activate_scope);
aoqi@0 624
stefank@6992 625 // General roots.
mgerdin@7975 626 assert(_strong_roots_parity != 0, "must have called prologue code");
mgerdin@7975 627 assert(code_roots != NULL, "code root closure should always be set");
mgerdin@7975 628 // _n_termination for _process_strong_tasks should be set up stream
mgerdin@7975 629 // in a method not running in a GC worker. Otherwise the GC worker
mgerdin@7975 630 // could be trying to change the termination condition while the task
mgerdin@7975 631 // is executing in another GC worker.
mgerdin@7975 632
mgerdin@7975 633 if (!_process_strong_tasks->is_task_claimed(GCH_PS_ClassLoaderDataGraph_oops_do)) {
mgerdin@7975 634 ClassLoaderDataGraph::roots_cld_do(strong_cld_closure, weak_cld_closure);
mgerdin@7975 635 }
mgerdin@7975 636
mgerdin@7975 637 // Some CLDs contained in the thread frames should be considered strong.
mgerdin@7975 638 // Don't process them if they will be processed during the ClassLoaderDataGraph phase.
mgerdin@7975 639 CLDClosure* roots_from_clds_p = (strong_cld_closure != weak_cld_closure) ? strong_cld_closure : NULL;
mgerdin@7975 640 // Only process code roots from thread stacks if we aren't visiting the entire CodeCache anyway
mgerdin@7975 641 CodeBlobClosure* roots_from_code_p = (so & SO_AllCodeCache) ? NULL : code_roots;
mgerdin@7975 642
mgerdin@7975 643 Threads::possibly_parallel_oops_do(strong_roots, roots_from_clds_p, roots_from_code_p);
mgerdin@7975 644
mgerdin@7975 645 if (!_process_strong_tasks->is_task_claimed(GCH_PS_Universe_oops_do)) {
mgerdin@7975 646 Universe::oops_do(strong_roots);
mgerdin@7975 647 }
mgerdin@7975 648 // Global (strong) JNI handles
mgerdin@7975 649 if (!_process_strong_tasks->is_task_claimed(GCH_PS_JNIHandles_oops_do)) {
mgerdin@7975 650 JNIHandles::oops_do(strong_roots);
mgerdin@7975 651 }
mgerdin@7975 652
mgerdin@7975 653 if (!_process_strong_tasks->is_task_claimed(GCH_PS_ObjectSynchronizer_oops_do)) {
mgerdin@7975 654 ObjectSynchronizer::oops_do(strong_roots);
mgerdin@7975 655 }
mgerdin@7975 656 if (!_process_strong_tasks->is_task_claimed(GCH_PS_FlatProfiler_oops_do)) {
mgerdin@7975 657 FlatProfiler::oops_do(strong_roots);
mgerdin@7975 658 }
mgerdin@7975 659 if (!_process_strong_tasks->is_task_claimed(GCH_PS_Management_oops_do)) {
mgerdin@7975 660 Management::oops_do(strong_roots);
mgerdin@7975 661 }
mgerdin@7975 662 if (!_process_strong_tasks->is_task_claimed(GCH_PS_jvmti_oops_do)) {
mgerdin@7975 663 JvmtiExport::oops_do(strong_roots);
mgerdin@7975 664 }
mgerdin@7975 665
mgerdin@7975 666 if (!_process_strong_tasks->is_task_claimed(GCH_PS_SystemDictionary_oops_do)) {
mgerdin@7975 667 SystemDictionary::roots_oops_do(strong_roots, weak_roots);
mgerdin@7975 668 }
mgerdin@7975 669
mgerdin@7975 670 // All threads execute the following. A specific chunk of buckets
mgerdin@7975 671 // from the StringTable are the individual tasks.
mgerdin@7975 672 if (weak_roots != NULL) {
mgerdin@7975 673 if (CollectedHeap::use_parallel_gc_threads()) {
mgerdin@7975 674 StringTable::possibly_parallel_oops_do(weak_roots);
mgerdin@7975 675 } else {
mgerdin@7975 676 StringTable::oops_do(weak_roots);
mgerdin@7975 677 }
mgerdin@7975 678 }
mgerdin@7975 679
mgerdin@7975 680 if (!_process_strong_tasks->is_task_claimed(GCH_PS_CodeCache_oops_do)) {
mgerdin@7975 681 if (so & SO_ScavengeCodeCache) {
mgerdin@7975 682 assert(code_roots != NULL, "must supply closure for code cache");
mgerdin@7975 683
mgerdin@7975 684 // We only visit parts of the CodeCache when scavenging.
mgerdin@7975 685 CodeCache::scavenge_root_nmethods_do(code_roots);
mgerdin@7975 686 }
mgerdin@7975 687 if (so & SO_AllCodeCache) {
mgerdin@7975 688 assert(code_roots != NULL, "must supply closure for code cache");
mgerdin@7975 689
mgerdin@7975 690 // CMSCollector uses this to do intermediate-strength collections.
mgerdin@7975 691 // We scan the entire code cache, since CodeCache::do_unloading is not called.
mgerdin@7975 692 CodeCache::blobs_do(code_roots);
mgerdin@7975 693 }
mgerdin@7975 694 // Verify that the code cache contents are not subject to
mgerdin@7975 695 // movement by a scavenging collection.
mgerdin@7975 696 DEBUG_ONLY(CodeBlobToOopClosure assert_code_is_non_scavengable(&assert_is_non_scavengable_closure, !CodeBlobToOopClosure::FixRelocations));
mgerdin@7975 697 DEBUG_ONLY(CodeCache::asserted_non_scavengable_nmethods_do(&assert_code_is_non_scavengable));
mgerdin@7975 698 }
mgerdin@7975 699
mgerdin@7975 700 }
mgerdin@7975 701
mgerdin@7975 702 void GenCollectedHeap::gen_process_roots(int level,
mgerdin@7975 703 bool younger_gens_as_roots,
mgerdin@7975 704 bool activate_scope,
mgerdin@7975 705 ScanningOption so,
mgerdin@7975 706 bool only_strong_roots,
mgerdin@7975 707 OopsInGenClosure* not_older_gens,
mgerdin@7975 708 OopsInGenClosure* older_gens,
mgerdin@7975 709 CLDClosure* cld_closure) {
mgerdin@7975 710 const bool is_adjust_phase = !only_strong_roots && !younger_gens_as_roots;
mgerdin@7975 711
mgerdin@7975 712 bool is_moving_collection = false;
mgerdin@7975 713 if (level == 0 || is_adjust_phase) {
mgerdin@7975 714 // young collections are always moving
mgerdin@7975 715 is_moving_collection = true;
mgerdin@7975 716 }
mgerdin@7975 717
mgerdin@7975 718 MarkingCodeBlobClosure mark_code_closure(not_older_gens, is_moving_collection);
mgerdin@7975 719 OopsInGenClosure* weak_roots = only_strong_roots ? NULL : not_older_gens;
mgerdin@7975 720 CLDClosure* weak_cld_closure = only_strong_roots ? NULL : cld_closure;
mgerdin@7975 721
mgerdin@7975 722 process_roots(activate_scope, so,
mgerdin@7975 723 not_older_gens, weak_roots,
mgerdin@7975 724 cld_closure, weak_cld_closure,
mgerdin@7975 725 &mark_code_closure);
aoqi@0 726
aoqi@0 727 if (younger_gens_as_roots) {
mgerdin@7975 728 if (!_process_strong_tasks->is_task_claimed(GCH_PS_younger_gens)) {
aoqi@0 729 for (int i = 0; i < level; i++) {
aoqi@0 730 not_older_gens->set_generation(_gens[i]);
aoqi@0 731 _gens[i]->oop_iterate(not_older_gens);
aoqi@0 732 }
aoqi@0 733 not_older_gens->reset_generation();
aoqi@0 734 }
aoqi@0 735 }
aoqi@0 736 // When collection is parallel, all threads get to cooperate to do
aoqi@0 737 // older-gen scanning.
aoqi@0 738 for (int i = level+1; i < _n_gens; i++) {
aoqi@0 739 older_gens->set_generation(_gens[i]);
aoqi@0 740 rem_set()->younger_refs_iterate(_gens[i], older_gens);
aoqi@0 741 older_gens->reset_generation();
aoqi@0 742 }
aoqi@0 743
mgerdin@7975 744 _process_strong_tasks->all_tasks_completed();
aoqi@0 745 }
aoqi@0 746
stefank@6992 747
mgerdin@7975 748 class AlwaysTrueClosure: public BoolObjectClosure {
mgerdin@7975 749 public:
mgerdin@7975 750 bool do_object_b(oop p) { return true; }
mgerdin@7975 751 };
mgerdin@7975 752 static AlwaysTrueClosure always_true;
drchase@6680 753
stefank@6971 754 void GenCollectedHeap::gen_process_weak_roots(OopClosure* root_closure) {
mgerdin@7975 755 JNIHandles::weak_oops_do(&always_true, root_closure);
aoqi@0 756 for (int i = 0; i < _n_gens; i++) {
aoqi@0 757 _gens[i]->ref_processor()->weak_oops_do(root_closure);
aoqi@0 758 }
aoqi@0 759 }
aoqi@0 760
aoqi@0 761 #define GCH_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix) \
aoqi@0 762 void GenCollectedHeap:: \
aoqi@0 763 oop_since_save_marks_iterate(int level, \
aoqi@0 764 OopClosureType* cur, \
aoqi@0 765 OopClosureType* older) { \
aoqi@0 766 _gens[level]->oop_since_save_marks_iterate##nv_suffix(cur); \
aoqi@0 767 for (int i = level+1; i < n_gens(); i++) { \
aoqi@0 768 _gens[i]->oop_since_save_marks_iterate##nv_suffix(older); \
aoqi@0 769 } \
aoqi@0 770 }
aoqi@0 771
aoqi@0 772 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DEFN)
aoqi@0 773
aoqi@0 774 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DEFN
aoqi@0 775
aoqi@0 776 bool GenCollectedHeap::no_allocs_since_save_marks(int level) {
aoqi@0 777 for (int i = level; i < _n_gens; i++) {
aoqi@0 778 if (!_gens[i]->no_allocs_since_save_marks()) return false;
aoqi@0 779 }
aoqi@0 780 return true;
aoqi@0 781 }
aoqi@0 782
aoqi@0 783 bool GenCollectedHeap::supports_inline_contig_alloc() const {
aoqi@0 784 return _gens[0]->supports_inline_contig_alloc();
aoqi@0 785 }
aoqi@0 786
aoqi@0 787 HeapWord** GenCollectedHeap::top_addr() const {
aoqi@0 788 return _gens[0]->top_addr();
aoqi@0 789 }
aoqi@0 790
aoqi@0 791 HeapWord** GenCollectedHeap::end_addr() const {
aoqi@0 792 return _gens[0]->end_addr();
aoqi@0 793 }
aoqi@0 794
aoqi@0 795 // public collection interfaces
aoqi@0 796
aoqi@0 797 void GenCollectedHeap::collect(GCCause::Cause cause) {
aoqi@0 798 if (should_do_concurrent_full_gc(cause)) {
aoqi@0 799 #if INCLUDE_ALL_GCS
aoqi@0 800 // mostly concurrent full collection
aoqi@0 801 collect_mostly_concurrent(cause);
aoqi@0 802 #else // INCLUDE_ALL_GCS
aoqi@0 803 ShouldNotReachHere();
aoqi@0 804 #endif // INCLUDE_ALL_GCS
tschatzl@7071 805 } else if (cause == GCCause::_wb_young_gc) {
tschatzl@7071 806 // minor collection for WhiteBox API
tschatzl@7071 807 collect(cause, 0);
aoqi@0 808 } else {
aoqi@0 809 #ifdef ASSERT
tschatzl@7071 810 if (cause == GCCause::_scavenge_alot) {
tschatzl@7071 811 // minor collection only
tschatzl@7071 812 collect(cause, 0);
tschatzl@7071 813 } else {
tschatzl@7071 814 // Stop-the-world full collection
tschatzl@7071 815 collect(cause, n_gens() - 1);
tschatzl@7071 816 }
aoqi@0 817 #else
aoqi@0 818 // Stop-the-world full collection
aoqi@0 819 collect(cause, n_gens() - 1);
aoqi@0 820 #endif
aoqi@0 821 }
aoqi@0 822 }
aoqi@0 823
aoqi@0 824 void GenCollectedHeap::collect(GCCause::Cause cause, int max_level) {
aoqi@0 825 // The caller doesn't have the Heap_lock
aoqi@0 826 assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
aoqi@0 827 MutexLocker ml(Heap_lock);
aoqi@0 828 collect_locked(cause, max_level);
aoqi@0 829 }
aoqi@0 830
aoqi@0 831 void GenCollectedHeap::collect_locked(GCCause::Cause cause) {
aoqi@0 832 // The caller has the Heap_lock
aoqi@0 833 assert(Heap_lock->owned_by_self(), "this thread should own the Heap_lock");
aoqi@0 834 collect_locked(cause, n_gens() - 1);
aoqi@0 835 }
aoqi@0 836
aoqi@0 837 // this is the private collection interface
aoqi@0 838 // The Heap_lock is expected to be held on entry.
aoqi@0 839
aoqi@0 840 void GenCollectedHeap::collect_locked(GCCause::Cause cause, int max_level) {
aoqi@0 841 // Read the GC count while holding the Heap_lock
aoqi@0 842 unsigned int gc_count_before = total_collections();
aoqi@0 843 unsigned int full_gc_count_before = total_full_collections();
aoqi@0 844 {
aoqi@0 845 MutexUnlocker mu(Heap_lock); // give up heap lock, execute gets it back
aoqi@0 846 VM_GenCollectFull op(gc_count_before, full_gc_count_before,
aoqi@0 847 cause, max_level);
aoqi@0 848 VMThread::execute(&op);
aoqi@0 849 }
aoqi@0 850 }
aoqi@0 851
aoqi@0 852 #if INCLUDE_ALL_GCS
aoqi@0 853 bool GenCollectedHeap::create_cms_collector() {
aoqi@0 854
aoqi@0 855 assert(((_gens[1]->kind() == Generation::ConcurrentMarkSweep) ||
aoqi@0 856 (_gens[1]->kind() == Generation::ASConcurrentMarkSweep)),
aoqi@0 857 "Unexpected generation kinds");
aoqi@0 858 // Skip two header words in the block content verification
aoqi@0 859 NOT_PRODUCT(_skip_header_HeapWords = CMSCollector::skip_header_HeapWords();)
aoqi@0 860 CMSCollector* collector = new CMSCollector(
aoqi@0 861 (ConcurrentMarkSweepGeneration*)_gens[1],
aoqi@0 862 _rem_set->as_CardTableRS(),
aoqi@0 863 (ConcurrentMarkSweepPolicy*) collector_policy());
aoqi@0 864
aoqi@0 865 if (collector == NULL || !collector->completed_initialization()) {
aoqi@0 866 if (collector) {
aoqi@0 867 delete collector; // Be nice in embedded situation
aoqi@0 868 }
aoqi@0 869 vm_shutdown_during_initialization("Could not create CMS collector");
aoqi@0 870 return false;
aoqi@0 871 }
aoqi@0 872 return true; // success
aoqi@0 873 }
aoqi@0 874
aoqi@0 875 void GenCollectedHeap::collect_mostly_concurrent(GCCause::Cause cause) {
aoqi@0 876 assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock");
aoqi@0 877
aoqi@0 878 MutexLocker ml(Heap_lock);
aoqi@0 879 // Read the GC counts while holding the Heap_lock
aoqi@0 880 unsigned int full_gc_count_before = total_full_collections();
aoqi@0 881 unsigned int gc_count_before = total_collections();
aoqi@0 882 {
aoqi@0 883 MutexUnlocker mu(Heap_lock);
aoqi@0 884 VM_GenCollectFullConcurrent op(gc_count_before, full_gc_count_before, cause);
aoqi@0 885 VMThread::execute(&op);
aoqi@0 886 }
aoqi@0 887 }
aoqi@0 888 #endif // INCLUDE_ALL_GCS
aoqi@0 889
aoqi@0 890 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs) {
aoqi@0 891 do_full_collection(clear_all_soft_refs, _n_gens - 1);
aoqi@0 892 }
aoqi@0 893
aoqi@0 894 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs,
aoqi@0 895 int max_level) {
aoqi@0 896 int local_max_level;
aoqi@0 897 if (!incremental_collection_will_fail(false /* don't consult_young */) &&
aoqi@0 898 gc_cause() == GCCause::_gc_locker) {
aoqi@0 899 local_max_level = 0;
aoqi@0 900 } else {
aoqi@0 901 local_max_level = max_level;
aoqi@0 902 }
aoqi@0 903
aoqi@0 904 do_collection(true /* full */,
aoqi@0 905 clear_all_soft_refs /* clear_all_soft_refs */,
aoqi@0 906 0 /* size */,
aoqi@0 907 false /* is_tlab */,
aoqi@0 908 local_max_level /* max_level */);
aoqi@0 909 // Hack XXX FIX ME !!!
aoqi@0 910 // A scavenge may not have been attempted, or may have
aoqi@0 911 // been attempted and failed, because the old gen was too full
aoqi@0 912 if (local_max_level == 0 && gc_cause() == GCCause::_gc_locker &&
aoqi@0 913 incremental_collection_will_fail(false /* don't consult_young */)) {
aoqi@0 914 if (PrintGCDetails) {
aoqi@0 915 gclog_or_tty->print_cr("GC locker: Trying a full collection "
aoqi@0 916 "because scavenge failed");
aoqi@0 917 }
aoqi@0 918 // This time allow the old gen to be collected as well
aoqi@0 919 do_collection(true /* full */,
aoqi@0 920 clear_all_soft_refs /* clear_all_soft_refs */,
aoqi@0 921 0 /* size */,
aoqi@0 922 false /* is_tlab */,
aoqi@0 923 n_gens() - 1 /* max_level */);
aoqi@0 924 }
aoqi@0 925 }
aoqi@0 926
aoqi@0 927 bool GenCollectedHeap::is_in_young(oop p) {
aoqi@0 928 bool result = ((HeapWord*)p) < _gens[_n_gens - 1]->reserved().start();
aoqi@0 929 assert(result == _gens[0]->is_in_reserved(p),
aoqi@0 930 err_msg("incorrect test - result=%d, p=" PTR_FORMAT, result, p2i((void*)p)));
aoqi@0 931 return result;
aoqi@0 932 }
aoqi@0 933
aoqi@0 934 // Returns "TRUE" iff "p" points into the committed areas of the heap.
aoqi@0 935 bool GenCollectedHeap::is_in(const void* p) const {
aoqi@0 936 #ifndef ASSERT
aoqi@0 937 guarantee(VerifyBeforeGC ||
aoqi@0 938 VerifyDuringGC ||
aoqi@0 939 VerifyBeforeExit ||
aoqi@0 940 VerifyDuringStartup ||
aoqi@0 941 PrintAssembly ||
aoqi@0 942 tty->count() != 0 || // already printing
aoqi@0 943 VerifyAfterGC ||
aoqi@0 944 VMError::fatal_error_in_progress(), "too expensive");
aoqi@0 945
aoqi@0 946 #endif
aoqi@0 947 // This might be sped up with a cache of the last generation that
aoqi@0 948 // answered yes.
aoqi@0 949 for (int i = 0; i < _n_gens; i++) {
aoqi@0 950 if (_gens[i]->is_in(p)) return true;
aoqi@0 951 }
aoqi@0 952 // Otherwise...
aoqi@0 953 return false;
aoqi@0 954 }
aoqi@0 955
aoqi@0 956 #ifdef ASSERT
aoqi@0 957 // Don't implement this by using is_in_young(). This method is used
aoqi@0 958 // in some cases to check that is_in_young() is correct.
aoqi@0 959 bool GenCollectedHeap::is_in_partial_collection(const void* p) {
aoqi@0 960 assert(is_in_reserved(p) || p == NULL,
aoqi@0 961 "Does not work if address is non-null and outside of the heap");
aoqi@0 962 return p < _gens[_n_gens - 2]->reserved().end() && p != NULL;
aoqi@0 963 }
aoqi@0 964 #endif
aoqi@0 965
aoqi@0 966 void GenCollectedHeap::oop_iterate(ExtendedOopClosure* cl) {
aoqi@0 967 for (int i = 0; i < _n_gens; i++) {
aoqi@0 968 _gens[i]->oop_iterate(cl);
aoqi@0 969 }
aoqi@0 970 }
aoqi@0 971
aoqi@0 972 void GenCollectedHeap::object_iterate(ObjectClosure* cl) {
aoqi@0 973 for (int i = 0; i < _n_gens; i++) {
aoqi@0 974 _gens[i]->object_iterate(cl);
aoqi@0 975 }
aoqi@0 976 }
aoqi@0 977
aoqi@0 978 void GenCollectedHeap::safe_object_iterate(ObjectClosure* cl) {
aoqi@0 979 for (int i = 0; i < _n_gens; i++) {
aoqi@0 980 _gens[i]->safe_object_iterate(cl);
aoqi@0 981 }
aoqi@0 982 }
aoqi@0 983
aoqi@0 984 Space* GenCollectedHeap::space_containing(const void* addr) const {
aoqi@0 985 for (int i = 0; i < _n_gens; i++) {
aoqi@0 986 Space* res = _gens[i]->space_containing(addr);
aoqi@0 987 if (res != NULL) return res;
aoqi@0 988 }
aoqi@0 989 // Otherwise...
aoqi@0 990 assert(false, "Could not find containing space");
aoqi@0 991 return NULL;
aoqi@0 992 }
aoqi@0 993
aoqi@0 994
aoqi@0 995 HeapWord* GenCollectedHeap::block_start(const void* addr) const {
aoqi@0 996 assert(is_in_reserved(addr), "block_start of address outside of heap");
aoqi@0 997 for (int i = 0; i < _n_gens; i++) {
aoqi@0 998 if (_gens[i]->is_in_reserved(addr)) {
aoqi@0 999 assert(_gens[i]->is_in(addr),
aoqi@0 1000 "addr should be in allocated part of generation");
aoqi@0 1001 return _gens[i]->block_start(addr);
aoqi@0 1002 }
aoqi@0 1003 }
aoqi@0 1004 assert(false, "Some generation should contain the address");
aoqi@0 1005 return NULL;
aoqi@0 1006 }
aoqi@0 1007
aoqi@0 1008 size_t GenCollectedHeap::block_size(const HeapWord* addr) const {
aoqi@0 1009 assert(is_in_reserved(addr), "block_size of address outside of heap");
aoqi@0 1010 for (int i = 0; i < _n_gens; i++) {
aoqi@0 1011 if (_gens[i]->is_in_reserved(addr)) {
aoqi@0 1012 assert(_gens[i]->is_in(addr),
aoqi@0 1013 "addr should be in allocated part of generation");
aoqi@0 1014 return _gens[i]->block_size(addr);
aoqi@0 1015 }
aoqi@0 1016 }
aoqi@0 1017 assert(false, "Some generation should contain the address");
aoqi@0 1018 return 0;
aoqi@0 1019 }
aoqi@0 1020
aoqi@0 1021 bool GenCollectedHeap::block_is_obj(const HeapWord* addr) const {
aoqi@0 1022 assert(is_in_reserved(addr), "block_is_obj of address outside of heap");
aoqi@0 1023 assert(block_start(addr) == addr, "addr must be a block start");
aoqi@0 1024 for (int i = 0; i < _n_gens; i++) {
aoqi@0 1025 if (_gens[i]->is_in_reserved(addr)) {
aoqi@0 1026 return _gens[i]->block_is_obj(addr);
aoqi@0 1027 }
aoqi@0 1028 }
aoqi@0 1029 assert(false, "Some generation should contain the address");
aoqi@0 1030 return false;
aoqi@0 1031 }
aoqi@0 1032
aoqi@0 1033 bool GenCollectedHeap::supports_tlab_allocation() const {
aoqi@0 1034 for (int i = 0; i < _n_gens; i += 1) {
aoqi@0 1035 if (_gens[i]->supports_tlab_allocation()) {
aoqi@0 1036 return true;
aoqi@0 1037 }
aoqi@0 1038 }
aoqi@0 1039 return false;
aoqi@0 1040 }
aoqi@0 1041
aoqi@0 1042 size_t GenCollectedHeap::tlab_capacity(Thread* thr) const {
aoqi@0 1043 size_t result = 0;
aoqi@0 1044 for (int i = 0; i < _n_gens; i += 1) {
aoqi@0 1045 if (_gens[i]->supports_tlab_allocation()) {
aoqi@0 1046 result += _gens[i]->tlab_capacity();
aoqi@0 1047 }
aoqi@0 1048 }
aoqi@0 1049 return result;
aoqi@0 1050 }
aoqi@0 1051
aoqi@0 1052 size_t GenCollectedHeap::tlab_used(Thread* thr) const {
aoqi@0 1053 size_t result = 0;
aoqi@0 1054 for (int i = 0; i < _n_gens; i += 1) {
aoqi@0 1055 if (_gens[i]->supports_tlab_allocation()) {
aoqi@0 1056 result += _gens[i]->tlab_used();
aoqi@0 1057 }
aoqi@0 1058 }
aoqi@0 1059 return result;
aoqi@0 1060 }
aoqi@0 1061
aoqi@0 1062 size_t GenCollectedHeap::unsafe_max_tlab_alloc(Thread* thr) const {
aoqi@0 1063 size_t result = 0;
aoqi@0 1064 for (int i = 0; i < _n_gens; i += 1) {
aoqi@0 1065 if (_gens[i]->supports_tlab_allocation()) {
aoqi@0 1066 result += _gens[i]->unsafe_max_tlab_alloc();
aoqi@0 1067 }
aoqi@0 1068 }
aoqi@0 1069 return result;
aoqi@0 1070 }
aoqi@0 1071
aoqi@0 1072 HeapWord* GenCollectedHeap::allocate_new_tlab(size_t size) {
aoqi@0 1073 bool gc_overhead_limit_was_exceeded;
aoqi@0 1074 return collector_policy()->mem_allocate_work(size /* size */,
aoqi@0 1075 true /* is_tlab */,
aoqi@0 1076 &gc_overhead_limit_was_exceeded);
aoqi@0 1077 }
aoqi@0 1078
aoqi@0 1079 // Requires "*prev_ptr" to be non-NULL. Deletes and a block of minimal size
aoqi@0 1080 // from the list headed by "*prev_ptr".
aoqi@0 1081 static ScratchBlock *removeSmallestScratch(ScratchBlock **prev_ptr) {
aoqi@0 1082 bool first = true;
aoqi@0 1083 size_t min_size = 0; // "first" makes this conceptually infinite.
aoqi@0 1084 ScratchBlock **smallest_ptr, *smallest;
aoqi@0 1085 ScratchBlock *cur = *prev_ptr;
aoqi@0 1086 while (cur) {
aoqi@0 1087 assert(*prev_ptr == cur, "just checking");
aoqi@0 1088 if (first || cur->num_words < min_size) {
aoqi@0 1089 smallest_ptr = prev_ptr;
aoqi@0 1090 smallest = cur;
aoqi@0 1091 min_size = smallest->num_words;
aoqi@0 1092 first = false;
aoqi@0 1093 }
aoqi@0 1094 prev_ptr = &cur->next;
aoqi@0 1095 cur = cur->next;
aoqi@0 1096 }
aoqi@0 1097 smallest = *smallest_ptr;
aoqi@0 1098 *smallest_ptr = smallest->next;
aoqi@0 1099 return smallest;
aoqi@0 1100 }
aoqi@0 1101
aoqi@0 1102 // Sort the scratch block list headed by res into decreasing size order,
aoqi@0 1103 // and set "res" to the result.
aoqi@0 1104 static void sort_scratch_list(ScratchBlock*& list) {
aoqi@0 1105 ScratchBlock* sorted = NULL;
aoqi@0 1106 ScratchBlock* unsorted = list;
aoqi@0 1107 while (unsorted) {
aoqi@0 1108 ScratchBlock *smallest = removeSmallestScratch(&unsorted);
aoqi@0 1109 smallest->next = sorted;
aoqi@0 1110 sorted = smallest;
aoqi@0 1111 }
aoqi@0 1112 list = sorted;
aoqi@0 1113 }
aoqi@0 1114
aoqi@0 1115 ScratchBlock* GenCollectedHeap::gather_scratch(Generation* requestor,
aoqi@0 1116 size_t max_alloc_words) {
aoqi@0 1117 ScratchBlock* res = NULL;
aoqi@0 1118 for (int i = 0; i < _n_gens; i++) {
aoqi@0 1119 _gens[i]->contribute_scratch(res, requestor, max_alloc_words);
aoqi@0 1120 }
aoqi@0 1121 sort_scratch_list(res);
aoqi@0 1122 return res;
aoqi@0 1123 }
aoqi@0 1124
aoqi@0 1125 void GenCollectedHeap::release_scratch() {
aoqi@0 1126 for (int i = 0; i < _n_gens; i++) {
aoqi@0 1127 _gens[i]->reset_scratch();
aoqi@0 1128 }
aoqi@0 1129 }
aoqi@0 1130
aoqi@0 1131 class GenPrepareForVerifyClosure: public GenCollectedHeap::GenClosure {
aoqi@0 1132 void do_generation(Generation* gen) {
aoqi@0 1133 gen->prepare_for_verify();
aoqi@0 1134 }
aoqi@0 1135 };
aoqi@0 1136
aoqi@0 1137 void GenCollectedHeap::prepare_for_verify() {
aoqi@0 1138 ensure_parsability(false); // no need to retire TLABs
aoqi@0 1139 GenPrepareForVerifyClosure blk;
aoqi@0 1140 generation_iterate(&blk, false);
aoqi@0 1141 }
aoqi@0 1142
aoqi@0 1143
aoqi@0 1144 void GenCollectedHeap::generation_iterate(GenClosure* cl,
aoqi@0 1145 bool old_to_young) {
aoqi@0 1146 if (old_to_young) {
aoqi@0 1147 for (int i = _n_gens-1; i >= 0; i--) {
aoqi@0 1148 cl->do_generation(_gens[i]);
aoqi@0 1149 }
aoqi@0 1150 } else {
aoqi@0 1151 for (int i = 0; i < _n_gens; i++) {
aoqi@0 1152 cl->do_generation(_gens[i]);
aoqi@0 1153 }
aoqi@0 1154 }
aoqi@0 1155 }
aoqi@0 1156
aoqi@0 1157 void GenCollectedHeap::space_iterate(SpaceClosure* cl) {
aoqi@0 1158 for (int i = 0; i < _n_gens; i++) {
aoqi@0 1159 _gens[i]->space_iterate(cl, true);
aoqi@0 1160 }
aoqi@0 1161 }
aoqi@0 1162
aoqi@0 1163 bool GenCollectedHeap::is_maximal_no_gc() const {
aoqi@0 1164 for (int i = 0; i < _n_gens; i++) {
aoqi@0 1165 if (!_gens[i]->is_maximal_no_gc()) {
aoqi@0 1166 return false;
aoqi@0 1167 }
aoqi@0 1168 }
aoqi@0 1169 return true;
aoqi@0 1170 }
aoqi@0 1171
aoqi@0 1172 void GenCollectedHeap::save_marks() {
aoqi@0 1173 for (int i = 0; i < _n_gens; i++) {
aoqi@0 1174 _gens[i]->save_marks();
aoqi@0 1175 }
aoqi@0 1176 }
aoqi@0 1177
aoqi@0 1178 GenCollectedHeap* GenCollectedHeap::heap() {
aoqi@0 1179 assert(_gch != NULL, "Uninitialized access to GenCollectedHeap::heap()");
aoqi@0 1180 assert(_gch->kind() == CollectedHeap::GenCollectedHeap, "not a generational heap");
aoqi@0 1181 return _gch;
aoqi@0 1182 }
aoqi@0 1183
aoqi@0 1184
aoqi@0 1185 void GenCollectedHeap::prepare_for_compaction() {
aoqi@0 1186 guarantee(_n_gens = 2, "Wrong number of generations");
aoqi@0 1187 Generation* old_gen = _gens[1];
aoqi@0 1188 // Start by compacting into same gen.
tschatzl@7009 1189 CompactPoint cp(old_gen);
aoqi@0 1190 old_gen->prepare_for_compaction(&cp);
aoqi@0 1191 Generation* young_gen = _gens[0];
aoqi@0 1192 young_gen->prepare_for_compaction(&cp);
aoqi@0 1193 }
aoqi@0 1194
aoqi@0 1195 GCStats* GenCollectedHeap::gc_stats(int level) const {
aoqi@0 1196 return _gens[level]->gc_stats();
aoqi@0 1197 }
aoqi@0 1198
aoqi@0 1199 void GenCollectedHeap::verify(bool silent, VerifyOption option /* ignored */) {
aoqi@0 1200 for (int i = _n_gens-1; i >= 0; i--) {
aoqi@0 1201 Generation* g = _gens[i];
aoqi@0 1202 if (!silent) {
aoqi@0 1203 gclog_or_tty->print("%s", g->name());
aoqi@0 1204 gclog_or_tty->print(" ");
aoqi@0 1205 }
aoqi@0 1206 g->verify();
aoqi@0 1207 }
aoqi@0 1208 if (!silent) {
aoqi@0 1209 gclog_or_tty->print("remset ");
aoqi@0 1210 }
aoqi@0 1211 rem_set()->verify();
aoqi@0 1212 }
aoqi@0 1213
aoqi@0 1214 void GenCollectedHeap::print_on(outputStream* st) const {
aoqi@0 1215 for (int i = 0; i < _n_gens; i++) {
aoqi@0 1216 _gens[i]->print_on(st);
aoqi@0 1217 }
aoqi@0 1218 MetaspaceAux::print_on(st);
aoqi@0 1219 }
aoqi@0 1220
aoqi@0 1221 void GenCollectedHeap::gc_threads_do(ThreadClosure* tc) const {
aoqi@0 1222 if (workers() != NULL) {
aoqi@0 1223 workers()->threads_do(tc);
aoqi@0 1224 }
aoqi@0 1225 #if INCLUDE_ALL_GCS
aoqi@0 1226 if (UseConcMarkSweepGC) {
aoqi@0 1227 ConcurrentMarkSweepThread::threads_do(tc);
aoqi@0 1228 }
aoqi@0 1229 #endif // INCLUDE_ALL_GCS
aoqi@0 1230 }
aoqi@0 1231
aoqi@0 1232 void GenCollectedHeap::print_gc_threads_on(outputStream* st) const {
aoqi@0 1233 #if INCLUDE_ALL_GCS
aoqi@0 1234 if (UseParNewGC) {
aoqi@0 1235 workers()->print_worker_threads_on(st);
aoqi@0 1236 }
aoqi@0 1237 if (UseConcMarkSweepGC) {
aoqi@0 1238 ConcurrentMarkSweepThread::print_all_on(st);
aoqi@0 1239 }
aoqi@0 1240 #endif // INCLUDE_ALL_GCS
aoqi@0 1241 }
aoqi@0 1242
aoqi@0 1243 void GenCollectedHeap::print_on_error(outputStream* st) const {
aoqi@0 1244 this->CollectedHeap::print_on_error(st);
aoqi@0 1245
aoqi@0 1246 #if INCLUDE_ALL_GCS
aoqi@0 1247 if (UseConcMarkSweepGC) {
aoqi@0 1248 st->cr();
aoqi@0 1249 CMSCollector::print_on_error(st);
aoqi@0 1250 }
aoqi@0 1251 #endif // INCLUDE_ALL_GCS
aoqi@0 1252 }
aoqi@0 1253
aoqi@0 1254 void GenCollectedHeap::print_tracing_info() const {
aoqi@0 1255 if (TraceGen0Time) {
aoqi@0 1256 get_gen(0)->print_summary_info();
aoqi@0 1257 }
aoqi@0 1258 if (TraceGen1Time) {
aoqi@0 1259 get_gen(1)->print_summary_info();
aoqi@0 1260 }
aoqi@0 1261 }
aoqi@0 1262
aoqi@0 1263 void GenCollectedHeap::print_heap_change(size_t prev_used) const {
aoqi@0 1264 if (PrintGCDetails && Verbose) {
aoqi@0 1265 gclog_or_tty->print(" " SIZE_FORMAT
aoqi@0 1266 "->" SIZE_FORMAT
aoqi@0 1267 "(" SIZE_FORMAT ")",
aoqi@0 1268 prev_used, used(), capacity());
aoqi@0 1269 } else {
aoqi@0 1270 gclog_or_tty->print(" " SIZE_FORMAT "K"
aoqi@0 1271 "->" SIZE_FORMAT "K"
aoqi@0 1272 "(" SIZE_FORMAT "K)",
aoqi@0 1273 prev_used / K, used() / K, capacity() / K);
aoqi@0 1274 }
aoqi@0 1275 }
aoqi@0 1276
aoqi@0 1277 class GenGCPrologueClosure: public GenCollectedHeap::GenClosure {
aoqi@0 1278 private:
aoqi@0 1279 bool _full;
aoqi@0 1280 public:
aoqi@0 1281 void do_generation(Generation* gen) {
aoqi@0 1282 gen->gc_prologue(_full);
aoqi@0 1283 }
aoqi@0 1284 GenGCPrologueClosure(bool full) : _full(full) {};
aoqi@0 1285 };
aoqi@0 1286
aoqi@0 1287 void GenCollectedHeap::gc_prologue(bool full) {
aoqi@0 1288 assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer");
aoqi@0 1289
aoqi@0 1290 always_do_update_barrier = false;
aoqi@0 1291 // Fill TLAB's and such
aoqi@0 1292 CollectedHeap::accumulate_statistics_all_tlabs();
aoqi@0 1293 ensure_parsability(true); // retire TLABs
aoqi@0 1294
aoqi@0 1295 // Walk generations
aoqi@0 1296 GenGCPrologueClosure blk(full);
aoqi@0 1297 generation_iterate(&blk, false); // not old-to-young.
aoqi@0 1298 };
aoqi@0 1299
aoqi@0 1300 class GenGCEpilogueClosure: public GenCollectedHeap::GenClosure {
aoqi@0 1301 private:
aoqi@0 1302 bool _full;
aoqi@0 1303 public:
aoqi@0 1304 void do_generation(Generation* gen) {
aoqi@0 1305 gen->gc_epilogue(_full);
aoqi@0 1306 }
aoqi@0 1307 GenGCEpilogueClosure(bool full) : _full(full) {};
aoqi@0 1308 };
aoqi@0 1309
aoqi@0 1310 void GenCollectedHeap::gc_epilogue(bool full) {
aoqi@0 1311 #ifdef COMPILER2
aoqi@0 1312 assert(DerivedPointerTable::is_empty(), "derived pointer present");
aoqi@0 1313 size_t actual_gap = pointer_delta((HeapWord*) (max_uintx-3), *(end_addr()));
aoqi@0 1314 guarantee(actual_gap > (size_t)FastAllocateSizeLimit, "inline allocation wraps");
aoqi@0 1315 #endif /* COMPILER2 */
aoqi@0 1316
aoqi@0 1317 resize_all_tlabs();
aoqi@0 1318
aoqi@0 1319 GenGCEpilogueClosure blk(full);
aoqi@0 1320 generation_iterate(&blk, false); // not old-to-young.
aoqi@0 1321
aoqi@0 1322 if (!CleanChunkPoolAsync) {
aoqi@0 1323 Chunk::clean_chunk_pool();
aoqi@0 1324 }
aoqi@0 1325
aoqi@0 1326 MetaspaceCounters::update_performance_counters();
aoqi@0 1327 CompressedClassSpaceCounters::update_performance_counters();
aoqi@0 1328
aoqi@0 1329 always_do_update_barrier = UseConcMarkSweepGC;
aoqi@0 1330 };
aoqi@0 1331
aoqi@0 1332 #ifndef PRODUCT
aoqi@0 1333 class GenGCSaveTopsBeforeGCClosure: public GenCollectedHeap::GenClosure {
aoqi@0 1334 private:
aoqi@0 1335 public:
aoqi@0 1336 void do_generation(Generation* gen) {
aoqi@0 1337 gen->record_spaces_top();
aoqi@0 1338 }
aoqi@0 1339 };
aoqi@0 1340
aoqi@0 1341 void GenCollectedHeap::record_gen_tops_before_GC() {
aoqi@0 1342 if (ZapUnusedHeapArea) {
aoqi@0 1343 GenGCSaveTopsBeforeGCClosure blk;
aoqi@0 1344 generation_iterate(&blk, false); // not old-to-young.
aoqi@0 1345 }
aoqi@0 1346 }
aoqi@0 1347 #endif // not PRODUCT
aoqi@0 1348
aoqi@0 1349 class GenEnsureParsabilityClosure: public GenCollectedHeap::GenClosure {
aoqi@0 1350 public:
aoqi@0 1351 void do_generation(Generation* gen) {
aoqi@0 1352 gen->ensure_parsability();
aoqi@0 1353 }
aoqi@0 1354 };
aoqi@0 1355
aoqi@0 1356 void GenCollectedHeap::ensure_parsability(bool retire_tlabs) {
aoqi@0 1357 CollectedHeap::ensure_parsability(retire_tlabs);
aoqi@0 1358 GenEnsureParsabilityClosure ep_cl;
aoqi@0 1359 generation_iterate(&ep_cl, false);
aoqi@0 1360 }
aoqi@0 1361
aoqi@0 1362 oop GenCollectedHeap::handle_failed_promotion(Generation* old_gen,
aoqi@0 1363 oop obj,
aoqi@0 1364 size_t obj_size) {
aoqi@0 1365 guarantee(old_gen->level() == 1, "We only get here with an old generation");
aoqi@0 1366 assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");
aoqi@0 1367 HeapWord* result = NULL;
aoqi@0 1368
aoqi@0 1369 result = old_gen->expand_and_allocate(obj_size, false);
aoqi@0 1370
aoqi@0 1371 if (result != NULL) {
aoqi@0 1372 Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size);
aoqi@0 1373 }
aoqi@0 1374 return oop(result);
aoqi@0 1375 }
aoqi@0 1376
aoqi@0 1377 class GenTimeOfLastGCClosure: public GenCollectedHeap::GenClosure {
aoqi@0 1378 jlong _time; // in ms
aoqi@0 1379 jlong _now; // in ms
aoqi@0 1380
aoqi@0 1381 public:
aoqi@0 1382 GenTimeOfLastGCClosure(jlong now) : _time(now), _now(now) { }
aoqi@0 1383
aoqi@0 1384 jlong time() { return _time; }
aoqi@0 1385
aoqi@0 1386 void do_generation(Generation* gen) {
aoqi@0 1387 _time = MIN2(_time, gen->time_of_last_gc(_now));
aoqi@0 1388 }
aoqi@0 1389 };
aoqi@0 1390
aoqi@0 1391 jlong GenCollectedHeap::millis_since_last_gc() {
aoqi@0 1392 // We need a monotonically non-deccreasing time in ms but
aoqi@0 1393 // os::javaTimeMillis() does not guarantee monotonicity.
aoqi@0 1394 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
aoqi@0 1395 GenTimeOfLastGCClosure tolgc_cl(now);
aoqi@0 1396 // iterate over generations getting the oldest
aoqi@0 1397 // time that a generation was collected
aoqi@0 1398 generation_iterate(&tolgc_cl, false);
aoqi@0 1399
aoqi@0 1400 // javaTimeNanos() is guaranteed to be monotonically non-decreasing
aoqi@0 1401 // provided the underlying platform provides such a time source
aoqi@0 1402 // (and it is bug free). So we still have to guard against getting
aoqi@0 1403 // back a time later than 'now'.
aoqi@0 1404 jlong retVal = now - tolgc_cl.time();
aoqi@0 1405 if (retVal < 0) {
aoqi@0 1406 NOT_PRODUCT(warning("time warp: "INT64_FORMAT, (int64_t) retVal);)
aoqi@0 1407 return 0;
aoqi@0 1408 }
aoqi@0 1409 return retVal;
aoqi@0 1410 }

mercurial