src/share/vm/memory/genCollectedHeap.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial