aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP aoqi@0: #define SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP aoqi@0: aoqi@0: #include "gc_implementation/g1/g1CollectedHeap.hpp" aoqi@0: #include "gc_implementation/g1/heapRegion.hpp" aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "memory/cardTableModRefBS.hpp" aoqi@0: #include "runtime/mutex.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: aoqi@0: // Sparse remembered set for a heap region (the "owning" region). Maps aoqi@0: // indices of other regions to short sequences of cards in the other region aoqi@0: // that might contain pointers into the owner region. aoqi@0: aoqi@0: // These tables only expand while they are accessed in parallel -- aoqi@0: // deletions may be done in single-threaded code. This allows us to allow aoqi@0: // unsynchronized reads/iterations, as long as expansions caused by aoqi@0: // insertions only enqueue old versions for deletions, but do not delete aoqi@0: // old versions synchronously. aoqi@0: aoqi@0: class SparsePRTEntry: public CHeapObj { aoqi@0: public: aoqi@0: enum SomePublicConstants { aoqi@0: NullEntry = -1, aoqi@0: UnrollFactor = 4 aoqi@0: }; aoqi@0: private: aoqi@0: RegionIdx_t _region_ind; aoqi@0: int _next_index; aoqi@0: CardIdx_t _cards[1]; aoqi@0: // WARNING: Don't put any data members beyond this line. Card array has, in fact, variable length. aoqi@0: // It should always be the last data member. aoqi@0: public: aoqi@0: // Returns the size of the entry, used for entry allocation. aoqi@0: static size_t size() { return sizeof(SparsePRTEntry) + sizeof(CardIdx_t) * (cards_num() - 1); } aoqi@0: // Returns the size of the card array. aoqi@0: static int cards_num() { aoqi@0: // The number of cards should be a multiple of 4, because that's our current aoqi@0: // unrolling factor. aoqi@0: static const int s = MAX2(G1RSetSparseRegionEntries & ~(UnrollFactor - 1), UnrollFactor); aoqi@0: return s; aoqi@0: } aoqi@0: aoqi@0: // Set the region_ind to the given value, and delete all cards. aoqi@0: inline void init(RegionIdx_t region_ind); aoqi@0: aoqi@0: RegionIdx_t r_ind() const { return _region_ind; } aoqi@0: bool valid_entry() const { return r_ind() >= 0; } aoqi@0: void set_r_ind(RegionIdx_t rind) { _region_ind = rind; } aoqi@0: aoqi@0: int next_index() const { return _next_index; } aoqi@0: int* next_index_addr() { return &_next_index; } aoqi@0: void set_next_index(int ni) { _next_index = ni; } aoqi@0: aoqi@0: // Returns "true" iff the entry contains the given card index. aoqi@0: inline bool contains_card(CardIdx_t card_index) const; aoqi@0: aoqi@0: // Returns the number of non-NULL card entries. aoqi@0: inline int num_valid_cards() const; aoqi@0: aoqi@0: // Requires that the entry not contain the given card index. If there is aoqi@0: // space available, add the given card index to the entry and return aoqi@0: // "true"; otherwise, return "false" to indicate that the entry is full. aoqi@0: enum AddCardResult { aoqi@0: overflow, aoqi@0: found, aoqi@0: added aoqi@0: }; aoqi@0: inline AddCardResult add_card(CardIdx_t card_index); aoqi@0: aoqi@0: // Copy the current entry's cards into "cards". aoqi@0: inline void copy_cards(CardIdx_t* cards) const; aoqi@0: // Copy the current entry's cards into the "_card" array of "e." aoqi@0: inline void copy_cards(SparsePRTEntry* e) const; aoqi@0: aoqi@0: inline CardIdx_t card(int i) const { return _cards[i]; } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class RSHashTable : public CHeapObj { aoqi@0: aoqi@0: friend class RSHashTableIter; aoqi@0: aoqi@0: enum SomePrivateConstants { aoqi@0: NullEntry = -1 aoqi@0: }; aoqi@0: aoqi@0: size_t _capacity; aoqi@0: size_t _capacity_mask; aoqi@0: size_t _occupied_entries; aoqi@0: size_t _occupied_cards; aoqi@0: aoqi@0: SparsePRTEntry* _entries; aoqi@0: int* _buckets; aoqi@0: int _free_region; aoqi@0: int _free_list; aoqi@0: aoqi@0: // Requires that the caller hold a lock preventing parallel modifying aoqi@0: // operations, and that the the table be less than completely full. If aoqi@0: // an entry for "region_ind" is already in the table, finds it and aoqi@0: // returns its address; otherwise returns "NULL." aoqi@0: SparsePRTEntry* entry_for_region_ind(RegionIdx_t region_ind) const; aoqi@0: aoqi@0: // Requires that the caller hold a lock preventing parallel modifying aoqi@0: // operations, and that the the table be less than completely full. If aoqi@0: // an entry for "region_ind" is already in the table, finds it and aoqi@0: // returns its address; otherwise allocates, initializes, inserts and aoqi@0: // returns a new entry for "region_ind". aoqi@0: SparsePRTEntry* entry_for_region_ind_create(RegionIdx_t region_ind); aoqi@0: aoqi@0: // Returns the index of the next free entry in "_entries". aoqi@0: int alloc_entry(); aoqi@0: // Declares the entry "fi" to be free. (It must have already been aoqi@0: // deleted from any bucket lists. aoqi@0: void free_entry(int fi); aoqi@0: aoqi@0: public: aoqi@0: RSHashTable(size_t capacity); aoqi@0: ~RSHashTable(); aoqi@0: aoqi@0: // Attempts to ensure that the given card_index in the given region is in aoqi@0: // the sparse table. If successful (because the card was already aoqi@0: // present, or because it was successfullly added) returns "true". aoqi@0: // Otherwise, returns "false" to indicate that the addition would aoqi@0: // overflow the entry for the region. The caller must transfer these aoqi@0: // entries to a larger-capacity representation. aoqi@0: bool add_card(RegionIdx_t region_id, CardIdx_t card_index); aoqi@0: aoqi@0: bool get_cards(RegionIdx_t region_id, CardIdx_t* cards); aoqi@0: aoqi@0: bool delete_entry(RegionIdx_t region_id); aoqi@0: aoqi@0: bool contains_card(RegionIdx_t region_id, CardIdx_t card_index) const; aoqi@0: aoqi@0: void add_entry(SparsePRTEntry* e); aoqi@0: aoqi@0: SparsePRTEntry* get_entry(RegionIdx_t region_id); aoqi@0: aoqi@0: void clear(); aoqi@0: aoqi@0: size_t capacity() const { return _capacity; } aoqi@0: size_t capacity_mask() const { return _capacity_mask; } aoqi@0: size_t occupied_entries() const { return _occupied_entries; } aoqi@0: size_t occupied_cards() const { return _occupied_cards; } aoqi@0: size_t mem_size() const; aoqi@0: aoqi@0: SparsePRTEntry* entry(int i) const { return (SparsePRTEntry*)((char*)_entries + SparsePRTEntry::size() * i); } aoqi@0: aoqi@0: void print(); aoqi@0: }; aoqi@0: aoqi@0: // ValueObj because will be embedded in HRRS iterator. aoqi@0: class RSHashTableIter VALUE_OBJ_CLASS_SPEC { aoqi@0: int _tbl_ind; // [-1, 0.._rsht->_capacity) aoqi@0: int _bl_ind; // [-1, 0.._rsht->_capacity) aoqi@0: short _card_ind; // [0..SparsePRTEntry::cards_num()) aoqi@0: RSHashTable* _rsht; aoqi@0: aoqi@0: // If the bucket list pointed to by _bl_ind contains a card, sets aoqi@0: // _bl_ind to the index of that entry, and returns the card. aoqi@0: // Otherwise, returns SparseEntry::NullEntry. aoqi@0: CardIdx_t find_first_card_in_list(); aoqi@0: aoqi@0: // Computes the proper card index for the card whose offset in the aoqi@0: // current region (as indicated by _bl_ind) is "ci". aoqi@0: // This is subject to errors when there is iteration concurrent with aoqi@0: // modification, but these errors should be benign. aoqi@0: size_t compute_card_ind(CardIdx_t ci); aoqi@0: aoqi@0: public: aoqi@0: RSHashTableIter(RSHashTable* rsht) : aoqi@0: _tbl_ind(RSHashTable::NullEntry), // So that first increment gets to 0. aoqi@0: _bl_ind(RSHashTable::NullEntry), aoqi@0: _card_ind((SparsePRTEntry::cards_num() - 1)), aoqi@0: _rsht(rsht) {} aoqi@0: aoqi@0: bool has_next(size_t& card_index); aoqi@0: }; aoqi@0: aoqi@0: // Concurrent accesss to a SparsePRT must be serialized by some external aoqi@0: // mutex. aoqi@0: aoqi@0: class SparsePRTIter; aoqi@0: class SparsePRTCleanupTask; aoqi@0: aoqi@0: class SparsePRT VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class SparsePRTCleanupTask; aoqi@0: aoqi@0: // Iterations are done on the _cur hash table, since they only need to aoqi@0: // see entries visible at the start of a collection pause. aoqi@0: // All other operations are done using the _next hash table. aoqi@0: RSHashTable* _cur; aoqi@0: RSHashTable* _next; aoqi@0: aoqi@0: HeapRegion* _hr; aoqi@0: aoqi@0: enum SomeAdditionalPrivateConstants { aoqi@0: InitialCapacity = 16 aoqi@0: }; aoqi@0: aoqi@0: void expand(); aoqi@0: aoqi@0: bool _expanded; aoqi@0: aoqi@0: bool expanded() { return _expanded; } aoqi@0: void set_expanded(bool b) { _expanded = b; } aoqi@0: aoqi@0: SparsePRT* _next_expanded; aoqi@0: aoqi@0: SparsePRT* next_expanded() { return _next_expanded; } aoqi@0: void set_next_expanded(SparsePRT* nxt) { _next_expanded = nxt; } aoqi@0: aoqi@0: bool should_be_on_expanded_list(); aoqi@0: aoqi@0: static SparsePRT* _head_expanded_list; aoqi@0: aoqi@0: public: aoqi@0: SparsePRT(HeapRegion* hr); aoqi@0: aoqi@0: ~SparsePRT(); aoqi@0: aoqi@0: size_t occupied() const { return _next->occupied_cards(); } aoqi@0: size_t mem_size() const; aoqi@0: aoqi@0: // Attempts to ensure that the given card_index in the given region is in aoqi@0: // the sparse table. If successful (because the card was already aoqi@0: // present, or because it was successfullly added) returns "true". aoqi@0: // Otherwise, returns "false" to indicate that the addition would aoqi@0: // overflow the entry for the region. The caller must transfer these aoqi@0: // entries to a larger-capacity representation. aoqi@0: bool add_card(RegionIdx_t region_id, CardIdx_t card_index); aoqi@0: aoqi@0: // If the table hold an entry for "region_ind", Copies its aoqi@0: // cards into "cards", which must be an array of length at least aoqi@0: // "SparePRTEntry::cards_num()", and returns "true"; otherwise, aoqi@0: // returns "false". aoqi@0: bool get_cards(RegionIdx_t region_ind, CardIdx_t* cards); aoqi@0: aoqi@0: // Return the pointer to the entry associated with the given region. aoqi@0: SparsePRTEntry* get_entry(RegionIdx_t region_ind); aoqi@0: aoqi@0: // If there is an entry for "region_ind", removes it and return "true"; aoqi@0: // otherwise returns "false." aoqi@0: bool delete_entry(RegionIdx_t region_ind); aoqi@0: aoqi@0: // Clear the table, and reinitialize to initial capacity. aoqi@0: void clear(); aoqi@0: aoqi@0: // Ensure that "_cur" and "_next" point to the same table. aoqi@0: void cleanup(); aoqi@0: aoqi@0: // Clean up all tables on the expanded list. Called single threaded. aoqi@0: static void cleanup_all(); aoqi@0: RSHashTable* cur() const { return _cur; } aoqi@0: aoqi@0: static void add_to_expanded_list(SparsePRT* sprt); aoqi@0: static SparsePRT* get_from_expanded_list(); aoqi@0: aoqi@0: // The purpose of these three methods is to help the GC workers aoqi@0: // during the cleanup pause to recreate the expanded list, purging aoqi@0: // any tables from it that belong to regions that are freed during aoqi@0: // cleanup (if we don't purge those tables, there is a race that aoqi@0: // causes various crashes; see CR 7014261). aoqi@0: // aoqi@0: // We chose to recreate the expanded list, instead of purging aoqi@0: // entries from it by iterating over it, to avoid this serial phase aoqi@0: // at the end of the cleanup pause. aoqi@0: // aoqi@0: // The three methods below work as follows: aoqi@0: // * reset_for_cleanup_tasks() : Nulls the expanded list head at the aoqi@0: // start of the cleanup pause. aoqi@0: // * do_cleanup_work() : Called by the cleanup workers for every aoqi@0: // region that is not free / is being freed by the cleanup aoqi@0: // pause. It creates a list of expanded tables whose head / tail aoqi@0: // are on the thread-local SparsePRTCleanupTask object. aoqi@0: // * finish_cleanup_task() : Called by the cleanup workers after aoqi@0: // they complete their cleanup task. It adds the local list into aoqi@0: // the global expanded list. It assumes that the aoqi@0: // ParGCRareEvent_lock is being held to ensure MT-safety. aoqi@0: static void reset_for_cleanup_tasks(); aoqi@0: void do_cleanup_work(SparsePRTCleanupTask* sprt_cleanup_task); aoqi@0: static void finish_cleanup_task(SparsePRTCleanupTask* sprt_cleanup_task); aoqi@0: aoqi@0: bool contains_card(RegionIdx_t region_id, CardIdx_t card_index) const { aoqi@0: return _next->contains_card(region_id, card_index); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class SparsePRTIter: public RSHashTableIter { aoqi@0: public: aoqi@0: SparsePRTIter(const SparsePRT* sprt) : aoqi@0: RSHashTableIter(sprt->cur()) {} aoqi@0: aoqi@0: bool has_next(size_t& card_index) { aoqi@0: return RSHashTableIter::has_next(card_index); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // This allows each worker during a cleanup pause to create a aoqi@0: // thread-local list of sparse tables that have been expanded and need aoqi@0: // to be processed at the beginning of the next GC pause. This lists aoqi@0: // are concatenated into the single expanded list at the end of the aoqi@0: // cleanup pause. aoqi@0: class SparsePRTCleanupTask VALUE_OBJ_CLASS_SPEC { aoqi@0: private: aoqi@0: SparsePRT* _head; aoqi@0: SparsePRT* _tail; aoqi@0: aoqi@0: public: aoqi@0: SparsePRTCleanupTask() : _head(NULL), _tail(NULL) { } aoqi@0: aoqi@0: void add(SparsePRT* sprt); aoqi@0: SparsePRT* head() { return _head; } aoqi@0: SparsePRT* tail() { return _tail; } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_SPARSEPRT_HPP