1 /* 2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 27 #include "gc/shared/blockOffsetTable.inline.hpp" 28 #include "gc/shared/cardGeneration.inline.hpp" 29 #include "gc/shared/gcLocker.hpp" 30 #include "gc/shared/genOopClosures.inline.hpp" 31 #include "gc/shared/genRemSet.hpp" 32 #include "gc/shared/generationSpec.hpp" 33 #include "gc/shared/space.inline.hpp" 34 #include "memory/iterator.hpp" 35 #include "memory/memRegion.hpp" 36 #include "runtime/java.hpp" 37 38 CardGeneration::CardGeneration(ReservedSpace rs, 39 size_t initial_byte_size, 40 GenRemSet* remset) : 41 Generation(rs, initial_byte_size), _rs(remset), 42 _shrink_factor(0), _min_heap_delta_bytes(), _capacity_at_prologue(), 43 _used_at_prologue() 44 { 45 HeapWord* start = (HeapWord*)rs.base(); 46 size_t reserved_byte_size = rs.size(); 47 assert((uintptr_t(start) & 3) == 0, "bad alignment"); 48 assert((reserved_byte_size & 3) == 0, "bad alignment"); 49 MemRegion reserved_mr(start, heap_word_size(reserved_byte_size)); 50 _bts = new BlockOffsetSharedArray(reserved_mr, 51 heap_word_size(initial_byte_size)); 52 MemRegion committed_mr(start, heap_word_size(initial_byte_size)); 53 _rs->resize_covered_region(committed_mr); 54 if (_bts == NULL) { 55 vm_exit_during_initialization("Could not allocate a BlockOffsetArray"); 56 } 57 58 // Verify that the start and end of this generation is the start of a card. 59 // If this wasn't true, a single card could span more than on generation, 60 // which would cause problems when we commit/uncommit memory, and when we 61 // clear and dirty cards. 62 guarantee(_rs->is_aligned(reserved_mr.start()), "generation must be card aligned"); 63 if (reserved_mr.end() != GenCollectedHeap::heap()->reserved_region().end()) { 64 // Don't check at the very end of the heap as we'll assert that we're probing off 65 // the end if we try. 66 guarantee(_rs->is_aligned(reserved_mr.end()), "generation must be card aligned"); 67 } 68 _min_heap_delta_bytes = MinHeapDeltaBytes; 69 _capacity_at_prologue = initial_byte_size; 70 _used_at_prologue = 0; 71 } 72 73 bool CardGeneration::grow_by(size_t bytes) { 74 assert_correct_size_change_locking(); 75 bool result = _virtual_space.expand_by(bytes); 76 if (result) { 77 size_t new_word_size = 78 heap_word_size(_virtual_space.committed_size()); 79 MemRegion mr(space()->bottom(), new_word_size); 80 // Expand card table 81 GenCollectedHeap::heap()->barrier_set()->resize_covered_region(mr); 82 // Expand shared block offset array 83 _bts->resize(new_word_size); 84 85 // Fix for bug #4668531 86 if (ZapUnusedHeapArea) { 87 MemRegion mangle_region(space()->end(), 88 (HeapWord*)_virtual_space.high()); 89 SpaceMangler::mangle_region(mangle_region); 90 } 91 92 // Expand space -- also expands space's BOT 93 // (which uses (part of) shared array above) 94 space()->set_end((HeapWord*)_virtual_space.high()); 95 96 // update the space and generation capacity counters 97 update_counters(); 98 99 if (Verbose && PrintGC) { 100 size_t new_mem_size = _virtual_space.committed_size(); 101 size_t old_mem_size = new_mem_size - bytes; 102 gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by " 103 SIZE_FORMAT "K to " SIZE_FORMAT "K", 104 name(), old_mem_size/K, bytes/K, new_mem_size/K); 105 } 106 } 107 return result; 108 } 109 110 bool CardGeneration::expand(size_t bytes, size_t expand_bytes) { 111 assert_locked_or_safepoint(Heap_lock); 112 if (bytes == 0) { 113 return true; // That's what grow_by(0) would return 114 } 115 size_t aligned_bytes = ReservedSpace::page_align_size_up(bytes); 116 if (aligned_bytes == 0){ 117 // The alignment caused the number of bytes to wrap. An expand_by(0) will 118 // return true with the implication that an expansion was done when it 119 // was not. A call to expand implies a best effort to expand by "bytes" 120 // but not a guarantee. Align down to give a best effort. This is likely 121 // the most that the generation can expand since it has some capacity to 122 // start with. 123 aligned_bytes = ReservedSpace::page_align_size_down(bytes); 124 } 125 size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes); 126 bool success = false; 127 if (aligned_expand_bytes > aligned_bytes) { 128 success = grow_by(aligned_expand_bytes); 129 } 130 if (!success) { 131 success = grow_by(aligned_bytes); 132 } 133 if (!success) { 134 success = grow_to_reserved(); 135 } 136 if (PrintGC && Verbose) { 137 if (success && GC_locker::is_active_and_needs_gc()) { 138 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead"); 139 } 140 } 141 142 return success; 143 } 144 145 bool CardGeneration::grow_to_reserved() { 146 assert_correct_size_change_locking(); 147 bool success = true; 148 const size_t remaining_bytes = _virtual_space.uncommitted_size(); 149 if (remaining_bytes > 0) { 150 success = grow_by(remaining_bytes); 151 DEBUG_ONLY(if (!success) warning("grow to reserved failed");) 152 } 153 return success; 154 } 155 156 void CardGeneration::shrink(size_t bytes) { 157 assert_correct_size_change_locking(); 158 159 size_t size = ReservedSpace::page_align_size_down(bytes); 160 if (size == 0) { 161 return; 162 } 163 164 // Shrink committed space 165 _virtual_space.shrink_by(size); 166 // Shrink space; this also shrinks the space's BOT 167 space()->set_end((HeapWord*) _virtual_space.high()); 168 size_t new_word_size = heap_word_size(space()->capacity()); 169 // Shrink the shared block offset array 170 _bts->resize(new_word_size); 171 MemRegion mr(space()->bottom(), new_word_size); 172 // Shrink the card table 173 GenCollectedHeap::heap()->barrier_set()->resize_covered_region(mr); 174 175 if (Verbose && PrintGC) { 176 size_t new_mem_size = _virtual_space.committed_size(); 177 size_t old_mem_size = new_mem_size + size; 178 gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K to " SIZE_FORMAT "K", 179 name(), old_mem_size/K, new_mem_size/K); 180 } 181 } 182 183 // No young generation references, clear this generation's cards. 184 void CardGeneration::clear_remembered_set() { 185 _rs->clear(reserved()); 186 } 187 188 // Objects in this generation may have moved, invalidate this 189 // generation's cards. 190 void CardGeneration::invalidate_remembered_set() { 191 _rs->invalidate(used_region()); 192 } 193 194 void CardGeneration::compute_new_size() { 195 assert(_shrink_factor <= 100, "invalid shrink factor"); 196 size_t current_shrink_factor = _shrink_factor; 197 _shrink_factor = 0; 198 199 // We don't have floating point command-line arguments 200 // Note: argument processing ensures that MinHeapFreeRatio < 100. 201 const double minimum_free_percentage = MinHeapFreeRatio / 100.0; 202 const double maximum_used_percentage = 1.0 - minimum_free_percentage; 203 204 // Compute some numbers about the state of the heap. 205 const size_t used_after_gc = used(); 206 const size_t capacity_after_gc = capacity(); 207 208 const double min_tmp = used_after_gc / maximum_used_percentage; 209 size_t minimum_desired_capacity = (size_t)MIN2(min_tmp, double(max_uintx)); 210 // Don't shrink less than the initial generation size 211 minimum_desired_capacity = MAX2(minimum_desired_capacity, 212 spec()->init_size()); 213 assert(used_after_gc <= minimum_desired_capacity, "sanity check"); 214 215 if (PrintGC && Verbose) { 216 const size_t free_after_gc = free(); 217 const double free_percentage = ((double)free_after_gc) / capacity_after_gc; 218 gclog_or_tty->print_cr("TenuredGeneration::compute_new_size: "); 219 gclog_or_tty->print_cr(" " 220 " minimum_free_percentage: %6.2f" 221 " maximum_used_percentage: %6.2f", 222 minimum_free_percentage, 223 maximum_used_percentage); 224 gclog_or_tty->print_cr(" " 225 " free_after_gc : %6.1fK" 226 " used_after_gc : %6.1fK" 227 " capacity_after_gc : %6.1fK", 228 free_after_gc / (double) K, 229 used_after_gc / (double) K, 230 capacity_after_gc / (double) K); 231 gclog_or_tty->print_cr(" " 232 " free_percentage: %6.2f", 233 free_percentage); 234 } 235 236 if (capacity_after_gc < minimum_desired_capacity) { 237 // If we have less free space than we want then expand 238 size_t expand_bytes = minimum_desired_capacity - capacity_after_gc; 239 // Don't expand unless it's significant 240 if (expand_bytes >= _min_heap_delta_bytes) { 241 expand(expand_bytes, 0); // safe if expansion fails 242 } 243 if (PrintGC && Verbose) { 244 gclog_or_tty->print_cr(" expanding:" 245 " minimum_desired_capacity: %6.1fK" 246 " expand_bytes: %6.1fK" 247 " _min_heap_delta_bytes: %6.1fK", 248 minimum_desired_capacity / (double) K, 249 expand_bytes / (double) K, 250 _min_heap_delta_bytes / (double) K); 251 } 252 return; 253 } 254 255 // No expansion, now see if we want to shrink 256 size_t shrink_bytes = 0; 257 // We would never want to shrink more than this 258 size_t max_shrink_bytes = capacity_after_gc - minimum_desired_capacity; 259 260 if (MaxHeapFreeRatio < 100) { 261 const double maximum_free_percentage = MaxHeapFreeRatio / 100.0; 262 const double minimum_used_percentage = 1.0 - maximum_free_percentage; 263 const double max_tmp = used_after_gc / minimum_used_percentage; 264 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); 265 maximum_desired_capacity = MAX2(maximum_desired_capacity, 266 spec()->init_size()); 267 if (PrintGC && Verbose) { 268 gclog_or_tty->print_cr(" " 269 " maximum_free_percentage: %6.2f" 270 " minimum_used_percentage: %6.2f", 271 maximum_free_percentage, 272 minimum_used_percentage); 273 gclog_or_tty->print_cr(" " 274 " _capacity_at_prologue: %6.1fK" 275 " minimum_desired_capacity: %6.1fK" 276 " maximum_desired_capacity: %6.1fK", 277 _capacity_at_prologue / (double) K, 278 minimum_desired_capacity / (double) K, 279 maximum_desired_capacity / (double) K); 280 } 281 assert(minimum_desired_capacity <= maximum_desired_capacity, 282 "sanity check"); 283 284 if (capacity_after_gc > maximum_desired_capacity) { 285 // Capacity too large, compute shrinking size 286 shrink_bytes = capacity_after_gc - maximum_desired_capacity; 287 // We don't want shrink all the way back to initSize if people call 288 // System.gc(), because some programs do that between "phases" and then 289 // we'd just have to grow the heap up again for the next phase. So we 290 // damp the shrinking: 0% on the first call, 10% on the second call, 40% 291 // on the third call, and 100% by the fourth call. But if we recompute 292 // size without shrinking, it goes back to 0%. 293 shrink_bytes = shrink_bytes / 100 * current_shrink_factor; 294 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); 295 if (current_shrink_factor == 0) { 296 _shrink_factor = 10; 297 } else { 298 _shrink_factor = MIN2(current_shrink_factor * 4, (size_t) 100); 299 } 300 if (PrintGC && Verbose) { 301 gclog_or_tty->print_cr(" " 302 " shrinking:" 303 " initSize: %.1fK" 304 " maximum_desired_capacity: %.1fK", 305 spec()->init_size() / (double) K, 306 maximum_desired_capacity / (double) K); 307 gclog_or_tty->print_cr(" " 308 " shrink_bytes: %.1fK" 309 " current_shrink_factor: " SIZE_FORMAT 310 " new shrink factor: " SIZE_FORMAT 311 " _min_heap_delta_bytes: %.1fK", 312 shrink_bytes / (double) K, 313 current_shrink_factor, 314 _shrink_factor, 315 _min_heap_delta_bytes / (double) K); 316 } 317 } 318 } 319 320 if (capacity_after_gc > _capacity_at_prologue) { 321 // We might have expanded for promotions, in which case we might want to 322 // take back that expansion if there's room after GC. That keeps us from 323 // stretching the heap with promotions when there's plenty of room. 324 size_t expansion_for_promotion = capacity_after_gc - _capacity_at_prologue; 325 expansion_for_promotion = MIN2(expansion_for_promotion, max_shrink_bytes); 326 // We have two shrinking computations, take the largest 327 shrink_bytes = MAX2(shrink_bytes, expansion_for_promotion); 328 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); 329 if (PrintGC && Verbose) { 330 gclog_or_tty->print_cr(" " 331 " aggressive shrinking:" 332 " _capacity_at_prologue: %.1fK" 333 " capacity_after_gc: %.1fK" 334 " expansion_for_promotion: %.1fK" 335 " shrink_bytes: %.1fK", 336 capacity_after_gc / (double) K, 337 _capacity_at_prologue / (double) K, 338 expansion_for_promotion / (double) K, 339 shrink_bytes / (double) K); 340 } 341 } 342 // Don't shrink unless it's significant 343 if (shrink_bytes >= _min_heap_delta_bytes) { 344 shrink(shrink_bytes); 345 } 346 } 347 348 // Currently nothing to do. 349 void CardGeneration::prepare_for_verify() {} 350 351 void CardGeneration::space_iterate(SpaceClosure* blk, 352 bool usedOnly) { 353 blk->do_space(space()); 354 } 355 356 void CardGeneration::younger_refs_iterate(OopsInGenClosure* blk, uint n_threads) { 357 blk->set_generation(this); 358 younger_refs_in_space_iterate(space(), blk, n_threads); 359 blk->reset_generation(); 360 }