annotate src/share/vm/gc_implementation/parallelScavenge/psYoungGen.cpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 183f41cf8bfe
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 2001-2007 Sun Microsystems, Inc. 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 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 # include "incls/_precompiled.incl"
duke@0 26 # include "incls/_psYoungGen.cpp.incl"
duke@0 27
duke@0 28 PSYoungGen::PSYoungGen(size_t initial_size,
duke@0 29 size_t min_size,
duke@0 30 size_t max_size) :
duke@0 31 _init_gen_size(initial_size),
duke@0 32 _min_gen_size(min_size),
duke@0 33 _max_gen_size(max_size)
duke@0 34 {}
duke@0 35
duke@0 36 void PSYoungGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) {
duke@0 37 assert(_init_gen_size != 0, "Should have a finite size");
duke@0 38 _virtual_space = new PSVirtualSpace(rs, alignment);
duke@0 39 if (!_virtual_space->expand_by(_init_gen_size)) {
duke@0 40 vm_exit_during_initialization("Could not reserve enough space for "
duke@0 41 "object heap");
duke@0 42 }
duke@0 43 }
duke@0 44
duke@0 45 void PSYoungGen::initialize(ReservedSpace rs, size_t alignment) {
duke@0 46 initialize_virtual_space(rs, alignment);
duke@0 47 initialize_work();
duke@0 48 }
duke@0 49
duke@0 50 void PSYoungGen::initialize_work() {
duke@0 51
duke@0 52 _reserved = MemRegion((HeapWord*)_virtual_space->low_boundary(),
duke@0 53 (HeapWord*)_virtual_space->high_boundary());
duke@0 54
duke@0 55 MemRegion cmr((HeapWord*)_virtual_space->low(),
duke@0 56 (HeapWord*)_virtual_space->high());
duke@0 57 Universe::heap()->barrier_set()->resize_covered_region(cmr);
duke@0 58
duke@0 59 if (UseNUMA) {
duke@0 60 _eden_space = new MutableNUMASpace();
duke@0 61 } else {
duke@0 62 _eden_space = new MutableSpace();
duke@0 63 }
duke@0 64 _from_space = new MutableSpace();
duke@0 65 _to_space = new MutableSpace();
duke@0 66
duke@0 67 if (_eden_space == NULL || _from_space == NULL || _to_space == NULL) {
duke@0 68 vm_exit_during_initialization("Could not allocate a young gen space");
duke@0 69 }
duke@0 70
duke@0 71 // Allocate the mark sweep views of spaces
duke@0 72 _eden_mark_sweep =
duke@0 73 new PSMarkSweepDecorator(_eden_space, NULL, MarkSweepDeadRatio);
duke@0 74 _from_mark_sweep =
duke@0 75 new PSMarkSweepDecorator(_from_space, NULL, MarkSweepDeadRatio);
duke@0 76 _to_mark_sweep =
duke@0 77 new PSMarkSweepDecorator(_to_space, NULL, MarkSweepDeadRatio);
duke@0 78
duke@0 79 if (_eden_mark_sweep == NULL ||
duke@0 80 _from_mark_sweep == NULL ||
duke@0 81 _to_mark_sweep == NULL) {
duke@0 82 vm_exit_during_initialization("Could not complete allocation"
duke@0 83 " of the young generation");
duke@0 84 }
duke@0 85
duke@0 86 // Generation Counters - generation 0, 3 subspaces
duke@0 87 _gen_counters = new PSGenerationCounters("new", 0, 3, _virtual_space);
duke@0 88
duke@0 89 // Compute maximum space sizes for performance counters
duke@0 90 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@0 91 size_t alignment = heap->intra_generation_alignment();
duke@0 92 size_t size = _virtual_space->reserved_size();
duke@0 93
duke@0 94 size_t max_survivor_size;
duke@0 95 size_t max_eden_size;
duke@0 96
duke@0 97 if (UseAdaptiveSizePolicy) {
duke@0 98 max_survivor_size = size / MinSurvivorRatio;
duke@0 99
duke@0 100 // round the survivor space size down to the nearest alignment
duke@0 101 // and make sure its size is greater than 0.
duke@0 102 max_survivor_size = align_size_down(max_survivor_size, alignment);
duke@0 103 max_survivor_size = MAX2(max_survivor_size, alignment);
duke@0 104
duke@0 105 // set the maximum size of eden to be the size of the young gen
duke@0 106 // less two times the minimum survivor size. The minimum survivor
duke@0 107 // size for UseAdaptiveSizePolicy is one alignment.
duke@0 108 max_eden_size = size - 2 * alignment;
duke@0 109 } else {
duke@0 110 max_survivor_size = size / InitialSurvivorRatio;
duke@0 111
duke@0 112 // round the survivor space size down to the nearest alignment
duke@0 113 // and make sure its size is greater than 0.
duke@0 114 max_survivor_size = align_size_down(max_survivor_size, alignment);
duke@0 115 max_survivor_size = MAX2(max_survivor_size, alignment);
duke@0 116
duke@0 117 // set the maximum size of eden to be the size of the young gen
duke@0 118 // less two times the survivor size when the generation is 100%
duke@0 119 // committed. The minimum survivor size for -UseAdaptiveSizePolicy
duke@0 120 // is dependent on the committed portion (current capacity) of the
duke@0 121 // generation - the less space committed, the smaller the survivor
duke@0 122 // space, possibly as small as an alignment. However, we are interested
duke@0 123 // in the case where the young generation is 100% committed, as this
duke@0 124 // is the point where eden reachs its maximum size. At this point,
duke@0 125 // the size of a survivor space is max_survivor_size.
duke@0 126 max_eden_size = size - 2 * max_survivor_size;
duke@0 127 }
duke@0 128
duke@0 129 _eden_counters = new SpaceCounters("eden", 0, max_eden_size, _eden_space,
duke@0 130 _gen_counters);
duke@0 131 _from_counters = new SpaceCounters("s0", 1, max_survivor_size, _from_space,
duke@0 132 _gen_counters);
duke@0 133 _to_counters = new SpaceCounters("s1", 2, max_survivor_size, _to_space,
duke@0 134 _gen_counters);
duke@0 135
duke@0 136 compute_initial_space_boundaries();
duke@0 137 }
duke@0 138
duke@0 139 void PSYoungGen::compute_initial_space_boundaries() {
duke@0 140 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@0 141 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@0 142
duke@0 143 // Compute sizes
duke@0 144 size_t alignment = heap->intra_generation_alignment();
duke@0 145 size_t size = _virtual_space->committed_size();
duke@0 146
duke@0 147 size_t survivor_size = size / InitialSurvivorRatio;
duke@0 148 survivor_size = align_size_down(survivor_size, alignment);
duke@0 149 // ... but never less than an alignment
duke@0 150 survivor_size = MAX2(survivor_size, alignment);
duke@0 151
duke@0 152 // Young generation is eden + 2 survivor spaces
duke@0 153 size_t eden_size = size - (2 * survivor_size);
duke@0 154
duke@0 155 // Now go ahead and set 'em.
duke@0 156 set_space_boundaries(eden_size, survivor_size);
duke@0 157 space_invariants();
duke@0 158
duke@0 159 if (UsePerfData) {
duke@0 160 _eden_counters->update_capacity();
duke@0 161 _from_counters->update_capacity();
duke@0 162 _to_counters->update_capacity();
duke@0 163 }
duke@0 164 }
duke@0 165
duke@0 166 void PSYoungGen::set_space_boundaries(size_t eden_size, size_t survivor_size) {
duke@0 167 assert(eden_size < _virtual_space->committed_size(), "just checking");
duke@0 168 assert(eden_size > 0 && survivor_size > 0, "just checking");
duke@0 169
duke@0 170 // Initial layout is Eden, to, from. After swapping survivor spaces,
duke@0 171 // that leaves us with Eden, from, to, which is step one in our two
duke@0 172 // step resize-with-live-data procedure.
duke@0 173 char *eden_start = _virtual_space->low();
duke@0 174 char *to_start = eden_start + eden_size;
duke@0 175 char *from_start = to_start + survivor_size;
duke@0 176 char *from_end = from_start + survivor_size;
duke@0 177
duke@0 178 assert(from_end == _virtual_space->high(), "just checking");
duke@0 179 assert(is_object_aligned((intptr_t)eden_start), "checking alignment");
duke@0 180 assert(is_object_aligned((intptr_t)to_start), "checking alignment");
duke@0 181 assert(is_object_aligned((intptr_t)from_start), "checking alignment");
duke@0 182
duke@0 183 MemRegion eden_mr((HeapWord*)eden_start, (HeapWord*)to_start);
duke@0 184 MemRegion to_mr ((HeapWord*)to_start, (HeapWord*)from_start);
duke@0 185 MemRegion from_mr((HeapWord*)from_start, (HeapWord*)from_end);
duke@0 186
duke@0 187 eden_space()->initialize(eden_mr, true);
duke@0 188 to_space()->initialize(to_mr , true);
duke@0 189 from_space()->initialize(from_mr, true);
duke@0 190 }
duke@0 191
duke@0 192 #ifndef PRODUCT
duke@0 193 void PSYoungGen::space_invariants() {
duke@0 194 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@0 195 const size_t alignment = heap->intra_generation_alignment();
duke@0 196
duke@0 197 // Currently, our eden size cannot shrink to zero
duke@0 198 guarantee(eden_space()->capacity_in_bytes() >= alignment, "eden too small");
duke@0 199 guarantee(from_space()->capacity_in_bytes() >= alignment, "from too small");
duke@0 200 guarantee(to_space()->capacity_in_bytes() >= alignment, "to too small");
duke@0 201
duke@0 202 // Relationship of spaces to each other
duke@0 203 char* eden_start = (char*)eden_space()->bottom();
duke@0 204 char* eden_end = (char*)eden_space()->end();
duke@0 205 char* from_start = (char*)from_space()->bottom();
duke@0 206 char* from_end = (char*)from_space()->end();
duke@0 207 char* to_start = (char*)to_space()->bottom();
duke@0 208 char* to_end = (char*)to_space()->end();
duke@0 209
duke@0 210 guarantee(eden_start >= _virtual_space->low(), "eden bottom");
duke@0 211 guarantee(eden_start < eden_end, "eden space consistency");
duke@0 212 guarantee(from_start < from_end, "from space consistency");
duke@0 213 guarantee(to_start < to_end, "to space consistency");
duke@0 214
duke@0 215 // Check whether from space is below to space
duke@0 216 if (from_start < to_start) {
duke@0 217 // Eden, from, to
duke@0 218 guarantee(eden_end <= from_start, "eden/from boundary");
duke@0 219 guarantee(from_end <= to_start, "from/to boundary");
duke@0 220 guarantee(to_end <= _virtual_space->high(), "to end");
duke@0 221 } else {
duke@0 222 // Eden, to, from
duke@0 223 guarantee(eden_end <= to_start, "eden/to boundary");
duke@0 224 guarantee(to_end <= from_start, "to/from boundary");
duke@0 225 guarantee(from_end <= _virtual_space->high(), "from end");
duke@0 226 }
duke@0 227
duke@0 228 // More checks that the virtual space is consistent with the spaces
duke@0 229 assert(_virtual_space->committed_size() >=
duke@0 230 (eden_space()->capacity_in_bytes() +
duke@0 231 to_space()->capacity_in_bytes() +
duke@0 232 from_space()->capacity_in_bytes()), "Committed size is inconsistent");
duke@0 233 assert(_virtual_space->committed_size() <= _virtual_space->reserved_size(),
duke@0 234 "Space invariant");
duke@0 235 char* eden_top = (char*)eden_space()->top();
duke@0 236 char* from_top = (char*)from_space()->top();
duke@0 237 char* to_top = (char*)to_space()->top();
duke@0 238 assert(eden_top <= _virtual_space->high(), "eden top");
duke@0 239 assert(from_top <= _virtual_space->high(), "from top");
duke@0 240 assert(to_top <= _virtual_space->high(), "to top");
duke@0 241
duke@0 242 _virtual_space->verify();
duke@0 243 }
duke@0 244 #endif
duke@0 245
duke@0 246 void PSYoungGen::resize(size_t eden_size, size_t survivor_size) {
duke@0 247 // Resize the generation if needed. If the generation resize
duke@0 248 // reports false, do not attempt to resize the spaces.
duke@0 249 if (resize_generation(eden_size, survivor_size)) {
duke@0 250 // Then we lay out the spaces inside the generation
duke@0 251 resize_spaces(eden_size, survivor_size);
duke@0 252
duke@0 253 space_invariants();
duke@0 254
duke@0 255 if (PrintAdaptiveSizePolicy && Verbose) {
duke@0 256 gclog_or_tty->print_cr("Young generation size: "
duke@0 257 "desired eden: " SIZE_FORMAT " survivor: " SIZE_FORMAT
duke@0 258 " used: " SIZE_FORMAT " capacity: " SIZE_FORMAT
duke@0 259 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,
duke@0 260 eden_size, survivor_size, used_in_bytes(), capacity_in_bytes(),
duke@0 261 _max_gen_size, min_gen_size());
duke@0 262 }
duke@0 263 }
duke@0 264 }
duke@0 265
duke@0 266
duke@0 267 bool PSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) {
duke@0 268 const size_t alignment = _virtual_space->alignment();
duke@0 269 size_t orig_size = _virtual_space->committed_size();
duke@0 270 bool size_changed = false;
duke@0 271
duke@0 272 // There used to be this guarantee there.
duke@0 273 // guarantee ((eden_size + 2*survivor_size) <= _max_gen_size, "incorrect input arguments");
duke@0 274 // Code below forces this requirement. In addition the desired eden
duke@0 275 // size and disired survivor sizes are desired goals and may
duke@0 276 // exceed the total generation size.
duke@0 277
duke@0 278 assert(min_gen_size() <= orig_size && orig_size <= max_size(), "just checking");
duke@0 279
duke@0 280 // Adjust new generation size
duke@0 281 const size_t eden_plus_survivors =
duke@0 282 align_size_up(eden_size + 2 * survivor_size, alignment);
duke@0 283 size_t desired_size = MAX2(MIN2(eden_plus_survivors, max_size()),
duke@0 284 min_gen_size());
duke@0 285 assert(desired_size <= max_size(), "just checking");
duke@0 286
duke@0 287 if (desired_size > orig_size) {
duke@0 288 // Grow the generation
duke@0 289 size_t change = desired_size - orig_size;
duke@0 290 assert(change % alignment == 0, "just checking");
duke@0 291 if (!_virtual_space->expand_by(change)) {
duke@0 292 return false; // Error if we fail to resize!
duke@0 293 }
duke@0 294
duke@0 295 size_changed = true;
duke@0 296 } else if (desired_size < orig_size) {
duke@0 297 size_t desired_change = orig_size - desired_size;
duke@0 298 assert(desired_change % alignment == 0, "just checking");
duke@0 299
duke@0 300 desired_change = limit_gen_shrink(desired_change);
duke@0 301
duke@0 302 if (desired_change > 0) {
duke@0 303 virtual_space()->shrink_by(desired_change);
duke@0 304 reset_survivors_after_shrink();
duke@0 305
duke@0 306 size_changed = true;
duke@0 307 }
duke@0 308 } else {
duke@0 309 if (Verbose && PrintGC) {
duke@0 310 if (orig_size == gen_size_limit()) {
duke@0 311 gclog_or_tty->print_cr("PSYoung generation size at maximum: "
duke@0 312 SIZE_FORMAT "K", orig_size/K);
duke@0 313 } else if (orig_size == min_gen_size()) {
duke@0 314 gclog_or_tty->print_cr("PSYoung generation size at minium: "
duke@0 315 SIZE_FORMAT "K", orig_size/K);
duke@0 316 }
duke@0 317 }
duke@0 318 }
duke@0 319
duke@0 320 if (size_changed) {
duke@0 321 post_resize();
duke@0 322
duke@0 323 if (Verbose && PrintGC) {
duke@0 324 size_t current_size = _virtual_space->committed_size();
duke@0 325 gclog_or_tty->print_cr("PSYoung generation size changed: "
duke@0 326 SIZE_FORMAT "K->" SIZE_FORMAT "K",
duke@0 327 orig_size/K, current_size/K);
duke@0 328 }
duke@0 329 }
duke@0 330
duke@0 331 guarantee(eden_plus_survivors <= _virtual_space->committed_size() ||
duke@0 332 _virtual_space->committed_size() == max_size(), "Sanity");
duke@0 333
duke@0 334 return true;
duke@0 335 }
duke@0 336
duke@0 337
duke@0 338 void PSYoungGen::resize_spaces(size_t requested_eden_size,
duke@0 339 size_t requested_survivor_size) {
duke@0 340 assert(UseAdaptiveSizePolicy, "sanity check");
duke@0 341 assert(requested_eden_size > 0 && requested_survivor_size > 0,
duke@0 342 "just checking");
duke@0 343
duke@0 344 // We require eden and to space to be empty
duke@0 345 if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) {
duke@0 346 return;
duke@0 347 }
duke@0 348
duke@0 349 if (PrintAdaptiveSizePolicy && Verbose) {
duke@0 350 gclog_or_tty->print_cr("PSYoungGen::resize_spaces(requested_eden_size: "
duke@0 351 SIZE_FORMAT
duke@0 352 ", requested_survivor_size: " SIZE_FORMAT ")",
duke@0 353 requested_eden_size, requested_survivor_size);
duke@0 354 gclog_or_tty->print_cr(" eden: [" PTR_FORMAT ".." PTR_FORMAT ") "
duke@0 355 SIZE_FORMAT,
duke@0 356 eden_space()->bottom(),
duke@0 357 eden_space()->end(),
duke@0 358 pointer_delta(eden_space()->end(),
duke@0 359 eden_space()->bottom(),
duke@0 360 sizeof(char)));
duke@0 361 gclog_or_tty->print_cr(" from: [" PTR_FORMAT ".." PTR_FORMAT ") "
duke@0 362 SIZE_FORMAT,
duke@0 363 from_space()->bottom(),
duke@0 364 from_space()->end(),
duke@0 365 pointer_delta(from_space()->end(),
duke@0 366 from_space()->bottom(),
duke@0 367 sizeof(char)));
duke@0 368 gclog_or_tty->print_cr(" to: [" PTR_FORMAT ".." PTR_FORMAT ") "
duke@0 369 SIZE_FORMAT,
duke@0 370 to_space()->bottom(),
duke@0 371 to_space()->end(),
duke@0 372 pointer_delta( to_space()->end(),
duke@0 373 to_space()->bottom(),
duke@0 374 sizeof(char)));
duke@0 375 }
duke@0 376
duke@0 377 // There's nothing to do if the new sizes are the same as the current
duke@0 378 if (requested_survivor_size == to_space()->capacity_in_bytes() &&
duke@0 379 requested_survivor_size == from_space()->capacity_in_bytes() &&
duke@0 380 requested_eden_size == eden_space()->capacity_in_bytes()) {
duke@0 381 if (PrintAdaptiveSizePolicy && Verbose) {
duke@0 382 gclog_or_tty->print_cr(" capacities are the right sizes, returning");
duke@0 383 }
duke@0 384 return;
duke@0 385 }
duke@0 386
duke@0 387 char* eden_start = (char*)eden_space()->bottom();
duke@0 388 char* eden_end = (char*)eden_space()->end();
duke@0 389 char* from_start = (char*)from_space()->bottom();
duke@0 390 char* from_end = (char*)from_space()->end();
duke@0 391 char* to_start = (char*)to_space()->bottom();
duke@0 392 char* to_end = (char*)to_space()->end();
duke@0 393
duke@0 394 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@0 395 const size_t alignment = heap->intra_generation_alignment();
duke@0 396 const bool maintain_minimum =
duke@0 397 (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size();
duke@0 398
duke@0 399 // Check whether from space is below to space
duke@0 400 if (from_start < to_start) {
duke@0 401 // Eden, from, to
duke@0 402 if (PrintAdaptiveSizePolicy && Verbose) {
duke@0 403 gclog_or_tty->print_cr(" Eden, from, to:");
duke@0 404 }
duke@0 405
duke@0 406 // Set eden
duke@0 407 // "requested_eden_size" is a goal for the size of eden
duke@0 408 // and may not be attainable. "eden_size" below is
duke@0 409 // calculated based on the location of from-space and
duke@0 410 // the goal for the size of eden. from-space is
duke@0 411 // fixed in place because it contains live data.
duke@0 412 // The calculation is done this way to avoid 32bit
duke@0 413 // overflow (i.e., eden_start + requested_eden_size
duke@0 414 // may too large for representation in 32bits).
duke@0 415 size_t eden_size;
duke@0 416 if (maintain_minimum) {
duke@0 417 // Only make eden larger than the requested size if
duke@0 418 // the minimum size of the generation has to be maintained.
duke@0 419 // This could be done in general but policy at a higher
duke@0 420 // level is determining a requested size for eden and that
duke@0 421 // should be honored unless there is a fundamental reason.
duke@0 422 eden_size = pointer_delta(from_start,
duke@0 423 eden_start,
duke@0 424 sizeof(char));
duke@0 425 } else {
duke@0 426 eden_size = MIN2(requested_eden_size,
duke@0 427 pointer_delta(from_start, eden_start, sizeof(char)));
duke@0 428 }
duke@0 429
duke@0 430 eden_end = eden_start + eden_size;
duke@0 431 assert(eden_end >= eden_start, "addition overflowed")
duke@0 432
duke@0 433 // To may resize into from space as long as it is clear of live data.
duke@0 434 // From space must remain page aligned, though, so we need to do some
duke@0 435 // extra calculations.
duke@0 436
duke@0 437 // First calculate an optimal to-space
duke@0 438 to_end = (char*)_virtual_space->high();
duke@0 439 to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size,
duke@0 440 sizeof(char));
duke@0 441
duke@0 442 // Does the optimal to-space overlap from-space?
duke@0 443 if (to_start < (char*)from_space()->end()) {
duke@0 444 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@0 445
duke@0 446 // Calculate the minimum offset possible for from_end
duke@0 447 size_t from_size = pointer_delta(from_space()->top(), from_start, sizeof(char));
duke@0 448
duke@0 449 // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME!
duke@0 450 if (from_size == 0) {
duke@0 451 from_size = alignment;
duke@0 452 } else {
duke@0 453 from_size = align_size_up(from_size, alignment);
duke@0 454 }
duke@0 455
duke@0 456 from_end = from_start + from_size;
duke@0 457 assert(from_end > from_start, "addition overflow or from_size problem");
duke@0 458
duke@0 459 guarantee(from_end <= (char*)from_space()->end(), "from_end moved to the right");
duke@0 460
duke@0 461 // Now update to_start with the new from_end
duke@0 462 to_start = MAX2(from_end, to_start);
duke@0 463 }
duke@0 464
duke@0 465 guarantee(to_start != to_end, "to space is zero sized");
duke@0 466
duke@0 467 if (PrintAdaptiveSizePolicy && Verbose) {
duke@0 468 gclog_or_tty->print_cr(" [eden_start .. eden_end): "
duke@0 469 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
duke@0 470 eden_start,
duke@0 471 eden_end,
duke@0 472 pointer_delta(eden_end, eden_start, sizeof(char)));
duke@0 473 gclog_or_tty->print_cr(" [from_start .. from_end): "
duke@0 474 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
duke@0 475 from_start,
duke@0 476 from_end,
duke@0 477 pointer_delta(from_end, from_start, sizeof(char)));
duke@0 478 gclog_or_tty->print_cr(" [ to_start .. to_end): "
duke@0 479 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
duke@0 480 to_start,
duke@0 481 to_end,
duke@0 482 pointer_delta( to_end, to_start, sizeof(char)));
duke@0 483 }
duke@0 484 } else {
duke@0 485 // Eden, to, from
duke@0 486 if (PrintAdaptiveSizePolicy && Verbose) {
duke@0 487 gclog_or_tty->print_cr(" Eden, to, from:");
duke@0 488 }
duke@0 489
duke@0 490 // To space gets priority over eden resizing. Note that we position
duke@0 491 // to space as if we were able to resize from space, even though from
duke@0 492 // space is not modified.
duke@0 493 // Giving eden priority was tried and gave poorer performance.
duke@0 494 to_end = (char*)pointer_delta(_virtual_space->high(),
duke@0 495 (char*)requested_survivor_size,
duke@0 496 sizeof(char));
duke@0 497 to_end = MIN2(to_end, from_start);
duke@0 498 to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size,
duke@0 499 sizeof(char));
duke@0 500 // if the space sizes are to be increased by several times then
duke@0 501 // 'to_start' will point beyond the young generation. In this case
duke@0 502 // 'to_start' should be adjusted.
duke@0 503 to_start = MAX2(to_start, eden_start + alignment);
duke@0 504
duke@0 505 // Compute how big eden can be, then adjust end.
duke@0 506 // See comments above on calculating eden_end.
duke@0 507 size_t eden_size;
duke@0 508 if (maintain_minimum) {
duke@0 509 eden_size = pointer_delta(to_start, eden_start, sizeof(char));
duke@0 510 } else {
duke@0 511 eden_size = MIN2(requested_eden_size,
duke@0 512 pointer_delta(to_start, eden_start, sizeof(char)));
duke@0 513 }
duke@0 514 eden_end = eden_start + eden_size;
duke@0 515 assert(eden_end >= eden_start, "addition overflowed")
duke@0 516
duke@0 517 // Could choose to not let eden shrink
duke@0 518 // to_start = MAX2(to_start, eden_end);
duke@0 519
duke@0 520 // Don't let eden shrink down to 0 or less.
duke@0 521 eden_end = MAX2(eden_end, eden_start + alignment);
duke@0 522 to_start = MAX2(to_start, eden_end);
duke@0 523
duke@0 524 if (PrintAdaptiveSizePolicy && Verbose) {
duke@0 525 gclog_or_tty->print_cr(" [eden_start .. eden_end): "
duke@0 526 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
duke@0 527 eden_start,
duke@0 528 eden_end,
duke@0 529 pointer_delta(eden_end, eden_start, sizeof(char)));
duke@0 530 gclog_or_tty->print_cr(" [ to_start .. to_end): "
duke@0 531 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
duke@0 532 to_start,
duke@0 533 to_end,
duke@0 534 pointer_delta( to_end, to_start, sizeof(char)));
duke@0 535 gclog_or_tty->print_cr(" [from_start .. from_end): "
duke@0 536 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
duke@0 537 from_start,
duke@0 538 from_end,
duke@0 539 pointer_delta(from_end, from_start, sizeof(char)));
duke@0 540 }
duke@0 541 }
duke@0 542
duke@0 543
duke@0 544 guarantee((HeapWord*)from_start <= from_space()->bottom(),
duke@0 545 "from start moved to the right");
duke@0 546 guarantee((HeapWord*)from_end >= from_space()->top(),
duke@0 547 "from end moved into live data");
duke@0 548 assert(is_object_aligned((intptr_t)eden_start), "checking alignment");
duke@0 549 assert(is_object_aligned((intptr_t)from_start), "checking alignment");
duke@0 550 assert(is_object_aligned((intptr_t)to_start), "checking alignment");
duke@0 551
duke@0 552 MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end);
duke@0 553 MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end);
duke@0 554 MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end);
duke@0 555
duke@0 556 // Let's make sure the call to initialize doesn't reset "top"!
duke@0 557 HeapWord* old_from_top = from_space()->top();
duke@0 558
duke@0 559 // For PrintAdaptiveSizePolicy block below
duke@0 560 size_t old_from = from_space()->capacity_in_bytes();
duke@0 561 size_t old_to = to_space()->capacity_in_bytes();
duke@0 562
duke@0 563 eden_space()->initialize(edenMR, true);
duke@0 564 to_space()->initialize(toMR , true);
duke@0 565 from_space()->initialize(fromMR, false); // Note, not cleared!
duke@0 566
duke@0 567 assert(from_space()->top() == old_from_top, "from top changed!");
duke@0 568
duke@0 569 if (PrintAdaptiveSizePolicy) {
duke@0 570 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@0 571 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@0 572
duke@0 573 gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: "
duke@0 574 "collection: %d "
duke@0 575 "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> "
duke@0 576 "(" SIZE_FORMAT ", " SIZE_FORMAT ") ",
duke@0 577 heap->total_collections(),
duke@0 578 old_from, old_to,
duke@0 579 from_space()->capacity_in_bytes(),
duke@0 580 to_space()->capacity_in_bytes());
duke@0 581 gclog_or_tty->cr();
duke@0 582 }
duke@0 583 }
duke@0 584
duke@0 585 void PSYoungGen::swap_spaces() {
duke@0 586 MutableSpace* s = from_space();
duke@0 587 _from_space = to_space();
duke@0 588 _to_space = s;
duke@0 589
duke@0 590 // Now update the decorators.
duke@0 591 PSMarkSweepDecorator* md = from_mark_sweep();
duke@0 592 _from_mark_sweep = to_mark_sweep();
duke@0 593 _to_mark_sweep = md;
duke@0 594
duke@0 595 assert(from_mark_sweep()->space() == from_space(), "Sanity");
duke@0 596 assert(to_mark_sweep()->space() == to_space(), "Sanity");
duke@0 597 }
duke@0 598
duke@0 599 size_t PSYoungGen::capacity_in_bytes() const {
duke@0 600 return eden_space()->capacity_in_bytes()
duke@0 601 + from_space()->capacity_in_bytes(); // to_space() is only used during scavenge
duke@0 602 }
duke@0 603
duke@0 604
duke@0 605 size_t PSYoungGen::used_in_bytes() const {
duke@0 606 return eden_space()->used_in_bytes()
duke@0 607 + from_space()->used_in_bytes(); // to_space() is only used during scavenge
duke@0 608 }
duke@0 609
duke@0 610
duke@0 611 size_t PSYoungGen::free_in_bytes() const {
duke@0 612 return eden_space()->free_in_bytes()
duke@0 613 + from_space()->free_in_bytes(); // to_space() is only used during scavenge
duke@0 614 }
duke@0 615
duke@0 616 size_t PSYoungGen::capacity_in_words() const {
duke@0 617 return eden_space()->capacity_in_words()
duke@0 618 + from_space()->capacity_in_words(); // to_space() is only used during scavenge
duke@0 619 }
duke@0 620
duke@0 621
duke@0 622 size_t PSYoungGen::used_in_words() const {
duke@0 623 return eden_space()->used_in_words()
duke@0 624 + from_space()->used_in_words(); // to_space() is only used during scavenge
duke@0 625 }
duke@0 626
duke@0 627
duke@0 628 size_t PSYoungGen::free_in_words() const {
duke@0 629 return eden_space()->free_in_words()
duke@0 630 + from_space()->free_in_words(); // to_space() is only used during scavenge
duke@0 631 }
duke@0 632
duke@0 633 void PSYoungGen::object_iterate(ObjectClosure* blk) {
duke@0 634 eden_space()->object_iterate(blk);
duke@0 635 from_space()->object_iterate(blk);
duke@0 636 to_space()->object_iterate(blk);
duke@0 637 }
duke@0 638
duke@0 639 void PSYoungGen::precompact() {
duke@0 640 eden_mark_sweep()->precompact();
duke@0 641 from_mark_sweep()->precompact();
duke@0 642 to_mark_sweep()->precompact();
duke@0 643 }
duke@0 644
duke@0 645 void PSYoungGen::adjust_pointers() {
duke@0 646 eden_mark_sweep()->adjust_pointers();
duke@0 647 from_mark_sweep()->adjust_pointers();
duke@0 648 to_mark_sweep()->adjust_pointers();
duke@0 649 }
duke@0 650
duke@0 651 void PSYoungGen::compact() {
duke@0 652 eden_mark_sweep()->compact(ZapUnusedHeapArea);
duke@0 653 from_mark_sweep()->compact(ZapUnusedHeapArea);
duke@0 654 // Mark sweep stores preserved markOops in to space, don't disturb!
duke@0 655 to_mark_sweep()->compact(false);
duke@0 656 }
duke@0 657
duke@0 658 void PSYoungGen::move_and_update(ParCompactionManager* cm) {
duke@0 659 PSParallelCompact::move_and_update(cm, PSParallelCompact::eden_space_id);
duke@0 660 PSParallelCompact::move_and_update(cm, PSParallelCompact::from_space_id);
duke@0 661 PSParallelCompact::move_and_update(cm, PSParallelCompact::to_space_id);
duke@0 662 }
duke@0 663
duke@0 664 void PSYoungGen::print() const { print_on(tty); }
duke@0 665 void PSYoungGen::print_on(outputStream* st) const {
duke@0 666 st->print(" %-15s", "PSYoungGen");
duke@0 667 if (PrintGCDetails && Verbose) {
duke@0 668 st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT,
duke@0 669 capacity_in_bytes(), used_in_bytes());
duke@0 670 } else {
duke@0 671 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
duke@0 672 capacity_in_bytes()/K, used_in_bytes()/K);
duke@0 673 }
duke@0 674 _virtual_space->print_space_boundaries_on(st);
duke@0 675 st->print(" eden"); eden_space()->print_on(st);
duke@0 676 st->print(" from"); from_space()->print_on(st);
duke@0 677 st->print(" to "); to_space()->print_on(st);
duke@0 678 }
duke@0 679
duke@0 680 void PSYoungGen::print_used_change(size_t prev_used) const {
duke@0 681 gclog_or_tty->print(" [%s:", name());
duke@0 682 gclog_or_tty->print(" " SIZE_FORMAT "K"
duke@0 683 "->" SIZE_FORMAT "K"
duke@0 684 "(" SIZE_FORMAT "K)",
duke@0 685 prev_used / K, used_in_bytes() / K,
duke@0 686 capacity_in_bytes() / K);
duke@0 687 gclog_or_tty->print("]");
duke@0 688 }
duke@0 689
duke@0 690 size_t PSYoungGen::available_for_expansion() {
duke@0 691 ShouldNotReachHere();
duke@0 692 return 0;
duke@0 693 }
duke@0 694
duke@0 695 size_t PSYoungGen::available_for_contraction() {
duke@0 696 ShouldNotReachHere();
duke@0 697 return 0;
duke@0 698 }
duke@0 699
duke@0 700 size_t PSYoungGen::available_to_min_gen() {
duke@0 701 assert(virtual_space()->committed_size() >= min_gen_size(), "Invariant");
duke@0 702 return virtual_space()->committed_size() - min_gen_size();
duke@0 703 }
duke@0 704
duke@0 705 // This method assumes that from-space has live data and that
duke@0 706 // any shrinkage of the young gen is limited by location of
duke@0 707 // from-space.
duke@0 708 size_t PSYoungGen::available_to_live() {
duke@0 709 size_t delta_in_survivor = 0;
duke@0 710 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@0 711 const size_t space_alignment = heap->intra_generation_alignment();
duke@0 712 const size_t gen_alignment = heap->young_gen_alignment();
duke@0 713
duke@0 714 MutableSpace* space_shrinking = NULL;
duke@0 715 if (from_space()->end() > to_space()->end()) {
duke@0 716 space_shrinking = from_space();
duke@0 717 } else {
duke@0 718 space_shrinking = to_space();
duke@0 719 }
duke@0 720
duke@0 721 // Include any space that is committed but not included in
duke@0 722 // the survivor spaces.
duke@0 723 assert(((HeapWord*)virtual_space()->high()) >= space_shrinking->end(),
duke@0 724 "Survivor space beyond high end");
duke@0 725 size_t unused_committed = pointer_delta(virtual_space()->high(),
duke@0 726 space_shrinking->end(), sizeof(char));
duke@0 727
duke@0 728 if (space_shrinking->is_empty()) {
duke@0 729 // Don't let the space shrink to 0
duke@0 730 assert(space_shrinking->capacity_in_bytes() >= space_alignment,
duke@0 731 "Space is too small");
duke@0 732 delta_in_survivor = space_shrinking->capacity_in_bytes() - space_alignment;
duke@0 733 } else {
duke@0 734 delta_in_survivor = pointer_delta(space_shrinking->end(),
duke@0 735 space_shrinking->top(),
duke@0 736 sizeof(char));
duke@0 737 }
duke@0 738
duke@0 739 size_t delta_in_bytes = unused_committed + delta_in_survivor;
duke@0 740 delta_in_bytes = align_size_down(delta_in_bytes, gen_alignment);
duke@0 741 return delta_in_bytes;
duke@0 742 }
duke@0 743
duke@0 744 // Return the number of bytes available for resizing down the young
duke@0 745 // generation. This is the minimum of
duke@0 746 // input "bytes"
duke@0 747 // bytes to the minimum young gen size
duke@0 748 // bytes to the size currently being used + some small extra
duke@0 749 size_t PSYoungGen::limit_gen_shrink(size_t bytes) {
duke@0 750 // Allow shrinkage into the current eden but keep eden large enough
duke@0 751 // to maintain the minimum young gen size
duke@0 752 bytes = MIN3(bytes, available_to_min_gen(), available_to_live());
duke@0 753 return align_size_down(bytes, virtual_space()->alignment());
duke@0 754 }
duke@0 755
duke@0 756 void PSYoungGen::reset_after_change() {
duke@0 757 ShouldNotReachHere();
duke@0 758 }
duke@0 759
duke@0 760 void PSYoungGen::reset_survivors_after_shrink() {
duke@0 761 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(),
duke@0 762 (HeapWord*)virtual_space()->high_boundary());
duke@0 763 PSScavenge::reference_processor()->set_span(_reserved);
duke@0 764
duke@0 765 MutableSpace* space_shrinking = NULL;
duke@0 766 if (from_space()->end() > to_space()->end()) {
duke@0 767 space_shrinking = from_space();
duke@0 768 } else {
duke@0 769 space_shrinking = to_space();
duke@0 770 }
duke@0 771
duke@0 772 HeapWord* new_end = (HeapWord*)virtual_space()->high();
duke@0 773 assert(new_end >= space_shrinking->bottom(), "Shrink was too large");
duke@0 774 // Was there a shrink of the survivor space?
duke@0 775 if (new_end < space_shrinking->end()) {
duke@0 776 MemRegion mr(space_shrinking->bottom(), new_end);
duke@0 777 space_shrinking->initialize(mr, false /* clear */);
duke@0 778 }
duke@0 779 }
duke@0 780
duke@0 781 // This method currently does not expect to expand into eden (i.e.,
duke@0 782 // the virtual space boundaries is expected to be consistent
duke@0 783 // with the eden boundaries..
duke@0 784 void PSYoungGen::post_resize() {
duke@0 785 assert_locked_or_safepoint(Heap_lock);
duke@0 786 assert((eden_space()->bottom() < to_space()->bottom()) &&
duke@0 787 (eden_space()->bottom() < from_space()->bottom()),
duke@0 788 "Eden is assumed to be below the survivor spaces");
duke@0 789
duke@0 790 MemRegion cmr((HeapWord*)virtual_space()->low(),
duke@0 791 (HeapWord*)virtual_space()->high());
duke@0 792 Universe::heap()->barrier_set()->resize_covered_region(cmr);
duke@0 793 space_invariants();
duke@0 794 }
duke@0 795
duke@0 796
duke@0 797
duke@0 798 void PSYoungGen::update_counters() {
duke@0 799 if (UsePerfData) {
duke@0 800 _eden_counters->update_all();
duke@0 801 _from_counters->update_all();
duke@0 802 _to_counters->update_all();
duke@0 803 _gen_counters->update_all();
duke@0 804 }
duke@0 805 }
duke@0 806
duke@0 807 void PSYoungGen::verify(bool allow_dirty) {
duke@0 808 eden_space()->verify(allow_dirty);
duke@0 809 from_space()->verify(allow_dirty);
duke@0 810 to_space()->verify(allow_dirty);
duke@0 811 }