annotate src/share/vm/memory/genCollectedHeap.cpp @ 2297:fe8ab1affd14

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