Thu, 22 Sep 2011 10:57:37 -0700
6484982: G1: process references during evacuation pauses
Summary: G1 now uses two reference processors - one is used by concurrent marking and the other is used by STW GCs (both full and incremental evacuation pauses). In an evacuation pause, the reference processor is embedded into the closures used to scan objects. Doing so causes causes reference objects to be 'discovered' by the reference processor. At the end of the evacuation pause, these discovered reference objects are processed - preserving (and copying) referent objects (and their reachable graphs) as appropriate.
Reviewed-by: ysr, jwilhelm, brutisso, stefank, tonyp
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 */
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 //////////////////// HeapRegionSetBase ////////////////////
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).
48 class hrs_ext_msg;
50 class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC {
51 friend class hrs_ext_msg;
53 protected:
54 static size_t calculate_region_num(HeapRegion* hr);
56 static size_t _unrealistically_long_length;
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;
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;
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.
72 // The sum of used bytes in the all the regions in the set.
73 size_t _total_used_bytes;
75 const char* _name;
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;
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);
93 // Indicates whether all regions in the set should be humongous or
94 // not. Only used during verification.
95 virtual bool regions_humongous() = 0;
97 // Indicates whether all regions in the set should be empty or
98 // not. Only used during verification.
99 virtual bool regions_empty() = 0;
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; }
106 // fill_in_ext_msg() writes the the values of the set's attributes
107 // in the custom err_msg (hrs_ext_msg). fill_in_ext_msg_extra()
108 // allows subclasses to append further information.
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);
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);
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);
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);
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);
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();
133 HeapRegionSetBase(const char* name);
135 public:
136 static void set_unrealistically_long_length(size_t len);
138 const char* name() { return _name; }
140 size_t length() { return _length; }
142 bool is_empty() { return _length == 0; }
144 size_t region_num() { return _region_num; }
146 size_t total_capacity_bytes() {
147 return region_num() << HeapRegion::LogOfHRGrainBytes;
148 }
150 size_t total_used_bytes() { return _total_used_bytes; }
152 virtual void verify();
153 void verify_start();
154 void verify_next_region(HeapRegion* hr);
155 void verify_end();
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
165 virtual void print_on(outputStream* out, bool print_contents = false);
166 };
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.
173 class hrs_ext_msg : public hrs_err_msg {
174 public:
175 hrs_ext_msg(HeapRegionSetBase* set, const char* message) : hrs_err_msg("") {
176 set->fill_in_ext_msg(this, message);
177 }
178 };
180 // These two macros are provided for convenience, to keep the uses of
181 // these two asserts a bit more concise.
183 #define hrs_assert_mt_safety_ok(_set_) \
184 do { \
185 assert((_set_)->check_mt_safety(), hrs_ext_msg((_set_), "MT safety")); \
186 } while (0)
188 #define hrs_assert_region_ok(_set_, _hr_, _expected_) \
189 do { \
190 assert((_set_)->verify_region((_hr_), (_expected_)), \
191 hrs_ext_msg((_set_), "region verification")); \
192 } while (0)
194 //////////////////// HeapRegionSet ////////////////////
196 #define hrs_assert_sets_match(_set1_, _set2_) \
197 do { \
198 assert(((_set1_)->regions_humongous() == \
199 (_set2_)->regions_humongous()) && \
200 ((_set1_)->regions_empty() == (_set2_)->regions_empty()), \
201 hrs_err_msg("the contents of set %s and set %s should match", \
202 (_set1_)->name(), (_set2_)->name())); \
203 } while (0)
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).
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 }
217 return HeapRegionSetBase::verify_region_extra(hr);
218 }
220 HeapRegionSet(const char* name) : HeapRegionSetBase(name) {
221 clear();
222 }
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);
229 // It removes hr from the set. The region should be a member of
230 // this set.
231 inline void remove(HeapRegion* hr);
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);
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 };
248 //////////////////// HeapRegionLinkedList ////////////////////
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.
256 class HeapRegionLinkedListIterator;
258 class HeapRegionLinkedList : public HeapRegionSetBase {
259 friend class HeapRegionLinkedListIterator;
261 private:
262 HeapRegion* _head;
263 HeapRegion* _tail;
265 // These are provided for use by the friend classes.
266 HeapRegion* head() { return _head; }
267 HeapRegion* tail() { return _tail; }
269 protected:
270 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg);
272 // See the comment for HeapRegionSetBase::clear()
273 virtual void clear();
275 HeapRegionLinkedList(const char* name) : HeapRegionSetBase(name) {
276 clear();
277 }
279 public:
280 // It adds hr to the list as the new head. The region should not be
281 // a member of another set.
282 inline void add_as_head(HeapRegion* hr);
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);
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();
292 // Convenience method.
293 inline HeapRegion* remove_head_or_null();
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
297 // were in from_list and be linked in the beginning of this list.
298 void add_as_head(HeapRegionLinkedList* from_list);
300 // It moves the regions from from_list to this list and empties
301 // from_list. The new regions will appear in the same order as they
302 // were in from_list and be linked in the end of this list.
303 void add_as_tail(HeapRegionLinkedList* from_list);
305 // It empties the list by removing all regions from it.
306 void remove_all();
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);
316 virtual void verify();
318 virtual void print_on(outputStream* out, bool print_contents = false);
319 };
321 //////////////////// HeapRegionLinkedListIterator ////////////////////
323 // Iterator class that provides a convenient way to iterate over the
324 // regions of a HeapRegionLinkedList instance.
326 class HeapRegionLinkedListIterator : public StackObj {
327 private:
328 HeapRegionLinkedList* _list;
329 HeapRegion* _curr;
331 public:
332 bool more_available() {
333 return _curr != NULL;
334 }
336 HeapRegion* get_next() {
337 assert(more_available(),
338 "get_next() should be called when more regions are available");
340 // If we are going to introduce a count in the iterator we should
341 // do the "cycle" check.
343 HeapRegion* hr = _curr;
344 assert(_list->verify_region(hr, _list), "region verification");
345 _curr = hr->next();
346 return hr;
347 }
349 HeapRegionLinkedListIterator(HeapRegionLinkedList* list)
350 : _curr(NULL), _list(list) {
351 _curr = list->head();
352 }
353 };
355 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP