src/share/vm/gc_implementation/g1/heapRegionRemSet.cpp

changeset 777
37f87013dfd8
child 924
2494ab195856
equal deleted inserted replaced
624:0b27f3512f9e 777:37f87013dfd8
1 /*
2 * Copyright 2001-2007 Sun Microsystems, Inc. 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 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 #include "incls/_precompiled.incl"
26 #include "incls/_heapRegionRemSet.cpp.incl"
27
28 #define HRRS_VERBOSE 0
29
30 #define PRT_COUNT_OCCUPIED 1
31
32 // OtherRegionsTable
33
34 class PerRegionTable: public CHeapObj {
35 friend class OtherRegionsTable;
36 friend class HeapRegionRemSetIterator;
37
38 HeapRegion* _hr;
39 BitMap _bm;
40 #if PRT_COUNT_OCCUPIED
41 jint _occupied;
42 #endif
43 PerRegionTable* _next_free;
44
45 PerRegionTable* next_free() { return _next_free; }
46 void set_next_free(PerRegionTable* prt) { _next_free = prt; }
47
48
49 static PerRegionTable* _free_list;
50
51 #ifdef _MSC_VER
52 // For some reason even though the classes are marked as friend they are unable
53 // to access CardsPerRegion when private/protected. Only the windows c++ compiler
54 // says this Sun CC and linux gcc don't have a problem with access when private
55
56 public:
57
58 #endif // _MSC_VER
59
60 enum SomePrivateConstants {
61 CardsPerRegion = HeapRegion::GrainBytes >> CardTableModRefBS::card_shift
62 };
63
64 protected:
65 // We need access in order to union things into the base table.
66 BitMap* bm() { return &_bm; }
67
68 void recount_occupied() {
69 _occupied = (jint) bm()->count_one_bits();
70 }
71
72 PerRegionTable(HeapRegion* hr) :
73 _hr(hr),
74 #if PRT_COUNT_OCCUPIED
75 _occupied(0),
76 #endif
77 _bm(CardsPerRegion, false /* in-resource-area */)
78 {}
79
80 static void free(PerRegionTable* prt) {
81 while (true) {
82 PerRegionTable* fl = _free_list;
83 prt->set_next_free(fl);
84 PerRegionTable* res =
85 (PerRegionTable*)
86 Atomic::cmpxchg_ptr(prt, &_free_list, fl);
87 if (res == fl) return;
88 }
89 ShouldNotReachHere();
90 }
91
92 static PerRegionTable* alloc(HeapRegion* hr) {
93 PerRegionTable* fl = _free_list;
94 while (fl != NULL) {
95 PerRegionTable* nxt = fl->next_free();
96 PerRegionTable* res =
97 (PerRegionTable*)
98 Atomic::cmpxchg_ptr(nxt, &_free_list, fl);
99 if (res == fl) {
100 fl->init(hr);
101 return fl;
102 } else {
103 fl = _free_list;
104 }
105 }
106 assert(fl == NULL, "Loop condition.");
107 return new PerRegionTable(hr);
108 }
109
110 void add_card_work(short from_card, bool par) {
111 if (!_bm.at(from_card)) {
112 if (par) {
113 if (_bm.par_at_put(from_card, 1)) {
114 #if PRT_COUNT_OCCUPIED
115 Atomic::inc(&_occupied);
116 #endif
117 }
118 } else {
119 _bm.at_put(from_card, 1);
120 #if PRT_COUNT_OCCUPIED
121 _occupied++;
122 #endif
123 }
124 }
125 }
126
127 void add_reference_work(oop* from, bool par) {
128 // Must make this robust in case "from" is not in "_hr", because of
129 // concurrency.
130
131 #if HRRS_VERBOSE
132 gclog_or_tty->print_cr(" PRT::Add_reference_work(" PTR_FORMAT "->" PTR_FORMAT").",
133 from, *from);
134 #endif
135
136 HeapRegion* loc_hr = hr();
137 // If the test below fails, then this table was reused concurrently
138 // with this operation. This is OK, since the old table was coarsened,
139 // and adding a bit to the new table is never incorrect.
140 if (loc_hr->is_in_reserved(from)) {
141 size_t hw_offset = pointer_delta((HeapWord*)from, loc_hr->bottom());
142 size_t from_card =
143 hw_offset >>
144 (CardTableModRefBS::card_shift - LogHeapWordSize);
145
146 add_card_work((short) from_card, par);
147 }
148 }
149
150 public:
151
152 HeapRegion* hr() const { return _hr; }
153
154 #if PRT_COUNT_OCCUPIED
155 jint occupied() const {
156 // Overkill, but if we ever need it...
157 // guarantee(_occupied == _bm.count_one_bits(), "Check");
158 return _occupied;
159 }
160 #else
161 jint occupied() const {
162 return _bm.count_one_bits();
163 }
164 #endif
165
166 void init(HeapRegion* hr) {
167 _hr = hr;
168 #if PRT_COUNT_OCCUPIED
169 _occupied = 0;
170 #endif
171 _bm.clear();
172 }
173
174 void add_reference(oop* from) {
175 add_reference_work(from, /*parallel*/ true);
176 }
177
178 void seq_add_reference(oop* from) {
179 add_reference_work(from, /*parallel*/ false);
180 }
181
182 void scrub(CardTableModRefBS* ctbs, BitMap* card_bm) {
183 HeapWord* hr_bot = hr()->bottom();
184 int hr_first_card_index = ctbs->index_for(hr_bot);
185 bm()->set_intersection_at_offset(*card_bm, hr_first_card_index);
186 #if PRT_COUNT_OCCUPIED
187 recount_occupied();
188 #endif
189 }
190
191 void add_card(short from_card_index) {
192 add_card_work(from_card_index, /*parallel*/ true);
193 }
194
195 void seq_add_card(short from_card_index) {
196 add_card_work(from_card_index, /*parallel*/ false);
197 }
198
199 // (Destructively) union the bitmap of the current table into the given
200 // bitmap (which is assumed to be of the same size.)
201 void union_bitmap_into(BitMap* bm) {
202 bm->set_union(_bm);
203 }
204
205 // Mem size in bytes.
206 size_t mem_size() const {
207 return sizeof(this) + _bm.size_in_words() * HeapWordSize;
208 }
209
210 static size_t fl_mem_size() {
211 PerRegionTable* cur = _free_list;
212 size_t res = 0;
213 while (cur != NULL) {
214 res += sizeof(PerRegionTable);
215 cur = cur->next_free();
216 }
217 return res;
218 }
219
220 // Requires "from" to be in "hr()".
221 bool contains_reference(oop* from) const {
222 assert(hr()->is_in_reserved(from), "Precondition.");
223 size_t card_ind = pointer_delta(from, hr()->bottom(),
224 CardTableModRefBS::card_size);
225 return _bm.at(card_ind);
226 }
227 };
228
229 PerRegionTable* PerRegionTable::_free_list = NULL;
230
231
232 #define COUNT_PAR_EXPANDS 0
233
234 #if COUNT_PAR_EXPANDS
235 static jint n_par_expands = 0;
236 static jint n_par_contracts = 0;
237 static jint par_expand_list_len = 0;
238 static jint max_par_expand_list_len = 0;
239
240 static void print_par_expand() {
241 Atomic::inc(&n_par_expands);
242 Atomic::inc(&par_expand_list_len);
243 if (par_expand_list_len > max_par_expand_list_len) {
244 max_par_expand_list_len = par_expand_list_len;
245 }
246 if ((n_par_expands % 10) == 0) {
247 gclog_or_tty->print_cr("\n\n%d par expands: %d contracts, "
248 "len = %d, max_len = %d\n.",
249 n_par_expands, n_par_contracts, par_expand_list_len,
250 max_par_expand_list_len);
251 }
252 }
253 #endif
254
255 class PosParPRT: public PerRegionTable {
256 PerRegionTable** _par_tables;
257
258 enum SomePrivateConstants {
259 ReserveParTableExpansion = 1
260 };
261
262 void par_expand() {
263 int n = HeapRegionRemSet::num_par_rem_sets()-1;
264 if (n <= 0) return;
265 if (_par_tables == NULL) {
266 PerRegionTable* res =
267 (PerRegionTable*)
268 Atomic::cmpxchg_ptr((PerRegionTable*)ReserveParTableExpansion,
269 &_par_tables, NULL);
270 if (res != NULL) return;
271 // Otherwise, we reserved the right to do the expansion.
272
273 PerRegionTable** ptables = NEW_C_HEAP_ARRAY(PerRegionTable*, n);
274 for (int i = 0; i < n; i++) {
275 PerRegionTable* ptable = PerRegionTable::alloc(hr());
276 ptables[i] = ptable;
277 }
278 // Here we do not need an atomic.
279 _par_tables = ptables;
280 #if COUNT_PAR_EXPANDS
281 print_par_expand();
282 #endif
283 // We must put this table on the expanded list.
284 PosParPRT* exp_head = _par_expanded_list;
285 while (true) {
286 set_next_par_expanded(exp_head);
287 PosParPRT* res =
288 (PosParPRT*)
289 Atomic::cmpxchg_ptr(this, &_par_expanded_list, exp_head);
290 if (res == exp_head) return;
291 // Otherwise.
292 exp_head = res;
293 }
294 ShouldNotReachHere();
295 }
296 }
297
298 void par_contract() {
299 assert(_par_tables != NULL, "Precondition.");
300 int n = HeapRegionRemSet::num_par_rem_sets()-1;
301 for (int i = 0; i < n; i++) {
302 _par_tables[i]->union_bitmap_into(bm());
303 PerRegionTable::free(_par_tables[i]);
304 _par_tables[i] = NULL;
305 }
306 #if PRT_COUNT_OCCUPIED
307 // We must recount the "occupied."
308 recount_occupied();
309 #endif
310 FREE_C_HEAP_ARRAY(PerRegionTable*, _par_tables);
311 _par_tables = NULL;
312 #if COUNT_PAR_EXPANDS
313 Atomic::inc(&n_par_contracts);
314 Atomic::dec(&par_expand_list_len);
315 #endif
316 }
317
318 static PerRegionTable** _par_table_fl;
319
320 PosParPRT* _next;
321
322 static PosParPRT* _free_list;
323
324 PerRegionTable** par_tables() const {
325 assert(uintptr_t(NULL) == 0, "Assumption.");
326 if (uintptr_t(_par_tables) <= ReserveParTableExpansion)
327 return NULL;
328 else
329 return _par_tables;
330 }
331
332 PosParPRT* _next_par_expanded;
333 PosParPRT* next_par_expanded() { return _next_par_expanded; }
334 void set_next_par_expanded(PosParPRT* ppprt) { _next_par_expanded = ppprt; }
335 static PosParPRT* _par_expanded_list;
336
337 public:
338
339 PosParPRT(HeapRegion* hr) : PerRegionTable(hr), _par_tables(NULL) {}
340
341 jint occupied() const {
342 jint res = PerRegionTable::occupied();
343 if (par_tables() != NULL) {
344 for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets()-1; i++) {
345 res += par_tables()[i]->occupied();
346 }
347 }
348 return res;
349 }
350
351 void init(HeapRegion* hr) {
352 PerRegionTable::init(hr);
353 _next = NULL;
354 if (par_tables() != NULL) {
355 for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets()-1; i++) {
356 par_tables()[i]->init(hr);
357 }
358 }
359 }
360
361 static void free(PosParPRT* prt) {
362 while (true) {
363 PosParPRT* fl = _free_list;
364 prt->set_next(fl);
365 PosParPRT* res =
366 (PosParPRT*)
367 Atomic::cmpxchg_ptr(prt, &_free_list, fl);
368 if (res == fl) return;
369 }
370 ShouldNotReachHere();
371 }
372
373 static PosParPRT* alloc(HeapRegion* hr) {
374 PosParPRT* fl = _free_list;
375 while (fl != NULL) {
376 PosParPRT* nxt = fl->next();
377 PosParPRT* res =
378 (PosParPRT*)
379 Atomic::cmpxchg_ptr(nxt, &_free_list, fl);
380 if (res == fl) {
381 fl->init(hr);
382 return fl;
383 } else {
384 fl = _free_list;
385 }
386 }
387 assert(fl == NULL, "Loop condition.");
388 return new PosParPRT(hr);
389 }
390
391 PosParPRT* next() const { return _next; }
392 void set_next(PosParPRT* nxt) { _next = nxt; }
393 PosParPRT** next_addr() { return &_next; }
394
395 void add_reference(oop* from, int tid) {
396 // Expand if necessary.
397 PerRegionTable** pt = par_tables();
398 if (par_tables() == NULL && tid > 0 && hr()->is_gc_alloc_region()) {
399 par_expand();
400 pt = par_tables();
401 }
402 if (pt != NULL) {
403 // We always have to assume that mods to table 0 are in parallel,
404 // because of the claiming scheme in parallel expansion. A thread
405 // with tid != 0 that finds the table to be NULL, but doesn't succeed
406 // in claiming the right of expanding it, will end up in the else
407 // clause of the above if test. That thread could be delayed, and a
408 // thread 0 add reference could see the table expanded, and come
409 // here. Both threads would be adding in parallel. But we get to
410 // not use atomics for tids > 0.
411 if (tid == 0) {
412 PerRegionTable::add_reference(from);
413 } else {
414 pt[tid-1]->seq_add_reference(from);
415 }
416 } else {
417 // Not expanded -- add to the base table.
418 PerRegionTable::add_reference(from);
419 }
420 }
421
422 void scrub(CardTableModRefBS* ctbs, BitMap* card_bm) {
423 assert(_par_tables == NULL, "Precondition");
424 PerRegionTable::scrub(ctbs, card_bm);
425 }
426
427 size_t mem_size() const {
428 size_t res =
429 PerRegionTable::mem_size() + sizeof(this) - sizeof(PerRegionTable);
430 if (_par_tables != NULL) {
431 for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets()-1; i++) {
432 res += _par_tables[i]->mem_size();
433 }
434 }
435 return res;
436 }
437
438 static size_t fl_mem_size() {
439 PosParPRT* cur = _free_list;
440 size_t res = 0;
441 while (cur != NULL) {
442 res += sizeof(PosParPRT);
443 cur = cur->next();
444 }
445 return res;
446 }
447
448 bool contains_reference(oop* from) const {
449 if (PerRegionTable::contains_reference(from)) return true;
450 if (_par_tables != NULL) {
451 for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets()-1; i++) {
452 if (_par_tables[i]->contains_reference(from)) return true;
453 }
454 }
455 return false;
456 }
457
458 static void par_contract_all();
459
460 };
461
462 void PosParPRT::par_contract_all() {
463 PosParPRT* hd = _par_expanded_list;
464 while (hd != NULL) {
465 PosParPRT* nxt = hd->next_par_expanded();
466 PosParPRT* res =
467 (PosParPRT*)
468 Atomic::cmpxchg_ptr(nxt, &_par_expanded_list, hd);
469 if (res == hd) {
470 // We claimed the right to contract this table.
471 hd->set_next_par_expanded(NULL);
472 hd->par_contract();
473 hd = _par_expanded_list;
474 } else {
475 hd = res;
476 }
477 }
478 }
479
480 PosParPRT* PosParPRT::_free_list = NULL;
481 PosParPRT* PosParPRT::_par_expanded_list = NULL;
482
483 jint OtherRegionsTable::_cache_probes = 0;
484 jint OtherRegionsTable::_cache_hits = 0;
485
486 size_t OtherRegionsTable::_max_fine_entries = 0;
487 size_t OtherRegionsTable::_mod_max_fine_entries_mask = 0;
488 #if SAMPLE_FOR_EVICTION
489 size_t OtherRegionsTable::_fine_eviction_stride = 0;
490 size_t OtherRegionsTable::_fine_eviction_sample_size = 0;
491 #endif
492
493 OtherRegionsTable::OtherRegionsTable(HeapRegion* hr) :
494 _g1h(G1CollectedHeap::heap()),
495 _m(Mutex::leaf, "An OtherRegionsTable lock", true),
496 _hr(hr),
497 _coarse_map(G1CollectedHeap::heap()->max_regions(),
498 false /* in-resource-area */),
499 _fine_grain_regions(NULL),
500 _n_fine_entries(0), _n_coarse_entries(0),
501 #if SAMPLE_FOR_EVICTION
502 _fine_eviction_start(0),
503 #endif
504 _sparse_table(hr)
505 {
506 typedef PosParPRT* PosParPRTPtr;
507 if (_max_fine_entries == 0) {
508 assert(_mod_max_fine_entries_mask == 0, "Both or none.");
509 _max_fine_entries = (1 << G1LogRSRegionEntries);
510 _mod_max_fine_entries_mask = _max_fine_entries - 1;
511 #if SAMPLE_FOR_EVICTION
512 assert(_fine_eviction_sample_size == 0
513 && _fine_eviction_stride == 0, "All init at same time.");
514 _fine_eviction_sample_size = MAX2((size_t)4, (size_t)G1LogRSRegionEntries);
515 _fine_eviction_stride = _max_fine_entries / _fine_eviction_sample_size;
516 #endif
517 }
518 _fine_grain_regions = new PosParPRTPtr[_max_fine_entries];
519 if (_fine_grain_regions == NULL)
520 vm_exit_out_of_memory(sizeof(void*)*_max_fine_entries,
521 "Failed to allocate _fine_grain_entries.");
522 for (size_t i = 0; i < _max_fine_entries; i++) {
523 _fine_grain_regions[i] = NULL;
524 }
525 }
526
527 int** OtherRegionsTable::_from_card_cache = NULL;
528 size_t OtherRegionsTable::_from_card_cache_max_regions = 0;
529 size_t OtherRegionsTable::_from_card_cache_mem_size = 0;
530
531 void OtherRegionsTable::init_from_card_cache(size_t max_regions) {
532 _from_card_cache_max_regions = max_regions;
533
534 int n_par_rs = HeapRegionRemSet::num_par_rem_sets();
535 _from_card_cache = NEW_C_HEAP_ARRAY(int*, n_par_rs);
536 for (int i = 0; i < n_par_rs; i++) {
537 _from_card_cache[i] = NEW_C_HEAP_ARRAY(int, max_regions);
538 for (size_t j = 0; j < max_regions; j++) {
539 _from_card_cache[i][j] = -1; // An invalid value.
540 }
541 }
542 _from_card_cache_mem_size = n_par_rs * max_regions * sizeof(int);
543 }
544
545 void OtherRegionsTable::shrink_from_card_cache(size_t new_n_regs) {
546 for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets(); i++) {
547 assert(new_n_regs <= _from_card_cache_max_regions, "Must be within max.");
548 for (size_t j = new_n_regs; j < _from_card_cache_max_regions; j++) {
549 _from_card_cache[i][j] = -1; // An invalid value.
550 }
551 }
552 }
553
554 #ifndef PRODUCT
555 void OtherRegionsTable::print_from_card_cache() {
556 for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets(); i++) {
557 for (size_t j = 0; j < _from_card_cache_max_regions; j++) {
558 gclog_or_tty->print_cr("_from_card_cache[%d][%d] = %d.",
559 i, j, _from_card_cache[i][j]);
560 }
561 }
562 }
563 #endif
564
565 void OtherRegionsTable::add_reference(oop* from, int tid) {
566 size_t cur_hrs_ind = hr()->hrs_index();
567
568 #if HRRS_VERBOSE
569 gclog_or_tty->print_cr("ORT::add_reference_work(" PTR_FORMAT "->" PTR_FORMAT ").",
570 from, *from);
571 #endif
572
573 int from_card = (int)(uintptr_t(from) >> CardTableModRefBS::card_shift);
574
575 #if HRRS_VERBOSE
576 gclog_or_tty->print_cr("Table for [" PTR_FORMAT "...): card %d (cache = %d)",
577 hr()->bottom(), from_card,
578 _from_card_cache[tid][cur_hrs_ind]);
579 #endif
580
581 #define COUNT_CACHE 0
582 #if COUNT_CACHE
583 jint p = Atomic::add(1, &_cache_probes);
584 if ((p % 10000) == 0) {
585 jint hits = _cache_hits;
586 gclog_or_tty->print_cr("%d/%d = %5.2f%% RS cache hits.",
587 _cache_hits, p, 100.0* (float)hits/(float)p);
588 }
589 #endif
590 if (from_card == _from_card_cache[tid][cur_hrs_ind]) {
591 #if HRRS_VERBOSE
592 gclog_or_tty->print_cr(" from-card cache hit.");
593 #endif
594 #if COUNT_CACHE
595 Atomic::inc(&_cache_hits);
596 #endif
597 assert(contains_reference(from), "We just added it!");
598 return;
599 } else {
600 _from_card_cache[tid][cur_hrs_ind] = from_card;
601 }
602
603 // Note that this may be a continued H region.
604 HeapRegion* from_hr = _g1h->heap_region_containing_raw(from);
605 size_t from_hrs_ind = (size_t)from_hr->hrs_index();
606
607 // If the region is already coarsened, return.
608 if (_coarse_map.at(from_hrs_ind)) {
609 #if HRRS_VERBOSE
610 gclog_or_tty->print_cr(" coarse map hit.");
611 #endif
612 assert(contains_reference(from), "We just added it!");
613 return;
614 }
615
616 // Otherwise find a per-region table to add it to.
617 size_t ind = from_hrs_ind & _mod_max_fine_entries_mask;
618 PosParPRT* prt = find_region_table(ind, from_hr);
619 if (prt == NULL) {
620 MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag);
621 // Confirm that it's really not there...
622 prt = find_region_table(ind, from_hr);
623 if (prt == NULL) {
624
625 uintptr_t from_hr_bot_card_index =
626 uintptr_t(from_hr->bottom())
627 >> CardTableModRefBS::card_shift;
628 int card_index = from_card - from_hr_bot_card_index;
629 assert(0 <= card_index && card_index < PosParPRT::CardsPerRegion,
630 "Must be in range.");
631 if (G1HRRSUseSparseTable &&
632 _sparse_table.add_card((short) from_hrs_ind, card_index)) {
633 if (G1RecordHRRSOops) {
634 HeapRegionRemSet::record(hr(), from);
635 #if HRRS_VERBOSE
636 gclog_or_tty->print(" Added card " PTR_FORMAT " to region "
637 "[" PTR_FORMAT "...) for ref " PTR_FORMAT ".\n",
638 align_size_down(uintptr_t(from),
639 CardTableModRefBS::card_size),
640 hr()->bottom(), from);
641 #endif
642 }
643 #if HRRS_VERBOSE
644 gclog_or_tty->print_cr(" added card to sparse table.");
645 #endif
646 assert(contains_reference_locked(from), "We just added it!");
647 return;
648 } else {
649 #if HRRS_VERBOSE
650 gclog_or_tty->print_cr(" [tid %d] sparse table entry "
651 "overflow(f: %d, t: %d)",
652 tid, from_hrs_ind, cur_hrs_ind);
653 #endif
654 }
655
656 // Otherwise, transfer from sparse to fine-grain.
657 short cards[SparsePRTEntry::CardsPerEntry];
658 if (G1HRRSUseSparseTable) {
659 bool res = _sparse_table.get_cards((short) from_hrs_ind, &cards[0]);
660 assert(res, "There should have been an entry");
661 }
662
663 if (_n_fine_entries == _max_fine_entries) {
664 prt = delete_region_table();
665 } else {
666 prt = PosParPRT::alloc(from_hr);
667 }
668 prt->init(from_hr);
669 // Record the outgoing pointer in the from_region's outgoing bitmap.
670 from_hr->rem_set()->add_outgoing_reference(hr());
671
672 PosParPRT* first_prt = _fine_grain_regions[ind];
673 prt->set_next(first_prt); // XXX Maybe move to init?
674 _fine_grain_regions[ind] = prt;
675 _n_fine_entries++;
676
677 // Add in the cards from the sparse table.
678 if (G1HRRSUseSparseTable) {
679 for (int i = 0; i < SparsePRTEntry::CardsPerEntry; i++) {
680 short c = cards[i];
681 if (c != SparsePRTEntry::NullEntry) {
682 prt->add_card(c);
683 }
684 }
685 // Now we can delete the sparse entry.
686 bool res = _sparse_table.delete_entry((short) from_hrs_ind);
687 assert(res, "It should have been there.");
688 }
689 }
690 assert(prt != NULL && prt->hr() == from_hr, "consequence");
691 }
692 // Note that we can't assert "prt->hr() == from_hr", because of the
693 // possibility of concurrent reuse. But see head comment of
694 // OtherRegionsTable for why this is OK.
695 assert(prt != NULL, "Inv");
696
697 prt->add_reference(from, tid);
698 if (G1RecordHRRSOops) {
699 HeapRegionRemSet::record(hr(), from);
700 #if HRRS_VERBOSE
701 gclog_or_tty->print("Added card " PTR_FORMAT " to region "
702 "[" PTR_FORMAT "...) for ref " PTR_FORMAT ".\n",
703 align_size_down(uintptr_t(from),
704 CardTableModRefBS::card_size),
705 hr()->bottom(), from);
706 #endif
707 }
708 assert(contains_reference(from), "We just added it!");
709 }
710
711 PosParPRT*
712 OtherRegionsTable::find_region_table(size_t ind, HeapRegion* hr) const {
713 assert(0 <= ind && ind < _max_fine_entries, "Preconditions.");
714 PosParPRT* prt = _fine_grain_regions[ind];
715 while (prt != NULL && prt->hr() != hr) {
716 prt = prt->next();
717 }
718 // Loop postcondition is the method postcondition.
719 return prt;
720 }
721
722
723 #define DRT_CENSUS 0
724
725 #if DRT_CENSUS
726 static const int HistoSize = 6;
727 static int global_histo[HistoSize] = { 0, 0, 0, 0, 0, 0 };
728 static int coarsenings = 0;
729 static int occ_sum = 0;
730 #endif
731
732 jint OtherRegionsTable::_n_coarsenings = 0;
733
734 PosParPRT* OtherRegionsTable::delete_region_table() {
735 #if DRT_CENSUS
736 int histo[HistoSize] = { 0, 0, 0, 0, 0, 0 };
737 const int histo_limits[] = { 1, 4, 16, 64, 256, 2048 };
738 #endif
739
740 assert(_m.owned_by_self(), "Precondition");
741 assert(_n_fine_entries == _max_fine_entries, "Precondition");
742 PosParPRT* max = NULL;
743 jint max_occ = 0;
744 PosParPRT** max_prev;
745 size_t max_ind;
746
747 #if SAMPLE_FOR_EVICTION
748 size_t i = _fine_eviction_start;
749 for (size_t k = 0; k < _fine_eviction_sample_size; k++) {
750 size_t ii = i;
751 // Make sure we get a non-NULL sample.
752 while (_fine_grain_regions[ii] == NULL) {
753 ii++;
754 if (ii == _max_fine_entries) ii = 0;
755 guarantee(ii != i, "We must find one.");
756 }
757 PosParPRT** prev = &_fine_grain_regions[ii];
758 PosParPRT* cur = *prev;
759 while (cur != NULL) {
760 jint cur_occ = cur->occupied();
761 if (max == NULL || cur_occ > max_occ) {
762 max = cur;
763 max_prev = prev;
764 max_ind = i;
765 max_occ = cur_occ;
766 }
767 prev = cur->next_addr();
768 cur = cur->next();
769 }
770 i = i + _fine_eviction_stride;
771 if (i >= _n_fine_entries) i = i - _n_fine_entries;
772 }
773 _fine_eviction_start++;
774 if (_fine_eviction_start >= _n_fine_entries)
775 _fine_eviction_start -= _n_fine_entries;
776 #else
777 for (int i = 0; i < _max_fine_entries; i++) {
778 PosParPRT** prev = &_fine_grain_regions[i];
779 PosParPRT* cur = *prev;
780 while (cur != NULL) {
781 jint cur_occ = cur->occupied();
782 #if DRT_CENSUS
783 for (int k = 0; k < HistoSize; k++) {
784 if (cur_occ <= histo_limits[k]) {
785 histo[k]++; global_histo[k]++; break;
786 }
787 }
788 #endif
789 if (max == NULL || cur_occ > max_occ) {
790 max = cur;
791 max_prev = prev;
792 max_ind = i;
793 max_occ = cur_occ;
794 }
795 prev = cur->next_addr();
796 cur = cur->next();
797 }
798 }
799 #endif
800 // XXX
801 guarantee(max != NULL, "Since _n_fine_entries > 0");
802 #if DRT_CENSUS
803 gclog_or_tty->print_cr("In a coarsening: histo of occs:");
804 for (int k = 0; k < HistoSize; k++) {
805 gclog_or_tty->print_cr(" <= %4d: %5d.", histo_limits[k], histo[k]);
806 }
807 coarsenings++;
808 occ_sum += max_occ;
809 if ((coarsenings % 100) == 0) {
810 gclog_or_tty->print_cr("\ncoarsenings = %d; global summary:", coarsenings);
811 for (int k = 0; k < HistoSize; k++) {
812 gclog_or_tty->print_cr(" <= %4d: %5d.", histo_limits[k], global_histo[k]);
813 }
814 gclog_or_tty->print_cr("Avg occ of deleted region = %6.2f.",
815 (float)occ_sum/(float)coarsenings);
816 }
817 #endif
818
819 // Set the corresponding coarse bit.
820 int max_hrs_index = max->hr()->hrs_index();
821 if (!_coarse_map.at(max_hrs_index)) {
822 _coarse_map.at_put(max_hrs_index, true);
823 _n_coarse_entries++;
824 #if 0
825 gclog_or_tty->print("Coarsened entry in region [" PTR_FORMAT "...] "
826 "for region [" PTR_FORMAT "...] (%d coarse entries).\n",
827 hr()->bottom(),
828 max->hr()->bottom(),
829 _n_coarse_entries);
830 #endif
831 }
832
833 // Unsplice.
834 *max_prev = max->next();
835 Atomic::inc(&_n_coarsenings);
836 _n_fine_entries--;
837 return max;
838 }
839
840
841 // At present, this must be called stop-world single-threaded.
842 void OtherRegionsTable::scrub(CardTableModRefBS* ctbs,
843 BitMap* region_bm, BitMap* card_bm) {
844 // First eliminated garbage regions from the coarse map.
845 if (G1RSScrubVerbose)
846 gclog_or_tty->print_cr("Scrubbing region %d:", hr()->hrs_index());
847
848 assert(_coarse_map.size() == region_bm->size(), "Precondition");
849 if (G1RSScrubVerbose)
850 gclog_or_tty->print(" Coarse map: before = %d...", _n_coarse_entries);
851 _coarse_map.set_intersection(*region_bm);
852 _n_coarse_entries = _coarse_map.count_one_bits();
853 if (G1RSScrubVerbose)
854 gclog_or_tty->print_cr(" after = %d.", _n_coarse_entries);
855
856 // Now do the fine-grained maps.
857 for (size_t i = 0; i < _max_fine_entries; i++) {
858 PosParPRT* cur = _fine_grain_regions[i];
859 PosParPRT** prev = &_fine_grain_regions[i];
860 while (cur != NULL) {
861 PosParPRT* nxt = cur->next();
862 // If the entire region is dead, eliminate.
863 if (G1RSScrubVerbose)
864 gclog_or_tty->print_cr(" For other region %d:", cur->hr()->hrs_index());
865 if (!region_bm->at(cur->hr()->hrs_index())) {
866 *prev = nxt;
867 cur->set_next(NULL);
868 _n_fine_entries--;
869 if (G1RSScrubVerbose)
870 gclog_or_tty->print_cr(" deleted via region map.");
871 PosParPRT::free(cur);
872 } else {
873 // Do fine-grain elimination.
874 if (G1RSScrubVerbose)
875 gclog_or_tty->print(" occ: before = %4d.", cur->occupied());
876 cur->scrub(ctbs, card_bm);
877 if (G1RSScrubVerbose)
878 gclog_or_tty->print_cr(" after = %4d.", cur->occupied());
879 // Did that empty the table completely?
880 if (cur->occupied() == 0) {
881 *prev = nxt;
882 cur->set_next(NULL);
883 _n_fine_entries--;
884 PosParPRT::free(cur);
885 } else {
886 prev = cur->next_addr();
887 }
888 }
889 cur = nxt;
890 }
891 }
892 // Since we may have deleted a from_card_cache entry from the RS, clear
893 // the FCC.
894 clear_fcc();
895 }
896
897
898 size_t OtherRegionsTable::occupied() const {
899 // Cast away const in this case.
900 MutexLockerEx x((Mutex*)&_m, Mutex::_no_safepoint_check_flag);
901 size_t sum = occ_fine();
902 sum += occ_sparse();
903 sum += occ_coarse();
904 return sum;
905 }
906
907 size_t OtherRegionsTable::occ_fine() const {
908 size_t sum = 0;
909 for (size_t i = 0; i < _max_fine_entries; i++) {
910 PosParPRT* cur = _fine_grain_regions[i];
911 while (cur != NULL) {
912 sum += cur->occupied();
913 cur = cur->next();
914 }
915 }
916 return sum;
917 }
918
919 size_t OtherRegionsTable::occ_coarse() const {
920 return (_n_coarse_entries * PosParPRT::CardsPerRegion);
921 }
922
923 size_t OtherRegionsTable::occ_sparse() const {
924 return _sparse_table.occupied();
925 }
926
927 size_t OtherRegionsTable::mem_size() const {
928 // Cast away const in this case.
929 MutexLockerEx x((Mutex*)&_m, Mutex::_no_safepoint_check_flag);
930 size_t sum = 0;
931 for (size_t i = 0; i < _max_fine_entries; i++) {
932 PosParPRT* cur = _fine_grain_regions[i];
933 while (cur != NULL) {
934 sum += cur->mem_size();
935 cur = cur->next();
936 }
937 }
938 sum += (sizeof(PosParPRT*) * _max_fine_entries);
939 sum += (_coarse_map.size_in_words() * HeapWordSize);
940 sum += (_sparse_table.mem_size());
941 sum += sizeof(*this) - sizeof(_sparse_table); // Avoid double counting above.
942 return sum;
943 }
944
945 size_t OtherRegionsTable::static_mem_size() {
946 return _from_card_cache_mem_size;
947 }
948
949 size_t OtherRegionsTable::fl_mem_size() {
950 return PerRegionTable::fl_mem_size() + PosParPRT::fl_mem_size();
951 }
952
953 void OtherRegionsTable::clear_fcc() {
954 for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets(); i++) {
955 _from_card_cache[i][hr()->hrs_index()] = -1;
956 }
957 }
958
959 void OtherRegionsTable::clear() {
960 MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag);
961 for (size_t i = 0; i < _max_fine_entries; i++) {
962 PosParPRT* cur = _fine_grain_regions[i];
963 while (cur != NULL) {
964 PosParPRT* nxt = cur->next();
965 PosParPRT::free(cur);
966 cur = nxt;
967 }
968 _fine_grain_regions[i] = NULL;
969 }
970 _sparse_table.clear();
971 _coarse_map.clear();
972 _n_fine_entries = 0;
973 _n_coarse_entries = 0;
974
975 clear_fcc();
976 }
977
978 void OtherRegionsTable::clear_incoming_entry(HeapRegion* from_hr) {
979 MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag);
980 size_t hrs_ind = (size_t)from_hr->hrs_index();
981 size_t ind = hrs_ind & _mod_max_fine_entries_mask;
982 if (del_single_region_table(ind, from_hr)) {
983 assert(!_coarse_map.at(hrs_ind), "Inv");
984 } else {
985 _coarse_map.par_at_put(hrs_ind, 0);
986 }
987 // Check to see if any of the fcc entries come from here.
988 int hr_ind = hr()->hrs_index();
989 for (int tid = 0; tid < HeapRegionRemSet::num_par_rem_sets(); tid++) {
990 int fcc_ent = _from_card_cache[tid][hr_ind];
991 if (fcc_ent != -1) {
992 HeapWord* card_addr = (HeapWord*)
993 (uintptr_t(fcc_ent) << CardTableModRefBS::card_shift);
994 if (hr()->is_in_reserved(card_addr)) {
995 // Clear the from card cache.
996 _from_card_cache[tid][hr_ind] = -1;
997 }
998 }
999 }
1000 }
1001
1002 bool OtherRegionsTable::del_single_region_table(size_t ind,
1003 HeapRegion* hr) {
1004 assert(0 <= ind && ind < _max_fine_entries, "Preconditions.");
1005 PosParPRT** prev_addr = &_fine_grain_regions[ind];
1006 PosParPRT* prt = *prev_addr;
1007 while (prt != NULL && prt->hr() != hr) {
1008 prev_addr = prt->next_addr();
1009 prt = prt->next();
1010 }
1011 if (prt != NULL) {
1012 assert(prt->hr() == hr, "Loop postcondition.");
1013 *prev_addr = prt->next();
1014 PosParPRT::free(prt);
1015 _n_fine_entries--;
1016 return true;
1017 } else {
1018 return false;
1019 }
1020 }
1021
1022 bool OtherRegionsTable::contains_reference(oop* from) const {
1023 // Cast away const in this case.
1024 MutexLockerEx x((Mutex*)&_m, Mutex::_no_safepoint_check_flag);
1025 return contains_reference_locked(from);
1026 }
1027
1028 bool OtherRegionsTable::contains_reference_locked(oop* from) const {
1029 HeapRegion* hr = _g1h->heap_region_containing_raw(from);
1030 if (hr == NULL) return false;
1031 size_t hr_ind = hr->hrs_index();
1032 // Is this region in the coarse map?
1033 if (_coarse_map.at(hr_ind)) return true;
1034
1035 PosParPRT* prt = find_region_table(hr_ind & _mod_max_fine_entries_mask,
1036 hr);
1037 if (prt != NULL) {
1038 return prt->contains_reference(from);
1039
1040 } else {
1041 uintptr_t from_card =
1042 (uintptr_t(from) >> CardTableModRefBS::card_shift);
1043 uintptr_t hr_bot_card_index =
1044 uintptr_t(hr->bottom()) >> CardTableModRefBS::card_shift;
1045 assert(from_card >= hr_bot_card_index, "Inv");
1046 int card_index = from_card - hr_bot_card_index;
1047 return _sparse_table.contains_card((short)hr_ind, card_index);
1048 }
1049
1050
1051 }
1052
1053
1054 bool HeapRegionRemSet::_par_traversal = false;
1055
1056 void HeapRegionRemSet::set_par_traversal(bool b) {
1057 assert(_par_traversal != b, "Proper alternation...");
1058 _par_traversal = b;
1059 }
1060
1061 int HeapRegionRemSet::num_par_rem_sets() {
1062 // We always have at least two, so that a mutator thread can claim an
1063 // id and add to a rem set.
1064 return (int) MAX2(ParallelGCThreads, (size_t)2);
1065 }
1066
1067 HeapRegionRemSet::HeapRegionRemSet(G1BlockOffsetSharedArray* bosa,
1068 HeapRegion* hr)
1069 : _bosa(bosa), _other_regions(hr),
1070 _outgoing_region_map(G1CollectedHeap::heap()->max_regions(),
1071 false /* in-resource-area */),
1072 _iter_state(Unclaimed)
1073 {}
1074
1075
1076 void HeapRegionRemSet::init_for_par_iteration() {
1077 _iter_state = Unclaimed;
1078 }
1079
1080 bool HeapRegionRemSet::claim_iter() {
1081 if (_iter_state != Unclaimed) return false;
1082 jint res = Atomic::cmpxchg(Claimed, (jint*)(&_iter_state), Unclaimed);
1083 return (res == Unclaimed);
1084 }
1085
1086 void HeapRegionRemSet::set_iter_complete() {
1087 _iter_state = Complete;
1088 }
1089
1090 bool HeapRegionRemSet::iter_is_complete() {
1091 return _iter_state == Complete;
1092 }
1093
1094
1095 void HeapRegionRemSet::init_iterator(HeapRegionRemSetIterator* iter) const {
1096 iter->initialize(this);
1097 }
1098
1099 #ifndef PRODUCT
1100 void HeapRegionRemSet::print() const {
1101 HeapRegionRemSetIterator iter;
1102 init_iterator(&iter);
1103 size_t card_index;
1104 while (iter.has_next(card_index)) {
1105 HeapWord* card_start =
1106 G1CollectedHeap::heap()->bot_shared()->address_for_index(card_index);
1107 gclog_or_tty->print_cr(" Card " PTR_FORMAT ".", card_start);
1108 }
1109 // XXX
1110 if (iter.n_yielded() != occupied()) {
1111 gclog_or_tty->print_cr("Yielded disagrees with occupied:");
1112 gclog_or_tty->print_cr(" %6d yielded (%6d coarse, %6d fine).",
1113 iter.n_yielded(),
1114 iter.n_yielded_coarse(), iter.n_yielded_fine());
1115 gclog_or_tty->print_cr(" %6d occ (%6d coarse, %6d fine).",
1116 occupied(), occ_coarse(), occ_fine());
1117 }
1118 guarantee(iter.n_yielded() == occupied(),
1119 "We should have yielded all the represented cards.");
1120 }
1121 #endif
1122
1123 void HeapRegionRemSet::cleanup() {
1124 SparsePRT::cleanup_all();
1125 }
1126
1127 void HeapRegionRemSet::par_cleanup() {
1128 PosParPRT::par_contract_all();
1129 }
1130
1131 void HeapRegionRemSet::add_outgoing_reference(HeapRegion* to_hr) {
1132 _outgoing_region_map.par_at_put(to_hr->hrs_index(), 1);
1133 }
1134
1135 void HeapRegionRemSet::clear() {
1136 clear_outgoing_entries();
1137 _outgoing_region_map.clear();
1138 _other_regions.clear();
1139 assert(occupied() == 0, "Should be clear.");
1140 }
1141
1142 void HeapRegionRemSet::clear_outgoing_entries() {
1143 G1CollectedHeap* g1h = G1CollectedHeap::heap();
1144 size_t i = _outgoing_region_map.get_next_one_offset(0);
1145 while (i < _outgoing_region_map.size()) {
1146 HeapRegion* to_region = g1h->region_at(i);
1147 to_region->rem_set()->clear_incoming_entry(hr());
1148 i = _outgoing_region_map.get_next_one_offset(i+1);
1149 }
1150 }
1151
1152
1153 void HeapRegionRemSet::scrub(CardTableModRefBS* ctbs,
1154 BitMap* region_bm, BitMap* card_bm) {
1155 _other_regions.scrub(ctbs, region_bm, card_bm);
1156 }
1157
1158 //-------------------- Iteration --------------------
1159
1160 HeapRegionRemSetIterator::
1161 HeapRegionRemSetIterator() :
1162 _hrrs(NULL),
1163 _g1h(G1CollectedHeap::heap()),
1164 _bosa(NULL),
1165 _sparse_iter(size_t(G1CollectedHeap::heap()->reserved_region().start())
1166 >> CardTableModRefBS::card_shift)
1167 {}
1168
1169 void HeapRegionRemSetIterator::initialize(const HeapRegionRemSet* hrrs) {
1170 _hrrs = hrrs;
1171 _coarse_map = &_hrrs->_other_regions._coarse_map;
1172 _fine_grain_regions = _hrrs->_other_regions._fine_grain_regions;
1173 _bosa = _hrrs->bosa();
1174
1175 _is = Sparse;
1176 // Set these values so that we increment to the first region.
1177 _coarse_cur_region_index = -1;
1178 _coarse_cur_region_cur_card = (PosParPRT::CardsPerRegion-1);;
1179
1180 _cur_region_cur_card = 0;
1181
1182 _fine_array_index = -1;
1183 _fine_cur_prt = NULL;
1184
1185 _n_yielded_coarse = 0;
1186 _n_yielded_fine = 0;
1187 _n_yielded_sparse = 0;
1188
1189 _sparse_iter.init(&hrrs->_other_regions._sparse_table);
1190 }
1191
1192 bool HeapRegionRemSetIterator::coarse_has_next(size_t& card_index) {
1193 if (_hrrs->_other_regions._n_coarse_entries == 0) return false;
1194 // Go to the next card.
1195 _coarse_cur_region_cur_card++;
1196 // Was the last the last card in the current region?
1197 if (_coarse_cur_region_cur_card == PosParPRT::CardsPerRegion) {
1198 // Yes: find the next region. This may leave _coarse_cur_region_index
1199 // Set to the last index, in which case there are no more coarse
1200 // regions.
1201 _coarse_cur_region_index =
1202 (int) _coarse_map->get_next_one_offset(_coarse_cur_region_index + 1);
1203 if ((size_t)_coarse_cur_region_index < _coarse_map->size()) {
1204 _coarse_cur_region_cur_card = 0;
1205 HeapWord* r_bot =
1206 _g1h->region_at(_coarse_cur_region_index)->bottom();
1207 _cur_region_card_offset = _bosa->index_for(r_bot);
1208 } else {
1209 return false;
1210 }
1211 }
1212 // If we didn't return false above, then we can yield a card.
1213 card_index = _cur_region_card_offset + _coarse_cur_region_cur_card;
1214 return true;
1215 }
1216
1217 void HeapRegionRemSetIterator::fine_find_next_non_null_prt() {
1218 // Otherwise, find the next bucket list in the array.
1219 _fine_array_index++;
1220 while (_fine_array_index < (int) OtherRegionsTable::_max_fine_entries) {
1221 _fine_cur_prt = _fine_grain_regions[_fine_array_index];
1222 if (_fine_cur_prt != NULL) return;
1223 else _fine_array_index++;
1224 }
1225 assert(_fine_cur_prt == NULL, "Loop post");
1226 }
1227
1228 bool HeapRegionRemSetIterator::fine_has_next(size_t& card_index) {
1229 if (fine_has_next()) {
1230 _cur_region_cur_card =
1231 _fine_cur_prt->_bm.get_next_one_offset(_cur_region_cur_card + 1);
1232 }
1233 while (!fine_has_next()) {
1234 if (_cur_region_cur_card == PosParPRT::CardsPerRegion) {
1235 _cur_region_cur_card = 0;
1236 _fine_cur_prt = _fine_cur_prt->next();
1237 }
1238 if (_fine_cur_prt == NULL) {
1239 fine_find_next_non_null_prt();
1240 if (_fine_cur_prt == NULL) return false;
1241 }
1242 assert(_fine_cur_prt != NULL && _cur_region_cur_card == 0,
1243 "inv.");
1244 HeapWord* r_bot =
1245 _fine_cur_prt->hr()->bottom();
1246 _cur_region_card_offset = _bosa->index_for(r_bot);
1247 _cur_region_cur_card = _fine_cur_prt->_bm.get_next_one_offset(0);
1248 }
1249 assert(fine_has_next(), "Or else we exited the loop via the return.");
1250 card_index = _cur_region_card_offset + _cur_region_cur_card;
1251 return true;
1252 }
1253
1254 bool HeapRegionRemSetIterator::fine_has_next() {
1255 return
1256 _fine_cur_prt != NULL &&
1257 _cur_region_cur_card < PosParPRT::CardsPerRegion;
1258 }
1259
1260 bool HeapRegionRemSetIterator::has_next(size_t& card_index) {
1261 switch (_is) {
1262 case Sparse:
1263 if (_sparse_iter.has_next(card_index)) {
1264 _n_yielded_sparse++;
1265 return true;
1266 }
1267 // Otherwise, deliberate fall-through
1268 _is = Fine;
1269 case Fine:
1270 if (fine_has_next(card_index)) {
1271 _n_yielded_fine++;
1272 return true;
1273 }
1274 // Otherwise, deliberate fall-through
1275 _is = Coarse;
1276 case Coarse:
1277 if (coarse_has_next(card_index)) {
1278 _n_yielded_coarse++;
1279 return true;
1280 }
1281 // Otherwise...
1282 break;
1283 }
1284 assert(ParallelGCThreads > 1 ||
1285 n_yielded() == _hrrs->occupied(),
1286 "Should have yielded all the cards in the rem set "
1287 "(in the non-par case).");
1288 return false;
1289 }
1290
1291
1292
1293 oop** HeapRegionRemSet::_recorded_oops = NULL;
1294 HeapWord** HeapRegionRemSet::_recorded_cards = NULL;
1295 HeapRegion** HeapRegionRemSet::_recorded_regions = NULL;
1296 int HeapRegionRemSet::_n_recorded = 0;
1297
1298 HeapRegionRemSet::Event* HeapRegionRemSet::_recorded_events = NULL;
1299 int* HeapRegionRemSet::_recorded_event_index = NULL;
1300 int HeapRegionRemSet::_n_recorded_events = 0;
1301
1302 void HeapRegionRemSet::record(HeapRegion* hr, oop* f) {
1303 if (_recorded_oops == NULL) {
1304 assert(_n_recorded == 0
1305 && _recorded_cards == NULL
1306 && _recorded_regions == NULL,
1307 "Inv");
1308 _recorded_oops = NEW_C_HEAP_ARRAY(oop*, MaxRecorded);
1309 _recorded_cards = NEW_C_HEAP_ARRAY(HeapWord*, MaxRecorded);
1310 _recorded_regions = NEW_C_HEAP_ARRAY(HeapRegion*, MaxRecorded);
1311 }
1312 if (_n_recorded == MaxRecorded) {
1313 gclog_or_tty->print_cr("Filled up 'recorded' (%d).", MaxRecorded);
1314 } else {
1315 _recorded_cards[_n_recorded] =
1316 (HeapWord*)align_size_down(uintptr_t(f),
1317 CardTableModRefBS::card_size);
1318 _recorded_oops[_n_recorded] = f;
1319 _recorded_regions[_n_recorded] = hr;
1320 _n_recorded++;
1321 }
1322 }
1323
1324 void HeapRegionRemSet::record_event(Event evnt) {
1325 if (!G1RecordHRRSEvents) return;
1326
1327 if (_recorded_events == NULL) {
1328 assert(_n_recorded_events == 0
1329 && _recorded_event_index == NULL,
1330 "Inv");
1331 _recorded_events = NEW_C_HEAP_ARRAY(Event, MaxRecordedEvents);
1332 _recorded_event_index = NEW_C_HEAP_ARRAY(int, MaxRecordedEvents);
1333 }
1334 if (_n_recorded_events == MaxRecordedEvents) {
1335 gclog_or_tty->print_cr("Filled up 'recorded_events' (%d).", MaxRecordedEvents);
1336 } else {
1337 _recorded_events[_n_recorded_events] = evnt;
1338 _recorded_event_index[_n_recorded_events] = _n_recorded;
1339 _n_recorded_events++;
1340 }
1341 }
1342
1343 void HeapRegionRemSet::print_event(outputStream* str, Event evnt) {
1344 switch (evnt) {
1345 case Event_EvacStart:
1346 str->print("Evac Start");
1347 break;
1348 case Event_EvacEnd:
1349 str->print("Evac End");
1350 break;
1351 case Event_RSUpdateEnd:
1352 str->print("RS Update End");
1353 break;
1354 }
1355 }
1356
1357 void HeapRegionRemSet::print_recorded() {
1358 int cur_evnt = 0;
1359 Event cur_evnt_kind;
1360 int cur_evnt_ind = 0;
1361 if (_n_recorded_events > 0) {
1362 cur_evnt_kind = _recorded_events[cur_evnt];
1363 cur_evnt_ind = _recorded_event_index[cur_evnt];
1364 }
1365
1366 for (int i = 0; i < _n_recorded; i++) {
1367 while (cur_evnt < _n_recorded_events && i == cur_evnt_ind) {
1368 gclog_or_tty->print("Event: ");
1369 print_event(gclog_or_tty, cur_evnt_kind);
1370 gclog_or_tty->print_cr("");
1371 cur_evnt++;
1372 if (cur_evnt < MaxRecordedEvents) {
1373 cur_evnt_kind = _recorded_events[cur_evnt];
1374 cur_evnt_ind = _recorded_event_index[cur_evnt];
1375 }
1376 }
1377 gclog_or_tty->print("Added card " PTR_FORMAT " to region [" PTR_FORMAT "...]"
1378 " for ref " PTR_FORMAT ".\n",
1379 _recorded_cards[i], _recorded_regions[i]->bottom(),
1380 _recorded_oops[i]);
1381 }
1382 }
1383
1384 #ifndef PRODUCT
1385 void HeapRegionRemSet::test() {
1386 os::sleep(Thread::current(), (jlong)5000, false);
1387 G1CollectedHeap* g1h = G1CollectedHeap::heap();
1388
1389 // Run with "-XX:G1LogRSRegionEntries=2", so that 1 and 5 end up in same
1390 // hash bucket.
1391 HeapRegion* hr0 = g1h->region_at(0);
1392 HeapRegion* hr1 = g1h->region_at(1);
1393 HeapRegion* hr2 = g1h->region_at(5);
1394 HeapRegion* hr3 = g1h->region_at(6);
1395 HeapRegion* hr4 = g1h->region_at(7);
1396 HeapRegion* hr5 = g1h->region_at(8);
1397
1398 HeapWord* hr1_start = hr1->bottom();
1399 HeapWord* hr1_mid = hr1_start + HeapRegion::GrainWords/2;
1400 HeapWord* hr1_last = hr1->end() - 1;
1401
1402 HeapWord* hr2_start = hr2->bottom();
1403 HeapWord* hr2_mid = hr2_start + HeapRegion::GrainWords/2;
1404 HeapWord* hr2_last = hr2->end() - 1;
1405
1406 HeapWord* hr3_start = hr3->bottom();
1407 HeapWord* hr3_mid = hr3_start + HeapRegion::GrainWords/2;
1408 HeapWord* hr3_last = hr3->end() - 1;
1409
1410 HeapRegionRemSet* hrrs = hr0->rem_set();
1411
1412 // Make three references from region 0x101...
1413 hrrs->add_reference((oop*)hr1_start);
1414 hrrs->add_reference((oop*)hr1_mid);
1415 hrrs->add_reference((oop*)hr1_last);
1416
1417 hrrs->add_reference((oop*)hr2_start);
1418 hrrs->add_reference((oop*)hr2_mid);
1419 hrrs->add_reference((oop*)hr2_last);
1420
1421 hrrs->add_reference((oop*)hr3_start);
1422 hrrs->add_reference((oop*)hr3_mid);
1423 hrrs->add_reference((oop*)hr3_last);
1424
1425 // Now cause a coarsening.
1426 hrrs->add_reference((oop*)hr4->bottom());
1427 hrrs->add_reference((oop*)hr5->bottom());
1428
1429 // Now, does iteration yield these three?
1430 HeapRegionRemSetIterator iter;
1431 hrrs->init_iterator(&iter);
1432 size_t sum = 0;
1433 size_t card_index;
1434 while (iter.has_next(card_index)) {
1435 HeapWord* card_start =
1436 G1CollectedHeap::heap()->bot_shared()->address_for_index(card_index);
1437 gclog_or_tty->print_cr(" Card " PTR_FORMAT ".", card_start);
1438 sum++;
1439 }
1440 guarantee(sum == 11 - 3 + 2048, "Failure");
1441 guarantee(sum == hrrs->occupied(), "Failure");
1442 }
1443 #endif

mercurial