sparsePRT.hpp 10.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34
#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP
#define SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP

#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
#include "gc_implementation/g1/heapRegion.hpp"
#include "memory/allocation.hpp"
#include "memory/cardTableModRefBS.hpp"
#include "runtime/mutex.hpp"
#include "utilities/globalDefinitions.hpp"

35 36 37 38 39 40 41 42 43 44
// Sparse remembered set for a heap region (the "owning" region).  Maps
// indices of other regions to short sequences of cards in the other region
// that might contain pointers into the owner region.

// These tables only expand while they are accessed in parallel --
// deletions may be done in single-threaded code.  This allows us to allow
// unsynchronized reads/iterations, as long as expansions caused by
// insertions only enqueue old versions for deletions, but do not delete
// old versions synchronously.

45
class SparsePRTEntry: public CHeapObj {
46 47
public:
  enum SomePublicConstants {
48 49
    NullEntry     = -1,
    UnrollFactor  =  4
50 51
  };
private:
52 53
  RegionIdx_t _region_ind;
  int         _next_index;
54 55 56
  CardIdx_t   _cards[1];
  // WARNING: Don't put any data members beyond this line. Card array has, in fact, variable length.
  // It should always be the last data member.
57
public:
58 59 60 61 62 63 64 65 66
  // Returns the size of the entry, used for entry allocation.
  static size_t size() { return sizeof(SparsePRTEntry) + sizeof(CardIdx_t) * (cards_num() - 1); }
  // Returns the size of the card array.
  static int cards_num() {
    // The number of cards should be a multiple of 4, because that's our current
    // unrolling factor.
    static const int s = MAX2<int>(G1RSetSparseRegionEntries & ~(UnrollFactor - 1), UnrollFactor);
    return s;
  }
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 76 77
  int next_index() const { return _next_index; }
  int* next_index_addr() { return &_next_index; }
  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 83 84 85 86 87 88 89 90 91 92

  // Returns the number of non-NULL card entries.
  inline int num_valid_cards() const;

  // Requires that the entry not contain the given card index.  If there is
  // space available, add the given card index to the entry and return
  // "true"; otherwise, return "false" to indicate that the entry is full.
  enum AddCardResult {
    overflow,
    found,
    added
  };
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 98 99
  // Copy the current entry's cards into the "_card" array of "e."
  inline void copy_cards(SparsePRTEntry* e) const;

100
  inline CardIdx_t card(int i) const { return _cards[i]; }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
};


class RSHashTable : public CHeapObj {

  friend class RSHashTableIter;

  enum SomePrivateConstants {
    NullEntry = -1
  };

  size_t _capacity;
  size_t _capacity_mask;
  size_t _occupied_entries;
  size_t _occupied_cards;

  SparsePRTEntry* _entries;
118 119 120
  int* _buckets;
  int  _free_region;
  int  _free_list;
121 122 123 124 125

  // Requires that the caller hold a lock preventing parallel modifying
  // operations, and that the the table be less than completely full.  If
  // an entry for "region_ind" is already in the table, finds it and
  // returns its address; otherwise returns "NULL."
126
  SparsePRTEntry* entry_for_region_ind(RegionIdx_t region_ind) const;
127 128 129 130 131 132

  // Requires that the caller hold a lock preventing parallel modifying
  // operations, and that the the table be less than completely full.  If
  // an entry for "region_ind" is already in the table, finds it and
  // returns its address; otherwise allocates, initializes, inserts and
  // returns a new entry for "region_ind".
133
  SparsePRTEntry* entry_for_region_ind_create(RegionIdx_t region_ind);
134 135

  // Returns the index of the next free entry in "_entries".
136
  int alloc_entry();
137 138
  // Declares the entry "fi" to be free.  (It must have already been
  // deleted from any bucket lists.
139
  void free_entry(int fi);
140 141 142 143 144 145 146 147 148 149 150

public:
  RSHashTable(size_t capacity);
  ~RSHashTable();

  // Attempts to ensure that the given card_index in the given region is in
  // the sparse table.  If successful (because the card was already
  // present, or because it was successfullly added) returns "true".
  // Otherwise, returns "false" to indicate that the addition would
  // overflow the entry for the region.  The caller must transfer these
  // entries to a larger-capacity representation.
151
  bool add_card(RegionIdx_t region_id, CardIdx_t card_index);
152

153
  bool get_cards(RegionIdx_t region_id, CardIdx_t* cards);
154

155
  bool delete_entry(RegionIdx_t region_id);
156

157
  bool contains_card(RegionIdx_t region_id, CardIdx_t card_index) const;
158 159 160

  void add_entry(SparsePRTEntry* e);

161 162
  SparsePRTEntry* get_entry(RegionIdx_t region_id);

163 164 165 166 167 168 169 170
  void clear();

  size_t capacity() const      { return _capacity;       }
  size_t capacity_mask() const { return _capacity_mask;  }
  size_t occupied_entries() const { return _occupied_entries; }
  size_t occupied_cards() const   { return _occupied_cards;   }
  size_t mem_size() const;

171
  SparsePRTEntry* entry(int i) const { return (SparsePRTEntry*)((char*)_entries + SparsePRTEntry::size() * i); }
172 173 174 175

