src/share/vm/gc_implementation/parallelScavenge/psYoungGen.cpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 448
183f41cf8bfe
permissions
-rw-r--r--

Initial load

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

mercurial