src/share/vm/memory/tenuredGeneration.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7576
1830156c6b7e
parent 6876
710a3c8b516e
child 9448
73d689add964
permissions
-rw-r--r--

merge

aoqi@0 1 /*
dholmes@7576 2 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "gc_implementation/shared/collectorCounters.hpp"
aoqi@0 27 #include "memory/allocation.inline.hpp"
aoqi@0 28 #include "memory/blockOffsetTable.inline.hpp"
aoqi@0 29 #include "memory/generation.inline.hpp"
aoqi@0 30 #include "memory/generationSpec.hpp"
aoqi@0 31 #include "memory/space.hpp"
aoqi@0 32 #include "memory/tenuredGeneration.hpp"
aoqi@0 33 #include "oops/oop.inline.hpp"
aoqi@0 34 #include "runtime/java.hpp"
aoqi@0 35 #include "utilities/macros.hpp"
dholmes@7576 36 #if INCLUDE_ALL_GCS
dholmes@7576 37 #include "gc_implementation/shared/parGCAllocBuffer.hpp"
dholmes@7576 38 #endif
aoqi@0 39
aoqi@0 40 TenuredGeneration::TenuredGeneration(ReservedSpace rs,
aoqi@0 41 size_t initial_byte_size, int level,
aoqi@0 42 GenRemSet* remset) :
aoqi@0 43 OneContigSpaceCardGeneration(rs, initial_byte_size,
aoqi@0 44 level, remset, NULL)
aoqi@0 45 {
aoqi@0 46 HeapWord* bottom = (HeapWord*) _virtual_space.low();
aoqi@0 47 HeapWord* end = (HeapWord*) _virtual_space.high();
aoqi@0 48 _the_space = new TenuredSpace(_bts, MemRegion(bottom, end));
aoqi@0 49 _the_space->reset_saved_mark();
aoqi@0 50 _shrink_factor = 0;
aoqi@0 51 _capacity_at_prologue = 0;
aoqi@0 52
aoqi@0 53 _gc_stats = new GCStats();
aoqi@0 54
aoqi@0 55 // initialize performance counters
aoqi@0 56
aoqi@0 57 const char* gen_name = "old";
aoqi@0 58
aoqi@0 59 // Generation Counters -- generation 1, 1 subspace
aoqi@0 60 _gen_counters = new GenerationCounters(gen_name, 1, 1, &_virtual_space);
aoqi@0 61
aoqi@0 62 _gc_counters = new CollectorCounters("MSC", 1);
aoqi@0 63
aoqi@0 64 _space_counters = new CSpaceCounters(gen_name, 0,
aoqi@0 65 _virtual_space.reserved_size(),
aoqi@0 66 _the_space, _gen_counters);
aoqi@0 67 #if INCLUDE_ALL_GCS
aoqi@0 68 if (UseParNewGC) {
aoqi@0 69 typedef ParGCAllocBufferWithBOT* ParGCAllocBufferWithBOTPtr;
aoqi@0 70 _alloc_buffers = NEW_C_HEAP_ARRAY(ParGCAllocBufferWithBOTPtr,
aoqi@0 71 ParallelGCThreads, mtGC);
aoqi@0 72 if (_alloc_buffers == NULL)
aoqi@0 73 vm_exit_during_initialization("Could not allocate alloc_buffers");
aoqi@0 74 for (uint i = 0; i < ParallelGCThreads; i++) {
aoqi@0 75 _alloc_buffers[i] =
aoqi@0 76 new ParGCAllocBufferWithBOT(OldPLABSize, _bts);
aoqi@0 77 if (_alloc_buffers[i] == NULL)
aoqi@0 78 vm_exit_during_initialization("Could not allocate alloc_buffers");
aoqi@0 79 }
aoqi@0 80 } else {
aoqi@0 81 _alloc_buffers = NULL;
aoqi@0 82 }
aoqi@0 83 #endif // INCLUDE_ALL_GCS
aoqi@0 84 }
aoqi@0 85
aoqi@0 86
aoqi@0 87 const char* TenuredGeneration::name() const {
aoqi@0 88 return "tenured generation";
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 void TenuredGeneration::gc_prologue(bool full) {
aoqi@0 92 _capacity_at_prologue = capacity();
aoqi@0 93 _used_at_prologue = used();
aoqi@0 94 if (VerifyBeforeGC) {
aoqi@0 95 verify_alloc_buffers_clean();
aoqi@0 96 }
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 void TenuredGeneration::gc_epilogue(bool full) {
aoqi@0 100 if (VerifyAfterGC) {
aoqi@0 101 verify_alloc_buffers_clean();
aoqi@0 102 }
aoqi@0 103 OneContigSpaceCardGeneration::gc_epilogue(full);
aoqi@0 104 }
aoqi@0 105
aoqi@0 106
aoqi@0 107 bool TenuredGeneration::should_collect(bool full,
aoqi@0 108 size_t size,
aoqi@0 109 bool is_tlab) {
aoqi@0 110 // This should be one big conditional or (||), but I want to be able to tell
aoqi@0 111 // why it returns what it returns (without re-evaluating the conditionals
aoqi@0 112 // in case they aren't idempotent), so I'm doing it this way.
aoqi@0 113 // DeMorgan says it's okay.
aoqi@0 114 bool result = false;
aoqi@0 115 if (!result && full) {
aoqi@0 116 result = true;
aoqi@0 117 if (PrintGC && Verbose) {
aoqi@0 118 gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
aoqi@0 119 " full");
aoqi@0 120 }
aoqi@0 121 }
aoqi@0 122 if (!result && should_allocate(size, is_tlab)) {
aoqi@0 123 result = true;
aoqi@0 124 if (PrintGC && Verbose) {
aoqi@0 125 gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
aoqi@0 126 " should_allocate(" SIZE_FORMAT ")",
aoqi@0 127 size);
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130 // If we don't have very much free space.
aoqi@0 131 // XXX: 10000 should be a percentage of the capacity!!!
aoqi@0 132 if (!result && free() < 10000) {
aoqi@0 133 result = true;
aoqi@0 134 if (PrintGC && Verbose) {
aoqi@0 135 gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
aoqi@0 136 " free(): " SIZE_FORMAT,
aoqi@0 137 free());
aoqi@0 138 }
aoqi@0 139 }
aoqi@0 140 // If we had to expand to accomodate promotions from younger generations
aoqi@0 141 if (!result && _capacity_at_prologue < capacity()) {
aoqi@0 142 result = true;
aoqi@0 143 if (PrintGC && Verbose) {
aoqi@0 144 gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
aoqi@0 145 "_capacity_at_prologue: " SIZE_FORMAT " < capacity(): " SIZE_FORMAT,
aoqi@0 146 _capacity_at_prologue, capacity());
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149 return result;
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 void TenuredGeneration::collect(bool full,
aoqi@0 153 bool clear_all_soft_refs,
aoqi@0 154 size_t size,
aoqi@0 155 bool is_tlab) {
aoqi@0 156 retire_alloc_buffers_before_full_gc();
aoqi@0 157 OneContigSpaceCardGeneration::collect(full, clear_all_soft_refs,
aoqi@0 158 size, is_tlab);
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 void TenuredGeneration::compute_new_size() {
aoqi@0 162 assert_locked_or_safepoint(Heap_lock);
aoqi@0 163
aoqi@0 164 // Compute some numbers about the state of the heap.
aoqi@0 165 const size_t used_after_gc = used();
aoqi@0 166 const size_t capacity_after_gc = capacity();
aoqi@0 167
aoqi@0 168 CardGeneration::compute_new_size();
aoqi@0 169
aoqi@0 170 assert(used() == used_after_gc && used_after_gc <= capacity(),
aoqi@0 171 err_msg("used: " SIZE_FORMAT " used_after_gc: " SIZE_FORMAT
aoqi@0 172 " capacity: " SIZE_FORMAT, used(), used_after_gc, capacity()));
aoqi@0 173 }
aoqi@0 174 void TenuredGeneration::update_gc_stats(int current_level,
aoqi@0 175 bool full) {
aoqi@0 176 // If the next lower level(s) has been collected, gather any statistics
aoqi@0 177 // that are of interest at this point.
aoqi@0 178 if (!full && (current_level + 1) == level()) {
aoqi@0 179 // Calculate size of data promoted from the younger generations
aoqi@0 180 // before doing the collection.
aoqi@0 181 size_t used_before_gc = used();
aoqi@0 182
aoqi@0 183 // If the younger gen collections were skipped, then the
aoqi@0 184 // number of promoted bytes will be 0 and adding it to the
aoqi@0 185 // average will incorrectly lessen the average. It is, however,
aoqi@0 186 // also possible that no promotion was needed.
aoqi@0 187 if (used_before_gc >= _used_at_prologue) {
aoqi@0 188 size_t promoted_in_bytes = used_before_gc - _used_at_prologue;
aoqi@0 189 gc_stats()->avg_promoted()->sample(promoted_in_bytes);
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 void TenuredGeneration::update_counters() {
aoqi@0 195 if (UsePerfData) {
aoqi@0 196 _space_counters->update_all();
aoqi@0 197 _gen_counters->update_all();
aoqi@0 198 }
aoqi@0 199 }
aoqi@0 200
aoqi@0 201
aoqi@0 202 #if INCLUDE_ALL_GCS
aoqi@0 203 oop TenuredGeneration::par_promote(int thread_num,
aoqi@0 204 oop old, markOop m, size_t word_sz) {
aoqi@0 205
aoqi@0 206 ParGCAllocBufferWithBOT* buf = _alloc_buffers[thread_num];
aoqi@0 207 HeapWord* obj_ptr = buf->allocate(word_sz);
aoqi@0 208 bool is_lab = true;
aoqi@0 209 if (obj_ptr == NULL) {
aoqi@0 210 #ifndef PRODUCT
aoqi@0 211 if (Universe::heap()->promotion_should_fail()) {
aoqi@0 212 return NULL;
aoqi@0 213 }
aoqi@0 214 #endif // #ifndef PRODUCT
aoqi@0 215
aoqi@0 216 // Slow path:
aoqi@0 217 if (word_sz * 100 < ParallelGCBufferWastePct * buf->word_sz()) {
aoqi@0 218 // Is small enough; abandon this buffer and start a new one.
aoqi@0 219 size_t buf_size = buf->word_sz();
aoqi@0 220 HeapWord* buf_space =
aoqi@0 221 TenuredGeneration::par_allocate(buf_size, false);
aoqi@0 222 if (buf_space == NULL) {
aoqi@0 223 buf_space = expand_and_allocate(buf_size, false, true /* parallel*/);
aoqi@0 224 }
aoqi@0 225 if (buf_space != NULL) {
aoqi@0 226 buf->retire(false, false);
aoqi@0 227 buf->set_buf(buf_space);
aoqi@0 228 obj_ptr = buf->allocate(word_sz);
aoqi@0 229 assert(obj_ptr != NULL, "Buffer was definitely big enough...");
aoqi@0 230 }
aoqi@0 231 };
aoqi@0 232 // Otherwise, buffer allocation failed; try allocating object
aoqi@0 233 // individually.
aoqi@0 234 if (obj_ptr == NULL) {
aoqi@0 235 obj_ptr = TenuredGeneration::par_allocate(word_sz, false);
aoqi@0 236 if (obj_ptr == NULL) {
aoqi@0 237 obj_ptr = expand_and_allocate(word_sz, false, true /* parallel */);
aoqi@0 238 }
aoqi@0 239 }
aoqi@0 240 if (obj_ptr == NULL) return NULL;
aoqi@0 241 }
aoqi@0 242 assert(obj_ptr != NULL, "program logic");
aoqi@0 243 Copy::aligned_disjoint_words((HeapWord*)old, obj_ptr, word_sz);
aoqi@0 244 oop obj = oop(obj_ptr);
aoqi@0 245 // Restore the mark word copied above.
aoqi@0 246 obj->set_mark(m);
aoqi@0 247 return obj;
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 void TenuredGeneration::par_promote_alloc_undo(int thread_num,
aoqi@0 251 HeapWord* obj,
aoqi@0 252 size_t word_sz) {
aoqi@0 253 ParGCAllocBufferWithBOT* buf = _alloc_buffers[thread_num];
aoqi@0 254 if (buf->contains(obj)) {
aoqi@0 255 guarantee(buf->contains(obj + word_sz - 1),
aoqi@0 256 "should contain whole object");
aoqi@0 257 buf->undo_allocation(obj, word_sz);
aoqi@0 258 } else {
aoqi@0 259 CollectedHeap::fill_with_object(obj, word_sz);
aoqi@0 260 }
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 void TenuredGeneration::par_promote_alloc_done(int thread_num) {
aoqi@0 264 ParGCAllocBufferWithBOT* buf = _alloc_buffers[thread_num];
aoqi@0 265 buf->retire(true, ParallelGCRetainPLAB);
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 void TenuredGeneration::retire_alloc_buffers_before_full_gc() {
aoqi@0 269 if (UseParNewGC) {
aoqi@0 270 for (uint i = 0; i < ParallelGCThreads; i++) {
aoqi@0 271 _alloc_buffers[i]->retire(true /*end_of_gc*/, false /*retain*/);
aoqi@0 272 }
aoqi@0 273 }
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 // Verify that any retained parallel allocation buffers do not
aoqi@0 277 // intersect with dirty cards.
aoqi@0 278 void TenuredGeneration::verify_alloc_buffers_clean() {
aoqi@0 279 if (UseParNewGC) {
aoqi@0 280 for (uint i = 0; i < ParallelGCThreads; i++) {
aoqi@0 281 _rs->verify_aligned_region_empty(_alloc_buffers[i]->range());
aoqi@0 282 }
aoqi@0 283 }
aoqi@0 284 }
aoqi@0 285
aoqi@0 286 #else // INCLUDE_ALL_GCS
aoqi@0 287 void TenuredGeneration::retire_alloc_buffers_before_full_gc() {}
aoqi@0 288 void TenuredGeneration::verify_alloc_buffers_clean() {}
aoqi@0 289 #endif // INCLUDE_ALL_GCS
aoqi@0 290
aoqi@0 291 bool TenuredGeneration::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const {
aoqi@0 292 size_t available = max_contiguous_available();
aoqi@0 293 size_t av_promo = (size_t)gc_stats()->avg_promoted()->padded_average();
aoqi@0 294 bool res = (available >= av_promo) || (available >= max_promotion_in_bytes);
aoqi@0 295 if (PrintGC && Verbose) {
aoqi@0 296 gclog_or_tty->print_cr(
aoqi@0 297 "Tenured: promo attempt is%s safe: available("SIZE_FORMAT") %s av_promo("SIZE_FORMAT"),"
aoqi@0 298 "max_promo("SIZE_FORMAT")",
aoqi@0 299 res? "":" not", available, res? ">=":"<",
aoqi@0 300 av_promo, max_promotion_in_bytes);
aoqi@0 301 }
aoqi@0 302 return res;
aoqi@0 303 }

mercurial