src/share/vm/memory/defNewGeneration.cpp

Mon, 07 Jul 2014 10:12:40 +0200

author
stefank
date
Mon, 07 Jul 2014 10:12:40 +0200
changeset 6992
2c6ef90f030a
parent 6971
7426d8d76305
child 7031
ee019285a52c
permissions
-rw-r--r--

8049421: G1 Class Unloading after completing a concurrent mark cycle
Reviewed-by: tschatzl, ehelin, brutisso, coleenp, roland, iveresov
Contributed-by: stefan.karlsson@oracle.com, mikael.gerdin@oracle.com

duke@435 1 /*
drchase@6680 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "gc_implementation/shared/collectorCounters.hpp"
stefank@2314 27 #include "gc_implementation/shared/gcPolicyCounters.hpp"
sla@5237 28 #include "gc_implementation/shared/gcHeapSummary.hpp"
sla@5237 29 #include "gc_implementation/shared/gcTimer.hpp"
sla@5237 30 #include "gc_implementation/shared/gcTraceTime.hpp"
sla@5237 31 #include "gc_implementation/shared/gcTrace.hpp"
stefank@2314 32 #include "gc_implementation/shared/spaceDecorator.hpp"
stefank@2314 33 #include "memory/defNewGeneration.inline.hpp"
stefank@2314 34 #include "memory/gcLocker.inline.hpp"
stefank@2314 35 #include "memory/genCollectedHeap.hpp"
stefank@2314 36 #include "memory/genOopClosures.inline.hpp"
coleenp@4037 37 #include "memory/genRemSet.hpp"
stefank@2314 38 #include "memory/generationSpec.hpp"
stefank@2314 39 #include "memory/iterator.hpp"
stefank@2314 40 #include "memory/referencePolicy.hpp"
stefank@2314 41 #include "memory/space.inline.hpp"
stefank@2314 42 #include "oops/instanceRefKlass.hpp"
stefank@2314 43 #include "oops/oop.inline.hpp"
stefank@2314 44 #include "runtime/java.hpp"
goetz@6912 45 #include "runtime/prefetch.inline.hpp"
stefank@4299 46 #include "runtime/thread.inline.hpp"
stefank@2314 47 #include "utilities/copy.hpp"
stefank@2314 48 #include "utilities/stack.inline.hpp"
duke@435 49
drchase@6680 50 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 51
duke@435 52 //
duke@435 53 // DefNewGeneration functions.
duke@435 54
duke@435 55 // Methods of protected closure types.
duke@435 56
duke@435 57 DefNewGeneration::IsAliveClosure::IsAliveClosure(Generation* g) : _g(g) {
duke@435 58 assert(g->level() == 0, "Optimized for youngest gen.");
duke@435 59 }
duke@435 60 bool DefNewGeneration::IsAliveClosure::do_object_b(oop p) {
duke@435 61 return (HeapWord*)p >= _g->reserved().end() || p->is_forwarded();
duke@435 62 }
duke@435 63
duke@435 64 DefNewGeneration::KeepAliveClosure::
duke@435 65 KeepAliveClosure(ScanWeakRefClosure* cl) : _cl(cl) {
duke@435 66 GenRemSet* rs = GenCollectedHeap::heap()->rem_set();
duke@435 67 assert(rs->rs_kind() == GenRemSet::CardTable, "Wrong rem set kind.");
duke@435 68 _rs = (CardTableRS*)rs;
duke@435 69 }
duke@435 70
coleenp@548 71 void DefNewGeneration::KeepAliveClosure::do_oop(oop* p) { DefNewGeneration::KeepAliveClosure::do_oop_work(p); }
coleenp@548 72 void DefNewGeneration::KeepAliveClosure::do_oop(narrowOop* p) { DefNewGeneration::KeepAliveClosure::do_oop_work(p); }
duke@435 73
duke@435 74
duke@435 75 DefNewGeneration::FastKeepAliveClosure::
duke@435 76 FastKeepAliveClosure(DefNewGeneration* g, ScanWeakRefClosure* cl) :
duke@435 77 DefNewGeneration::KeepAliveClosure(cl) {
duke@435 78 _boundary = g->reserved().end();
duke@435 79 }
duke@435 80
coleenp@548 81 void DefNewGeneration::FastKeepAliveClosure::do_oop(oop* p) { DefNewGeneration::FastKeepAliveClosure::do_oop_work(p); }
coleenp@548 82 void DefNewGeneration::FastKeepAliveClosure::do_oop(narrowOop* p) { DefNewGeneration::FastKeepAliveClosure::do_oop_work(p); }
duke@435 83
duke@435 84 DefNewGeneration::EvacuateFollowersClosure::
duke@435 85 EvacuateFollowersClosure(GenCollectedHeap* gch, int level,
duke@435 86 ScanClosure* cur, ScanClosure* older) :
duke@435 87 _gch(gch), _level(level),
duke@435 88 _scan_cur_or_nonheap(cur), _scan_older(older)
duke@435 89 {}
duke@435 90
duke@435 91 void DefNewGeneration::EvacuateFollowersClosure::do_void() {
duke@435 92 do {
duke@435 93 _gch->oop_since_save_marks_iterate(_level, _scan_cur_or_nonheap,
duke@435 94 _scan_older);
duke@435 95 } while (!_gch->no_allocs_since_save_marks(_level));
duke@435 96 }
duke@435 97
duke@435 98 DefNewGeneration::FastEvacuateFollowersClosure::
duke@435 99 FastEvacuateFollowersClosure(GenCollectedHeap* gch, int level,
duke@435 100 DefNewGeneration* gen,
duke@435 101 FastScanClosure* cur, FastScanClosure* older) :
duke@435 102 _gch(gch), _level(level), _gen(gen),
duke@435 103 _scan_cur_or_nonheap(cur), _scan_older(older)
duke@435 104 {}
duke@435 105
duke@435 106 void DefNewGeneration::FastEvacuateFollowersClosure::do_void() {
duke@435 107 do {
duke@435 108 _gch->oop_since_save_marks_iterate(_level, _scan_cur_or_nonheap,
duke@435 109 _scan_older);
duke@435 110 } while (!_gch->no_allocs_since_save_marks(_level));
jcoomes@2191 111 guarantee(_gen->promo_failure_scan_is_complete(), "Failed to finish scan");
duke@435 112 }
duke@435 113
duke@435 114 ScanClosure::ScanClosure(DefNewGeneration* g, bool gc_barrier) :
coleenp@4037 115 OopsInKlassOrGenClosure(g), _g(g), _gc_barrier(gc_barrier)
duke@435 116 {
duke@435 117 assert(_g->level() == 0, "Optimized for youngest generation");
duke@435 118 _boundary = _g->reserved().end();
duke@435 119 }
duke@435 120
coleenp@548 121 void ScanClosure::do_oop(oop* p) { ScanClosure::do_oop_work(p); }
coleenp@548 122 void ScanClosure::do_oop(narrowOop* p) { ScanClosure::do_oop_work(p); }
coleenp@548 123
duke@435 124 FastScanClosure::FastScanClosure(DefNewGeneration* g, bool gc_barrier) :
coleenp@4037 125 OopsInKlassOrGenClosure(g), _g(g), _gc_barrier(gc_barrier)
duke@435 126 {
duke@435 127 assert(_g->level() == 0, "Optimized for youngest generation");
duke@435 128 _boundary = _g->reserved().end();
duke@435 129 }
duke@435 130
coleenp@548 131 void FastScanClosure::do_oop(oop* p) { FastScanClosure::do_oop_work(p); }
coleenp@548 132 void FastScanClosure::do_oop(narrowOop* p) { FastScanClosure::do_oop_work(p); }
coleenp@548 133
coleenp@4037 134 void KlassScanClosure::do_klass(Klass* klass) {
coleenp@4037 135 #ifndef PRODUCT
coleenp@4037 136 if (TraceScavenge) {
coleenp@4037 137 ResourceMark rm;
coleenp@4037 138 gclog_or_tty->print_cr("KlassScanClosure::do_klass %p, %s, dirty: %s",
coleenp@4037 139 klass,
coleenp@4037 140 klass->external_name(),
coleenp@4037 141 klass->has_modified_oops() ? "true" : "false");
coleenp@4037 142 }
coleenp@4037 143 #endif
coleenp@4037 144
coleenp@4037 145 // If the klass has not been dirtied we know that there's
coleenp@4037 146 // no references into the young gen and we can skip it.
coleenp@4037 147 if (klass->has_modified_oops()) {
coleenp@4037 148 if (_accumulate_modified_oops) {
coleenp@4037 149 klass->accumulate_modified_oops();
coleenp@4037 150 }
coleenp@4037 151
coleenp@4037 152 // Clear this state since we're going to scavenge all the metadata.
coleenp@4037 153 klass->clear_modified_oops();
coleenp@4037 154
coleenp@4037 155 // Tell the closure which Klass is being scanned so that it can be dirtied
coleenp@4037 156 // if oops are left pointing into the young gen.
coleenp@4037 157 _scavenge_closure->set_scanned_klass(klass);
coleenp@4037 158
coleenp@4037 159 klass->oops_do(_scavenge_closure);
coleenp@4037 160
coleenp@4037 161 _scavenge_closure->set_scanned_klass(NULL);
coleenp@4037 162 }
coleenp@4037 163 }
coleenp@4037 164
duke@435 165 ScanWeakRefClosure::ScanWeakRefClosure(DefNewGeneration* g) :
coleenp@4037 166 _g(g)
duke@435 167 {
duke@435 168 assert(_g->level() == 0, "Optimized for youngest generation");
duke@435 169 _boundary = _g->reserved().end();
duke@435 170 }
duke@435 171
coleenp@548 172 void ScanWeakRefClosure::do_oop(oop* p) { ScanWeakRefClosure::do_oop_work(p); }
coleenp@548 173 void ScanWeakRefClosure::do_oop(narrowOop* p) { ScanWeakRefClosure::do_oop_work(p); }
coleenp@548 174
coleenp@548 175 void FilteringClosure::do_oop(oop* p) { FilteringClosure::do_oop_work(p); }
coleenp@548 176 void FilteringClosure::do_oop(narrowOop* p) { FilteringClosure::do_oop_work(p); }
duke@435 177
coleenp@4037 178 KlassScanClosure::KlassScanClosure(OopsInKlassOrGenClosure* scavenge_closure,
coleenp@4037 179 KlassRemSet* klass_rem_set)
coleenp@4037 180 : _scavenge_closure(scavenge_closure),
coleenp@4037 181 _accumulate_modified_oops(klass_rem_set->accumulate_modified_oops()) {}
coleenp@4037 182
coleenp@4037 183
duke@435 184 DefNewGeneration::DefNewGeneration(ReservedSpace rs,
duke@435 185 size_t initial_size,
duke@435 186 int level,
duke@435 187 const char* policy)
duke@435 188 : Generation(rs, initial_size, level),
duke@435 189 _promo_failure_drain_in_progress(false),
duke@435 190 _should_allocate_from_space(false)
duke@435 191 {
duke@435 192 MemRegion cmr((HeapWord*)_virtual_space.low(),
duke@435 193 (HeapWord*)_virtual_space.high());
duke@435 194 Universe::heap()->barrier_set()->resize_covered_region(cmr);
duke@435 195
duke@435 196 if (GenCollectedHeap::heap()->collector_policy()->has_soft_ended_eden()) {
duke@435 197 _eden_space = new ConcEdenSpace(this);
duke@435 198 } else {
duke@435 199 _eden_space = new EdenSpace(this);
duke@435 200 }
duke@435 201 _from_space = new ContiguousSpace();
duke@435 202 _to_space = new ContiguousSpace();
duke@435 203
duke@435 204 if (_eden_space == NULL || _from_space == NULL || _to_space == NULL)
duke@435 205 vm_exit_during_initialization("Could not allocate a new gen space");
duke@435 206
duke@435 207 // Compute the maximum eden and survivor space sizes. These sizes
duke@435 208 // are computed assuming the entire reserved space is committed.
duke@435 209 // These values are exported as performance counters.
jwilhelm@6085 210 uintx alignment = GenCollectedHeap::heap()->collector_policy()->space_alignment();
duke@435 211 uintx size = _virtual_space.reserved_size();
duke@435 212 _max_survivor_size = compute_survivor_size(size, alignment);
duke@435 213 _max_eden_size = size - (2*_max_survivor_size);
duke@435 214
duke@435 215 // allocate the performance counters
duke@435 216
duke@435 217 // Generation counters -- generation 0, 3 subspaces
duke@435 218 _gen_counters = new GenerationCounters("new", 0, 3, &_virtual_space);
duke@435 219 _gc_counters = new CollectorCounters(policy, 0);
duke@435 220
duke@435 221 _eden_counters = new CSpaceCounters("eden", 0, _max_eden_size, _eden_space,
duke@435 222 _gen_counters);
duke@435 223 _from_counters = new CSpaceCounters("s0", 1, _max_survivor_size, _from_space,
duke@435 224 _gen_counters);
duke@435 225 _to_counters = new CSpaceCounters("s1", 2, _max_survivor_size, _to_space,
duke@435 226 _gen_counters);
duke@435 227
jmasa@698 228 compute_space_boundaries(0, SpaceDecorator::Clear, SpaceDecorator::Mangle);
duke@435 229 update_counters();
duke@435 230 _next_gen = NULL;
duke@435 231 _tenuring_threshold = MaxTenuringThreshold;
duke@435 232 _pretenure_size_threshold_words = PretenureSizeThreshold >> LogHeapWordSize;
sla@5237 233
sla@5237 234 _gc_timer = new (ResourceObj::C_HEAP, mtGC) STWGCTimer();
duke@435 235 }
duke@435 236
jmasa@698 237 void DefNewGeneration::compute_space_boundaries(uintx minimum_eden_size,
jmasa@698 238 bool clear_space,
jmasa@698 239 bool mangle_space) {
jmasa@698 240 uintx alignment =
jwilhelm@6085 241 GenCollectedHeap::heap()->collector_policy()->space_alignment();
jmasa@698 242
jmasa@698 243 // If the spaces are being cleared (only done at heap initialization
jmasa@698 244 // currently), the survivor spaces need not be empty.
jmasa@698 245 // Otherwise, no care is taken for used areas in the survivor spaces
jmasa@698 246 // so check.
jmasa@698 247 assert(clear_space || (to()->is_empty() && from()->is_empty()),
jmasa@698 248 "Initialization of the survivor spaces assumes these are empty");
duke@435 249
duke@435 250 // Compute sizes
duke@435 251 uintx size = _virtual_space.committed_size();
duke@435 252 uintx survivor_size = compute_survivor_size(size, alignment);
duke@435 253 uintx eden_size = size - (2*survivor_size);
duke@435 254 assert(eden_size > 0 && survivor_size <= eden_size, "just checking");
duke@435 255
duke@435 256 if (eden_size < minimum_eden_size) {
duke@435 257 // May happen due to 64Kb rounding, if so adjust eden size back up
duke@435 258 minimum_eden_size = align_size_up(minimum_eden_size, alignment);
duke@435 259 uintx maximum_survivor_size = (size - minimum_eden_size) / 2;
duke@435 260 uintx unaligned_survivor_size =
duke@435 261 align_size_down(maximum_survivor_size, alignment);
duke@435 262 survivor_size = MAX2(unaligned_survivor_size, alignment);
duke@435 263 eden_size = size - (2*survivor_size);
duke@435 264 assert(eden_size > 0 && survivor_size <= eden_size, "just checking");
duke@435 265 assert(eden_size >= minimum_eden_size, "just checking");
duke@435 266 }
duke@435 267
duke@435 268 char *eden_start = _virtual_space.low();
duke@435 269 char *from_start = eden_start + eden_size;
duke@435 270 char *to_start = from_start + survivor_size;
duke@435 271 char *to_end = to_start + survivor_size;
duke@435 272
duke@435 273 assert(to_end == _virtual_space.high(), "just checking");
duke@435 274 assert(Space::is_aligned((HeapWord*)eden_start), "checking alignment");
duke@435 275 assert(Space::is_aligned((HeapWord*)from_start), "checking alignment");
duke@435 276 assert(Space::is_aligned((HeapWord*)to_start), "checking alignment");
duke@435 277
duke@435 278 MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)from_start);
duke@435 279 MemRegion fromMR((HeapWord*)from_start, (HeapWord*)to_start);
duke@435 280 MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end);
duke@435 281
jmasa@698 282 // A minimum eden size implies that there is a part of eden that
jmasa@698 283 // is being used and that affects the initialization of any
jmasa@698 284 // newly formed eden.
jmasa@698 285 bool live_in_eden = minimum_eden_size > 0;
jmasa@698 286
jmasa@698 287 // If not clearing the spaces, do some checking to verify that
jmasa@698 288 // the space are already mangled.
jmasa@698 289 if (!clear_space) {
jmasa@698 290 // Must check mangling before the spaces are reshaped. Otherwise,
jmasa@698 291 // the bottom or end of one space may have moved into another
jmasa@698 292 // a failure of the check may not correctly indicate which space
jmasa@698 293 // is not properly mangled.
jmasa@698 294 if (ZapUnusedHeapArea) {
jmasa@698 295 HeapWord* limit = (HeapWord*) _virtual_space.high();
jmasa@698 296 eden()->check_mangled_unused_area(limit);
jmasa@698 297 from()->check_mangled_unused_area(limit);
jmasa@698 298 to()->check_mangled_unused_area(limit);
jmasa@698 299 }
jmasa@698 300 }
jmasa@698 301
jmasa@698 302 // Reset the spaces for their new regions.
jmasa@698 303 eden()->initialize(edenMR,
jmasa@698 304 clear_space && !live_in_eden,
jmasa@698 305 SpaceDecorator::Mangle);
jmasa@698 306 // If clear_space and live_in_eden, we will not have cleared any
duke@435 307 // portion of eden above its top. This can cause newly
duke@435 308 // expanded space not to be mangled if using ZapUnusedHeapArea.
duke@435 309 // We explicitly do such mangling here.
jmasa@698 310 if (ZapUnusedHeapArea && clear_space && live_in_eden && mangle_space) {
duke@435 311 eden()->mangle_unused_area();
duke@435 312 }
jmasa@698 313 from()->initialize(fromMR, clear_space, mangle_space);
jmasa@698 314 to()->initialize(toMR, clear_space, mangle_space);
jmasa@698 315
jmasa@698 316 // Set next compaction spaces.
duke@435 317 eden()->set_next_compaction_space(from());
duke@435 318 // The to-space is normally empty before a compaction so need
duke@435 319 // not be considered. The exception is during promotion
duke@435 320 // failure handling when to-space can contain live objects.
duke@435 321 from()->set_next_compaction_space(NULL);
duke@435 322 }
duke@435 323
duke@435 324 void DefNewGeneration::swap_spaces() {
duke@435 325 ContiguousSpace* s = from();
duke@435 326 _from_space = to();
duke@435 327 _to_space = s;
duke@435 328 eden()->set_next_compaction_space(from());
duke@435 329 // The to-space is normally empty before a compaction so need
duke@435 330 // not be considered. The exception is during promotion
duke@435 331 // failure handling when to-space can contain live objects.
duke@435 332 from()->set_next_compaction_space(NULL);
duke@435 333
duke@435 334 if (UsePerfData) {
duke@435 335 CSpaceCounters* c = _from_counters;
duke@435 336 _from_counters = _to_counters;
duke@435 337 _to_counters = c;
duke@435 338 }
duke@435 339 }
duke@435 340
duke@435 341 bool DefNewGeneration::expand(size_t bytes) {
duke@435 342 MutexLocker x(ExpandHeap_lock);
jmasa@698 343 HeapWord* prev_high = (HeapWord*) _virtual_space.high();
duke@435 344 bool success = _virtual_space.expand_by(bytes);
jmasa@698 345 if (success && ZapUnusedHeapArea) {
jmasa@698 346 // Mangle newly committed space immediately because it
jmasa@698 347 // can be done here more simply that after the new
jmasa@698 348 // spaces have been computed.
jmasa@698 349 HeapWord* new_high = (HeapWord*) _virtual_space.high();
jmasa@698 350 MemRegion mangle_region(prev_high, new_high);
jmasa@698 351 SpaceMangler::mangle_region(mangle_region);
jmasa@698 352 }
duke@435 353
duke@435 354 // Do not attempt an expand-to-the reserve size. The
duke@435 355 // request should properly observe the maximum size of
duke@435 356 // the generation so an expand-to-reserve should be
duke@435 357 // unnecessary. Also a second call to expand-to-reserve
duke@435 358 // value potentially can cause an undue expansion.
duke@435 359 // For example if the first expand fail for unknown reasons,
duke@435 360 // but the second succeeds and expands the heap to its maximum
duke@435 361 // value.
duke@435 362 if (GC_locker::is_active()) {
duke@435 363 if (PrintGC && Verbose) {
jmasa@698 364 gclog_or_tty->print_cr("Garbage collection disabled, "
jmasa@698 365 "expanded heap instead");
duke@435 366 }
duke@435 367 }
duke@435 368
duke@435 369 return success;
duke@435 370 }
duke@435 371
duke@435 372
duke@435 373 void DefNewGeneration::compute_new_size() {
duke@435 374 // This is called after a gc that includes the following generation
duke@435 375 // (which is required to exist.) So from-space will normally be empty.
duke@435 376 // Note that we check both spaces, since if scavenge failed they revert roles.
duke@435 377 // If not we bail out (otherwise we would have to relocate the objects)
duke@435 378 if (!from()->is_empty() || !to()->is_empty()) {
duke@435 379 return;
duke@435 380 }
duke@435 381
duke@435 382 int next_level = level() + 1;
duke@435 383 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 384 assert(next_level < gch->_n_gens,
duke@435 385 "DefNewGeneration cannot be an oldest gen");
duke@435 386
duke@435 387 Generation* next_gen = gch->_gens[next_level];
duke@435 388 size_t old_size = next_gen->capacity();
duke@435 389 size_t new_size_before = _virtual_space.committed_size();
duke@435 390 size_t min_new_size = spec()->init_size();
duke@435 391 size_t max_new_size = reserved().byte_size();
duke@435 392 assert(min_new_size <= new_size_before &&
duke@435 393 new_size_before <= max_new_size,
duke@435 394 "just checking");
duke@435 395 // All space sizes must be multiples of Generation::GenGrain.
duke@435 396 size_t alignment = Generation::GenGrain;
duke@435 397
duke@435 398 // Compute desired new generation size based on NewRatio and
duke@435 399 // NewSizeThreadIncrease
duke@435 400 size_t desired_new_size = old_size/NewRatio;
duke@435 401 int threads_count = Threads::number_of_non_daemon_threads();
duke@435 402 size_t thread_increase_size = threads_count * NewSizeThreadIncrease;
duke@435 403 desired_new_size = align_size_up(desired_new_size + thread_increase_size, alignment);
duke@435 404
duke@435 405 // Adjust new generation size
duke@435 406 desired_new_size = MAX2(MIN2(desired_new_size, max_new_size), min_new_size);
duke@435 407 assert(desired_new_size <= max_new_size, "just checking");
duke@435 408
duke@435 409 bool changed = false;
duke@435 410 if (desired_new_size > new_size_before) {
duke@435 411 size_t change = desired_new_size - new_size_before;
duke@435 412 assert(change % alignment == 0, "just checking");
duke@435 413 if (expand(change)) {
duke@435 414 changed = true;
duke@435 415 }
duke@435 416 // If the heap failed to expand to the desired size,
duke@435 417 // "changed" will be false. If the expansion failed
duke@435 418 // (and at this point it was expected to succeed),
duke@435 419 // ignore the failure (leaving "changed" as false).
duke@435 420 }
duke@435 421 if (desired_new_size < new_size_before && eden()->is_empty()) {
duke@435 422 // bail out of shrinking if objects in eden
duke@435 423 size_t change = new_size_before - desired_new_size;
duke@435 424 assert(change % alignment == 0, "just checking");
duke@435 425 _virtual_space.shrink_by(change);
duke@435 426 changed = true;
duke@435 427 }
duke@435 428 if (changed) {
jmasa@698 429 // The spaces have already been mangled at this point but
jmasa@698 430 // may not have been cleared (set top = bottom) and should be.
jmasa@698 431 // Mangling was done when the heap was being expanded.
jmasa@698 432 compute_space_boundaries(eden()->used(),
jmasa@698 433 SpaceDecorator::Clear,
jmasa@698 434 SpaceDecorator::DontMangle);
jmasa@698 435 MemRegion cmr((HeapWord*)_virtual_space.low(),
jmasa@698 436 (HeapWord*)_virtual_space.high());
duke@435 437 Universe::heap()->barrier_set()->resize_covered_region(cmr);
duke@435 438 if (Verbose && PrintGC) {
duke@435 439 size_t new_size_after = _virtual_space.committed_size();
duke@435 440 size_t eden_size_after = eden()->capacity();
duke@435 441 size_t survivor_size_after = from()->capacity();
jmasa@698 442 gclog_or_tty->print("New generation size " SIZE_FORMAT "K->"
jmasa@698 443 SIZE_FORMAT "K [eden="
duke@435 444 SIZE_FORMAT "K,survivor=" SIZE_FORMAT "K]",
jmasa@698 445 new_size_before/K, new_size_after/K,
jmasa@698 446 eden_size_after/K, survivor_size_after/K);
duke@435 447 if (WizardMode) {
duke@435 448 gclog_or_tty->print("[allowed " SIZE_FORMAT "K extra for %d threads]",
duke@435 449 thread_increase_size/K, threads_count);
duke@435 450 }
duke@435 451 gclog_or_tty->cr();
duke@435 452 }
duke@435 453 }
duke@435 454 }
duke@435 455
duke@435 456 void DefNewGeneration::younger_refs_iterate(OopsInGenClosure* cl) {
duke@435 457 assert(false, "NYI -- are you sure you want to call this?");
duke@435 458 }
duke@435 459
duke@435 460
duke@435 461 size_t DefNewGeneration::capacity() const {
duke@435 462 return eden()->capacity()
duke@435 463 + from()->capacity(); // to() is only used during scavenge
duke@435 464 }
duke@435 465
duke@435 466
duke@435 467 size_t DefNewGeneration::used() const {
duke@435 468 return eden()->used()
duke@435 469 + from()->used(); // to() is only used during scavenge
duke@435 470 }
duke@435 471
duke@435 472
duke@435 473 size_t DefNewGeneration::free() const {
duke@435 474 return eden()->free()
duke@435 475 + from()->free(); // to() is only used during scavenge
duke@435 476 }
duke@435 477
duke@435 478 size_t DefNewGeneration::max_capacity() const {
jwilhelm@6085 479 const size_t alignment = GenCollectedHeap::heap()->collector_policy()->space_alignment();
duke@435 480 const size_t reserved_bytes = reserved().byte_size();
duke@435 481 return reserved_bytes - compute_survivor_size(reserved_bytes, alignment);
duke@435 482 }
duke@435 483
duke@435 484 size_t DefNewGeneration::unsafe_max_alloc_nogc() const {
duke@435 485 return eden()->free();
duke@435 486 }
duke@435 487
duke@435 488 size_t DefNewGeneration::capacity_before_gc() const {
duke@435 489 return eden()->capacity();
duke@435 490 }
duke@435 491
duke@435 492 size_t DefNewGeneration::contiguous_available() const {
duke@435 493 return eden()->free();
duke@435 494 }
duke@435 495
duke@435 496
duke@435 497 HeapWord** DefNewGeneration::top_addr() const { return eden()->top_addr(); }
duke@435 498 HeapWord** DefNewGeneration::end_addr() const { return eden()->end_addr(); }
duke@435 499
duke@435 500 void DefNewGeneration::object_iterate(ObjectClosure* blk) {
duke@435 501 eden()->object_iterate(blk);
duke@435 502 from()->object_iterate(blk);
duke@435 503 }
duke@435 504
duke@435 505
duke@435 506 void DefNewGeneration::space_iterate(SpaceClosure* blk,
duke@435 507 bool usedOnly) {
duke@435 508 blk->do_space(eden());
duke@435 509 blk->do_space(from());
duke@435 510 blk->do_space(to());
duke@435 511 }
duke@435 512
duke@435 513 // The last collection bailed out, we are running out of heap space,
duke@435 514 // so we try to allocate the from-space, too.
duke@435 515 HeapWord* DefNewGeneration::allocate_from_space(size_t size) {
duke@435 516 HeapWord* result = NULL;
ysr@2336 517 if (Verbose && PrintGCDetails) {
duke@435 518 gclog_or_tty->print("DefNewGeneration::allocate_from_space(%u):"
ysr@2336 519 " will_fail: %s"
ysr@2336 520 " heap_lock: %s"
ysr@2336 521 " free: " SIZE_FORMAT,
ysr@2336 522 size,
ysr@2336 523 GenCollectedHeap::heap()->incremental_collection_will_fail(false /* don't consult_young */) ?
ysr@2336 524 "true" : "false",
ysr@2336 525 Heap_lock->is_locked() ? "locked" : "unlocked",
ysr@2336 526 from()->free());
ysr@2336 527 }
duke@435 528 if (should_allocate_from_space() || GC_locker::is_active_and_needs_gc()) {
duke@435 529 if (Heap_lock->owned_by_self() ||
duke@435 530 (SafepointSynchronize::is_at_safepoint() &&
duke@435 531 Thread::current()->is_VM_thread())) {
duke@435 532 // If the Heap_lock is not locked by this thread, this will be called
duke@435 533 // again later with the Heap_lock held.
duke@435 534 result = from()->allocate(size);
duke@435 535 } else if (PrintGC && Verbose) {
duke@435 536 gclog_or_tty->print_cr(" Heap_lock is not owned by self");
duke@435 537 }
duke@435 538 } else if (PrintGC && Verbose) {
duke@435 539 gclog_or_tty->print_cr(" should_allocate_from_space: NOT");
duke@435 540 }
duke@435 541 if (PrintGC && Verbose) {
duke@435 542 gclog_or_tty->print_cr(" returns %s", result == NULL ? "NULL" : "object");
duke@435 543 }
duke@435 544 return result;
duke@435 545 }
duke@435 546
duke@435 547 HeapWord* DefNewGeneration::expand_and_allocate(size_t size,
duke@435 548 bool is_tlab,
duke@435 549 bool parallel) {
duke@435 550 // We don't attempt to expand the young generation (but perhaps we should.)
duke@435 551 return allocate(size, is_tlab);
duke@435 552 }
duke@435 553
brutisso@4452 554 void DefNewGeneration::adjust_desired_tenuring_threshold() {
brutisso@4452 555 // Set the desired survivor size to half the real survivor space
brutisso@4452 556 _tenuring_threshold =
brutisso@4452 557 age_table()->compute_tenuring_threshold(to()->capacity()/HeapWordSize);
brutisso@4452 558 }
duke@435 559
duke@435 560 void DefNewGeneration::collect(bool full,
duke@435 561 bool clear_all_soft_refs,
duke@435 562 size_t size,
duke@435 563 bool is_tlab) {
duke@435 564 assert(full || size > 0, "otherwise we don't want to collect");
sla@5237 565
duke@435 566 GenCollectedHeap* gch = GenCollectedHeap::heap();
sla@5237 567
mgronlun@6131 568 _gc_timer->register_gc_start();
sla@5237 569 DefNewTracer gc_tracer;
sla@5237 570 gc_tracer.report_gc_start(gch->gc_cause(), _gc_timer->gc_start());
sla@5237 571
duke@435 572 _next_gen = gch->next_gen(this);
duke@435 573
sla@5237 574 // If the next generation is too full to accommodate promotion
duke@435 575 // from this generation, pass on collection; let the next generation
duke@435 576 // do it.
duke@435 577 if (!collection_attempt_is_safe()) {
ysr@2336 578 if (Verbose && PrintGCDetails) {
ysr@2336 579 gclog_or_tty->print(" :: Collection attempt not safe :: ");
ysr@2336 580 }
ysr@2243 581 gch->set_incremental_collection_failed(); // Slight lie: we did not even attempt one
duke@435 582 return;
duke@435 583 }
duke@435 584 assert(to()->is_empty(), "Else not collection_attempt_is_safe");
duke@435 585
duke@435 586 init_assuming_no_promotion_failure();
duke@435 587
brutisso@6904 588 GCTraceTime t1(GCCauseString("GC", gch->gc_cause()), PrintGC && !PrintGCDetails, true, NULL, gc_tracer.gc_id());
duke@435 589 // Capture heap used before collection (for printing).
duke@435 590 size_t gch_prev_used = gch->used();
duke@435 591
sla@5237 592 gch->trace_heap_before_gc(&gc_tracer);
sla@5237 593
duke@435 594 SpecializationStats::clear();
duke@435 595
duke@435 596 // These can be shared for all code paths
duke@435 597 IsAliveClosure is_alive(this);
duke@435 598 ScanWeakRefClosure scan_weak_ref(this);
duke@435 599
duke@435 600 age_table()->clear();
jmasa@698 601 to()->clear(SpaceDecorator::Mangle);
duke@435 602
duke@435 603 gch->rem_set()->prepare_for_younger_refs_iterate(false);
duke@435 604
duke@435 605 assert(gch->no_allocs_since_save_marks(0),
duke@435 606 "save marks have not been newly set.");
duke@435 607
duke@435 608 // Not very pretty.
duke@435 609 CollectorPolicy* cp = gch->collector_policy();
duke@435 610
duke@435 611 FastScanClosure fsc_with_no_gc_barrier(this, false);
duke@435 612 FastScanClosure fsc_with_gc_barrier(this, true);
duke@435 613
coleenp@4037 614 KlassScanClosure klass_scan_closure(&fsc_with_no_gc_barrier,
coleenp@4037 615 gch->rem_set()->klass_rem_set());
stefank@6992 616 CLDToKlassAndOopClosure cld_scan_closure(&klass_scan_closure,
stefank@6992 617 &fsc_with_no_gc_barrier,
stefank@6992 618 false);
coleenp@4037 619
duke@435 620 set_promo_failure_scan_stack_closure(&fsc_with_no_gc_barrier);
duke@435 621 FastEvacuateFollowersClosure evacuate_followers(gch, _level, this,
duke@435 622 &fsc_with_no_gc_barrier,
duke@435 623 &fsc_with_gc_barrier);
duke@435 624
duke@435 625 assert(gch->no_allocs_since_save_marks(0),
duke@435 626 "save marks have not been newly set.");
duke@435 627
stefank@6992 628 gch->gen_process_roots(_level,
stefank@6992 629 true, // Process younger gens, if any,
stefank@6992 630 // as strong roots.
stefank@6992 631 true, // activate StrongRootsScope
stefank@6992 632 SharedHeap::SO_ScavengeCodeCache,
stefank@6992 633 GenCollectedHeap::StrongAndWeakRoots,
stefank@6992 634 &fsc_with_no_gc_barrier,
stefank@6992 635 &fsc_with_gc_barrier,
stefank@6992 636 &cld_scan_closure);
duke@435 637
duke@435 638 // "evacuate followers".
duke@435 639 evacuate_followers.do_void();
duke@435 640
duke@435 641 FastKeepAliveClosure keep_alive(this, &scan_weak_ref);
ysr@888 642 ReferenceProcessor* rp = ref_processor();
ysr@892 643 rp->setup_policy(clear_all_soft_refs);
sla@5237 644 const ReferenceProcessorStats& stats =
ysr@888 645 rp->process_discovered_references(&is_alive, &keep_alive, &evacuate_followers,
brutisso@6904 646 NULL, _gc_timer, gc_tracer.gc_id());
sla@5237 647 gc_tracer.report_gc_reference_stats(stats);
sla@5237 648
sla@5237 649 if (!_promotion_failed) {
duke@435 650 // Swap the survivor spaces.
jmasa@698 651 eden()->clear(SpaceDecorator::Mangle);
jmasa@698 652 from()->clear(SpaceDecorator::Mangle);
jmasa@698 653 if (ZapUnusedHeapArea) {
jmasa@698 654 // This is now done here because of the piece-meal mangling which
jmasa@698 655 // can check for valid mangling at intermediate points in the
jmasa@698 656 // collection(s). When a minor collection fails to collect
jmasa@698 657 // sufficient space resizing of the young generation can occur
jmasa@698 658 // an redistribute the spaces in the young generation. Mangle
jmasa@698 659 // here so that unzapped regions don't get distributed to
jmasa@698 660 // other spaces.
jmasa@698 661 to()->mangle_unused_area();
jmasa@698 662 }
duke@435 663 swap_spaces();
duke@435 664
duke@435 665 assert(to()->is_empty(), "to space should be empty now");
duke@435 666
brutisso@4452 667 adjust_desired_tenuring_threshold();
duke@435 668
jmasa@1822 669 // A successful scavenge should restart the GC time limit count which is
jmasa@1822 670 // for full GC's.
jmasa@1822 671 AdaptiveSizePolicy* size_policy = gch->gen_policy()->size_policy();
jmasa@1822 672 size_policy->reset_gc_overhead_limit_count();
duke@435 673 if (PrintGC && !PrintGCDetails) {
duke@435 674 gch->print_heap_change(gch_prev_used);
duke@435 675 }
ysr@2243 676 assert(!gch->incremental_collection_failed(), "Should be clear");
duke@435 677 } else {
jcoomes@2191 678 assert(_promo_failure_scan_stack.is_empty(), "post condition");
jcoomes@2191 679 _promo_failure_scan_stack.clear(true); // Clear cached segments.
duke@435 680
duke@435 681 remove_forwarding_pointers();
duke@435 682 if (PrintGCDetails) {
ysr@1580 683 gclog_or_tty->print(" (promotion failed) ");
duke@435 684 }
duke@435 685 // Add to-space to the list of space to compact
duke@435 686 // when a promotion failure has occurred. In that
duke@435 687 // case there can be live objects in to-space
duke@435 688 // as a result of a partial evacuation of eden
duke@435 689 // and from-space.
jcoomes@2191 690 swap_spaces(); // For uniformity wrt ParNewGeneration.
duke@435 691 from()->set_next_compaction_space(to());
ysr@2243 692 gch->set_incremental_collection_failed();
duke@435 693
ysr@1580 694 // Inform the next generation that a promotion failure occurred.
ysr@1580 695 _next_gen->promotion_failure_occurred();
sla@5237 696 gc_tracer.report_promotion_failed(_promotion_failed_info);
ysr@1580 697
duke@435 698 // Reset the PromotionFailureALot counters.
duke@435 699 NOT_PRODUCT(Universe::heap()->reset_promotion_should_fail();)
duke@435 700 }
duke@435 701 // set new iteration safe limit for the survivor spaces
duke@435 702 from()->set_concurrent_iteration_safe_limit(from()->top());
duke@435 703 to()->set_concurrent_iteration_safe_limit(to()->top());
duke@435 704 SpecializationStats::print();
johnc@3538 705
sla@5237 706 // We need to use a monotonically non-decreasing time in ms
johnc@3538 707 // or we will see time-warp warnings and os::javaTimeMillis()
johnc@3538 708 // does not guarantee monotonicity.
johnc@3538 709 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
johnc@3538 710 update_time_of_last_gc(now);
sla@5237 711
sla@5237 712 gch->trace_heap_after_gc(&gc_tracer);
sla@5237 713 gc_tracer.report_tenuring_threshold(tenuring_threshold());
sla@5237 714
mgronlun@6131 715 _gc_timer->register_gc_end();
sla@5237 716
sla@5237 717 gc_tracer.report_gc_end(_gc_timer->gc_end(), _gc_timer->time_partitions());
duke@435 718 }
duke@435 719
duke@435 720 class RemoveForwardPointerClosure: public ObjectClosure {
duke@435 721 public:
duke@435 722 void do_object(oop obj) {
duke@435 723 obj->init_mark();
duke@435 724 }
duke@435 725 };
duke@435 726
duke@435 727 void DefNewGeneration::init_assuming_no_promotion_failure() {
duke@435 728 _promotion_failed = false;
sla@5237 729 _promotion_failed_info.reset();
duke@435 730 from()->set_next_compaction_space(NULL);
duke@435 731 }
duke@435 732
duke@435 733 void DefNewGeneration::remove_forwarding_pointers() {
duke@435 734 RemoveForwardPointerClosure rspc;
duke@435 735 eden()->object_iterate(&rspc);
duke@435 736 from()->object_iterate(&rspc);
jcoomes@2191 737
duke@435 738 // Now restore saved marks, if any.
jcoomes@2191 739 assert(_objs_with_preserved_marks.size() == _preserved_marks_of_objs.size(),
jcoomes@2191 740 "should be the same");
jcoomes@2191 741 while (!_objs_with_preserved_marks.is_empty()) {
jcoomes@2191 742 oop obj = _objs_with_preserved_marks.pop();
jcoomes@2191 743 markOop m = _preserved_marks_of_objs.pop();
jcoomes@2191 744 obj->set_mark(m);
duke@435 745 }
jcoomes@2191 746 _objs_with_preserved_marks.clear(true);
jcoomes@2191 747 _preserved_marks_of_objs.clear(true);
duke@435 748 }
duke@435 749
ysr@2380 750 void DefNewGeneration::preserve_mark(oop obj, markOop m) {
sla@5237 751 assert(_promotion_failed && m->must_be_preserved_for_promotion_failure(obj),
ysr@2380 752 "Oversaving!");
ysr@2380 753 _objs_with_preserved_marks.push(obj);
ysr@2380 754 _preserved_marks_of_objs.push(m);
ysr@2380 755 }
ysr@2380 756
duke@435 757 void DefNewGeneration::preserve_mark_if_necessary(oop obj, markOop m) {
duke@435 758 if (m->must_be_preserved_for_promotion_failure(obj)) {
ysr@2380 759 preserve_mark(obj, m);
duke@435 760 }
duke@435 761 }
duke@435 762
duke@435 763 void DefNewGeneration::handle_promotion_failure(oop old) {
ysr@2380 764 if (PrintPromotionFailure && !_promotion_failed) {
ysr@1580 765 gclog_or_tty->print(" (promotion failure size = " SIZE_FORMAT ") ",
ysr@1580 766 old->size());
ysr@1580 767 }
ysr@2380 768 _promotion_failed = true;
sla@5237 769 _promotion_failed_info.register_copy_failure(old->size());
ysr@2380 770 preserve_mark_if_necessary(old, old->mark());
duke@435 771 // forward to self
duke@435 772 old->forward_to(old);
duke@435 773
jcoomes@2191 774 _promo_failure_scan_stack.push(old);
duke@435 775
duke@435 776 if (!_promo_failure_drain_in_progress) {
duke@435 777 // prevent recursion in copy_to_survivor_space()
duke@435 778 _promo_failure_drain_in_progress = true;
duke@435 779 drain_promo_failure_scan_stack();
duke@435 780 _promo_failure_drain_in_progress = false;
duke@435 781 }
duke@435 782 }
duke@435 783
coleenp@548 784 oop DefNewGeneration::copy_to_survivor_space(oop old) {
duke@435 785 assert(is_in_reserved(old) && !old->is_forwarded(),
duke@435 786 "shouldn't be scavenging this oop");
duke@435 787 size_t s = old->size();
duke@435 788 oop obj = NULL;
duke@435 789
duke@435 790 // Try allocating obj in to-space (unless too old)
duke@435 791 if (old->age() < tenuring_threshold()) {
duke@435 792 obj = (oop) to()->allocate(s);
duke@435 793 }
duke@435 794
duke@435 795 // Otherwise try allocating obj tenured
duke@435 796 if (obj == NULL) {
coleenp@548 797 obj = _next_gen->promote(old, s);
duke@435 798 if (obj == NULL) {
duke@435 799 handle_promotion_failure(old);
duke@435 800 return old;
duke@435 801 }
duke@435 802 } else {
duke@435 803 // Prefetch beyond obj
duke@435 804 const intx interval = PrefetchCopyIntervalInBytes;
duke@435 805 Prefetch::write(obj, interval);
duke@435 806
duke@435 807 // Copy obj
duke@435 808 Copy::aligned_disjoint_words((HeapWord*)old, (HeapWord*)obj, s);
duke@435 809
duke@435 810 // Increment age if obj still in new generation
duke@435 811 obj->incr_age();
duke@435 812 age_table()->add(obj, s);
duke@435 813 }
duke@435 814
duke@435 815 // Done, insert forward pointer to obj in this header
duke@435 816 old->forward_to(obj);
duke@435 817
duke@435 818 return obj;
duke@435 819 }
duke@435 820
duke@435 821 void DefNewGeneration::drain_promo_failure_scan_stack() {
jcoomes@2191 822 while (!_promo_failure_scan_stack.is_empty()) {
jcoomes@2191 823 oop obj = _promo_failure_scan_stack.pop();
duke@435 824 obj->oop_iterate(_promo_failure_scan_stack_closure);
duke@435 825 }
duke@435 826 }
duke@435 827
duke@435 828 void DefNewGeneration::save_marks() {
duke@435 829 eden()->set_saved_mark();
duke@435 830 to()->set_saved_mark();
duke@435 831 from()->set_saved_mark();
duke@435 832 }
duke@435 833
duke@435 834
duke@435 835 void DefNewGeneration::reset_saved_marks() {
duke@435 836 eden()->reset_saved_mark();
duke@435 837 to()->reset_saved_mark();
duke@435 838 from()->reset_saved_mark();
duke@435 839 }
duke@435 840
duke@435 841
duke@435 842 bool DefNewGeneration::no_allocs_since_save_marks() {
duke@435 843 assert(eden()->saved_mark_at_top(), "Violated spec - alloc in eden");
duke@435 844 assert(from()->saved_mark_at_top(), "Violated spec - alloc in from");
duke@435 845 return to()->saved_mark_at_top();
duke@435 846 }
duke@435 847
duke@435 848 #define DefNew_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \
duke@435 849 \
duke@435 850 void DefNewGeneration:: \
duke@435 851 oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl) { \
duke@435 852 cl->set_generation(this); \
duke@435 853 eden()->oop_since_save_marks_iterate##nv_suffix(cl); \
duke@435 854 to()->oop_since_save_marks_iterate##nv_suffix(cl); \
duke@435 855 from()->oop_since_save_marks_iterate##nv_suffix(cl); \
duke@435 856 cl->reset_generation(); \
duke@435 857 save_marks(); \
duke@435 858 }
duke@435 859
duke@435 860 ALL_SINCE_SAVE_MARKS_CLOSURES(DefNew_SINCE_SAVE_MARKS_DEFN)
duke@435 861
duke@435 862 #undef DefNew_SINCE_SAVE_MARKS_DEFN
duke@435 863
duke@435 864 void DefNewGeneration::contribute_scratch(ScratchBlock*& list, Generation* requestor,
duke@435 865 size_t max_alloc_words) {
duke@435 866 if (requestor == this || _promotion_failed) return;
duke@435 867 assert(requestor->level() > level(), "DefNewGeneration must be youngest");
duke@435 868
duke@435 869 /* $$$ Assert this? "trace" is a "MarkSweep" function so that's not appropriate.
duke@435 870 if (to_space->top() > to_space->bottom()) {
duke@435 871 trace("to_space not empty when contribute_scratch called");
duke@435 872 }
duke@435 873 */
duke@435 874
duke@435 875 ContiguousSpace* to_space = to();
duke@435 876 assert(to_space->end() >= to_space->top(), "pointers out of order");
duke@435 877 size_t free_words = pointer_delta(to_space->end(), to_space->top());
duke@435 878 if (free_words >= MinFreeScratchWords) {
duke@435 879 ScratchBlock* sb = (ScratchBlock*)to_space->top();
duke@435 880 sb->num_words = free_words;
duke@435 881 sb->next = list;
duke@435 882 list = sb;
duke@435 883 }
duke@435 884 }
duke@435 885
jmasa@698 886 void DefNewGeneration::reset_scratch() {
jmasa@698 887 // If contributing scratch in to_space, mangle all of
jmasa@698 888 // to_space if ZapUnusedHeapArea. This is needed because
jmasa@698 889 // top is not maintained while using to-space as scratch.
jmasa@698 890 if (ZapUnusedHeapArea) {
jmasa@698 891 to()->mangle_unused_area_complete();
jmasa@698 892 }
jmasa@698 893 }
jmasa@698 894
duke@435 895 bool DefNewGeneration::collection_attempt_is_safe() {
duke@435 896 if (!to()->is_empty()) {
ysr@2336 897 if (Verbose && PrintGCDetails) {
ysr@2336 898 gclog_or_tty->print(" :: to is not empty :: ");
ysr@2336 899 }
duke@435 900 return false;
duke@435 901 }
duke@435 902 if (_next_gen == NULL) {
duke@435 903 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 904 _next_gen = gch->next_gen(this);
duke@435 905 }
ysr@2243 906 return _next_gen->promotion_attempt_is_safe(used());
duke@435 907 }
duke@435 908
duke@435 909 void DefNewGeneration::gc_epilogue(bool full) {
ysr@2244 910 DEBUG_ONLY(static bool seen_incremental_collection_failed = false;)
ysr@2244 911
ysr@2244 912 assert(!GC_locker::is_active(), "We should not be executing here");
duke@435 913 // Check if the heap is approaching full after a collection has
duke@435 914 // been done. Generally the young generation is empty at
duke@435 915 // a minimum at the end of a collection. If it is not, then
duke@435 916 // the heap is approaching full.
duke@435 917 GenCollectedHeap* gch = GenCollectedHeap::heap();
ysr@2243 918 if (full) {
ysr@2244 919 DEBUG_ONLY(seen_incremental_collection_failed = false;)
ysr@2336 920 if (!collection_attempt_is_safe() && !_eden_space->is_empty()) {
ysr@2336 921 if (Verbose && PrintGCDetails) {
ysr@2336 922 gclog_or_tty->print("DefNewEpilogue: cause(%s), full, not safe, set_failed, set_alloc_from, clear_seen",
ysr@2336 923 GCCause::to_string(gch->gc_cause()));
ysr@2336 924 }
ysr@2243 925 gch->set_incremental_collection_failed(); // Slight lie: a full gc left us in that state
ysr@2243 926 set_should_allocate_from_space(); // we seem to be running out of space
ysr@2243 927 } else {
ysr@2336 928 if (Verbose && PrintGCDetails) {
ysr@2336 929 gclog_or_tty->print("DefNewEpilogue: cause(%s), full, safe, clear_failed, clear_alloc_from, clear_seen",
ysr@2336 930 GCCause::to_string(gch->gc_cause()));
ysr@2336 931 }
ysr@2243 932 gch->clear_incremental_collection_failed(); // We just did a full collection
ysr@2243 933 clear_should_allocate_from_space(); // if set
ysr@2243 934 }
duke@435 935 } else {
ysr@2244 936 #ifdef ASSERT
ysr@2244 937 // It is possible that incremental_collection_failed() == true
ysr@2244 938 // here, because an attempted scavenge did not succeed. The policy
ysr@2244 939 // is normally expected to cause a full collection which should
ysr@2244 940 // clear that condition, so we should not be here twice in a row
ysr@2244 941 // with incremental_collection_failed() == true without having done
ysr@2244 942 // a full collection in between.
ysr@2244 943 if (!seen_incremental_collection_failed &&
ysr@2244 944 gch->incremental_collection_failed()) {
ysr@2336 945 if (Verbose && PrintGCDetails) {
ysr@2336 946 gclog_or_tty->print("DefNewEpilogue: cause(%s), not full, not_seen_failed, failed, set_seen_failed",
ysr@2336 947 GCCause::to_string(gch->gc_cause()));
ysr@2336 948 }
ysr@2244 949 seen_incremental_collection_failed = true;
ysr@2244 950 } else if (seen_incremental_collection_failed) {
ysr@2336 951 if (Verbose && PrintGCDetails) {
ysr@2336 952 gclog_or_tty->print("DefNewEpilogue: cause(%s), not full, seen_failed, will_clear_seen_failed",
ysr@2336 953 GCCause::to_string(gch->gc_cause()));
ysr@2336 954 }
ysr@2336 955 assert(gch->gc_cause() == GCCause::_scavenge_alot ||
ysr@2336 956 (gch->gc_cause() == GCCause::_java_lang_system_gc && UseConcMarkSweepGC && ExplicitGCInvokesConcurrent) ||
ysr@2336 957 !gch->incremental_collection_failed(),
ysr@2295 958 "Twice in a row");
ysr@2244 959 seen_incremental_collection_failed = false;
ysr@2244 960 }
ysr@2244 961 #endif // ASSERT
duke@435 962 }
duke@435 963
jmasa@698 964 if (ZapUnusedHeapArea) {
jmasa@698 965 eden()->check_mangled_unused_area_complete();
jmasa@698 966 from()->check_mangled_unused_area_complete();
jmasa@698 967 to()->check_mangled_unused_area_complete();
jmasa@698 968 }
jmasa@698 969
jcoomes@2996 970 if (!CleanChunkPoolAsync) {
jcoomes@2996 971 Chunk::clean_chunk_pool();
jcoomes@2996 972 }
jcoomes@2996 973
duke@435 974 // update the generation and space performance counters
duke@435 975 update_counters();
duke@435 976 gch->collector_policy()->counters()->update_counters();
duke@435 977 }
duke@435 978
jmasa@698 979 void DefNewGeneration::record_spaces_top() {
jmasa@698 980 assert(ZapUnusedHeapArea, "Not mangling unused space");
jmasa@698 981 eden()->set_top_for_allocations();
jmasa@698 982 to()->set_top_for_allocations();
jmasa@698 983 from()->set_top_for_allocations();
jmasa@698 984 }
jmasa@698 985
sla@5237 986 void DefNewGeneration::ref_processor_init() {
sla@5237 987 Generation::ref_processor_init();
sla@5237 988 }
sla@5237 989
jmasa@698 990
duke@435 991 void DefNewGeneration::update_counters() {
duke@435 992 if (UsePerfData) {
duke@435 993 _eden_counters->update_all();
duke@435 994 _from_counters->update_all();
duke@435 995 _to_counters->update_all();
duke@435 996 _gen_counters->update_all();
duke@435 997 }
duke@435 998 }
duke@435 999
brutisso@3711 1000 void DefNewGeneration::verify() {
brutisso@3711 1001 eden()->verify();
brutisso@3711 1002 from()->verify();
brutisso@3711 1003 to()->verify();
duke@435 1004 }
duke@435 1005
duke@435 1006 void DefNewGeneration::print_on(outputStream* st) const {
duke@435 1007 Generation::print_on(st);
duke@435 1008 st->print(" eden");
duke@435 1009 eden()->print_on(st);
duke@435 1010 st->print(" from");
duke@435 1011 from()->print_on(st);
duke@435 1012 st->print(" to ");
duke@435 1013 to()->print_on(st);
duke@435 1014 }
duke@435 1015
duke@435 1016
duke@435 1017 const char* DefNewGeneration::name() const {
duke@435 1018 return "def new generation";
duke@435 1019 }
coleenp@548 1020
coleenp@548 1021 // Moved from inline file as they are not called inline
coleenp@548 1022 CompactibleSpace* DefNewGeneration::first_compaction_space() const {
coleenp@548 1023 return eden();
coleenp@548 1024 }
coleenp@548 1025
coleenp@548 1026 HeapWord* DefNewGeneration::allocate(size_t word_size,
coleenp@548 1027 bool is_tlab) {
coleenp@548 1028 // This is the slow-path allocation for the DefNewGeneration.
coleenp@548 1029 // Most allocations are fast-path in compiled code.
coleenp@548 1030 // We try to allocate from the eden. If that works, we are happy.
coleenp@548 1031 // Note that since DefNewGeneration supports lock-free allocation, we
coleenp@548 1032 // have to use it here, as well.
coleenp@548 1033 HeapWord* result = eden()->par_allocate(word_size);
coleenp@548 1034 if (result != NULL) {
jmasa@5459 1035 if (CMSEdenChunksRecordAlways && _next_gen != NULL) {
jmasa@5459 1036 _next_gen->sample_eden_chunk();
jmasa@5459 1037 }
coleenp@548 1038 return result;
coleenp@548 1039 }
coleenp@548 1040 do {
coleenp@548 1041 HeapWord* old_limit = eden()->soft_end();
coleenp@548 1042 if (old_limit < eden()->end()) {
coleenp@548 1043 // Tell the next generation we reached a limit.
coleenp@548 1044 HeapWord* new_limit =
coleenp@548 1045 next_gen()->allocation_limit_reached(eden(), eden()->top(), word_size);
coleenp@548 1046 if (new_limit != NULL) {
coleenp@548 1047 Atomic::cmpxchg_ptr(new_limit, eden()->soft_end_addr(), old_limit);
coleenp@548 1048 } else {
coleenp@548 1049 assert(eden()->soft_end() == eden()->end(),
coleenp@548 1050 "invalid state after allocation_limit_reached returned null");
coleenp@548 1051 }
coleenp@548 1052 } else {
coleenp@548 1053 // The allocation failed and the soft limit is equal to the hard limit,
coleenp@548 1054 // there are no reasons to do an attempt to allocate
coleenp@548 1055 assert(old_limit == eden()->end(), "sanity check");
coleenp@548 1056 break;
coleenp@548 1057 }
coleenp@548 1058 // Try to allocate until succeeded or the soft limit can't be adjusted
coleenp@548 1059 result = eden()->par_allocate(word_size);
coleenp@548 1060 } while (result == NULL);
coleenp@548 1061
coleenp@548 1062 // If the eden is full and the last collection bailed out, we are running
coleenp@548 1063 // out of heap space, and we try to allocate the from-space, too.
coleenp@548 1064 // allocate_from_space can't be inlined because that would introduce a
coleenp@548 1065 // circular dependency at compile time.
coleenp@548 1066 if (result == NULL) {
coleenp@548 1067 result = allocate_from_space(word_size);
jmasa@5459 1068 } else if (CMSEdenChunksRecordAlways && _next_gen != NULL) {
jmasa@5459 1069 _next_gen->sample_eden_chunk();
coleenp@548 1070 }
coleenp@548 1071 return result;
coleenp@548 1072 }
coleenp@548 1073
coleenp@548 1074 HeapWord* DefNewGeneration::par_allocate(size_t word_size,
coleenp@548 1075 bool is_tlab) {
jmasa@5459 1076 HeapWord* res = eden()->par_allocate(word_size);
jmasa@5459 1077 if (CMSEdenChunksRecordAlways && _next_gen != NULL) {
jmasa@5459 1078 _next_gen->sample_eden_chunk();
jmasa@5459 1079 }
jmasa@5459 1080 return res;
coleenp@548 1081 }
coleenp@548 1082
coleenp@548 1083 void DefNewGeneration::gc_prologue(bool full) {
coleenp@548 1084 // Ensure that _end and _soft_end are the same in eden space.
coleenp@548 1085 eden()->set_soft_end(eden()->end());
coleenp@548 1086 }
coleenp@548 1087
coleenp@548 1088 size_t DefNewGeneration::tlab_capacity() const {
coleenp@548 1089 return eden()->capacity();
coleenp@548 1090 }
coleenp@548 1091
brutisso@6376 1092 size_t DefNewGeneration::tlab_used() const {
brutisso@6376 1093 return eden()->used();
brutisso@6376 1094 }
brutisso@6376 1095
coleenp@548 1096 size_t DefNewGeneration::unsafe_max_tlab_alloc() const {
coleenp@548 1097 return unsafe_max_alloc_nogc();
coleenp@548 1098 }

mercurial