tonyp@2472: /* jwilhelm@6422: * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. tonyp@2472: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. tonyp@2472: * tonyp@2472: * This code is free software; you can redistribute it and/or modify it tonyp@2472: * under the terms of the GNU General Public License version 2 only, as tonyp@2472: * published by the Free Software Foundation. tonyp@2472: * tonyp@2472: * This code is distributed in the hope that it will be useful, but WITHOUT tonyp@2472: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or tonyp@2472: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License tonyp@2472: * version 2 for more details (a copy is included in the LICENSE file that tonyp@2472: * accompanied this code). tonyp@2472: * tonyp@2472: * You should have received a copy of the GNU General Public License version tonyp@2472: * 2 along with this work; if not, write to the Free Software Foundation, tonyp@2472: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. tonyp@2472: * tonyp@2472: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA tonyp@2472: * or visit www.oracle.com if you need additional information or have any tonyp@2472: * questions. tonyp@2472: * tonyp@2472: */ tonyp@2472: tonyp@2472: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP tonyp@2472: #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP tonyp@2472: tonyp@2472: #include "gc_implementation/g1/heapRegion.hpp" tonyp@2472: tonyp@2472: // Large buffer for some cases where the output might be larger than normal. tonyp@2643: #define HRS_ERR_MSG_BUFSZ 512 tonyp@2643: typedef FormatBuffer hrs_err_msg; tonyp@2472: tonyp@2472: // Set verification will be forced either if someone defines tonyp@2472: // HEAP_REGION_SET_FORCE_VERIFY to be 1, or in builds in which tonyp@2472: // asserts are compiled in. tonyp@2472: #ifndef HEAP_REGION_SET_FORCE_VERIFY tonyp@2472: #define HEAP_REGION_SET_FORCE_VERIFY defined(ASSERT) tonyp@2472: #endif // HEAP_REGION_SET_FORCE_VERIFY tonyp@2472: brutisso@6385: class hrs_ext_msg; brutisso@6385: brutisso@6385: class HRSMtSafeChecker : public CHeapObj { brutisso@6385: public: brutisso@6385: virtual void check() = 0; brutisso@6385: }; brutisso@6385: brutisso@6385: class MasterFreeRegionListMtSafeChecker : public HRSMtSafeChecker { public: void check(); }; brutisso@6385: class SecondaryFreeRegionListMtSafeChecker : public HRSMtSafeChecker { public: void check(); }; brutisso@6385: class HumongousRegionSetMtSafeChecker : public HRSMtSafeChecker { public: void check(); }; brutisso@6385: class OldRegionSetMtSafeChecker : public HRSMtSafeChecker { public: void check(); }; brutisso@6385: brutisso@6385: class HeapRegionSetCount VALUE_OBJ_CLASS_SPEC { brutisso@6385: friend class VMStructs; brutisso@6385: uint _length; brutisso@6385: size_t _capacity; brutisso@6385: brutisso@6385: public: brutisso@6385: HeapRegionSetCount() : _length(0), _capacity(0) { } brutisso@6385: brutisso@6385: const uint length() const { return _length; } brutisso@6385: const size_t capacity() const { return _capacity; } brutisso@6385: brutisso@6385: void increment(uint length_to_add, size_t capacity_to_add) { brutisso@6385: _length += length_to_add; brutisso@6385: _capacity += capacity_to_add; brutisso@6385: } brutisso@6385: brutisso@6385: void decrement(const uint length_to_remove, const size_t capacity_to_remove) { brutisso@6385: _length -= length_to_remove; brutisso@6385: _capacity -= capacity_to_remove; brutisso@6385: } brutisso@6385: }; tonyp@2472: tonyp@2472: // Base class for all the classes that represent heap region sets. It tonyp@2472: // contains the basic attributes that each set needs to maintain tonyp@2472: // (e.g., length, region num, used bytes sum) plus any shared tonyp@2472: // functionality (e.g., verification). tonyp@2472: tonyp@2472: class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC { tonyp@3457: friend class VMStructs; brutisso@6385: private: brutisso@6385: bool _is_humongous; brutisso@6385: bool _is_empty; brutisso@6385: HRSMtSafeChecker* _mt_safety_checker; tonyp@2472: tonyp@2472: protected: tonyp@2472: // The number of regions added to the set. If the set contains tonyp@2472: // only humongous regions, this reflects only 'starts humongous' tonyp@2472: // regions and does not include 'continues humongous' ones. brutisso@6385: HeapRegionSetCount _count; tonyp@2472: tonyp@2472: const char* _name; tonyp@2472: brutisso@6385: bool _verify_in_progress; tonyp@3268: tonyp@2472: // verify_region() is used to ensure that the contents of a region brutisso@6385: // added to / removed from a set are consistent. brutisso@6385: void verify_region(HeapRegion* hr) PRODUCT_RETURN; tonyp@2472: tonyp@2472: // Indicates whether all regions in the set should be humongous or tonyp@2472: // not. Only used during verification. brutisso@6385: bool regions_humongous() { return _is_humongous; } tonyp@2472: tonyp@2472: // Indicates whether all regions in the set should be empty or tonyp@2472: // not. Only used during verification. brutisso@6385: bool regions_empty() { return _is_empty; } tonyp@2472: brutisso@6385: void check_mt_safety() { brutisso@6385: if (_mt_safety_checker != NULL) { brutisso@6385: _mt_safety_checker->check(); brutisso@6385: } brutisso@6385: } brutisso@6385: brutisso@6385: virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg) { } brutisso@6385: brutisso@6385: HeapRegionSetBase(const char* name, bool humongous, bool empty, HRSMtSafeChecker* mt_safety_checker); brutisso@6385: brutisso@6385: public: brutisso@6385: const char* name() { return _name; } brutisso@6385: tschatzl@7009: uint length() const { return _count.length(); } brutisso@6385: brutisso@6385: bool is_empty() { return _count.length() == 0; } brutisso@6385: brutisso@6385: size_t total_capacity_bytes() { brutisso@6385: return _count.capacity(); brutisso@6385: } brutisso@6385: brutisso@6385: // It updates the fields of the set to reflect hr being added to brutisso@6385: // the set and tags the region appropriately. brutisso@6385: inline void add(HeapRegion* hr); brutisso@6385: brutisso@6385: // It updates the fields of the set to reflect hr being removed brutisso@6385: // from the set and tags the region appropriately. brutisso@6385: inline void remove(HeapRegion* hr); tonyp@2472: tonyp@2472: // fill_in_ext_msg() writes the the values of the set's attributes tonyp@2643: // in the custom err_msg (hrs_ext_msg). fill_in_ext_msg_extra() tonyp@2472: // allows subclasses to append further information. tonyp@2643: void fill_in_ext_msg(hrs_ext_msg* msg, const char* message); tonyp@2472: tonyp@2472: virtual void verify(); tonyp@2472: void verify_start(); tonyp@2472: void verify_next_region(HeapRegion* hr); tonyp@2472: void verify_end(); tonyp@2472: tonyp@2472: #if HEAP_REGION_SET_FORCE_VERIFY tonyp@2472: void verify_optional() { tonyp@2472: verify(); tonyp@2472: } tonyp@2472: #else // HEAP_REGION_SET_FORCE_VERIFY tonyp@2472: void verify_optional() { } tonyp@2472: #endif // HEAP_REGION_SET_FORCE_VERIFY tonyp@2472: tonyp@2472: virtual void print_on(outputStream* out, bool print_contents = false); tonyp@2472: }; tonyp@2472: tonyp@2472: // Customized err_msg for heap region sets. Apart from a tonyp@2472: // assert/guarantee-specific message it also prints out the values of tonyp@2472: // the fields of the associated set. This can be very helpful in tonyp@2472: // diagnosing failures. tonyp@2643: class hrs_ext_msg : public hrs_err_msg { tonyp@2472: public: tschatzl@7050: hrs_ext_msg(HeapRegionSetBase* set, const char* message) : hrs_err_msg("%s", "") { tonyp@2472: set->fill_in_ext_msg(this, message); tonyp@2472: } tonyp@2472: }; tonyp@2472: tonyp@2643: #define hrs_assert_sets_match(_set1_, _set2_) \ tonyp@2472: do { \ tonyp@2472: assert(((_set1_)->regions_humongous() == \ tonyp@2472: (_set2_)->regions_humongous()) && \ tonyp@2472: ((_set1_)->regions_empty() == (_set2_)->regions_empty()), \ tonyp@2643: hrs_err_msg("the contents of set %s and set %s should match", \ tonyp@2472: (_set1_)->name(), (_set2_)->name())); \ tonyp@2472: } while (0) tonyp@2472: tonyp@2472: // This class represents heap region sets whose members are not tonyp@2472: // explicitly tracked. It's helpful to group regions using such sets tonyp@2472: // so that we can reason about all the region groups in the heap using tonyp@2472: // the same interface (namely, the HeapRegionSetBase API). tonyp@2472: tonyp@2472: class HeapRegionSet : public HeapRegionSetBase { brutisso@6385: public: brutisso@6385: HeapRegionSet(const char* name, bool humongous, HRSMtSafeChecker* mt_safety_checker): brutisso@6385: HeapRegionSetBase(name, humongous, false /* empty */, mt_safety_checker) { } tonyp@2472: brutisso@6385: void bulk_remove(const HeapRegionSetCount& removed) { brutisso@6385: _count.decrement(removed.length(), removed.capacity()); tonyp@2472: } tonyp@2472: }; tonyp@2472: jwilhelm@6422: // A set that links all the regions added to it in a doubly-linked tschatzl@7050: // sorted list. We should try to avoid doing operations that iterate over tonyp@2472: // such lists in performance critical paths. Typically we should tschatzl@7050: // add / remove one region at a time or concatenate two lists. tonyp@2472: brutisso@6385: class FreeRegionListIterator; tonyp@2472: brutisso@6385: class FreeRegionList : public HeapRegionSetBase { brutisso@6385: friend class FreeRegionListIterator; tonyp@2472: tonyp@2472: private: tonyp@2472: HeapRegion* _head; tonyp@2472: HeapRegion* _tail; tonyp@2472: jwilhelm@6422: // _last is used to keep track of where we added an element the last tschatzl@7050: // time. It helps to improve performance when adding several ordered items in a row. jwilhelm@6422: HeapRegion* _last; jwilhelm@6422: brutisso@6385: static uint _unrealistically_long_length; brutisso@6385: tschatzl@7050: inline HeapRegion* remove_from_head_impl(); tschatzl@7050: inline HeapRegion* remove_from_tail_impl(); tonyp@2472: tonyp@2472: protected: tonyp@2643: virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg); tonyp@2472: tonyp@2472: // See the comment for HeapRegionSetBase::clear() tonyp@2472: virtual void clear(); tonyp@2472: brutisso@6385: public: brutisso@6385: FreeRegionList(const char* name, HRSMtSafeChecker* mt_safety_checker = NULL): brutisso@6385: HeapRegionSetBase(name, false /* humongous */, true /* empty */, mt_safety_checker) { tonyp@2472: clear(); tonyp@2472: } tonyp@2472: brutisso@6385: void verify_list(); brutisso@6385: tschatzl@7050: #ifdef ASSERT tschatzl@7050: bool contains(HeapRegion* hr) const { tschatzl@7050: return hr->containing_set() == this; tschatzl@7050: } tschatzl@7050: #endif brutisso@6385: brutisso@6385: static void set_unrealistically_long_length(uint len); brutisso@6385: jwilhelm@6422: // Add hr to the list. The region should not be a member of another set. jwilhelm@6422: // Assumes that the list is ordered and will preserve that order. The order tschatzl@7091: // is determined by hrm_index. jwilhelm@6422: inline void add_ordered(HeapRegion* hr); jwilhelm@6422: jwilhelm@6422: // Removes from head or tail based on the given argument. tschatzl@7050: HeapRegion* remove_region(bool from_head); jwilhelm@6422: jwilhelm@6422: // Merge two ordered lists. The result is also ordered. The order is tschatzl@7091: // determined by hrm_index. jwilhelm@6422: void add_ordered(FreeRegionList* from_list); jwilhelm@6422: tonyp@2472: // It empties the list by removing all regions from it. tonyp@2472: void remove_all(); tonyp@2472: tschatzl@7050: // Remove all (contiguous) regions from first to first + num_regions -1 from tschatzl@7050: // this list. tschatzl@7050: // Num_regions must be > 1. tschatzl@7050: void remove_starting_at(HeapRegion* first, uint num_regions); tonyp@2472: tonyp@2472: virtual void verify(); tonyp@2472: tonyp@2472: virtual void print_on(outputStream* out, bool print_contents = false); tonyp@2472: }; tonyp@2472: tonyp@2643: // Iterator class that provides a convenient way to iterate over the tschatzl@7050: // regions of a FreeRegionList. tonyp@2472: brutisso@6385: class FreeRegionListIterator : public StackObj { tonyp@2472: private: brutisso@6385: FreeRegionList* _list; jwilhelm@6422: HeapRegion* _curr; tonyp@2472: tonyp@2472: public: tonyp@2472: bool more_available() { tonyp@2472: return _curr != NULL; tonyp@2472: } tonyp@2472: tonyp@2472: HeapRegion* get_next() { tonyp@2472: assert(more_available(), tonyp@2472: "get_next() should be called when more regions are available"); tonyp@2472: tonyp@2472: // If we are going to introduce a count in the iterator we should tonyp@2472: // do the "cycle" check. tonyp@2472: tonyp@2472: HeapRegion* hr = _curr; brutisso@6385: _list->verify_region(hr); tonyp@2472: _curr = hr->next(); tonyp@2472: return hr; tonyp@2472: } tonyp@2472: jwilhelm@6422: FreeRegionListIterator(FreeRegionList* list) : _curr(NULL), _list(list) { tschatzl@7050: _curr = list->_head; tonyp@2472: } tonyp@2472: }; tonyp@2472: tonyp@2472: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP