comparison src/share/vm/gc_implementation/g1/heapRegionSet.hpp @ 17736:58fc1b1523dc

8034079: G1: Refactor the HeapRegionSet hierarchy Reviewed-by: tschatzl, pliden
author brutisso
date Fri, 14 Mar 2014 10:15:46 +0100
parents a2f7274eb6ef
children 8ee855b4e667
comparison
equal deleted inserted replaced
17735:8f28240318a2 17736:58fc1b1523dc
36 // asserts are compiled in. 36 // asserts are compiled in.
37 #ifndef HEAP_REGION_SET_FORCE_VERIFY 37 #ifndef HEAP_REGION_SET_FORCE_VERIFY
38 #define HEAP_REGION_SET_FORCE_VERIFY defined(ASSERT) 38 #define HEAP_REGION_SET_FORCE_VERIFY defined(ASSERT)
39 #endif // HEAP_REGION_SET_FORCE_VERIFY 39 #endif // HEAP_REGION_SET_FORCE_VERIFY
40 40
41 //////////////////// HeapRegionSetBase //////////////////// 41 class hrs_ext_msg;
42
43 class HRSMtSafeChecker : public CHeapObj<mtGC> {
44 public:
45 virtual void check() = 0;
46 };
47
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(); };
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 };
42 74
43 // Base class for all the classes that represent heap region sets. It 75 // Base class for all the classes that represent heap region sets. It
44 // contains the basic attributes that each set needs to maintain 76 // contains the basic attributes that each set needs to maintain
45 // (e.g., length, region num, used bytes sum) plus any shared 77 // (e.g., length, region num, used bytes sum) plus any shared
46 // functionality (e.g., verification). 78 // functionality (e.g., verification).
47 79
48 class hrs_ext_msg;
49
50 typedef enum {
51 HRSPhaseNone,
52 HRSPhaseEvacuation,
53 HRSPhaseCleanup,
54 HRSPhaseFullGC
55 } HRSPhase;
56
57 class HRSPhaseSetter;
58
59 class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC { 80 class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC {
60 friend class hrs_ext_msg;
61 friend class HRSPhaseSetter;
62 friend class VMStructs; 81 friend class VMStructs;
82 private:
83 bool _is_humongous;
84 bool _is_empty;
85 HRSMtSafeChecker* _mt_safety_checker;
63 86
64 protected: 87 protected:
65 static uint _unrealistically_long_length;
66
67 // The number of regions added to the set. If the set contains 88 // The number of regions added to the set. If the set contains
68 // only humongous regions, this reflects only 'starts humongous' 89 // only humongous regions, this reflects only 'starts humongous'
69 // regions and does not include 'continues humongous' ones. 90 // regions and does not include 'continues humongous' ones.
70 uint _length; 91 HeapRegionSetCount _count;
71
72 // The total number of regions represented by the set. If the set
73 // does not contain humongous regions, this should be the same as
74 // _length. If the set contains only humongous regions, this will
75 // include the 'continues humongous' regions.
76 uint _region_num;
77
78 // We don't keep track of the total capacity explicitly, we instead
79 // recalculate it based on _region_num and the heap region size.
80
81 // The sum of used bytes in the all the regions in the set.
82 size_t _total_used_bytes;
83 92
84 const char* _name; 93 const char* _name;
85 94
86 bool _verify_in_progress; 95 bool _verify_in_progress;
87 uint _calc_length;
88 uint _calc_region_num;
89 size_t _calc_total_capacity_bytes;
90 size_t _calc_total_used_bytes;
91
92 // This is here so that it can be used in the subclasses to assert
93 // something different depending on which phase the GC is in. This
94 // can be particularly helpful in the check_mt_safety() methods.
95 static HRSPhase _phase;
96
97 // Only used by HRSPhaseSetter.
98 static void clear_phase();
99 static void set_phase(HRSPhase phase);
100 96
101 // verify_region() is used to ensure that the contents of a region 97 // verify_region() is used to ensure that the contents of a region
102 // added to / removed from a set are consistent. Different sets 98 // added to / removed from a set are consistent.
103 // make different assumptions about the regions added to them. So 99 void verify_region(HeapRegion* hr) PRODUCT_RETURN;
104 // each set can override verify_region_extra(), which is called
105 // from verify_region(), and do any extra verification it needs to
106 // perform in that.
107 virtual const char* verify_region_extra(HeapRegion* hr) { return NULL; }
108 bool verify_region(HeapRegion* hr,
109 HeapRegionSetBase* expected_containing_set);
110 100
111 // Indicates whether all regions in the set should be humongous or 101 // Indicates whether all regions in the set should be humongous or
112 // not. Only used during verification. 102 // not. Only used during verification.
113 virtual bool regions_humongous() = 0; 103 bool regions_humongous() { return _is_humongous; }
114 104
115 // Indicates whether all regions in the set should be empty or 105 // Indicates whether all regions in the set should be empty or
116 // not. Only used during verification. 106 // not. Only used during verification.
117 virtual bool regions_empty() = 0; 107 bool regions_empty() { return _is_empty; }
118 108
119 // Subclasses can optionally override this to do MT safety protocol 109 void check_mt_safety() {
120 // checks. It is called in an assert from all methods that perform 110 if (_mt_safety_checker != NULL) {
121 // updates on the set (and subclasses should also call it too). 111 _mt_safety_checker->check();
122 virtual bool check_mt_safety() { return true; } 112 }
113 }
114
115 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg) { }
116
117 HeapRegionSetBase(const char* name, bool humongous, bool empty, HRSMtSafeChecker* mt_safety_checker);
118
119 public:
120 const char* name() { return _name; }
121
122 uint length() { return _count.length(); }
123
124 bool is_empty() { return _count.length() == 0; }
125
126 size_t total_capacity_bytes() {
127 return _count.capacity();
128 }
129
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);
133
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);
123 137
124 // fill_in_ext_msg() writes the the values of the set's attributes 138 // fill_in_ext_msg() writes the the values of the set's attributes
125 // in the custom err_msg (hrs_ext_msg). fill_in_ext_msg_extra() 139 // in the custom err_msg (hrs_ext_msg). fill_in_ext_msg_extra()
126 // allows subclasses to append further information. 140 // allows subclasses to append further information.
127 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg) { }
128 void fill_in_ext_msg(hrs_ext_msg* msg, const char* message); 141 void fill_in_ext_msg(hrs_ext_msg* msg, const char* message);
129
130 // It updates the fields of the set to reflect hr being added to
131 // the set.
132 inline void update_for_addition(HeapRegion* hr);
133
134 // It updates the fields of the set to reflect hr being added to
135 // the set and tags the region appropriately.
136 inline void add_internal(HeapRegion* hr);
137
138 // It updates the fields of the set to reflect hr being removed
139 // from the set.
140 inline void update_for_removal(HeapRegion* hr);
141
142 // It updates the fields of the set to reflect hr being removed
143 // from the set and tags the region appropriately.
144 inline void remove_internal(HeapRegion* hr);
145
146 // It clears all the fields of the sets. Note: it will not iterate
147 // over the set and remove regions from it. It assumes that the
148 // caller has already done so. It will literally just clear the fields.
149 virtual void clear();
150
151 HeapRegionSetBase(const char* name);
152
153 public:
154 static void set_unrealistically_long_length(uint len);
155
156 const char* name() { return _name; }
157
158 uint length() { return _length; }
159
160 bool is_empty() { return _length == 0; }
161
162 uint region_num() { return _region_num; }
163
164 size_t total_capacity_bytes() {
165 return (size_t) region_num() << HeapRegion::LogOfHRGrainBytes;
166 }
167
168 size_t total_used_bytes() { return _total_used_bytes; }
169 142
170 virtual void verify(); 143 virtual void verify();
171 void verify_start(); 144 void verify_start();
172 void verify_next_region(HeapRegion* hr); 145 void verify_next_region(HeapRegion* hr);
173 void verify_end(); 146 void verify_end();
185 158
186 // Customized err_msg for heap region sets. Apart from a 159 // Customized err_msg for heap region sets. Apart from a
187 // assert/guarantee-specific message it also prints out the values of 160 // assert/guarantee-specific message it also prints out the values of
188 // the fields of the associated set. This can be very helpful in 161 // the fields of the associated set. This can be very helpful in
189 // diagnosing failures. 162 // diagnosing failures.
190
191 class hrs_ext_msg : public hrs_err_msg { 163 class hrs_ext_msg : public hrs_err_msg {
192 public: 164 public:
193 hrs_ext_msg(HeapRegionSetBase* set, const char* message) : hrs_err_msg("") { 165 hrs_ext_msg(HeapRegionSetBase* set, const char* message) : hrs_err_msg("") {
194 set->fill_in_ext_msg(this, message); 166 set->fill_in_ext_msg(this, message);
195 } 167 }
196 }; 168 };
197
198 class HRSPhaseSetter {
199 public:
200 HRSPhaseSetter(HRSPhase phase) {
201 HeapRegionSetBase::set_phase(phase);
202 }
203 ~HRSPhaseSetter() {
204 HeapRegionSetBase::clear_phase();
205 }
206 };
207
208 // These two macros are provided for convenience, to keep the uses of
209 // these two asserts a bit more concise.
210
211 #define hrs_assert_mt_safety_ok(_set_) \
212 do { \
213 assert((_set_)->check_mt_safety(), hrs_ext_msg((_set_), "MT safety")); \
214 } while (0)
215
216 #define hrs_assert_region_ok(_set_, _hr_, _expected_) \
217 do { \
218 assert((_set_)->verify_region((_hr_), (_expected_)), \
219 hrs_ext_msg((_set_), "region verification")); \
220 } while (0)
221
222 //////////////////// HeapRegionSet ////////////////////
223 169
224 #define hrs_assert_sets_match(_set1_, _set2_) \ 170 #define hrs_assert_sets_match(_set1_, _set2_) \
225 do { \ 171 do { \
226 assert(((_set1_)->regions_humongous() == \ 172 assert(((_set1_)->regions_humongous() == \
227 (_set2_)->regions_humongous()) && \ 173 (_set2_)->regions_humongous()) && \
234 // explicitly tracked. It's helpful to group regions using such sets 180 // explicitly tracked. It's helpful to group regions using such sets
235 // so that we can reason about all the region groups in the heap using 181 // so that we can reason about all the region groups in the heap using
236 // the same interface (namely, the HeapRegionSetBase API). 182 // the same interface (namely, the HeapRegionSetBase API).
237 183
238 class HeapRegionSet : public HeapRegionSetBase { 184 class HeapRegionSet : public HeapRegionSetBase {
239 protected: 185 public:
240 virtual const char* verify_region_extra(HeapRegion* hr) { 186 HeapRegionSet(const char* name, bool humongous, HRSMtSafeChecker* mt_safety_checker):
241 if (hr->next() != NULL) { 187 HeapRegionSetBase(name, humongous, false /* empty */, mt_safety_checker) { }
242 return "next() should always be NULL as we do not link the regions"; 188
243 } 189 void bulk_remove(const HeapRegionSetCount& removed) {
244 190 _count.decrement(removed.length(), removed.capacity());
245 return HeapRegionSetBase::verify_region_extra(hr); 191 }
246 } 192 };
247
248 HeapRegionSet(const char* name) : HeapRegionSetBase(name) {
249 clear();
250 }
251
252 public:
253 // It adds hr to the set. The region should not be a member of
254 // another set.
255 inline void add(HeapRegion* hr);
256
257 // It removes hr from the set. The region should be a member of
258 // this set.
259 inline void remove(HeapRegion* hr);
260
261 // It removes a region from the set. Instead of updating the fields
262 // of the set to reflect this removal, it accumulates the updates
263 // in proxy_set. The idea is that proxy_set is thread-local to
264 // avoid multiple threads updating the fields of the set
265 // concurrently and having to synchronize. The method
266 // update_from_proxy() will update the fields of the set from the
267 // proxy_set.
268 inline void remove_with_proxy(HeapRegion* hr, HeapRegionSet* proxy_set);
269
270 // After multiple calls to remove_with_proxy() the updates to the
271 // fields of the set are accumulated in proxy_set. This call
272 // updates the fields of the set from proxy_set.
273 void update_from_proxy(HeapRegionSet* proxy_set);
274 };
275
276 //////////////////// HeapRegionLinkedList ////////////////////
277 193
278 // A set that links all the regions added to it in a singly-linked 194 // A set that links all the regions added to it in a singly-linked
279 // list. We should try to avoid doing operations that iterate over 195 // list. We should try to avoid doing operations that iterate over
280 // such lists in performance critical paths. Typically we should 196 // such lists in performance critical paths. Typically we should
281 // add / remove one region at a time or concatenate two lists. All 197 // add / remove one region at a time or concatenate two lists. All
282 // those operations are done in constant time. 198 // those operations are done in constant time.
283 199
284 class HeapRegionLinkedListIterator; 200 class FreeRegionListIterator;
285 201
286 class HeapRegionLinkedList : public HeapRegionSetBase { 202 class FreeRegionList : public HeapRegionSetBase {
287 friend class HeapRegionLinkedListIterator; 203 friend class FreeRegionListIterator;
288 204
289 private: 205 private:
290 HeapRegion* _head; 206 HeapRegion* _head;
291 HeapRegion* _tail; 207 HeapRegion* _tail;
292 208
293 // These are provided for use by the friend classes. 209 static uint _unrealistically_long_length;
210
211 void add_as_head_or_tail(FreeRegionList* from_list, bool as_head);
212
213 protected:
214 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg);
215
216 // See the comment for HeapRegionSetBase::clear()
217 virtual void clear();
218
219 public:
220 FreeRegionList(const char* name, HRSMtSafeChecker* mt_safety_checker = NULL):
221 HeapRegionSetBase(name, false /* humongous */, true /* empty */, mt_safety_checker) {
222 clear();
223 }
224
225 void verify_list();
226
294 HeapRegion* head() { return _head; } 227 HeapRegion* head() { return _head; }
295 HeapRegion* tail() { return _tail; } 228 HeapRegion* tail() { return _tail; }
296 229
297 protected: 230 static void set_unrealistically_long_length(uint len);
298 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg); 231
299
300 // See the comment for HeapRegionSetBase::clear()
301 virtual void clear();
302
303 HeapRegionLinkedList(const char* name) : HeapRegionSetBase(name) {
304 clear();
305 }
306
307 public:
308 // It adds hr to the list as the new head. The region should not be 232 // It adds hr to the list as the new head. The region should not be
309 // a member of another set. 233 // a member of another set.
310 inline void add_as_head(HeapRegion* hr); 234 inline void add_as_head(HeapRegion* hr);
311 235
312 // It adds hr to the list as the new tail. The region should not be 236 // It adds hr to the list as the new tail. The region should not be
321 inline HeapRegion* remove_head_or_null(); 245 inline HeapRegion* remove_head_or_null();
322 246
323 // It moves the regions from from_list to this list and empties 247 // It moves the regions from from_list to this list and empties
324 // from_list. The new regions will appear in the same order as they 248 // from_list. The new regions will appear in the same order as they
325 // were in from_list and be linked in the beginning of this list. 249 // were in from_list and be linked in the beginning of this list.
326 void add_as_head(HeapRegionLinkedList* from_list); 250 void add_as_head(FreeRegionList* from_list);
327 251
328 // It moves the regions from from_list to this list and empties 252 // It moves the regions from from_list to this list and empties
329 // from_list. The new regions will appear in the same order as they 253 // from_list. The new regions will appear in the same order as they
330 // were in from_list and be linked in the end of this list. 254 // were in from_list and be linked in the end of this list.
331 void add_as_tail(HeapRegionLinkedList* from_list); 255 void add_as_tail(FreeRegionList* from_list);
332 256
333 // It empties the list by removing all regions from it. 257 // It empties the list by removing all regions from it.
334 void remove_all(); 258 void remove_all();
335 259
336 // It removes all regions in the list that are pending for removal 260 // It removes all regions in the list that are pending for removal
344 virtual void verify(); 268 virtual void verify();
345 269
346 virtual void print_on(outputStream* out, bool print_contents = false); 270 virtual void print_on(outputStream* out, bool print_contents = false);
347 }; 271 };
348 272
349 //////////////////// HeapRegionLinkedListIterator ////////////////////
350
351 // Iterator class that provides a convenient way to iterate over the 273 // Iterator class that provides a convenient way to iterate over the
352 // regions of a HeapRegionLinkedList instance. 274 // regions of a HeapRegionLinkedList instance.
353 275
354 class HeapRegionLinkedListIterator : public StackObj { 276 class FreeRegionListIterator : public StackObj {
355 private: 277 private:
356 HeapRegionLinkedList* _list; 278 FreeRegionList* _list;
357 HeapRegion* _curr; 279 HeapRegion* _curr;
358 280
359 public: 281 public:
360 bool more_available() { 282 bool more_available() {
361 return _curr != NULL; 283 return _curr != NULL;
367 289
368 // If we are going to introduce a count in the iterator we should 290 // If we are going to introduce a count in the iterator we should
369 // do the "cycle" check. 291 // do the "cycle" check.
370 292
371 HeapRegion* hr = _curr; 293 HeapRegion* hr = _curr;
372 assert(_list->verify_region(hr, _list), "region verification"); 294 _list->verify_region(hr);
373 _curr = hr->next(); 295 _curr = hr->next();
374 return hr; 296 return hr;
375 } 297 }
376 298
377 HeapRegionLinkedListIterator(HeapRegionLinkedList* list) 299 FreeRegionListIterator(FreeRegionList* list)
378 : _curr(NULL), _list(list) { 300 : _curr(NULL), _list(list) {
379 _curr = list->head(); 301 _curr = list->head();
380 } 302 }
381 }; 303 };
382 304