Mercurial > hg > graal-jvmci-8
annotate src/share/vm/gc_implementation/g1/heapRegionRemSet.hpp @ 1387:0bfd3fb24150
6858496: Clear all SoftReferences before an out-of-memory due to GC overhead limit.
Summary: Ensure a full GC that clears SoftReferences before throwing an out-of-memory
Reviewed-by: ysr, jcoomes
author | jmasa |
---|---|
date | Tue, 13 Apr 2010 13:52:10 -0700 |
parents | 0414c1049f15 |
children | f9ec1e4bbb44 |
rev | line source |
---|---|
342 | 1 /* |
579 | 2 * Copyright 2001-2009 Sun Microsystems, Inc. All Rights Reserved. |
342 | 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
20 * CA 95054 USA or visit www.sun.com if you need additional information or | |
21 * have any questions. | |
22 * | |
23 */ | |
24 | |
25 // Remembered set for a heap region. Represent a set of "cards" that | |
26 // contain pointers into the owner heap region. Cards are defined somewhat | |
27 // abstractly, in terms of what the "BlockOffsetTable" in use can parse. | |
28 | |
29 class G1CollectedHeap; | |
30 class G1BlockOffsetSharedArray; | |
31 class HeapRegion; | |
32 class HeapRegionRemSetIterator; | |
33 class PosParPRT; | |
34 class SparsePRT; | |
35 | |
36 | |
37 // The "_coarse_map" is a bitmap with one bit for each region, where set | |
38 // bits indicate that the corresponding region may contain some pointer | |
39 // into the owning region. | |
40 | |
41 // The "_fine_grain_entries" array is an open hash table of PerRegionTables | |
42 // (PRTs), indicating regions for which we're keeping the RS as a set of | |
43 // cards. The strategy is to cap the size of the fine-grain table, | |
44 // deleting an entry and setting the corresponding coarse-grained bit when | |
45 // we would overflow this cap. | |
46 | |
47 // We use a mixture of locking and lock-free techniques here. We allow | |
48 // threads to locate PRTs without locking, but threads attempting to alter | |
49 // a bucket list obtain a lock. This means that any failing attempt to | |
50 // find a PRT must be retried with the lock. It might seem dangerous that | |
51 // a read can find a PRT that is concurrently deleted. This is all right, | |
52 // because: | |
53 // | |
54 // 1) We only actually free PRT's at safe points (though we reuse them at | |
55 // other times). | |
56 // 2) We find PRT's in an attempt to add entries. If a PRT is deleted, | |
57 // it's _coarse_map bit is set, so the that we were attempting to add | |
58 // is represented. If a deleted PRT is re-used, a thread adding a bit, | |
59 // thinking the PRT is for a different region, does no harm. | |
60 | |
549
fe3d7c11b4b7
6700941: G1: allocation spec missing for some G1 classes
apetrusenko
parents:
342
diff
changeset
|
61 class OtherRegionsTable VALUE_OBJ_CLASS_SPEC { |
342 | 62 friend class HeapRegionRemSetIterator; |
63 | |
64 G1CollectedHeap* _g1h; | |
65 Mutex _m; | |
66 HeapRegion* _hr; | |
67 | |
68 // These are protected by "_m". | |
69 BitMap _coarse_map; | |
70 size_t _n_coarse_entries; | |
71 static jint _n_coarsenings; | |
72 | |
73 PosParPRT** _fine_grain_regions; | |
74 size_t _n_fine_entries; | |
75 | |
76 #define SAMPLE_FOR_EVICTION 1 | |
77 #if SAMPLE_FOR_EVICTION | |
78 size_t _fine_eviction_start; | |
79 static size_t _fine_eviction_stride; | |
80 static size_t _fine_eviction_sample_size; | |
81 #endif | |
82 | |
83 SparsePRT _sparse_table; | |
84 | |
85 // These are static after init. | |
86 static size_t _max_fine_entries; | |
87 static size_t _mod_max_fine_entries_mask; | |
88 | |
89 // Requires "prt" to be the first element of the bucket list appropriate | |
90 // for "hr". If this list contains an entry for "hr", return it, | |
91 // otherwise return "NULL". | |
92 PosParPRT* find_region_table(size_t ind, HeapRegion* hr) const; | |
93 | |
94 // Find, delete, and return a candidate PosParPRT, if any exists, | |
95 // adding the deleted region to the coarse bitmap. Requires the caller | |
96 // to hold _m, and the fine-grain table to be full. | |
97 PosParPRT* delete_region_table(); | |
98 | |
99 // If a PRT for "hr" is in the bucket list indicated by "ind" (which must | |
100 // be the correct index for "hr"), delete it and return true; else return | |
101 // false. | |
102 bool del_single_region_table(size_t ind, HeapRegion* hr); | |
103 | |
104 static jint _cache_probes; | |
105 static jint _cache_hits; | |
106 | |
107 // Indexed by thread X heap region, to minimize thread contention. | |
108 static int** _from_card_cache; | |
109 static size_t _from_card_cache_max_regions; | |
110 static size_t _from_card_cache_mem_size; | |
111 | |
112 public: | |
113 OtherRegionsTable(HeapRegion* hr); | |
114 | |
115 HeapRegion* hr() const { return _hr; } | |
116 | |
117 // For now. Could "expand" some tables in the future, so that this made | |
118 // sense. | |
845
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
119 void add_reference(OopOrNarrowOopStar from, int tid); |
342 | 120 |
845
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
121 void add_reference(OopOrNarrowOopStar from) { |
342 | 122 return add_reference(from, 0); |
123 } | |
124 | |
125 // Removes any entries shown by the given bitmaps to contain only dead | |
126 // objects. | |
127 void scrub(CardTableModRefBS* ctbs, BitMap* region_bm, BitMap* card_bm); | |
128 | |
129 // Not const because it takes a lock. | |
130 size_t occupied() const; | |
131 size_t occ_fine() const; | |
132 size_t occ_coarse() const; | |
133 size_t occ_sparse() const; | |
134 | |
135 static jint n_coarsenings() { return _n_coarsenings; } | |
136 | |
137 // Returns size in bytes. | |
138 // Not const because it takes a lock. | |
139 size_t mem_size() const; | |
140 static size_t static_mem_size(); | |
141 static size_t fl_mem_size(); | |
142 | |
845
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
143 bool contains_reference(OopOrNarrowOopStar from) const; |
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
144 bool contains_reference_locked(OopOrNarrowOopStar from) const; |
342 | 145 |
146 void clear(); | |
147 | |
148 // Specifically clear the from_card_cache. | |
149 void clear_fcc(); | |
150 | |
151 // "from_hr" is being cleared; remove any entries from it. | |
152 void clear_incoming_entry(HeapRegion* from_hr); | |
153 | |
154 // Declare the heap size (in # of regions) to the OtherRegionsTable. | |
155 // (Uses it to initialize from_card_cache). | |
156 static void init_from_card_cache(size_t max_regions); | |
157 | |
158 // Declares that only regions i s.t. 0 <= i < new_n_regs are in use. | |
159 // Make sure any entries for higher regions are invalid. | |
160 static void shrink_from_card_cache(size_t new_n_regs); | |
161 | |
162 static void print_from_card_cache(); | |
163 | |
164 }; | |
165 | |
166 | |
167 class HeapRegionRemSet : public CHeapObj { | |
168 friend class VMStructs; | |
169 friend class HeapRegionRemSetIterator; | |
170 | |
171 public: | |
172 enum Event { | |
173 Event_EvacStart, Event_EvacEnd, Event_RSUpdateEnd | |
174 }; | |
175 | |
176 private: | |
177 G1BlockOffsetSharedArray* _bosa; | |
178 G1BlockOffsetSharedArray* bosa() const { return _bosa; } | |
179 | |
180 OtherRegionsTable _other_regions; | |
181 | |
182 // One set bit for every region that has an entry for this one. | |
183 BitMap _outgoing_region_map; | |
184 | |
185 // Clear entries for the current region in any rem sets named in | |
186 // the _outgoing_region_map. | |
187 void clear_outgoing_entries(); | |
188 | |
189 enum ParIterState { Unclaimed, Claimed, Complete }; | |
1261 | 190 volatile ParIterState _iter_state; |
191 volatile jlong _iter_claimed; | |
342 | 192 |
193 // Unused unless G1RecordHRRSOops is true. | |
194 | |
195 static const int MaxRecorded = 1000000; | |
845
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
196 static OopOrNarrowOopStar* _recorded_oops; |
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
197 static HeapWord** _recorded_cards; |
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
198 static HeapRegion** _recorded_regions; |
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
199 static int _n_recorded; |
342 | 200 |
201 static const int MaxRecordedEvents = 1000; | |
202 static Event* _recorded_events; | |
203 static int* _recorded_event_index; | |
204 static int _n_recorded_events; | |
205 | |
206 static void print_event(outputStream* str, Event evnt); | |
207 | |
208 public: | |
209 HeapRegionRemSet(G1BlockOffsetSharedArray* bosa, | |
210 HeapRegion* hr); | |
211 | |
212 static int num_par_rem_sets(); | |
1261 | 213 static void setup_remset_size(); |
342 | 214 |
215 HeapRegion* hr() const { | |
216 return _other_regions.hr(); | |
217 } | |
218 | |
219 size_t occupied() const { | |
220 return _other_regions.occupied(); | |
221 } | |
222 size_t occ_fine() const { | |
223 return _other_regions.occ_fine(); | |
224 } | |
225 size_t occ_coarse() const { | |
226 return _other_regions.occ_coarse(); | |
227 } | |
228 size_t occ_sparse() const { | |
229 return _other_regions.occ_sparse(); | |
230 } | |
231 | |
232 static jint n_coarsenings() { return OtherRegionsTable::n_coarsenings(); } | |
233 | |
234 /* Used in the sequential case. Returns "true" iff this addition causes | |
235 the size limit to be reached. */ | |
845
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
236 void add_reference(OopOrNarrowOopStar from) { |
342 | 237 _other_regions.add_reference(from); |
238 } | |
239 | |
240 /* Used in the parallel case. Returns "true" iff this addition causes | |
241 the size limit to be reached. */ | |
845
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
242 void add_reference(OopOrNarrowOopStar from, int tid) { |
342 | 243 _other_regions.add_reference(from, tid); |
244 } | |
245 | |
246 // Records the fact that the current region contains an outgoing | |
247 // reference into "to_hr". | |
248 void add_outgoing_reference(HeapRegion* to_hr); | |
249 | |
250 // Removes any entries shown by the given bitmaps to contain only dead | |
251 // objects. | |
252 void scrub(CardTableModRefBS* ctbs, BitMap* region_bm, BitMap* card_bm); | |
253 | |
254 // The region is being reclaimed; clear its remset, and any mention of | |
255 // entries for this region in other remsets. | |
256 void clear(); | |
257 | |
258 // Forget any entries due to pointers from "from_hr". | |
259 void clear_incoming_entry(HeapRegion* from_hr) { | |
260 _other_regions.clear_incoming_entry(from_hr); | |
261 } | |
262 | |
263 #if 0 | |
264 virtual void cleanup() = 0; | |
265 #endif | |
266 | |
267 // Should be called from single-threaded code. | |
268 void init_for_par_iteration(); | |
269 // Attempt to claim the region. Returns true iff this call caused an | |
270 // atomic transition from Unclaimed to Claimed. | |
271 bool claim_iter(); | |
272 // Sets the iteration state to "complete". | |
273 void set_iter_complete(); | |
274 // Returns "true" iff the region's iteration is complete. | |
275 bool iter_is_complete(); | |
276 | |
1261 | 277 // Support for claiming blocks of cards during iteration |
278 void set_iter_claimed(size_t x) { _iter_claimed = (jlong)x; } | |
279 size_t iter_claimed() const { return (size_t)_iter_claimed; } | |
280 // Claim the next block of cards | |
281 size_t iter_claimed_next(size_t step) { | |
282 size_t current, next; | |
283 do { | |
284 current = iter_claimed(); | |
285 next = current + step; | |
286 } while (Atomic::cmpxchg((jlong)next, &_iter_claimed, (jlong)current) != (jlong)current); | |
287 return current; | |
288 } | |
289 | |
342 | 290 // Initialize the given iterator to iterate over this rem set. |
291 void init_iterator(HeapRegionRemSetIterator* iter) const; | |
292 | |
293 #if 0 | |
294 // Apply the "do_card" method to the start address of every card in the | |
295 // rem set. Returns false if some application of the closure aborted. | |
296 virtual bool card_iterate(CardClosure* iter) = 0; | |
297 #endif | |
298 | |
299 // The actual # of bytes this hr_remset takes up. | |
300 size_t mem_size() { | |
301 return _other_regions.mem_size() | |
302 // This correction is necessary because the above includes the second | |
303 // part. | |
304 + sizeof(this) - sizeof(OtherRegionsTable); | |
305 } | |
306 | |
307 // Returns the memory occupancy of all static data structures associated | |
308 // with remembered sets. | |
309 static size_t static_mem_size() { | |
310 return OtherRegionsTable::static_mem_size(); | |
311 } | |
312 | |
313 // Returns the memory occupancy of all free_list data structures associated | |
314 // with remembered sets. | |
315 static size_t fl_mem_size() { | |
316 return OtherRegionsTable::fl_mem_size(); | |
317 } | |
318 | |
845
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
319 bool contains_reference(OopOrNarrowOopStar from) const { |
342 | 320 return _other_regions.contains_reference(from); |
321 } | |
322 void print() const; | |
323 | |
324 // Called during a stop-world phase to perform any deferred cleanups. | |
325 // The second version may be called by parallel threads after then finish | |
326 // collection work. | |
327 static void cleanup(); | |
328 static void par_cleanup(); | |
329 | |
330 // Declare the heap size (in # of regions) to the HeapRegionRemSet(s). | |
331 // (Uses it to initialize from_card_cache). | |
332 static void init_heap(size_t max_regions) { | |
333 OtherRegionsTable::init_from_card_cache(max_regions); | |
334 } | |
335 | |
336 // Declares that only regions i s.t. 0 <= i < new_n_regs are in use. | |
337 static void shrink_heap(size_t new_n_regs) { | |
338 OtherRegionsTable::shrink_from_card_cache(new_n_regs); | |
339 } | |
340 | |
341 #ifndef PRODUCT | |
342 static void print_from_card_cache() { | |
343 OtherRegionsTable::print_from_card_cache(); | |
344 } | |
345 #endif | |
346 | |
845
df6caf649ff7
6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents:
794
diff
changeset
|
347 static void record(HeapRegion* hr, OopOrNarrowOopStar f); |
342 | 348 static void print_recorded(); |
349 static void record_event(Event evnt); | |
350 | |
351 // Run unit tests. | |
352 #ifndef PRODUCT | |
353 static void test(); | |
354 #endif | |
355 | |
356 }; | |
357 | |
358 class HeapRegionRemSetIterator : public CHeapObj { | |
359 | |
360 // The region over which we're iterating. | |
361 const HeapRegionRemSet* _hrrs; | |
362 | |
363 // Local caching of HRRS fields. | |
364 const BitMap* _coarse_map; | |
365 PosParPRT** _fine_grain_regions; | |
366 | |
367 G1BlockOffsetSharedArray* _bosa; | |
368 G1CollectedHeap* _g1h; | |
369 | |
370 // The number yielded since initialization. | |
371 size_t _n_yielded_fine; | |
372 size_t _n_yielded_coarse; | |
373 size_t _n_yielded_sparse; | |
374 | |
375 // If true we're iterating over the coarse table; if false the fine | |
376 // table. | |
377 enum IterState { | |
378 Sparse, | |
379 Fine, | |
380 Coarse | |
381 }; | |
382 IterState _is; | |
383 | |
384 // In both kinds of iteration, heap offset of first card of current | |
385 // region. | |
386 size_t _cur_region_card_offset; | |
387 // Card offset within cur region. | |
388 size_t _cur_region_cur_card; | |
389 | |
390 // Coarse table iteration fields: | |
391 | |
392 // Current region index; | |
393 int _coarse_cur_region_index; | |
394 int _coarse_cur_region_cur_card; | |
395 | |
396 bool coarse_has_next(size_t& card_index); | |
397 | |
398 // Fine table iteration fields: | |
399 | |
400 // Index of bucket-list we're working on. | |
401 int _fine_array_index; | |
402 // Per Region Table we're doing within current bucket list. | |
403 PosParPRT* _fine_cur_prt; | |
404 | |
405 /* SparsePRT::*/ SparsePRTIter _sparse_iter; | |
406 | |
407 void fine_find_next_non_null_prt(); | |
408 | |
409 bool fine_has_next(); | |
410 bool fine_has_next(size_t& card_index); | |
411 | |
412 public: | |
413 // We require an iterator to be initialized before use, so the | |
414 // constructor does little. | |
415 HeapRegionRemSetIterator(); | |
416 | |
417 void initialize(const HeapRegionRemSet* hrrs); | |
418 | |
419 // If there remains one or more cards to be yielded, returns true and | |
420 // sets "card_index" to one of those cards (which is then considered | |
421 // yielded.) Otherwise, returns false (and leaves "card_index" | |
422 // undefined.) | |
423 bool has_next(size_t& card_index); | |
424 | |
425 size_t n_yielded_fine() { return _n_yielded_fine; } | |
426 size_t n_yielded_coarse() { return _n_yielded_coarse; } | |
427 size_t n_yielded_sparse() { return _n_yielded_sparse; } | |
428 size_t n_yielded() { | |
429 return n_yielded_fine() + n_yielded_coarse() + n_yielded_sparse(); | |
430 } | |
431 }; | |
432 | |
433 #if 0 | |
434 class CardClosure: public Closure { | |
435 public: | |
436 virtual void do_card(HeapWord* card_start) = 0; | |
437 }; | |
438 | |
439 #endif |