1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP
  27 
  28 #include "gc_implementation/g1/heapRegion.hpp"
  29 
  30 // Large buffer for some cases where the output might be larger than normal.
  31 #define HRS_ERR_MSG_BUFSZ 512
  32 typedef FormatBuffer<HRS_ERR_MSG_BUFSZ> hrs_err_msg;
  33 
  34 // Set verification will be forced either if someone defines
  35 // HEAP_REGION_SET_FORCE_VERIFY to be 1, or in builds in which
  36 // asserts are compiled in.
  37 #ifndef HEAP_REGION_SET_FORCE_VERIFY
  38 #define HEAP_REGION_SET_FORCE_VERIFY defined(ASSERT)
  39 #endif // HEAP_REGION_SET_FORCE_VERIFY
  40 
  41 class hrs_ext_msg;
  42 
  43 class HRSMtSafeChecker : public CHeapObj<mtGC> {
  44 public:
  45   virtual bool check() = 0;
  46 };
  47 
  48 class MasterFreeRegionListMtSafeChecker    : public HRSMtSafeChecker { public: bool check(); };
  49 class SecondaryFreeRegionListMtSafeChecker : public HRSMtSafeChecker { public: bool check(); };
  50 class HumongousRegionSetMtSafeChecker      : public HRSMtSafeChecker { public: bool check(); };
  51 class OldRegionSetMtSafeChecker            : public HRSMtSafeChecker { public: bool check(); };
  52 
  53 class HeapRegionSetCount VALUE_OBJ_CLASS_SPEC {
  54   friend class VMStructs;
  55   uint   _length;
  56   size_t _capacity;
  57 
  58 public:
  59   HeapRegionSetCount() : _length(0), _capacity(0) { }
  60 
  61   const uint   length()   const { return _length;   }
  62   const size_t capacity() const { return _capacity; }
  63 
  64   void increment(uint length_to_add, size_t capacity_to_add) {
  65     _length += length_to_add;
  66     _capacity += capacity_to_add;
  67   }
  68 
  69   void decrement(const uint length_to_remove, const size_t capacity_to_remove) {
  70     _length -= length_to_remove;
  71     _capacity -= capacity_to_remove;
  72   }
  73 };
  74 
  75 //////////////////// HeapRegionSetBase ////////////////////
  76 
  77 // Base class for all the classes that represent heap region sets. It
  78 // contains the basic attributes that each set needs to maintain
  79 // (e.g., length, region num, used bytes sum) plus any shared
  80 // functionality (e.g., verification).
  81 
  82 class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC {
  83   friend class VMStructs;
  84 private:
  85   bool _is_humongous;
  86   bool _is_empty;
  87   bool _is_linked;
  88   HRSMtSafeChecker* _mt_safety_checker;
  89 
  90 protected:
  91   // The number of regions added to the set. If the set contains
  92   // only humongous regions, this reflects only 'starts humongous'
  93   // regions and does not include 'continues humongous' ones.
  94   HeapRegionSetCount _count;
  95 
  96   const char* _name;
  97 
  98   bool _verify_in_progress;
  99 
 100   // verify_region() is used to ensure that the contents of a region
 101   // added to / removed from a set are consistent.
 102   void verify_region(HeapRegion* hr) PRODUCT_RETURN;
 103 
 104   // Indicates whether all regions in the set should be humongous or
 105   // not. Only used during verification.
 106   bool regions_humongous() { return _is_humongous; }
 107 
 108   // Indicates whether all regions in the set should be empty or
 109   // not. Only used during verification.
 110   bool regions_empty() { return _is_empty; }
 111 
 112   bool regions_linked() { return _is_linked; }
 113 
 114   bool check_mt_safety() {
 115     if (_mt_safety_checker != NULL) {
 116       _mt_safety_checker->check();
 117     }
 118     return true;
 119   }
 120 
 121   virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg) { }
 122 
 123   HeapRegionSetBase(const char* name, bool humongous, bool empty, bool linked, HRSMtSafeChecker* mt_safety_checker);
 124 
 125 public:
 126   const char* name() { return _name; }
 127 
 128   uint length() { return _count.length(); }
 129 
 130   bool is_empty() { return _count.length() == 0; }
 131 
 132   size_t total_capacity_bytes() {
 133     return _count.capacity();
 134   }
 135 
 136   // It updates the fields of the set to reflect hr being added to
 137   // the set and tags the region appropriately.
 138   inline void add(HeapRegion* hr);
 139 
 140   // It updates the fields of the set to reflect hr being removed
 141   // from the set and tags the region appropriately.
 142   inline void remove(HeapRegion* hr);
 143 
 144   // fill_in_ext_msg() writes the the values of the set's attributes
 145   // in the custom err_msg (hrs_ext_msg). fill_in_ext_msg_extra()
 146   // allows subclasses to append further information.
 147   void fill_in_ext_msg(hrs_ext_msg* msg, const char* message);
 148 
 149   virtual void verify();
 150   void verify_start();
 151   void verify_next_region(HeapRegion* hr);
 152   void verify_end();
 153 
 154 #if HEAP_REGION_SET_FORCE_VERIFY
 155   void verify_optional() {
 156     verify();
 157   }
 158 #else // HEAP_REGION_SET_FORCE_VERIFY
 159   void verify_optional() { }
 160 #endif // HEAP_REGION_SET_FORCE_VERIFY
 161 
 162   virtual void print_on(outputStream* out, bool print_contents = false);
 163 };
 164 
 165 // Customized err_msg for heap region sets. Apart from a
 166 // assert/guarantee-specific message it also prints out the values of
 167 // the fields of the associated set. This can be very helpful in
 168 // diagnosing failures.
 169 class hrs_ext_msg : public hrs_err_msg {
 170 public:
 171   hrs_ext_msg(HeapRegionSetBase* set, const char* message) : hrs_err_msg("") {
 172     set->fill_in_ext_msg(this, message);
 173   }
 174 };
 175 
 176 //////////////////// HeapRegionSet ////////////////////
 177 
 178 #define hrs_assert_sets_match(_set1_, _set2_)                                 \
 179   do {                                                                        \
 180     assert(((_set1_)->regions_humongous() ==                                  \
 181                                             (_set2_)->regions_humongous()) && \
 182            ((_set1_)->regions_empty() == (_set2_)->regions_empty()),          \
 183            hrs_err_msg("the contents of set %s and set %s should match",      \
 184                        (_set1_)->name(), (_set2_)->name()));                  \
 185   } while (0)
 186 
 187 // This class represents heap region sets whose members are not
 188 // explicitly tracked. It's helpful to group regions using such sets
 189 // so that we can reason about all the region groups in the heap using
 190 // the same interface (namely, the HeapRegionSetBase API).
 191 
 192 class HeapRegionSet : public HeapRegionSetBase {
 193 public:
 194   HeapRegionSet(const char* name, bool humongous, HRSMtSafeChecker* mt_safety_checker):
 195     HeapRegionSetBase(name, humongous, false /* empty */, false /* linked */, mt_safety_checker) { }
 196 
 197   void bulk_remove(const HeapRegionSetCount& removed) {
 198     _count.decrement(removed.length(), removed.capacity());
 199   }
 200 };
 201 
 202 //////////////////// HeapRegionLinkedList ////////////////////
 203 
 204 // A set that links all the regions added to it in a singly-linked
 205 // list. We should try to avoid doing operations that iterate over
 206 // such lists in performance critical paths. Typically we should
 207 // add / remove one region at a time or concatenate two lists. All
 208 // those operations are done in constant time.
 209 
 210 class FreeRegionListIterator;
 211 
 212 class FreeRegionList : public HeapRegionSetBase {
 213   friend class FreeRegionListIterator;
 214 
 215 private:
 216   HeapRegion* _head;
 217   HeapRegion* _tail;
 218 
 219 protected:
 220   virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg);
 221 
 222   // See the comment for HeapRegionSetBase::clear()
 223   virtual void clear();
 224 
 225 public:
 226   FreeRegionList(const char* name, HRSMtSafeChecker* mt_safety_checker):
 227     HeapRegionSetBase(name, false /* humongous */, true /* empty */, true /* linked */, mt_safety_checker) {
 228     clear();
 229   }
 230 
 231   void verify_list();
 232 
 233   HeapRegion* head() { return _head; }
 234   HeapRegion* tail() { return _tail; }
 235 
 236   static uint _unrealistically_long_length;
 237   static void set_unrealistically_long_length(uint len);
 238 
 239   // It adds hr to the list as the new head. The region should not be
 240   // a member of another set.
 241   inline void add_as_head(HeapRegion* hr);
 242 
 243   // It adds hr to the list as the new tail. The region should not be
 244   // a member of another set.
 245   inline void add_as_tail(HeapRegion* hr);
 246 
 247   // It removes and returns the head of the list. It assumes that the
 248   // list is not empty so it will return a non-NULL value.
 249   inline HeapRegion* remove_head();
 250 
 251   // Convenience method.
 252   inline HeapRegion* remove_head_or_null();
 253 
 254   // It moves the regions from from_list to this list and empties
 255   // from_list. The new regions will appear in the same order as they
 256   // were in from_list and be linked in the beginning of this list.
 257   void add_as_head(FreeRegionList* from_list);
 258 
 259   // It moves the regions from from_list to this list and empties
 260   // from_list. The new regions will appear in the same order as they
 261   // were in from_list and be linked in the end of this list.
 262   void add_as_tail(FreeRegionList* from_list);
 263 
 264   // It empties the list by removing all regions from it.
 265   void remove_all();
 266 
 267   // It removes all regions in the list that are pending for removal
 268   // (i.e., they have been tagged with "pending_removal"). The list
 269   // must not be empty, target_count should reflect the exact number
 270   // of regions that are pending for removal in the list, and
 271   // target_count should be > 1 (currently, we never need to remove a
 272   // single region using this).
 273   void remove_all_pending(uint target_count);
 274 
 275   virtual void verify();
 276 
 277   virtual void print_on(outputStream* out, bool print_contents = false);
 278 };
 279 
 280 //////////////////// FreeRegionListIterator ////////////////////
 281 
 282 // Iterator class that provides a convenient way to iterate over the
 283 // regions of a HeapRegionLinkedList instance.
 284 
 285 class FreeRegionListIterator : public StackObj {
 286 private:
 287   FreeRegionList* _list;
 288   HeapRegion*           _curr;
 289 
 290 public:
 291   bool more_available() {
 292     return _curr != NULL;
 293   }
 294 
 295   HeapRegion* get_next() {
 296     assert(more_available(),
 297            "get_next() should be called when more regions are available");
 298 
 299     // If we are going to introduce a count in the iterator we should
 300     // do the "cycle" check.
 301 
 302     HeapRegion* hr = _curr;
 303     _list->verify_region(hr);
 304     _curr = hr->next();
 305     return hr;
 306   }
 307 
 308   FreeRegionListIterator(FreeRegionList* list)
 309     : _curr(NULL), _list(list) {
 310     _curr = list->head();
 311   }
 312 };
 313 
 314 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP