1 /* 2 * Copyright (c) 2014, 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 #include "memory/blockOffsetTable.inline.hpp" 27 #include "memory/gcLocker.hpp" 28 #include "memory/generationSpec.hpp" 29 #include "memory/genOopClosures.inline.hpp" 30 #include "memory/genRemSet.hpp" 31 #include "memory/iterator.hpp" 32 #include "memory/memRegion.hpp" 33 #include "memory/space.inline.hpp" 34 #include "runtime/java.hpp" 35 36 CardGeneration::CardGeneration(ReservedSpace rs, size_t initial_byte_size, 37 int level, 38 GenRemSet* remset) : 39 Generation(rs, initial_byte_size, level), _rs(remset), 40 _shrink_factor(0), _min_heap_delta_bytes(), _capacity_at_prologue(), 41 _used_at_prologue() 42 { 43 HeapWord* start = (HeapWord*)rs.base(); 44 size_t reserved_byte_size = rs.size(); 45 assert((uintptr_t(start) & 3) == 0, "bad alignment"); 46 assert((reserved_byte_size & 3) == 0, "bad alignment"); 47 MemRegion reserved_mr(start, heap_word_size(reserved_byte_size)); 48 _bts = new BlockOffsetSharedArray(reserved_mr, 49 heap_word_size(initial_byte_size)); 50 MemRegion committed_mr(start, heap_word_size(initial_byte_size)); 51 _rs->resize_covered_region(committed_mr); 52 if (_bts == NULL) 53 vm_exit_during_initialization("Could not allocate a BlockOffsetArray"); 54 55 // Verify that the start and end of this generation is the start of a card. 56 // If this wasn't true, a single card could span more than on generation, 57 // which would cause problems when we commit/uncommit memory, and when we 58 // clear and dirty cards. 59 guarantee(_rs->is_aligned(reserved_mr.start()), "generation must be card aligned"); 60 if (reserved_mr.end() != Universe::heap()->reserved_region().end()) { 61 // Don't check at the very end of the heap as we'll assert that we're probing off 62 // the end if we try. 63 guarantee(_rs->is_aligned(reserved_mr.end()), "generation must be card aligned"); 64 } 65 _min_heap_delta_bytes = MinHeapDeltaBytes; 66 _capacity_at_prologue = initial_byte_size; 67 _used_at_prologue = 0; 68 } 69 70 bool CardGeneration::expand(size_t bytes, size_t expand_bytes) { 71 assert_locked_or_safepoint(Heap_lock); 72 if (bytes == 0) { 73 return true; // That's what grow_by(0) would return 74 } 75 size_t aligned_bytes = ReservedSpace::page_align_size_up(bytes); 76 if (aligned_bytes == 0){ 77 // The alignment caused the number of bytes to wrap. An expand_by(0) will 78 // return true with the implication that an expansion was done when it 79 // was not. A call to expand implies a best effort to expand by "bytes" 80 // but not a guarantee. Align down to give a best effort. This is likely 81 // the most that the generation can expand since it has some capacity to 82 // start with. 83 aligned_bytes = ReservedSpace::page_align_size_down(bytes); 84 } 85 size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes); 86 bool success = false; 87 if (aligned_expand_bytes > aligned_bytes) { 88 success = grow_by(aligned_expand_bytes); 89 } 90 if (!success) { 91 success = grow_by(aligned_bytes); 92 } 93 if (!success) { 94 success = grow_to_reserved(); 95 } 96 if (PrintGC && Verbose) { 97 if (success && GC_locker::is_active_and_needs_gc()) { 98 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead"); 99 } 100 } 101 102 return success; 103 } 104 105 // No young generation references, clear this generation's cards. 106 void CardGeneration::clear_remembered_set() { 107 _rs->clear(reserved()); 108 } 109 110 // Objects in this generation may have moved, invalidate this 111 // generation's cards. 112 void CardGeneration::invalidate_remembered_set() { 113 _rs->invalidate(used_region()); 114 } 115 116 void CardGeneration::compute_new_size() { 117 assert(_shrink_factor <= 100, "invalid shrink factor"); 118 size_t current_shrink_factor = _shrink_factor; 119 _shrink_factor = 0; 120 121 // We don't have floating point command-line arguments 122 // Note: argument processing ensures that MinHeapFreeRatio < 100. 123 const double minimum_free_percentage = MinHeapFreeRatio / 100.0; 124 const double maximum_used_percentage = 1.0 - minimum_free_percentage; 125 126 // Compute some numbers about the state of the heap. 127 const size_t used_after_gc = used(); 128 const size_t capacity_after_gc = capacity(); 129 130 const double min_tmp = used_after_gc / maximum_used_percentage; 131 size_t minimum_desired_capacity = (size_t)MIN2(min_tmp, double(max_uintx)); 132 // Don't shrink less than the initial generation size 133 minimum_desired_capacity = MAX2(minimum_desired_capacity, 134 spec()->init_size()); 135 assert(used_after_gc <= minimum_desired_capacity, "sanity check"); 136 137 if (PrintGC && Verbose) { 138 const size_t free_after_gc = free(); 139 const double free_percentage = ((double)free_after_gc) / capacity_after_gc; 140 gclog_or_tty->print_cr("TenuredGeneration::compute_new_size: "); 141 gclog_or_tty->print_cr(" " 142 " minimum_free_percentage: %6.2f" 143 " maximum_used_percentage: %6.2f", 144 minimum_free_percentage, 145 maximum_used_percentage); 146 gclog_or_tty->print_cr(" " 147 " free_after_gc : %6.1fK" 148 " used_after_gc : %6.1fK" 149 " capacity_after_gc : %6.1fK", 150 free_after_gc / (double) K, 151 used_after_gc / (double) K, 152 capacity_after_gc / (double) K); 153 gclog_or_tty->print_cr(" " 154 " free_percentage: %6.2f", 155 free_percentage); 156 } 157 158 if (capacity_after_gc < minimum_desired_capacity) { 159 // If we have less free space than we want then expand 160 size_t expand_bytes = minimum_desired_capacity - capacity_after_gc; 161 // Don't expand unless it's significant 162 if (expand_bytes >= _min_heap_delta_bytes) { 163 expand(expand_bytes, 0); // safe if expansion fails 164 } 165 if (PrintGC && Verbose) { 166 gclog_or_tty->print_cr(" expanding:" 167 " minimum_desired_capacity: %6.1fK" 168 " expand_bytes: %6.1fK" 169 " _min_heap_delta_bytes: %6.1fK", 170 minimum_desired_capacity / (double) K, 171 expand_bytes / (double) K, 172 _min_heap_delta_bytes / (double) K); 173 } 174 return; 175 } 176 177 // No expansion, now see if we want to shrink 178 size_t shrink_bytes = 0; 179 // We would never want to shrink more than this 180 size_t max_shrink_bytes = capacity_after_gc - minimum_desired_capacity; 181 182 if (MaxHeapFreeRatio < 100) { 183 const double maximum_free_percentage = MaxHeapFreeRatio / 100.0; 184 const double minimum_used_percentage = 1.0 - maximum_free_percentage; 185 const double max_tmp = used_after_gc / minimum_used_percentage; 186 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); 187 maximum_desired_capacity = MAX2(maximum_desired_capacity, 188 spec()->init_size()); 189 if (PrintGC && Verbose) { 190 gclog_or_tty->print_cr(" " 191 " maximum_free_percentage: %6.2f" 192 " minimum_used_percentage: %6.2f", 193 maximum_free_percentage, 194 minimum_used_percentage); 195 gclog_or_tty->print_cr(" " 196 " _capacity_at_prologue: %6.1fK" 197 " minimum_desired_capacity: %6.1fK" 198 " maximum_desired_capacity: %6.1fK", 199 _capacity_at_prologue / (double) K, 200 minimum_desired_capacity / (double) K, 201 maximum_desired_capacity / (double) K); 202 } 203 assert(minimum_desired_capacity <= maximum_desired_capacity, 204 "sanity check"); 205 206 if (capacity_after_gc > maximum_desired_capacity) { 207 // Capacity too large, compute shrinking size 208 shrink_bytes = capacity_after_gc - maximum_desired_capacity; 209 // We don't want shrink all the way back to initSize if people call 210 // System.gc(), because some programs do that between "phases" and then 211 // we'd just have to grow the heap up again for the next phase. So we 212 // damp the shrinking: 0% on the first call, 10% on the second call, 40% 213 // on the third call, and 100% by the fourth call. But if we recompute 214 // size without shrinking, it goes back to 0%. 215 shrink_bytes = shrink_bytes / 100 * current_shrink_factor; 216 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); 217 if (current_shrink_factor == 0) { 218 _shrink_factor = 10; 219 } else { 220 _shrink_factor = MIN2(current_shrink_factor * 4, (size_t) 100); 221 } 222 if (PrintGC && Verbose) { 223 gclog_or_tty->print_cr(" " 224 " shrinking:" 225 " initSize: %.1fK" 226 " maximum_desired_capacity: %.1fK", 227 spec()->init_size() / (double) K, 228 maximum_desired_capacity / (double) K); 229 gclog_or_tty->print_cr(" " 230 " shrink_bytes: %.1fK" 231 " current_shrink_factor: " SIZE_FORMAT 232 " new shrink factor: " SIZE_FORMAT 233 " _min_heap_delta_bytes: %.1fK", 234 shrink_bytes / (double) K, 235 current_shrink_factor, 236 _shrink_factor, 237 _min_heap_delta_bytes / (double) K); 238 } 239 } 240 } 241 242 if (capacity_after_gc > _capacity_at_prologue) { 243 // We might have expanded for promotions, in which case we might want to 244 // take back that expansion if there's room after GC. That keeps us from 245 // stretching the heap with promotions when there's plenty of room. 246 size_t expansion_for_promotion = capacity_after_gc - _capacity_at_prologue; 247 expansion_for_promotion = MIN2(expansion_for_promotion, max_shrink_bytes); 248 // We have two shrinking computations, take the largest 249 shrink_bytes = MAX2(shrink_bytes, expansion_for_promotion); 250 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); 251 if (PrintGC && Verbose) { 252 gclog_or_tty->print_cr(" " 253 " aggressive shrinking:" 254 " _capacity_at_prologue: %.1fK" 255 " capacity_after_gc: %.1fK" 256 " expansion_for_promotion: %.1fK" 257 " shrink_bytes: %.1fK", 258 capacity_after_gc / (double) K, 259 _capacity_at_prologue / (double) K, 260 expansion_for_promotion / (double) K, 261 shrink_bytes / (double) K); 262 } 263 } 264 // Don't shrink unless it's significant 265 if (shrink_bytes >= _min_heap_delta_bytes) { 266 shrink(shrink_bytes); 267 } 268 } 269 270 // Currently nothing to do. 271 void CardGeneration::prepare_for_verify() {}