src/share/vm/memory/cardTableRS.cpp

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 9465
1556c6d89036
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

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

mercurial