hotspot/src/share/vm/gc_implementation/g1/heapRegionSet.hpp
changeset 7923 fc200fcd4e05
child 8680 f1c414e16a4c
equal deleted inserted replaced
7922:e97540c35e38 7923:fc200fcd4e05
       
     1 /*
       
     2  * copyright (c) 2011, 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 HRL_ERR_MSG_BUFSZ 512
       
    32 typedef FormatBuffer<HRL_ERR_MSG_BUFSZ> hrl_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 //////////////////// HeapRegionSetBase ////////////////////
       
    42 
       
    43 // Base class for all the classes that represent heap region sets. It
       
    44 // contains the basic attributes that each set needs to maintain
       
    45 // (e.g., length, region num, used bytes sum) plus any shared
       
    46 // functionality (e.g., verification).
       
    47 
       
    48 class hrl_ext_msg;
       
    49 
       
    50 class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC {
       
    51   friend class hrl_ext_msg;
       
    52 
       
    53 protected:
       
    54   static size_t calculate_region_num(HeapRegion* hr);
       
    55 
       
    56   static size_t _unrealistically_long_length;
       
    57 
       
    58   // The number of regions added to the set. If the set contains
       
    59   // only humongous regions, this reflects only 'starts humongous'
       
    60   // regions and does not include 'continues humongous' ones.
       
    61   size_t _length;
       
    62 
       
    63   // The total number of regions represented by the set. If the set
       
    64   // does not contain humongous regions, this should be the same as
       
    65   // _length. If the set contains only humongous regions, this will
       
    66   // include the 'continues humongous' regions.
       
    67   size_t _region_num;
       
    68 
       
    69   // We don't keep track of the total capacity explicitly, we instead
       
    70   // recalculate it based on _region_num and the heap region size.
       
    71 
       
    72   // The sum of used bytes in the all the regions in the set.
       
    73   size_t _total_used_bytes;
       
    74 
       
    75   const char* _name;
       
    76 
       
    77   bool        _verify_in_progress;
       
    78   size_t      _calc_length;
       
    79   size_t      _calc_region_num;
       
    80   size_t      _calc_total_capacity_bytes;
       
    81   size_t      _calc_total_used_bytes;
       
    82 
       
    83   // verify_region() is used to ensure that the contents of a region
       
    84   // added to / removed from a set are consistent. Different sets
       
    85   // make different assumptions about the regions added to them. So
       
    86   // each set can override verify_region_extra(), which is called
       
    87   // from verify_region(), and do any extra verification it needs to
       
    88   // perform in that.
       
    89   virtual const char* verify_region_extra(HeapRegion* hr) { return NULL; }
       
    90   bool verify_region(HeapRegion* hr,
       
    91                      HeapRegionSetBase* expected_containing_set);
       
    92 
       
    93   // Indicates whether all regions in the set should be humongous or
       
    94   // not. Only used during verification.
       
    95   virtual bool regions_humongous() = 0;
       
    96 
       
    97   // Indicates whether all regions in the set should be empty or
       
    98   // not. Only used during verification.
       
    99   virtual bool regions_empty() = 0;
       
   100 
       
   101   // Subclasses can optionally override this to do MT safety protocol
       
   102   // checks. It is called in an assert from all methods that perform
       
   103   // updates on the set (and subclasses should also call it too).
       
   104   virtual bool check_mt_safety() { return true; }
       
   105 
       
   106   // fill_in_ext_msg() writes the the values of the set's attributes
       
   107   // in the custom err_msg (hrl_ext_msg). fill_in_ext_msg_extra()
       
   108   // allows subclasses to append further information.
       
   109   virtual void fill_in_ext_msg_extra(hrl_ext_msg* msg) { }
       
   110   void fill_in_ext_msg(hrl_ext_msg* msg, const char* message);
       
   111 
       
   112   // It updates the fields of the set to reflect hr being added to
       
   113   // the set.
       
   114   inline void update_for_addition(HeapRegion* hr);
       
   115 
       
   116   // It updates the fields of the set to reflect hr being added to
       
   117   // the set and tags the region appropriately.
       
   118   inline void add_internal(HeapRegion* hr);
       
   119 
       
   120   // It updates the fields of the set to reflect hr being removed
       
   121   // from the set.
       
   122   inline void update_for_removal(HeapRegion* hr);
       
   123 
       
   124   // It updates the fields of the set to reflect hr being removed
       
   125   // from the set and tags the region appropriately.
       
   126   inline void remove_internal(HeapRegion* hr);
       
   127 
       
   128   // It clears all the fields of the sets. Note: it will not iterate
       
   129   // over the set and remove regions from it. It assumes that the
       
   130   // caller has already done so. It will literally just clear the fields.
       
   131   virtual void clear();
       
   132 
       
   133   HeapRegionSetBase(const char* name);
       
   134 
       
   135 public:
       
   136   static void set_unrealistically_long_length(size_t len);
       
   137 
       
   138   const char* name() { return _name; }
       
   139 
       
   140   size_t length() { return _length; }
       
   141 
       
   142   bool is_empty() { return _length == 0; }
       
   143 
       
   144   size_t region_num() { return _region_num; }
       
   145 
       
   146   size_t total_capacity_bytes() {
       
   147     return region_num() << HeapRegion::LogOfHRGrainBytes;
       
   148   }
       
   149 
       
   150   size_t total_used_bytes() { return _total_used_bytes; }
       
   151 
       
   152   virtual void verify();
       
   153   void verify_start();
       
   154   void verify_next_region(HeapRegion* hr);
       
   155   void verify_end();
       
   156 
       
   157 #if HEAP_REGION_SET_FORCE_VERIFY
       
   158   void verify_optional() {
       
   159     verify();
       
   160   }
       
   161 #else // HEAP_REGION_SET_FORCE_VERIFY
       
   162   void verify_optional() { }
       
   163 #endif // HEAP_REGION_SET_FORCE_VERIFY
       
   164 
       
   165   virtual void print_on(outputStream* out, bool print_contents = false);
       
   166 };
       
   167 
       
   168 // Customized err_msg for heap region sets. Apart from a
       
   169 // assert/guarantee-specific message it also prints out the values of
       
   170 // the fields of the associated set. This can be very helpful in
       
   171 // diagnosing failures.
       
   172 
       
   173 class hrl_ext_msg : public hrl_err_msg {
       
   174 public:
       
   175   hrl_ext_msg(HeapRegionSetBase* set, const char* message) : hrl_err_msg("") {
       
   176     set->fill_in_ext_msg(this, message);
       
   177   }
       
   178 };
       
   179 
       
   180 // These two macros are provided for convenience, to keep the uses of
       
   181 // these two asserts a bit more concise.
       
   182 
       
   183 #define hrl_assert_mt_safety_ok(_set_)                                        \
       
   184   do {                                                                        \
       
   185     assert((_set_)->check_mt_safety(), hrl_ext_msg((_set_), "MT safety"));    \
       
   186   } while (0)
       
   187 
       
   188 #define hrl_assert_region_ok(_set_, _hr_, _expected_)                         \
       
   189   do {                                                                        \
       
   190     assert((_set_)->verify_region((_hr_), (_expected_)),                      \
       
   191            hrl_ext_msg((_set_), "region verification"));                      \
       
   192   } while (0)
       
   193 
       
   194 //////////////////// HeapRegionSet ////////////////////
       
   195 
       
   196 #define hrl_assert_sets_match(_set1_, _set2_)                                 \
       
   197   do {                                                                        \
       
   198     assert(((_set1_)->regions_humongous() ==                                  \
       
   199                                             (_set2_)->regions_humongous()) && \
       
   200            ((_set1_)->regions_empty() == (_set2_)->regions_empty()),          \
       
   201            hrl_err_msg("the contents of set %s and set %s should match",      \
       
   202                        (_set1_)->name(), (_set2_)->name()));                  \
       
   203   } while (0)
       
   204 
       
   205 // This class represents heap region sets whose members are not
       
   206 // explicitly tracked. It's helpful to group regions using such sets
       
   207 // so that we can reason about all the region groups in the heap using
       
   208 // the same interface (namely, the HeapRegionSetBase API).
       
   209 
       
   210 class HeapRegionSet : public HeapRegionSetBase {
       
   211 protected:
       
   212   virtual const char* verify_region_extra(HeapRegion* hr) {
       
   213     if (hr->next() != NULL) {
       
   214       return "next() should always be NULL as we do not link the regions";
       
   215     }
       
   216 
       
   217     return HeapRegionSetBase::verify_region_extra(hr);
       
   218   }
       
   219 
       
   220   HeapRegionSet(const char* name) : HeapRegionSetBase(name) {
       
   221     clear();
       
   222   }
       
   223 
       
   224 public:
       
   225   // It adds hr to the set. The region should not be a member of
       
   226   // another set.
       
   227   inline void add(HeapRegion* hr);
       
   228 
       
   229   // It removes hr from the set. The region should be a member of
       
   230   // this set.
       
   231   inline void remove(HeapRegion* hr);
       
   232 
       
   233   // It removes a region from the set. Instead of updating the fields
       
   234   // of the set to reflect this removal, it accumulates the updates
       
   235   // in proxy_set. The idea is that proxy_set is thread-local to
       
   236   // avoid multiple threads updating the fields of the set
       
   237   // concurrently and having to synchronize. The method
       
   238   // update_from_proxy() will update the fields of the set from the
       
   239   // proxy_set.
       
   240   inline void remove_with_proxy(HeapRegion* hr, HeapRegionSet* proxy_set);
       
   241 
       
   242   // After multiple calls to remove_with_proxy() the updates to the
       
   243   // fields of the set are accumulated in proxy_set. This call
       
   244   // updates the fields of the set from proxy_set.
       
   245   void update_from_proxy(HeapRegionSet* proxy_set);
       
   246 };
       
   247 
       
   248 //////////////////// HeapRegionLinkedList ////////////////////
       
   249 
       
   250 // A set that links all the regions added to it in a singly-linked
       
   251 // list. We should try to avoid doing operations that iterate over
       
   252 // such lists in performance critical paths. Typically we should
       
   253 // add / remove one region at a time or concatenate two lists. All
       
   254 // those operations are done in constant time.
       
   255 
       
   256 class HeapRegionLinkedListIterator;
       
   257 
       
   258 class HeapRegionLinkedList : public HeapRegionSetBase {
       
   259   friend class HeapRegionLinkedListIterator;
       
   260 
       
   261 private:
       
   262   HeapRegion* _head;
       
   263   HeapRegion* _tail;
       
   264 
       
   265   // These are provided for use by the friend classes.
       
   266   HeapRegion* head() { return _head; }
       
   267   HeapRegion* tail() { return _tail; }
       
   268 
       
   269 protected:
       
   270   virtual void fill_in_ext_msg_extra(hrl_ext_msg* msg);
       
   271 
       
   272   // See the comment for HeapRegionSetBase::clear()
       
   273   virtual void clear();
       
   274 
       
   275   HeapRegionLinkedList(const char* name) : HeapRegionSetBase(name) {
       
   276     clear();
       
   277   }
       
   278 
       
   279 public:
       
   280   // It adds hr to the list as the new tail. The region should not be
       
   281   // a member of another set.
       
   282   inline void add_as_tail(HeapRegion* hr);
       
   283 
       
   284   // It removes and returns the head of the list. It assumes that the
       
   285   // list is not empty so it will return a non-NULL value.
       
   286   inline HeapRegion* remove_head();
       
   287 
       
   288   // Convenience method.
       
   289   inline HeapRegion* remove_head_or_null();
       
   290 
       
   291   // It moves the regions from from_list to this list and empties
       
   292   // from_list. The new regions will appear in the same order as they
       
   293   // were in from_list and be linked in the end of this list.
       
   294   void add_as_tail(HeapRegionLinkedList* from_list);
       
   295 
       
   296   // It empties the list by removing all regions from it.
       
   297   void remove_all();
       
   298 
       
   299   // It removes all regions in the list that are pending for removal
       
   300   // (i.e., they have been tagged with "pending_removal"). The list
       
   301   // must not be empty, target_count should reflect the exact number
       
   302   // of regions that are pending for removal in the list, and
       
   303   // target_count should be > 1 (currently, we never need to remove a
       
   304   // single region using this).
       
   305   void remove_all_pending(size_t target_count);
       
   306 
       
   307   virtual void verify();
       
   308 
       
   309   virtual void print_on(outputStream* out, bool print_contents = false);
       
   310 };
       
   311 
       
   312 //////////////////// HeapRegionLinkedList ////////////////////
       
   313 
       
   314 // Iterator class that provides a convenient way to iterator over the
       
   315 // regions in a HeapRegionLinkedList instance.
       
   316 
       
   317 class HeapRegionLinkedListIterator : public StackObj {
       
   318 private:
       
   319   HeapRegionLinkedList* _list;
       
   320   HeapRegion*           _curr;
       
   321 
       
   322 public:
       
   323   bool more_available() {
       
   324     return _curr != NULL;
       
   325   }
       
   326 
       
   327   HeapRegion* get_next() {
       
   328     assert(more_available(),
       
   329            "get_next() should be called when more regions are available");
       
   330 
       
   331     // If we are going to introduce a count in the iterator we should
       
   332     // do the "cycle" check.
       
   333 
       
   334     HeapRegion* hr = _curr;
       
   335     assert(_list->verify_region(hr, _list), "region verification");
       
   336     _curr = hr->next();
       
   337     return hr;
       
   338   }
       
   339 
       
   340   HeapRegionLinkedListIterator(HeapRegionLinkedList* list)
       
   341     : _curr(NULL), _list(list) {
       
   342     _curr = list->head();
       
   343   }
       
   344 };
       
   345 
       
   346 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP