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