aoqi@0: /* aoqi@0: * Copyright (c) 2011, 2014, 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_HEAPREGIONSET_HPP aoqi@0: #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP aoqi@0: aoqi@0: #include "gc_implementation/g1/heapRegion.hpp" aoqi@0: aoqi@0: // Large buffer for some cases where the output might be larger than normal. aoqi@0: #define HRS_ERR_MSG_BUFSZ 512 aoqi@0: typedef FormatBuffer hrs_err_msg; aoqi@0: aoqi@0: // Set verification will be forced either if someone defines aoqi@0: // HEAP_REGION_SET_FORCE_VERIFY to be 1, or in builds in which aoqi@0: // asserts are compiled in. aoqi@0: #ifndef HEAP_REGION_SET_FORCE_VERIFY aoqi@0: #define HEAP_REGION_SET_FORCE_VERIFY defined(ASSERT) aoqi@0: #endif // HEAP_REGION_SET_FORCE_VERIFY aoqi@0: aoqi@0: class hrs_ext_msg; aoqi@0: aoqi@0: class HRSMtSafeChecker : public CHeapObj { aoqi@0: public: aoqi@0: virtual void check() = 0; aoqi@0: }; aoqi@0: aoqi@0: class MasterFreeRegionListMtSafeChecker : public HRSMtSafeChecker { public: void check(); }; aoqi@0: class SecondaryFreeRegionListMtSafeChecker : public HRSMtSafeChecker { public: void check(); }; aoqi@0: class HumongousRegionSetMtSafeChecker : public HRSMtSafeChecker { public: void check(); }; aoqi@0: class OldRegionSetMtSafeChecker : public HRSMtSafeChecker { public: void check(); }; aoqi@0: aoqi@0: class HeapRegionSetCount VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class VMStructs; aoqi@0: uint _length; aoqi@0: size_t _capacity; aoqi@0: aoqi@0: public: aoqi@0: HeapRegionSetCount() : _length(0), _capacity(0) { } aoqi@0: aoqi@0: const uint length() const { return _length; } aoqi@0: const size_t capacity() const { return _capacity; } aoqi@0: aoqi@0: void increment(uint length_to_add, size_t capacity_to_add) { aoqi@0: _length += length_to_add; aoqi@0: _capacity += capacity_to_add; aoqi@0: } aoqi@0: aoqi@0: void decrement(const uint length_to_remove, const size_t capacity_to_remove) { aoqi@0: _length -= length_to_remove; aoqi@0: _capacity -= capacity_to_remove; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // Base class for all the classes that represent heap region sets. It aoqi@0: // contains the basic attributes that each set needs to maintain aoqi@0: // (e.g., length, region num, used bytes sum) plus any shared aoqi@0: // functionality (e.g., verification). aoqi@0: aoqi@0: class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: bool _is_humongous; aoqi@0: bool _is_empty; aoqi@0: HRSMtSafeChecker* _mt_safety_checker; aoqi@0: aoqi@0: protected: aoqi@0: // The number of regions added to the set. If the set contains aoqi@0: // only humongous regions, this reflects only 'starts humongous' aoqi@0: // regions and does not include 'continues humongous' ones. aoqi@0: HeapRegionSetCount _count; aoqi@0: aoqi@0: const char* _name; aoqi@0: aoqi@0: bool _verify_in_progress; aoqi@0: aoqi@0: // verify_region() is used to ensure that the contents of a region aoqi@0: // added to / removed from a set are consistent. aoqi@0: void verify_region(HeapRegion* hr) PRODUCT_RETURN; aoqi@0: aoqi@0: // Indicates whether all regions in the set should be humongous or aoqi@0: // not. Only used during verification. aoqi@0: bool regions_humongous() { return _is_humongous; } aoqi@0: aoqi@0: // Indicates whether all regions in the set should be empty or aoqi@0: // not. Only used during verification. aoqi@0: bool regions_empty() { return _is_empty; } aoqi@0: aoqi@0: void check_mt_safety() { aoqi@0: if (_mt_safety_checker != NULL) { aoqi@0: _mt_safety_checker->check(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg) { } aoqi@0: aoqi@0: HeapRegionSetBase(const char* name, bool humongous, bool empty, HRSMtSafeChecker* mt_safety_checker); aoqi@0: aoqi@0: public: aoqi@0: const char* name() { return _name; } aoqi@0: aoqi@0: uint length() { return _count.length(); } aoqi@0: aoqi@0: bool is_empty() { return _count.length() == 0; } aoqi@0: aoqi@0: size_t total_capacity_bytes() { aoqi@0: return _count.capacity(); aoqi@0: } aoqi@0: aoqi@0: // It updates the fields of the set to reflect hr being added to aoqi@0: // the set and tags the region appropriately. aoqi@0: inline void add(HeapRegion* hr); aoqi@0: aoqi@0: // It updates the fields of the set to reflect hr being removed aoqi@0: // from the set and tags the region appropriately. aoqi@0: inline void remove(HeapRegion* hr); aoqi@0: aoqi@0: // fill_in_ext_msg() writes the the values of the set's attributes aoqi@0: // in the custom err_msg (hrs_ext_msg). fill_in_ext_msg_extra() aoqi@0: // allows subclasses to append further information. aoqi@0: void fill_in_ext_msg(hrs_ext_msg* msg, const char* message); aoqi@0: aoqi@0: virtual void verify(); aoqi@0: void verify_start(); aoqi@0: void verify_next_region(HeapRegion* hr); aoqi@0: void verify_end(); aoqi@0: aoqi@0: #if HEAP_REGION_SET_FORCE_VERIFY aoqi@0: void verify_optional() { aoqi@0: verify(); aoqi@0: } aoqi@0: #else // HEAP_REGION_SET_FORCE_VERIFY aoqi@0: void verify_optional() { } aoqi@0: #endif // HEAP_REGION_SET_FORCE_VERIFY aoqi@0: aoqi@0: virtual void print_on(outputStream* out, bool print_contents = false); aoqi@0: }; aoqi@0: aoqi@0: // Customized err_msg for heap region sets. Apart from a aoqi@0: // assert/guarantee-specific message it also prints out the values of aoqi@0: // the fields of the associated set. This can be very helpful in aoqi@0: // diagnosing failures. aoqi@0: class hrs_ext_msg : public hrs_err_msg { aoqi@0: public: aoqi@0: hrs_ext_msg(HeapRegionSetBase* set, const char* message) : hrs_err_msg("%s","") { aoqi@0: set->fill_in_ext_msg(this, message); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #define hrs_assert_sets_match(_set1_, _set2_) \ aoqi@0: do { \ aoqi@0: assert(((_set1_)->regions_humongous() == \ aoqi@0: (_set2_)->regions_humongous()) && \ aoqi@0: ((_set1_)->regions_empty() == (_set2_)->regions_empty()), \ aoqi@0: hrs_err_msg("the contents of set %s and set %s should match", \ aoqi@0: (_set1_)->name(), (_set2_)->name())); \ aoqi@0: } while (0) aoqi@0: aoqi@0: // This class represents heap region sets whose members are not aoqi@0: // explicitly tracked. It's helpful to group regions using such sets aoqi@0: // so that we can reason about all the region groups in the heap using aoqi@0: // the same interface (namely, the HeapRegionSetBase API). aoqi@0: aoqi@0: class HeapRegionSet : public HeapRegionSetBase { aoqi@0: public: aoqi@0: HeapRegionSet(const char* name, bool humongous, HRSMtSafeChecker* mt_safety_checker): aoqi@0: HeapRegionSetBase(name, humongous, false /* empty */, mt_safety_checker) { } aoqi@0: aoqi@0: void bulk_remove(const HeapRegionSetCount& removed) { aoqi@0: _count.decrement(removed.length(), removed.capacity()); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // A set that links all the regions added to it in a doubly-linked aoqi@0: // list. We should try to avoid doing operations that iterate over aoqi@0: // such lists in performance critical paths. Typically we should aoqi@0: // add / remove one region at a time or concatenate two lists. There are aoqi@0: // two ways to treat your lists, ordered and un-ordered. All un-ordered aoqi@0: // operations are done in constant time. To keep a list ordered only use aoqi@0: // add_ordered() to add elements to the list. If a list is not ordered aoqi@0: // from start, there is no way to sort it later. aoqi@0: aoqi@0: class FreeRegionListIterator; aoqi@0: aoqi@0: class FreeRegionList : public HeapRegionSetBase { aoqi@0: friend class FreeRegionListIterator; aoqi@0: aoqi@0: private: aoqi@0: HeapRegion* _head; aoqi@0: HeapRegion* _tail; aoqi@0: aoqi@0: // _last is used to keep track of where we added an element the last aoqi@0: // time in ordered lists. It helps to improve performance when adding aoqi@0: // several ordered items in a row. aoqi@0: HeapRegion* _last; aoqi@0: aoqi@0: static uint _unrealistically_long_length; aoqi@0: aoqi@0: void add_as_head_or_tail(FreeRegionList* from_list, bool as_head); aoqi@0: aoqi@0: protected: aoqi@0: virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg); aoqi@0: aoqi@0: // See the comment for HeapRegionSetBase::clear() aoqi@0: virtual void clear(); aoqi@0: aoqi@0: public: aoqi@0: FreeRegionList(const char* name, HRSMtSafeChecker* mt_safety_checker = NULL): aoqi@0: HeapRegionSetBase(name, false /* humongous */, true /* empty */, mt_safety_checker) { aoqi@0: clear(); aoqi@0: } aoqi@0: aoqi@0: void verify_list(); aoqi@0: aoqi@0: HeapRegion* head() { return _head; } aoqi@0: HeapRegion* tail() { return _tail; } aoqi@0: aoqi@0: static void set_unrealistically_long_length(uint len); aoqi@0: aoqi@0: // Add hr to the list. The region should not be a member of another set. aoqi@0: // Assumes that the list is ordered and will preserve that order. The order aoqi@0: // is determined by hrs_index. aoqi@0: inline void add_ordered(HeapRegion* hr); aoqi@0: aoqi@0: // It adds hr to the list as the new head. The region should not be aoqi@0: // a member of another set. aoqi@0: inline void add_as_head(HeapRegion* hr); aoqi@0: aoqi@0: // It adds hr to the list as the new tail. The region should not be aoqi@0: // a member of another set. aoqi@0: inline void add_as_tail(HeapRegion* hr); aoqi@0: aoqi@0: // It removes and returns the head of the list. It assumes that the aoqi@0: // list is not empty so it will return a non-NULL value. aoqi@0: inline HeapRegion* remove_head(); aoqi@0: aoqi@0: // Convenience method. aoqi@0: inline HeapRegion* remove_head_or_null(); aoqi@0: aoqi@0: // Removes and returns the last element (_tail) of the list. It assumes aoqi@0: // that the list isn't empty so that it can return a non-NULL value. aoqi@0: inline HeapRegion* remove_tail(); aoqi@0: aoqi@0: // Convenience method aoqi@0: inline HeapRegion* remove_tail_or_null(); aoqi@0: aoqi@0: // Removes from head or tail based on the given argument. aoqi@0: inline HeapRegion* remove_region(bool from_head); aoqi@0: aoqi@0: // Merge two ordered lists. The result is also ordered. The order is aoqi@0: // determined by hrs_index. aoqi@0: void add_ordered(FreeRegionList* from_list); aoqi@0: aoqi@0: // It moves the regions from from_list to this list and empties aoqi@0: // from_list. The new regions will appear in the same order as they aoqi@0: // were in from_list and be linked in the beginning of this list. aoqi@0: void add_as_head(FreeRegionList* from_list); aoqi@0: aoqi@0: // It moves the regions from from_list to this list and empties aoqi@0: // from_list. The new regions will appear in the same order as they aoqi@0: // were in from_list and be linked in the end of this list. aoqi@0: void add_as_tail(FreeRegionList* from_list); aoqi@0: aoqi@0: // It empties the list by removing all regions from it. aoqi@0: void remove_all(); aoqi@0: aoqi@0: // It removes all regions in the list that are pending for removal aoqi@0: // (i.e., they have been tagged with "pending_removal"). The list aoqi@0: // must not be empty, target_count should reflect the exact number aoqi@0: // of regions that are pending for removal in the list, and aoqi@0: // target_count should be > 1 (currently, we never need to remove a aoqi@0: // single region using this). aoqi@0: void remove_all_pending(uint target_count); aoqi@0: aoqi@0: virtual void verify(); aoqi@0: aoqi@0: virtual void print_on(outputStream* out, bool print_contents = false); aoqi@0: }; aoqi@0: aoqi@0: // Iterator class that provides a convenient way to iterate over the aoqi@0: // regions of a HeapRegionLinkedList instance. aoqi@0: aoqi@0: class FreeRegionListIterator : public StackObj { aoqi@0: private: aoqi@0: FreeRegionList* _list; aoqi@0: HeapRegion* _curr; aoqi@0: aoqi@0: public: aoqi@0: bool more_available() { aoqi@0: return _curr != NULL; aoqi@0: } aoqi@0: aoqi@0: HeapRegion* get_next() { aoqi@0: assert(more_available(), aoqi@0: "get_next() should be called when more regions are available"); aoqi@0: aoqi@0: // If we are going to introduce a count in the iterator we should aoqi@0: // do the "cycle" check. aoqi@0: aoqi@0: HeapRegion* hr = _curr; aoqi@0: _list->verify_region(hr); aoqi@0: _curr = hr->next(); aoqi@0: return hr; aoqi@0: } aoqi@0: aoqi@0: FreeRegionListIterator(FreeRegionList* list) : _curr(NULL), _list(list) { aoqi@0: _curr = list->head(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP