Mercurial > hg > truffle
annotate src/share/vm/gc_implementation/g1/heapRegionSet.hpp @ 2432:455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
Summary: What the synopsis says.
Reviewed-by: jwilhelm, iveresov, johnc
author | tonyp |
---|---|
date | Tue, 29 Mar 2011 22:36:16 -0400 |
parents | 1216415d8e35 |
children | fe189d4a44e9 |
rev | line source |
---|---|
2152 | 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. | |
2361 | 31 #define HRS_ERR_MSG_BUFSZ 512 |
32 typedef FormatBuffer<HRS_ERR_MSG_BUFSZ> hrs_err_msg; | |
2152 | 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 | |
2361 | 48 class hrs_ext_msg; |
2152 | 49 |
50 class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC { | |
2361 | 51 friend class hrs_ext_msg; |
2152 | 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 | |
2361 | 107 // in the custom err_msg (hrs_ext_msg). fill_in_ext_msg_extra() |
2152 | 108 // allows subclasses to append further information. |
2361 | 109 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg) { } |
110 void fill_in_ext_msg(hrs_ext_msg* msg, const char* message); | |
2152 | 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 | |
2361 | 173 class hrs_ext_msg : public hrs_err_msg { |
2152 | 174 public: |
2361 | 175 hrs_ext_msg(HeapRegionSetBase* set, const char* message) : hrs_err_msg("") { |
2152 | 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 | |
2361 | 183 #define hrs_assert_mt_safety_ok(_set_) \ |
2152 | 184 do { \ |
2361 | 185 assert((_set_)->check_mt_safety(), hrs_ext_msg((_set_), "MT safety")); \ |
2152 | 186 } while (0) |
187 | |
2361 | 188 #define hrs_assert_region_ok(_set_, _hr_, _expected_) \ |
2152 | 189 do { \ |
190 assert((_set_)->verify_region((_hr_), (_expected_)), \ | |
2361 | 191 hrs_ext_msg((_set_), "region verification")); \ |
2152 | 192 } while (0) |
193 | |
194 //////////////////// HeapRegionSet //////////////////// | |
195 | |
2361 | 196 #define hrs_assert_sets_match(_set1_, _set2_) \ |
2152 | 197 do { \ |
198 assert(((_set1_)->regions_humongous() == \ | |
199 (_set2_)->regions_humongous()) && \ | |
200 ((_set1_)->regions_empty() == (_set2_)->regions_empty()), \ | |
2361 | 201 hrs_err_msg("the contents of set %s and set %s should match", \ |
2152 | 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: | |
2361 | 270 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg); |
2152 | 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: | |
2432
455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
tonyp
parents:
2361
diff
changeset
|
280 // It adds hr to the list as the new head. The region should not be |
455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
tonyp
parents:
2361
diff
changeset
|
281 // a member of another set. |
455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
tonyp
parents:
2361
diff
changeset
|
282 inline void add_as_head(HeapRegion* hr); |
455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
tonyp
parents:
2361
diff
changeset
|
283 |
2152 | 284 // It adds hr to the list as the new tail. The region should not be |
285 // a member of another set. | |
286 inline void add_as_tail(HeapRegion* hr); | |
287 | |
288 // It removes and returns the head of the list. It assumes that the | |
289 // list is not empty so it will return a non-NULL value. | |
290 inline HeapRegion* remove_head(); | |
291 | |
292 // Convenience method. | |
293 inline HeapRegion* remove_head_or_null(); | |
294 | |
295 // It moves the regions from from_list to this list and empties | |
296 // from_list. The new regions will appear in the same order as they | |
2432
455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
tonyp
parents:
2361
diff
changeset
|
297 // were in from_list and be linked in the beginning of this list. |
455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
tonyp
parents:
2361
diff
changeset
|
298 void add_as_head(HeapRegionLinkedList* from_list); |
455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
tonyp
parents:
2361
diff
changeset
|
299 |
455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
tonyp
parents:
2361
diff
changeset
|
300 // It moves the regions from from_list to this list and empties |
455328d90876
7029458: G1: Add newly-reclaimed regions to the beginning of the region free list, not the end
tonyp
parents:
2361
diff
changeset
|
301 // from_list. The new regions will appear in the same order as they |
2152 | 302 // were in from_list and be linked in the end of this list. |
303 void add_as_tail(HeapRegionLinkedList* from_list); | |
304 | |
305 // It empties the list by removing all regions from it. | |
306 void remove_all(); | |
307 | |
308 // It removes all regions in the list that are pending for removal | |
309 // (i.e., they have been tagged with "pending_removal"). The list | |
310 // must not be empty, target_count should reflect the exact number | |
311 // of regions that are pending for removal in the list, and | |
312 // target_count should be > 1 (currently, we never need to remove a | |
313 // single region using this). | |
314 void remove_all_pending(size_t target_count); | |
315 | |
316 virtual void verify(); | |
317 | |
318 virtual void print_on(outputStream* out, bool print_contents = false); | |
319 }; | |
320 | |
2361 | 321 //////////////////// HeapRegionLinkedListIterator //////////////////// |
2152 | 322 |
2361 | 323 // Iterator class that provides a convenient way to iterate over the |
324 // regions of a HeapRegionLinkedList instance. | |
2152 | 325 |
326 class HeapRegionLinkedListIterator : public StackObj { | |
327 private: | |
328 HeapRegionLinkedList* _list; | |
329 HeapRegion* _curr; | |
330 | |
331 public: | |
332 bool more_available() { | |
333 return _curr != NULL; | |
334 } | |
335 | |
336 HeapRegion* get_next() { | |
337 assert(more_available(), | |
338 "get_next() should be called when more regions are available"); | |
339 | |
340 // If we are going to introduce a count in the iterator we should | |
341 // do the "cycle" check. | |
342 | |
343 HeapRegion* hr = _curr; | |
344 assert(_list->verify_region(hr, _list), "region verification"); | |
345 _curr = hr->next(); | |
346 return hr; | |
347 } | |
348 | |
349 HeapRegionLinkedListIterator(HeapRegionLinkedList* list) | |
350 : _curr(NULL), _list(list) { | |
351 _curr = list->head(); | |
352 } | |
353 }; | |
354 | |
355 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP |