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

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6541
bfdf528be8e8
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP
aoqi@0 26 #define SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP
aoqi@0 27
aoqi@0 28 #include "gc_implementation/g1/g1CollectedHeap.hpp"
aoqi@0 29 #include "gc_implementation/g1/heapRegion.hpp"
aoqi@0 30 #include "memory/allocation.hpp"
aoqi@0 31 #include "memory/cardTableModRefBS.hpp"
aoqi@0 32 #include "runtime/mutex.hpp"
aoqi@0 33 #include "utilities/globalDefinitions.hpp"
aoqi@0 34
aoqi@0 35 // Sparse remembered set for a heap region (the "owning" region). Maps
aoqi@0 36 // indices of other regions to short sequences of cards in the other region
aoqi@0 37 // that might contain pointers into the owner region.
aoqi@0 38
aoqi@0 39 // These tables only expand while they are accessed in parallel --
aoqi@0 40 // deletions may be done in single-threaded code. This allows us to allow
aoqi@0 41 // unsynchronized reads/iterations, as long as expansions caused by
aoqi@0 42 // insertions only enqueue old versions for deletions, but do not delete
aoqi@0 43 // old versions synchronously.
aoqi@0 44
aoqi@0 45 class SparsePRTEntry: public CHeapObj<mtGC> {
aoqi@0 46 public:
aoqi@0 47 enum SomePublicConstants {
aoqi@0 48 NullEntry = -1,
aoqi@0 49 UnrollFactor = 4
aoqi@0 50 };
aoqi@0 51 private:
aoqi@0 52 RegionIdx_t _region_ind;
aoqi@0 53 int _next_index;
aoqi@0 54 CardIdx_t _cards[1];
aoqi@0 55 // WARNING: Don't put any data members beyond this line. Card array has, in fact, variable length.
aoqi@0 56 // It should always be the last data member.
aoqi@0 57 public:
aoqi@0 58 // Returns the size of the entry, used for entry allocation.
aoqi@0 59 static size_t size() { return sizeof(SparsePRTEntry) + sizeof(CardIdx_t) * (cards_num() - 1); }
aoqi@0 60 // Returns the size of the card array.
aoqi@0 61 static int cards_num() {
aoqi@0 62 // The number of cards should be a multiple of 4, because that's our current
aoqi@0 63 // unrolling factor.
aoqi@0 64 static const int s = MAX2<int>(G1RSetSparseRegionEntries & ~(UnrollFactor - 1), UnrollFactor);
aoqi@0 65 return s;
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 // Set the region_ind to the given value, and delete all cards.
aoqi@0 69 inline void init(RegionIdx_t region_ind);
aoqi@0 70
aoqi@0 71 RegionIdx_t r_ind() const { return _region_ind; }
aoqi@0 72 bool valid_entry() const { return r_ind() >= 0; }
aoqi@0 73 void set_r_ind(RegionIdx_t rind) { _region_ind = rind; }
aoqi@0 74
aoqi@0 75 int next_index() const { return _next_index; }
aoqi@0 76 int* next_index_addr() { return &_next_index; }
aoqi@0 77 void set_next_index(int ni) { _next_index = ni; }
aoqi@0 78
aoqi@0 79 // Returns "true" iff the entry contains the given card index.
aoqi@0 80 inline bool contains_card(CardIdx_t card_index) const;
aoqi@0 81
aoqi@0 82 // Returns the number of non-NULL card entries.
aoqi@0 83 inline int num_valid_cards() const;
aoqi@0 84
aoqi@0 85 // Requires that the entry not contain the given card index. If there is
aoqi@0 86 // space available, add the given card index to the entry and return
aoqi@0 87 // "true"; otherwise, return "false" to indicate that the entry is full.
aoqi@0 88 enum AddCardResult {
aoqi@0 89 overflow,
aoqi@0 90 found,
aoqi@0 91 added
aoqi@0 92 };
aoqi@0 93 inline AddCardResult add_card(CardIdx_t card_index);
aoqi@0 94
aoqi@0 95 // Copy the current entry's cards into "cards".
aoqi@0 96 inline void copy_cards(CardIdx_t* cards) const;
aoqi@0 97 // Copy the current entry's cards into the "_card" array of "e."
aoqi@0 98 inline void copy_cards(SparsePRTEntry* e) const;
aoqi@0 99
aoqi@0 100 inline CardIdx_t card(int i) const { return _cards[i]; }
aoqi@0 101 };
aoqi@0 102
aoqi@0 103
aoqi@0 104 class RSHashTable : public CHeapObj<mtGC> {
aoqi@0 105
aoqi@0 106 friend class RSHashTableIter;
aoqi@0 107
aoqi@0 108 enum SomePrivateConstants {
aoqi@0 109 NullEntry = -1
aoqi@0 110 };
aoqi@0 111
aoqi@0 112 size_t _capacity;
aoqi@0 113 size_t _capacity_mask;
aoqi@0 114 size_t _occupied_entries;
aoqi@0 115 size_t _occupied_cards;
aoqi@0 116
aoqi@0 117 SparsePRTEntry* _entries;
aoqi@0 118 int* _buckets;
aoqi@0 119 int _free_region;
aoqi@0 120 int _free_list;
aoqi@0 121
aoqi@0 122 // Requires that the caller hold a lock preventing parallel modifying
aoqi@0 123 // operations, and that the the table be less than completely full. If
aoqi@0 124 // an entry for "region_ind" is already in the table, finds it and
aoqi@0 125 // returns its address; otherwise returns "NULL."
aoqi@0 126 SparsePRTEntry* entry_for_region_ind(RegionIdx_t region_ind) const;
aoqi@0 127
aoqi@0 128 // Requires that the caller hold a lock preventing parallel modifying
aoqi@0 129 // operations, and that the the table be less than completely full. If
aoqi@0 130 // an entry for "region_ind" is already in the table, finds it and
aoqi@0 131 // returns its address; otherwise allocates, initializes, inserts and
aoqi@0 132 // returns a new entry for "region_ind".
aoqi@0 133 SparsePRTEntry* entry_for_region_ind_create(RegionIdx_t region_ind);
aoqi@0 134
aoqi@0 135 // Returns the index of the next free entry in "_entries".
aoqi@0 136 int alloc_entry();
aoqi@0 137 // Declares the entry "fi" to be free. (It must have already been
aoqi@0 138 // deleted from any bucket lists.
aoqi@0 139 void free_entry(int fi);
aoqi@0 140
aoqi@0 141 public:
aoqi@0 142 RSHashTable(size_t capacity);
aoqi@0 143 ~RSHashTable();
aoqi@0 144
aoqi@0 145 // Attempts to ensure that the given card_index in the given region is in
aoqi@0 146 // the sparse table. If successful (because the card was already
aoqi@0 147 // present, or because it was successfullly added) returns "true".
aoqi@0 148 // Otherwise, returns "false" to indicate that the addition would
aoqi@0 149 // overflow the entry for the region. The caller must transfer these
aoqi@0 150 // entries to a larger-capacity representation.
aoqi@0 151 bool add_card(RegionIdx_t region_id, CardIdx_t card_index);
aoqi@0 152
aoqi@0 153 bool get_cards(RegionIdx_t region_id, CardIdx_t* cards);
aoqi@0 154
aoqi@0 155 bool delete_entry(RegionIdx_t region_id);
aoqi@0 156
aoqi@0 157 bool contains_card(RegionIdx_t region_id, CardIdx_t card_index) const;
aoqi@0 158
aoqi@0 159 void add_entry(SparsePRTEntry* e);
aoqi@0 160
aoqi@0 161 SparsePRTEntry* get_entry(RegionIdx_t region_id);
aoqi@0 162
aoqi@0 163 void clear();
aoqi@0 164
aoqi@0 165 size_t capacity() const { return _capacity; }
aoqi@0 166 size_t capacity_mask() const { return _capacity_mask; }
aoqi@0 167 size_t occupied_entries() const { return _occupied_entries; }
aoqi@0 168 size_t occupied_cards() const { return _occupied_cards; }
aoqi@0 169 size_t mem_size() const;
aoqi@0 170
aoqi@0 171 SparsePRTEntry* entry(int i) const { return (SparsePRTEntry*)((char*)_entries + SparsePRTEntry::size() * i); }
aoqi@0 172
aoqi@0 173 void print();
aoqi@0 174 };
aoqi@0 175
aoqi@0 176 // ValueObj because will be embedded in HRRS iterator.
aoqi@0 177 class RSHashTableIter VALUE_OBJ_CLASS_SPEC {
aoqi@0 178 int _tbl_ind; // [-1, 0.._rsht->_capacity)
aoqi@0 179 int _bl_ind; // [-1, 0.._rsht->_capacity)
aoqi@0 180 short _card_ind; // [0..SparsePRTEntry::cards_num())
aoqi@0 181 RSHashTable* _rsht;
aoqi@0 182
aoqi@0 183 // If the bucket list pointed to by _bl_ind contains a card, sets
aoqi@0 184 // _bl_ind to the index of that entry, and returns the card.
aoqi@0 185 // Otherwise, returns SparseEntry::NullEntry.
aoqi@0 186 CardIdx_t find_first_card_in_list();
aoqi@0 187
aoqi@0 188 // Computes the proper card index for the card whose offset in the
aoqi@0 189 // current region (as indicated by _bl_ind) is "ci".
aoqi@0 190 // This is subject to errors when there is iteration concurrent with
aoqi@0 191 // modification, but these errors should be benign.
aoqi@0 192 size_t compute_card_ind(CardIdx_t ci);
aoqi@0 193
aoqi@0 194 public:
aoqi@0 195 RSHashTableIter(RSHashTable* rsht) :
aoqi@0 196 _tbl_ind(RSHashTable::NullEntry), // So that first increment gets to 0.
aoqi@0 197 _bl_ind(RSHashTable::NullEntry),
aoqi@0 198 _card_ind((SparsePRTEntry::cards_num() - 1)),
aoqi@0 199 _rsht(rsht) {}
aoqi@0 200
aoqi@0 201 bool has_next(size_t& card_index);
aoqi@0 202 };
aoqi@0 203
aoqi@0 204 // Concurrent accesss to a SparsePRT must be serialized by some external
aoqi@0 205 // mutex.
aoqi@0 206
aoqi@0 207 class SparsePRTIter;
aoqi@0 208 class SparsePRTCleanupTask;
aoqi@0 209
aoqi@0 210 class SparsePRT VALUE_OBJ_CLASS_SPEC {
aoqi@0 211 friend class SparsePRTCleanupTask;
aoqi@0 212
aoqi@0 213 // Iterations are done on the _cur hash table, since they only need to
aoqi@0 214 // see entries visible at the start of a collection pause.
aoqi@0 215 // All other operations are done using the _next hash table.
aoqi@0 216 RSHashTable* _cur;
aoqi@0 217 RSHashTable* _next;
aoqi@0 218
aoqi@0 219 HeapRegion* _hr;
aoqi@0 220
aoqi@0 221 enum SomeAdditionalPrivateConstants {
aoqi@0 222 InitialCapacity = 16
aoqi@0 223 };
aoqi@0 224
aoqi@0 225 void expand();
aoqi@0 226
aoqi@0 227 bool _expanded;
aoqi@0 228
aoqi@0 229 bool expanded() { return _expanded; }
aoqi@0 230 void set_expanded(bool b) { _expanded = b; }
aoqi@0 231
aoqi@0 232 SparsePRT* _next_expanded;
aoqi@0 233
aoqi@0 234 SparsePRT* next_expanded() { return _next_expanded; }
aoqi@0 235 void set_next_expanded(SparsePRT* nxt) { _next_expanded = nxt; }
aoqi@0 236
aoqi@0 237 bool should_be_on_expanded_list();
aoqi@0 238
aoqi@0 239 static SparsePRT* _head_expanded_list;
aoqi@0 240
aoqi@0 241 public:
aoqi@0 242 SparsePRT(HeapRegion* hr);
aoqi@0 243
aoqi@0 244 ~SparsePRT();
aoqi@0 245
aoqi@0 246 size_t occupied() const { return _next->occupied_cards(); }
aoqi@0 247 size_t mem_size() const;
aoqi@0 248
aoqi@0 249 // Attempts to ensure that the given card_index in the given region is in
aoqi@0 250 // the sparse table. If successful (because the card was already
aoqi@0 251 // present, or because it was successfullly added) returns "true".
aoqi@0 252 // Otherwise, returns "false" to indicate that the addition would
aoqi@0 253 // overflow the entry for the region. The caller must transfer these
aoqi@0 254 // entries to a larger-capacity representation.
aoqi@0 255 bool add_card(RegionIdx_t region_id, CardIdx_t card_index);
aoqi@0 256
aoqi@0 257 // If the table hold an entry for "region_ind", Copies its
aoqi@0 258 // cards into "cards", which must be an array of length at least
aoqi@0 259 // "SparePRTEntry::cards_num()", and returns "true"; otherwise,
aoqi@0 260 // returns "false".
aoqi@0 261 bool get_cards(RegionIdx_t region_ind, CardIdx_t* cards);
aoqi@0 262
aoqi@0 263 // Return the pointer to the entry associated with the given region.
aoqi@0 264 SparsePRTEntry* get_entry(RegionIdx_t region_ind);
aoqi@0 265
aoqi@0 266 // If there is an entry for "region_ind", removes it and return "true";
aoqi@0 267 // otherwise returns "false."
aoqi@0 268 bool delete_entry(RegionIdx_t region_ind);
aoqi@0 269
aoqi@0 270 // Clear the table, and reinitialize to initial capacity.
aoqi@0 271 void clear();
aoqi@0 272
aoqi@0 273 // Ensure that "_cur" and "_next" point to the same table.
aoqi@0 274 void cleanup();
aoqi@0 275
aoqi@0 276 // Clean up all tables on the expanded list. Called single threaded.
aoqi@0 277 static void cleanup_all();
aoqi@0 278 RSHashTable* cur() const { return _cur; }
aoqi@0 279
aoqi@0 280 static void add_to_expanded_list(SparsePRT* sprt);
aoqi@0 281 static SparsePRT* get_from_expanded_list();
aoqi@0 282
aoqi@0 283 // The purpose of these three methods is to help the GC workers
aoqi@0 284 // during the cleanup pause to recreate the expanded list, purging
aoqi@0 285 // any tables from it that belong to regions that are freed during
aoqi@0 286 // cleanup (if we don't purge those tables, there is a race that
aoqi@0 287 // causes various crashes; see CR 7014261).
aoqi@0 288 //
aoqi@0 289 // We chose to recreate the expanded list, instead of purging
aoqi@0 290 // entries from it by iterating over it, to avoid this serial phase
aoqi@0 291 // at the end of the cleanup pause.
aoqi@0 292 //
aoqi@0 293 // The three methods below work as follows:
aoqi@0 294 // * reset_for_cleanup_tasks() : Nulls the expanded list head at the
aoqi@0 295 // start of the cleanup pause.
aoqi@0 296 // * do_cleanup_work() : Called by the cleanup workers for every
aoqi@0 297 // region that is not free / is being freed by the cleanup
aoqi@0 298 // pause. It creates a list of expanded tables whose head / tail
aoqi@0 299 // are on the thread-local SparsePRTCleanupTask object.
aoqi@0 300 // * finish_cleanup_task() : Called by the cleanup workers after
aoqi@0 301 // they complete their cleanup task. It adds the local list into
aoqi@0 302 // the global expanded list. It assumes that the
aoqi@0 303 // ParGCRareEvent_lock is being held to ensure MT-safety.
aoqi@0 304 static void reset_for_cleanup_tasks();
aoqi@0 305 void do_cleanup_work(SparsePRTCleanupTask* sprt_cleanup_task);
aoqi@0 306 static void finish_cleanup_task(SparsePRTCleanupTask* sprt_cleanup_task);
aoqi@0 307
aoqi@0 308 bool contains_card(RegionIdx_t region_id, CardIdx_t card_index) const {
aoqi@0 309 return _next->contains_card(region_id, card_index);
aoqi@0 310 }
aoqi@0 311 };
aoqi@0 312
aoqi@0 313 class SparsePRTIter: public RSHashTableIter {
aoqi@0 314 public:
aoqi@0 315 SparsePRTIter(const SparsePRT* sprt) :
aoqi@0 316 RSHashTableIter(sprt->cur()) {}
aoqi@0 317
aoqi@0 318 bool has_next(size_t& card_index) {
aoqi@0 319 return RSHashTableIter::has_next(card_index);
aoqi@0 320 }
aoqi@0 321 };
aoqi@0 322
aoqi@0 323 // This allows each worker during a cleanup pause to create a
aoqi@0 324 // thread-local list of sparse tables that have been expanded and need
aoqi@0 325 // to be processed at the beginning of the next GC pause. This lists
aoqi@0 326 // are concatenated into the single expanded list at the end of the
aoqi@0 327 // cleanup pause.
aoqi@0 328 class SparsePRTCleanupTask VALUE_OBJ_CLASS_SPEC {
aoqi@0 329 private:
aoqi@0 330 SparsePRT* _head;
aoqi@0 331 SparsePRT* _tail;
aoqi@0 332
aoqi@0 333 public:
aoqi@0 334 SparsePRTCleanupTask() : _head(NULL), _tail(NULL) { }
aoqi@0 335
aoqi@0 336 void add(SparsePRT* sprt);
aoqi@0 337 SparsePRT* head() { return _head; }
aoqi@0 338 SparsePRT* tail() { return _tail; }
aoqi@0 339 };
aoqi@0 340
aoqi@0 341 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP

mercurial