rev 11552 : imported patch 8159978-collection-set-as-array
1 /*
2 * Copyright (c) 2001, 2016, 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 */
24
25 #include "precompiled.hpp"
26 #include "code/nmethod.hpp"
27 #include "gc/g1/g1BlockOffsetTable.inline.hpp"
28 #include "gc/g1/g1CollectedHeap.inline.hpp"
29 #include "gc/g1/g1HeapRegionTraceType.hpp"
30 #include "gc/g1/g1OopClosures.inline.hpp"
31 #include "gc/g1/heapRegion.inline.hpp"
32 #include "gc/g1/heapRegionBounds.inline.hpp"
33 #include "gc/g1/heapRegionManager.inline.hpp"
34 #include "gc/g1/heapRegionRemSet.hpp"
35 #include "gc/g1/heapRegionTracer.hpp"
36 #include "gc/shared/genOopClosures.inline.hpp"
37 #include "gc/shared/space.inline.hpp"
38 #include "logging/log.hpp"
39 #include "memory/iterator.hpp"
40 #include "memory/resourceArea.hpp"
41 #include "oops/oop.inline.hpp"
42 #include "runtime/atomic.inline.hpp"
43 #include "runtime/orderAccess.inline.hpp"
44
45 int HeapRegion::LogOfHRGrainBytes = 0;
46 int HeapRegion::LogOfHRGrainWords = 0;
47 size_t HeapRegion::GrainBytes = 0;
48 size_t HeapRegion::GrainWords = 0;
49 size_t HeapRegion::CardsPerRegion = 0;
50
51 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1,
52 HeapRegion* hr,
53 G1ParPushHeapRSClosure* cl,
54 CardTableModRefBS::PrecisionStyle precision) :
55 DirtyCardToOopClosure(hr, cl, precision, NULL),
56 _hr(hr), _rs_scan(cl), _g1(g1) { }
57
58 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r,
59 OopClosure* oc) :
60 _r_bottom(r->bottom()), _r_end(r->end()), _oc(oc) { }
61
62 void HeapRegionDCTOC::walk_mem_region(MemRegion mr,
63 HeapWord* bottom,
64 HeapWord* top) {
65 G1CollectedHeap* g1h = _g1;
66 size_t oop_size;
67 HeapWord* cur = bottom;
68
69 // Start filtering what we add to the remembered set. If the object is
70 // not considered dead, either because it is marked (in the mark bitmap)
71 // or it was allocated after marking finished, then we add it. Otherwise
72 // we can safely ignore the object.
73 if (!g1h->is_obj_dead(oop(cur))) {
74 oop_size = oop(cur)->oop_iterate_size(_rs_scan, mr);
75 } else {
76 oop_size = _hr->block_size(cur);
77 }
78
79 cur += oop_size;
80
81 if (cur < top) {
82 oop cur_oop = oop(cur);
83 oop_size = _hr->block_size(cur);
84 HeapWord* next_obj = cur + oop_size;
85 while (next_obj < top) {
86 // Keep filtering the remembered set.
87 if (!g1h->is_obj_dead(cur_oop)) {
88 // Bottom lies entirely below top, so we can call the
89 // non-memRegion version of oop_iterate below.
90 cur_oop->oop_iterate(_rs_scan);
91 }
92 cur = next_obj;
93 cur_oop = oop(cur);
94 oop_size = _hr->block_size(cur);
95 next_obj = cur + oop_size;
96 }
97
98 // Last object. Need to do dead-obj filtering here too.
99 if (!g1h->is_obj_dead(oop(cur))) {
100 oop(cur)->oop_iterate(_rs_scan, mr);
101 }
102 }
103 }
104
105 size_t HeapRegion::max_region_size() {
106 return HeapRegionBounds::max_size();
107 }
108
109 size_t HeapRegion::min_region_size_in_words() {
110 return HeapRegionBounds::min_size() >> LogHeapWordSize;
111 }
112
113 void HeapRegion::setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size) {
114 size_t region_size = G1HeapRegionSize;
115 if (FLAG_IS_DEFAULT(G1HeapRegionSize)) {
116 size_t average_heap_size = (initial_heap_size + max_heap_size) / 2;
117 region_size = MAX2(average_heap_size / HeapRegionBounds::target_number(),
118 HeapRegionBounds::min_size());
119 }
120
121 int region_size_log = log2_long((jlong) region_size);
122 // Recalculate the region size to make sure it's a power of
123 // 2. This means that region_size is the largest power of 2 that's
124 // <= what we've calculated so far.
125 region_size = ((size_t)1 << region_size_log);
126
127 // Now make sure that we don't go over or under our limits.
128 if (region_size < HeapRegionBounds::min_size()) {
129 region_size = HeapRegionBounds::min_size();
130 } else if (region_size > HeapRegionBounds::max_size()) {
131 region_size = HeapRegionBounds::max_size();
132 }
133
134 // And recalculate the log.
135 region_size_log = log2_long((jlong) region_size);
136
137 // Now, set up the globals.
138 guarantee(LogOfHRGrainBytes == 0, "we should only set it once");
139 LogOfHRGrainBytes = region_size_log;
140
141 guarantee(LogOfHRGrainWords == 0, "we should only set it once");
142 LogOfHRGrainWords = LogOfHRGrainBytes - LogHeapWordSize;
143
144 guarantee(GrainBytes == 0, "we should only set it once");
145 // The cast to int is safe, given that we've bounded region_size by
146 // MIN_REGION_SIZE and MAX_REGION_SIZE.
147 GrainBytes = region_size;
148 log_info(gc, heap)("Heap region size: " SIZE_FORMAT "M", GrainBytes / M);
149
150 guarantee(GrainWords == 0, "we should only set it once");
151 GrainWords = GrainBytes >> LogHeapWordSize;
152 guarantee((size_t) 1 << LogOfHRGrainWords == GrainWords, "sanity");
153
154 guarantee(CardsPerRegion == 0, "we should only set it once");
155 CardsPerRegion = GrainBytes >> CardTableModRefBS::card_shift;
156
157 if (G1HeapRegionSize != GrainBytes) {
158 FLAG_SET_ERGO(size_t, G1HeapRegionSize, GrainBytes);
159 }
160 }
161
162 void HeapRegion::reset_after_compaction() {
163 G1ContiguousSpace::reset_after_compaction();
164 // After a compaction the mark bitmap is invalid, so we must
165 // treat all objects as being inside the unmarked area.
166 zero_marked_bytes();
167 init_top_at_mark_start();
168 }
169
170 void HeapRegion::hr_clear(bool par, bool clear_space, bool locked) {
171 assert(_humongous_start_region == NULL,
172 "we should have already filtered out humongous regions");
173 assert(!in_collection_set(),
174 "Should not clear heap region %u in the collection set", hrm_index());
175
176 set_allocation_context(AllocationContext::system());
177 set_young_index_in_cset(-1);
178 uninstall_surv_rate_group();
179 set_free();
180 reset_pre_dummy_top();
181
182 if (!par) {
183 // If this is parallel, this will be done later.
184 HeapRegionRemSet* hrrs = rem_set();
185 if (locked) {
186 hrrs->clear_locked();
187 } else {
188 hrrs->clear();
189 }
190 }
191 zero_marked_bytes();
192
193 init_top_at_mark_start();
194 _gc_time_stamp = G1CollectedHeap::heap()->get_gc_time_stamp();
195 if (clear_space) clear(SpaceDecorator::Mangle);
196 }
197
198 void HeapRegion::par_clear() {
199 assert(used() == 0, "the region should have been already cleared");
200 assert(capacity() == HeapRegion::GrainBytes, "should be back to normal");
201 HeapRegionRemSet* hrrs = rem_set();
202 hrrs->clear();
203 CardTableModRefBS* ct_bs =
204 barrier_set_cast<CardTableModRefBS>(G1CollectedHeap::heap()->barrier_set());
205 ct_bs->clear(MemRegion(bottom(), end()));
206 }
207
208 void HeapRegion::calc_gc_efficiency() {
209 // GC efficiency is the ratio of how much space would be
210 // reclaimed over how long we predict it would take to reclaim it.
211 G1CollectedHeap* g1h = G1CollectedHeap::heap();
212 G1Policy* g1p = g1h->g1_policy();
213
214 // Retrieve a prediction of the elapsed time for this region for
215 // a mixed gc because the region will only be evacuated during a
216 // mixed gc.
217 double region_elapsed_time_ms =
218 g1p->predict_region_elapsed_time_ms(this, false /* for_young_gc */);
219 _gc_efficiency = (double) reclaimable_bytes() / region_elapsed_time_ms;
220 }
221
222 void HeapRegion::set_free() {
223 report_region_type_change(G1HeapRegionTraceType::Free);
224 _type.set_free();
225 }
226
227 void HeapRegion::set_eden() {
228 report_region_type_change(G1HeapRegionTraceType::Eden);
229 _type.set_eden();
230 }
231
232 void HeapRegion::set_eden_pre_gc() {
233 report_region_type_change(G1HeapRegionTraceType::Eden);
234 _type.set_eden_pre_gc();
235 }
236
237 void HeapRegion::set_survivor() {
238 report_region_type_change(G1HeapRegionTraceType::Survivor);
239 _type.set_survivor();
240 }
241
242 void HeapRegion::set_old() {
243 report_region_type_change(G1HeapRegionTraceType::Old);
244 _type.set_old();
245 }
246
247 void HeapRegion::set_archive() {
248 report_region_type_change(G1HeapRegionTraceType::Archive);
249 _type.set_archive();
250 }
251
252 void HeapRegion::set_starts_humongous(HeapWord* obj_top, size_t fill_size) {
253 assert(!is_humongous(), "sanity / pre-condition");
254 assert(top() == bottom(), "should be empty");
255
256 report_region_type_change(G1HeapRegionTraceType::StartsHumongous);
257 _type.set_starts_humongous();
258 _humongous_start_region = this;
259
260 _bot_part.set_for_starts_humongous(obj_top, fill_size);
261 }
262
263 void HeapRegion::set_continues_humongous(HeapRegion* first_hr) {
264 assert(!is_humongous(), "sanity / pre-condition");
265 assert(top() == bottom(), "should be empty");
266 assert(first_hr->is_starts_humongous(), "pre-condition");
267
268 report_region_type_change(G1HeapRegionTraceType::ContinuesHumongous);
269 _type.set_continues_humongous();
270 _humongous_start_region = first_hr;
271 }
272
273 void HeapRegion::clear_humongous() {
274 assert(is_humongous(), "pre-condition");
275
276 assert(capacity() == HeapRegion::GrainBytes, "pre-condition");
277 _humongous_start_region = NULL;
278 }
279
280 HeapRegion::HeapRegion(uint hrm_index,
281 G1BlockOffsetTable* bot,
282 MemRegion mr) :
283 G1ContiguousSpace(bot),
284 _hrm_index(hrm_index),
285 _allocation_context(AllocationContext::system()),
286 _humongous_start_region(NULL),
287 _next_in_special_set(NULL),
288 _evacuation_failed(false),
289 _prev_marked_bytes(0), _next_marked_bytes(0), _gc_efficiency(0.0),
290 _next(NULL), _prev(NULL),
291 #ifdef ASSERT
292 _containing_set(NULL),
293 #endif // ASSERT
294 _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1),
295 _rem_set(NULL), _recorded_rs_length(0), _predicted_elapsed_time_ms(0),
296 _predicted_bytes_to_copy(0)
297 {
298 _rem_set = new HeapRegionRemSet(bot, this);
299
300 initialize(mr);
301 }
302
303 void HeapRegion::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
304 assert(_rem_set->is_empty(), "Remembered set must be empty");
305
306 G1ContiguousSpace::initialize(mr, clear_space, mangle_space);
307
308 hr_clear(false /*par*/, false /*clear_space*/);
309 set_top(bottom());
310 record_timestamp();
311 }
312
313 void HeapRegion::report_region_type_change(G1HeapRegionTraceType::Type to) {
314 HeapRegionTracer::send_region_type_change(_hrm_index,
315 get_trace_type(),
316 to,
317 (uintptr_t)bottom(),
318 used(),
319 (uint)allocation_context());
320 }
321
322 CompactibleSpace* HeapRegion::next_compaction_space() const {
323 return G1CollectedHeap::heap()->next_compaction_region(this);
324 }
325
326 void HeapRegion::note_self_forwarding_removal_start(bool during_initial_mark,
327 bool during_conc_mark) {
328 // We always recreate the prev marking info and we'll explicitly
329 // mark all objects we find to be self-forwarded on the prev
330 // bitmap. So all objects need to be below PTAMS.
331 _prev_marked_bytes = 0;
332
333 if (during_initial_mark) {
334 // During initial-mark, we'll also explicitly mark all objects
335 // we find to be self-forwarded on the next bitmap. So all
336 // objects need to be below NTAMS.
337 _next_top_at_mark_start = top();
338 _next_marked_bytes = 0;
339 } else if (during_conc_mark) {
340 // During concurrent mark, all objects in the CSet (including
341 // the ones we find to be self-forwarded) are implicitly live.
342 // So all objects need to be above NTAMS.
343 _next_top_at_mark_start = bottom();
344 _next_marked_bytes = 0;
345 }
346 }
347
348 void HeapRegion::note_self_forwarding_removal_end(bool during_initial_mark,
349 bool during_conc_mark,
350 size_t marked_bytes) {
351 assert(marked_bytes <= used(),
352 "marked: " SIZE_FORMAT " used: " SIZE_FORMAT, marked_bytes, used());
353 _prev_top_at_mark_start = top();
354 _prev_marked_bytes = marked_bytes;
355 }
356
357 HeapWord*
358 HeapRegion::object_iterate_mem_careful(MemRegion mr,
359 ObjectClosure* cl) {
360 G1CollectedHeap* g1h = G1CollectedHeap::heap();
361 // We used to use "block_start_careful" here. But we're actually happy
362 // to update the BOT while we do this...
363 HeapWord* cur = block_start(mr.start());
364 mr = mr.intersection(used_region());
365 if (mr.is_empty()) return NULL;
366 // Otherwise, find the obj that extends onto mr.start().
367
368 assert(cur <= mr.start()
369 && (oop(cur)->klass_or_null() == NULL ||
370 cur + oop(cur)->size() > mr.start()),
371 "postcondition of block_start");
372 oop obj;
373 while (cur < mr.end()) {
374 obj = oop(cur);
375 if (obj->klass_or_null() == NULL) {
376 // Ran into an unparseable point.
377 return cur;
378 } else if (!g1h->is_obj_dead(obj)) {
379 cl->do_object(obj);
380 }
381 cur += block_size(cur);
382 }
383 return NULL;
384 }
385
386 HeapWord*
387 HeapRegion::
388 oops_on_card_seq_iterate_careful(MemRegion mr,
389 FilterOutOfRegionClosure* cl,
390 bool filter_young,
391 jbyte* card_ptr) {
392 // Currently, we should only have to clean the card if filter_young
393 // is true and vice versa.
394 if (filter_young) {
395 assert(card_ptr != NULL, "pre-condition");
396 } else {
397 assert(card_ptr == NULL, "pre-condition");
398 }
399 G1CollectedHeap* g1h = G1CollectedHeap::heap();
400
401 // If we're within a stop-world GC, then we might look at a card in a
402 // GC alloc region that extends onto a GC LAB, which may not be
403 // parseable. Stop such at the "scan_top" of the region.
404 if (g1h->is_gc_active()) {
405 mr = mr.intersection(MemRegion(bottom(), scan_top()));
406 } else {
407 mr = mr.intersection(used_region());
408 }
409 if (mr.is_empty()) return NULL;
410 // Otherwise, find the obj that extends onto mr.start().
411
412 // The intersection of the incoming mr (for the card) and the
413 // allocated part of the region is non-empty. This implies that
414 // we have actually allocated into this region. The code in
415 // G1CollectedHeap.cpp that allocates a new region sets the
416 // is_young tag on the region before allocating. Thus we
417 // safely know if this region is young.
418 if (is_young() && filter_young) {
419 return NULL;
420 }
421
422 assert(!is_young(), "check value of filter_young");
423
424 // We can only clean the card here, after we make the decision that
425 // the card is not young. And we only clean the card if we have been
426 // asked to (i.e., card_ptr != NULL).
427 if (card_ptr != NULL) {
428 *card_ptr = CardTableModRefBS::clean_card_val();
429 // We must complete this write before we do any of the reads below.
430 OrderAccess::storeload();
431 }
432
433 // Cache the boundaries of the memory region in some const locals
434 HeapWord* const start = mr.start();
435 HeapWord* const end = mr.end();
436
437 // We used to use "block_start_careful" here. But we're actually happy
438 // to update the BOT while we do this...
439 HeapWord* cur = block_start(start);
440 assert(cur <= start, "Postcondition");
441
442 oop obj;
443
444 HeapWord* next = cur;
445 do {
446 cur = next;
447 obj = oop(cur);
448 if (obj->klass_or_null() == NULL) {
449 // Ran into an unparseable point.
450 return cur;
451 }
452 // Otherwise...
453 next = cur + block_size(cur);
454 } while (next <= start);
455
456 // If we finish the above loop...We have a parseable object that
457 // begins on or before the start of the memory region, and ends
458 // inside or spans the entire region.
459 assert(cur <= start, "Loop postcondition");
460 assert(obj->klass_or_null() != NULL, "Loop postcondition");
461
462 do {
463 obj = oop(cur);
464 assert((cur + block_size(cur)) > (HeapWord*)obj, "Loop invariant");
465 if (obj->klass_or_null() == NULL) {
466 // Ran into an unparseable point.
467 return cur;
468 }
469
470 // Advance the current pointer. "obj" still points to the object to iterate.
471 cur = cur + block_size(cur);
472
473 if (!g1h->is_obj_dead(obj)) {
474 // Non-objArrays are sometimes marked imprecise at the object start. We
475 // always need to iterate over them in full.
476 // We only iterate over object arrays in full if they are completely contained
477 // in the memory region.
478 if (!obj->is_objArray() || (((HeapWord*)obj) >= start && cur <= end)) {
479 obj->oop_iterate(cl);
480 } else {
481 obj->oop_iterate(cl, mr);
482 }
483 }
484 } while (cur < end);
485
486 return NULL;
487 }
488
489 // Code roots support
490
491 void HeapRegion::add_strong_code_root(nmethod* nm) {
492 HeapRegionRemSet* hrrs = rem_set();
493 hrrs->add_strong_code_root(nm);
494 }
495
496 void HeapRegion::add_strong_code_root_locked(nmethod* nm) {
497 assert_locked_or_safepoint(CodeCache_lock);
498 HeapRegionRemSet* hrrs = rem_set();
499 hrrs->add_strong_code_root_locked(nm);
500 }
501
502 void HeapRegion::remove_strong_code_root(nmethod* nm) {
503 HeapRegionRemSet* hrrs = rem_set();
504 hrrs->remove_strong_code_root(nm);
505 }
506
507 void HeapRegion::strong_code_roots_do(CodeBlobClosure* blk) const {
508 HeapRegionRemSet* hrrs = rem_set();
509 hrrs->strong_code_roots_do(blk);
510 }
511
512 class VerifyStrongCodeRootOopClosure: public OopClosure {
513 const HeapRegion* _hr;
514 nmethod* _nm;
515 bool _failures;
516 bool _has_oops_in_region;
517
518 template <class T> void do_oop_work(T* p) {
519 T heap_oop = oopDesc::load_heap_oop(p);
520 if (!oopDesc::is_null(heap_oop)) {
521 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
522
523 // Note: not all the oops embedded in the nmethod are in the
524 // current region. We only look at those which are.
525 if (_hr->is_in(obj)) {
526 // Object is in the region. Check that its less than top
527 if (_hr->top() <= (HeapWord*)obj) {
528 // Object is above top
529 log_error(gc, verify)("Object " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ") is above top " PTR_FORMAT,
530 p2i(obj), p2i(_hr->bottom()), p2i(_hr->end()), p2i(_hr->top()));
531 _failures = true;
532 return;
533 }
534 // Nmethod has at least one oop in the current region
535 _has_oops_in_region = true;
536 }
537 }
538 }
539
540 public:
541 VerifyStrongCodeRootOopClosure(const HeapRegion* hr, nmethod* nm):
542 _hr(hr), _failures(false), _has_oops_in_region(false) {}
543
544 void do_oop(narrowOop* p) { do_oop_work(p); }
545 void do_oop(oop* p) { do_oop_work(p); }
546
547 bool failures() { return _failures; }
548 bool has_oops_in_region() { return _has_oops_in_region; }
549 };
550
551 class VerifyStrongCodeRootCodeBlobClosure: public CodeBlobClosure {
552 const HeapRegion* _hr;
553 bool _failures;
554 public:
555 VerifyStrongCodeRootCodeBlobClosure(const HeapRegion* hr) :
556 _hr(hr), _failures(false) {}
557
558 void do_code_blob(CodeBlob* cb) {
559 nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null();
560 if (nm != NULL) {
561 // Verify that the nemthod is live
562 if (!nm->is_alive()) {
563 log_error(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has dead nmethod " PTR_FORMAT " in its strong code roots",
564 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
565 _failures = true;
566 } else {
567 VerifyStrongCodeRootOopClosure oop_cl(_hr, nm);
568 nm->oops_do(&oop_cl);
569 if (!oop_cl.has_oops_in_region()) {
570 log_error(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has nmethod " PTR_FORMAT " in its strong code roots with no pointers into region",
571 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
572 _failures = true;
573 } else if (oop_cl.failures()) {
574 log_error(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] has other failures for nmethod " PTR_FORMAT,
575 p2i(_hr->bottom()), p2i(_hr->end()), p2i(nm));
576 _failures = true;
577 }
578 }
579 }
580 }
581
582 bool failures() { return _failures; }
583 };
584
585 void HeapRegion::verify_strong_code_roots(VerifyOption vo, bool* failures) const {
586 if (!G1VerifyHeapRegionCodeRoots) {
587 // We're not verifying code roots.
588 return;
589 }
590 if (vo == VerifyOption_G1UseMarkWord) {
591 // Marking verification during a full GC is performed after class
592 // unloading, code cache unloading, etc so the strong code roots
593 // attached to each heap region are in an inconsistent state. They won't
594 // be consistent until the strong code roots are rebuilt after the
595 // actual GC. Skip verifying the strong code roots in this particular
596 // time.
597 assert(VerifyDuringGC, "only way to get here");
598 return;
599 }
600
601 HeapRegionRemSet* hrrs = rem_set();
602 size_t strong_code_roots_length = hrrs->strong_code_roots_list_length();
603
604 // if this region is empty then there should be no entries
605 // on its strong code root list
606 if (is_empty()) {
607 if (strong_code_roots_length > 0) {
608 log_error(gc, verify)("region [" PTR_FORMAT "," PTR_FORMAT "] is empty but has " SIZE_FORMAT " code root entries",
609 p2i(bottom()), p2i(end()), strong_code_roots_length);
610 *failures = true;
611 }
612 return;
613 }
614
615 if (is_continues_humongous()) {
616 if (strong_code_roots_length > 0) {
617 log_error(gc, verify)("region " HR_FORMAT " is a continuation of a humongous region but has " SIZE_FORMAT " code root entries",
618 HR_FORMAT_PARAMS(this), strong_code_roots_length);
619 *failures = true;
620 }
621 return;
622 }
623
624 VerifyStrongCodeRootCodeBlobClosure cb_cl(this);
625 strong_code_roots_do(&cb_cl);
626
627 if (cb_cl.failures()) {
628 *failures = true;
629 }
630 }
631
632 void HeapRegion::print() const { print_on(tty); }
633 void HeapRegion::print_on(outputStream* st) const {
634 st->print("|%4u", this->_hrm_index);
635 st->print("|" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT,
636 p2i(bottom()), p2i(top()), p2i(end()));
637 st->print("|%3d%%", (int) ((double) used() * 100 / capacity()));
638 st->print("|%2s", get_short_type_str());
639 if (in_collection_set()) {
640 st->print("|CS");
641 } else {
642 st->print("| ");
643 }
644 st->print("|TS%3u", _gc_time_stamp);
645 st->print("|AC%3u", allocation_context());
646 st->print_cr("|TAMS " PTR_FORMAT ", " PTR_FORMAT "|",
647 p2i(prev_top_at_mark_start()), p2i(next_top_at_mark_start()));
648 }
649
650 class G1VerificationClosure : public OopClosure {
651 protected:
652 G1CollectedHeap* _g1h;
653 CardTableModRefBS* _bs;
654 oop _containing_obj;
655 bool _failures;
656 int _n_failures;
657 VerifyOption _vo;
658 public:
659 // _vo == UsePrevMarking -> use "prev" marking information,
660 // _vo == UseNextMarking -> use "next" marking information,
661 // _vo == UseMarkWord -> use mark word from object header.
662 G1VerificationClosure(G1CollectedHeap* g1h, VerifyOption vo) :
663 _g1h(g1h), _bs(barrier_set_cast<CardTableModRefBS>(g1h->barrier_set())),
664 _containing_obj(NULL), _failures(false), _n_failures(0), _vo(vo) {
665 }
666
667 void set_containing_obj(oop obj) {
668 _containing_obj = obj;
669 }
670
671 bool failures() { return _failures; }
672 int n_failures() { return _n_failures; }
673
674 void print_object(outputStream* out, oop obj) {
675 #ifdef PRODUCT
676 Klass* k = obj->klass();
677 const char* class_name = k->external_name();
678 out->print_cr("class name %s", class_name);
679 #else // PRODUCT
680 obj->print_on(out);
681 #endif // PRODUCT
682 }
683 };
684
685 class VerifyLiveClosure : public G1VerificationClosure {
686 public:
687 VerifyLiveClosure(G1CollectedHeap* g1h, VerifyOption vo) : G1VerificationClosure(g1h, vo) {}
688 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
689 virtual void do_oop(oop* p) { do_oop_work(p); }
690
691 template <class T>
692 void do_oop_work(T* p) {
693 assert(_containing_obj != NULL, "Precondition");
694 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
695 "Precondition");
696 verify_liveness(p);
697 }
698
699 template <class T>
700 void verify_liveness(T* p) {
701 T heap_oop = oopDesc::load_heap_oop(p);
702 Log(gc, verify) log;
703 if (!oopDesc::is_null(heap_oop)) {
704 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
705 bool failed = false;
706 if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) {
707 MutexLockerEx x(ParGCRareEvent_lock,
708 Mutex::_no_safepoint_check_flag);
709
710 if (!_failures) {
711 log.error("----------");
712 }
713 ResourceMark rm;
714 if (!_g1h->is_in_closed_subset(obj)) {
715 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
716 log.error("Field " PTR_FORMAT " of live obj " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ")",
717 p2i(p), p2i(_containing_obj), p2i(from->bottom()), p2i(from->end()));
718 print_object(log.error_stream(), _containing_obj);
719 log.error("points to obj " PTR_FORMAT " not in the heap", p2i(obj));
720 } else {
721 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
722 HeapRegion* to = _g1h->heap_region_containing((HeapWord*)obj);
723 log.error("Field " PTR_FORMAT " of live obj " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ")",
724 p2i(p), p2i(_containing_obj), p2i(from->bottom()), p2i(from->end()));
725 print_object(log.error_stream(), _containing_obj);
726 log.error("points to dead obj " PTR_FORMAT " in region [" PTR_FORMAT ", " PTR_FORMAT ")",
727 p2i(obj), p2i(to->bottom()), p2i(to->end()));
728 print_object(log.error_stream(), obj);
729 }
730 log.error("----------");
731 _failures = true;
732 failed = true;
733 _n_failures++;
734 }
735 }
736 }
737 };
738
739 class VerifyRemSetClosure : public G1VerificationClosure {
740 public:
741 VerifyRemSetClosure(G1CollectedHeap* g1h, VerifyOption vo) : G1VerificationClosure(g1h, vo) {}
742 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
743 virtual void do_oop(oop* p) { do_oop_work(p); }
744
745 template <class T>
746 void do_oop_work(T* p) {
747 assert(_containing_obj != NULL, "Precondition");
748 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
749 "Precondition");
750 verify_remembered_set(p);
751 }
752
753 template <class T>
754 void verify_remembered_set(T* p) {
755 T heap_oop = oopDesc::load_heap_oop(p);
756 Log(gc, verify) log;
757 if (!oopDesc::is_null(heap_oop)) {
758 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
759 bool failed = false;
760 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
761 HeapRegion* to = _g1h->heap_region_containing(obj);
762 if (from != NULL && to != NULL &&
763 from != to &&
764 !to->is_pinned()) {
765 jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
766 jbyte cv_field = *_bs->byte_for_const(p);
767 const jbyte dirty = CardTableModRefBS::dirty_card_val();
768
769 bool is_bad = !(from->is_young()
770 || to->rem_set()->contains_reference(p)
771 || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
772 (_containing_obj->is_objArray() ?
773 cv_field == dirty
774 : cv_obj == dirty || cv_field == dirty));
775 if (is_bad) {
776 MutexLockerEx x(ParGCRareEvent_lock,
777 Mutex::_no_safepoint_check_flag);
778
779 if (!_failures) {
780 log.error("----------");
781 }
782 log.error("Missing rem set entry:");
783 log.error("Field " PTR_FORMAT " of obj " PTR_FORMAT ", in region " HR_FORMAT,
784 p2i(p), p2i(_containing_obj), HR_FORMAT_PARAMS(from));
785 ResourceMark rm;
786 _containing_obj->print_on(log.error_stream());
787 log.error("points to obj " PTR_FORMAT " in region " HR_FORMAT, p2i(obj), HR_FORMAT_PARAMS(to));
788 if (obj->is_oop()) {
789 obj->print_on(log.error_stream());
790 }
791 log.error("Obj head CTE = %d, field CTE = %d.", cv_obj, cv_field);
792 log.error("----------");
793 _failures = true;
794 if (!failed) _n_failures++;
795 }
796 }
797 }
798 }
799 };
800
801 // This really ought to be commoned up into OffsetTableContigSpace somehow.
802 // We would need a mechanism to make that code skip dead objects.
803
804 void HeapRegion::verify(VerifyOption vo,
805 bool* failures) const {
806 G1CollectedHeap* g1 = G1CollectedHeap::heap();
807 *failures = false;
808 HeapWord* p = bottom();
809 HeapWord* prev_p = NULL;
810 VerifyLiveClosure vl_cl(g1, vo);
811 VerifyRemSetClosure vr_cl(g1, vo);
812 bool is_region_humongous = is_humongous();
813 size_t object_num = 0;
814 while (p < top()) {
815 oop obj = oop(p);
816 size_t obj_size = block_size(p);
817 object_num += 1;
818
819 if (!g1->is_obj_dead_cond(obj, this, vo)) {
820 if (obj->is_oop()) {
821 Klass* klass = obj->klass();
822 bool is_metaspace_object = Metaspace::contains(klass) ||
823 (vo == VerifyOption_G1UsePrevMarking &&
824 ClassLoaderDataGraph::unload_list_contains(klass));
825 if (!is_metaspace_object) {
826 log_error(gc, verify)("klass " PTR_FORMAT " of object " PTR_FORMAT " "
827 "not metadata", p2i(klass), p2i(obj));
828 *failures = true;
829 return;
830 } else if (!klass->is_klass()) {
831 log_error(gc, verify)("klass " PTR_FORMAT " of object " PTR_FORMAT " "
832 "not a klass", p2i(klass), p2i(obj));
833 *failures = true;
834 return;
835 } else {
836 vl_cl.set_containing_obj(obj);
837 if (!g1->collector_state()->full_collection() || G1VerifyRSetsDuringFullGC) {
838 // verify liveness and rem_set
839 vr_cl.set_containing_obj(obj);
840 G1Mux2Closure mux(&vl_cl, &vr_cl);
841 obj->oop_iterate_no_header(&mux);
842
843 if (vr_cl.failures()) {
844 *failures = true;
845 }
846 if (G1MaxVerifyFailures >= 0 &&
847 vr_cl.n_failures() >= G1MaxVerifyFailures) {
848 return;
849 }
850 } else {
851 // verify only liveness
852 obj->oop_iterate_no_header(&vl_cl);
853 }
854 if (vl_cl.failures()) {
855 *failures = true;
856 }
857 if (G1MaxVerifyFailures >= 0 &&
858 vl_cl.n_failures() >= G1MaxVerifyFailures) {
859 return;
860 }
861 }
862 } else {
863 log_error(gc, verify)(PTR_FORMAT " not an oop", p2i(obj));
864 *failures = true;
865 return;
866 }
867 }
868 prev_p = p;
869 p += obj_size;
870 }
871
872 if (!is_young() && !is_empty()) {
873 _bot_part.verify();
874 }
875
876 if (is_region_humongous) {
877 oop obj = oop(this->humongous_start_region()->bottom());
878 if ((HeapWord*)obj > bottom() || (HeapWord*)obj + obj->size() < bottom()) {
879 log_error(gc, verify)("this humongous region is not part of its' humongous object " PTR_FORMAT, p2i(obj));
880 *failures = true;
881 return;
882 }
883 }
884
885 if (!is_region_humongous && p != top()) {
886 log_error(gc, verify)("end of last object " PTR_FORMAT " "
887 "does not match top " PTR_FORMAT, p2i(p), p2i(top()));
888 *failures = true;
889 return;
890 }
891
892 HeapWord* the_end = end();
893 // Do some extra BOT consistency checking for addresses in the
894 // range [top, end). BOT look-ups in this range should yield
895 // top. No point in doing that if top == end (there's nothing there).
896 if (p < the_end) {
897 // Look up top
898 HeapWord* addr_1 = p;
899 HeapWord* b_start_1 = _bot_part.block_start_const(addr_1);
900 if (b_start_1 != p) {
901 log_error(gc, verify)("BOT look up for top: " PTR_FORMAT " "
902 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
903 p2i(addr_1), p2i(b_start_1), p2i(p));
904 *failures = true;
905 return;
906 }
907
908 // Look up top + 1
909 HeapWord* addr_2 = p + 1;
910 if (addr_2 < the_end) {
911 HeapWord* b_start_2 = _bot_part.block_start_const(addr_2);
912 if (b_start_2 != p) {
913 log_error(gc, verify)("BOT look up for top + 1: " PTR_FORMAT " "
914 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
915 p2i(addr_2), p2i(b_start_2), p2i(p));
916 *failures = true;
917 return;
918 }
919 }
920
921 // Look up an address between top and end
922 size_t diff = pointer_delta(the_end, p) / 2;
923 HeapWord* addr_3 = p + diff;
924 if (addr_3 < the_end) {
925 HeapWord* b_start_3 = _bot_part.block_start_const(addr_3);
926 if (b_start_3 != p) {
927 log_error(gc, verify)("BOT look up for top + diff: " PTR_FORMAT " "
928 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
929 p2i(addr_3), p2i(b_start_3), p2i(p));
930 *failures = true;
931 return;
932 }
933 }
934
935 // Look up end - 1
936 HeapWord* addr_4 = the_end - 1;
937 HeapWord* b_start_4 = _bot_part.block_start_const(addr_4);
938 if (b_start_4 != p) {
939 log_error(gc, verify)("BOT look up for end - 1: " PTR_FORMAT " "
940 " yielded " PTR_FORMAT ", expecting " PTR_FORMAT,
941 p2i(addr_4), p2i(b_start_4), p2i(p));
942 *failures = true;
943 return;
944 }
945 }
946
947 verify_strong_code_roots(vo, failures);
948 }
949
950 void HeapRegion::verify() const {
951 bool dummy = false;
952 verify(VerifyOption_G1UsePrevMarking, /* failures */ &dummy);
953 }
954
955 void HeapRegion::verify_rem_set(VerifyOption vo, bool* failures) const {
956 G1CollectedHeap* g1 = G1CollectedHeap::heap();
957 *failures = false;
958 HeapWord* p = bottom();
959 HeapWord* prev_p = NULL;
960 VerifyRemSetClosure vr_cl(g1, vo);
961 while (p < top()) {
962 oop obj = oop(p);
963 size_t obj_size = block_size(p);
964
965 if (!g1->is_obj_dead_cond(obj, this, vo)) {
966 if (obj->is_oop()) {
967 vr_cl.set_containing_obj(obj);
968 obj->oop_iterate_no_header(&vr_cl);
969
970 if (vr_cl.failures()) {
971 *failures = true;
972 }
973 if (G1MaxVerifyFailures >= 0 &&
974 vr_cl.n_failures() >= G1MaxVerifyFailures) {
975 return;
976 }
977 } else {
978 log_error(gc, verify)(PTR_FORMAT " not an oop", p2i(obj));
979 *failures = true;
980 return;
981 }
982 }
983
984 prev_p = p;
985 p += obj_size;
986 }
987 }
988
989 void HeapRegion::verify_rem_set() const {
990 bool failures = false;
991 verify_rem_set(VerifyOption_G1UsePrevMarking, &failures);
992 guarantee(!failures, "HeapRegion RemSet verification failed");
993 }
994
995 void HeapRegion::prepare_for_compaction(CompactPoint* cp) {
996 scan_and_forward(this, cp);
997 }
998
999 // G1OffsetTableContigSpace code; copied from space.cpp. Hope this can go
1000 // away eventually.
1001
1002 void G1ContiguousSpace::clear(bool mangle_space) {
1003 set_top(bottom());
1004 _scan_top = bottom();
1005 CompactibleSpace::clear(mangle_space);
1006 reset_bot();
1007 }
1008
1009 #ifndef PRODUCT
1010 void G1ContiguousSpace::mangle_unused_area() {
1011 mangle_unused_area_complete();
1012 }
1013
1014 void G1ContiguousSpace::mangle_unused_area_complete() {
1015 SpaceMangler::mangle_region(MemRegion(top(), end()));
1016 }
1017 #endif
1018
1019 void G1ContiguousSpace::print() const {
1020 print_short();
1021 tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
1022 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
1023 p2i(bottom()), p2i(top()), p2i(_bot_part.threshold()), p2i(end()));
1024 }
1025
1026 HeapWord* G1ContiguousSpace::initialize_threshold() {
1027 return _bot_part.initialize_threshold();
1028 }
1029
1030 HeapWord* G1ContiguousSpace::cross_threshold(HeapWord* start,
1031 HeapWord* end) {
1032 _bot_part.alloc_block(start, end);
1033 return _bot_part.threshold();
1034 }
1035
1036 HeapWord* G1ContiguousSpace::scan_top() const {
1037 G1CollectedHeap* g1h = G1CollectedHeap::heap();
1038 HeapWord* local_top = top();
1039 OrderAccess::loadload();
1040 const unsigned local_time_stamp = _gc_time_stamp;
1041 assert(local_time_stamp <= g1h->get_gc_time_stamp(), "invariant");
1042 if (local_time_stamp < g1h->get_gc_time_stamp()) {
1043 return local_top;
1044 } else {
1045 return _scan_top;
1046 }
1047 }
1048
1049 void G1ContiguousSpace::record_timestamp() {
1050 G1CollectedHeap* g1h = G1CollectedHeap::heap();
1051 uint curr_gc_time_stamp = g1h->get_gc_time_stamp();
1052
1053 if (_gc_time_stamp < curr_gc_time_stamp) {
1054 // Setting the time stamp here tells concurrent readers to look at
1055 // scan_top to know the maximum allowed address to look at.
1056
1057 // scan_top should be bottom for all regions except for the
1058 // retained old alloc region which should have scan_top == top
1059 HeapWord* st = _scan_top;
1060 guarantee(st == _bottom || st == _top, "invariant");
1061
1062 _gc_time_stamp = curr_gc_time_stamp;
1063 }
1064 }
1065
1066 void G1ContiguousSpace::record_retained_region() {
1067 // scan_top is the maximum address where it's safe for the next gc to
1068 // scan this region.
1069 _scan_top = top();
1070 }
1071
1072 void G1ContiguousSpace::safe_object_iterate(ObjectClosure* blk) {
1073 object_iterate(blk);
1074 }
1075
1076 void G1ContiguousSpace::object_iterate(ObjectClosure* blk) {
1077 HeapWord* p = bottom();
1078 while (p < top()) {
1079 if (block_is_obj(p)) {
1080 blk->do_object(oop(p));
1081 }
1082 p += block_size(p);
1083 }
1084 }
1085
1086 G1ContiguousSpace::G1ContiguousSpace(G1BlockOffsetTable* bot) :
1087 _bot_part(bot, this),
1088 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true),
1089 _gc_time_stamp(0)
1090 {
1091 }
1092
1093 void G1ContiguousSpace::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
1094 CompactibleSpace::initialize(mr, clear_space, mangle_space);
1095 _top = bottom();
1096 _scan_top = bottom();
1097 set_saved_mark_word(NULL);
1098 reset_bot();
1099 }
1100
--- EOF ---