annotate src/share/vm/gc_implementation/g1/heapRegion.cpp @ 896:df6caf649ff7

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