Mon, 28 Jul 2008 15:30:23 -0700
Merge
1 /*
2 * Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 # include "incls/_precompiled.incl"
26 # include "incls/_asPSYoungGen.cpp.incl"
28 ASPSYoungGen::ASPSYoungGen(size_t init_byte_size,
29 size_t minimum_byte_size,
30 size_t byte_size_limit) :
31 PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit),
32 _gen_size_limit(byte_size_limit) {
33 }
36 ASPSYoungGen::ASPSYoungGen(PSVirtualSpace* vs,
37 size_t init_byte_size,
38 size_t minimum_byte_size,
39 size_t byte_size_limit) :
40 //PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit),
41 PSYoungGen(vs->committed_size(), minimum_byte_size, byte_size_limit),
42 _gen_size_limit(byte_size_limit) {
44 assert(vs->committed_size() == init_byte_size, "Cannot replace with");
46 _virtual_space = vs;
47 }
49 void ASPSYoungGen::initialize_virtual_space(ReservedSpace rs,
50 size_t alignment) {
51 assert(_init_gen_size != 0, "Should have a finite size");
52 _virtual_space = new PSVirtualSpaceHighToLow(rs, alignment);
53 if (!_virtual_space->expand_by(_init_gen_size)) {
54 vm_exit_during_initialization("Could not reserve enough space for "
55 "object heap");
56 }
57 }
59 void ASPSYoungGen::initialize(ReservedSpace rs, size_t alignment) {
60 initialize_virtual_space(rs, alignment);
61 initialize_work();
62 }
64 size_t ASPSYoungGen::available_for_expansion() {
66 size_t current_committed_size = virtual_space()->committed_size();
67 assert((gen_size_limit() >= current_committed_size),
68 "generation size limit is wrong");
69 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
70 size_t result = gen_size_limit() - current_committed_size;
71 size_t result_aligned = align_size_down(result, heap->young_gen_alignment());
72 return result_aligned;
73 }
75 // Return the number of bytes the young gen is willing give up.
76 //
77 // Future implementations could check the survivors and if to_space is in the
78 // right place (below from_space), take a chunk from to_space.
79 size_t ASPSYoungGen::available_for_contraction() {
81 size_t uncommitted_bytes = virtual_space()->uncommitted_size();
82 if (uncommitted_bytes != 0) {
83 return uncommitted_bytes;
84 }
86 if (eden_space()->is_empty()) {
87 // Respect the minimum size for eden and for the young gen as a whole.
88 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
89 const size_t eden_alignment = heap->intra_heap_alignment();
90 const size_t gen_alignment = heap->young_gen_alignment();
92 assert(eden_space()->capacity_in_bytes() >= eden_alignment,
93 "Alignment is wrong");
94 size_t eden_avail = eden_space()->capacity_in_bytes() - eden_alignment;
95 eden_avail = align_size_down(eden_avail, gen_alignment);
97 assert(virtual_space()->committed_size() >= min_gen_size(),
98 "minimum gen size is wrong");
99 size_t gen_avail = virtual_space()->committed_size() - min_gen_size();
100 assert(virtual_space()->is_aligned(gen_avail), "not aligned");
102 const size_t max_contraction = MIN2(eden_avail, gen_avail);
103 // See comment for ASPSOldGen::available_for_contraction()
104 // for reasons the "increment" fraction is used.
105 PSAdaptiveSizePolicy* policy = heap->size_policy();
106 size_t result = policy->eden_increment_aligned_down(max_contraction);
107 size_t result_aligned = align_size_down(result, gen_alignment);
108 if (PrintAdaptiveSizePolicy && Verbose) {
109 gclog_or_tty->print_cr("ASPSYoungGen::available_for_contraction: %d K",
110 result_aligned/K);
111 gclog_or_tty->print_cr(" max_contraction %d K", max_contraction/K);
112 gclog_or_tty->print_cr(" eden_avail %d K", eden_avail/K);
113 gclog_or_tty->print_cr(" gen_avail %d K", gen_avail/K);
114 }
115 return result_aligned;
117 }
119 return 0;
120 }
122 // The current implementation only considers to the end of eden.
123 // If to_space is below from_space, to_space is not considered.
124 // to_space can be.
125 size_t ASPSYoungGen::available_to_live() {
126 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
127 const size_t alignment = heap->intra_heap_alignment();
129 // Include any space that is committed but is not in eden.
130 size_t available = pointer_delta(eden_space()->bottom(),
131 virtual_space()->low(),
132 sizeof(char));
134 const size_t eden_capacity = eden_space()->capacity_in_bytes();
135 if (eden_space()->is_empty() && eden_capacity > alignment) {
136 available += eden_capacity - alignment;
137 }
138 return available;
139 }
141 // Similar to PSYoungGen::resize_generation() but
142 // allows sum of eden_size and 2 * survivor_size to exceed _max_gen_size
143 // expands at the low end of the virtual space
144 // moves the boundary between the generations in order to expand
145 // some additional diagnostics
146 // If no additional changes are required, this can be deleted
147 // and the changes factored back into PSYoungGen::resize_generation().
148 bool ASPSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) {
149 const size_t alignment = virtual_space()->alignment();
150 size_t orig_size = virtual_space()->committed_size();
151 bool size_changed = false;
153 // There used to be a guarantee here that
154 // (eden_size + 2*survivor_size) <= _max_gen_size
155 // This requirement is enforced by the calculation of desired_size
156 // below. It may not be true on entry since the size of the
157 // eden_size is no bounded by the generation size.
159 assert(max_size() == reserved().byte_size(), "max gen size problem?");
160 assert(min_gen_size() <= orig_size && orig_size <= max_size(),
161 "just checking");
163 // Adjust new generation size
164 const size_t eden_plus_survivors =
165 align_size_up(eden_size + 2 * survivor_size, alignment);
166 size_t desired_size = MAX2(MIN2(eden_plus_survivors, gen_size_limit()),
167 min_gen_size());
168 assert(desired_size <= gen_size_limit(), "just checking");
170 if (desired_size > orig_size) {
171 // Grow the generation
172 size_t change = desired_size - orig_size;
173 HeapWord* prev_low = (HeapWord*) virtual_space()->low();
174 if (!virtual_space()->expand_by(change)) {
175 return false;
176 }
177 if (ZapUnusedHeapArea) {
178 // Mangle newly committed space immediately because it
179 // can be done here more simply that after the new
180 // spaces have been computed.
181 HeapWord* new_low = (HeapWord*) virtual_space()->low();
182 assert(new_low < prev_low, "Did not grow");
184 MemRegion mangle_region(new_low, prev_low);
185 SpaceMangler::mangle_region(mangle_region);
186 }
187 size_changed = true;
188 } else if (desired_size < orig_size) {
189 size_t desired_change = orig_size - desired_size;
191 // How much is available for shrinking.
192 size_t available_bytes = limit_gen_shrink(desired_change);
193 size_t change = MIN2(desired_change, available_bytes);
194 virtual_space()->shrink_by(change);
195 size_changed = true;
196 } else {
197 if (Verbose && PrintGC) {
198 if (orig_size == gen_size_limit()) {
199 gclog_or_tty->print_cr("ASPSYoung generation size at maximum: "
200 SIZE_FORMAT "K", orig_size/K);
201 } else if (orig_size == min_gen_size()) {
202 gclog_or_tty->print_cr("ASPSYoung generation size at minium: "
203 SIZE_FORMAT "K", orig_size/K);
204 }
205 }
206 }
208 if (size_changed) {
209 reset_after_change();
210 if (Verbose && PrintGC) {
211 size_t current_size = virtual_space()->committed_size();
212 gclog_or_tty->print_cr("ASPSYoung generation size changed: "
213 SIZE_FORMAT "K->" SIZE_FORMAT "K",
214 orig_size/K, current_size/K);
215 }
216 }
218 guarantee(eden_plus_survivors <= virtual_space()->committed_size() ||
219 virtual_space()->committed_size() == max_size(), "Sanity");
221 return true;
222 }
224 // Similar to PSYoungGen::resize_spaces() but
225 // eden always starts at the low end of the committed virtual space
226 // current implementation does not allow holes between the spaces
227 // _young_generation_boundary has to be reset because it changes.
228 // so additional verification
230 void ASPSYoungGen::resize_spaces(size_t requested_eden_size,
231 size_t requested_survivor_size) {
232 assert(UseAdaptiveSizePolicy, "sanity check");
233 assert(requested_eden_size > 0 && requested_survivor_size > 0,
234 "just checking");
236 space_invariants();
238 // We require eden and to space to be empty
239 if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) {
240 return;
241 }
243 if (PrintAdaptiveSizePolicy && Verbose) {
244 gclog_or_tty->print_cr("PSYoungGen::resize_spaces(requested_eden_size: "
245 SIZE_FORMAT
246 ", requested_survivor_size: " SIZE_FORMAT ")",
247 requested_eden_size, requested_survivor_size);
248 gclog_or_tty->print_cr(" eden: [" PTR_FORMAT ".." PTR_FORMAT ") "
249 SIZE_FORMAT,
250 eden_space()->bottom(),
251 eden_space()->end(),
252 pointer_delta(eden_space()->end(),
253 eden_space()->bottom(),
254 sizeof(char)));
255 gclog_or_tty->print_cr(" from: [" PTR_FORMAT ".." PTR_FORMAT ") "
256 SIZE_FORMAT,
257 from_space()->bottom(),
258 from_space()->end(),
259 pointer_delta(from_space()->end(),
260 from_space()->bottom(),
261 sizeof(char)));
262 gclog_or_tty->print_cr(" to: [" PTR_FORMAT ".." PTR_FORMAT ") "
263 SIZE_FORMAT,
264 to_space()->bottom(),
265 to_space()->end(),
266 pointer_delta( to_space()->end(),
267 to_space()->bottom(),
268 sizeof(char)));
269 }
271 // There's nothing to do if the new sizes are the same as the current
272 if (requested_survivor_size == to_space()->capacity_in_bytes() &&
273 requested_survivor_size == from_space()->capacity_in_bytes() &&
274 requested_eden_size == eden_space()->capacity_in_bytes()) {
275 if (PrintAdaptiveSizePolicy && Verbose) {
276 gclog_or_tty->print_cr(" capacities are the right sizes, returning");
277 }
278 return;
279 }
281 char* eden_start = (char*)virtual_space()->low();
282 char* eden_end = (char*)eden_space()->end();
283 char* from_start = (char*)from_space()->bottom();
284 char* from_end = (char*)from_space()->end();
285 char* to_start = (char*)to_space()->bottom();
286 char* to_end = (char*)to_space()->end();
288 assert(eden_start < from_start, "Cannot push into from_space");
290 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
291 const size_t alignment = heap->intra_heap_alignment();
292 const bool maintain_minimum =
293 (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size();
295 bool eden_from_to_order = from_start < to_start;
296 // Check whether from space is below to space
297 if (eden_from_to_order) {
298 // Eden, from, to
300 if (PrintAdaptiveSizePolicy && Verbose) {
301 gclog_or_tty->print_cr(" Eden, from, to:");
302 }
304 // Set eden
305 // "requested_eden_size" is a goal for the size of eden
306 // and may not be attainable. "eden_size" below is
307 // calculated based on the location of from-space and
308 // the goal for the size of eden. from-space is
309 // fixed in place because it contains live data.
310 // The calculation is done this way to avoid 32bit
311 // overflow (i.e., eden_start + requested_eden_size
312 // may too large for representation in 32bits).
313 size_t eden_size;
314 if (maintain_minimum) {
315 // Only make eden larger than the requested size if
316 // the minimum size of the generation has to be maintained.
317 // This could be done in general but policy at a higher
318 // level is determining a requested size for eden and that
319 // should be honored unless there is a fundamental reason.
320 eden_size = pointer_delta(from_start,
321 eden_start,
322 sizeof(char));
323 } else {
324 eden_size = MIN2(requested_eden_size,
325 pointer_delta(from_start, eden_start, sizeof(char)));
326 }
328 eden_end = eden_start + eden_size;
329 assert(eden_end >= eden_start, "addition overflowed")
331 // To may resize into from space as long as it is clear of live data.
332 // From space must remain page aligned, though, so we need to do some
333 // extra calculations.
335 // First calculate an optimal to-space
336 to_end = (char*)virtual_space()->high();
337 to_start = (char*)pointer_delta(to_end,
338 (char*)requested_survivor_size,
339 sizeof(char));
341 // Does the optimal to-space overlap from-space?
342 if (to_start < (char*)from_space()->end()) {
343 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
345 // Calculate the minimum offset possible for from_end
346 size_t from_size =
347 pointer_delta(from_space()->top(), from_start, sizeof(char));
349 // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME!
350 if (from_size == 0) {
351 from_size = alignment;
352 } else {
353 from_size = align_size_up(from_size, alignment);
354 }
356 from_end = from_start + from_size;
357 assert(from_end > from_start, "addition overflow or from_size problem");
359 guarantee(from_end <= (char*)from_space()->end(),
360 "from_end moved to the right");
362 // Now update to_start with the new from_end
363 to_start = MAX2(from_end, to_start);
364 }
366 guarantee(to_start != to_end, "to space is zero sized");
368 if (PrintAdaptiveSizePolicy && Verbose) {
369 gclog_or_tty->print_cr(" [eden_start .. eden_end): "
370 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
371 eden_start,
372 eden_end,
373 pointer_delta(eden_end, eden_start, sizeof(char)));
374 gclog_or_tty->print_cr(" [from_start .. from_end): "
375 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
376 from_start,
377 from_end,
378 pointer_delta(from_end, from_start, sizeof(char)));
379 gclog_or_tty->print_cr(" [ to_start .. to_end): "
380 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
381 to_start,
382 to_end,
383 pointer_delta( to_end, to_start, sizeof(char)));
384 }
385 } else {
386 // Eden, to, from
387 if (PrintAdaptiveSizePolicy && Verbose) {
388 gclog_or_tty->print_cr(" Eden, to, from:");
389 }
391 // To space gets priority over eden resizing. Note that we position
392 // to space as if we were able to resize from space, even though from
393 // space is not modified.
394 // Giving eden priority was tried and gave poorer performance.
395 to_end = (char*)pointer_delta(virtual_space()->high(),
396 (char*)requested_survivor_size,
397 sizeof(char));
398 to_end = MIN2(to_end, from_start);
399 to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size,
400 sizeof(char));
401 // if the space sizes are to be increased by several times then
402 // 'to_start' will point beyond the young generation. In this case
403 // 'to_start' should be adjusted.
404 to_start = MAX2(to_start, eden_start + alignment);
406 // Compute how big eden can be, then adjust end.
407 // See comments above on calculating eden_end.
408 size_t eden_size;
409 if (maintain_minimum) {
410 eden_size = pointer_delta(to_start, eden_start, sizeof(char));
411 } else {
412 eden_size = MIN2(requested_eden_size,
413 pointer_delta(to_start, eden_start, sizeof(char)));
414 }
415 eden_end = eden_start + eden_size;
416 assert(eden_end >= eden_start, "addition overflowed")
418 // Don't let eden shrink down to 0 or less.
419 eden_end = MAX2(eden_end, eden_start + alignment);
420 to_start = MAX2(to_start, eden_end);
422 if (PrintAdaptiveSizePolicy && Verbose) {
423 gclog_or_tty->print_cr(" [eden_start .. eden_end): "
424 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
425 eden_start,
426 eden_end,
427 pointer_delta(eden_end, eden_start, sizeof(char)));
428 gclog_or_tty->print_cr(" [ to_start .. to_end): "
429 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
430 to_start,
431 to_end,
432 pointer_delta( to_end, to_start, sizeof(char)));
433 gclog_or_tty->print_cr(" [from_start .. from_end): "
434 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
435 from_start,
436 from_end,
437 pointer_delta(from_end, from_start, sizeof(char)));
438 }
439 }
442 guarantee((HeapWord*)from_start <= from_space()->bottom(),
443 "from start moved to the right");
444 guarantee((HeapWord*)from_end >= from_space()->top(),
445 "from end moved into live data");
446 assert(is_object_aligned((intptr_t)eden_start), "checking alignment");
447 assert(is_object_aligned((intptr_t)from_start), "checking alignment");
448 assert(is_object_aligned((intptr_t)to_start), "checking alignment");
450 MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end);
451 MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end);
452 MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end);
454 // Let's make sure the call to initialize doesn't reset "top"!
455 DEBUG_ONLY(HeapWord* old_from_top = from_space()->top();)
457 // For PrintAdaptiveSizePolicy block below
458 size_t old_from = from_space()->capacity_in_bytes();
459 size_t old_to = to_space()->capacity_in_bytes();
461 if (ZapUnusedHeapArea) {
462 // NUMA is a special case because a numa space is not mangled
463 // in order to not prematurely bind its address to memory to
464 // the wrong memory (i.e., don't want the GC thread to first
465 // touch the memory). The survivor spaces are not numa
466 // spaces and are mangled.
467 if (UseNUMA) {
468 if (eden_from_to_order) {
469 mangle_survivors(from_space(), fromMR, to_space(), toMR);
470 } else {
471 mangle_survivors(to_space(), toMR, from_space(), fromMR);
472 }
473 }
475 // If not mangling the spaces, do some checking to verify that
476 // the spaces are already mangled.
477 // The spaces should be correctly mangled at this point so
478 // do some checking here. Note that they are not being mangled
479 // in the calls to initialize().
480 // Must check mangling before the spaces are reshaped. Otherwise,
481 // the bottom or end of one space may have moved into an area
482 // covered by another space and a failure of the check may
483 // not correctly indicate which space is not properly mangled.
485 HeapWord* limit = (HeapWord*) virtual_space()->high();
486 eden_space()->check_mangled_unused_area(limit);
487 from_space()->check_mangled_unused_area(limit);
488 to_space()->check_mangled_unused_area(limit);
489 }
490 // When an existing space is being initialized, it is not
491 // mangled because the space has been previously mangled.
492 eden_space()->initialize(edenMR,
493 SpaceDecorator::Clear,
494 SpaceDecorator::DontMangle);
495 to_space()->initialize(toMR,
496 SpaceDecorator::Clear,
497 SpaceDecorator::DontMangle);
498 from_space()->initialize(fromMR,
499 SpaceDecorator::DontClear,
500 SpaceDecorator::DontMangle);
502 PSScavenge::set_young_generation_boundary(eden_space()->bottom());
504 assert(from_space()->top() == old_from_top, "from top changed!");
506 if (PrintAdaptiveSizePolicy) {
507 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
508 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
510 gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: "
511 "collection: %d "
512 "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> "
513 "(" SIZE_FORMAT ", " SIZE_FORMAT ") ",
514 heap->total_collections(),
515 old_from, old_to,
516 from_space()->capacity_in_bytes(),
517 to_space()->capacity_in_bytes());
518 gclog_or_tty->cr();
519 }
520 space_invariants();
521 }
522 void ASPSYoungGen::reset_after_change() {
523 assert_locked_or_safepoint(Heap_lock);
525 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(),
526 (HeapWord*)virtual_space()->high_boundary());
527 PSScavenge::reference_processor()->set_span(_reserved);
529 HeapWord* new_eden_bottom = (HeapWord*)virtual_space()->low();
530 HeapWord* eden_bottom = eden_space()->bottom();
531 if (new_eden_bottom != eden_bottom) {
532 MemRegion eden_mr(new_eden_bottom, eden_space()->end());
533 eden_space()->initialize(eden_mr,
534 SpaceDecorator::Clear,
535 SpaceDecorator::Mangle);
536 PSScavenge::set_young_generation_boundary(eden_space()->bottom());
537 }
538 MemRegion cmr((HeapWord*)virtual_space()->low(),
539 (HeapWord*)virtual_space()->high());
540 Universe::heap()->barrier_set()->resize_covered_region(cmr);
542 space_invariants();
543 }