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 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1INCSETSTATE_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1INCSETSTATE_HPP
  27 
  28 #include "gc_implementation/g1/g1BiasedArray.hpp"
  29 #include "memory/allocation.hpp"
  30 
  31 // Per-region state during garbage collection.
  32 struct InCSetState {
  33  private:
  34   // We use different types to represent the state value. Particularly SPARC puts
  35   // values in structs from "left to right", i.e. MSB to LSB. This results in many
  36   // unnecessary shift operations when loading and storing values of this type.
  37   // This degrades performance significantly (>10%) on that platform.
  38   // Other tested ABIs do not seem to have this problem, and actually tend to
  39   // favor smaller types, so we use the smallest usable type there.
  40   SPARC_ONLY(typedef intptr_t in_cset_state_t;)
  41   NOT_SPARC(typedef int8_t in_cset_state_t;)
  42 
  43   in_cset_state_t _value;
  44  public:
  45   enum {
  46     // Selection of the values were driven to micro-optimize the encoding and
  47     // frequency of the checks.
  48     // The most common check is whether the region is in the collection set or not.
  49     // This encoding allows us to use an != 0 check which in some architectures
  50     // (x86*) can be encoded slightly more efficently than a normal comparison
  51     // against zero.
  52     // The same situation occurs when checking whether the region is humongous
  53     // or not, which is encoded by values < 0.
  54     // The other values are simply encoded in increasing generation order, which
  55     // makes getting the next generation fast by a simple increment.
  56     Humongous    = -1,    // The region is humongous - note that actually any value < 0 would be possible here.
  57     NotInCSet    =  0,    // The region is not in the collection set.
  58     Young        =  1,    // The region is in the collection set and a young region.
  59     Old          =  2,    // The region is in the collection set and an old region.
  60     Num
  61   };
  62 
  63   InCSetState() { _value = NotInCSet; }
  64   InCSetState(in_cset_state_t value)   { _value = value; }
  65 
  66   in_cset_state_t value() const        { return _value; }
  67 
  68   void set_old()                       { _value = Old; }
  69 
  70   bool is_not_in_cset() const          { return _value == NotInCSet; }
  71   bool is_in_cset_or_humongous() const { return _value != NotInCSet; }
  72   bool is_in_cset() const              { return _value > NotInCSet; }
  73   bool is_humongous() const            { return _value < NotInCSet; }
  74   bool is_young() const                { return _value == Young; }
  75   bool is_old() const                  { return _value == Old; }
  76 
  77 #ifdef ASSERT
  78   bool is_default() const              { return is_not_in_cset(); }
  79   bool is_valid() const                { return (_value >= Humongous) && (_value < Num); }
  80   bool is_valid_gen() const            { return (_value >= Young && _value <= Old); }
  81 #endif
  82 };
  83 
  84 // Instances of this class are used for quick tests on whether a reference points
  85 // into the collection set and into which generation or is a humongous object
  86 //
  87 // Each of the array's elements indicates whether the corresponding region is in
  88 // the collection set and if so in which generation, or a humongous region.
  89 //
  90 // We use this to speed up reference processing during young collection and
  91 // quickly reclaim humongous objects. For the latter, by making a humongous region
  92 // succeed this test, we sort-of add it to the collection set. During the reference
  93 // iteration closures, when we see a humongous region, we then simply mark it as
  94 // referenced, i.e. live.
  95 class G1InCSetStateFastTestBiasedMappedArray : public G1BiasedMappedArray<InCSetState> {
  96  protected:
  97   InCSetState default_value() const { return InCSetState::NotInCSet; }
  98  public:
  99   void set_humongous(uintptr_t index) {
 100     assert(get_by_index(index).is_default(),
 101            err_msg("State at index " INTPTR_FORMAT" should be default but is %d", index, get_by_index(index).value()));
 102     set_by_index(index, InCSetState::Humongous);
 103   }
 104 
 105   void clear_humongous(uintptr_t index) {
 106     set_by_index(index, InCSetState::NotInCSet);
 107   }
 108 
 109   void set_in_young(uintptr_t index) {
 110     assert(get_by_index(index).is_default(),
 111            err_msg("State at index " INTPTR_FORMAT" should be default but is %d", index, get_by_index(index).value()));
 112     set_by_index(index, InCSetState::Young);
 113   }
 114 
 115   void set_in_old(uintptr_t index) {
 116     assert(get_by_index(index).is_default(),
 117            err_msg("State at index " INTPTR_FORMAT" should be default but is %d", index, get_by_index(index).value()));
 118     set_by_index(index, InCSetState::Old);
 119   }
 120 
 121   bool is_in_cset_or_humongous(HeapWord* addr) const { return at(addr).is_in_cset_or_humongous(); }
 122   bool is_in_cset(HeapWord* addr) const { return at(addr).is_in_cset(); }
 123   InCSetState at(HeapWord* addr) const { return get_by_address(addr); }
 124   void clear() { G1BiasedMappedArray<InCSetState>::clear(); }
 125 };
 126 
 127 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1INCSETSTATE_HPP