annotate src/hotspot/share/gc/shared/cardTableRS.cpp @ 52321:31b159f30fb2

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