1 /*
   2  * Copyright (c) 2013, 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 "gc_implementation/g1/g1CardCounts.hpp"
  27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
  29 #include "gc_implementation/g1/g1GCPhaseTimes.hpp"
  30 #include "memory/cardTableModRefBS.hpp"
  31 #include "services/memTracker.hpp"
  32 
  33 void G1CardCounts::clear_range(size_t from_card_num, size_t to_card_num) {
  34   if (has_count_table()) {
  35     check_card_num(from_card_num,
  36                    err_msg("from card num out of range: "SIZE_FORMAT, from_card_num));
  37     assert(from_card_num < to_card_num,
  38            err_msg("Wrong order? from: " SIZE_FORMAT ", to: "SIZE_FORMAT,
  39                    from_card_num, to_card_num));
  40     assert(to_card_num <= _committed_max_card_num,
  41            err_msg("to card num out of range: "
  42                    "to: "SIZE_FORMAT ", "
  43                    "max: "SIZE_FORMAT,
  44                    to_card_num, _committed_max_card_num));
  45 
  46     to_card_num = MIN2(_committed_max_card_num, to_card_num);
  47 
  48     for (size_t card_num = from_card_num;
  49          card_num < to_card_num; card_num += 1) {
  50       _card_counts[card_num] = 0;
  51     }
  52   }
  53 }
  54 
  55 G1CardCounts::G1CardCounts(G1CollectedHeap *g1h):
  56   _g1h(g1h), _card_counts(NULL),
  57   _reserved_max_card_num(0), _committed_max_card_num(0),
  58   _committed_size(0) {}
  59 
  60 void G1CardCounts::initialize() {
  61   assert(_g1h->max_capacity() > 0, "initialization order");
  62   assert(_g1h->capacity() == 0, "initialization order");
  63 
  64   if (G1ConcRSHotCardLimit > 0) {
  65     // The max value we can store in the counts table is
  66     // max_jubyte. Guarantee the value of the hot
  67     // threshold limit is no more than this.
  68     guarantee(G1ConcRSHotCardLimit <= max_jubyte, "sanity");
  69 
  70     ModRefBarrierSet* bs = _g1h->mr_bs();
  71     guarantee(bs->is_a(BarrierSet::CardTableModRef), "Precondition");
  72     _ct_bs = (CardTableModRefBS*)bs;  
  73     _ct_bot = _ct_bs->byte_for_const(_g1h->reserved_region().start());
  74 
  75     // Allocate/Reserve the counts table
  76     size_t reserved_bytes = _g1h->max_capacity();
  77     _reserved_max_card_num = reserved_bytes >> CardTableModRefBS::card_shift;
  78 
  79     size_t reserved_size = _reserved_max_card_num * sizeof(jbyte);
  80     ReservedSpace rs(ReservedSpace::allocation_align_size_up(reserved_size));
  81     if (!rs.is_reserved()) {
  82       warning("Could not reserve enough space for the card counts table");
  83       guarantee(!has_reserved_count_table(), "should be NULL");
  84       return;
  85     }
  86 
  87     MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);
  88 
  89     _card_counts_storage.initialize(rs, 0);
  90     _card_counts = (jubyte*) _card_counts_storage.low();
  91   }
  92 }
  93 
  94 void G1CardCounts::resize(size_t heap_capacity) {
  95   // Expand the card counts table to handle a heap with the given capacity.
  96 
  97   if (!has_reserved_count_table()) {
  98     // Don't expand if we failed to reserve the card counts table.
  99     return;
 100   }
 101 
 102   assert(_committed_size ==
 103          ReservedSpace::allocation_align_size_up(_committed_size),
 104          err_msg("Unaligned? committed_size: " SIZE_FORMAT, _committed_size));
 105          
 106   // Verify that the committed space for the card counts
 107   // matches our committed max card num.
 108   size_t prev_committed_size = _committed_size;
 109   size_t prev_committed_card_num = prev_committed_size / sizeof(jbyte);
 110   assert(prev_committed_card_num == _committed_max_card_num,
 111          err_msg("Card mismatch: "
 112                  "prev: " SIZE_FORMAT ", "
 113                  "committed: "SIZE_FORMAT,
 114                  prev_committed_card_num, _committed_max_card_num));
 115 
 116   size_t new_size = (heap_capacity >> CardTableModRefBS::card_shift) * sizeof(jbyte);
 117   size_t new_committed_size = ReservedSpace::allocation_align_size_up(new_size);
 118   size_t new_committed_card_num =
 119                 MIN2(_reserved_max_card_num, new_committed_size / sizeof(jbyte));
 120 
 121   if (_committed_max_card_num < new_committed_card_num) {
 122     // we need to expand the backing store for the card counts
 123     size_t expand_size = new_committed_size - prev_committed_size;
 124 
 125     if (!_card_counts_storage.expand_by(expand_size)) {
 126       warning("Card counts table backing store commit failure");
 127       return;
 128     }
 129     assert(_card_counts_storage.committed_size() == new_committed_size,
 130            "expansion commit failure");
 131    
 132     _committed_size = new_committed_size;
 133     _committed_max_card_num = new_committed_card_num;
 134 
 135     clear_range(prev_committed_card_num, _committed_max_card_num);
 136   }
 137 }
 138 
 139 uint G1CardCounts::add_card_count(jbyte* card_ptr) {
 140   // Returns the number of times the card has been refined.
 141   // If we failed to reserve/commit the counts table, return 0.
 142   // If card_ptr is beyond the committed end of the counts table,
 143   // return 0.
 144   // Otherwise return the actual count.
 145   // Unless G1ConcRSHotCardLimit has been set appropriately,
 146   // returning 0 will result in the card being considered
 147   // cold and will be refined immediately.
 148   uint count = 0;
 149   if (has_count_table()) {
 150     size_t card_num = ptr_2_card_num(card_ptr);
 151     if (card_num < _committed_max_card_num) {
 152       count = (uint) _card_counts[card_num];
 153       if (count < G1ConcRSHotCardLimit) {
 154         _card_counts[card_num] += 1;
 155       }
 156       assert(_card_counts[card_num] <= G1ConcRSHotCardLimit,
 157              err_msg("Refinement count overflow? "
 158                      "new count: "UINT32_FORMAT,
 159                      (uint) _card_counts[card_num]));
 160     }
 161   }
 162   return count;
 163 }
 164 
 165 bool G1CardCounts::is_hot(uint count) {
 166   return (count >= G1ConcRSHotCardLimit);
 167 }
 168 
 169 void G1CardCounts::clear_region(HeapRegion* hr) {
 170   assert(!hr->isHumongous(), "Should have been cleared");
 171   if (has_count_table()) {
 172     HeapWord* bottom = hr->bottom();
 173 
 174     // We use the last address in hr as hr could be the
 175     // last region in the heap. In which case trying to find
 176     // the card for hr->end() will be an OOB accesss to the
 177     // card table.
 178     HeapWord* last = hr->end() - 1;
 179     assert(_g1h->g1_committed().contains(last),
 180            err_msg("last not in committed: "
 181                    "last: " PTR_FORMAT ", "
 182                    "committed: [" PTR_FORMAT ", " PTR_FORMAT ")",
 183                    last,
 184                    _g1h->g1_committed().start(),
 185                    _g1h->g1_committed().end()));
 186 
 187     const jbyte* from_card_ptr = _ct_bs->byte_for_const(bottom);
 188     const jbyte* last_card_ptr = _ct_bs->byte_for_const(last);
 189 
 190 #ifdef ASSERT
 191     HeapWord* start_addr = _ct_bs->addr_for(from_card_ptr);
 192     assert(start_addr == hr->bottom(), "alignment");
 193     HeapWord* last_addr = _ct_bs->addr_for(last_card_ptr);
 194     assert((last_addr + CardTableModRefBS::card_size_in_words) == hr->end(), "alignment");
 195 #endif // ASSERT
 196 
 197     // Clear the counts for the (exclusive) card range.
 198     size_t from_card_num = ptr_2_card_num(from_card_ptr);
 199     size_t to_card_num = ptr_2_card_num(last_card_ptr) + 1;
 200     clear_range(from_card_num, to_card_num);
 201   }
 202 }
 203 
 204 void G1CardCounts::clear_all() {
 205   assert(SafepointSynchronize::is_at_safepoint(), "don't call this otherwise");
 206   clear_range((size_t)0, _committed_max_card_num);
 207 }
 208 
 209 G1CardCounts::~G1CardCounts() {
 210   if (has_reserved_count_table()) {
 211     _card_counts_storage.release();
 212   }
 213 }
 214