Mon, 08 Dec 2014 18:57:33 +0100
8067655: Clean up G1 remembered set oop iteration
Summary: Pass on the static type G1ParPushHeapRSClosure to allow oop_iterate devirtualization
Reviewed-by: jmasa, kbarrett
1 /*
2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "code/nmethod.hpp"
27 #include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
28 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
29 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
30 #include "gc_implementation/g1/heapRegion.inline.hpp"
31 #include "gc_implementation/g1/heapRegionBounds.inline.hpp"
32 #include "gc_implementation/g1/heapRegionRemSet.hpp"
33 #include "gc_implementation/g1/heapRegionManager.inline.hpp"
34 #include "gc_implementation/shared/liveRange.hpp"
35 #include "memory/genOopClosures.inline.hpp"
36 #include "memory/iterator.hpp"
37 #include "memory/space.inline.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "runtime/orderAccess.inline.hpp"
41 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
43 int HeapRegion::LogOfHRGrainBytes = 0;
44 int HeapRegion::LogOfHRGrainWords = 0;
45 size_t HeapRegion::GrainBytes = 0;
46 size_t HeapRegion::GrainWords = 0;
47 size_t HeapRegion::CardsPerRegion = 0;
49 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1,
50 HeapRegion* hr,
51 G1ParPushHeapRSClosure* cl,
52 CardTableModRefBS::PrecisionStyle precision) :
53 DirtyCardToOopClosure(hr, cl, precision, NULL),
54 _hr(hr), _rs_scan(cl), _g1(g1) { }
56 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r,
57 OopClosure* oc) :
58 _r_bottom(r->bottom()), _r_end(r->end()), _oc(oc) { }
60 void HeapRegionDCTOC::walk_mem_region(MemRegion mr,
61 HeapWord* bottom,
62 HeapWord* top) {
63 G1CollectedHeap* g1h = _g1;
64 size_t oop_size;
65 HeapWord* cur = bottom;
67 // Start filtering what we add to the remembered set. If the object is
68 // not considered dead, either because it is marked (in the mark bitmap)
69 // or it was allocated after marking finished, then we add it. Otherwise
70 // we can safely ignore the object.
71 if (!g1h->is_obj_dead(oop(cur), _hr)) {
72 oop_size = oop(cur)->oop_iterate(_rs_scan, mr);
73 } else {
74 oop_size = _hr->block_size(cur);
75 }
77 cur += oop_size;
79 if (cur < top) {
80 oop cur_oop = oop(cur);
81 oop_size = _hr->block_size(cur);
82 HeapWord* next_obj = cur + oop_size;
83 while (next_obj < top) {
84 // Keep filtering the remembered set.
85 if (!g1h->is_obj_dead(cur_oop, _hr)) {
86 // Bottom lies entirely below top, so we can call the
87 // non-memRegion version of oop_iterate below.
88 cur_oop->oop_iterate(_rs_scan);
89 }
90 cur = next_obj;
91 cur_oop = oop(cur);
92 oop_size = _hr->block_size(cur);
93 next_obj = cur + oop_size;
94 }
96 // Last object. Need to do dead-obj filtering here too.
97 if (!g1h->is_obj_dead(oop(cur), _hr)) {
98 oop(cur)->oop_iterate(_rs_scan, mr);
99 }
100 }
101 }
103 size_t HeapRegion::max_region_size() {
104 return HeapRegionBounds::max_size();
105 }
107 void HeapRegion::setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size) {
108 uintx region_size = G1HeapRegionSize;
109 if (FLAG_IS_DEFAULT(G1HeapRegionSize)) {
110 size_t average_heap_size = (initial_heap_size + max_heap_size) / 2;
111 region_size = MAX2(average_heap_size / HeapRegionBounds::target_number(),
112 (uintx) HeapRegionBounds::min_size());
113 }
115 int region_size_log = log2_long((jlong) region_size);
116 // Recalculate the region size to make sure it's a power of
117 // 2. This means that region_size is the largest power of 2 that's
118 // <= what we've calculated so far.
119 region_size = ((uintx)1 << region_size_log);
121 // Now make sure that we don't go over or under our limits.
122 if (region_size < HeapRegionBounds::min_size()) {
123 region_size = HeapRegionBounds::min_size();
124 } else if (region_size > HeapRegionBounds::max_size()) {
125 region_size = HeapRegionBounds::max_size();
126 }
128 // And recalculate the log.
129 region_size_log = log2_long((jlong) region_size);
131 // Now, set up the globals.
132 guarantee(LogOfHRGrainBytes == 0, "we should only set it once");
133 LogOfHRGrainBytes = region_size_log;
135 guarantee(LogOfHRGrainWords == 0, "we should only set it once");
136 LogOfHRGrainWords = LogOfHRGrainBytes - LogHeapWordSize;
138 guarantee(GrainBytes == 0, "we should only set it once");
139 // The cast to int is safe, given that we've bounded region_size by
140 // MIN_REGION_SIZE and MAX_REGION_SIZE.
141 GrainBytes = (size_t)region_size;
143 guarantee(GrainWords == 0, "we should only set it once");
144 GrainWords = GrainBytes >> LogHeapWordSize;
145 guarantee((size_t) 1 << LogOfHRGrainWords == GrainWords, "sanity");
147 guarantee(CardsPerRegion == 0, "we should only set it once");
148 CardsPerRegion = GrainBytes >> CardTableModRefBS::card_shift;
149 }
151 void HeapRegion::reset_after_compaction() {
152 G1OffsetTableContigSpace::reset_after_compaction();
153 // After a compaction the mark bitmap is invalid, so we must
154 // treat all objects as being inside the unmarked area.
155 zero_marked_bytes();
156 init_top_at_mark_start();
157 }
159 void HeapRegion::hr_clear(bool par, bool clear_space, bool locked) {
160 assert(_humongous_start_region == NULL,
161 "we should have already filtered out humongous regions");
162 assert(_end == _orig_end,
163 "we should have already filtered out humongous regions");
165 _in_collection_set = false;
167 set_allocation_context(AllocationContext::system());
168 set_young_index_in_cset(-1);
169 uninstall_surv_rate_group();
170 set_free();
171 reset_pre_dummy_top();
173 if (!par) {
174 // If this is parallel, this will be done later.
175 HeapRegionRemSet* hrrs = rem_set();
176 if (locked) {
177 hrrs->clear_locked();
178 } else {
179 hrrs->clear();
180 }
181 _claimed = InitialClaimValue;
182 }
183 zero_marked_bytes();
185 _offsets.resize(HeapRegion::GrainWords);
186 init_top_at_mark_start();
187 if (clear_space) clear(SpaceDecorator::Mangle);
188 }
190 void HeapRegion::par_clear() {
191 assert(used() == 0, "the region should have been already cleared");
192 assert(capacity() == HeapRegion::GrainBytes, "should be back to normal");
193 HeapRegionRemSet* hrrs = rem_set();
194 hrrs->clear();
195 CardTableModRefBS* ct_bs =
196 (CardTableModRefBS*)G1CollectedHeap::heap()->barrier_set();
197 ct_bs->clear(MemRegion(bottom(), end()));
198 }
200 void HeapRegion::calc_gc_efficiency() {
201 // GC efficiency is the ratio of how much space would be
202 // reclaimed over how long we predict it would take to reclaim it.
203 G1CollectedHeap* g1h = G1CollectedHeap::heap();
204 G1CollectorPolicy* g1p = g1h->g1_policy();
206 // Retrieve a prediction of the elapsed time for this region for
207 // a mixed gc because the region will only be evacuated during a
208 // mixed gc.
209 double region_elapsed_time_ms =
210 g1p->predict_region_elapsed_time_ms(this, false /* for_young_gc */);
211 _gc_efficiency = (double) reclaimable_bytes() / region_elapsed_time_ms;
212 }
214 void HeapRegion::set_startsHumongous(HeapWord* new_top, HeapWord* new_end) {
215 assert(!isHumongous(), "sanity / pre-condition");
216 assert(end() == _orig_end,
217 "Should be normal before the humongous object allocation");
218 assert(top() == bottom(), "should be empty");
219 assert(bottom() <= new_top && new_top <= new_end, "pre-condition");
221 _type.set_starts_humongous();
222 _humongous_start_region = this;
224 set_end(new_end);
225 _offsets.set_for_starts_humongous(new_top);
226 }
228 void HeapRegion::set_continuesHumongous(HeapRegion* first_hr) {
229 assert(!isHumongous(), "sanity / pre-condition");
230 assert(end() == _orig_end,
231 "Should be normal before the humongous object allocation");
232 assert(top() == bottom(), "should be empty");
233 assert(first_hr->startsHumongous(), "pre-condition");
235 _type.set_continues_humongous();
236 _humongous_start_region = first_hr;
237 }
239 void HeapRegion::clear_humongous() {
240 assert(isHumongous(), "pre-condition");
242 if (startsHumongous()) {
243 assert(top() <= end(), "pre-condition");
244 set_end(_orig_end);
245 if (top() > end()) {
246 // at least one "continues humongous" region after it
247 set_top(end());
248 }
249 } else {
250 // continues humongous
251 assert(end() == _orig_end, "sanity");
252 }
254 assert(capacity() == HeapRegion::GrainBytes, "pre-condition");
255 _humongous_start_region = NULL;
256 }
258 bool HeapRegion::claimHeapRegion(jint claimValue) {
259 jint current = _claimed;
260 if (current != claimValue) {
261 jint res = Atomic::cmpxchg(claimValue, &_claimed, current);
262 if (res == current) {
263 return true;
264 }
265 }
266 return false;
267 }
269 HeapRegion::HeapRegion(uint hrm_index,
270 G1BlockOffsetSharedArray* sharedOffsetArray,
271 MemRegion mr) :
272 G1OffsetTableContigSpace(sharedOffsetArray, mr),
273 _hrm_index(hrm_index),
274 _allocation_context(AllocationContext::system()),
275 _humongous_start_region(NULL),
276 _in_collection_set(false),
277 _next_in_special_set(NULL), _orig_end(NULL),
278 _claimed(InitialClaimValue), _evacuation_failed(false),
279 _prev_marked_bytes(0), _next_marked_bytes(0), _gc_efficiency(0.0),
280 _next_young_region(NULL),
281 _next_dirty_cards_region(NULL), _next(NULL), _prev(NULL),
282 #ifdef ASSERT
283 _containing_set(NULL),
284 #endif // ASSERT
285 _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1),
286 _rem_set(NULL), _recorded_rs_length(0), _predicted_elapsed_time_ms(0),
287 _predicted_bytes_to_copy(0)
288 {
289 _rem_set = new HeapRegionRemSet(sharedOffsetArray, this);
290 assert(HeapRegionRemSet::num_par_rem_sets() > 0, "Invariant.");
292 initialize(mr);
293 }
295 void HeapRegion::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
296 assert(_rem_set->is_empty(), "Remembered set must be empty");
298 G1OffsetTableContigSpace::initialize(mr, clear_space, mangle_space);
300 _orig_end = mr.end();
301 hr_clear(false /*par*/, false /*clear_space*/);
302 set_top(bottom());
303 record_top_and_timestamp();
304 }
306 CompactibleSpace* HeapRegion::next_compaction_space() const {
307 return G1CollectedHeap::heap()->next_compaction_region(this);
308 }
310 void HeapRegion::note_self_forwarding_removal_start(bool during_initial_mark,
311 bool during_conc_mark) {
312 // We always recreate the prev marking info and we'll explicitly
313 // mark all objects we find to be self-forwarded on the prev
314 // bitmap. So all objects need to be below PTAMS.
315 _prev_marked_bytes = 0;
317 if (during_initial_mark) {
318 // During initial-mark, we'll also explicitly mark all objects
319 // we find to be self-forwarded on the next bitmap. So all
320 // objects need to be below NTAMS.
321 _next_top_at_mark_start = top();
322 _next_marked_bytes = 0;
323 } else if (during_conc_mark) {
324 // During concurrent mark, all objects in the CSet (including
325 // the ones we find to be self-forwarded) are implicitly live.
326 // So all objects need to be above NTAMS.
327 _next_top_at_mark_start = bottom();
328 _next_marked_bytes = 0;
329 }
330 }
332 void HeapRegion::note_self_forwarding_removal_end(bool during_initial_mark,
333 bool during_conc_mark,
334 size_t marked_bytes) {
335 assert(0 <= marked_bytes && marked_bytes <= used(),
336 err_msg("marked: "SIZE_FORMAT" used: "SIZE_FORMAT,
337 marked_bytes, used()));
338 _prev_top_at_mark_start = top();
339 _prev_marked_bytes = marked_bytes;
340 }
342 HeapWord*
343 HeapRegion::object_iterate_mem_careful(MemRegion mr,
344 ObjectClosure* cl) {
345 G1CollectedHeap* g1h = G1CollectedHeap::heap();
346 // We used to use "block_start_careful" here. But we're actually happy
347 // to update the BOT while we do this...
348 HeapWord* cur = block_start(mr.start());
349 mr = mr.intersection(used_region());
350 if (mr.is_empty()) return NULL;
351 // Otherwise, find the obj that extends onto mr.start().
353 assert(cur <= mr.start()
354 && (oop(cur)->klass_or_null() == NULL ||
355 cur + oop(cur)->size() > mr.start()),
356 "postcondition of block_start");
357 oop obj;
358 while (cur < mr.end()) {
359 obj = oop(cur);
360 if (obj->klass_or_null() == NULL) {
361 // Ran into an unparseable point.
362 return cur;
363 } else if (!g1h->is_obj_dead(obj)) {
364 cl->do_object(obj);
365 }
366 if (cl->abort()) return cur;
367 // The check above must occur before the operation below, since an
368 // abort might invalidate the "size" operation.
369 cur += block_size(cur);
370 }
371 return NULL;
372 }
374 HeapWord*
375 HeapRegion::
376 oops_on_card_seq_iterate_careful(MemRegion mr,
377 FilterOutOfRegionClosure* cl,
378 bool filter_young,
379 jbyte* card_ptr) {
380 // Currently, we should only have to clean the card if filter_young
381 // is true and vice versa.
382 if (filter_young) {
383 assert(card_ptr != NULL, "pre-condition");
384 } else {
385 assert(card_ptr == NULL, "pre-condition");
386 }
387 G1CollectedHeap* g1h = G1CollectedHeap::heap();
389 // If we're within a stop-world GC, then we might look at a card in a
390 // GC alloc region that extends onto a GC LAB, which may not be
391 // parseable. Stop such at the "saved_mark" of the region.
392 if (g1h->is_gc_active()) {
393 mr = mr.intersection(used_region_at_save_marks());
394 } else {
395 mr = mr.intersection(used_region());
396 }
397 if (mr.is_empty()) return NULL;
398 // Otherwise, find the obj that extends onto mr.start().
400 // The intersection of the incoming mr (for the card) and the
401 // allocated part of the region is non-empty. This implies that
402 // we have actually allocated into this region. The code in
403 // G1CollectedHeap.cpp that allocates a new region sets the
404 // is_young tag on the region before allocating. Thus we
405 // safely know if this region is young.
406 if (is_young() && filter_young) {
407 return NULL;
408 }
410 assert(!is_young(), "check value of filter_young");
412 // We can only clean the card here, after we make the decision that
413 // the card is not young. And we only clean the card if we have been
414 // asked to (i.e., card_ptr != NULL).
415 if (card_ptr != NULL) {
416 *card_ptr = CardTableModRefBS::clean_card_val();
417 // We must complete this write before we do any of the reads below.
418 OrderAccess::storeload();
419 }
421 // Cache the boundaries of the memory region in some const locals
422 HeapWord* const start = mr.start();
423 HeapWord* const end = mr.end();
425 // We used to use "block_start_careful" here. But we're actually happy
426 // to update the BOT while we do this...
427 HeapWord* cur = block_start(start);
428 assert(cur <= start, "Postcondition");
430 oop obj;
432 HeapWord* next = cur;
433 while (next <= start) {
434 cur = next;
435 obj = oop(cur);
436 if (obj->klass_or_null() == NULL) {
437 // Ran into an unparseable point.
438 return cur;
439 }
440 // Otherwise...
441 next = cur + block_size(cur);
442 }
444 // If we finish the above loop...We have a parseable object that
445 // begins on or before the start of the memory region, and ends
446 // inside or spans the entire region.
448 assert(obj == oop(cur), "sanity");
449 assert(cur <= start, "Loop postcondition");
450 assert(obj->klass_or_null() != NULL, "Loop postcondition");
451 assert((cur + block_size(cur)) > start, "Loop postcondition");
453 if (!g1h->is_obj_dead(obj)) {
454 obj->oop_iterate(cl, mr);
455 }
457 while (cur < end) {
458 obj = oop(cur);
459 if (obj->klass_or_null() == NULL) {
460 // Ran into an unparseable point.
461 return cur;
462 };
464 // Otherwise:
465 next = cur + block_size(cur);
467 if (!g1h->is_obj_dead(obj)) {
468 if (next < end || !obj->is_objArray()) {
469 // This object either does not span the MemRegion
470 // boundary, or if it does it's not an array.
471 // Apply closure to whole object.
472 obj->oop_iterate(cl);
473 } else {
474 // This obj is an array that spans the boundary.
475 // Stop at the boundary.
476 obj->oop_iterate(cl, mr);
477 }
478 }
479 cur = next;
480 }
481 return NULL;
482 }
484 // Code roots support
486 void HeapRegion::add_strong_code_root(nmethod* nm) {
487 HeapRegionRemSet* hrrs = rem_set();
488 hrrs->add_strong_code_root(nm);
489 }
491 void HeapRegion::add_strong_code_root_locked(nmethod* nm) {
492 assert_locked_or_safepoint(CodeCache_lock);
493 HeapRegionRemSet* hrrs = rem_set();
494 hrrs->add_strong_code_root_locked(nm);
495 }
497 void HeapRegion::remove_strong_code_root(nmethod* nm) {
498 HeapRegionRemSet* hrrs = rem_set();
499 hrrs->remove_strong_code_root(nm);
500 }
502 void HeapRegion::strong_code_roots_do(CodeBlobClosure* blk) const {
503 HeapRegionRemSet* hrrs = rem_set();
504 hrrs->strong_code_roots_do(blk);
505 }
507 class VerifyStrongCodeRootOopClosure: public OopClosure {
508 const HeapRegion* _hr;
509 nmethod* _nm;
510 bool _failures;
511 bool _has_oops_in_region;
513 template <class T> void do_oop_work(T* p) {
514 T heap_oop = oopDesc::load_heap_oop(p);
515 if (!oopDesc::is_null(heap_oop)) {
516 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
518 // Note: not all the oops embedded in the nmethod are in the
519 // current region. We only look at those which are.
520 if (_hr->is_in(obj)) {
521 // Object is in the region. Check that its less than top
522 if (_hr->top() <= (HeapWord*)obj) {
523 // Object is above top
524 gclog_or_tty->print_cr("Object "PTR_FORMAT" in region "
525 "["PTR_FORMAT", "PTR_FORMAT") is above "
526 "top "PTR_FORMAT,
527 (void *)obj, _hr->bottom(), _hr->end(), _hr->top());
528 _failures = true;
529 return;
530 }
531 // Nmethod has at least one oop in the current region
532 _has_oops_in_region = true;
533 }
534 }
535 }
537 public:
538 VerifyStrongCodeRootOopClosure(const HeapRegion* hr, nmethod* nm):
539 _hr(hr), _failures(false), _has_oops_in_region(false) {}
541 void do_oop(narrowOop* p) { do_oop_work(p); }
542 void do_oop(oop* p) { do_oop_work(p); }
544 bool failures() { return _failures; }
545 bool has_oops_in_region() { return _has_oops_in_region; }
546 };
548 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure {
549 const HeapRegion* _hr;
550 bool _failures;
551 public:
552 VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) :
553 _hr(hr), _failures(false) {}
555 void do_code_blob(CodeBlob* cb) {
556 nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null();
557 if (nm != NULL) {
558 // Verify that the nemthod is live
559 if (!nm->is_alive()) {
560 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] has dead nmethod "
561 PTR_FORMAT" in its strong code roots",
562 _hr->bottom(), _hr->end(), nm);
563 _failures = true;
564 } else {
565 VerifyStrongCodeRootOopClosure oop_cl(_hr, nm);
566 nm->oops_do(&oop_cl);
567 if (!oop_cl.has_oops_in_region()) {
568 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] has nmethod "
569 PTR_FORMAT" in its strong code roots "
570 "with no pointers into region",
571 _hr->bottom(), _hr->end(), nm);
572 _failures = true;
573 } else if (oop_cl.failures()) {
574 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] has other "
575 "failures for nmethod "PTR_FORMAT,
576 _hr->bottom(), _hr->end(), nm);
577 _failures = true;
578 }
579 }
580 }
581 }
583 bool failures() { return _failures; }
584 };
586 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const {
587 if (!G1VerifyHeapRegionCodeRoots) {
588 // We're not verifying code roots.
589 return;
590 }
591 if (vo == VerifyOption_G1UseMarkWord) {
592 // Marking verification during a full GC is performed after class
593 // unloading, code cache unloading, etc so the strong code roots
594 // attached to each heap region are in an inconsistent state. They won't
595 // be consistent until the strong code roots are rebuilt after the
596 // actual GC. Skip verifying the strong code roots in this particular
597 // time.
598 assert(VerifyDuringGC, "only way to get here");
599 return;
600 }
602 HeapRegionRemSet* hrrs = rem_set();
603 size_t strong_code_roots_length = hrrs->strong_code_roots_list_length();
605 // if this region is empty then there should be no entries
606 // on its strong code root list
607 if (is_empty()) {
608 if (strong_code_roots_length > 0) {
609 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is empty "
610 "but has "SIZE_FORMAT" code root entries",
611 bottom(), end(), strong_code_roots_length);
612 *failures = true;
613 }
614 return;
615 }
617 if (continuesHumongous()) {
618 if (strong_code_roots_length > 0) {
619 gclog_or_tty->print_cr("region "HR_FORMAT" is a continuation of a humongous "
620 "region but has "SIZE_FORMAT" code root entries",
621 HR_FORMAT_PARAMS(this), strong_code_roots_length);
622 *failures = true;
623 }
624 return;
625 }
627 VerifyStrongCodeRootCodeBlobClosure cb_cl(this);
628 strong_code_roots_do(&cb_cl);
630 if (cb_cl.failures()) {
631 *failures = true;
632 }
633 }
635 void HeapRegion::print() const { print_on(gclog_or_tty); }
636 void HeapRegion::print_on(outputStream* st) const {
637 st->print("AC%4u", allocation_context());
638 st->print(" %2s", get_short_type_str());
639 if (in_collection_set())
640 st->print(" CS");
641 else
642 st->print(" ");
643 st->print(" TS %5d", _gc_time_stamp);
644 st->print(" PTAMS "PTR_FORMAT" NTAMS "PTR_FORMAT,
645 prev_top_at_mark_start(), next_top_at_mark_start());
646 G1OffsetTableContigSpace::print_on(st);
647 }
649 class VerifyLiveClosure: public OopClosure {
650 private:
651 G1CollectedHeap* _g1h;
652 CardTableModRefBS* _bs;
653 oop _containing_obj;
654 bool _failures;
655 int _n_failures;
656 VerifyOption _vo;
657 public:
658 // _vo == UsePrevMarking -> use "prev" marking information,
659 // _vo == UseNextMarking -> use "next" marking information,
660 // _vo == UseMarkWord -> use mark word from object header.
661 VerifyLiveClosure(G1CollectedHeap* g1h, VerifyOption vo) :
662 _g1h(g1h), _bs(NULL), _containing_obj(NULL),
663 _failures(false), _n_failures(0), _vo(vo)
664 {
665 BarrierSet* bs = _g1h->barrier_set();
666 if (bs->is_a(BarrierSet::CardTableModRef))
667 _bs = (CardTableModRefBS*)bs;
668 }
670 void set_containing_obj(oop obj) {
671 _containing_obj = obj;
672 }
674 bool failures() { return _failures; }
675 int n_failures() { return _n_failures; }
677 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
678 virtual void do_oop( oop* p) { do_oop_work(p); }
680 void print_object(outputStream* out, oop obj) {
681 #ifdef PRODUCT
682 Klass* k = obj->klass();
683 const char* class_name = InstanceKlass::cast(k)->external_name();
684 out->print_cr("class name %s", class_name);
685 #else // PRODUCT
686 obj->print_on(out);
687 #endif // PRODUCT
688 }
690 template <class T>
691 void do_oop_work(T* p) {
692 assert(_containing_obj != NULL, "Precondition");
693 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
694 "Precondition");
695 T heap_oop = oopDesc::load_heap_oop(p);
696 if (!oopDesc::is_null(heap_oop)) {
697 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
698 bool failed = false;
699 if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) {
700 MutexLockerEx x(ParGCRareEvent_lock,
701 Mutex::_no_safepoint_check_flag);
703 if (!_failures) {
704 gclog_or_tty->cr();
705 gclog_or_tty->print_cr("----------");
706 }
707 if (!_g1h->is_in_closed_subset(obj)) {
708 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
709 gclog_or_tty->print_cr("Field "PTR_FORMAT
710 " of live obj "PTR_FORMAT" in region "
711 "["PTR_FORMAT", "PTR_FORMAT")",
712 p, (void*) _containing_obj,
713 from->bottom(), from->end());
714 print_object(gclog_or_tty, _containing_obj);
715 gclog_or_tty->print_cr("points to obj "PTR_FORMAT" not in the heap",
716 (void*) obj);
717 } else {
718 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
719 HeapRegion* to = _g1h->heap_region_containing((HeapWord*)obj);
720 gclog_or_tty->print_cr("Field "PTR_FORMAT
721 " of live obj "PTR_FORMAT" in region "
722 "["PTR_FORMAT", "PTR_FORMAT")",
723 p, (void*) _containing_obj,
724 from->bottom(), from->end());
725 print_object(gclog_or_tty, _containing_obj);
726 gclog_or_tty->print_cr("points to dead obj "PTR_FORMAT" in region "
727 "["PTR_FORMAT", "PTR_FORMAT")",
728 (void*) obj, to->bottom(), to->end());
729 print_object(gclog_or_tty, obj);
730 }
731 gclog_or_tty->print_cr("----------");
732 gclog_or_tty->flush();
733 _failures = true;
734 failed = true;
735 _n_failures++;
736 }
738 if (!_g1h->full_collection() || G1VerifyRSetsDuringFullGC) {
739 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
740 HeapRegion* to = _g1h->heap_region_containing(obj);
741 if (from != NULL && to != NULL &&
742 from != to &&
743 !to->isHumongous()) {
744 jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
745 jbyte cv_field = *_bs->byte_for_const(p);
746 const jbyte dirty = CardTableModRefBS::dirty_card_val();
748 bool is_bad = !(from->is_young()
749 || to->rem_set()->contains_reference(p)
750 || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
751 (_containing_obj->is_objArray() ?
752 cv_field == dirty
753 : cv_obj == dirty || cv_field == dirty));
754 if (is_bad) {
755 MutexLockerEx x(ParGCRareEvent_lock,
756 Mutex::_no_safepoint_check_flag);
758 if (!_failures) {
759 gclog_or_tty->cr();
760 gclog_or_tty->print_cr("----------");
761 }
762 gclog_or_tty->print_cr("Missing rem set entry:");
763 gclog_or_tty->print_cr("Field "PTR_FORMAT" "
764 "of obj "PTR_FORMAT", "
765 "in region "HR_FORMAT,
766 p, (void*) _containing_obj,
767 HR_FORMAT_PARAMS(from));
768 _containing_obj->print_on(gclog_or_tty);
769 gclog_or_tty->print_cr("points to obj "PTR_FORMAT" "
770 "in region "HR_FORMAT,
771 (void*) obj,
772 HR_FORMAT_PARAMS(to));
773 obj->print_on(gclog_or_tty);
774 gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.",
775 cv_obj, cv_field);
776 gclog_or_tty->print_cr("----------");
777 gclog_or_tty->flush();
778 _failures = true;
779 if (!failed) _n_failures++;
780 }
781 }
782 }
783 }
784 }
785 };
787 // This really ought to be commoned up into OffsetTableContigSpace somehow.
788 // We would need a mechanism to make that code skip dead objects.
790 void HeapRegion::verify(VerifyOption vo,
791 bool* failures) const {
792 G1CollectedHeap* g1 = G1CollectedHeap::heap();
793 *failures = false;
794 HeapWord* p = bottom();
795 HeapWord* prev_p = NULL;
796 VerifyLiveClosure vl_cl(g1, vo);
797 bool is_humongous = isHumongous();
798 bool do_bot_verify = !is_young();
799 size_t object_num = 0;
800 while (p < top()) {
801 oop obj = oop(p);
802 size_t obj_size = block_size(p);
803 object_num += 1;
805 if (is_humongous != g1->isHumongous(obj_size) &&
806 !g1->is_obj_dead(obj, this)) { // Dead objects may have bigger block_size since they span several objects.
807 gclog_or_tty->print_cr("obj "PTR_FORMAT" is of %shumongous size ("
808 SIZE_FORMAT" words) in a %shumongous region",
809 p, g1->isHumongous(obj_size) ? "" : "non-",
810 obj_size, is_humongous ? "" : "non-");
811 *failures = true;
812 return;
813 }
815 // If it returns false, verify_for_object() will output the
816 // appropriate message.
817 if (do_bot_verify &&
818 !g1->is_obj_dead(obj, this) &&
819 !_offsets.verify_for_object(p, obj_size)) {
820 *failures = true;
821 return;
822 }
824 if (!g1->is_obj_dead_cond(obj, this, vo)) {
825 if (obj->is_oop()) {
826 Klass* klass = obj->klass();
827 bool is_metaspace_object = Metaspace::contains(klass) ||
828 (vo == VerifyOption_G1UsePrevMarking &&
829 ClassLoaderDataGraph::unload_list_contains(klass));
830 if (!is_metaspace_object) {
831 gclog_or_tty->print_cr("klass "PTR_FORMAT" of object "PTR_FORMAT" "
832 "not metadata", klass, (void *)obj);
833 *failures = true;
834 return;
835 } else if (!klass->is_klass()) {
836 gclog_or_tty->print_cr("klass "PTR_FORMAT" of object "PTR_FORMAT" "
837 "not a klass", klass, (void *)obj);
838 *failures = true;
839 return;
840 } else {
841 vl_cl.set_containing_obj(obj);
842 obj->oop_iterate_no_header(&vl_cl);
843 if (vl_cl.failures()) {
844 *failures = true;
845 }
846 if (G1MaxVerifyFailures >= 0 &&
847 vl_cl.n_failures() >= G1MaxVerifyFailures) {
848 return;
849 }
850 }
851 } else {
852 gclog_or_tty->print_cr(PTR_FORMAT" no an oop", (void *)obj);
853 *failures = true;
854 return;
855 }
856 }
857 prev_p = p;
858 p += obj_size;
859 }
861 if (p != top()) {
862 gclog_or_tty->print_cr("end of last object "PTR_FORMAT" "
863 "does not match top "PTR_FORMAT, p, top());
864 *failures = true;
865 return;
866 }
868 HeapWord* the_end = end();
869 assert(p == top(), "it should still hold");
870 // Do some extra BOT consistency checking for addresses in the
871 // range [top, end). BOT look-ups in this range should yield
872 // top. No point in doing that if top == end (there's nothing there).
873 if (p < the_end) {
874 // Look up top
875 HeapWord* addr_1 = p;
876 HeapWord* b_start_1 = _offsets.block_start_const(addr_1);
877 if (b_start_1 != p) {
878 gclog_or_tty->print_cr("BOT look up for top: "PTR_FORMAT" "
879 " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
880 addr_1, b_start_1, p);
881 *failures = true;
882 return;
883 }
885 // Look up top + 1
886 HeapWord* addr_2 = p + 1;
887 if (addr_2 < the_end) {
888 HeapWord* b_start_2 = _offsets.block_start_const(addr_2);
889 if (b_start_2 != p) {
890 gclog_or_tty->print_cr("BOT look up for top + 1: "PTR_FORMAT" "
891 " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
892 addr_2, b_start_2, p);
893 *failures = true;
894 return;
895 }
896 }
898 // Look up an address between top and end
899 size_t diff = pointer_delta(the_end, p) / 2;
900 HeapWord* addr_3 = p + diff;
901 if (addr_3 < the_end) {
902 HeapWord* b_start_3 = _offsets.block_start_const(addr_3);
903 if (b_start_3 != p) {
904 gclog_or_tty->print_cr("BOT look up for top + diff: "PTR_FORMAT" "
905 " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
906 addr_3, b_start_3, p);
907 *failures = true;
908 return;
909 }
910 }
912 // Loook up end - 1
913 HeapWord* addr_4 = the_end - 1;
914 HeapWord* b_start_4 = _offsets.block_start_const(addr_4);
915 if (b_start_4 != p) {
916 gclog_or_tty->print_cr("BOT look up for end - 1: "PTR_FORMAT" "
917 " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
918 addr_4, b_start_4, p);
919 *failures = true;
920 return;
921 }
922 }
924 if (is_humongous && object_num > 1) {
925 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is humongous "
926 "but has "SIZE_FORMAT", objects",
927 bottom(), end(), object_num);
928 *failures = true;
929 return;
930 }
932 verify_strong_code_roots(vo, failures);
933 }
935 void HeapRegion::verify() const {
936 bool dummy = false;
937 verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy);
938 }
940 // G1OffsetTableContigSpace code; copied from space.cpp. Hope this can go
941 // away eventually.
943 void G1OffsetTableContigSpace::clear(bool mangle_space) {
944 set_top(bottom());
945 set_saved_mark_word(bottom());
946 CompactibleSpace::clear(mangle_space);
947 reset_bot();
948 }
950 void G1OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
951 Space::set_bottom(new_bottom);
952 _offsets.set_bottom(new_bottom);
953 }
955 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
956 Space::set_end(new_end);
957 _offsets.resize(new_end - bottom());
958 }
960 void G1OffsetTableContigSpace::print() const {
961 print_short();
962 gclog_or_tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
963 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
964 bottom(), top(), _offsets.threshold(), end());
965 }
967 HeapWord* G1OffsetTableContigSpace::initialize_threshold() {
968 return _offsets.initialize_threshold();
969 }
971 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start,
972 HeapWord* end) {
973 _offsets.alloc_block(start, end);
974 return _offsets.threshold();
975 }
977 HeapWord* G1OffsetTableContigSpace::saved_mark_word() const {
978 G1CollectedHeap* g1h = G1CollectedHeap::heap();
979 assert( _gc_time_stamp <= g1h->get_gc_time_stamp(), "invariant" );
980 HeapWord* local_top = top();
981 OrderAccess::loadload();
982 if (_gc_time_stamp < g1h->get_gc_time_stamp()) {
983 return local_top;
984 } else {
985 return Space::saved_mark_word();
986 }
987 }
989 void G1OffsetTableContigSpace::record_top_and_timestamp() {
990 G1CollectedHeap* g1h = G1CollectedHeap::heap();
991 unsigned curr_gc_time_stamp = g1h->get_gc_time_stamp();
993 if (_gc_time_stamp < curr_gc_time_stamp) {
994 // The order of these is important, as another thread might be
995 // about to start scanning this region. If it does so after
996 // set_saved_mark and before _gc_time_stamp = ..., then the latter
997 // will be false, and it will pick up top() as the high water mark
998 // of region. If it does so after _gc_time_stamp = ..., then it
999 // will pick up the right saved_mark_word() as the high water mark
1000 // of the region. Either way, the behaviour will be correct.
1001 Space::set_saved_mark_word(top());
1002 OrderAccess::storestore();
1003 _gc_time_stamp = curr_gc_time_stamp;
1004 // No need to do another barrier to flush the writes above. If
1005 // this is called in parallel with other threads trying to
1006 // allocate into the region, the caller should call this while
1007 // holding a lock and when the lock is released the writes will be
1008 // flushed.
1009 }
1010 }
1012 void G1OffsetTableContigSpace::safe_object_iterate(ObjectClosure* blk) {
1013 object_iterate(blk);
1014 }
1016 void G1OffsetTableContigSpace::object_iterate(ObjectClosure* blk) {
1017 HeapWord* p = bottom();
1018 while (p < top()) {
1019 if (block_is_obj(p)) {
1020 blk->do_object(oop(p));
1021 }
1022 p += block_size(p);
1023 }
1024 }
1026 #define block_is_always_obj(q) true
1027 void G1OffsetTableContigSpace::prepare_for_compaction(CompactPoint* cp) {
1028 SCAN_AND_FORWARD(cp, top, block_is_always_obj, block_size);
1029 }
1030 #undef block_is_always_obj
1032 G1OffsetTableContigSpace::
1033 G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray,
1034 MemRegion mr) :
1035 _offsets(sharedOffsetArray, mr),
1036 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true),
1037 _gc_time_stamp(0)
1038 {
1039 _offsets.set_space(this);
1040 }
1042 void G1OffsetTableContigSpace::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
1043 CompactibleSpace::initialize(mr, clear_space, mangle_space);
1044 _top = bottom();
1045 reset_bot();
1046 }