src/share/vm/gc_implementation/g1/sparsePRT.hpp

Print this page
rev 6583 : 8047328: Change typedef CardIdx_t from int to uint16_t
Summary: Changed the typedef and made the necessary changes to code interacting with the SparsePRTEntry class.
Reviewed-by:
Contributed-by: andreas.sjoberg@oracle.com


  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP
  27 
  28 #include "gc_implementation/g1/g1CollectedHeap.hpp"
  29 #include "gc_implementation/g1/heapRegion.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "memory/cardTableModRefBS.hpp"
  32 #include "runtime/mutex.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 // Sparse remembered set for a heap region (the "owning" region).  Maps
  36 // indices of other regions to short sequences of cards in the other region
  37 // that might contain pointers into the owner region.
  38 
  39 // These tables only expand while they are accessed in parallel --
  40 // deletions may be done in single-threaded code.  This allows us to allow
  41 // unsynchronized reads/iterations, as long as expansions caused by
  42 // insertions only enqueue old versions for deletions, but do not delete
  43 // old versions synchronously.
  44 
  45 class SparsePRTEntry: public CHeapObj<mtGC> {
  46 public:
  47   enum SomePublicConstants {
  48     NullEntry     = -1,
  49     UnrollFactor  =  4
  50   };
  51 private:
  52   RegionIdx_t _region_ind;
  53   int         _next_index;
  54   CardIdx_t   _cards[1];

  55   // WARNING: Don't put any data members beyond this line. Card array has, in fact, variable length.
  56   // It should always be the last data member.




  57 public:
  58   // Returns the size of the entry, used for entry allocation.
  59   static size_t size() { return sizeof(SparsePRTEntry) + sizeof(CardIdx_t) * (cards_num() - 1); }
  60   // Returns the size of the card array.
  61   static int cards_num() {
  62     // The number of cards should be a multiple of 4, because that's our current
  63     // unrolling factor.
  64     static const int s = MAX2<int>(G1RSetSparseRegionEntries & ~(UnrollFactor - 1), UnrollFactor);
  65     return s;
  66   }
  67 
  68   // Set the region_ind to the given value, and delete all cards.
  69   inline void init(RegionIdx_t region_ind);
  70 
  71   RegionIdx_t r_ind() const { return _region_ind; }
  72   bool valid_entry() const { return r_ind() >= 0; }
  73   void set_r_ind(RegionIdx_t rind) { _region_ind = rind; }
  74 
  75   int next_index() const { return _next_index; }
  76   int* next_index_addr() { return &_next_index; }
  77   void set_next_index(int ni) { _next_index = ni; }
  78 
  79   // Returns "true" iff the entry contains the given card index.
  80   inline bool contains_card(CardIdx_t card_index) const;
  81 
  82   // Returns the number of non-NULL card entries.
  83   inline int num_valid_cards() const;
  84 
  85   // Requires that the entry not contain the given card index.  If there is
  86   // space available, add the given card index to the entry and return
  87   // "true"; otherwise, return "false" to indicate that the entry is full.
  88   enum AddCardResult {
  89     overflow,
  90     found,
  91     added
  92   };
  93   inline AddCardResult add_card(CardIdx_t card_index);
  94 
  95   // Copy the current entry's cards into "cards".
  96   inline void copy_cards(CardIdx_t* cards) const;
  97   // Copy the current entry's cards into the "_card" array of "e."
  98   inline void copy_cards(SparsePRTEntry* e) const;
  99 
 100   inline CardIdx_t card(int i) const { return _cards[i]; }




 101 };
 102 
 103 
 104 class RSHashTable : public CHeapObj<mtGC> {
 105 
 106   friend class RSHashTableIter;
 107 
 108   enum SomePrivateConstants {
 109     NullEntry = -1
 110   };
 111 
 112   size_t _capacity;
 113   size_t _capacity_mask;
 114   size_t _occupied_entries;
 115   size_t _occupied_cards;
 116 
 117   SparsePRTEntry* _entries;
 118   int* _buckets;
 119   int  _free_region;
 120   int  _free_list;
 121 
 122   // Requires that the caller hold a lock preventing parallel modifying
 123   // operations, and that the the table be less than completely full.  If
 124   // an entry for "region_ind" is already in the table, finds it and
 125   // returns its address; otherwise allocates, initializes, inserts and
 126   // returns a new entry for "region_ind".
 127   SparsePRTEntry* entry_for_region_ind_create(RegionIdx_t region_ind);
 128 
 129   // Returns the index of the next free entry in "_entries".
 130   int alloc_entry();
 131   // Declares the entry "fi" to be free.  (It must have already been
 132   // deleted from any bucket lists.
 133   void free_entry(int fi);
 134 
 135 public:
 136   RSHashTable(size_t capacity);
 137   ~RSHashTable();
 138 




 139   // Attempts to ensure that the given card_index in the given region is in
 140   // the sparse table.  If successful (because the card was already
 141   // present, or because it was successfully added) returns "true".
 142   // Otherwise, returns "false" to indicate that the addition would
 143   // overflow the entry for the region.  The caller must transfer these
 144   // entries to a larger-capacity representation.
 145   bool add_card(RegionIdx_t region_id, CardIdx_t card_index);
 146 
 147   bool get_cards(RegionIdx_t region_id, CardIdx_t* cards);
 148 
 149   bool delete_entry(RegionIdx_t region_id);
 150 
 151   bool contains_card(RegionIdx_t region_id, CardIdx_t card_index) const;
 152 
 153   void add_entry(SparsePRTEntry* e);
 154 
 155   SparsePRTEntry* get_entry(RegionIdx_t region_id) const;
 156 
 157   void clear();
 158 
 159   size_t capacity() const      { return _capacity;       }
 160   size_t capacity_mask() const { return _capacity_mask;  }
 161   size_t occupied_entries() const { return _occupied_entries; }
 162   size_t occupied_cards() const   { return _occupied_cards;   }
 163   size_t mem_size() const;
 164 
 165   SparsePRTEntry* entry(int i) const { return (SparsePRTEntry*)((char*)_entries + SparsePRTEntry::size() * i); }
 166 
 167   void print();
 168 };
 169 
 170 // ValueObj because will be embedded in HRRS iterator.
 171 class RSHashTableIter VALUE_OBJ_CLASS_SPEC {
 172   int _tbl_ind;         // [-1, 0.._rsht->_capacity)
 173   int _bl_ind;          // [-1, 0.._rsht->_capacity)
 174   short _card_ind;      // [0..SparsePRTEntry::cards_num())
 175   RSHashTable* _rsht;
 176 
 177   // If the bucket list pointed to by _bl_ind contains a card, sets
 178   // _bl_ind to the index of that entry, and returns the card.
 179   // Otherwise, returns SparseEntry::NullEntry.
 180   CardIdx_t find_first_card_in_list();

 181 
 182   // Computes the proper card index for the card whose offset in the
 183   // current region (as indicated by _bl_ind) is "ci".
 184   // This is subject to errors when there is iteration concurrent with
 185   // modification, but these errors should be benign.
 186   size_t compute_card_ind(CardIdx_t ci);
 187 
 188 public:
 189   RSHashTableIter(RSHashTable* rsht) :
 190     _tbl_ind(RSHashTable::NullEntry), // So that first increment gets to 0.
 191     _bl_ind(RSHashTable::NullEntry),
 192     _card_ind((SparsePRTEntry::cards_num() - 1)),
 193     _rsht(rsht) {}
 194 
 195   bool has_next(size_t& card_index);
 196 };
 197 
 198 // Concurrent access to a SparsePRT must be serialized by some external mutex.
 199 
 200 class SparsePRTIter;




  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP
  27 
  28 #include "gc_implementation/g1/g1CollectedHeap.hpp"
  29 #include "gc_implementation/g1/heapRegion.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "memory/cardTableModRefBS.hpp"
  32 #include "runtime/mutex.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 // Sparse remembered set for a heap region (the "owning" region).  Maps
  36 // indices of other regions to short sequences of cards in the other region
  37 // that might contain pointers into the owner region.
  38 
  39 // These tables only expand while they are accessed in parallel --
  40 // deletions may be done in single-threaded code.  This allows us to allow
  41 // unsynchronized reads/iterations, as long as expansions caused by
  42 // insertions only enqueue old versions for deletions, but do not delete
  43 // old versions synchronously.
  44 
  45 class SparsePRTEntry: public CHeapObj<mtGC> {





  46 private:
  47   RegionIdx_t _region_ind;
  48   int         _next_index;
  49   int         _next_null;
  50   CardIdx_t   _cards[4];
  51   // WARNING: Don't put any data members beyond this line. Card array has, in fact, variable length.
  52   // It should always be the last data member.
  53   // Note regarding the declared length of _cards:
  54   //  it is used to make sizeof(SparsePRTEntry) be an even multiple of 4, 
  55   //  in order to force alignment that could otherwise cause SIGBUS errors when
  56   //  reading the int variables.
  57 public:
  58   // Returns the size of the entry, used for entry allocation.
  59   static size_t size() { return sizeof(SparsePRTEntry) + sizeof(CardIdx_t) * (cards_num() - 4); }
  60   // Returns the size of the card array.
  61   static int cards_num() {
  62       return G1RSetSparseRegionEntries;



  63   }
  64 
  65   // Set the region_ind to the given value, and delete all cards.
  66   inline void init(RegionIdx_t region_ind);
  67 
  68   RegionIdx_t r_ind() const { return _region_ind; }
  69   bool valid_entry() const { return r_ind() >= 0; }
  70   void set_r_ind(RegionIdx_t rind) { _region_ind = rind; }
  71 
  72   int next_index() const { return _next_index; }
  73   int* next_index_addr() { return &_next_index; }
  74   void set_next_index(int ni) { _next_index = ni; }
  75 
  76   // Returns "true" iff the entry contains the given card index.
  77   inline bool contains_card(CardIdx_t card_index) const;
  78 
  79   // Returns the number of non-NULL card entries.
  80   inline int num_valid_cards() const { return _next_null; }
  81 
  82   // Requires that the entry not contain the given card index.  If there is
  83   // space available, add the given card index to the entry and return
  84   // "true"; otherwise, return "false" to indicate that the entry is full.
  85   enum AddCardResult {
  86     overflow,
  87     found,
  88     added
  89   };
  90   inline AddCardResult add_card(CardIdx_t card_index);
  91 
  92   // Copy the current entry's cards into "cards".
  93   inline void copy_cards(CardIdx_t* cards) const;
  94   // Copy the current entry's cards into the "_card" array of "e."
  95   inline void copy_cards(SparsePRTEntry* e) const;
  96 
  97   inline CardIdx_t card(int i) const {
  98       assert(i >= 0, "must be nonnegative");
  99       assert(i < cards_num(), "range checking");
 100       return _cards[i]; 
 101   }
 102 };
 103 
 104 
 105 class RSHashTable : public CHeapObj<mtGC> {
 106 
 107   friend class RSHashTableIter;
 108 



 109 
 110   size_t _capacity;
 111   size_t _capacity_mask;
 112   size_t _occupied_entries;
 113   size_t _occupied_cards;
 114 
 115   SparsePRTEntry* _entries;
 116   int* _buckets;
 117   int  _free_region;
 118   int  _free_list;
 119 
 120   // Requires that the caller hold a lock preventing parallel modifying
 121   // operations, and that the the table be less than completely full.  If
 122   // an entry for "region_ind" is already in the table, finds it and
 123   // returns its address; otherwise allocates, initializes, inserts and
 124   // returns a new entry for "region_ind".
 125   SparsePRTEntry* entry_for_region_ind_create(RegionIdx_t region_ind);
 126 
 127   // Returns the index of the next free entry in "_entries".
 128   int alloc_entry();
 129   // Declares the entry "fi" to be free.  (It must have already been
 130   // deleted from any bucket lists.
 131   void free_entry(int fi);
 132 
 133 public:
 134   RSHashTable(size_t capacity);
 135   ~RSHashTable();
 136   
 137   enum SomePublicConstants {
 138     NullEntry = -1
 139   };
 140 
 141   // Attempts to ensure that the given card_index in the given region is in
 142   // the sparse table.  If successful (because the card was already
 143   // present, or because it was successfully added) returns "true".
 144   // Otherwise, returns "false" to indicate that the addition would
 145   // overflow the entry for the region.  The caller must transfer these
 146   // entries to a larger-capacity representation.
 147   bool add_card(RegionIdx_t region_id, CardIdx_t card_index);
 148 
 149   bool get_cards(RegionIdx_t region_id, CardIdx_t* cards);
 150 
 151   bool delete_entry(RegionIdx_t region_id);
 152 
 153   bool contains_card(RegionIdx_t region_id, CardIdx_t card_index) const;
 154 
 155   void add_entry(SparsePRTEntry* e);
 156 
 157   SparsePRTEntry* get_entry(RegionIdx_t region_id) const;
 158 
 159   void clear();
 160 
 161   size_t capacity() const      { return _capacity; }
 162   size_t capacity_mask() const { return _capacity_mask;  }
 163   size_t occupied_entries() const { return _occupied_entries; }
 164   size_t occupied_cards() const   { return _occupied_cards; }
 165   size_t mem_size() const;
 166 
 167   SparsePRTEntry* entry(int i) const { return (SparsePRTEntry*)((char*)_entries + SparsePRTEntry::size() * i); }
 168 
 169   void print();
 170 };
 171 
 172 // ValueObj because will be embedded in HRRS iterator.
 173 class RSHashTableIter VALUE_OBJ_CLASS_SPEC {
 174   int _tbl_ind;         // [-1, 0.._rsht->_capacity)
 175   int _bl_ind;          // [-1, 0.._rsht->_capacity)
 176   short _card_ind;      // [0..SparsePRTEntry::cards_num())
 177   RSHashTable* _rsht;
 178 
 179   // If the bucket list pointed to by _bl_ind contains a card, sets
 180   // _bl_ind to the index of that entry, 
 181   //   updates the card reference to contain the found card and returns true.
 182   // Otherwise, returns false.
 183   bool find_first_card_in_list(CardIdx_t & );
 184 
 185   // Computes the proper card index for the card whose offset in the
 186   // current region (as indicated by _bl_ind) is "ci".
 187   // This is subject to errors when there is iteration concurrent with
 188   // modification, but these errors should be benign.
 189   size_t compute_card_ind(CardIdx_t ci);
 190 
 191 public:
 192   RSHashTableIter(RSHashTable* rsht) :
 193     _tbl_ind(RSHashTable::NullEntry), // So that first increment gets to 0.
 194     _bl_ind(RSHashTable::NullEntry),
 195     _card_ind((SparsePRTEntry::cards_num() - 1)),
 196     _rsht(rsht) {}
 197 
 198   bool has_next(size_t& card_index);
 199 };
 200 
 201 // Concurrent access to a SparsePRT must be serialized by some external mutex.
 202 
 203 class SparsePRTIter;