src/share/vm/memory/genCollectedHeap.cpp

Mon, 01 Dec 2014 15:24:56 +0100

author
mgerdin
date
Mon, 01 Dec 2014 15:24:56 +0100
changeset 7659
38d6febe66af
parent 7612
f74dbdd45754
child 7990
1f646daf0d67
permissions
-rw-r--r--

8075210: Refactor strong root processing in order to allow G1 to evolve separately from GenCollectedHeap
Summary: Create a G1RootProcessor and move SharedHeap root processing to GenCollectedHeap
Reviewed-by: brutisso, tschatzl, ehelin

duke@435 1 /*
drchase@6680 2 * Copyright (c) 2000, 2014, 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@7659 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@7659 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@7659 68 GCH_PS_Universe_oops_do,
mgerdin@7659 69 GCH_PS_JNIHandles_oops_do,
mgerdin@7659 70 GCH_PS_ObjectSynchronizer_oops_do,
mgerdin@7659 71 GCH_PS_FlatProfiler_oops_do,
mgerdin@7659 72 GCH_PS_Management_oops_do,
mgerdin@7659 73 GCH_PS_SystemDictionary_oops_do,
mgerdin@7659 74 GCH_PS_ClassLoaderDataGraph_oops_do,
mgerdin@7659 75 GCH_PS_jvmti_oops_do,
mgerdin@7659 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@7659 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@7659 598 set_n_termination(t);
duke@435 599 }
duke@435 600
mgerdin@7659 601 void GenCollectedHeap::set_n_termination(uint t) {
mgerdin@7659 602 _process_strong_tasks->set_n_threads(t);
mgerdin@7659 603 }
mgerdin@7659 604
mgerdin@7659 605 #ifdef ASSERT
mgerdin@7659 606 class AssertNonScavengableClosure: public OopClosure {
mgerdin@7659 607 public:
mgerdin@7659 608 virtual void do_oop(oop* p) {
mgerdin@7659 609 assert(!Universe::heap()->is_in_partial_collection(*p),
mgerdin@7659 610 "Referent should not be scavengable."); }
mgerdin@7659 611 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
mgerdin@7659 612 };
mgerdin@7659 613 static AssertNonScavengableClosure assert_is_non_scavengable_closure;
mgerdin@7659 614 #endif
mgerdin@7659 615
mgerdin@7659 616 void GenCollectedHeap::process_roots(bool activate_scope,
mgerdin@7659 617 ScanningOption so,
mgerdin@7659 618 OopClosure* strong_roots,
mgerdin@7659 619 OopClosure* weak_roots,
mgerdin@7659 620 CLDClosure* strong_cld_closure,
mgerdin@7659 621 CLDClosure* weak_cld_closure,
mgerdin@7659 622 CodeBlobClosure* code_roots) {
mgerdin@7659 623 StrongRootsScope srs(this, activate_scope);
jrose@1424 624
stefank@6992 625 // General roots.
mgerdin@7659 626 assert(_strong_roots_parity != 0, "must have called prologue code");
mgerdin@7659 627 assert(code_roots != NULL, "code root closure should always be set");
mgerdin@7659 628 // _n_termination for _process_strong_tasks should be set up stream
mgerdin@7659 629 // in a method not running in a GC worker. Otherwise the GC worker
mgerdin@7659 630 // could be trying to change the termination condition while the task
mgerdin@7659 631 // is executing in another GC worker.
mgerdin@7659 632
mgerdin@7659 633 if (!_process_strong_tasks->is_task_claimed(GCH_PS_ClassLoaderDataGraph_oops_do)) {
mgerdin@7659 634 ClassLoaderDataGraph::roots_cld_do(strong_cld_closure, weak_cld_closure);
mgerdin@7659 635 }
mgerdin@7659 636
mgerdin@7659 637 // Some CLDs contained in the thread frames should be considered strong.
mgerdin@7659 638 // Don't process them if they will be processed during the ClassLoaderDataGraph phase.
mgerdin@7659 639 CLDClosure* roots_from_clds_p = (strong_cld_closure != weak_cld_closure) ? strong_cld_closure : NULL;
mgerdin@7659 640 // Only process code roots from thread stacks if we aren't visiting the entire CodeCache anyway
mgerdin@7659 641 CodeBlobClosure* roots_from_code_p = (so & SO_AllCodeCache) ? NULL : code_roots;
mgerdin@7659 642
mgerdin@7659 643 Threads::possibly_parallel_oops_do(strong_roots, roots_from_clds_p, roots_from_code_p);
mgerdin@7659 644
mgerdin@7659 645 if (!_process_strong_tasks->is_task_claimed(GCH_PS_Universe_oops_do)) {
mgerdin@7659 646 Universe::oops_do(strong_roots);
mgerdin@7659 647 }
mgerdin@7659 648 // Global (strong) JNI handles
mgerdin@7659 649 if (!_process_strong_tasks->is_task_claimed(GCH_PS_JNIHandles_oops_do)) {
mgerdin@7659 650 JNIHandles::oops_do(strong_roots);
mgerdin@7659 651 }
mgerdin@7659 652
mgerdin@7659 653 if (!_process_strong_tasks->is_task_claimed(GCH_PS_ObjectSynchronizer_oops_do)) {
mgerdin@7659 654 ObjectSynchronizer::oops_do(strong_roots);
mgerdin@7659 655 }
mgerdin@7659 656 if (!_process_strong_tasks->is_task_claimed(GCH_PS_FlatProfiler_oops_do)) {
mgerdin@7659 657 FlatProfiler::oops_do(strong_roots);
mgerdin@7659 658 }
mgerdin@7659 659 if (!_process_strong_tasks->is_task_claimed(GCH_PS_Management_oops_do)) {
mgerdin@7659 660 Management::oops_do(strong_roots);
mgerdin@7659 661 }
mgerdin@7659 662 if (!_process_strong_tasks->is_task_claimed(GCH_PS_jvmti_oops_do)) {
mgerdin@7659 663 JvmtiExport::oops_do(strong_roots);
mgerdin@7659 664 }
mgerdin@7659 665
mgerdin@7659 666 if (!_process_strong_tasks->is_task_claimed(GCH_PS_SystemDictionary_oops_do)) {
mgerdin@7659 667 SystemDictionary::roots_oops_do(strong_roots, weak_roots);
mgerdin@7659 668 }
mgerdin@7659 669
mgerdin@7659 670 // All threads execute the following. A specific chunk of buckets
mgerdin@7659 671 // from the StringTable are the individual tasks.
mgerdin@7659 672 if (weak_roots != NULL) {
mgerdin@7659 673 if (CollectedHeap::use_parallel_gc_threads()) {
mgerdin@7659 674 StringTable::possibly_parallel_oops_do(weak_roots);
mgerdin@7659 675 } else {
mgerdin@7659 676 StringTable::oops_do(weak_roots);
mgerdin@7659 677 }
mgerdin@7659 678 }
mgerdin@7659 679
mgerdin@7659 680 if (!_process_strong_tasks->is_task_claimed(GCH_PS_CodeCache_oops_do)) {
mgerdin@7659 681 if (so & SO_ScavengeCodeCache) {
mgerdin@7659 682 assert(code_roots != NULL, "must supply closure for code cache");
mgerdin@7659 683
mgerdin@7659 684 // We only visit parts of the CodeCache when scavenging.
mgerdin@7659 685 CodeCache::scavenge_root_nmethods_do(code_roots);
mgerdin@7659 686 }
mgerdin@7659 687 if (so & SO_AllCodeCache) {
mgerdin@7659 688 assert(code_roots != NULL, "must supply closure for code cache");
mgerdin@7659 689
mgerdin@7659 690 // CMSCollector uses this to do intermediate-strength collections.
mgerdin@7659 691 // We scan the entire code cache, since CodeCache::do_unloading is not called.
mgerdin@7659 692 CodeCache::blobs_do(code_roots);
mgerdin@7659 693 }
mgerdin@7659 694 // Verify that the code cache contents are not subject to
mgerdin@7659 695 // movement by a scavenging collection.
mgerdin@7659 696 DEBUG_ONLY(CodeBlobToOopClosure assert_code_is_non_scavengable(&assert_is_non_scavengable_closure, !CodeBlobToOopClosure::FixRelocations));
mgerdin@7659 697 DEBUG_ONLY(CodeCache::asserted_non_scavengable_nmethods_do(&assert_code_is_non_scavengable));
mgerdin@7659 698 }
mgerdin@7659 699
mgerdin@7659 700 }
mgerdin@7659 701
mgerdin@7659 702 void GenCollectedHeap::gen_process_roots(int level,
mgerdin@7659 703 bool younger_gens_as_roots,
mgerdin@7659 704 bool activate_scope,
mgerdin@7659 705 ScanningOption so,
mgerdin@7659 706 bool only_strong_roots,
mgerdin@7659 707 OopsInGenClosure* not_older_gens,
mgerdin@7659 708 OopsInGenClosure* older_gens,
mgerdin@7659 709 CLDClosure* cld_closure) {
mgerdin@7659 710 const bool is_adjust_phase = !only_strong_roots && !younger_gens_as_roots;
mgerdin@7659 711
mgerdin@7659 712 bool is_moving_collection = false;
mgerdin@7659 713 if (level == 0 || is_adjust_phase) {
mgerdin@7659 714 // young collections are always moving
mgerdin@7659 715 is_moving_collection = true;
mgerdin@7659 716 }
mgerdin@7659 717
mgerdin@7659 718 MarkingCodeBlobClosure mark_code_closure(not_older_gens, is_moving_collection);
mgerdin@7659 719 OopsInGenClosure* weak_roots = only_strong_roots ? NULL : not_older_gens;
mgerdin@7659 720 CLDClosure* weak_cld_closure = only_strong_roots ? NULL : cld_closure;
mgerdin@7659 721
mgerdin@7659 722 process_roots(activate_scope, so,
mgerdin@7659 723 not_older_gens, weak_roots,
mgerdin@7659 724 cld_closure, weak_cld_closure,
mgerdin@7659 725 &mark_code_closure);
duke@435 726
duke@435 727 if (younger_gens_as_roots) {
mgerdin@7659 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@7659 744 _process_strong_tasks->all_tasks_completed();
stefank@6992 745 }
stefank@6992 746
stefank@6992 747
mgerdin@7659 748 class AlwaysTrueClosure: public BoolObjectClosure {
mgerdin@7659 749 public:
mgerdin@7659 750 bool do_object_b(oop p) { return true; }
mgerdin@7659 751 };
mgerdin@7659 752 static AlwaysTrueClosure always_true;
duke@435 753
stefank@6971 754 void GenCollectedHeap::gen_process_weak_roots(OopClosure* root_closure) {
mgerdin@7659 755 JNIHandles::weak_oops_do(&always_true, root_closure);
duke@435 756 for (int i = 0; i < _n_gens; i++) {
duke@435 757 _gens[i]->ref_processor()->weak_oops_do(root_closure);
duke@435 758 }
duke@435 759 }
duke@435 760
duke@435 761 #define GCH_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix) \
duke@435 762 void GenCollectedHeap:: \
duke@435 763 oop_since_save_marks_iterate(int level, \
duke@435 764 OopClosureType* cur, \
duke@435 765 OopClosureType* older) { \
duke@435 766 _gens[level]->oop_since_save_marks_iterate##nv_suffix(cur); \
duke@435 767 for (int i = level+1; i < n_gens(); i++) { \
duke@435 768 _gens[i]->oop_since_save_marks_iterate##nv_suffix(older); \
duke@435 769 } \
duke@435 770 }
duke@435 771
duke@435 772 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DEFN)
duke@435 773
duke@435 774 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DEFN
duke@435 775
duke@435 776 bool GenCollectedHeap::no_allocs_since_save_marks(int level) {
duke@435 777 for (int i = level; i < _n_gens; i++) {
duke@435 778 if (!_gens[i]->no_allocs_since_save_marks()) return false;
duke@435 779 }
coleenp@4037 780 return true;
duke@435 781 }
duke@435 782
duke@435 783 bool GenCollectedHeap::supports_inline_contig_alloc() const {
duke@435 784 return _gens[0]->supports_inline_contig_alloc();
duke@435 785 }
duke@435 786
duke@435 787 HeapWord** GenCollectedHeap::top_addr() const {
duke@435 788 return _gens[0]->top_addr();
duke@435 789 }
duke@435 790
duke@435 791 HeapWord** GenCollectedHeap::end_addr() const {
duke@435 792 return _gens[0]->end_addr();
duke@435 793 }
duke@435 794
duke@435 795 // public collection interfaces
duke@435 796
duke@435 797 void GenCollectedHeap::collect(GCCause::Cause cause) {
duke@435 798 if (should_do_concurrent_full_gc(cause)) {
jprovino@4542 799 #if INCLUDE_ALL_GCS
duke@435 800 // mostly concurrent full collection
duke@435 801 collect_mostly_concurrent(cause);
jprovino@4542 802 #else // INCLUDE_ALL_GCS
duke@435 803 ShouldNotReachHere();
jprovino@4542 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);
duke@435 808 } else {
duke@435 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 }
duke@435 817 #else
duke@435 818 // Stop-the-world full collection
duke@435 819 collect(cause, n_gens() - 1);
duke@435 820 #endif
duke@435 821 }
duke@435 822 }
duke@435 823
duke@435 824 void GenCollectedHeap::collect(GCCause::Cause cause, int max_level) {
duke@435 825 // The caller doesn't have the Heap_lock
duke@435 826 assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
duke@435 827 MutexLocker ml(Heap_lock);
duke@435 828 collect_locked(cause, max_level);
duke@435 829 }
duke@435 830
duke@435 831 void GenCollectedHeap::collect_locked(GCCause::Cause cause) {
duke@435 832 // The caller has the Heap_lock
duke@435 833 assert(Heap_lock->owned_by_self(), "this thread should own the Heap_lock");
duke@435 834 collect_locked(cause, n_gens() - 1);
duke@435 835 }
duke@435 836
duke@435 837 // this is the private collection interface
duke@435 838 // The Heap_lock is expected to be held on entry.
duke@435 839
duke@435 840 void GenCollectedHeap::collect_locked(GCCause::Cause cause, int max_level) {
duke@435 841 // Read the GC count while holding the Heap_lock
duke@435 842 unsigned int gc_count_before = total_collections();
duke@435 843 unsigned int full_gc_count_before = total_full_collections();
duke@435 844 {
duke@435 845 MutexUnlocker mu(Heap_lock); // give up heap lock, execute gets it back
duke@435 846 VM_GenCollectFull op(gc_count_before, full_gc_count_before,
duke@435 847 cause, max_level);
duke@435 848 VMThread::execute(&op);
duke@435 849 }
duke@435 850 }
duke@435 851
jprovino@4542 852 #if INCLUDE_ALL_GCS
duke@435 853 bool GenCollectedHeap::create_cms_collector() {
duke@435 854
duke@435 855 assert(((_gens[1]->kind() == Generation::ConcurrentMarkSweep) ||
coleenp@4037 856 (_gens[1]->kind() == Generation::ASConcurrentMarkSweep)),
duke@435 857 "Unexpected generation kinds");
duke@435 858 // Skip two header words in the block content verification
duke@435 859 NOT_PRODUCT(_skip_header_HeapWords = CMSCollector::skip_header_HeapWords();)
duke@435 860 CMSCollector* collector = new CMSCollector(
duke@435 861 (ConcurrentMarkSweepGeneration*)_gens[1],
duke@435 862 _rem_set->as_CardTableRS(),
duke@435 863 (ConcurrentMarkSweepPolicy*) collector_policy());
duke@435 864
duke@435 865 if (collector == NULL || !collector->completed_initialization()) {
duke@435 866 if (collector) {
duke@435 867 delete collector; // Be nice in embedded situation
duke@435 868 }
duke@435 869 vm_shutdown_during_initialization("Could not create CMS collector");
duke@435 870 return false;
duke@435 871 }
duke@435 872 return true; // success
duke@435 873 }
duke@435 874
duke@435 875 void GenCollectedHeap::collect_mostly_concurrent(GCCause::Cause cause) {
duke@435 876 assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock");
duke@435 877
duke@435 878 MutexLocker ml(Heap_lock);
duke@435 879 // Read the GC counts while holding the Heap_lock
duke@435 880 unsigned int full_gc_count_before = total_full_collections();
duke@435 881 unsigned int gc_count_before = total_collections();
duke@435 882 {
duke@435 883 MutexUnlocker mu(Heap_lock);
duke@435 884 VM_GenCollectFullConcurrent op(gc_count_before, full_gc_count_before, cause);
duke@435 885 VMThread::execute(&op);
duke@435 886 }
duke@435 887 }
jprovino@4542 888 #endif // INCLUDE_ALL_GCS
duke@435 889
coleenp@4037 890 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs) {
coleenp@4037 891 do_full_collection(clear_all_soft_refs, _n_gens - 1);
coleenp@4037 892 }
duke@435 893
duke@435 894 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs,
duke@435 895 int max_level) {
duke@435 896 int local_max_level;
ysr@2336 897 if (!incremental_collection_will_fail(false /* don't consult_young */) &&
duke@435 898 gc_cause() == GCCause::_gc_locker) {
duke@435 899 local_max_level = 0;
duke@435 900 } else {
duke@435 901 local_max_level = max_level;
duke@435 902 }
duke@435 903
duke@435 904 do_collection(true /* full */,
duke@435 905 clear_all_soft_refs /* clear_all_soft_refs */,
duke@435 906 0 /* size */,
duke@435 907 false /* is_tlab */,
duke@435 908 local_max_level /* max_level */);
duke@435 909 // Hack XXX FIX ME !!!
duke@435 910 // A scavenge may not have been attempted, or may have
duke@435 911 // been attempted and failed, because the old gen was too full
duke@435 912 if (local_max_level == 0 && gc_cause() == GCCause::_gc_locker &&
ysr@2336 913 incremental_collection_will_fail(false /* don't consult_young */)) {
duke@435 914 if (PrintGCDetails) {
duke@435 915 gclog_or_tty->print_cr("GC locker: Trying a full collection "
duke@435 916 "because scavenge failed");
duke@435 917 }
duke@435 918 // This time allow the old gen to be collected as well
duke@435 919 do_collection(true /* full */,
duke@435 920 clear_all_soft_refs /* clear_all_soft_refs */,
duke@435 921 0 /* size */,
duke@435 922 false /* is_tlab */,
duke@435 923 n_gens() - 1 /* max_level */);
duke@435 924 }
duke@435 925 }
duke@435 926
jmasa@2909 927 bool GenCollectedHeap::is_in_young(oop p) {
jmasa@2909 928 bool result = ((HeapWord*)p) < _gens[_n_gens - 1]->reserved().start();
jmasa@2909 929 assert(result == _gens[0]->is_in_reserved(p),
drchase@6680 930 err_msg("incorrect test - result=%d, p=" PTR_FORMAT, result, p2i((void*)p)));
jmasa@2909 931 return result;
jmasa@2909 932 }
jmasa@2909 933
stefank@3335 934 // Returns "TRUE" iff "p" points into the committed areas of the heap.
duke@435 935 bool GenCollectedHeap::is_in(const void* p) const {
duke@435 936 #ifndef ASSERT
johnc@4899 937 guarantee(VerifyBeforeGC ||
johnc@4899 938 VerifyDuringGC ||
johnc@4899 939 VerifyBeforeExit ||
johnc@4899 940 VerifyDuringStartup ||
johnc@4899 941 PrintAssembly ||
johnc@4899 942 tty->count() != 0 || // already printing
johnc@4899 943 VerifyAfterGC ||
bobv@2036 944 VMError::fatal_error_in_progress(), "too expensive");
bobv@2036 945
duke@435 946 #endif
duke@435 947 // This might be sped up with a cache of the last generation that
duke@435 948 // answered yes.
duke@435 949 for (int i = 0; i < _n_gens; i++) {
duke@435 950 if (_gens[i]->is_in(p)) return true;
duke@435 951 }
duke@435 952 // Otherwise...
duke@435 953 return false;
duke@435 954 }
duke@435 955
jmasa@2909 956 #ifdef ASSERT
jmasa@2909 957 // Don't implement this by using is_in_young(). This method is used
jmasa@2909 958 // in some cases to check that is_in_young() is correct.
jmasa@2909 959 bool GenCollectedHeap::is_in_partial_collection(const void* p) {
jmasa@2909 960 assert(is_in_reserved(p) || p == NULL,
jmasa@2909 961 "Does not work if address is non-null and outside of the heap");
jmasa@2909 962 return p < _gens[_n_gens - 2]->reserved().end() && p != NULL;
duke@435 963 }
jmasa@2909 964 #endif
duke@435 965
coleenp@4037 966 void GenCollectedHeap::oop_iterate(ExtendedOopClosure* cl) {
duke@435 967 for (int i = 0; i < _n_gens; i++) {
duke@435 968 _gens[i]->oop_iterate(cl);
duke@435 969 }
duke@435 970 }
duke@435 971
duke@435 972 void GenCollectedHeap::object_iterate(ObjectClosure* cl) {
duke@435 973 for (int i = 0; i < _n_gens; i++) {
duke@435 974 _gens[i]->object_iterate(cl);
duke@435 975 }
duke@435 976 }
duke@435 977
jmasa@952 978 void GenCollectedHeap::safe_object_iterate(ObjectClosure* cl) {
jmasa@952 979 for (int i = 0; i < _n_gens; i++) {
jmasa@952 980 _gens[i]->safe_object_iterate(cl);
jmasa@952 981 }
jmasa@952 982 }
jmasa@952 983
duke@435 984 Space* GenCollectedHeap::space_containing(const void* addr) const {
duke@435 985 for (int i = 0; i < _n_gens; i++) {
duke@435 986 Space* res = _gens[i]->space_containing(addr);
duke@435 987 if (res != NULL) return res;
duke@435 988 }
duke@435 989 // Otherwise...
duke@435 990 assert(false, "Could not find containing space");
duke@435 991 return NULL;
duke@435 992 }
duke@435 993
duke@435 994
duke@435 995 HeapWord* GenCollectedHeap::block_start(const void* addr) const {
duke@435 996 assert(is_in_reserved(addr), "block_start of address outside of heap");
duke@435 997 for (int i = 0; i < _n_gens; i++) {
duke@435 998 if (_gens[i]->is_in_reserved(addr)) {
duke@435 999 assert(_gens[i]->is_in(addr),
duke@435 1000 "addr should be in allocated part of generation");
duke@435 1001 return _gens[i]->block_start(addr);
duke@435 1002 }
duke@435 1003 }
duke@435 1004 assert(false, "Some generation should contain the address");
duke@435 1005 return NULL;
duke@435 1006 }
duke@435 1007
duke@435 1008 size_t GenCollectedHeap::block_size(const HeapWord* addr) const {
duke@435 1009 assert(is_in_reserved(addr), "block_size of address outside of heap");
duke@435 1010 for (int i = 0; i < _n_gens; i++) {
duke@435 1011 if (_gens[i]->is_in_reserved(addr)) {
duke@435 1012 assert(_gens[i]->is_in(addr),
duke@435 1013 "addr should be in allocated part of generation");
duke@435 1014 return _gens[i]->block_size(addr);
duke@435 1015 }
duke@435 1016 }
duke@435 1017 assert(false, "Some generation should contain the address");
duke@435 1018 return 0;
duke@435 1019 }
duke@435 1020
duke@435 1021 bool GenCollectedHeap::block_is_obj(const HeapWord* addr) const {
duke@435 1022 assert(is_in_reserved(addr), "block_is_obj of address outside of heap");
duke@435 1023 assert(block_start(addr) == addr, "addr must be a block start");
duke@435 1024 for (int i = 0; i < _n_gens; i++) {
duke@435 1025 if (_gens[i]->is_in_reserved(addr)) {
duke@435 1026 return _gens[i]->block_is_obj(addr);
duke@435 1027 }
duke@435 1028 }
duke@435 1029 assert(false, "Some generation should contain the address");
duke@435 1030 return false;
duke@435 1031 }
duke@435 1032
duke@435 1033 bool GenCollectedHeap::supports_tlab_allocation() const {
duke@435 1034 for (int i = 0; i < _n_gens; i += 1) {
duke@435 1035 if (_gens[i]->supports_tlab_allocation()) {
duke@435 1036 return true;
duke@435 1037 }
duke@435 1038 }
duke@435 1039 return false;
duke@435 1040 }
duke@435 1041
duke@435 1042 size_t GenCollectedHeap::tlab_capacity(Thread* thr) const {
duke@435 1043 size_t result = 0;
duke@435 1044 for (int i = 0; i < _n_gens; i += 1) {
duke@435 1045 if (_gens[i]->supports_tlab_allocation()) {
duke@435 1046 result += _gens[i]->tlab_capacity();
duke@435 1047 }
duke@435 1048 }
duke@435 1049 return result;
duke@435 1050 }
duke@435 1051
brutisso@6376 1052 size_t GenCollectedHeap::tlab_used(Thread* thr) const {
brutisso@6376 1053 size_t result = 0;
brutisso@6376 1054 for (int i = 0; i < _n_gens; i += 1) {
brutisso@6376 1055 if (_gens[i]->supports_tlab_allocation()) {
brutisso@6376 1056 result += _gens[i]->tlab_used();
brutisso@6376 1057 }
brutisso@6376 1058 }
brutisso@6376 1059 return result;
brutisso@6376 1060 }
brutisso@6376 1061
duke@435 1062 size_t GenCollectedHeap::unsafe_max_tlab_alloc(Thread* thr) const {
duke@435 1063 size_t result = 0;
duke@435 1064 for (int i = 0; i < _n_gens; i += 1) {
duke@435 1065 if (_gens[i]->supports_tlab_allocation()) {
duke@435 1066 result += _gens[i]->unsafe_max_tlab_alloc();
duke@435 1067 }
duke@435 1068 }
duke@435 1069 return result;
duke@435 1070 }
duke@435 1071
duke@435 1072 HeapWord* GenCollectedHeap::allocate_new_tlab(size_t size) {
duke@435 1073 bool gc_overhead_limit_was_exceeded;
tonyp@2971 1074 return collector_policy()->mem_allocate_work(size /* size */,
tonyp@2971 1075 true /* is_tlab */,
tonyp@2971 1076 &gc_overhead_limit_was_exceeded);
duke@435 1077 }
duke@435 1078
duke@435 1079 // Requires "*prev_ptr" to be non-NULL. Deletes and a block of minimal size
duke@435 1080 // from the list headed by "*prev_ptr".
duke@435 1081 static ScratchBlock *removeSmallestScratch(ScratchBlock **prev_ptr) {
duke@435 1082 bool first = true;
duke@435 1083 size_t min_size = 0; // "first" makes this conceptually infinite.
duke@435 1084 ScratchBlock **smallest_ptr, *smallest;
duke@435 1085 ScratchBlock *cur = *prev_ptr;
duke@435 1086 while (cur) {
duke@435 1087 assert(*prev_ptr == cur, "just checking");
duke@435 1088 if (first || cur->num_words < min_size) {
duke@435 1089 smallest_ptr = prev_ptr;
duke@435 1090 smallest = cur;
duke@435 1091 min_size = smallest->num_words;
duke@435 1092 first = false;
duke@435 1093 }
duke@435 1094 prev_ptr = &cur->next;
duke@435 1095 cur = cur->next;
duke@435 1096 }
duke@435 1097 smallest = *smallest_ptr;
duke@435 1098 *smallest_ptr = smallest->next;
duke@435 1099 return smallest;
duke@435 1100 }
duke@435 1101
duke@435 1102 // Sort the scratch block list headed by res into decreasing size order,
duke@435 1103 // and set "res" to the result.
duke@435 1104 static void sort_scratch_list(ScratchBlock*& list) {
duke@435 1105 ScratchBlock* sorted = NULL;
duke@435 1106 ScratchBlock* unsorted = list;
duke@435 1107 while (unsorted) {
duke@435 1108 ScratchBlock *smallest = removeSmallestScratch(&unsorted);
duke@435 1109 smallest->next = sorted;
duke@435 1110 sorted = smallest;
duke@435 1111 }
duke@435 1112 list = sorted;
duke@435 1113 }
duke@435 1114
duke@435 1115 ScratchBlock* GenCollectedHeap::gather_scratch(Generation* requestor,
duke@435 1116 size_t max_alloc_words) {
duke@435 1117 ScratchBlock* res = NULL;
duke@435 1118 for (int i = 0; i < _n_gens; i++) {
duke@435 1119 _gens[i]->contribute_scratch(res, requestor, max_alloc_words);
duke@435 1120 }
duke@435 1121 sort_scratch_list(res);
duke@435 1122 return res;
duke@435 1123 }
duke@435 1124
jmasa@698 1125 void GenCollectedHeap::release_scratch() {
jmasa@698 1126 for (int i = 0; i < _n_gens; i++) {
jmasa@698 1127 _gens[i]->reset_scratch();
jmasa@698 1128 }
jmasa@698 1129 }
jmasa@698 1130
duke@435 1131 class GenPrepareForVerifyClosure: public GenCollectedHeap::GenClosure {
duke@435 1132 void do_generation(Generation* gen) {
duke@435 1133 gen->prepare_for_verify();
duke@435 1134 }
duke@435 1135 };
duke@435 1136
duke@435 1137 void GenCollectedHeap::prepare_for_verify() {
duke@435 1138 ensure_parsability(false); // no need to retire TLABs
duke@435 1139 GenPrepareForVerifyClosure blk;
duke@435 1140 generation_iterate(&blk, false);
duke@435 1141 }
duke@435 1142
duke@435 1143
duke@435 1144 void GenCollectedHeap::generation_iterate(GenClosure* cl,
duke@435 1145 bool old_to_young) {
duke@435 1146 if (old_to_young) {
duke@435 1147 for (int i = _n_gens-1; i >= 0; i--) {
duke@435 1148 cl->do_generation(_gens[i]);
duke@435 1149 }
duke@435 1150 } else {
duke@435 1151 for (int i = 0; i < _n_gens; i++) {
duke@435 1152 cl->do_generation(_gens[i]);
duke@435 1153 }
duke@435 1154 }
duke@435 1155 }
duke@435 1156
duke@435 1157 void GenCollectedHeap::space_iterate(SpaceClosure* cl) {
duke@435 1158 for (int i = 0; i < _n_gens; i++) {
duke@435 1159 _gens[i]->space_iterate(cl, true);
duke@435 1160 }
duke@435 1161 }
duke@435 1162
duke@435 1163 bool GenCollectedHeap::is_maximal_no_gc() const {
coleenp@4037 1164 for (int i = 0; i < _n_gens; i++) {
duke@435 1165 if (!_gens[i]->is_maximal_no_gc()) {
duke@435 1166 return false;
duke@435 1167 }
duke@435 1168 }
duke@435 1169 return true;
duke@435 1170 }
duke@435 1171
duke@435 1172 void GenCollectedHeap::save_marks() {
duke@435 1173 for (int i = 0; i < _n_gens; i++) {
duke@435 1174 _gens[i]->save_marks();
duke@435 1175 }
duke@435 1176 }
duke@435 1177
duke@435 1178 GenCollectedHeap* GenCollectedHeap::heap() {
duke@435 1179 assert(_gch != NULL, "Uninitialized access to GenCollectedHeap::heap()");
duke@435 1180 assert(_gch->kind() == CollectedHeap::GenCollectedHeap, "not a generational heap");
duke@435 1181 return _gch;
duke@435 1182 }
duke@435 1183
duke@435 1184
duke@435 1185 void GenCollectedHeap::prepare_for_compaction() {
brutisso@5516 1186 guarantee(_n_gens = 2, "Wrong number of generations");
brutisso@5516 1187 Generation* old_gen = _gens[1];
duke@435 1188 // Start by compacting into same gen.
tschatzl@7009 1189 CompactPoint cp(old_gen);
brutisso@5516 1190 old_gen->prepare_for_compaction(&cp);
brutisso@5516 1191 Generation* young_gen = _gens[0];
brutisso@5516 1192 young_gen->prepare_for_compaction(&cp);
duke@435 1193 }
duke@435 1194
duke@435 1195 GCStats* GenCollectedHeap::gc_stats(int level) const {
duke@435 1196 return _gens[level]->gc_stats();
duke@435 1197 }
duke@435 1198
brutisso@3711 1199 void GenCollectedHeap::verify(bool silent, VerifyOption option /* ignored */) {
duke@435 1200 for (int i = _n_gens-1; i >= 0; i--) {
duke@435 1201 Generation* g = _gens[i];
duke@435 1202 if (!silent) {
drchase@6680 1203 gclog_or_tty->print("%s", g->name());
duke@435 1204 gclog_or_tty->print(" ");
duke@435 1205 }
brutisso@3711 1206 g->verify();
duke@435 1207 }
duke@435 1208 if (!silent) {
duke@435 1209 gclog_or_tty->print("remset ");
duke@435 1210 }
duke@435 1211 rem_set()->verify();
duke@435 1212 }
duke@435 1213
duke@435 1214 void GenCollectedHeap::print_on(outputStream* st) const {
duke@435 1215 for (int i = 0; i < _n_gens; i++) {
duke@435 1216 _gens[i]->print_on(st);
duke@435 1217 }
coleenp@4037 1218 MetaspaceAux::print_on(st);
duke@435 1219 }
duke@435 1220
duke@435 1221 void GenCollectedHeap::gc_threads_do(ThreadClosure* tc) const {
duke@435 1222 if (workers() != NULL) {
duke@435 1223 workers()->threads_do(tc);
duke@435 1224 }
jprovino@4542 1225 #if INCLUDE_ALL_GCS
duke@435 1226 if (UseConcMarkSweepGC) {
duke@435 1227 ConcurrentMarkSweepThread::threads_do(tc);
duke@435 1228 }
jprovino@4542 1229 #endif // INCLUDE_ALL_GCS
duke@435 1230 }
duke@435 1231
duke@435 1232 void GenCollectedHeap::print_gc_threads_on(outputStream* st) const {
jprovino@4542 1233 #if INCLUDE_ALL_GCS
duke@435 1234 if (UseParNewGC) {
duke@435 1235 workers()->print_worker_threads_on(st);
duke@435 1236 }
duke@435 1237 if (UseConcMarkSweepGC) {
duke@435 1238 ConcurrentMarkSweepThread::print_all_on(st);
duke@435 1239 }
jprovino@4542 1240 #endif // INCLUDE_ALL_GCS
duke@435 1241 }
duke@435 1242
stefank@4904 1243 void GenCollectedHeap::print_on_error(outputStream* st) const {
stefank@4904 1244 this->CollectedHeap::print_on_error(st);
stefank@4904 1245
stefank@4904 1246 #if INCLUDE_ALL_GCS
stefank@4904 1247 if (UseConcMarkSweepGC) {
stefank@4904 1248 st->cr();
stefank@4904 1249 CMSCollector::print_on_error(st);
stefank@4904 1250 }
stefank@4904 1251 #endif // INCLUDE_ALL_GCS
stefank@4904 1252 }
stefank@4904 1253
duke@435 1254 void GenCollectedHeap::print_tracing_info() const {
duke@435 1255 if (TraceGen0Time) {
duke@435 1256 get_gen(0)->print_summary_info();
duke@435 1257 }
duke@435 1258 if (TraceGen1Time) {
duke@435 1259 get_gen(1)->print_summary_info();
duke@435 1260 }
duke@435 1261 }
duke@435 1262
duke@435 1263 void GenCollectedHeap::print_heap_change(size_t prev_used) const {
duke@435 1264 if (PrintGCDetails && Verbose) {
duke@435 1265 gclog_or_tty->print(" " SIZE_FORMAT
duke@435 1266 "->" SIZE_FORMAT
duke@435 1267 "(" SIZE_FORMAT ")",
duke@435 1268 prev_used, used(), capacity());
duke@435 1269 } else {
duke@435 1270 gclog_or_tty->print(" " SIZE_FORMAT "K"
duke@435 1271 "->" SIZE_FORMAT "K"
duke@435 1272 "(" SIZE_FORMAT "K)",
duke@435 1273 prev_used / K, used() / K, capacity() / K);
duke@435 1274 }
duke@435 1275 }
duke@435 1276
duke@435 1277 class GenGCPrologueClosure: public GenCollectedHeap::GenClosure {
duke@435 1278 private:
duke@435 1279 bool _full;
duke@435 1280 public:
duke@435 1281 void do_generation(Generation* gen) {
duke@435 1282 gen->gc_prologue(_full);
duke@435 1283 }
duke@435 1284 GenGCPrologueClosure(bool full) : _full(full) {};
duke@435 1285 };
duke@435 1286
duke@435 1287 void GenCollectedHeap::gc_prologue(bool full) {
duke@435 1288 assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer");
duke@435 1289
duke@435 1290 always_do_update_barrier = false;
duke@435 1291 // Fill TLAB's and such
duke@435 1292 CollectedHeap::accumulate_statistics_all_tlabs();
duke@435 1293 ensure_parsability(true); // retire TLABs
duke@435 1294
duke@435 1295 // Walk generations
duke@435 1296 GenGCPrologueClosure blk(full);
duke@435 1297 generation_iterate(&blk, false); // not old-to-young.
duke@435 1298 };
duke@435 1299
duke@435 1300 class GenGCEpilogueClosure: public GenCollectedHeap::GenClosure {
duke@435 1301 private:
duke@435 1302 bool _full;
duke@435 1303 public:
duke@435 1304 void do_generation(Generation* gen) {
duke@435 1305 gen->gc_epilogue(_full);
duke@435 1306 }
duke@435 1307 GenGCEpilogueClosure(bool full) : _full(full) {};
duke@435 1308 };
duke@435 1309
duke@435 1310 void GenCollectedHeap::gc_epilogue(bool full) {
duke@435 1311 #ifdef COMPILER2
duke@435 1312 assert(DerivedPointerTable::is_empty(), "derived pointer present");
duke@435 1313 size_t actual_gap = pointer_delta((HeapWord*) (max_uintx-3), *(end_addr()));
duke@435 1314 guarantee(actual_gap > (size_t)FastAllocateSizeLimit, "inline allocation wraps");
duke@435 1315 #endif /* COMPILER2 */
duke@435 1316
duke@435 1317 resize_all_tlabs();
duke@435 1318
duke@435 1319 GenGCEpilogueClosure blk(full);
duke@435 1320 generation_iterate(&blk, false); // not old-to-young.
duke@435 1321
jcoomes@2996 1322 if (!CleanChunkPoolAsync) {
jcoomes@2996 1323 Chunk::clean_chunk_pool();
jcoomes@2996 1324 }
jcoomes@2996 1325
coleenp@4037 1326 MetaspaceCounters::update_performance_counters();
ehelin@5531 1327 CompressedClassSpaceCounters::update_performance_counters();
coleenp@4037 1328
duke@435 1329 always_do_update_barrier = UseConcMarkSweepGC;
duke@435 1330 };
duke@435 1331
jmasa@698 1332 #ifndef PRODUCT
jmasa@698 1333 class GenGCSaveTopsBeforeGCClosure: public GenCollectedHeap::GenClosure {
jmasa@698 1334 private:
jmasa@698 1335 public:
jmasa@698 1336 void do_generation(Generation* gen) {
jmasa@698 1337 gen->record_spaces_top();
jmasa@698 1338 }
jmasa@698 1339 };
jmasa@698 1340
jmasa@698 1341 void GenCollectedHeap::record_gen_tops_before_GC() {
jmasa@698 1342 if (ZapUnusedHeapArea) {
jmasa@698 1343 GenGCSaveTopsBeforeGCClosure blk;
jmasa@698 1344 generation_iterate(&blk, false); // not old-to-young.
jmasa@698 1345 }
jmasa@698 1346 }
jmasa@698 1347 #endif // not PRODUCT
jmasa@698 1348
duke@435 1349 class GenEnsureParsabilityClosure: public GenCollectedHeap::GenClosure {
duke@435 1350 public:
duke@435 1351 void do_generation(Generation* gen) {
duke@435 1352 gen->ensure_parsability();
duke@435 1353 }
duke@435 1354 };
duke@435 1355
duke@435 1356 void GenCollectedHeap::ensure_parsability(bool retire_tlabs) {
duke@435 1357 CollectedHeap::ensure_parsability(retire_tlabs);
duke@435 1358 GenEnsureParsabilityClosure ep_cl;
duke@435 1359 generation_iterate(&ep_cl, false);
duke@435 1360 }
duke@435 1361
brutisso@5516 1362 oop GenCollectedHeap::handle_failed_promotion(Generation* old_gen,
duke@435 1363 oop obj,
coleenp@548 1364 size_t obj_size) {
brutisso@5516 1365 guarantee(old_gen->level() == 1, "We only get here with an old generation");
duke@435 1366 assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");
duke@435 1367 HeapWord* result = NULL;
duke@435 1368
brutisso@5516 1369 result = old_gen->expand_and_allocate(obj_size, false);
duke@435 1370
duke@435 1371 if (result != NULL) {
duke@435 1372 Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size);
duke@435 1373 }
duke@435 1374 return oop(result);
duke@435 1375 }
duke@435 1376
duke@435 1377 class GenTimeOfLastGCClosure: public GenCollectedHeap::GenClosure {
duke@435 1378 jlong _time; // in ms
duke@435 1379 jlong _now; // in ms
duke@435 1380
duke@435 1381 public:
duke@435 1382 GenTimeOfLastGCClosure(jlong now) : _time(now), _now(now) { }
duke@435 1383
duke@435 1384 jlong time() { return _time; }
duke@435 1385
duke@435 1386 void do_generation(Generation* gen) {
duke@435 1387 _time = MIN2(_time, gen->time_of_last_gc(_now));
duke@435 1388 }
duke@435 1389 };
duke@435 1390
duke@435 1391 jlong GenCollectedHeap::millis_since_last_gc() {
johnc@3339 1392 // We need a monotonically non-deccreasing time in ms but
johnc@3339 1393 // os::javaTimeMillis() does not guarantee monotonicity.
johnc@3339 1394 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
duke@435 1395 GenTimeOfLastGCClosure tolgc_cl(now);
duke@435 1396 // iterate over generations getting the oldest
duke@435 1397 // time that a generation was collected
duke@435 1398 generation_iterate(&tolgc_cl, false);
johnc@3339 1399
johnc@3339 1400 // javaTimeNanos() is guaranteed to be monotonically non-decreasing
johnc@3339 1401 // provided the underlying platform provides such a time source
johnc@3339 1402 // (and it is bug free). So we still have to guard against getting
johnc@3339 1403 // back a time later than 'now'.
duke@435 1404 jlong retVal = now - tolgc_cl.time();
duke@435 1405 if (retVal < 0) {
drchase@6680 1406 NOT_PRODUCT(warning("time warp: "INT64_FORMAT, (int64_t) retVal);)
duke@435 1407 return 0;
duke@435 1408 }
duke@435 1409 return retVal;
duke@435 1410 }

mercurial