src/share/vm/memory/cardTableRS.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 121
fc16fcee952c
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "memory/allocation.inline.hpp"
aoqi@0 27 #include "memory/cardTableRS.hpp"
aoqi@0 28 #include "memory/genCollectedHeap.hpp"
aoqi@0 29 #include "memory/generation.hpp"
aoqi@0 30 #include "memory/space.hpp"
aoqi@0 31 #include "oops/oop.inline.hpp"
aoqi@0 32 #include "runtime/java.hpp"
aoqi@0 33 #include "runtime/os.hpp"
aoqi@0 34 #include "utilities/macros.hpp"
aoqi@0 35 #if INCLUDE_ALL_GCS
aoqi@0 36 #include "gc_implementation/g1/concurrentMark.hpp"
aoqi@0 37 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
aoqi@0 38 #endif // INCLUDE_ALL_GCS
aoqi@0 39
aoqi@0 40 CardTableRS::CardTableRS(MemRegion whole_heap,
aoqi@0 41 int max_covered_regions) :
aoqi@0 42 GenRemSet(),
aoqi@0 43 _cur_youngergen_card_val(youngergenP1_card),
aoqi@0 44 _regions_to_iterate(max_covered_regions - 1)
aoqi@0 45 {
aoqi@0 46 #if INCLUDE_ALL_GCS
aoqi@0 47 if (UseG1GC) {
aoqi@0 48 _ct_bs = new G1SATBCardTableLoggingModRefBS(whole_heap,
aoqi@0 49 max_covered_regions);
aoqi@0 50 } else {
aoqi@0 51 _ct_bs = new CardTableModRefBSForCTRS(whole_heap, max_covered_regions);
aoqi@0 52 }
aoqi@0 53 #else
aoqi@0 54 _ct_bs = new CardTableModRefBSForCTRS(whole_heap, max_covered_regions);
aoqi@0 55 #endif
aoqi@0 56 set_bs(_ct_bs);
aoqi@0 57 _last_cur_val_in_gen = NEW_C_HEAP_ARRAY3(jbyte, GenCollectedHeap::max_gens + 1,
aoqi@0 58 mtGC, 0, AllocFailStrategy::RETURN_NULL);
aoqi@0 59 if (_last_cur_val_in_gen == NULL) {
aoqi@0 60 vm_exit_during_initialization("Could not create last_cur_val_in_gen array.");
aoqi@0 61 }
aoqi@0 62 for (int i = 0; i < GenCollectedHeap::max_gens + 1; i++) {
aoqi@0 63 _last_cur_val_in_gen[i] = clean_card_val();
aoqi@0 64 }
aoqi@0 65 _ct_bs->set_CTRS(this);
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 CardTableRS::~CardTableRS() {
aoqi@0 69 if (_ct_bs) {
aoqi@0 70 delete _ct_bs;
aoqi@0 71 _ct_bs = NULL;
aoqi@0 72 }
aoqi@0 73 if (_last_cur_val_in_gen) {
aoqi@0 74 FREE_C_HEAP_ARRAY(jbyte, _last_cur_val_in_gen, mtInternal);
aoqi@0 75 }
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 void CardTableRS::resize_covered_region(MemRegion new_region) {
aoqi@0 79 _ct_bs->resize_covered_region(new_region);
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 jbyte CardTableRS::find_unused_youngergenP_card_value() {
aoqi@0 83 for (jbyte v = youngergenP1_card;
aoqi@0 84 v < cur_youngergen_and_prev_nonclean_card;
aoqi@0 85 v++) {
aoqi@0 86 bool seen = false;
aoqi@0 87 for (int g = 0; g < _regions_to_iterate; g++) {
aoqi@0 88 if (_last_cur_val_in_gen[g] == v) {
aoqi@0 89 seen = true;
aoqi@0 90 break;
aoqi@0 91 }
aoqi@0 92 }
aoqi@0 93 if (!seen) return v;
aoqi@0 94 }
aoqi@0 95 ShouldNotReachHere();
aoqi@0 96 return 0;
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 void CardTableRS::prepare_for_younger_refs_iterate(bool parallel) {
aoqi@0 100 // Parallel or sequential, we must always set the prev to equal the
aoqi@0 101 // last one written.
aoqi@0 102 if (parallel) {
aoqi@0 103 // Find a parallel value to be used next.
aoqi@0 104 jbyte next_val = find_unused_youngergenP_card_value();
aoqi@0 105 set_cur_youngergen_card_val(next_val);
aoqi@0 106
aoqi@0 107 } else {
aoqi@0 108 // In an sequential traversal we will always write youngergen, so that
aoqi@0 109 // the inline barrier is correct.
aoqi@0 110 set_cur_youngergen_card_val(youngergen_card);
aoqi@0 111 }
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 void CardTableRS::younger_refs_iterate(Generation* g,
aoqi@0 115 OopsInGenClosure* blk) {
aoqi@0 116 _last_cur_val_in_gen[g->level()+1] = cur_youngergen_card_val();
aoqi@0 117 g->younger_refs_iterate(blk);
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 inline bool ClearNoncleanCardWrapper::clear_card(jbyte* entry) {
aoqi@0 121 if (_is_par) {
aoqi@0 122 return clear_card_parallel(entry);
aoqi@0 123 } else {
aoqi@0 124 return clear_card_serial(entry);
aoqi@0 125 }
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 inline bool ClearNoncleanCardWrapper::clear_card_parallel(jbyte* entry) {
aoqi@0 129 while (true) {
aoqi@0 130 // In the parallel case, we may have to do this several times.
aoqi@0 131 jbyte entry_val = *entry;
aoqi@0 132 assert(entry_val != CardTableRS::clean_card_val(),
aoqi@0 133 "We shouldn't be looking at clean cards, and this should "
aoqi@0 134 "be the only place they get cleaned.");
aoqi@0 135 if (CardTableRS::card_is_dirty_wrt_gen_iter(entry_val)
aoqi@0 136 || _ct->is_prev_youngergen_card_val(entry_val)) {
aoqi@0 137 jbyte res =
aoqi@0 138 Atomic::cmpxchg(CardTableRS::clean_card_val(), entry, entry_val);
aoqi@0 139 if (res == entry_val) {
aoqi@0 140 break;
aoqi@0 141 } else {
aoqi@0 142 assert(res == CardTableRS::cur_youngergen_and_prev_nonclean_card,
aoqi@0 143 "The CAS above should only fail if another thread did "
aoqi@0 144 "a GC write barrier.");
aoqi@0 145 }
aoqi@0 146 } else if (entry_val ==
aoqi@0 147 CardTableRS::cur_youngergen_and_prev_nonclean_card) {
aoqi@0 148 // Parallelism shouldn't matter in this case. Only the thread
aoqi@0 149 // assigned to scan the card should change this value.
aoqi@0 150 *entry = _ct->cur_youngergen_card_val();
aoqi@0 151 break;
aoqi@0 152 } else {
aoqi@0 153 assert(entry_val == _ct->cur_youngergen_card_val(),
aoqi@0 154 "Should be the only possibility.");
aoqi@0 155 // In this case, the card was clean before, and become
aoqi@0 156 // cur_youngergen only because of processing of a promoted object.
aoqi@0 157 // We don't have to look at the card.
aoqi@0 158 return false;
aoqi@0 159 }
aoqi@0 160 }
aoqi@0 161 return true;
aoqi@0 162 }
aoqi@0 163
aoqi@0 164
aoqi@0 165 inline bool ClearNoncleanCardWrapper::clear_card_serial(jbyte* entry) {
aoqi@0 166 jbyte entry_val = *entry;
aoqi@0 167 assert(entry_val != CardTableRS::clean_card_val(),
aoqi@0 168 "We shouldn't be looking at clean cards, and this should "
aoqi@0 169 "be the only place they get cleaned.");
aoqi@0 170 assert(entry_val != CardTableRS::cur_youngergen_and_prev_nonclean_card,
aoqi@0 171 "This should be possible in the sequential case.");
aoqi@0 172 *entry = CardTableRS::clean_card_val();
aoqi@0 173 return true;
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 ClearNoncleanCardWrapper::ClearNoncleanCardWrapper(
aoqi@0 177 DirtyCardToOopClosure* dirty_card_closure, CardTableRS* ct) :
aoqi@0 178 _dirty_card_closure(dirty_card_closure), _ct(ct) {
aoqi@0 179 // Cannot yet substitute active_workers for n_par_threads
aoqi@0 180 // in the case where parallelism is being turned off by
aoqi@0 181 // setting n_par_threads to 0.
aoqi@0 182 _is_par = (SharedHeap::heap()->n_par_threads() > 0);
aoqi@0 183 assert(!_is_par ||
aoqi@0 184 (SharedHeap::heap()->n_par_threads() ==
aoqi@0 185 SharedHeap::heap()->workers()->active_workers()), "Mismatch");
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 bool ClearNoncleanCardWrapper::is_word_aligned(jbyte* entry) {
aoqi@0 189 return (((intptr_t)entry) & (BytesPerWord-1)) == 0;
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 void ClearNoncleanCardWrapper::do_MemRegion(MemRegion mr) {
aoqi@0 193 assert(mr.word_size() > 0, "Error");
aoqi@0 194 assert(_ct->is_aligned(mr.start()), "mr.start() should be card aligned");
aoqi@0 195 // mr.end() may not necessarily be card aligned.
aoqi@0 196 jbyte* cur_entry = _ct->byte_for(mr.last());
aoqi@0 197 const jbyte* limit = _ct->byte_for(mr.start());
aoqi@0 198 HeapWord* end_of_non_clean = mr.end();
aoqi@0 199 HeapWord* start_of_non_clean = end_of_non_clean;
aoqi@0 200 while (cur_entry >= limit) {
aoqi@0 201 HeapWord* cur_hw = _ct->addr_for(cur_entry);
aoqi@0 202 if ((*cur_entry != CardTableRS::clean_card_val()) && clear_card(cur_entry)) {
aoqi@0 203 // Continue the dirty range by opening the
aoqi@0 204 // dirty window one card to the left.
aoqi@0 205 start_of_non_clean = cur_hw;
aoqi@0 206 } else {
aoqi@0 207 // We hit a "clean" card; process any non-empty
aoqi@0 208 // "dirty" range accumulated so far.
aoqi@0 209 if (start_of_non_clean < end_of_non_clean) {
aoqi@0 210 const MemRegion mrd(start_of_non_clean, end_of_non_clean);
aoqi@0 211 _dirty_card_closure->do_MemRegion(mrd);
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 // fast forward through potential continuous whole-word range of clean cards beginning at a word-boundary
aoqi@0 215 if (is_word_aligned(cur_entry)) {
aoqi@0 216 jbyte* cur_row = cur_entry - BytesPerWord;
aoqi@0 217 while (cur_row >= limit && *((intptr_t*)cur_row) == CardTableRS::clean_card_row()) {
aoqi@0 218 cur_row -= BytesPerWord;
aoqi@0 219 }
aoqi@0 220 cur_entry = cur_row + BytesPerWord;
aoqi@0 221 cur_hw = _ct->addr_for(cur_entry);
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 // Reset the dirty window, while continuing to look
aoqi@0 225 // for the next dirty card that will start a
aoqi@0 226 // new dirty window.
aoqi@0 227 end_of_non_clean = cur_hw;
aoqi@0 228 start_of_non_clean = cur_hw;
aoqi@0 229 }
aoqi@0 230 // Note that "cur_entry" leads "start_of_non_clean" in
aoqi@0 231 // its leftward excursion after this point
aoqi@0 232 // in the loop and, when we hit the left end of "mr",
aoqi@0 233 // will point off of the left end of the card-table
aoqi@0 234 // for "mr".
aoqi@0 235 cur_entry--;
aoqi@0 236 }
aoqi@0 237 // If the first card of "mr" was dirty, we will have
aoqi@0 238 // been left with a dirty window, co-initial with "mr",
aoqi@0 239 // which we now process.
aoqi@0 240 if (start_of_non_clean < end_of_non_clean) {
aoqi@0 241 const MemRegion mrd(start_of_non_clean, end_of_non_clean);
aoqi@0 242 _dirty_card_closure->do_MemRegion(mrd);
aoqi@0 243 }
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 // clean (by dirty->clean before) ==> cur_younger_gen
aoqi@0 247 // dirty ==> cur_youngergen_and_prev_nonclean_card
aoqi@0 248 // precleaned ==> cur_youngergen_and_prev_nonclean_card
aoqi@0 249 // prev-younger-gen ==> cur_youngergen_and_prev_nonclean_card
aoqi@0 250 // cur-younger-gen ==> cur_younger_gen
aoqi@0 251 // cur_youngergen_and_prev_nonclean_card ==> no change.
aoqi@0 252 void CardTableRS::write_ref_field_gc_par(void* field, oop new_val) {
aoqi@0 253 jbyte* entry = ct_bs()->byte_for(field);
fujie@116 254 #ifdef MIPS64
fujie@121 255 if (Use3A2000) OrderAccess::fence();
fujie@116 256 #endif
aoqi@0 257 do {
aoqi@0 258 jbyte entry_val = *entry;
aoqi@0 259 // We put this first because it's probably the most common case.
aoqi@0 260 if (entry_val == clean_card_val()) {
aoqi@0 261 // No threat of contention with cleaning threads.
aoqi@0 262 *entry = cur_youngergen_card_val();
aoqi@0 263 return;
aoqi@0 264 } else if (card_is_dirty_wrt_gen_iter(entry_val)
aoqi@0 265 || is_prev_youngergen_card_val(entry_val)) {
aoqi@0 266 // Mark it as both cur and prev youngergen; card cleaning thread will
aoqi@0 267 // eventually remove the previous stuff.
aoqi@0 268 jbyte new_val = cur_youngergen_and_prev_nonclean_card;
aoqi@0 269 jbyte res = Atomic::cmpxchg(new_val, entry, entry_val);
aoqi@0 270 // Did the CAS succeed?
fujie@116 271 if (res == entry_val) {
fujie@116 272 #ifdef MIPS64
fujie@121 273 if (Use3A2000) OrderAccess::fence();
fujie@116 274 #endif
fujie@116 275 return;
fujie@116 276 }
aoqi@0 277 // Otherwise, retry, to see the new value.
aoqi@0 278 continue;
aoqi@0 279 } else {
aoqi@0 280 assert(entry_val == cur_youngergen_and_prev_nonclean_card
aoqi@0 281 || entry_val == cur_youngergen_card_val(),
aoqi@0 282 "should be only possibilities.");
aoqi@0 283 return;
aoqi@0 284 }
aoqi@0 285 } while (true);
aoqi@0 286 }
aoqi@0 287
aoqi@0 288 void CardTableRS::younger_refs_in_space_iterate(Space* sp,
aoqi@0 289 OopsInGenClosure* cl) {
aoqi@0 290 const MemRegion urasm = sp->used_region_at_save_marks();
aoqi@0 291 #ifdef ASSERT
aoqi@0 292 // Convert the assertion check to a warning if we are running
aoqi@0 293 // CMS+ParNew until related bug is fixed.
aoqi@0 294 MemRegion ur = sp->used_region();
aoqi@0 295 assert(ur.contains(urasm) || (UseConcMarkSweepGC && UseParNewGC),
aoqi@0 296 err_msg("Did you forget to call save_marks()? "
aoqi@0 297 "[" PTR_FORMAT ", " PTR_FORMAT ") is not contained in "
aoqi@0 298 "[" PTR_FORMAT ", " PTR_FORMAT ")",
aoqi@0 299 p2i(urasm.start()), p2i(urasm.end()), p2i(ur.start()), p2i(ur.end())));
aoqi@0 300 // In the case of CMS+ParNew, issue a warning
aoqi@0 301 if (!ur.contains(urasm)) {
aoqi@0 302 assert(UseConcMarkSweepGC && UseParNewGC, "Tautology: see assert above");
aoqi@0 303 warning("CMS+ParNew: Did you forget to call save_marks()? "
aoqi@0 304 "[" PTR_FORMAT ", " PTR_FORMAT ") is not contained in "
aoqi@0 305 "[" PTR_FORMAT ", " PTR_FORMAT ")",
aoqi@0 306 p2i(urasm.start()), p2i(urasm.end()), p2i(ur.start()), p2i(ur.end()));
aoqi@0 307 MemRegion ur2 = sp->used_region();
aoqi@0 308 MemRegion urasm2 = sp->used_region_at_save_marks();
aoqi@0 309 if (!ur.equals(ur2)) {
aoqi@0 310 warning("CMS+ParNew: Flickering used_region()!!");
aoqi@0 311 }
aoqi@0 312 if (!urasm.equals(urasm2)) {
aoqi@0 313 warning("CMS+ParNew: Flickering used_region_at_save_marks()!!");
aoqi@0 314 }
aoqi@0 315 ShouldNotReachHere();
aoqi@0 316 }
aoqi@0 317 #endif
aoqi@0 318 _ct_bs->non_clean_card_iterate_possibly_parallel(sp, urasm, cl, this);
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 void CardTableRS::clear_into_younger(Generation* old_gen) {
aoqi@0 322 assert(old_gen->level() == 1, "Should only be called for the old generation");
aoqi@0 323 // The card tables for the youngest gen need never be cleared.
aoqi@0 324 // There's a bit of subtlety in the clear() and invalidate()
aoqi@0 325 // methods that we exploit here and in invalidate_or_clear()
aoqi@0 326 // below to avoid missing cards at the fringes. If clear() or
aoqi@0 327 // invalidate() are changed in the future, this code should
aoqi@0 328 // be revisited. 20040107.ysr
aoqi@0 329 clear(old_gen->prev_used_region());
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 void CardTableRS::invalidate_or_clear(Generation* old_gen) {
aoqi@0 333 assert(old_gen->level() == 1, "Should only be called for the old generation");
aoqi@0 334 // Invalidate the cards for the currently occupied part of
aoqi@0 335 // the old generation and clear the cards for the
aoqi@0 336 // unoccupied part of the generation (if any, making use
aoqi@0 337 // of that generation's prev_used_region to determine that
aoqi@0 338 // region). No need to do anything for the youngest
aoqi@0 339 // generation. Also see note#20040107.ysr above.
aoqi@0 340 MemRegion used_mr = old_gen->used_region();
aoqi@0 341 MemRegion to_be_cleared_mr = old_gen->prev_used_region().minus(used_mr);
aoqi@0 342 if (!to_be_cleared_mr.is_empty()) {
aoqi@0 343 clear(to_be_cleared_mr);
aoqi@0 344 }
aoqi@0 345 invalidate(used_mr);
aoqi@0 346 }
aoqi@0 347
aoqi@0 348
aoqi@0 349 class VerifyCleanCardClosure: public OopClosure {
aoqi@0 350 private:
aoqi@0 351 HeapWord* _boundary;
aoqi@0 352 HeapWord* _begin;
aoqi@0 353 HeapWord* _end;
aoqi@0 354 protected:
aoqi@0 355 template <class T> void do_oop_work(T* p) {
aoqi@0 356 HeapWord* jp = (HeapWord*)p;
aoqi@0 357 assert(jp >= _begin && jp < _end,
aoqi@0 358 err_msg("Error: jp " PTR_FORMAT " should be within "
aoqi@0 359 "[_begin, _end) = [" PTR_FORMAT "," PTR_FORMAT ")",
aoqi@0 360 p2i(jp), p2i(_begin), p2i(_end)));
aoqi@0 361 oop obj = oopDesc::load_decode_heap_oop(p);
aoqi@0 362 guarantee(obj == NULL || (HeapWord*)obj >= _boundary,
aoqi@0 363 err_msg("pointer " PTR_FORMAT " at " PTR_FORMAT " on "
aoqi@0 364 "clean card crosses boundary" PTR_FORMAT,
aoqi@0 365 p2i((HeapWord*)obj), p2i(jp), p2i(_boundary)));
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 public:
aoqi@0 369 VerifyCleanCardClosure(HeapWord* b, HeapWord* begin, HeapWord* end) :
aoqi@0 370 _boundary(b), _begin(begin), _end(end) {
aoqi@0 371 assert(b <= begin,
aoqi@0 372 err_msg("Error: boundary " PTR_FORMAT " should be at or below begin " PTR_FORMAT,
aoqi@0 373 p2i(b), p2i(begin)));
aoqi@0 374 assert(begin <= end,
aoqi@0 375 err_msg("Error: begin " PTR_FORMAT " should be strictly below end " PTR_FORMAT,
aoqi@0 376 p2i(begin), p2i(end)));
aoqi@0 377 }
aoqi@0 378
aoqi@0 379 virtual void do_oop(oop* p) { VerifyCleanCardClosure::do_oop_work(p); }
aoqi@0 380 virtual void do_oop(narrowOop* p) { VerifyCleanCardClosure::do_oop_work(p); }
aoqi@0 381 };
aoqi@0 382
aoqi@0 383 class VerifyCTSpaceClosure: public SpaceClosure {
aoqi@0 384 private:
aoqi@0 385 CardTableRS* _ct;
aoqi@0 386 HeapWord* _boundary;
aoqi@0 387 public:
aoqi@0 388 VerifyCTSpaceClosure(CardTableRS* ct, HeapWord* boundary) :
aoqi@0 389 _ct(ct), _boundary(boundary) {}
aoqi@0 390 virtual void do_space(Space* s) { _ct->verify_space(s, _boundary); }
aoqi@0 391 };
aoqi@0 392
aoqi@0 393 class VerifyCTGenClosure: public GenCollectedHeap::GenClosure {
aoqi@0 394 CardTableRS* _ct;
aoqi@0 395 public:
aoqi@0 396 VerifyCTGenClosure(CardTableRS* ct) : _ct(ct) {}
aoqi@0 397 void do_generation(Generation* gen) {
aoqi@0 398 // Skip the youngest generation.
aoqi@0 399 if (gen->level() == 0) return;
aoqi@0 400 // Normally, we're interested in pointers to younger generations.
aoqi@0 401 VerifyCTSpaceClosure blk(_ct, gen->reserved().start());
aoqi@0 402 gen->space_iterate(&blk, true);
aoqi@0 403 }
aoqi@0 404 };
aoqi@0 405
aoqi@0 406 void CardTableRS::verify_space(Space* s, HeapWord* gen_boundary) {
aoqi@0 407 // We don't need to do young-gen spaces.
aoqi@0 408 if (s->end() <= gen_boundary) return;
aoqi@0 409 MemRegion used = s->used_region();
aoqi@0 410
aoqi@0 411 jbyte* cur_entry = byte_for(used.start());
aoqi@0 412 jbyte* limit = byte_after(used.last());
aoqi@0 413 while (cur_entry < limit) {
aoqi@0 414 if (*cur_entry == CardTableModRefBS::clean_card) {
aoqi@0 415 jbyte* first_dirty = cur_entry+1;
aoqi@0 416 while (first_dirty < limit &&
aoqi@0 417 *first_dirty == CardTableModRefBS::clean_card) {
aoqi@0 418 first_dirty++;
aoqi@0 419 }
aoqi@0 420 // If the first object is a regular object, and it has a
aoqi@0 421 // young-to-old field, that would mark the previous card.
aoqi@0 422 HeapWord* boundary = addr_for(cur_entry);
aoqi@0 423 HeapWord* end = (first_dirty >= limit) ? used.end() : addr_for(first_dirty);
aoqi@0 424 HeapWord* boundary_block = s->block_start(boundary);
aoqi@0 425 HeapWord* begin = boundary; // Until proven otherwise.
aoqi@0 426 HeapWord* start_block = boundary_block; // Until proven otherwise.
aoqi@0 427 if (boundary_block < boundary) {
aoqi@0 428 if (s->block_is_obj(boundary_block) && s->obj_is_alive(boundary_block)) {
aoqi@0 429 oop boundary_obj = oop(boundary_block);
aoqi@0 430 if (!boundary_obj->is_objArray() &&
aoqi@0 431 !boundary_obj->is_typeArray()) {
aoqi@0 432 guarantee(cur_entry > byte_for(used.start()),
aoqi@0 433 "else boundary would be boundary_block");
aoqi@0 434 if (*byte_for(boundary_block) != CardTableModRefBS::clean_card) {
aoqi@0 435 begin = boundary_block + s->block_size(boundary_block);
aoqi@0 436 start_block = begin;
aoqi@0 437 }
aoqi@0 438 }
aoqi@0 439 }
aoqi@0 440 }
aoqi@0 441 // Now traverse objects until end.
aoqi@0 442 if (begin < end) {
aoqi@0 443 MemRegion mr(begin, end);
aoqi@0 444 VerifyCleanCardClosure verify_blk(gen_boundary, begin, end);
aoqi@0 445 for (HeapWord* cur = start_block; cur < end; cur += s->block_size(cur)) {
aoqi@0 446 if (s->block_is_obj(cur) && s->obj_is_alive(cur)) {
aoqi@0 447 oop(cur)->oop_iterate_no_header(&verify_blk, mr);
aoqi@0 448 }
aoqi@0 449 }
aoqi@0 450 }
aoqi@0 451 cur_entry = first_dirty;
aoqi@0 452 } else {
aoqi@0 453 // We'd normally expect that cur_youngergen_and_prev_nonclean_card
aoqi@0 454 // is a transient value, that cannot be in the card table
aoqi@0 455 // except during GC, and thus assert that:
aoqi@0 456 // guarantee(*cur_entry != cur_youngergen_and_prev_nonclean_card,
aoqi@0 457 // "Illegal CT value");
aoqi@0 458 // That however, need not hold, as will become clear in the
aoqi@0 459 // following...
aoqi@0 460
aoqi@0 461 // We'd normally expect that if we are in the parallel case,
aoqi@0 462 // we can't have left a prev value (which would be different
aoqi@0 463 // from the current value) in the card table, and so we'd like to
aoqi@0 464 // assert that:
aoqi@0 465 // guarantee(cur_youngergen_card_val() == youngergen_card
aoqi@0 466 // || !is_prev_youngergen_card_val(*cur_entry),
aoqi@0 467 // "Illegal CT value");
aoqi@0 468 // That, however, may not hold occasionally, because of
aoqi@0 469 // CMS or MSC in the old gen. To wit, consider the
aoqi@0 470 // following two simple illustrative scenarios:
aoqi@0 471 // (a) CMS: Consider the case where a large object L
aoqi@0 472 // spanning several cards is allocated in the old
aoqi@0 473 // gen, and has a young gen reference stored in it, dirtying
aoqi@0 474 // some interior cards. A young collection scans the card,
aoqi@0 475 // finds a young ref and installs a youngergenP_n value.
aoqi@0 476 // L then goes dead. Now a CMS collection starts,
aoqi@0 477 // finds L dead and sweeps it up. Assume that L is
aoqi@0 478 // abutting _unallocated_blk, so _unallocated_blk is
aoqi@0 479 // adjusted down to (below) L. Assume further that
aoqi@0 480 // no young collection intervenes during this CMS cycle.
aoqi@0 481 // The next young gen cycle will not get to look at this
aoqi@0 482 // youngergenP_n card since it lies in the unoccupied
aoqi@0 483 // part of the space.
aoqi@0 484 // Some young collections later the blocks on this
aoqi@0 485 // card can be re-allocated either due to direct allocation
aoqi@0 486 // or due to absorbing promotions. At this time, the
aoqi@0 487 // before-gc verification will fail the above assert.
aoqi@0 488 // (b) MSC: In this case, an object L with a young reference
aoqi@0 489 // is on a card that (therefore) holds a youngergen_n value.
aoqi@0 490 // Suppose also that L lies towards the end of the used
aoqi@0 491 // the used space before GC. An MSC collection
aoqi@0 492 // occurs that compacts to such an extent that this
aoqi@0 493 // card is no longer in the occupied part of the space.
aoqi@0 494 // Since current code in MSC does not always clear cards
aoqi@0 495 // in the unused part of old gen, this stale youngergen_n
aoqi@0 496 // value is left behind and can later be covered by
aoqi@0 497 // an object when promotion or direct allocation
aoqi@0 498 // re-allocates that part of the heap.
aoqi@0 499 //
aoqi@0 500 // Fortunately, the presence of such stale card values is
aoqi@0 501 // "only" a minor annoyance in that subsequent young collections
aoqi@0 502 // might needlessly scan such cards, but would still never corrupt
aoqi@0 503 // the heap as a result. However, it's likely not to be a significant
aoqi@0 504 // performance inhibitor in practice. For instance,
aoqi@0 505 // some recent measurements with unoccupied cards eagerly cleared
aoqi@0 506 // out to maintain this invariant, showed next to no
aoqi@0 507 // change in young collection times; of course one can construct
aoqi@0 508 // degenerate examples where the cost can be significant.)
aoqi@0 509 // Note, in particular, that if the "stale" card is modified
aoqi@0 510 // after re-allocation, it would be dirty, not "stale". Thus,
aoqi@0 511 // we can never have a younger ref in such a card and it is
aoqi@0 512 // safe not to scan that card in any collection. [As we see
aoqi@0 513 // below, we do some unnecessary scanning
aoqi@0 514 // in some cases in the current parallel scanning algorithm.]
aoqi@0 515 //
aoqi@0 516 // The main point below is that the parallel card scanning code
aoqi@0 517 // deals correctly with these stale card values. There are two main
aoqi@0 518 // cases to consider where we have a stale "younger gen" value and a
aoqi@0 519 // "derivative" case to consider, where we have a stale
aoqi@0 520 // "cur_younger_gen_and_prev_non_clean" value, as will become
aoqi@0 521 // apparent in the case analysis below.
aoqi@0 522 // o Case 1. If the stale value corresponds to a younger_gen_n
aoqi@0 523 // value other than the cur_younger_gen value then the code
aoqi@0 524 // treats this as being tantamount to a prev_younger_gen
aoqi@0 525 // card. This means that the card may be unnecessarily scanned.
aoqi@0 526 // There are two sub-cases to consider:
aoqi@0 527 // o Case 1a. Let us say that the card is in the occupied part
aoqi@0 528 // of the generation at the time the collection begins. In
aoqi@0 529 // that case the card will be either cleared when it is scanned
aoqi@0 530 // for young pointers, or will be set to cur_younger_gen as a
aoqi@0 531 // result of promotion. (We have elided the normal case where
aoqi@0 532 // the scanning thread and the promoting thread interleave
aoqi@0 533 // possibly resulting in a transient
aoqi@0 534 // cur_younger_gen_and_prev_non_clean value before settling
aoqi@0 535 // to cur_younger_gen. [End Case 1a.]
aoqi@0 536 // o Case 1b. Consider now the case when the card is in the unoccupied
aoqi@0 537 // part of the space which becomes occupied because of promotions
aoqi@0 538 // into it during the current young GC. In this case the card
aoqi@0 539 // will never be scanned for young references. The current
aoqi@0 540 // code will set the card value to either
aoqi@0 541 // cur_younger_gen_and_prev_non_clean or leave
aoqi@0 542 // it with its stale value -- because the promotions didn't
aoqi@0 543 // result in any younger refs on that card. Of these two
aoqi@0 544 // cases, the latter will be covered in Case 1a during
aoqi@0 545 // a subsequent scan. To deal with the former case, we need
aoqi@0 546 // to further consider how we deal with a stale value of
aoqi@0 547 // cur_younger_gen_and_prev_non_clean in our case analysis
aoqi@0 548 // below. This we do in Case 3 below. [End Case 1b]
aoqi@0 549 // [End Case 1]
aoqi@0 550 // o Case 2. If the stale value corresponds to cur_younger_gen being
aoqi@0 551 // a value not necessarily written by a current promotion, the
aoqi@0 552 // card will not be scanned by the younger refs scanning code.
aoqi@0 553 // (This is OK since as we argued above such cards cannot contain
aoqi@0 554 // any younger refs.) The result is that this value will be
aoqi@0 555 // treated as a prev_younger_gen value in a subsequent collection,
aoqi@0 556 // which is addressed in Case 1 above. [End Case 2]
aoqi@0 557 // o Case 3. We here consider the "derivative" case from Case 1b. above
aoqi@0 558 // because of which we may find a stale
aoqi@0 559 // cur_younger_gen_and_prev_non_clean card value in the table.
aoqi@0 560 // Once again, as in Case 1, we consider two subcases, depending
aoqi@0 561 // on whether the card lies in the occupied or unoccupied part
aoqi@0 562 // of the space at the start of the young collection.
aoqi@0 563 // o Case 3a. Let us say the card is in the occupied part of
aoqi@0 564 // the old gen at the start of the young collection. In that
aoqi@0 565 // case, the card will be scanned by the younger refs scanning
aoqi@0 566 // code which will set it to cur_younger_gen. In a subsequent
aoqi@0 567 // scan, the card will be considered again and get its final
aoqi@0 568 // correct value. [End Case 3a]
aoqi@0 569 // o Case 3b. Now consider the case where the card is in the
aoqi@0 570 // unoccupied part of the old gen, and is occupied as a result
aoqi@0 571 // of promotions during thus young gc. In that case,
aoqi@0 572 // the card will not be scanned for younger refs. The presence
aoqi@0 573 // of newly promoted objects on the card will then result in
aoqi@0 574 // its keeping the value cur_younger_gen_and_prev_non_clean
aoqi@0 575 // value, which we have dealt with in Case 3 here. [End Case 3b]
aoqi@0 576 // [End Case 3]
aoqi@0 577 //
aoqi@0 578 // (Please refer to the code in the helper class
aoqi@0 579 // ClearNonCleanCardWrapper and in CardTableModRefBS for details.)
aoqi@0 580 //
aoqi@0 581 // The informal arguments above can be tightened into a formal
aoqi@0 582 // correctness proof and it behooves us to write up such a proof,
aoqi@0 583 // or to use model checking to prove that there are no lingering
aoqi@0 584 // concerns.
aoqi@0 585 //
aoqi@0 586 // Clearly because of Case 3b one cannot bound the time for
aoqi@0 587 // which a card will retain what we have called a "stale" value.
aoqi@0 588 // However, one can obtain a Loose upper bound on the redundant
aoqi@0 589 // work as a result of such stale values. Note first that any
aoqi@0 590 // time a stale card lies in the occupied part of the space at
aoqi@0 591 // the start of the collection, it is scanned by younger refs
aoqi@0 592 // code and we can define a rank function on card values that
aoqi@0 593 // declines when this is so. Note also that when a card does not
aoqi@0 594 // lie in the occupied part of the space at the beginning of a
aoqi@0 595 // young collection, its rank can either decline or stay unchanged.
aoqi@0 596 // In this case, no extra work is done in terms of redundant
aoqi@0 597 // younger refs scanning of that card.
aoqi@0 598 // Then, the case analysis above reveals that, in the worst case,
aoqi@0 599 // any such stale card will be scanned unnecessarily at most twice.
aoqi@0 600 //
aoqi@0 601 // It is nonethelss advisable to try and get rid of some of this
aoqi@0 602 // redundant work in a subsequent (low priority) re-design of
aoqi@0 603 // the card-scanning code, if only to simplify the underlying
aoqi@0 604 // state machine analysis/proof. ysr 1/28/2002. XXX
aoqi@0 605 cur_entry++;
aoqi@0 606 }
aoqi@0 607 }
aoqi@0 608 }
aoqi@0 609
aoqi@0 610 void CardTableRS::verify() {
aoqi@0 611 // At present, we only know how to verify the card table RS for
aoqi@0 612 // generational heaps.
aoqi@0 613 VerifyCTGenClosure blk(this);
aoqi@0 614 CollectedHeap* ch = Universe::heap();
aoqi@0 615
aoqi@0 616 if (ch->kind() == CollectedHeap::GenCollectedHeap) {
aoqi@0 617 GenCollectedHeap::heap()->generation_iterate(&blk, false);
aoqi@0 618 _ct_bs->verify();
aoqi@0 619 }
aoqi@0 620 }
aoqi@0 621
aoqi@0 622
aoqi@0 623 void CardTableRS::verify_aligned_region_empty(MemRegion mr) {
aoqi@0 624 if (!mr.is_empty()) {
aoqi@0 625 jbyte* cur_entry = byte_for(mr.start());
aoqi@0 626 jbyte* limit = byte_after(mr.last());
aoqi@0 627 // The region mr may not start on a card boundary so
aoqi@0 628 // the first card may reflect a write to the space
aoqi@0 629 // just prior to mr.
aoqi@0 630 if (!is_aligned(mr.start())) {
aoqi@0 631 cur_entry++;
aoqi@0 632 }
aoqi@0 633 for (;cur_entry < limit; cur_entry++) {
aoqi@0 634 guarantee(*cur_entry == CardTableModRefBS::clean_card,
aoqi@0 635 "Unexpected dirty card found");
aoqi@0 636 }
aoqi@0 637 }
aoqi@0 638 }

mercurial