1 /*
   2  * Copyright (c) 2013, 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 "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 #include "utilities/copy.hpp"
  33 
  34 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  35 
  36 void G1CardCountsMappingChangedListener::on_commit(uint start_idx, size_t num_regions) {
  37   MemRegion mr(G1CollectedHeap::heap()->bottom_addr_for_region(start_idx), num_regions * HeapRegion::GrainWords);
  38   _counts->clear_range(mr);
  39 }
  40 
  41 void G1CardCounts::clear_range(size_t from_card_num, size_t to_card_num) {
  42   if (has_count_table()) {
  43     assert(from_card_num < to_card_num,
  44            err_msg("Wrong order? from: " SIZE_FORMAT ", to: "SIZE_FORMAT,
  45                    from_card_num, to_card_num));
  46     Copy::fill_to_bytes(&_card_counts[from_card_num], (to_card_num - from_card_num));
  47   }
  48 }
  49 
  50 G1CardCounts::G1CardCounts(G1CollectedHeap *g1h):
  51   _listener(), _g1h(g1h), _card_counts(NULL), _reserved_max_card_num(0) {
  52   _listener.set_cardcounts(this);
  53 }
  54 
  55 void G1CardCounts::initialize(G1RegionToSpaceMapper* mapper) {
  56   assert(_g1h->max_capacity() > 0, "initialization order");
  57   assert(_g1h->capacity() == 0, "initialization order");
  58 
  59   if (G1ConcRSHotCardLimit > 0) {
  60     // The max value we can store in the counts table is
  61     // max_jubyte. Guarantee the value of the hot
  62     // threshold limit is no more than this.
  63     guarantee(G1ConcRSHotCardLimit <= max_jubyte, "sanity");
  64 
  65     _ct_bs = _g1h->g1_barrier_set();
  66     _ct_bot = _ct_bs->byte_for_const(_g1h->reserved_region().start());
  67 
  68     _card_counts = (jubyte*) mapper->reserved().start();
  69     _reserved_max_card_num = mapper->reserved().byte_size();
  70     mapper->set_mapping_changed_listener(&_listener);
  71   }
  72 }
  73 
  74 uint G1CardCounts::add_card_count(jbyte* card_ptr) {
  75   // Returns the number of times the card has been refined.
  76   // If we failed to reserve/commit the counts table, return 0.
  77   // If card_ptr is beyond the committed end of the counts table,
  78   // return 0.
  79   // Otherwise return the actual count.
  80   // Unless G1ConcRSHotCardLimit has been set appropriately,
  81   // returning 0 will result in the card being considered
  82   // cold and will be refined immediately.
  83   uint count = 0;
  84   if (has_count_table()) {
  85     size_t card_num = ptr_2_card_num(card_ptr);
  86     assert(card_num < _reserved_max_card_num,
  87            err_msg("Card "SIZE_FORMAT" outside of card counts table (max size "SIZE_FORMAT")",
  88                    card_num, _reserved_max_card_num));
  89     count = (uint) _card_counts[card_num];
  90     if (count < G1ConcRSHotCardLimit) {
  91       _card_counts[card_num] =
  92         (jubyte)(MIN2((uintx)(_card_counts[card_num] + 1), G1ConcRSHotCardLimit));
  93     }
  94   }
  95   return count;
  96 }
  97 
  98 bool G1CardCounts::is_hot(uint count) {
  99   return (count >= G1ConcRSHotCardLimit);
 100 }
 101 
 102 void G1CardCounts::clear_region(HeapRegion* hr) {
 103   MemRegion mr(hr->bottom(), hr->end());
 104   clear_range(mr);
 105 }
 106 
 107 void G1CardCounts::clear_range(MemRegion mr) {
 108   if (has_count_table()) {
 109     const jbyte* from_card_ptr = _ct_bs->byte_for_const(mr.start());
 110     // We use the last address in the range as the range could represent the
 111     // last region in the heap. In which case trying to find the card will be an
 112     // OOB access to the card table.
 113     const jbyte* last_card_ptr = _ct_bs->byte_for_const(mr.last());
 114 
 115 #ifdef ASSERT
 116     HeapWord* start_addr = _ct_bs->addr_for(from_card_ptr);
 117     assert(start_addr == mr.start(), "MemRegion start must be aligned to a card.");
 118     HeapWord* last_addr = _ct_bs->addr_for(last_card_ptr);
 119     assert((last_addr + CardTableModRefBS::card_size_in_words) == mr.end(), "MemRegion end must be aligned to a card.");
 120 #endif // ASSERT
 121 
 122     // Clear the counts for the (exclusive) card range.
 123     size_t from_card_num = ptr_2_card_num(from_card_ptr);
 124     size_t to_card_num = ptr_2_card_num(last_card_ptr) + 1;
 125     clear_range(from_card_num, to_card_num);
 126   }
 127 }
 128 
 129 class G1CardCountsClearClosure : public HeapRegionClosure {
 130  private:
 131   G1CardCounts* _card_counts;
 132  public:
 133   G1CardCountsClearClosure(G1CardCounts* card_counts) :
 134     HeapRegionClosure(), _card_counts(card_counts) { }
 135 
 136 
 137   virtual bool doHeapRegion(HeapRegion* r) {
 138     _card_counts->clear_region(r);
 139     return false;
 140   }
 141 };
 142 
 143 void G1CardCounts::clear_all() {
 144   assert(SafepointSynchronize::is_at_safepoint(), "don't call this otherwise");
 145   G1CardCountsClearClosure cl(this);
 146   _g1h->heap_region_iterate(&cl);
 147 }