src/share/vm/memory/genCollectedHeap.cpp

Wed, 31 Jul 2019 14:28:51 -0400

author
kbarrett
date
Wed, 31 Jul 2019 14:28:51 -0400
changeset 9787
9f28a4cac6d9
parent 9665
a8441ccaff15
child 9806
758c07667682
child 9896
1b8c45b8216a
permissions
-rw-r--r--

8048556: Unnecessary GCLocker-initiated young GCs
Summary: Fixed recognition of unnecessary GCLocker collections.
Reviewed-by: pliden, tschatzl
Contributed-by: johnc@azul.com

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

mercurial