1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/g1CardCounts.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,212 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "gc_implementation/g1/g1CardCounts.hpp" 1.30 +#include "gc_implementation/g1/g1CollectedHeap.inline.hpp" 1.31 +#include "gc_implementation/g1/g1CollectorPolicy.hpp" 1.32 +#include "gc_implementation/g1/g1GCPhaseTimes.hpp" 1.33 +#include "memory/cardTableModRefBS.hpp" 1.34 +#include "services/memTracker.hpp" 1.35 +#include "utilities/copy.hpp" 1.36 + 1.37 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 1.38 + 1.39 +void G1CardCounts::clear_range(size_t from_card_num, size_t to_card_num) { 1.40 + if (has_count_table()) { 1.41 + assert(from_card_num >= 0 && from_card_num < _committed_max_card_num, 1.42 + err_msg("from card num out of range: "SIZE_FORMAT, from_card_num)); 1.43 + assert(from_card_num < to_card_num, 1.44 + err_msg("Wrong order? from: " SIZE_FORMAT ", to: "SIZE_FORMAT, 1.45 + from_card_num, to_card_num)); 1.46 + assert(to_card_num <= _committed_max_card_num, 1.47 + err_msg("to card num out of range: " 1.48 + "to: "SIZE_FORMAT ", " 1.49 + "max: "SIZE_FORMAT, 1.50 + to_card_num, _committed_max_card_num)); 1.51 + 1.52 + to_card_num = MIN2(_committed_max_card_num, to_card_num); 1.53 + 1.54 + Copy::fill_to_bytes(&_card_counts[from_card_num], (to_card_num - from_card_num)); 1.55 + } 1.56 +} 1.57 + 1.58 +G1CardCounts::G1CardCounts(G1CollectedHeap *g1h): 1.59 + _g1h(g1h), _card_counts(NULL), 1.60 + _reserved_max_card_num(0), _committed_max_card_num(0), 1.61 + _committed_size(0) {} 1.62 + 1.63 +void G1CardCounts::initialize() { 1.64 + assert(_g1h->max_capacity() > 0, "initialization order"); 1.65 + assert(_g1h->capacity() == 0, "initialization order"); 1.66 + 1.67 + if (G1ConcRSHotCardLimit > 0) { 1.68 + // The max value we can store in the counts table is 1.69 + // max_jubyte. Guarantee the value of the hot 1.70 + // threshold limit is no more than this. 1.71 + guarantee(G1ConcRSHotCardLimit <= max_jubyte, "sanity"); 1.72 + 1.73 + _ct_bs = _g1h->g1_barrier_set(); 1.74 + _ct_bot = _ct_bs->byte_for_const(_g1h->reserved_region().start()); 1.75 + 1.76 + // Allocate/Reserve the counts table 1.77 + size_t reserved_bytes = _g1h->max_capacity(); 1.78 + _reserved_max_card_num = reserved_bytes >> CardTableModRefBS::card_shift; 1.79 + 1.80 + size_t reserved_size = _reserved_max_card_num * sizeof(jbyte); 1.81 + ReservedSpace rs(ReservedSpace::allocation_align_size_up(reserved_size)); 1.82 + if (!rs.is_reserved()) { 1.83 + warning("Could not reserve enough space for the card counts table"); 1.84 + guarantee(!has_reserved_count_table(), "should be NULL"); 1.85 + return; 1.86 + } 1.87 + 1.88 + MemTracker::record_virtual_memory_type((address)rs.base(), mtGC); 1.89 + 1.90 + _card_counts_storage.initialize(rs, 0); 1.91 + _card_counts = (jubyte*) _card_counts_storage.low(); 1.92 + } 1.93 +} 1.94 + 1.95 +void G1CardCounts::resize(size_t heap_capacity) { 1.96 + // Expand the card counts table to handle a heap with the given capacity. 1.97 + 1.98 + if (!has_reserved_count_table()) { 1.99 + // Don't expand if we failed to reserve the card counts table. 1.100 + return; 1.101 + } 1.102 + 1.103 + assert(_committed_size == 1.104 + ReservedSpace::allocation_align_size_up(_committed_size), 1.105 + err_msg("Unaligned? committed_size: " SIZE_FORMAT, _committed_size)); 1.106 + 1.107 + // Verify that the committed space for the card counts matches our 1.108 + // committed max card num. Note for some allocation alignments, the 1.109 + // amount of space actually committed for the counts table will be able 1.110 + // to span more cards than the number spanned by the maximum heap. 1.111 + size_t prev_committed_size = _committed_size; 1.112 + size_t prev_committed_card_num = committed_to_card_num(prev_committed_size); 1.113 + 1.114 + assert(prev_committed_card_num == _committed_max_card_num, 1.115 + err_msg("Card mismatch: " 1.116 + "prev: " SIZE_FORMAT ", " 1.117 + "committed: "SIZE_FORMAT", " 1.118 + "reserved: "SIZE_FORMAT, 1.119 + prev_committed_card_num, _committed_max_card_num, _reserved_max_card_num)); 1.120 + 1.121 + size_t new_size = (heap_capacity >> CardTableModRefBS::card_shift) * sizeof(jbyte); 1.122 + size_t new_committed_size = ReservedSpace::allocation_align_size_up(new_size); 1.123 + size_t new_committed_card_num = committed_to_card_num(new_committed_size); 1.124 + 1.125 + if (_committed_max_card_num < new_committed_card_num) { 1.126 + // we need to expand the backing store for the card counts 1.127 + size_t expand_size = new_committed_size - prev_committed_size; 1.128 + 1.129 + if (!_card_counts_storage.expand_by(expand_size)) { 1.130 + warning("Card counts table backing store commit failure"); 1.131 + return; 1.132 + } 1.133 + assert(_card_counts_storage.committed_size() == new_committed_size, 1.134 + "expansion commit failure"); 1.135 + 1.136 + _committed_size = new_committed_size; 1.137 + _committed_max_card_num = new_committed_card_num; 1.138 + 1.139 + clear_range(prev_committed_card_num, _committed_max_card_num); 1.140 + } 1.141 +} 1.142 + 1.143 +uint G1CardCounts::add_card_count(jbyte* card_ptr) { 1.144 + // Returns the number of times the card has been refined. 1.145 + // If we failed to reserve/commit the counts table, return 0. 1.146 + // If card_ptr is beyond the committed end of the counts table, 1.147 + // return 0. 1.148 + // Otherwise return the actual count. 1.149 + // Unless G1ConcRSHotCardLimit has been set appropriately, 1.150 + // returning 0 will result in the card being considered 1.151 + // cold and will be refined immediately. 1.152 + uint count = 0; 1.153 + if (has_count_table()) { 1.154 + size_t card_num = ptr_2_card_num(card_ptr); 1.155 + if (card_num < _committed_max_card_num) { 1.156 + count = (uint) _card_counts[card_num]; 1.157 + if (count < G1ConcRSHotCardLimit) { 1.158 + _card_counts[card_num] = 1.159 + (jubyte)(MIN2((uintx)(_card_counts[card_num] + 1), G1ConcRSHotCardLimit)); 1.160 + } 1.161 + } 1.162 + } 1.163 + return count; 1.164 +} 1.165 + 1.166 +bool G1CardCounts::is_hot(uint count) { 1.167 + return (count >= G1ConcRSHotCardLimit); 1.168 +} 1.169 + 1.170 +void G1CardCounts::clear_region(HeapRegion* hr) { 1.171 + assert(!hr->isHumongous(), "Should have been cleared"); 1.172 + if (has_count_table()) { 1.173 + HeapWord* bottom = hr->bottom(); 1.174 + 1.175 + // We use the last address in hr as hr could be the 1.176 + // last region in the heap. In which case trying to find 1.177 + // the card for hr->end() will be an OOB accesss to the 1.178 + // card table. 1.179 + HeapWord* last = hr->end() - 1; 1.180 + assert(_g1h->g1_committed().contains(last), 1.181 + err_msg("last not in committed: " 1.182 + "last: " PTR_FORMAT ", " 1.183 + "committed: [" PTR_FORMAT ", " PTR_FORMAT ")", 1.184 + last, 1.185 + _g1h->g1_committed().start(), 1.186 + _g1h->g1_committed().end())); 1.187 + 1.188 + const jbyte* from_card_ptr = _ct_bs->byte_for_const(bottom); 1.189 + const jbyte* last_card_ptr = _ct_bs->byte_for_const(last); 1.190 + 1.191 +#ifdef ASSERT 1.192 + HeapWord* start_addr = _ct_bs->addr_for(from_card_ptr); 1.193 + assert(start_addr == hr->bottom(), "alignment"); 1.194 + HeapWord* last_addr = _ct_bs->addr_for(last_card_ptr); 1.195 + assert((last_addr + CardTableModRefBS::card_size_in_words) == hr->end(), "alignment"); 1.196 +#endif // ASSERT 1.197 + 1.198 + // Clear the counts for the (exclusive) card range. 1.199 + size_t from_card_num = ptr_2_card_num(from_card_ptr); 1.200 + size_t to_card_num = ptr_2_card_num(last_card_ptr) + 1; 1.201 + clear_range(from_card_num, to_card_num); 1.202 + } 1.203 +} 1.204 + 1.205 +void G1CardCounts::clear_all() { 1.206 + assert(SafepointSynchronize::is_at_safepoint(), "don't call this otherwise"); 1.207 + clear_range((size_t)0, _committed_max_card_num); 1.208 +} 1.209 + 1.210 +G1CardCounts::~G1CardCounts() { 1.211 + if (has_reserved_count_table()) { 1.212 + _card_counts_storage.release(); 1.213 + } 1.214 +} 1.215 +