Tue, 05 May 2009 22:15:35 -0700
6833576: G1: assert illegal index, growableArray.hpp:186
Summary: The code that calculates the heap region index for an object address incorrectly used signed arithmetic.
Reviewed-by: jcoomes, ysr
1 /*
2 * Copyright 2001-2008 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 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_heapRegion.cpp.incl"
28 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1,
29 HeapRegion* hr, OopClosure* cl,
30 CardTableModRefBS::PrecisionStyle precision,
31 FilterKind fk) :
32 ContiguousSpaceDCTOC(hr, cl, precision, NULL),
33 _hr(hr), _fk(fk), _g1(g1)
34 {}
36 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r,
37 OopClosure* oc) :
38 _r_bottom(r->bottom()), _r_end(r->end()),
39 _oc(oc), _out_of_region(0)
40 {}
42 class VerifyLiveClosure: public OopClosure {
43 G1CollectedHeap* _g1h;
44 CardTableModRefBS* _bs;
45 oop _containing_obj;
46 bool _failures;
47 int _n_failures;
48 public:
49 VerifyLiveClosure(G1CollectedHeap* g1h) :
50 _g1h(g1h), _bs(NULL), _containing_obj(NULL),
51 _failures(false), _n_failures(0)
52 {
53 BarrierSet* bs = _g1h->barrier_set();
54 if (bs->is_a(BarrierSet::CardTableModRef))
55 _bs = (CardTableModRefBS*)bs;
56 }
58 void set_containing_obj(oop obj) {
59 _containing_obj = obj;
60 }
62 bool failures() { return _failures; }
63 int n_failures() { return _n_failures; }
65 virtual void do_oop(narrowOop* p) {
66 guarantee(false, "NYI");
67 }
69 void do_oop(oop* p) {
70 assert(_containing_obj != NULL, "Precondition");
71 assert(!_g1h->is_obj_dead(_containing_obj), "Precondition");
72 oop obj = *p;
73 if (obj != NULL) {
74 bool failed = false;
75 if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead(obj)) {
76 if (!_failures) {
77 gclog_or_tty->print_cr("");
78 gclog_or_tty->print_cr("----------");
79 }
80 if (!_g1h->is_in_closed_subset(obj)) {
81 gclog_or_tty->print_cr("Field "PTR_FORMAT
82 " of live obj "PTR_FORMAT
83 " points to obj "PTR_FORMAT
84 " not in the heap.",
85 p, (void*) _containing_obj, (void*) obj);
86 } else {
87 gclog_or_tty->print_cr("Field "PTR_FORMAT
88 " of live obj "PTR_FORMAT
89 " points to dead obj "PTR_FORMAT".",
90 p, (void*) _containing_obj, (void*) obj);
91 }
92 gclog_or_tty->print_cr("Live obj:");
93 _containing_obj->print_on(gclog_or_tty);
94 gclog_or_tty->print_cr("Bad referent:");
95 obj->print_on(gclog_or_tty);
96 gclog_or_tty->print_cr("----------");
97 _failures = true;
98 failed = true;
99 _n_failures++;
100 }
102 if (!_g1h->full_collection()) {
103 HeapRegion* from = _g1h->heap_region_containing(p);
104 HeapRegion* to = _g1h->heap_region_containing(*p);
105 if (from != NULL && to != NULL &&
106 from != to &&
107 !to->isHumongous()) {
108 jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
109 jbyte cv_field = *_bs->byte_for_const(p);
110 const jbyte dirty = CardTableModRefBS::dirty_card_val();
112 bool is_bad = !(from->is_young()
113 || to->rem_set()->contains_reference(p)
114 || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
115 (_containing_obj->is_objArray() ?
116 cv_field == dirty
117 : cv_obj == dirty || cv_field == dirty));
118 if (is_bad) {
119 if (!_failures) {
120 gclog_or_tty->print_cr("");
121 gclog_or_tty->print_cr("----------");
122 }
123 gclog_or_tty->print_cr("Missing rem set entry:");
124 gclog_or_tty->print_cr("Field "PTR_FORMAT
125 " of obj "PTR_FORMAT
126 ", in region %d ["PTR_FORMAT
127 ", "PTR_FORMAT"),",
128 p, (void*) _containing_obj,
129 from->hrs_index(),
130 from->bottom(),
131 from->end());
132 _containing_obj->print_on(gclog_or_tty);
133 gclog_or_tty->print_cr("points to obj "PTR_FORMAT
134 " in region %d ["PTR_FORMAT
135 ", "PTR_FORMAT").",
136 (void*) obj, to->hrs_index(),
137 to->bottom(), to->end());
138 obj->print_on(gclog_or_tty);
139 gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.",
140 cv_obj, cv_field);
141 gclog_or_tty->print_cr("----------");
142 _failures = true;
143 if (!failed) _n_failures++;
144 }
145 }
146 }
147 }
148 }
149 };
151 template<class ClosureType>
152 HeapWord* walk_mem_region_loop(ClosureType* cl, G1CollectedHeap* g1h,
153 HeapRegion* hr,
154 HeapWord* cur, HeapWord* top) {
155 oop cur_oop = oop(cur);
156 int oop_size = cur_oop->size();
157 HeapWord* next_obj = cur + oop_size;
158 while (next_obj < top) {
159 // Keep filtering the remembered set.
160 if (!g1h->is_obj_dead(cur_oop, hr)) {
161 // Bottom lies entirely below top, so we can call the
162 // non-memRegion version of oop_iterate below.
163 cur_oop->oop_iterate(cl);
164 }
165 cur = next_obj;
166 cur_oop = oop(cur);
167 oop_size = cur_oop->size();
168 next_obj = cur + oop_size;
169 }
170 return cur;
171 }
173 void HeapRegionDCTOC::walk_mem_region_with_cl(MemRegion mr,
174 HeapWord* bottom,
175 HeapWord* top,
176 OopClosure* cl) {
177 G1CollectedHeap* g1h = _g1;
179 int oop_size;
181 OopClosure* cl2 = cl;
182 FilterIntoCSClosure intoCSFilt(this, g1h, cl);
183 FilterOutOfRegionClosure outOfRegionFilt(_hr, cl);
184 switch (_fk) {
185 case IntoCSFilterKind: cl2 = &intoCSFilt; break;
186 case OutOfRegionFilterKind: cl2 = &outOfRegionFilt; break;
187 }
189 // Start filtering what we add to the remembered set. If the object is
190 // not considered dead, either because it is marked (in the mark bitmap)
191 // or it was allocated after marking finished, then we add it. Otherwise
192 // we can safely ignore the object.
193 if (!g1h->is_obj_dead(oop(bottom), _hr)) {
194 oop_size = oop(bottom)->oop_iterate(cl2, mr);
195 } else {
196 oop_size = oop(bottom)->size();
197 }
199 bottom += oop_size;
201 if (bottom < top) {
202 // We replicate the loop below for several kinds of possible filters.
203 switch (_fk) {
204 case NoFilterKind:
205 bottom = walk_mem_region_loop(cl, g1h, _hr, bottom, top);
206 break;
207 case IntoCSFilterKind: {
208 FilterIntoCSClosure filt(this, g1h, cl);
209 bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top);
210 break;
211 }
212 case OutOfRegionFilterKind: {
213 FilterOutOfRegionClosure filt(_hr, cl);
214 bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top);
215 break;
216 }
217 default:
218 ShouldNotReachHere();
219 }
221 // Last object. Need to do dead-obj filtering here too.
222 if (!g1h->is_obj_dead(oop(bottom), _hr)) {
223 oop(bottom)->oop_iterate(cl2, mr);
224 }
225 }
226 }
228 void HeapRegion::reset_after_compaction() {
229 G1OffsetTableContigSpace::reset_after_compaction();
230 // After a compaction the mark bitmap is invalid, so we must
231 // treat all objects as being inside the unmarked area.
232 zero_marked_bytes();
233 init_top_at_mark_start();
234 }
236 DirtyCardToOopClosure*
237 HeapRegion::new_dcto_closure(OopClosure* cl,
238 CardTableModRefBS::PrecisionStyle precision,
239 HeapRegionDCTOC::FilterKind fk) {
240 return new HeapRegionDCTOC(G1CollectedHeap::heap(),
241 this, cl, precision, fk);
242 }
244 void HeapRegion::hr_clear(bool par, bool clear_space) {
245 _humongous_type = NotHumongous;
246 _humongous_start_region = NULL;
247 _in_collection_set = false;
248 _is_gc_alloc_region = false;
250 // Age stuff (if parallel, this will be done separately, since it needs
251 // to be sequential).
252 G1CollectedHeap* g1h = G1CollectedHeap::heap();
254 set_young_index_in_cset(-1);
255 uninstall_surv_rate_group();
256 set_young_type(NotYoung);
258 // In case it had been the start of a humongous sequence, reset its end.
259 set_end(_orig_end);
261 if (!par) {
262 // If this is parallel, this will be done later.
263 HeapRegionRemSet* hrrs = rem_set();
264 if (hrrs != NULL) hrrs->clear();
265 _claimed = InitialClaimValue;
266 }
267 zero_marked_bytes();
268 set_sort_index(-1);
270 _offsets.resize(HeapRegion::GrainWords);
271 init_top_at_mark_start();
272 if (clear_space) clear(SpaceDecorator::Mangle);
273 }
275 // <PREDICTION>
276 void HeapRegion::calc_gc_efficiency() {
277 G1CollectedHeap* g1h = G1CollectedHeap::heap();
278 _gc_efficiency = (double) garbage_bytes() /
279 g1h->predict_region_elapsed_time_ms(this, false);
280 }
281 // </PREDICTION>
283 void HeapRegion::set_startsHumongous() {
284 _humongous_type = StartsHumongous;
285 _humongous_start_region = this;
286 assert(end() == _orig_end, "Should be normal before alloc.");
287 }
289 bool HeapRegion::claimHeapRegion(jint claimValue) {
290 jint current = _claimed;
291 if (current != claimValue) {
292 jint res = Atomic::cmpxchg(claimValue, &_claimed, current);
293 if (res == current) {
294 return true;
295 }
296 }
297 return false;
298 }
300 HeapWord* HeapRegion::next_block_start_careful(HeapWord* addr) {
301 HeapWord* low = addr;
302 HeapWord* high = end();
303 while (low < high) {
304 size_t diff = pointer_delta(high, low);
305 // Must add one below to bias toward the high amount. Otherwise, if
306 // "high" were at the desired value, and "low" were one less, we
307 // would not converge on "high". This is not symmetric, because
308 // we set "high" to a block start, which might be the right one,
309 // which we don't do for "low".
310 HeapWord* middle = low + (diff+1)/2;
311 if (middle == high) return high;
312 HeapWord* mid_bs = block_start_careful(middle);
313 if (mid_bs < addr) {
314 low = middle;
315 } else {
316 high = mid_bs;
317 }
318 }
319 assert(low == high && low >= addr, "Didn't work.");
320 return low;
321 }
323 void HeapRegion::set_next_on_unclean_list(HeapRegion* r) {
324 assert(r == NULL || r->is_on_unclean_list(), "Malformed unclean list.");
325 _next_in_special_set = r;
326 }
328 void HeapRegion::set_on_unclean_list(bool b) {
329 _is_on_unclean_list = b;
330 }
332 void HeapRegion::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
333 G1OffsetTableContigSpace::initialize(mr, false, mangle_space);
334 hr_clear(false/*par*/, clear_space);
335 }
336 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
337 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
338 #endif // _MSC_VER
341 HeapRegion::
342 HeapRegion(G1BlockOffsetSharedArray* sharedOffsetArray,
343 MemRegion mr, bool is_zeroed)
344 : G1OffsetTableContigSpace(sharedOffsetArray, mr, is_zeroed),
345 _next_fk(HeapRegionDCTOC::NoFilterKind),
346 _hrs_index(-1),
347 _humongous_type(NotHumongous), _humongous_start_region(NULL),
348 _in_collection_set(false), _is_gc_alloc_region(false),
349 _is_on_free_list(false), _is_on_unclean_list(false),
350 _next_in_special_set(NULL), _orig_end(NULL),
351 _claimed(InitialClaimValue), _evacuation_failed(false),
352 _prev_marked_bytes(0), _next_marked_bytes(0), _sort_index(-1),
353 _young_type(NotYoung), _next_young_region(NULL),
354 _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1),
355 _rem_set(NULL), _zfs(NotZeroFilled)
356 {
357 _orig_end = mr.end();
358 // Note that initialize() will set the start of the unmarked area of the
359 // region.
360 this->initialize(mr, !is_zeroed, SpaceDecorator::Mangle);
361 set_top(bottom());
362 set_saved_mark();
364 _rem_set = new HeapRegionRemSet(sharedOffsetArray, this);
366 assert(HeapRegionRemSet::num_par_rem_sets() > 0, "Invariant.");
367 // In case the region is allocated during a pause, note the top.
368 // We haven't done any counting on a brand new region.
369 _top_at_conc_mark_count = bottom();
370 }
372 class NextCompactionHeapRegionClosure: public HeapRegionClosure {
373 const HeapRegion* _target;
374 bool _target_seen;
375 HeapRegion* _last;
376 CompactibleSpace* _res;
377 public:
378 NextCompactionHeapRegionClosure(const HeapRegion* target) :
379 _target(target), _target_seen(false), _res(NULL) {}
380 bool doHeapRegion(HeapRegion* cur) {
381 if (_target_seen) {
382 if (!cur->isHumongous()) {
383 _res = cur;
384 return true;
385 }
386 } else if (cur == _target) {
387 _target_seen = true;
388 }
389 return false;
390 }
391 CompactibleSpace* result() { return _res; }
392 };
394 CompactibleSpace* HeapRegion::next_compaction_space() const {
395 G1CollectedHeap* g1h = G1CollectedHeap::heap();
396 // cast away const-ness
397 HeapRegion* r = (HeapRegion*) this;
398 NextCompactionHeapRegionClosure blk(r);
399 g1h->heap_region_iterate_from(r, &blk);
400 return blk.result();
401 }
403 void HeapRegion::set_continuesHumongous(HeapRegion* start) {
404 // The order is important here.
405 start->add_continuingHumongousRegion(this);
406 _humongous_type = ContinuesHumongous;
407 _humongous_start_region = start;
408 }
410 void HeapRegion::add_continuingHumongousRegion(HeapRegion* cont) {
411 // Must join the blocks of the current H region seq with the block of the
412 // added region.
413 offsets()->join_blocks(bottom(), cont->bottom());
414 arrayOop obj = (arrayOop)(bottom());
415 obj->set_length((int) (obj->length() + cont->capacity()/jintSize));
416 set_end(cont->end());
417 set_top(cont->end());
418 }
420 void HeapRegion::save_marks() {
421 set_saved_mark();
422 }
424 void HeapRegion::oops_in_mr_iterate(MemRegion mr, OopClosure* cl) {
425 HeapWord* p = mr.start();
426 HeapWord* e = mr.end();
427 oop obj;
428 while (p < e) {
429 obj = oop(p);
430 p += obj->oop_iterate(cl);
431 }
432 assert(p == e, "bad memregion: doesn't end on obj boundary");
433 }
435 #define HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \
436 void HeapRegion::oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl) { \
437 ContiguousSpace::oop_since_save_marks_iterate##nv_suffix(cl); \
438 }
439 SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN)
442 void HeapRegion::oop_before_save_marks_iterate(OopClosure* cl) {
443 oops_in_mr_iterate(MemRegion(bottom(), saved_mark_word()), cl);
444 }
446 #ifdef DEBUG
447 HeapWord* HeapRegion::allocate(size_t size) {
448 jint state = zero_fill_state();
449 assert(!G1CollectedHeap::heap()->allocs_are_zero_filled() ||
450 zero_fill_is_allocated(),
451 "When ZF is on, only alloc in ZF'd regions");
452 return G1OffsetTableContigSpace::allocate(size);
453 }
454 #endif
456 void HeapRegion::set_zero_fill_state_work(ZeroFillState zfs) {
457 assert(top() == bottom() || zfs == Allocated,
458 "Region must be empty, or we must be setting it to allocated.");
459 assert(ZF_mon->owned_by_self() ||
460 Universe::heap()->is_gc_active(),
461 "Must hold the lock or be a full GC to modify.");
462 _zfs = zfs;
463 }
465 void HeapRegion::set_zero_fill_complete() {
466 set_zero_fill_state_work(ZeroFilled);
467 if (ZF_mon->owned_by_self()) {
468 ZF_mon->notify_all();
469 }
470 }
473 void HeapRegion::ensure_zero_filled() {
474 MutexLockerEx x(ZF_mon, Mutex::_no_safepoint_check_flag);
475 ensure_zero_filled_locked();
476 }
478 void HeapRegion::ensure_zero_filled_locked() {
479 assert(ZF_mon->owned_by_self(), "Precondition");
480 bool should_ignore_zf = SafepointSynchronize::is_at_safepoint();
481 assert(should_ignore_zf || Heap_lock->is_locked(),
482 "Either we're in a GC or we're allocating a region.");
483 switch (zero_fill_state()) {
484 case HeapRegion::NotZeroFilled:
485 set_zero_fill_in_progress(Thread::current());
486 {
487 ZF_mon->unlock();
488 Copy::fill_to_words(bottom(), capacity()/HeapWordSize);
489 ZF_mon->lock_without_safepoint_check();
490 }
491 // A trap.
492 guarantee(zero_fill_state() == HeapRegion::ZeroFilling
493 && zero_filler() == Thread::current(),
494 "AHA! Tell Dave D if you see this...");
495 set_zero_fill_complete();
496 // gclog_or_tty->print_cr("Did sync ZF.");
497 ConcurrentZFThread::note_sync_zfs();
498 break;
499 case HeapRegion::ZeroFilling:
500 if (should_ignore_zf) {
501 // We can "break" the lock and take over the work.
502 Copy::fill_to_words(bottom(), capacity()/HeapWordSize);
503 set_zero_fill_complete();
504 ConcurrentZFThread::note_sync_zfs();
505 break;
506 } else {
507 ConcurrentZFThread::wait_for_ZF_completed(this);
508 }
509 case HeapRegion::ZeroFilled:
510 // Nothing to do.
511 break;
512 case HeapRegion::Allocated:
513 guarantee(false, "Should not call on allocated regions.");
514 }
515 assert(zero_fill_state() == HeapRegion::ZeroFilled, "Post");
516 }
518 HeapWord*
519 HeapRegion::object_iterate_mem_careful(MemRegion mr,
520 ObjectClosure* cl) {
521 G1CollectedHeap* g1h = G1CollectedHeap::heap();
522 // We used to use "block_start_careful" here. But we're actually happy
523 // to update the BOT while we do this...
524 HeapWord* cur = block_start(mr.start());
525 mr = mr.intersection(used_region());
526 if (mr.is_empty()) return NULL;
527 // Otherwise, find the obj that extends onto mr.start().
529 assert(cur <= mr.start()
530 && (oop(cur)->klass() == NULL ||
531 cur + oop(cur)->size() > mr.start()),
532 "postcondition of block_start");
533 oop obj;
534 while (cur < mr.end()) {
535 obj = oop(cur);
536 if (obj->klass() == NULL) {
537 // Ran into an unparseable point.
538 return cur;
539 } else if (!g1h->is_obj_dead(obj)) {
540 cl->do_object(obj);
541 }
542 if (cl->abort()) return cur;
543 // The check above must occur before the operation below, since an
544 // abort might invalidate the "size" operation.
545 cur += obj->size();
546 }
547 return NULL;
548 }
550 HeapWord*
551 HeapRegion::
552 oops_on_card_seq_iterate_careful(MemRegion mr,
553 FilterOutOfRegionClosure* cl) {
554 G1CollectedHeap* g1h = G1CollectedHeap::heap();
556 // If we're within a stop-world GC, then we might look at a card in a
557 // GC alloc region that extends onto a GC LAB, which may not be
558 // parseable. Stop such at the "saved_mark" of the region.
559 if (G1CollectedHeap::heap()->is_gc_active()) {
560 mr = mr.intersection(used_region_at_save_marks());
561 } else {
562 mr = mr.intersection(used_region());
563 }
564 if (mr.is_empty()) return NULL;
565 // Otherwise, find the obj that extends onto mr.start().
567 // We used to use "block_start_careful" here. But we're actually happy
568 // to update the BOT while we do this...
569 HeapWord* cur = block_start(mr.start());
570 assert(cur <= mr.start(), "Postcondition");
572 while (cur <= mr.start()) {
573 if (oop(cur)->klass() == NULL) {
574 // Ran into an unparseable point.
575 return cur;
576 }
577 // Otherwise...
578 int sz = oop(cur)->size();
579 if (cur + sz > mr.start()) break;
580 // Otherwise, go on.
581 cur = cur + sz;
582 }
583 oop obj;
584 obj = oop(cur);
585 // If we finish this loop...
586 assert(cur <= mr.start()
587 && obj->klass() != NULL
588 && cur + obj->size() > mr.start(),
589 "Loop postcondition");
590 if (!g1h->is_obj_dead(obj)) {
591 obj->oop_iterate(cl, mr);
592 }
594 HeapWord* next;
595 while (cur < mr.end()) {
596 obj = oop(cur);
597 if (obj->klass() == NULL) {
598 // Ran into an unparseable point.
599 return cur;
600 };
601 // Otherwise:
602 next = (cur + obj->size());
603 if (!g1h->is_obj_dead(obj)) {
604 if (next < mr.end()) {
605 obj->oop_iterate(cl);
606 } else {
607 // this obj spans the boundary. If it's an array, stop at the
608 // boundary.
609 if (obj->is_objArray()) {
610 obj->oop_iterate(cl, mr);
611 } else {
612 obj->oop_iterate(cl);
613 }
614 }
615 }
616 cur = next;
617 }
618 return NULL;
619 }
621 void HeapRegion::print() const { print_on(gclog_or_tty); }
622 void HeapRegion::print_on(outputStream* st) const {
623 if (isHumongous()) {
624 if (startsHumongous())
625 st->print(" HS");
626 else
627 st->print(" HC");
628 } else {
629 st->print(" ");
630 }
631 if (in_collection_set())
632 st->print(" CS");
633 else if (is_gc_alloc_region())
634 st->print(" A ");
635 else
636 st->print(" ");
637 if (is_young())
638 st->print(is_scan_only() ? " SO" : (is_survivor() ? " SU" : " Y "));
639 else
640 st->print(" ");
641 if (is_empty())
642 st->print(" F");
643 else
644 st->print(" ");
645 st->print(" %d", _gc_time_stamp);
646 G1OffsetTableContigSpace::print_on(st);
647 }
649 #define OBJ_SAMPLE_INTERVAL 0
650 #define BLOCK_SAMPLE_INTERVAL 100
652 // This really ought to be commoned up into OffsetTableContigSpace somehow.
653 // We would need a mechanism to make that code skip dead objects.
655 void HeapRegion::verify(bool allow_dirty) const {
656 G1CollectedHeap* g1 = G1CollectedHeap::heap();
657 HeapWord* p = bottom();
658 HeapWord* prev_p = NULL;
659 int objs = 0;
660 int blocks = 0;
661 VerifyLiveClosure vl_cl(g1);
662 while (p < top()) {
663 size_t size = oop(p)->size();
664 if (blocks == BLOCK_SAMPLE_INTERVAL) {
665 guarantee(p == block_start_const(p + (size/2)),
666 "check offset computation");
667 blocks = 0;
668 } else {
669 blocks++;
670 }
671 if (objs == OBJ_SAMPLE_INTERVAL) {
672 oop obj = oop(p);
673 if (!g1->is_obj_dead(obj, this)) {
674 obj->verify();
675 vl_cl.set_containing_obj(obj);
676 obj->oop_iterate(&vl_cl);
677 if (G1MaxVerifyFailures >= 0
678 && vl_cl.n_failures() >= G1MaxVerifyFailures) break;
679 }
680 objs = 0;
681 } else {
682 objs++;
683 }
684 prev_p = p;
685 p += size;
686 }
687 HeapWord* rend = end();
688 HeapWord* rtop = top();
689 if (rtop < rend) {
690 guarantee(block_start_const(rtop + (rend - rtop) / 2) == rtop,
691 "check offset computation");
692 }
693 if (vl_cl.failures()) {
694 gclog_or_tty->print_cr("Heap:");
695 G1CollectedHeap::heap()->print();
696 gclog_or_tty->print_cr("");
697 }
698 if (VerifyDuringGC &&
699 G1VerifyConcMarkPrintReachable &&
700 vl_cl.failures()) {
701 g1->concurrent_mark()->print_prev_bitmap_reachable();
702 }
703 guarantee(!vl_cl.failures(), "region verification failed");
704 guarantee(p == top(), "end of last object must match end of space");
705 }
707 // G1OffsetTableContigSpace code; copied from space.cpp. Hope this can go
708 // away eventually.
710 void G1OffsetTableContigSpace::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
711 // false ==> we'll do the clearing if there's clearing to be done.
712 ContiguousSpace::initialize(mr, false, mangle_space);
713 _offsets.zero_bottom_entry();
714 _offsets.initialize_threshold();
715 if (clear_space) clear(mangle_space);
716 }
718 void G1OffsetTableContigSpace::clear(bool mangle_space) {
719 ContiguousSpace::clear(mangle_space);
720 _offsets.zero_bottom_entry();
721 _offsets.initialize_threshold();
722 }
724 void G1OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
725 Space::set_bottom(new_bottom);
726 _offsets.set_bottom(new_bottom);
727 }
729 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
730 Space::set_end(new_end);
731 _offsets.resize(new_end - bottom());
732 }
734 void G1OffsetTableContigSpace::print() const {
735 print_short();
736 gclog_or_tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
737 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
738 bottom(), top(), _offsets.threshold(), end());
739 }
741 HeapWord* G1OffsetTableContigSpace::initialize_threshold() {
742 return _offsets.initialize_threshold();
743 }
745 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start,
746 HeapWord* end) {
747 _offsets.alloc_block(start, end);
748 return _offsets.threshold();
749 }
751 HeapWord* G1OffsetTableContigSpace::saved_mark_word() const {
752 G1CollectedHeap* g1h = G1CollectedHeap::heap();
753 assert( _gc_time_stamp <= g1h->get_gc_time_stamp(), "invariant" );
754 if (_gc_time_stamp < g1h->get_gc_time_stamp())
755 return top();
756 else
757 return ContiguousSpace::saved_mark_word();
758 }
760 void G1OffsetTableContigSpace::set_saved_mark() {
761 G1CollectedHeap* g1h = G1CollectedHeap::heap();
762 unsigned curr_gc_time_stamp = g1h->get_gc_time_stamp();
764 if (_gc_time_stamp < curr_gc_time_stamp) {
765 // The order of these is important, as another thread might be
766 // about to start scanning this region. If it does so after
767 // set_saved_mark and before _gc_time_stamp = ..., then the latter
768 // will be false, and it will pick up top() as the high water mark
769 // of region. If it does so after _gc_time_stamp = ..., then it
770 // will pick up the right saved_mark_word() as the high water mark
771 // of the region. Either way, the behaviour will be correct.
772 ContiguousSpace::set_saved_mark();
773 _gc_time_stamp = curr_gc_time_stamp;
774 OrderAccess::fence();
775 }
776 }
778 G1OffsetTableContigSpace::
779 G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray,
780 MemRegion mr, bool is_zeroed) :
781 _offsets(sharedOffsetArray, mr),
782 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true),
783 _gc_time_stamp(0)
784 {
785 _offsets.set_space(this);
786 initialize(mr, !is_zeroed, SpaceDecorator::Mangle);
787 }
789 size_t RegionList::length() {
790 size_t len = 0;
791 HeapRegion* cur = hd();
792 DEBUG_ONLY(HeapRegion* last = NULL);
793 while (cur != NULL) {
794 len++;
795 DEBUG_ONLY(last = cur);
796 cur = get_next(cur);
797 }
798 assert(last == tl(), "Invariant");
799 return len;
800 }
802 void RegionList::insert_before_head(HeapRegion* r) {
803 assert(well_formed(), "Inv");
804 set_next(r, hd());
805 _hd = r;
806 _sz++;
807 if (tl() == NULL) _tl = r;
808 assert(well_formed(), "Inv");
809 }
811 void RegionList::prepend_list(RegionList* new_list) {
812 assert(well_formed(), "Precondition");
813 assert(new_list->well_formed(), "Precondition");
814 HeapRegion* new_tl = new_list->tl();
815 if (new_tl != NULL) {
816 set_next(new_tl, hd());
817 _hd = new_list->hd();
818 _sz += new_list->sz();
819 if (tl() == NULL) _tl = new_list->tl();
820 } else {
821 assert(new_list->hd() == NULL && new_list->sz() == 0, "Inv");
822 }
823 assert(well_formed(), "Inv");
824 }
826 void RegionList::delete_after(HeapRegion* r) {
827 assert(well_formed(), "Precondition");
828 HeapRegion* next = get_next(r);
829 assert(r != NULL, "Precondition");
830 HeapRegion* next_tl = get_next(next);
831 set_next(r, next_tl);
832 dec_sz();
833 if (next == tl()) {
834 assert(next_tl == NULL, "Inv");
835 _tl = r;
836 }
837 assert(well_formed(), "Inv");
838 }
840 HeapRegion* RegionList::pop() {
841 assert(well_formed(), "Inv");
842 HeapRegion* res = hd();
843 if (res != NULL) {
844 _hd = get_next(res);
845 _sz--;
846 set_next(res, NULL);
847 if (sz() == 0) _tl = NULL;
848 }
849 assert(well_formed(), "Inv");
850 return res;
851 }