  void print();
};

176
// ValueObj because will be embedded in HRRS iterator.
177
class RSHashTableIter VALUE_OBJ_CLASS_SPEC {
178 179
  int _tbl_ind;         // [-1, 0.._rsht->_capacity)
  int _bl_ind;          // [-1, 0.._rsht->_capacity)
180
  short _card_ind;      // [0..SparsePRTEntry::cards_num())
181
  RSHashTable* _rsht;
182

183 184 185 186 187 188 189 190 191 192 193 194
  // If the bucket list pointed to by _bl_ind contains a card, sets
  // _bl_ind to the index of that entry, and returns the card.
  // Otherwise, returns SparseEntry::NullEntry.
  CardIdx_t find_first_card_in_list();

  // Computes the proper card index for the card whose offset in the
  // current region (as indicated by _bl_ind) is "ci".
  // This is subject to errors when there is iteration concurrent with
  // modification, but these errors should be benign.
  size_t compute_card_ind(CardIdx_t ci);

public:
195
  RSHashTableIter() :
196 197
    _tbl_ind(RSHashTable::NullEntry),
    _bl_ind(RSHashTable::NullEntry),
198
    _card_ind((SparsePRTEntry::cards_num() - 1)),
199
    _rsht(NULL) {}
200 201 202 203 204

  void init(RSHashTable* rsht) {
    _rsht = rsht;
    _tbl_ind = -1; // So that first increment gets to 0.
    _bl_ind = RSHashTable::NullEntry;
205
    _card_ind = (SparsePRTEntry::cards_num() - 1);
206 207 208 209 210
  }

  bool has_next(size_t& card_index);
};

211 212 213 214 215
// Concurrent accesss to a SparsePRT must be serialized by some external
// mutex.

class SparsePRTIter;

216
class SparsePRT VALUE_OBJ_CLASS_SPEC {
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  //  Iterations are done on the _cur hash table, since they only need to
  //  see entries visible at the start of a collection pause.
  //  All other operations are done using the _next hash table.
  RSHashTable* _cur;
  RSHashTable* _next;

  HeapRegion* _hr;

  enum SomeAdditionalPrivateConstants {
    InitialCapacity = 16
  };

  void expand();

  bool _expanded;

  bool expanded() { return _expanded; }
  void set_expanded(bool b) { _expanded = b; }

  SparsePRT* _next_expanded;

  SparsePRT* next_expanded() { return _next_expanded; }
  void set_next_expanded(SparsePRT* nxt) { _next_expanded = nxt; }

  static SparsePRT* _head_expanded_list;

public:
  SparsePRT(HeapRegion* hr);

  ~SparsePRT();

  size_t occupied() const { return _next->occupied_cards(); }
  size_t mem_size() const;

  // Attempts to ensure that the given card_index in the given region is in
  // the sparse table.  If successful (because the card was already
  // present, or because it was successfullly added) returns "true".
  // Otherwise, returns "false" to indicate that the addition would
  // overflow the entry for the region.  The caller must transfer these
  // entries to a larger-capacity representation.
257
  bool add_card(RegionIdx_t region_id, CardIdx_t card_index);
258 259 260

  // If the table hold an entry for "region_ind",  Copies its
  // cards into "cards", which must be an array of length at least
261 262
  // "SparePRTEntry::cards_num()", and returns "true"; otherwise,
  // returns "false".
263
  bool get_cards(RegionIdx_t region_ind, CardIdx_t* cards);
264

265 266 267
  // Return the pointer to the entry associated with the given region.
  SparsePRTEntry* get_entry(RegionIdx_t region_ind);

268 269
  // If there is an entry for "region_ind", removes it and return "true";
  // otherwise returns "false."
270
  bool delete_entry(RegionIdx_t region_ind);
271 272 273 274 275 276 277 278 279

  // Clear the table, and reinitialize to initial capacity.
  void clear();

  // Ensure that "_cur" and "_next" point to the same table.
  void cleanup();

  // Clean up all tables on the expanded list.  Called single threaded.
  static void cleanup_all();
280
  RSHashTable* cur() const { return _cur; }
281 282 283 284 285 286

  void init_iterator(SparsePRTIter* sprt_iter);

  static void add_to_expanded_list(SparsePRT* sprt);
  static SparsePRT* get_from_expanded_list();

287
  bool contains_card(RegionIdx_t region_id, CardIdx_t card_index) const {
288 289 290 291 292
    return _next->contains_card(region_id, card_index);
  }
};


293
class SparsePRTIter: public RSHashTableIter {
294 295
public:
  void init(const SparsePRT* sprt) {
296
    RSHashTableIter::init(sprt->cur());
297 298 299 300 301
  }
  bool has_next(size_t& card_index) {
    return RSHashTableIter::has_next(card_index);
  }
};
302 303

#endif // SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP