Mon, 21 Jul 2014 10:00:31 +0200
8048112: G1 Full GC needs to support the case when the very first region is not available
Summary: Refactor preparation for compaction during Full GC so that it lazily initializes the first compaction point. This also avoids problems later when the first region may not be committed. Also reviewed by K. Barrett.
Reviewed-by: brutisso
1 /*
2 * Copyright (c) 2011, 2014, 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 */
25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP
26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP
28 #include "gc_implementation/g1/heapRegion.hpp"
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;
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
41 class hrs_ext_msg;
43 class HRSMtSafeChecker : public CHeapObj<mtGC> {
44 public:
45 virtual void check() = 0;
46 };
48 class MasterFreeRegionListMtSafeChecker : public HRSMtSafeChecker { public: void check(); };
49 class SecondaryFreeRegionListMtSafeChecker : public HRSMtSafeChecker { public: void check(); };
50 class HumongousRegionSetMtSafeChecker : public HRSMtSafeChecker { public: void check(); };
51 class OldRegionSetMtSafeChecker : public HRSMtSafeChecker { public: void check(); };
53 class HeapRegionSetCount VALUE_OBJ_CLASS_SPEC {
54 friend class VMStructs;
55 uint _length;
56 size_t _capacity;
58 public:
59 HeapRegionSetCount() : _length(0), _capacity(0) { }
61 const uint length() const { return _length; }
62 const size_t capacity() const { return _capacity; }
64 void increment(uint length_to_add, size_t capacity_to_add) {
65 _length += length_to_add;
66 _capacity += capacity_to_add;
67 }
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 };
75 // Base class for all the classes that represent heap region sets. It
76 // contains the basic attributes that each set needs to maintain
77 // (e.g., length, region num, used bytes sum) plus any shared
78 // functionality (e.g., verification).
80 class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC {
81 friend class VMStructs;
82 private:
83 bool _is_humongous;
84 bool _is_empty;
85 HRSMtSafeChecker* _mt_safety_checker;
87 protected:
88 // The number of regions added to the set. If the set contains
89 // only humongous regions, this reflects only 'starts humongous'
90 // regions and does not include 'continues humongous' ones.
91 HeapRegionSetCount _count;
93 const char* _name;
95 bool _verify_in_progress;
97 // verify_region() is used to ensure that the contents of a region
98 // added to / removed from a set are consistent.
99 void verify_region(HeapRegion* hr) PRODUCT_RETURN;
101 // Indicates whether all regions in the set should be humongous or
102 // not. Only used during verification.
103 bool regions_humongous() { return _is_humongous; }
105 // Indicates whether all regions in the set should be empty or
106 // not. Only used during verification.
107 bool regions_empty() { return _is_empty; }
109 void check_mt_safety() {
110 if (_mt_safety_checker != NULL) {
111 _mt_safety_checker->check();
112 }
113 }
115 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg) { }
117 HeapRegionSetBase(const char* name, bool humongous, bool empty, HRSMtSafeChecker* mt_safety_checker);
119 public:
120 const char* name() { return _name; }
122 uint length() const { return _count.length(); }
124 bool is_empty() { return _count.length() == 0; }
126 size_t total_capacity_bytes() {
127 return _count.capacity();
128 }
130 // It updates the fields of the set to reflect hr being added to
131 // the set and tags the region appropriately.
132 inline void add(HeapRegion* hr);
134 // It updates the fields of the set to reflect hr being removed
135 // from the set and tags the region appropriately.
136 inline void remove(HeapRegion* hr);
138 // fill_in_ext_msg() writes the the values of the set's attributes
139 // in the custom err_msg (hrs_ext_msg). fill_in_ext_msg_extra()
140 // allows subclasses to append further information.
141 void fill_in_ext_msg(hrs_ext_msg* msg, const char* message);
143 virtual void verify();
144 void verify_start();
145 void verify_next_region(HeapRegion* hr);
146 void verify_end();
148 #if HEAP_REGION_SET_FORCE_VERIFY
149 void verify_optional() {
150 verify();
151 }
152 #else // HEAP_REGION_SET_FORCE_VERIFY
153 void verify_optional() { }
154 #endif // HEAP_REGION_SET_FORCE_VERIFY
156 virtual void print_on(outputStream* out, bool print_contents = false);
157 };
159 // Customized err_msg for heap region sets. Apart from a
160 // assert/guarantee-specific message it also prints out the values of
161 // the fields of the associated set. This can be very helpful in
162 // diagnosing failures.
163 class hrs_ext_msg : public hrs_err_msg {
164 public:
165 hrs_ext_msg(HeapRegionSetBase* set, const char* message) : hrs_err_msg("%s","") {
166 set->fill_in_ext_msg(this, message);
167 }
168 };
170 #define hrs_assert_sets_match(_set1_, _set2_) \
171 do { \
172 assert(((_set1_)->regions_humongous() == \
173 (_set2_)->regions_humongous()) && \
174 ((_set1_)->regions_empty() == (_set2_)->regions_empty()), \
175 hrs_err_msg("the contents of set %s and set %s should match", \
176 (_set1_)->name(), (_set2_)->name())); \
177 } while (0)
179 // This class represents heap region sets whose members are not
180 // explicitly tracked. It's helpful to group regions using such sets
181 // so that we can reason about all the region groups in the heap using
182 // the same interface (namely, the HeapRegionSetBase API).
184 class HeapRegionSet : public HeapRegionSetBase {
185 public:
186 HeapRegionSet(const char* name, bool humongous, HRSMtSafeChecker* mt_safety_checker):
187 HeapRegionSetBase(name, humongous, false /* empty */, mt_safety_checker) { }
189 void bulk_remove(const HeapRegionSetCount& removed) {
190 _count.decrement(removed.length(), removed.capacity());
191 }
192 };
194 // A set that links all the regions added to it in a doubly-linked
195 // list. We should try to avoid doing operations that iterate over
196 // such lists in performance critical paths. Typically we should
197 // add / remove one region at a time or concatenate two lists. There are
198 // two ways to treat your lists, ordered and un-ordered. All un-ordered
199 // operations are done in constant time. To keep a list ordered only use
200 // add_ordered() to add elements to the list. If a list is not ordered
201 // from start, there is no way to sort it later.
203 class FreeRegionListIterator;
205 class FreeRegionList : public HeapRegionSetBase {
206 friend class FreeRegionListIterator;
208 private:
209 HeapRegion* _head;
210 HeapRegion* _tail;
212 // _last is used to keep track of where we added an element the last
213 // time in ordered lists. It helps to improve performance when adding
214 // several ordered items in a row.
215 HeapRegion* _last;
217 static uint _unrealistically_long_length;
219 void add_as_head_or_tail(FreeRegionList* from_list, bool as_head);
221 protected:
222 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg);
224 // See the comment for HeapRegionSetBase::clear()
225 virtual void clear();
227 public:
228 FreeRegionList(const char* name, HRSMtSafeChecker* mt_safety_checker = NULL):
229 HeapRegionSetBase(name, false /* humongous */, true /* empty */, mt_safety_checker) {
230 clear();
231 }
233 void verify_list();
235 HeapRegion* head() { return _head; }
236 HeapRegion* tail() { return _tail; }
238 static void set_unrealistically_long_length(uint len);
240 // Add hr to the list. The region should not be a member of another set.
241 // Assumes that the list is ordered and will preserve that order. The order
242 // is determined by hrs_index.
243 inline void add_ordered(HeapRegion* hr);
245 // It adds hr to the list as the new head. The region should not be
246 // a member of another set.
247 inline void add_as_head(HeapRegion* hr);
249 // It adds hr to the list as the new tail. The region should not be
250 // a member of another set.
251 inline void add_as_tail(HeapRegion* hr);
253 // It removes and returns the head of the list. It assumes that the
254 // list is not empty so it will return a non-NULL value.
255 inline HeapRegion* remove_head();
257 // Convenience method.
258 inline HeapRegion* remove_head_or_null();
260 // Removes and returns the last element (_tail) of the list. It assumes
261 // that the list isn't empty so that it can return a non-NULL value.
262 inline HeapRegion* remove_tail();
264 // Convenience method
265 inline HeapRegion* remove_tail_or_null();
267 // Removes from head or tail based on the given argument.
268 inline HeapRegion* remove_region(bool from_head);
270 // Merge two ordered lists. The result is also ordered. The order is
271 // determined by hrs_index.
272 void add_ordered(FreeRegionList* from_list);
274 // It moves the regions from from_list to this list and empties
275 // from_list. The new regions will appear in the same order as they
276 // were in from_list and be linked in the beginning of this list.
277 void add_as_head(FreeRegionList* from_list);
279 // It moves the regions from from_list to this list and empties
280 // from_list. The new regions will appear in the same order as they
281 // were in from_list and be linked in the end of this list.
282 void add_as_tail(FreeRegionList* from_list);
284 // It empties the list by removing all regions from it.
285 void remove_all();
287 // It removes all regions in the list that are pending for removal
288 // (i.e., they have been tagged with "pending_removal"). The list
289 // must not be empty, target_count should reflect the exact number
290 // of regions that are pending for removal in the list, and
291 // target_count should be > 1 (currently, we never need to remove a
292 // single region using this).
293 void remove_all_pending(uint target_count);
295 virtual void verify();
297 virtual void print_on(outputStream* out, bool print_contents = false);
298 };
300 // Iterator class that provides a convenient way to iterate over the
301 // regions of a HeapRegionLinkedList instance.
303 class FreeRegionListIterator : public StackObj {
304 private:
305 FreeRegionList* _list;
306 HeapRegion* _curr;
308 public:
309 bool more_available() {
310 return _curr != NULL;
311 }
313 HeapRegion* get_next() {
314 assert(more_available(),
315 "get_next() should be called when more regions are available");
317 // If we are going to introduce a count in the iterator we should
318 // do the "cycle" check.
320 HeapRegion* hr = _curr;
321 _list->verify_region(hr);
322 _curr = hr->next();
323 return hr;
324 }
326 FreeRegionListIterator(FreeRegionList* list) : _curr(NULL), _list(list) {
327 _curr = list->head();
328 }
329 };
331 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP