src/share/vm/gc_implementation/g1/concurrentMark.cpp

Tue, 31 Mar 2015 11:36:37 +0200

author
tschatzl
date
Tue, 31 Mar 2015 11:36:37 +0200
changeset 7673
c04f46b4abe4
parent 7370
8d27d6113625
child 7535
7ae4e26cb1e0
child 7781
33e421924c67
permissions
-rw-r--r--

8068036: assert(is_available(index)) failed in G1 cset
Summary: Some verification code iterated over the heap using the region mapping array. This is not allowed. Changed to use the regular iteration method with closure.
Reviewed-by: jwilhelm, brutisso

ysr@777 1 /*
drchase@6680 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
ysr@777 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ysr@777 4 *
ysr@777 5 * This code is free software; you can redistribute it and/or modify it
ysr@777 6 * under the terms of the GNU General Public License version 2 only, as
ysr@777 7 * published by the Free Software Foundation.
ysr@777 8 *
ysr@777 9 * This code is distributed in the hope that it will be useful, but WITHOUT
ysr@777 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ysr@777 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ysr@777 12 * version 2 for more details (a copy is included in the LICENSE file that
ysr@777 13 * accompanied this code).
ysr@777 14 *
ysr@777 15 * You should have received a copy of the GNU General Public License version
ysr@777 16 * 2 along with this work; if not, write to the Free Software Foundation,
ysr@777 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ysr@777 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
ysr@777 22 *
ysr@777 23 */
ysr@777 24
stefank@2314 25 #include "precompiled.hpp"
stefank@7333 26 #include "classfile/metadataOnStackMark.hpp"
stefank@2314 27 #include "classfile/symbolTable.hpp"
stefank@6992 28 #include "code/codeCache.hpp"
tonyp@2968 29 #include "gc_implementation/g1/concurrentMark.inline.hpp"
stefank@2314 30 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"
stefank@2314 31 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
stefank@2314 32 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
tonyp@3114 33 #include "gc_implementation/g1/g1ErgoVerbose.hpp"
brutisso@3710 34 #include "gc_implementation/g1/g1Log.hpp"
tonyp@2968 35 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
stefank@2314 36 #include "gc_implementation/g1/g1RemSet.hpp"
tonyp@3416 37 #include "gc_implementation/g1/heapRegion.inline.hpp"
tschatzl@7091 38 #include "gc_implementation/g1/heapRegionManager.inline.hpp"
stefank@2314 39 #include "gc_implementation/g1/heapRegionRemSet.hpp"
tschatzl@7051 40 #include "gc_implementation/g1/heapRegionSet.inline.hpp"
kamg@2445 41 #include "gc_implementation/shared/vmGCOperations.hpp"
sla@5237 42 #include "gc_implementation/shared/gcTimer.hpp"
sla@5237 43 #include "gc_implementation/shared/gcTrace.hpp"
sla@5237 44 #include "gc_implementation/shared/gcTraceTime.hpp"
stefank@6992 45 #include "memory/allocation.hpp"
stefank@2314 46 #include "memory/genOopClosures.inline.hpp"
stefank@2314 47 #include "memory/referencePolicy.hpp"
stefank@2314 48 #include "memory/resourceArea.hpp"
stefank@2314 49 #include "oops/oop.inline.hpp"
stefank@2314 50 #include "runtime/handles.inline.hpp"
stefank@2314 51 #include "runtime/java.hpp"
goetz@6912 52 #include "runtime/prefetch.inline.hpp"
zgu@3900 53 #include "services/memTracker.hpp"
ysr@777 54
brutisso@3455 55 // Concurrent marking bit map wrapper
ysr@777 56
johnc@4333 57 CMBitMapRO::CMBitMapRO(int shifter) :
johnc@4333 58 _bm(),
ysr@777 59 _shifter(shifter) {
johnc@4333 60 _bmStartWord = 0;
johnc@4333 61 _bmWordSize = 0;
ysr@777 62 }
ysr@777 63
stefank@6992 64 HeapWord* CMBitMapRO::getNextMarkedWordAddress(const HeapWord* addr,
stefank@6992 65 const HeapWord* limit) const {
ysr@777 66 // First we must round addr *up* to a possible object boundary.
ysr@777 67 addr = (HeapWord*)align_size_up((intptr_t)addr,
ysr@777 68 HeapWordSize << _shifter);
ysr@777 69 size_t addrOffset = heapWordToOffset(addr);
tonyp@2973 70 if (limit == NULL) {
tonyp@2973 71 limit = _bmStartWord + _bmWordSize;
tonyp@2973 72 }
ysr@777 73 size_t limitOffset = heapWordToOffset(limit);
ysr@777 74 size_t nextOffset = _bm.get_next_one_offset(addrOffset, limitOffset);
ysr@777 75 HeapWord* nextAddr = offsetToHeapWord(nextOffset);
ysr@777 76 assert(nextAddr >= addr, "get_next_one postcondition");
ysr@777 77 assert(nextAddr == limit || isMarked(nextAddr),
ysr@777 78 "get_next_one postcondition");
ysr@777 79 return nextAddr;
ysr@777 80 }
ysr@777 81
stefank@6992 82 HeapWord* CMBitMapRO::getNextUnmarkedWordAddress(const HeapWord* addr,
stefank@6992 83 const HeapWord* limit) const {
ysr@777 84 size_t addrOffset = heapWordToOffset(addr);
tonyp@2973 85 if (limit == NULL) {
tonyp@2973 86 limit = _bmStartWord + _bmWordSize;
tonyp@2973 87 }
ysr@777 88 size_t limitOffset = heapWordToOffset(limit);
ysr@777 89 size_t nextOffset = _bm.get_next_zero_offset(addrOffset, limitOffset);
ysr@777 90 HeapWord* nextAddr = offsetToHeapWord(nextOffset);
ysr@777 91 assert(nextAddr >= addr, "get_next_one postcondition");
ysr@777 92 assert(nextAddr == limit || !isMarked(nextAddr),
ysr@777 93 "get_next_one postcondition");
ysr@777 94 return nextAddr;
ysr@777 95 }
ysr@777 96
ysr@777 97 int CMBitMapRO::heapWordDiffToOffsetDiff(size_t diff) const {
ysr@777 98 assert((diff & ((1 << _shifter) - 1)) == 0, "argument check");
ysr@777 99 return (int) (diff >> _shifter);
ysr@777 100 }
ysr@777 101
ysr@777 102 #ifndef PRODUCT
tschatzl@7051 103 bool CMBitMapRO::covers(MemRegion heap_rs) const {
ysr@777 104 // assert(_bm.map() == _virtual_space.low(), "map inconsistency");
brutisso@4061 105 assert(((size_t)_bm.size() * ((size_t)1 << _shifter)) == _bmWordSize,
ysr@777 106 "size inconsistency");
tschatzl@7051 107 return _bmStartWord == (HeapWord*)(heap_rs.start()) &&
tschatzl@7051 108 _bmWordSize == heap_rs.word_size();
ysr@777 109 }
ysr@777 110 #endif
ysr@777 111
stefank@4904 112 void CMBitMapRO::print_on_error(outputStream* st, const char* prefix) const {
stefank@4904 113 _bm.print_on_error(st, prefix);
stefank@4904 114 }
stefank@4904 115
tschatzl@7051 116 size_t CMBitMap::compute_size(size_t heap_size) {
tschatzl@7051 117 return heap_size / mark_distance();
tschatzl@7051 118 }
tschatzl@7051 119
tschatzl@7051 120 size_t CMBitMap::mark_distance() {
tschatzl@7051 121 return MinObjAlignmentInBytes * BitsPerByte;
tschatzl@7051 122 }
tschatzl@7051 123
tschatzl@7051 124 void CMBitMap::initialize(MemRegion heap, G1RegionToSpaceMapper* storage) {
tschatzl@7051 125 _bmStartWord = heap.start();
tschatzl@7051 126 _bmWordSize = heap.word_size();
tschatzl@7051 127
tschatzl@7051 128 _bm.set_map((BitMap::bm_word_t*) storage->reserved().start());
tschatzl@7051 129 _bm.set_size(_bmWordSize >> _shifter);
tschatzl@7051 130
tschatzl@7051 131 storage->set_mapping_changed_listener(&_listener);
tschatzl@7051 132 }
tschatzl@7051 133
tschatzl@7257 134 void CMBitMapMappingChangedListener::on_commit(uint start_region, size_t num_regions, bool zero_filled) {
tschatzl@7257 135 if (zero_filled) {
tschatzl@7257 136 return;
tschatzl@7257 137 }
tschatzl@7051 138 // We need to clear the bitmap on commit, removing any existing information.
tschatzl@7051 139 MemRegion mr(G1CollectedHeap::heap()->bottom_addr_for_region(start_region), num_regions * HeapRegion::GrainWords);
tschatzl@7051 140 _bm->clearRange(mr);
tschatzl@7051 141 }
tschatzl@7051 142
tschatzl@7051 143 // Closure used for clearing the given mark bitmap.
tschatzl@7051 144 class ClearBitmapHRClosure : public HeapRegionClosure {
tschatzl@7051 145 private:
tschatzl@7051 146 ConcurrentMark* _cm;
tschatzl@7051 147 CMBitMap* _bitmap;
tschatzl@7051 148 bool _may_yield; // The closure may yield during iteration. If yielded, abort the iteration.
tschatzl@7051 149 public:
tschatzl@7051 150 ClearBitmapHRClosure(ConcurrentMark* cm, CMBitMap* bitmap, bool may_yield) : HeapRegionClosure(), _cm(cm), _bitmap(bitmap), _may_yield(may_yield) {
tschatzl@7051 151 assert(!may_yield || cm != NULL, "CM must be non-NULL if this closure is expected to yield.");
tschatzl@7051 152 }
tschatzl@7051 153
tschatzl@7051 154 virtual bool doHeapRegion(HeapRegion* r) {
tschatzl@7051 155 size_t const chunk_size_in_words = M / HeapWordSize;
tschatzl@7051 156
tschatzl@7051 157 HeapWord* cur = r->bottom();
tschatzl@7051 158 HeapWord* const end = r->end();
tschatzl@7051 159
tschatzl@7051 160 while (cur < end) {
tschatzl@7051 161 MemRegion mr(cur, MIN2(cur + chunk_size_in_words, end));
tschatzl@7051 162 _bitmap->clearRange(mr);
tschatzl@7051 163
tschatzl@7051 164 cur += chunk_size_in_words;
tschatzl@7051 165
tschatzl@7051 166 // Abort iteration if after yielding the marking has been aborted.
tschatzl@7051 167 if (_may_yield && _cm->do_yield_check() && _cm->has_aborted()) {
tschatzl@7051 168 return true;
tschatzl@7051 169 }
tschatzl@7051 170 // Repeat the asserts from before the start of the closure. We will do them
tschatzl@7051 171 // as asserts here to minimize their overhead on the product. However, we
tschatzl@7051 172 // will have them as guarantees at the beginning / end of the bitmap
tschatzl@7051 173 // clearing to get some checking in the product.
tschatzl@7051 174 assert(!_may_yield || _cm->cmThread()->during_cycle(), "invariant");
tschatzl@7051 175 assert(!_may_yield || !G1CollectedHeap::heap()->mark_in_progress(), "invariant");
tschatzl@7051 176 }
tschatzl@7051 177
johnc@4333 178 return false;
johnc@4333 179 }
tschatzl@7051 180 };
johnc@4333 181
ysr@777 182 void CMBitMap::clearAll() {
tschatzl@7051 183 ClearBitmapHRClosure cl(NULL, this, false /* may_yield */);
tschatzl@7051 184 G1CollectedHeap::heap()->heap_region_iterate(&cl);
tschatzl@7051 185 guarantee(cl.complete(), "Must have completed iteration.");
ysr@777 186 return;
ysr@777 187 }
ysr@777 188
ysr@777 189 void CMBitMap::markRange(MemRegion mr) {
ysr@777 190 mr.intersection(MemRegion(_bmStartWord, _bmWordSize));
ysr@777 191 assert(!mr.is_empty(), "unexpected empty region");
ysr@777 192 assert((offsetToHeapWord(heapWordToOffset(mr.end())) ==
ysr@777 193 ((HeapWord *) mr.end())),
ysr@777 194 "markRange memory region end is not card aligned");
ysr@777 195 // convert address range into offset range
ysr@777 196 _bm.at_put_range(heapWordToOffset(mr.start()),
ysr@777 197 heapWordToOffset(mr.end()), true);
ysr@777 198 }
ysr@777 199
ysr@777 200 void CMBitMap::clearRange(MemRegion mr) {
ysr@777 201 mr.intersection(MemRegion(_bmStartWord, _bmWordSize));
ysr@777 202 assert(!mr.is_empty(), "unexpected empty region");
ysr@777 203 // convert address range into offset range
ysr@777 204 _bm.at_put_range(heapWordToOffset(mr.start()),
ysr@777 205 heapWordToOffset(mr.end()), false);
ysr@777 206 }
ysr@777 207
ysr@777 208 MemRegion CMBitMap::getAndClearMarkedRegion(HeapWord* addr,
ysr@777 209 HeapWord* end_addr) {
ysr@777 210 HeapWord* start = getNextMarkedWordAddress(addr);
ysr@777 211 start = MIN2(start, end_addr);
ysr@777 212 HeapWord* end = getNextUnmarkedWordAddress(start);
ysr@777 213 end = MIN2(end, end_addr);
ysr@777 214 assert(start <= end, "Consistency check");
ysr@777 215 MemRegion mr(start, end);
ysr@777 216 if (!mr.is_empty()) {
ysr@777 217 clearRange(mr);
ysr@777 218 }
ysr@777 219 return mr;
ysr@777 220 }
ysr@777 221
ysr@777 222 CMMarkStack::CMMarkStack(ConcurrentMark* cm) :
ysr@777 223 _base(NULL), _cm(cm)
ysr@777 224 #ifdef ASSERT
ysr@777 225 , _drain_in_progress(false)
ysr@777 226 , _drain_in_progress_yields(false)
ysr@777 227 #endif
ysr@777 228 {}
ysr@777 229
johnc@4333 230 bool CMMarkStack::allocate(size_t capacity) {
johnc@4333 231 // allocate a stack of the requisite depth
johnc@4333 232 ReservedSpace rs(ReservedSpace::allocation_align_size_up(capacity * sizeof(oop)));
johnc@4333 233 if (!rs.is_reserved()) {
johnc@4333 234 warning("ConcurrentMark MarkStack allocation failure");
johnc@4333 235 return false;
tonyp@2973 236 }
johnc@4333 237 MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);
johnc@4333 238 if (!_virtual_space.initialize(rs, rs.size())) {
johnc@4333 239 warning("ConcurrentMark MarkStack backing store failure");
johnc@4333 240 // Release the virtual memory reserved for the marking stack
johnc@4333 241 rs.release();
johnc@4333 242 return false;
johnc@4333 243 }
johnc@4333 244 assert(_virtual_space.committed_size() == rs.size(),
johnc@4333 245 "Didn't reserve backing store for all of ConcurrentMark stack?");
johnc@4333 246 _base = (oop*) _virtual_space.low();
johnc@4333 247 setEmpty();
johnc@4333 248 _capacity = (jint) capacity;
tonyp@3416 249 _saved_index = -1;
johnc@4386 250 _should_expand = false;
ysr@777 251 NOT_PRODUCT(_max_depth = 0);
johnc@4333 252 return true;
johnc@4333 253 }
johnc@4333 254
johnc@4333 255 void CMMarkStack::expand() {
johnc@4333 256 // Called, during remark, if we've overflown the marking stack during marking.
johnc@4333 257 assert(isEmpty(), "stack should been emptied while handling overflow");
johnc@4333 258 assert(_capacity <= (jint) MarkStackSizeMax, "stack bigger than permitted");
johnc@4333 259 // Clear expansion flag
johnc@4333 260 _should_expand = false;
johnc@4333 261 if (_capacity == (jint) MarkStackSizeMax) {
johnc@4333 262 if (PrintGCDetails && Verbose) {
johnc@4333 263 gclog_or_tty->print_cr(" (benign) Can't expand marking stack capacity, at max size limit");
johnc@4333 264 }
johnc@4333 265 return;
johnc@4333 266 }
johnc@4333 267 // Double capacity if possible
johnc@4333 268 jint new_capacity = MIN2(_capacity*2, (jint) MarkStackSizeMax);
johnc@4333 269 // Do not give up existing stack until we have managed to
johnc@4333 270 // get the double capacity that we desired.
johnc@4333 271 ReservedSpace rs(ReservedSpace::allocation_align_size_up(new_capacity *
johnc@4333 272 sizeof(oop)));
johnc@4333 273 if (rs.is_reserved()) {
johnc@4333 274 // Release the backing store associated with old stack
johnc@4333 275 _virtual_space.release();
johnc@4333 276 // Reinitialize virtual space for new stack
johnc@4333 277 if (!_virtual_space.initialize(rs, rs.size())) {
johnc@4333 278 fatal("Not enough swap for expanded marking stack capacity");
johnc@4333 279 }
johnc@4333 280 _base = (oop*)(_virtual_space.low());
johnc@4333 281 _index = 0;
johnc@4333 282 _capacity = new_capacity;
johnc@4333 283 } else {
johnc@4333 284 if (PrintGCDetails && Verbose) {
johnc@4333 285 // Failed to double capacity, continue;
johnc@4333 286 gclog_or_tty->print(" (benign) Failed to expand marking stack capacity from "
johnc@4333 287 SIZE_FORMAT"K to " SIZE_FORMAT"K",
johnc@4333 288 _capacity / K, new_capacity / K);
johnc@4333 289 }
johnc@4333 290 }
johnc@4333 291 }
johnc@4333 292
johnc@4333 293 void CMMarkStack::set_should_expand() {
johnc@4333 294 // If we're resetting the marking state because of an
johnc@4333 295 // marking stack overflow, record that we should, if
johnc@4333 296 // possible, expand the stack.
johnc@4333 297 _should_expand = _cm->has_overflown();
ysr@777 298 }
ysr@777 299
ysr@777 300 CMMarkStack::~CMMarkStack() {
tonyp@2973 301 if (_base != NULL) {
johnc@4333 302 _base = NULL;
johnc@4333 303 _virtual_space.release();
tonyp@2973 304 }
ysr@777 305 }
ysr@777 306
ysr@777 307 void CMMarkStack::par_push(oop ptr) {
ysr@777 308 while (true) {
ysr@777 309 if (isFull()) {
ysr@777 310 _overflow = true;
ysr@777 311 return;
ysr@777 312 }
ysr@777 313 // Otherwise...
ysr@777 314 jint index = _index;
ysr@777 315 jint next_index = index+1;
ysr@777 316 jint res = Atomic::cmpxchg(next_index, &_index, index);
ysr@777 317 if (res == index) {
ysr@777 318 _base[index] = ptr;
ysr@777 319 // Note that we don't maintain this atomically. We could, but it
ysr@777 320 // doesn't seem necessary.
ysr@777 321 NOT_PRODUCT(_max_depth = MAX2(_max_depth, next_index));
ysr@777 322 return;
ysr@777 323 }
ysr@777 324 // Otherwise, we need to try again.
ysr@777 325 }
ysr@777 326 }
ysr@777 327
ysr@777 328 void CMMarkStack::par_adjoin_arr(oop* ptr_arr, int n) {
ysr@777 329 while (true) {
ysr@777 330 if (isFull()) {
ysr@777 331 _overflow = true;
ysr@777 332 return;
ysr@777 333 }
ysr@777 334 // Otherwise...
ysr@777 335 jint index = _index;
ysr@777 336 jint next_index = index + n;
ysr@777 337 if (next_index > _capacity) {
ysr@777 338 _overflow = true;
ysr@777 339 return;
ysr@777 340 }
ysr@777 341 jint res = Atomic::cmpxchg(next_index, &_index, index);
ysr@777 342 if (res == index) {
ysr@777 343 for (int i = 0; i < n; i++) {
johnc@4333 344 int ind = index + i;
ysr@777 345 assert(ind < _capacity, "By overflow test above.");
ysr@777 346 _base[ind] = ptr_arr[i];
ysr@777 347 }
ysr@777 348 NOT_PRODUCT(_max_depth = MAX2(_max_depth, next_index));
ysr@777 349 return;
ysr@777 350 }
ysr@777 351 // Otherwise, we need to try again.
ysr@777 352 }
ysr@777 353 }
ysr@777 354
ysr@777 355 void CMMarkStack::par_push_arr(oop* ptr_arr, int n) {
ysr@777 356 MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);
ysr@777 357 jint start = _index;
ysr@777 358 jint next_index = start + n;
ysr@777 359 if (next_index > _capacity) {
ysr@777 360 _overflow = true;
ysr@777 361 return;
ysr@777 362 }
ysr@777 363 // Otherwise.
ysr@777 364 _index = next_index;
ysr@777 365 for (int i = 0; i < n; i++) {
ysr@777 366 int ind = start + i;
tonyp@1458 367 assert(ind < _capacity, "By overflow test above.");
ysr@777 368 _base[ind] = ptr_arr[i];
ysr@777 369 }
johnc@4333 370 NOT_PRODUCT(_max_depth = MAX2(_max_depth, next_index));
ysr@777 371 }
ysr@777 372
ysr@777 373 bool CMMarkStack::par_pop_arr(oop* ptr_arr, int max, int* n) {
ysr@777 374 MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);
ysr@777 375 jint index = _index;
ysr@777 376 if (index == 0) {
ysr@777 377 *n = 0;
ysr@777 378 return false;
ysr@777 379 } else {
ysr@777 380 int k = MIN2(max, index);
johnc@4333 381 jint new_ind = index - k;
ysr@777 382 for (int j = 0; j < k; j++) {
ysr@777 383 ptr_arr[j] = _base[new_ind + j];
ysr@777 384 }
ysr@777 385 _index = new_ind;
ysr@777 386 *n = k;
ysr@777 387 return true;
ysr@777 388 }
ysr@777 389 }
ysr@777 390
ysr@777 391 template<class OopClosureClass>
ysr@777 392 bool CMMarkStack::drain(OopClosureClass* cl, CMBitMap* bm, bool yield_after) {
ysr@777 393 assert(!_drain_in_progress || !_drain_in_progress_yields || yield_after
ysr@777 394 || SafepointSynchronize::is_at_safepoint(),
ysr@777 395 "Drain recursion must be yield-safe.");
ysr@777 396 bool res = true;
ysr@777 397 debug_only(_drain_in_progress = true);
ysr@777 398 debug_only(_drain_in_progress_yields = yield_after);
ysr@777 399 while (!isEmpty()) {
ysr@777 400 oop newOop = pop();
ysr@777 401 assert(G1CollectedHeap::heap()->is_in_reserved(newOop), "Bad pop");
ysr@777 402 assert(newOop->is_oop(), "Expected an oop");
ysr@777 403 assert(bm == NULL || bm->isMarked((HeapWord*)newOop),
ysr@777 404 "only grey objects on this stack");
ysr@777 405 newOop->oop_iterate(cl);
ysr@777 406 if (yield_after && _cm->do_yield_check()) {
tonyp@2973 407 res = false;
tonyp@2973 408 break;
ysr@777 409 }
ysr@777 410 }
ysr@777 411 debug_only(_drain_in_progress = false);
ysr@777 412 return res;
ysr@777 413 }
ysr@777 414
tonyp@3416 415 void CMMarkStack::note_start_of_gc() {
tonyp@3416 416 assert(_saved_index == -1,
tonyp@3416 417 "note_start_of_gc()/end_of_gc() bracketed incorrectly");
tonyp@3416 418 _saved_index = _index;
tonyp@3416 419 }
tonyp@3416 420
tonyp@3416 421 void CMMarkStack::note_end_of_gc() {
tonyp@3416 422 // This is intentionally a guarantee, instead of an assert. If we
tonyp@3416 423 // accidentally add something to the mark stack during GC, it
tonyp@3416 424 // will be a correctness issue so it's better if we crash. we'll
tonyp@3416 425 // only check this once per GC anyway, so it won't be a performance
tonyp@3416 426 // issue in any way.
tonyp@3416 427 guarantee(_saved_index == _index,
tonyp@3416 428 err_msg("saved index: %d index: %d", _saved_index, _index));
tonyp@3416 429 _saved_index = -1;
tonyp@3416 430 }
tonyp@3416 431
ysr@777 432 void CMMarkStack::oops_do(OopClosure* f) {
tonyp@3416 433 assert(_saved_index == _index,
tonyp@3416 434 err_msg("saved index: %d index: %d", _saved_index, _index));
tonyp@3416 435 for (int i = 0; i < _index; i += 1) {
ysr@777 436 f->do_oop(&_base[i]);
ysr@777 437 }
ysr@777 438 }
ysr@777 439
tonyp@3464 440 CMRootRegions::CMRootRegions() :
tonyp@3464 441 _young_list(NULL), _cm(NULL), _scan_in_progress(false),
tonyp@3464 442 _should_abort(false), _next_survivor(NULL) { }
tonyp@3464 443
tonyp@3464 444 void CMRootRegions::init(G1CollectedHeap* g1h, ConcurrentMark* cm) {
tonyp@3464 445 _young_list = g1h->young_list();
tonyp@3464 446 _cm = cm;
tonyp@3464 447 }
tonyp@3464 448
tonyp@3464 449 void CMRootRegions::prepare_for_scan() {
tonyp@3464 450 assert(!scan_in_progress(), "pre-condition");
tonyp@3464 451
tonyp@3464 452 // Currently, only survivors can be root regions.
tonyp@3464 453 assert(_next_survivor == NULL, "pre-condition");
tonyp@3464 454 _next_survivor = _young_list->first_survivor_region();
tonyp@3464 455 _scan_in_progress = (_next_survivor != NULL);
tonyp@3464 456 _should_abort = false;
tonyp@3464 457 }
tonyp@3464 458
tonyp@3464 459 HeapRegion* CMRootRegions::claim_next() {
tonyp@3464 460 if (_should_abort) {
tonyp@3464 461 // If someone has set the should_abort flag, we return NULL to
tonyp@3464 462 // force the caller to bail out of their loop.
tonyp@3464 463 return NULL;
tonyp@3464 464 }
tonyp@3464 465
tonyp@3464 466 // Currently, only survivors can be root regions.
tonyp@3464 467 HeapRegion* res = _next_survivor;
tonyp@3464 468 if (res != NULL) {
tonyp@3464 469 MutexLockerEx x(RootRegionScan_lock, Mutex::_no_safepoint_check_flag);
tonyp@3464 470 // Read it again in case it changed while we were waiting for the lock.
tonyp@3464 471 res = _next_survivor;
tonyp@3464 472 if (res != NULL) {
tonyp@3464 473 if (res == _young_list->last_survivor_region()) {
tonyp@3464 474 // We just claimed the last survivor so store NULL to indicate
tonyp@3464 475 // that we're done.
tonyp@3464 476 _next_survivor = NULL;
tonyp@3464 477 } else {
tonyp@3464 478 _next_survivor = res->get_next_young_region();
tonyp@3464 479 }
tonyp@3464 480 } else {
tonyp@3464 481 // Someone else claimed the last survivor while we were trying
tonyp@3464 482 // to take the lock so nothing else to do.
tonyp@3464 483 }
tonyp@3464 484 }
tonyp@3464 485 assert(res == NULL || res->is_survivor(), "post-condition");
tonyp@3464 486
tonyp@3464 487 return res;
tonyp@3464 488 }
tonyp@3464 489
tonyp@3464 490 void CMRootRegions::scan_finished() {
tonyp@3464 491 assert(scan_in_progress(), "pre-condition");
tonyp@3464 492
tonyp@3464 493 // Currently, only survivors can be root regions.
tonyp@3464 494 if (!_should_abort) {
tonyp@3464 495 assert(_next_survivor == NULL, "we should have claimed all survivors");
tonyp@3464 496 }
tonyp@3464 497 _next_survivor = NULL;
tonyp@3464 498
tonyp@3464 499 {
tonyp@3464 500 MutexLockerEx x(RootRegionScan_lock, Mutex::_no_safepoint_check_flag);
tonyp@3464 501 _scan_in_progress = false;
tonyp@3464 502 RootRegionScan_lock->notify_all();
tonyp@3464 503 }
tonyp@3464 504 }
tonyp@3464 505
tonyp@3464 506 bool CMRootRegions::wait_until_scan_finished() {
tonyp@3464 507 if (!scan_in_progress()) return false;
tonyp@3464 508
tonyp@3464 509 {
tonyp@3464 510 MutexLockerEx x(RootRegionScan_lock, Mutex::_no_safepoint_check_flag);
tonyp@3464 511 while (scan_in_progress()) {
tonyp@3464 512 RootRegionScan_lock->wait(Mutex::_no_safepoint_check_flag);
tonyp@3464 513 }
tonyp@3464 514 }
tonyp@3464 515 return true;
tonyp@3464 516 }
tonyp@3464 517
ysr@777 518 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
ysr@777 519 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
ysr@777 520 #endif // _MSC_VER
ysr@777 521
jmasa@3357 522 uint ConcurrentMark::scale_parallel_threads(uint n_par_threads) {
jmasa@3357 523 return MAX2((n_par_threads + 2) / 4, 1U);
jmasa@3294 524 }
jmasa@3294 525
tschatzl@7051 526 ConcurrentMark::ConcurrentMark(G1CollectedHeap* g1h, G1RegionToSpaceMapper* prev_bitmap_storage, G1RegionToSpaceMapper* next_bitmap_storage) :
johnc@4333 527 _g1h(g1h),
tschatzl@7051 528 _markBitMap1(),
tschatzl@7051 529 _markBitMap2(),
ysr@777 530 _parallel_marking_threads(0),
jmasa@3294 531 _max_parallel_marking_threads(0),
ysr@777 532 _sleep_factor(0.0),
ysr@777 533 _marking_task_overhead(1.0),
ysr@777 534 _cleanup_sleep_factor(0.0),
ysr@777 535 _cleanup_task_overhead(1.0),
tonyp@2472 536 _cleanup_list("Cleanup List"),
johnc@4333 537 _region_bm((BitMap::idx_t)(g1h->max_regions()), false /* in_resource_area*/),
tschatzl@7051 538 _card_bm((g1h->reserved_region().byte_size() + CardTableModRefBS::card_size - 1) >>
johnc@4333 539 CardTableModRefBS::card_shift,
johnc@4333 540 false /* in_resource_area*/),
johnc@3463 541
ysr@777 542 _prevMarkBitMap(&_markBitMap1),
ysr@777 543 _nextMarkBitMap(&_markBitMap2),
ysr@777 544
ysr@777 545 _markStack(this),
ysr@777 546 // _finger set in set_non_marking_state
ysr@777 547
johnc@4173 548 _max_worker_id(MAX2((uint)ParallelGCThreads, 1U)),
ysr@777 549 // _active_tasks set in set_non_marking_state
ysr@777 550 // _tasks set inside the constructor
johnc@4173 551 _task_queues(new CMTaskQueueSet((int) _max_worker_id)),
johnc@4173 552 _terminator(ParallelTaskTerminator((int) _max_worker_id, _task_queues)),
ysr@777 553
ysr@777 554 _has_overflown(false),
ysr@777 555 _concurrent(false),
tonyp@1054 556 _has_aborted(false),
brutisso@6904 557 _aborted_gc_id(GCId::undefined()),
tonyp@1054 558 _restart_for_overflow(false),
tonyp@1054 559 _concurrent_marking_in_progress(false),
ysr@777 560
ysr@777 561 // _verbose_level set below
ysr@777 562
ysr@777 563 _init_times(),
ysr@777 564 _remark_times(), _remark_mark_times(), _remark_weak_ref_times(),
ysr@777 565 _cleanup_times(),
ysr@777 566 _total_counting_time(0.0),
ysr@777 567 _total_rs_scrub_time(0.0),
johnc@3463 568
johnc@3463 569 _parallel_workers(NULL),
johnc@3463 570
johnc@3463 571 _count_card_bitmaps(NULL),
johnc@4333 572 _count_marked_bytes(NULL),
johnc@4333 573 _completed_initialization(false) {
tonyp@2973 574 CMVerboseLevel verbose_level = (CMVerboseLevel) G1MarkingVerboseLevel;
tonyp@2973 575 if (verbose_level < no_verbose) {
ysr@777 576 verbose_level = no_verbose;
tonyp@2973 577 }
tonyp@2973 578 if (verbose_level > high_verbose) {
ysr@777 579 verbose_level = high_verbose;
tonyp@2973 580 }
ysr@777 581 _verbose_level = verbose_level;
ysr@777 582
tonyp@2973 583 if (verbose_low()) {
ysr@777 584 gclog_or_tty->print_cr("[global] init, heap start = "PTR_FORMAT", "
drchase@6680 585 "heap end = " INTPTR_FORMAT, p2i(_heap_start), p2i(_heap_end));
tonyp@2973 586 }
ysr@777 587
tschatzl@7051 588 _markBitMap1.initialize(g1h->reserved_region(), prev_bitmap_storage);
tschatzl@7051 589 _markBitMap2.initialize(g1h->reserved_region(), next_bitmap_storage);
ysr@777 590
ysr@777 591 // Create & start a ConcurrentMark thread.
ysr@1280 592 _cmThread = new ConcurrentMarkThread(this);
ysr@1280 593 assert(cmThread() != NULL, "CM Thread should have been created");
ysr@1280 594 assert(cmThread()->cm() != NULL, "CM Thread should refer to this cm");
ehelin@6168 595 if (_cmThread->osthread() == NULL) {
ehelin@6168 596 vm_shutdown_during_initialization("Could not create ConcurrentMarkThread");
ehelin@6168 597 }
ysr@1280 598
ysr@777 599 assert(CGC_lock != NULL, "Where's the CGC_lock?");
tschatzl@7051 600 assert(_markBitMap1.covers(g1h->reserved_region()), "_markBitMap1 inconsistency");
tschatzl@7051 601 assert(_markBitMap2.covers(g1h->reserved_region()), "_markBitMap2 inconsistency");
ysr@777 602
ysr@777 603 SATBMarkQueueSet& satb_qs = JavaThread::satb_mark_queue_set();
tonyp@1717 604 satb_qs.set_buffer_size(G1SATBBufferSize);
ysr@777 605
tonyp@3464 606 _root_regions.init(_g1h, this);
tonyp@3464 607
jmasa@1719 608 if (ConcGCThreads > ParallelGCThreads) {
drchase@6680 609 warning("Can't have more ConcGCThreads (" UINTX_FORMAT ") "
drchase@6680 610 "than ParallelGCThreads (" UINTX_FORMAT ").",
johnc@4333 611 ConcGCThreads, ParallelGCThreads);
johnc@4333 612 return;
ysr@777 613 }
ysr@777 614 if (ParallelGCThreads == 0) {
ysr@777 615 // if we are not running with any parallel GC threads we will not
ysr@777 616 // spawn any marking threads either
jmasa@3294 617 _parallel_marking_threads = 0;
jmasa@3294 618 _max_parallel_marking_threads = 0;
jmasa@3294 619 _sleep_factor = 0.0;
jmasa@3294 620 _marking_task_overhead = 1.0;
ysr@777 621 } else {
johnc@4547 622 if (!FLAG_IS_DEFAULT(ConcGCThreads) && ConcGCThreads > 0) {
johnc@4547 623 // Note: ConcGCThreads has precedence over G1MarkingOverheadPercent
ysr@777 624 // if both are set
ysr@777 625 _sleep_factor = 0.0;
ysr@777 626 _marking_task_overhead = 1.0;
johnc@1186 627 } else if (G1MarkingOverheadPercent > 0) {
johnc@4547 628 // We will calculate the number of parallel marking threads based
johnc@4547 629 // on a target overhead with respect to the soft real-time goal
johnc@1186 630 double marking_overhead = (double) G1MarkingOverheadPercent / 100.0;
ysr@777 631 double overall_cm_overhead =
johnc@1186 632 (double) MaxGCPauseMillis * marking_overhead /
johnc@1186 633 (double) GCPauseIntervalMillis;
ysr@777 634 double cpu_ratio = 1.0 / (double) os::processor_count();
ysr@777 635 double marking_thread_num = ceil(overall_cm_overhead / cpu_ratio);
ysr@777 636 double marking_task_overhead =
ysr@777 637 overall_cm_overhead / marking_thread_num *
ysr@777 638 (double) os::processor_count();
ysr@777 639 double sleep_factor =
ysr@777 640 (1.0 - marking_task_overhead) / marking_task_overhead;
ysr@777 641
johnc@4547 642 FLAG_SET_ERGO(uintx, ConcGCThreads, (uint) marking_thread_num);
ysr@777 643 _sleep_factor = sleep_factor;
ysr@777 644 _marking_task_overhead = marking_task_overhead;
ysr@777 645 } else {
johnc@4547 646 // Calculate the number of parallel marking threads by scaling
johnc@4547 647 // the number of parallel GC threads.
johnc@4547 648 uint marking_thread_num = scale_parallel_threads((uint) ParallelGCThreads);
johnc@4547 649 FLAG_SET_ERGO(uintx, ConcGCThreads, marking_thread_num);
ysr@777 650 _sleep_factor = 0.0;
ysr@777 651 _marking_task_overhead = 1.0;
ysr@777 652 }
ysr@777 653
johnc@4547 654 assert(ConcGCThreads > 0, "Should have been set");
johnc@4547 655 _parallel_marking_threads = (uint) ConcGCThreads;
johnc@4547 656 _max_parallel_marking_threads = _parallel_marking_threads;
johnc@4547 657
tonyp@2973 658 if (parallel_marking_threads() > 1) {
ysr@777 659 _cleanup_task_overhead = 1.0;
tonyp@2973 660 } else {
ysr@777 661 _cleanup_task_overhead = marking_task_overhead();
tonyp@2973 662 }
ysr@777 663 _cleanup_sleep_factor =
ysr@777 664 (1.0 - cleanup_task_overhead()) / cleanup_task_overhead();
ysr@777 665
ysr@777 666 #if 0
ysr@777 667 gclog_or_tty->print_cr("Marking Threads %d", parallel_marking_threads());
ysr@777 668 gclog_or_tty->print_cr("CM Marking Task Overhead %1.4lf", marking_task_overhead());
ysr@777 669 gclog_or_tty->print_cr("CM Sleep Factor %1.4lf", sleep_factor());
ysr@777 670 gclog_or_tty->print_cr("CL Marking Task Overhead %1.4lf", cleanup_task_overhead());
ysr@777 671 gclog_or_tty->print_cr("CL Sleep Factor %1.4lf", cleanup_sleep_factor());
ysr@777 672 #endif
ysr@777 673
tonyp@1458 674 guarantee(parallel_marking_threads() > 0, "peace of mind");
jmasa@2188 675 _parallel_workers = new FlexibleWorkGang("G1 Parallel Marking Threads",
jmasa@3357 676 _max_parallel_marking_threads, false, true);
jmasa@2188 677 if (_parallel_workers == NULL) {
ysr@777 678 vm_exit_during_initialization("Failed necessary allocation.");
jmasa@2188 679 } else {
jmasa@2188 680 _parallel_workers->initialize_workers();
jmasa@2188 681 }
ysr@777 682 }
ysr@777 683
johnc@4333 684 if (FLAG_IS_DEFAULT(MarkStackSize)) {
johnc@4333 685 uintx mark_stack_size =
johnc@4333 686 MIN2(MarkStackSizeMax,
johnc@4333 687 MAX2(MarkStackSize, (uintx) (parallel_marking_threads() * TASKQUEUE_SIZE)));
johnc@4333 688 // Verify that the calculated value for MarkStackSize is in range.
johnc@4333 689 // It would be nice to use the private utility routine from Arguments.
johnc@4333 690 if (!(mark_stack_size >= 1 && mark_stack_size <= MarkStackSizeMax)) {
johnc@4333 691 warning("Invalid value calculated for MarkStackSize (" UINTX_FORMAT "): "
johnc@4333 692 "must be between " UINTX_FORMAT " and " UINTX_FORMAT,
drchase@6680 693 mark_stack_size, (uintx) 1, MarkStackSizeMax);
johnc@4333 694 return;
johnc@4333 695 }
johnc@4333 696 FLAG_SET_ERGO(uintx, MarkStackSize, mark_stack_size);
johnc@4333 697 } else {
johnc@4333 698 // Verify MarkStackSize is in range.
johnc@4333 699 if (FLAG_IS_CMDLINE(MarkStackSize)) {
johnc@4333 700 if (FLAG_IS_DEFAULT(MarkStackSizeMax)) {
johnc@4333 701 if (!(MarkStackSize >= 1 && MarkStackSize <= MarkStackSizeMax)) {
johnc@4333 702 warning("Invalid value specified for MarkStackSize (" UINTX_FORMAT "): "
johnc@4333 703 "must be between " UINTX_FORMAT " and " UINTX_FORMAT,
drchase@6680 704 MarkStackSize, (uintx) 1, MarkStackSizeMax);
johnc@4333 705 return;
johnc@4333 706 }
johnc@4333 707 } else if (FLAG_IS_CMDLINE(MarkStackSizeMax)) {
johnc@4333 708 if (!(MarkStackSize >= 1 && MarkStackSize <= MarkStackSizeMax)) {
johnc@4333 709 warning("Invalid value specified for MarkStackSize (" UINTX_FORMAT ")"
johnc@4333 710 " or for MarkStackSizeMax (" UINTX_FORMAT ")",
johnc@4333 711 MarkStackSize, MarkStackSizeMax);
johnc@4333 712 return;
johnc@4333 713 }
johnc@4333 714 }
johnc@4333 715 }
johnc@4333 716 }
johnc@4333 717
johnc@4333 718 if (!_markStack.allocate(MarkStackSize)) {
johnc@4333 719 warning("Failed to allocate CM marking stack");
johnc@4333 720 return;
johnc@4333 721 }
johnc@4333 722
johnc@4333 723 _tasks = NEW_C_HEAP_ARRAY(CMTask*, _max_worker_id, mtGC);
johnc@4333 724 _accum_task_vtime = NEW_C_HEAP_ARRAY(double, _max_worker_id, mtGC);
johnc@4333 725
johnc@4333 726 _count_card_bitmaps = NEW_C_HEAP_ARRAY(BitMap, _max_worker_id, mtGC);
johnc@4333 727 _count_marked_bytes = NEW_C_HEAP_ARRAY(size_t*, _max_worker_id, mtGC);
johnc@4333 728
johnc@4333 729 BitMap::idx_t card_bm_size = _card_bm.size();
johnc@4333 730
johnc@4333 731 // so that the assertion in MarkingTaskQueue::task_queue doesn't fail
johnc@4333 732 _active_tasks = _max_worker_id;
johnc@4333 733
johnc@4333 734 size_t max_regions = (size_t) _g1h->max_regions();
johnc@4333 735 for (uint i = 0; i < _max_worker_id; ++i) {
johnc@4333 736 CMTaskQueue* task_queue = new CMTaskQueue();
johnc@4333 737 task_queue->initialize();
johnc@4333 738 _task_queues->register_queue(i, task_queue);
johnc@4333 739
johnc@4333 740 _count_card_bitmaps[i] = BitMap(card_bm_size, false);
johnc@4333 741 _count_marked_bytes[i] = NEW_C_HEAP_ARRAY(size_t, max_regions, mtGC);
johnc@4333 742
johnc@4333 743 _tasks[i] = new CMTask(i, this,
johnc@4333 744 _count_marked_bytes[i],
johnc@4333 745 &_count_card_bitmaps[i],
johnc@4333 746 task_queue, _task_queues);
johnc@4333 747
johnc@4333 748 _accum_task_vtime[i] = 0.0;
johnc@4333 749 }
johnc@4333 750
johnc@4333 751 // Calculate the card number for the bottom of the heap. Used
johnc@4333 752 // in biasing indexes into the accounting card bitmaps.
johnc@4333 753 _heap_bottom_card_num =
johnc@4333 754 intptr_t(uintptr_t(_g1h->reserved_region().start()) >>
johnc@4333 755 CardTableModRefBS::card_shift);
johnc@4333 756
johnc@4333 757 // Clear all the liveness counting data
johnc@4333 758 clear_all_count_data();
johnc@4333 759
ysr@777 760 // so that the call below can read a sensible value
tschatzl@7051 761 _heap_start = g1h->reserved_region().start();
ysr@777 762 set_non_marking_state();
johnc@4333 763 _completed_initialization = true;
ysr@777 764 }
ysr@777 765
ysr@777 766 void ConcurrentMark::reset() {
ysr@777 767 // Starting values for these two. This should be called in a STW
tschatzl@7051 768 // phase.
tschatzl@7051 769 MemRegion reserved = _g1h->g1_reserved();
tschatzl@7051 770 _heap_start = reserved.start();
tschatzl@7051 771 _heap_end = reserved.end();
ysr@777 772
tonyp@1458 773 // Separated the asserts so that we know which one fires.
tonyp@1458 774 assert(_heap_start != NULL, "heap bounds should look ok");
tonyp@1458 775 assert(_heap_end != NULL, "heap bounds should look ok");
tonyp@1458 776 assert(_heap_start < _heap_end, "heap bounds should look ok");
ysr@777 777
johnc@4386 778 // Reset all the marking data structures and any necessary flags
johnc@4386 779 reset_marking_state();
ysr@777 780
tonyp@2973 781 if (verbose_low()) {
ysr@777 782 gclog_or_tty->print_cr("[global] resetting");
tonyp@2973 783 }
ysr@777 784
ysr@777 785 // We do reset all of them, since different phases will use
ysr@777 786 // different number of active threads. So, it's easiest to have all
ysr@777 787 // of them ready.
johnc@4173 788 for (uint i = 0; i < _max_worker_id; ++i) {
ysr@777 789 _tasks[i]->reset(_nextMarkBitMap);
johnc@2190 790 }
ysr@777 791
ysr@777 792 // we need this to make sure that the flag is on during the evac
ysr@777 793 // pause with initial mark piggy-backed
ysr@777 794 set_concurrent_marking_in_progress();
ysr@777 795 }
ysr@777 796
johnc@4386 797
johnc@4386 798 void ConcurrentMark::reset_marking_state(bool clear_overflow) {
johnc@4386 799 _markStack.set_should_expand();
johnc@4386 800 _markStack.setEmpty(); // Also clears the _markStack overflow flag
johnc@4386 801 if (clear_overflow) {
johnc@4386 802 clear_has_overflown();
johnc@4386 803 } else {
johnc@4386 804 assert(has_overflown(), "pre-condition");
johnc@4386 805 }
johnc@4386 806 _finger = _heap_start;
johnc@4386 807
johnc@4386 808 for (uint i = 0; i < _max_worker_id; ++i) {
johnc@4386 809 CMTaskQueue* queue = _task_queues->queue(i);
johnc@4386 810 queue->set_empty();
johnc@4386 811 }
johnc@4386 812 }
johnc@4386 813
johnc@4788 814 void ConcurrentMark::set_concurrency(uint active_tasks) {
johnc@4173 815 assert(active_tasks <= _max_worker_id, "we should not have more");
ysr@777 816
ysr@777 817 _active_tasks = active_tasks;
ysr@777 818 // Need to update the three data structures below according to the
ysr@777 819 // number of active threads for this phase.
ysr@777 820 _terminator = ParallelTaskTerminator((int) active_tasks, _task_queues);
ysr@777 821 _first_overflow_barrier_sync.set_n_workers((int) active_tasks);
ysr@777 822 _second_overflow_barrier_sync.set_n_workers((int) active_tasks);
johnc@4788 823 }
johnc@4788 824
johnc@4788 825 void ConcurrentMark::set_concurrency_and_phase(uint active_tasks, bool concurrent) {
johnc@4788 826 set_concurrency(active_tasks);
ysr@777 827
ysr@777 828 _concurrent = concurrent;
ysr@777 829 // We propagate this to all tasks, not just the active ones.
johnc@4173 830 for (uint i = 0; i < _max_worker_id; ++i)
ysr@777 831 _tasks[i]->set_concurrent(concurrent);
ysr@777 832
ysr@777 833 if (concurrent) {
ysr@777 834 set_concurrent_marking_in_progress();
ysr@777 835 } else {
ysr@777 836 // We currently assume that the concurrent flag has been set to
ysr@777 837 // false before we start remark. At this point we should also be
ysr@777 838 // in a STW phase.
tonyp@1458 839 assert(!concurrent_marking_in_progress(), "invariant");
pliden@6693 840 assert(out_of_regions(),
johnc@4788 841 err_msg("only way to get here: _finger: "PTR_FORMAT", _heap_end: "PTR_FORMAT,
drchase@6680 842 p2i(_finger), p2i(_heap_end)));
ysr@777 843 }
ysr@777 844 }
ysr@777 845
ysr@777 846 void ConcurrentMark::set_non_marking_state() {
ysr@777 847 // We set the global marking state to some default values when we're
ysr@777 848 // not doing marking.
johnc@4386 849 reset_marking_state();
ysr@777 850 _active_tasks = 0;
ysr@777 851 clear_concurrent_marking_in_progress();
ysr@777 852 }
ysr@777 853
ysr@777 854 ConcurrentMark::~ConcurrentMark() {
stefank@3364 855 // The ConcurrentMark instance is never freed.
stefank@3364 856 ShouldNotReachHere();
ysr@777 857 }
ysr@777 858
ysr@777 859 void ConcurrentMark::clearNextBitmap() {
tonyp@1794 860 G1CollectedHeap* g1h = G1CollectedHeap::heap();
tonyp@1794 861
tonyp@1794 862 // Make sure that the concurrent mark thread looks to still be in
tonyp@1794 863 // the current cycle.
tonyp@1794 864 guarantee(cmThread()->during_cycle(), "invariant");
tonyp@1794 865
tonyp@1794 866 // We are finishing up the current cycle by clearing the next
tonyp@1794 867 // marking bitmap and getting it ready for the next cycle. During
tonyp@1794 868 // this time no other cycle can start. So, let's make sure that this
tonyp@1794 869 // is the case.
tonyp@1794 870 guarantee(!g1h->mark_in_progress(), "invariant");
tonyp@1794 871
tschatzl@7051 872 ClearBitmapHRClosure cl(this, _nextMarkBitMap, true /* may_yield */);
tschatzl@7051 873 g1h->heap_region_iterate(&cl);
tschatzl@7051 874
tschatzl@7051 875 // Clear the liveness counting data. If the marking has been aborted, the abort()
tschatzl@7051 876 // call already did that.
tschatzl@7051 877 if (cl.complete()) {
tschatzl@7051 878 clear_all_count_data();
tonyp@1794 879 }
tonyp@1794 880
tonyp@1794 881 // Repeat the asserts from above.
tonyp@1794 882 guarantee(cmThread()->during_cycle(), "invariant");
tonyp@1794 883 guarantee(!g1h->mark_in_progress(), "invariant");
ysr@777 884 }
ysr@777 885
tschatzl@7051 886 class CheckBitmapClearHRClosure : public HeapRegionClosure {
tschatzl@7051 887 CMBitMap* _bitmap;
tschatzl@7051 888 bool _error;
tschatzl@7051 889 public:
tschatzl@7051 890 CheckBitmapClearHRClosure(CMBitMap* bitmap) : _bitmap(bitmap) {
tschatzl@7051 891 }
tschatzl@7051 892
tschatzl@7051 893 virtual bool doHeapRegion(HeapRegion* r) {
tschatzl@7100 894 // This closure can be called concurrently to the mutator, so we must make sure
tschatzl@7100 895 // that the result of the getNextMarkedWordAddress() call is compared to the
tschatzl@7100 896 // value passed to it as limit to detect any found bits.
tschatzl@7100 897 // We can use the region's orig_end() for the limit and the comparison value
tschatzl@7100 898 // as it always contains the "real" end of the region that never changes and
tschatzl@7100 899 // has no side effects.
tschatzl@7100 900 // Due to the latter, there can also be no problem with the compiler generating
tschatzl@7100 901 // reloads of the orig_end() call.
tschatzl@7100 902 HeapWord* end = r->orig_end();
tschatzl@7100 903 return _bitmap->getNextMarkedWordAddress(r->bottom(), end) != end;
tschatzl@7051 904 }
tschatzl@7051 905 };
tschatzl@7051 906
tschatzl@7016 907 bool ConcurrentMark::nextMarkBitmapIsClear() {
tschatzl@7051 908 CheckBitmapClearHRClosure cl(_nextMarkBitMap);
tschatzl@7051 909 _g1h->heap_region_iterate(&cl);
tschatzl@7051 910 return cl.complete();
tschatzl@7016 911 }
tschatzl@7016 912
ysr@777 913 class NoteStartOfMarkHRClosure: public HeapRegionClosure {
ysr@777 914 public:
ysr@777 915 bool doHeapRegion(HeapRegion* r) {
ysr@777 916 if (!r->continuesHumongous()) {
tonyp@3416 917 r->note_start_of_marking();
ysr@777 918 }
ysr@777 919 return false;
ysr@777 920 }
ysr@777 921 };
ysr@777 922
ysr@777 923 void ConcurrentMark::checkpointRootsInitialPre() {
ysr@777 924 G1CollectedHeap* g1h = G1CollectedHeap::heap();
ysr@777 925 G1CollectorPolicy* g1p = g1h->g1_policy();
ysr@777 926
ysr@777 927 _has_aborted = false;
ysr@777 928
jcoomes@1902 929 #ifndef PRODUCT
tonyp@1479 930 if (G1PrintReachableAtInitialMark) {
tonyp@1823 931 print_reachable("at-cycle-start",
johnc@2969 932 VerifyOption_G1UsePrevMarking, true /* all */);
tonyp@1479 933 }
jcoomes@1902 934 #endif
ysr@777 935
ysr@777 936 // Initialise marking structures. This has to be done in a STW phase.
ysr@777 937 reset();
tonyp@3416 938
tonyp@3416 939 // For each region note start of marking.
tonyp@3416 940 NoteStartOfMarkHRClosure startcl;
tonyp@3416 941 g1h->heap_region_iterate(&startcl);
ysr@777 942 }
ysr@777 943
ysr@777 944
ysr@777 945 void ConcurrentMark::checkpointRootsInitialPost() {
ysr@777 946 G1CollectedHeap* g1h = G1CollectedHeap::heap();
ysr@777 947
tonyp@2848 948 // If we force an overflow during remark, the remark operation will
tonyp@2848 949 // actually abort and we'll restart concurrent marking. If we always
tonyp@2848 950 // force an oveflow during remark we'll never actually complete the
tonyp@2848 951 // marking phase. So, we initilize this here, at the start of the
tonyp@2848 952 // cycle, so that at the remaining overflow number will decrease at
tonyp@2848 953 // every remark and we'll eventually not need to cause one.
tonyp@2848 954 force_overflow_stw()->init();
tonyp@2848 955
johnc@3175 956 // Start Concurrent Marking weak-reference discovery.
johnc@3175 957 ReferenceProcessor* rp = g1h->ref_processor_cm();
johnc@3175 958 // enable ("weak") refs discovery
johnc@3175 959 rp->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/);
ysr@892 960 rp->setup_policy(false); // snapshot the soft ref policy to be used in this cycle
ysr@777 961
ysr@777 962 SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();
tonyp@1752 963 // This is the start of the marking cycle, we're expected all
tonyp@1752 964 // threads to have SATB queues with active set to false.
tonyp@1752 965 satb_mq_set.set_active_all_threads(true, /* new active value */
tonyp@1752 966 false /* expected_active */);
ysr@777 967
tonyp@3464 968 _root_regions.prepare_for_scan();
tonyp@3464 969
ysr@777 970 // update_g1_committed() will be called at the end of an evac pause
ysr@777 971 // when marking is on. So, it's also called at the end of the
ysr@777 972 // initial-mark pause to update the heap end, if the heap expands
ysr@777 973 // during it. No need to call it here.
ysr@777 974 }
ysr@777 975
ysr@777 976 /*
tonyp@2848 977 * Notice that in the next two methods, we actually leave the STS
tonyp@2848 978 * during the barrier sync and join it immediately afterwards. If we
tonyp@2848 979 * do not do this, the following deadlock can occur: one thread could
tonyp@2848 980 * be in the barrier sync code, waiting for the other thread to also
tonyp@2848 981 * sync up, whereas another one could be trying to yield, while also
tonyp@2848 982 * waiting for the other threads to sync up too.
tonyp@2848 983 *
tonyp@2848 984 * Note, however, that this code is also used during remark and in
tonyp@2848 985 * this case we should not attempt to leave / enter the STS, otherwise
tonyp@2848 986 * we'll either hit an asseert (debug / fastdebug) or deadlock
tonyp@2848 987 * (product). So we should only leave / enter the STS if we are
tonyp@2848 988 * operating concurrently.
tonyp@2848 989 *
tonyp@2848 990 * Because the thread that does the sync barrier has left the STS, it
tonyp@2848 991 * is possible to be suspended for a Full GC or an evacuation pause
tonyp@2848 992 * could occur. This is actually safe, since the entering the sync
tonyp@2848 993 * barrier is one of the last things do_marking_step() does, and it
tonyp@2848 994 * doesn't manipulate any data structures afterwards.
tonyp@2848 995 */
ysr@777 996
johnc@4173 997 void ConcurrentMark::enter_first_sync_barrier(uint worker_id) {
tonyp@2973 998 if (verbose_low()) {
johnc@4173 999 gclog_or_tty->print_cr("[%u] entering first barrier", worker_id);
tonyp@2973 1000 }
ysr@777 1001
tonyp@2848 1002 if (concurrent()) {
pliden@6906 1003 SuspendibleThreadSet::leave();
tonyp@2848 1004 }
pliden@6692 1005
pliden@6692 1006 bool barrier_aborted = !_first_overflow_barrier_sync.enter();
pliden@6692 1007
tonyp@2848 1008 if (concurrent()) {
pliden@6906 1009 SuspendibleThreadSet::join();
tonyp@2848 1010 }
ysr@777 1011 // at this point everyone should have synced up and not be doing any
ysr@777 1012 // more work
ysr@777 1013
tonyp@2973 1014 if (verbose_low()) {
pliden@6692 1015 if (barrier_aborted) {
pliden@6692 1016 gclog_or_tty->print_cr("[%u] aborted first barrier", worker_id);
pliden@6692 1017 } else {
pliden@6692 1018 gclog_or_tty->print_cr("[%u] leaving first barrier", worker_id);
pliden@6692 1019 }
pliden@6692 1020 }
pliden@6692 1021
pliden@6692 1022 if (barrier_aborted) {
pliden@6692 1023 // If the barrier aborted we ignore the overflow condition and
pliden@6692 1024 // just abort the whole marking phase as quickly as possible.
pliden@6692 1025 return;
tonyp@2973 1026 }
ysr@777 1027
johnc@4788 1028 // If we're executing the concurrent phase of marking, reset the marking
johnc@4788 1029 // state; otherwise the marking state is reset after reference processing,
johnc@4788 1030 // during the remark pause.
johnc@4788 1031 // If we reset here as a result of an overflow during the remark we will
johnc@4788 1032 // see assertion failures from any subsequent set_concurrency_and_phase()
johnc@4788 1033 // calls.
johnc@4788 1034 if (concurrent()) {
johnc@4788 1035 // let the task associated with with worker 0 do this
johnc@4788 1036 if (worker_id == 0) {
johnc@4788 1037 // task 0 is responsible for clearing the global data structures
johnc@4788 1038 // We should be here because of an overflow. During STW we should
johnc@4788 1039 // not clear the overflow flag since we rely on it being true when
johnc@4788 1040 // we exit this method to abort the pause and restart concurent
johnc@4788 1041 // marking.
johnc@4788 1042 reset_marking_state(true /* clear_overflow */);
johnc@4788 1043 force_overflow()->update();
johnc@4788 1044
johnc@4788 1045 if (G1Log::fine()) {
brutisso@6904 1046 gclog_or_tty->gclog_stamp(concurrent_gc_id());
johnc@4788 1047 gclog_or_tty->print_cr("[GC concurrent-mark-reset-for-overflow]");
johnc@4788 1048 }
ysr@777 1049 }
ysr@777 1050 }
ysr@777 1051
ysr@777 1052 // after this, each task should reset its own data structures then
ysr@777 1053 // then go into the second barrier
ysr@777 1054 }
ysr@777 1055
johnc@4173 1056 void ConcurrentMark::enter_second_sync_barrier(uint worker_id) {
tonyp@2973 1057 if (verbose_low()) {
johnc@4173 1058 gclog_or_tty->print_cr("[%u] entering second barrier", worker_id);
tonyp@2973 1059 }
ysr@777 1060
tonyp@2848 1061 if (concurrent()) {
pliden@6906 1062 SuspendibleThreadSet::leave();
tonyp@2848 1063 }
pliden@6692 1064
pliden@6692 1065 bool barrier_aborted = !_second_overflow_barrier_sync.enter();
pliden@6692 1066
tonyp@2848 1067 if (concurrent()) {
pliden@6906 1068 SuspendibleThreadSet::join();
tonyp@2848 1069 }
johnc@4788 1070 // at this point everything should be re-initialized and ready to go
ysr@777 1071
tonyp@2973 1072 if (verbose_low()) {
pliden@6692 1073 if (barrier_aborted) {
pliden@6692 1074 gclog_or_tty->print_cr("[%u] aborted second barrier", worker_id);
pliden@6692 1075 } else {
pliden@6692 1076 gclog_or_tty->print_cr("[%u] leaving second barrier", worker_id);
pliden@6692 1077 }
tonyp@2973 1078 }
ysr@777 1079 }
ysr@777 1080
tonyp@2848 1081 #ifndef PRODUCT
tonyp@2848 1082 void ForceOverflowSettings::init() {
tonyp@2848 1083 _num_remaining = G1ConcMarkForceOverflow;
tonyp@2848 1084 _force = false;
tonyp@2848 1085 update();
tonyp@2848 1086 }
tonyp@2848 1087
tonyp@2848 1088 void ForceOverflowSettings::update() {
tonyp@2848 1089 if (_num_remaining > 0) {
tonyp@2848 1090 _num_remaining -= 1;
tonyp@2848 1091 _force = true;
tonyp@2848 1092 } else {
tonyp@2848 1093 _force = false;
tonyp@2848 1094 }
tonyp@2848 1095 }
tonyp@2848 1096
tonyp@2848 1097 bool ForceOverflowSettings::should_force() {
tonyp@2848 1098 if (_force) {
tonyp@2848 1099 _force = false;
tonyp@2848 1100 return true;
tonyp@2848 1101 } else {
tonyp@2848 1102 return false;
tonyp@2848 1103 }
tonyp@2848 1104 }
tonyp@2848 1105 #endif // !PRODUCT
tonyp@2848 1106
ysr@777 1107 class CMConcurrentMarkingTask: public AbstractGangTask {
ysr@777 1108 private:
ysr@777 1109 ConcurrentMark* _cm;
ysr@777 1110 ConcurrentMarkThread* _cmt;
ysr@777 1111
ysr@777 1112 public:
jmasa@3357 1113 void work(uint worker_id) {
tonyp@1458 1114 assert(Thread::current()->is_ConcurrentGC_thread(),
tonyp@1458 1115 "this should only be done by a conc GC thread");
johnc@2316 1116 ResourceMark rm;
ysr@777 1117
ysr@777 1118 double start_vtime = os::elapsedVTime();
ysr@777 1119
pliden@6906 1120 SuspendibleThreadSet::join();
ysr@777 1121
jmasa@3357 1122 assert(worker_id < _cm->active_tasks(), "invariant");
jmasa@3357 1123 CMTask* the_task = _cm->task(worker_id);
ysr@777 1124 the_task->record_start_time();
ysr@777 1125 if (!_cm->has_aborted()) {
ysr@777 1126 do {
ysr@777 1127 double start_vtime_sec = os::elapsedVTime();
johnc@2494 1128 double mark_step_duration_ms = G1ConcMarkStepDurationMillis;
johnc@2494 1129
johnc@2494 1130 the_task->do_marking_step(mark_step_duration_ms,
johnc@4787 1131 true /* do_termination */,
johnc@4787 1132 false /* is_serial*/);
johnc@2494 1133
ysr@777 1134 double end_vtime_sec = os::elapsedVTime();
ysr@777 1135 double elapsed_vtime_sec = end_vtime_sec - start_vtime_sec;
ysr@777 1136 _cm->clear_has_overflown();
ysr@777 1137
tschatzl@7094 1138 _cm->do_yield_check(worker_id);
ysr@777 1139
ysr@777 1140 jlong sleep_time_ms;
ysr@777 1141 if (!_cm->has_aborted() && the_task->has_aborted()) {
ysr@777 1142 sleep_time_ms =
ysr@777 1143 (jlong) (elapsed_vtime_sec * _cm->sleep_factor() * 1000.0);
pliden@6906 1144 SuspendibleThreadSet::leave();
ysr@777 1145 os::sleep(Thread::current(), sleep_time_ms, false);
pliden@6906 1146 SuspendibleThreadSet::join();
ysr@777 1147 }
ysr@777 1148 } while (!_cm->has_aborted() && the_task->has_aborted());
ysr@777 1149 }
ysr@777 1150 the_task->record_end_time();
tonyp@1458 1151 guarantee(!the_task->has_aborted() || _cm->has_aborted(), "invariant");
ysr@777 1152
pliden@6906 1153 SuspendibleThreadSet::leave();
ysr@777 1154
ysr@777 1155 double end_vtime = os::elapsedVTime();
jmasa@3357 1156 _cm->update_accum_task_vtime(worker_id, end_vtime - start_vtime);
ysr@777 1157 }
ysr@777 1158
ysr@777 1159 CMConcurrentMarkingTask(ConcurrentMark* cm,
ysr@777 1160 ConcurrentMarkThread* cmt) :
ysr@777 1161 AbstractGangTask("Concurrent Mark"), _cm(cm), _cmt(cmt) { }
ysr@777 1162
ysr@777 1163 ~CMConcurrentMarkingTask() { }
ysr@777 1164 };
ysr@777 1165
jmasa@3294 1166 // Calculates the number of active workers for a concurrent
jmasa@3294 1167 // phase.
jmasa@3357 1168 uint ConcurrentMark::calc_parallel_marking_threads() {
johnc@3338 1169 if (G1CollectedHeap::use_parallel_gc_threads()) {
jmasa@3357 1170 uint n_conc_workers = 0;
jmasa@3294 1171 if (!UseDynamicNumberOfGCThreads ||
jmasa@3294 1172 (!FLAG_IS_DEFAULT(ConcGCThreads) &&
jmasa@3294 1173 !ForceDynamicNumberOfGCThreads)) {
jmasa@3294 1174 n_conc_workers = max_parallel_marking_threads();
jmasa@3294 1175 } else {
jmasa@3294 1176 n_conc_workers =
jmasa@3294 1177 AdaptiveSizePolicy::calc_default_active_workers(
jmasa@3294 1178 max_parallel_marking_threads(),
jmasa@3294 1179 1, /* Minimum workers */
jmasa@3294 1180 parallel_marking_threads(),
jmasa@3294 1181 Threads::number_of_non_daemon_threads());
jmasa@3294 1182 // Don't scale down "n_conc_workers" by scale_parallel_threads() because
jmasa@3294 1183 // that scaling has already gone into "_max_parallel_marking_threads".
jmasa@3294 1184 }
johnc@3338 1185 assert(n_conc_workers > 0, "Always need at least 1");
johnc@3338 1186 return n_conc_workers;
jmasa@3294 1187 }
johnc@3338 1188 // If we are not running with any parallel GC threads we will not
johnc@3338 1189 // have spawned any marking threads either. Hence the number of
johnc@3338 1190 // concurrent workers should be 0.
johnc@3338 1191 return 0;
jmasa@3294 1192 }
jmasa@3294 1193
tonyp@3464 1194 void ConcurrentMark::scanRootRegion(HeapRegion* hr, uint worker_id) {
tonyp@3464 1195 // Currently, only survivors can be root regions.
tonyp@3464 1196 assert(hr->next_top_at_mark_start() == hr->bottom(), "invariant");
tonyp@3464 1197 G1RootRegionScanClosure cl(_g1h, this, worker_id);
tonyp@3464 1198
tonyp@3464 1199 const uintx interval = PrefetchScanIntervalInBytes;
tonyp@3464 1200 HeapWord* curr = hr->bottom();
tonyp@3464 1201 const HeapWord* end = hr->top();
tonyp@3464 1202 while (curr < end) {
tonyp@3464 1203 Prefetch::read(curr, interval);
tonyp@3464 1204 oop obj = oop(curr);
tonyp@3464 1205 int size = obj->oop_iterate(&cl);
tonyp@3464 1206 assert(size == obj->size(), "sanity");
tonyp@3464 1207 curr += size;
tonyp@3464 1208 }
tonyp@3464 1209 }
tonyp@3464 1210
tonyp@3464 1211 class CMRootRegionScanTask : public AbstractGangTask {
tonyp@3464 1212 private:
tonyp@3464 1213 ConcurrentMark* _cm;
tonyp@3464 1214
tonyp@3464 1215 public:
tonyp@3464 1216 CMRootRegionScanTask(ConcurrentMark* cm) :
tonyp@3464 1217 AbstractGangTask("Root Region Scan"), _cm(cm) { }
tonyp@3464 1218
tonyp@3464 1219 void work(uint worker_id) {
tonyp@3464 1220 assert(Thread::current()->is_ConcurrentGC_thread(),
tonyp@3464 1221 "this should only be done by a conc GC thread");
tonyp@3464 1222
tonyp@3464 1223 CMRootRegions* root_regions = _cm->root_regions();
tonyp@3464 1224 HeapRegion* hr = root_regions->claim_next();
tonyp@3464 1225 while (hr != NULL) {
tonyp@3464 1226 _cm->scanRootRegion(hr, worker_id);
tonyp@3464 1227 hr = root_regions->claim_next();
tonyp@3464 1228 }
tonyp@3464 1229 }
tonyp@3464 1230 };
tonyp@3464 1231
tonyp@3464 1232 void ConcurrentMark::scanRootRegions() {
stefank@6992 1233 // Start of concurrent marking.
stefank@6992 1234 ClassLoaderDataGraph::clear_claimed_marks();
stefank@6992 1235
tonyp@3464 1236 // scan_in_progress() will have been set to true only if there was
tonyp@3464 1237 // at least one root region to scan. So, if it's false, we
tonyp@3464 1238 // should not attempt to do any further work.
tonyp@3464 1239 if (root_regions()->scan_in_progress()) {
tonyp@3464 1240 _parallel_marking_threads = calc_parallel_marking_threads();
tonyp@3464 1241 assert(parallel_marking_threads() <= max_parallel_marking_threads(),
tonyp@3464 1242 "Maximum number of marking threads exceeded");
tonyp@3464 1243 uint active_workers = MAX2(1U, parallel_marking_threads());
tonyp@3464 1244
tonyp@3464 1245 CMRootRegionScanTask task(this);
johnc@4549 1246 if (use_parallel_marking_threads()) {
tonyp@3464 1247 _parallel_workers->set_active_workers((int) active_workers);
tonyp@3464 1248 _parallel_workers->run_task(&task);
tonyp@3464 1249 } else {
tonyp@3464 1250 task.work(0);
tonyp@3464 1251 }
tonyp@3464 1252
tonyp@3464 1253 // It's possible that has_aborted() is true here without actually
tonyp@3464 1254 // aborting the survivor scan earlier. This is OK as it's
tonyp@3464 1255 // mainly used for sanity checking.
tonyp@3464 1256 root_regions()->scan_finished();
tonyp@3464 1257 }
tonyp@3464 1258 }
tonyp@3464 1259
ysr@777 1260 void ConcurrentMark::markFromRoots() {
ysr@777 1261 // we might be tempted to assert that:
ysr@777 1262 // assert(asynch == !SafepointSynchronize::is_at_safepoint(),
ysr@777 1263 // "inconsistent argument?");
ysr@777 1264 // However that wouldn't be right, because it's possible that
ysr@777 1265 // a safepoint is indeed in progress as a younger generation
ysr@777 1266 // stop-the-world GC happens even as we mark in this generation.
ysr@777 1267
ysr@777 1268 _restart_for_overflow = false;
tonyp@2848 1269 force_overflow_conc()->init();
jmasa@3294 1270
jmasa@3294 1271 // _g1h has _n_par_threads
jmasa@3294 1272 _parallel_marking_threads = calc_parallel_marking_threads();
jmasa@3294 1273 assert(parallel_marking_threads() <= max_parallel_marking_threads(),
jmasa@3294 1274 "Maximum number of marking threads exceeded");
johnc@3338 1275
jmasa@3357 1276 uint active_workers = MAX2(1U, parallel_marking_threads());
johnc@3338 1277
johnc@4788 1278 // Parallel task terminator is set in "set_concurrency_and_phase()"
johnc@4788 1279 set_concurrency_and_phase(active_workers, true /* concurrent */);
ysr@777 1280
ysr@777 1281 CMConcurrentMarkingTask markingTask(this, cmThread());
johnc@4549 1282 if (use_parallel_marking_threads()) {
johnc@3338 1283 _parallel_workers->set_active_workers((int)active_workers);
stefank@6992 1284 // Don't set _n_par_threads because it affects MT in process_roots()
johnc@3338 1285 // and the decisions on that MT processing is made elsewhere.
johnc@3338 1286 assert(_parallel_workers->active_workers() > 0, "Should have been set");
ysr@777 1287 _parallel_workers->run_task(&markingTask);
tonyp@2973 1288 } else {
ysr@777 1289 markingTask.work(0);
tonyp@2973 1290 }
ysr@777 1291 print_stats();
ysr@777 1292 }
ysr@777 1293
ysr@777 1294 void ConcurrentMark::checkpointRootsFinal(bool clear_all_soft_refs) {
ysr@777 1295 // world is stopped at this checkpoint
ysr@777 1296 assert(SafepointSynchronize::is_at_safepoint(),
ysr@777 1297 "world should be stopped");
johnc@3175 1298
ysr@777 1299 G1CollectedHeap* g1h = G1CollectedHeap::heap();
ysr@777 1300
ysr@777 1301 // If a full collection has happened, we shouldn't do this.
ysr@777 1302 if (has_aborted()) {
ysr@777 1303 g1h->set_marking_complete(); // So bitmap clearing isn't confused
ysr@777 1304 return;
ysr@777 1305 }
ysr@777 1306
kamg@2445 1307 SvcGCMarker sgcm(SvcGCMarker::OTHER);
kamg@2445 1308
ysr@1280 1309 if (VerifyDuringGC) {
ysr@1280 1310 HandleMark hm; // handle scope
ysr@1280 1311 Universe::heap()->prepare_for_verify();
stefank@5018 1312 Universe::verify(VerifyOption_G1UsePrevMarking,
stefank@5018 1313 " VerifyDuringGC:(before)");
ysr@1280 1314 }
brutisso@7005 1315 g1h->check_bitmaps("Remark Start");
ysr@1280 1316
ysr@777 1317 G1CollectorPolicy* g1p = g1h->g1_policy();
ysr@777 1318 g1p->record_concurrent_mark_remark_start();
ysr@777 1319
ysr@777 1320 double start = os::elapsedTime();
ysr@777 1321
ysr@777 1322 checkpointRootsFinalWork();
ysr@777 1323
ysr@777 1324 double mark_work_end = os::elapsedTime();
ysr@777 1325
ysr@777 1326 weakRefsWork(clear_all_soft_refs);
ysr@777 1327
ysr@777 1328 if (has_overflown()) {
ysr@777 1329 // Oops. We overflowed. Restart concurrent marking.
ysr@777 1330 _restart_for_overflow = true;
johnc@4789 1331 if (G1TraceMarkStackOverflow) {
johnc@4789 1332 gclog_or_tty->print_cr("\nRemark led to restart for overflow.");
johnc@4789 1333 }
johnc@4789 1334
johnc@4789 1335 // Verify the heap w.r.t. the previous marking bitmap.
johnc@4789 1336 if (VerifyDuringGC) {
johnc@4789 1337 HandleMark hm; // handle scope
johnc@4789 1338 Universe::heap()->prepare_for_verify();
stefank@5018 1339 Universe::verify(VerifyOption_G1UsePrevMarking,
stefank@5018 1340 " VerifyDuringGC:(overflow)");
johnc@4789 1341 }
johnc@4789 1342
johnc@4386 1343 // Clear the marking state because we will be restarting
johnc@4386 1344 // marking due to overflowing the global mark stack.
johnc@4386 1345 reset_marking_state();
ysr@777 1346 } else {
johnc@3463 1347 // Aggregate the per-task counting data that we have accumulated
johnc@3463 1348 // while marking.
johnc@3463 1349 aggregate_count_data();
johnc@3463 1350
tonyp@2469 1351 SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();
ysr@777 1352 // We're done with marking.
tonyp@1752 1353 // This is the end of the marking cycle, we're expected all
tonyp@1752 1354 // threads to have SATB queues with active set to true.
tonyp@2469 1355 satb_mq_set.set_active_all_threads(false, /* new active value */
tonyp@2469 1356 true /* expected_active */);
tonyp@1246 1357
tonyp@1246 1358 if (VerifyDuringGC) {
ysr@1280 1359 HandleMark hm; // handle scope
ysr@1280 1360 Universe::heap()->prepare_for_verify();
stefank@5018 1361 Universe::verify(VerifyOption_G1UseNextMarking,
stefank@5018 1362 " VerifyDuringGC:(after)");
tonyp@1246 1363 }
brutisso@7005 1364 g1h->check_bitmaps("Remark End");
johnc@2494 1365 assert(!restart_for_overflow(), "sanity");
johnc@4386 1366 // Completely reset the marking state since marking completed
johnc@4386 1367 set_non_marking_state();
johnc@2494 1368 }
johnc@2494 1369
johnc@4333 1370 // Expand the marking stack, if we have to and if we can.
johnc@4333 1371 if (_markStack.should_expand()) {
johnc@4333 1372 _markStack.expand();
johnc@4333 1373 }
johnc@4333 1374
ysr@777 1375 // Statistics
ysr@777 1376 double now = os::elapsedTime();
ysr@777 1377 _remark_mark_times.add((mark_work_end - start) * 1000.0);
ysr@777 1378 _remark_weak_ref_times.add((now - mark_work_end) * 1000.0);
ysr@777 1379 _remark_times.add((now - start) * 1000.0);
ysr@777 1380
ysr@777 1381 g1p->record_concurrent_mark_remark_end();
sla@5237 1382
sla@5237 1383 G1CMIsAliveClosure is_alive(g1h);
sla@5237 1384 g1h->gc_tracer_cm()->report_object_count_after_gc(&is_alive);
ysr@777 1385 }
ysr@777 1386
johnc@3731 1387 // Base class of the closures that finalize and verify the
johnc@3731 1388 // liveness counting data.
johnc@3731 1389 class CMCountDataClosureBase: public HeapRegionClosure {
johnc@3731 1390 protected:
johnc@4123 1391 G1CollectedHeap* _g1h;
ysr@777 1392 ConcurrentMark* _cm;
johnc@4123 1393 CardTableModRefBS* _ct_bs;
johnc@4123 1394
johnc@3463 1395 BitMap* _region_bm;
johnc@3463 1396 BitMap* _card_bm;
johnc@3463 1397
johnc@4123 1398 // Takes a region that's not empty (i.e., it has at least one
tonyp@1264 1399 // live object in it and sets its corresponding bit on the region
tonyp@1264 1400 // bitmap to 1. If the region is "starts humongous" it will also set
tonyp@1264 1401 // to 1 the bits on the region bitmap that correspond to its
tonyp@1264 1402 // associated "continues humongous" regions.
tonyp@1264 1403 void set_bit_for_region(HeapRegion* hr) {
tonyp@1264 1404 assert(!hr->continuesHumongous(), "should have filtered those out");
tonyp@1264 1405
tschatzl@7091 1406 BitMap::idx_t index = (BitMap::idx_t) hr->hrm_index();
tonyp@1264 1407 if (!hr->startsHumongous()) {
tonyp@1264 1408 // Normal (non-humongous) case: just set the bit.
tonyp@3713 1409 _region_bm->par_at_put(index, true);
tonyp@1264 1410 } else {
tonyp@1264 1411 // Starts humongous case: calculate how many regions are part of
johnc@3463 1412 // this humongous region and then set the bit range.
tonyp@3957 1413 BitMap::idx_t end_index = (BitMap::idx_t) hr->last_hc_index();
tonyp@3713 1414 _region_bm->par_at_put_range(index, end_index, true);
tonyp@1264 1415 }
tonyp@1264 1416 }
tonyp@1264 1417
johnc@3731 1418 public:
johnc@4123 1419 CMCountDataClosureBase(G1CollectedHeap* g1h,
johnc@3731 1420 BitMap* region_bm, BitMap* card_bm):
johnc@4123 1421 _g1h(g1h), _cm(g1h->concurrent_mark()),
johnc@4123 1422 _ct_bs((CardTableModRefBS*) (g1h->barrier_set())),
johnc@4123 1423 _region_bm(region_bm), _card_bm(card_bm) { }
johnc@3731 1424 };
johnc@3731 1425
johnc@3731 1426 // Closure that calculates the # live objects per region. Used
johnc@3731 1427 // for verification purposes during the cleanup pause.
johnc@3731 1428 class CalcLiveObjectsClosure: public CMCountDataClosureBase {
johnc@3731 1429 CMBitMapRO* _bm;
johnc@3731 1430 size_t _region_marked_bytes;
johnc@3731 1431
johnc@3731 1432 public:
johnc@4123 1433 CalcLiveObjectsClosure(CMBitMapRO *bm, G1CollectedHeap* g1h,
johnc@3731 1434 BitMap* region_bm, BitMap* card_bm) :
johnc@4123 1435 CMCountDataClosureBase(g1h, region_bm, card_bm),
johnc@3731 1436 _bm(bm), _region_marked_bytes(0) { }
johnc@3731 1437
ysr@777 1438 bool doHeapRegion(HeapRegion* hr) {
ysr@777 1439
iveresov@1074 1440 if (hr->continuesHumongous()) {
tonyp@1264 1441 // We will ignore these here and process them when their
tonyp@1264 1442 // associated "starts humongous" region is processed (see
tonyp@1264 1443 // set_bit_for_heap_region()). Note that we cannot rely on their
tonyp@1264 1444 // associated "starts humongous" region to have their bit set to
tonyp@1264 1445 // 1 since, due to the region chunking in the parallel region
tonyp@1264 1446 // iteration, a "continues humongous" region might be visited
tonyp@1264 1447 // before its associated "starts humongous".
iveresov@1074 1448 return false;
iveresov@1074 1449 }
ysr@777 1450
johnc@4123 1451 HeapWord* ntams = hr->next_top_at_mark_start();
johnc@4123 1452 HeapWord* start = hr->bottom();
johnc@4123 1453
johnc@4123 1454 assert(start <= hr->end() && start <= ntams && ntams <= hr->end(),
johnc@3463 1455 err_msg("Preconditions not met - "
johnc@4123 1456 "start: "PTR_FORMAT", ntams: "PTR_FORMAT", end: "PTR_FORMAT,
drchase@6680 1457 p2i(start), p2i(ntams), p2i(hr->end())));
johnc@3463 1458
ysr@777 1459 // Find the first marked object at or after "start".
johnc@4123 1460 start = _bm->getNextMarkedWordAddress(start, ntams);
johnc@3463 1461
ysr@777 1462 size_t marked_bytes = 0;
ysr@777 1463
johnc@4123 1464 while (start < ntams) {
ysr@777 1465 oop obj = oop(start);
ysr@777 1466 int obj_sz = obj->size();
johnc@4123 1467 HeapWord* obj_end = start + obj_sz;
johnc@3731 1468
johnc@3731 1469 BitMap::idx_t start_idx = _cm->card_bitmap_index_for(start);
johnc@4123 1470 BitMap::idx_t end_idx = _cm->card_bitmap_index_for(obj_end);
johnc@4123 1471
johnc@4123 1472 // Note: if we're looking at the last region in heap - obj_end
johnc@4123 1473 // could be actually just beyond the end of the heap; end_idx
johnc@4123 1474 // will then correspond to a (non-existent) card that is also
johnc@4123 1475 // just beyond the heap.
johnc@4123 1476 if (_g1h->is_in_g1_reserved(obj_end) && !_ct_bs->is_card_aligned(obj_end)) {
johnc@4123 1477 // end of object is not card aligned - increment to cover
johnc@4123 1478 // all the cards spanned by the object
johnc@4123 1479 end_idx += 1;
johnc@4123 1480 }
johnc@4123 1481
johnc@4123 1482 // Set the bits in the card BM for the cards spanned by this object.
johnc@4123 1483 _cm->set_card_bitmap_range(_card_bm, start_idx, end_idx, true /* is_par */);
johnc@3731 1484
johnc@3731 1485 // Add the size of this object to the number of marked bytes.
apetrusenko@1465 1486 marked_bytes += (size_t)obj_sz * HeapWordSize;
johnc@3463 1487
ysr@777 1488 // Find the next marked object after this one.
johnc@4123 1489 start = _bm->getNextMarkedWordAddress(obj_end, ntams);
tonyp@2973 1490 }
johnc@3463 1491
johnc@3463 1492 // Mark the allocated-since-marking portion...
johnc@3463 1493 HeapWord* top = hr->top();
johnc@4123 1494 if (ntams < top) {
johnc@4123 1495 BitMap::idx_t start_idx = _cm->card_bitmap_index_for(ntams);
johnc@4123 1496 BitMap::idx_t end_idx = _cm->card_bitmap_index_for(top);
johnc@4123 1497
johnc@4123 1498 // Note: if we're looking at the last region in heap - top
johnc@4123 1499 // could be actually just beyond the end of the heap; end_idx
johnc@4123 1500 // will then correspond to a (non-existent) card that is also
johnc@4123 1501 // just beyond the heap.
johnc@4123 1502 if (_g1h->is_in_g1_reserved(top) && !_ct_bs->is_card_aligned(top)) {
johnc@4123 1503 // end of object is not card aligned - increment to cover
johnc@4123 1504 // all the cards spanned by the object
johnc@4123 1505 end_idx += 1;
johnc@4123 1506 }
johnc@4123 1507 _cm->set_card_bitmap_range(_card_bm, start_idx, end_idx, true /* is_par */);
johnc@3463 1508
johnc@3463 1509 // This definitely means the region has live objects.
johnc@3463 1510 set_bit_for_region(hr);
ysr@777 1511 }
ysr@777 1512
ysr@777 1513 // Update the live region bitmap.
ysr@777 1514 if (marked_bytes > 0) {
tonyp@1264 1515 set_bit_for_region(hr);
ysr@777 1516 }
johnc@3463 1517
johnc@3463 1518 // Set the marked bytes for the current region so that
johnc@3463 1519 // it can be queried by a calling verificiation routine
johnc@3463 1520 _region_marked_bytes = marked_bytes;
johnc@3463 1521
johnc@3463 1522 return false;
johnc@3463 1523 }
johnc@3463 1524
johnc@3463 1525 size_t region_marked_bytes() const { return _region_marked_bytes; }
johnc@3463 1526 };
johnc@3463 1527
johnc@3463 1528 // Heap region closure used for verifying the counting data
johnc@3463 1529 // that was accumulated concurrently and aggregated during
johnc@3463 1530 // the remark pause. This closure is applied to the heap
johnc@3463 1531 // regions during the STW cleanup pause.
johnc@3463 1532
johnc@3463 1533 class VerifyLiveObjectDataHRClosure: public HeapRegionClosure {
johnc@4123 1534 G1CollectedHeap* _g1h;
johnc@3463 1535 ConcurrentMark* _cm;
johnc@3463 1536 CalcLiveObjectsClosure _calc_cl;
johnc@3463 1537 BitMap* _region_bm; // Region BM to be verified
johnc@3463 1538 BitMap* _card_bm; // Card BM to be verified
johnc@3463 1539 bool _verbose; // verbose output?
johnc@3463 1540
johnc@3463 1541 BitMap* _exp_region_bm; // Expected Region BM values
johnc@3463 1542 BitMap* _exp_card_bm; // Expected card BM values
johnc@3463 1543
johnc@3463 1544 int _failures;
johnc@3463 1545
johnc@3463 1546 public:
johnc@4123 1547 VerifyLiveObjectDataHRClosure(G1CollectedHeap* g1h,
johnc@3463 1548 BitMap* region_bm,
johnc@3463 1549 BitMap* card_bm,
johnc@3463 1550 BitMap* exp_region_bm,
johnc@3463 1551 BitMap* exp_card_bm,
johnc@3463 1552 bool verbose) :
johnc@4123 1553 _g1h(g1h), _cm(g1h->concurrent_mark()),
johnc@4123 1554 _calc_cl(_cm->nextMarkBitMap(), g1h, exp_region_bm, exp_card_bm),
johnc@3463 1555 _region_bm(region_bm), _card_bm(card_bm), _verbose(verbose),
johnc@3463 1556 _exp_region_bm(exp_region_bm), _exp_card_bm(exp_card_bm),
johnc@3463 1557 _failures(0) { }
johnc@3463 1558
johnc@3463 1559 int failures() const { return _failures; }
johnc@3463 1560
johnc@3463 1561 bool doHeapRegion(HeapRegion* hr) {
johnc@3463 1562 if (hr->continuesHumongous()) {
johnc@3463 1563 // We will ignore these here and process them when their
johnc@3463 1564 // associated "starts humongous" region is processed (see
johnc@3463 1565 // set_bit_for_heap_region()). Note that we cannot rely on their
johnc@3463 1566 // associated "starts humongous" region to have their bit set to
johnc@3463 1567 // 1 since, due to the region chunking in the parallel region
johnc@3463 1568 // iteration, a "continues humongous" region might be visited
johnc@3463 1569 // before its associated "starts humongous".
johnc@3463 1570 return false;
johnc@3463 1571 }
johnc@3463 1572
johnc@3463 1573 int failures = 0;
johnc@3463 1574
johnc@3463 1575 // Call the CalcLiveObjectsClosure to walk the marking bitmap for
johnc@3463 1576 // this region and set the corresponding bits in the expected region
johnc@3463 1577 // and card bitmaps.
johnc@3463 1578 bool res = _calc_cl.doHeapRegion(hr);
johnc@3463 1579 assert(res == false, "should be continuing");
johnc@3463 1580
johnc@3463 1581 MutexLockerEx x((_verbose ? ParGCRareEvent_lock : NULL),
johnc@3463 1582 Mutex::_no_safepoint_check_flag);
johnc@3463 1583
johnc@3463 1584 // Verify the marked bytes for this region.
johnc@3463 1585 size_t exp_marked_bytes = _calc_cl.region_marked_bytes();
johnc@3463 1586 size_t act_marked_bytes = hr->next_marked_bytes();
johnc@3463 1587
johnc@3463 1588 // We're not OK if expected marked bytes > actual marked bytes. It means
johnc@3463 1589 // we have missed accounting some objects during the actual marking.
johnc@3463 1590 if (exp_marked_bytes > act_marked_bytes) {
johnc@3463 1591 if (_verbose) {
tonyp@3713 1592 gclog_or_tty->print_cr("Region %u: marked bytes mismatch: "
johnc@3463 1593 "expected: " SIZE_FORMAT ", actual: " SIZE_FORMAT,
tschatzl@7091 1594 hr->hrm_index(), exp_marked_bytes, act_marked_bytes);
johnc@3463 1595 }
johnc@3463 1596 failures += 1;
johnc@3463 1597 }
johnc@3463 1598
johnc@3463 1599 // Verify the bit, for this region, in the actual and expected
johnc@3463 1600 // (which was just calculated) region bit maps.
johnc@3463 1601 // We're not OK if the bit in the calculated expected region
johnc@3463 1602 // bitmap is set and the bit in the actual region bitmap is not.
tschatzl@7091 1603 BitMap::idx_t index = (BitMap::idx_t) hr->hrm_index();
johnc@3463 1604
johnc@3463 1605 bool expected = _exp_region_bm->at(index);
johnc@3463 1606 bool actual = _region_bm->at(index);
johnc@3463 1607 if (expected && !actual) {
johnc@3463 1608 if (_verbose) {
tonyp@3713 1609 gclog_or_tty->print_cr("Region %u: region bitmap mismatch: "
tonyp@3713 1610 "expected: %s, actual: %s",
tschatzl@7091 1611 hr->hrm_index(),
tonyp@3713 1612 BOOL_TO_STR(expected), BOOL_TO_STR(actual));
johnc@3463 1613 }
johnc@3463 1614 failures += 1;
johnc@3463 1615 }
johnc@3463 1616
johnc@3463 1617 // Verify that the card bit maps for the cards spanned by the current
johnc@3463 1618 // region match. We have an error if we have a set bit in the expected
johnc@3463 1619 // bit map and the corresponding bit in the actual bitmap is not set.
johnc@3463 1620
johnc@3463 1621 BitMap::idx_t start_idx = _cm->card_bitmap_index_for(hr->bottom());
johnc@3463 1622 BitMap::idx_t end_idx = _cm->card_bitmap_index_for(hr->top());
johnc@3463 1623
johnc@3463 1624 for (BitMap::idx_t i = start_idx; i < end_idx; i+=1) {
johnc@3463 1625 expected = _exp_card_bm->at(i);
johnc@3463 1626 actual = _card_bm->at(i);
johnc@3463 1627
johnc@3463 1628 if (expected && !actual) {
johnc@3463 1629 if (_verbose) {
tonyp@3713 1630 gclog_or_tty->print_cr("Region %u: card bitmap mismatch at " SIZE_FORMAT ": "
tonyp@3713 1631 "expected: %s, actual: %s",
tschatzl@7091 1632 hr->hrm_index(), i,
tonyp@3713 1633 BOOL_TO_STR(expected), BOOL_TO_STR(actual));
ysr@777 1634 }
johnc@3463 1635 failures += 1;
ysr@777 1636 }
ysr@777 1637 }
ysr@777 1638
johnc@3463 1639 if (failures > 0 && _verbose) {
johnc@3463 1640 gclog_or_tty->print_cr("Region " HR_FORMAT ", ntams: " PTR_FORMAT ", "
johnc@3463 1641 "marked_bytes: calc/actual " SIZE_FORMAT "/" SIZE_FORMAT,
drchase@6680 1642 HR_FORMAT_PARAMS(hr), p2i(hr->next_top_at_mark_start()),
johnc@3463 1643 _calc_cl.region_marked_bytes(), hr->next_marked_bytes());
johnc@3463 1644 }
johnc@3463 1645
johnc@3463 1646 _failures += failures;
johnc@3463 1647
johnc@3463 1648 // We could stop iteration over the heap when we
johnc@3731 1649 // find the first violating region by returning true.
ysr@777 1650 return false;
ysr@777 1651 }
ysr@777 1652 };
ysr@777 1653
johnc@3463 1654 class G1ParVerifyFinalCountTask: public AbstractGangTask {
johnc@3463 1655 protected:
johnc@3463 1656 G1CollectedHeap* _g1h;
johnc@3463 1657 ConcurrentMark* _cm;
johnc@3463 1658 BitMap* _actual_region_bm;
johnc@3463 1659 BitMap* _actual_card_bm;
johnc@3463 1660
johnc@3463 1661 uint _n_workers;
johnc@3463 1662
johnc@3463 1663 BitMap* _expected_region_bm;
johnc@3463 1664 BitMap* _expected_card_bm;
johnc@3463 1665
johnc@3463 1666 int _failures;
johnc@3463 1667 bool _verbose;
johnc@3463 1668
johnc@3463 1669 public:
johnc@3463 1670 G1ParVerifyFinalCountTask(G1CollectedHeap* g1h,
johnc@3463 1671 BitMap* region_bm, BitMap* card_bm,
johnc@3463 1672 BitMap* expected_region_bm, BitMap* expected_card_bm)
johnc@3463 1673 : AbstractGangTask("G1 verify final counting"),
johnc@3463 1674 _g1h(g1h), _cm(_g1h->concurrent_mark()),
johnc@3463 1675 _actual_region_bm(region_bm), _actual_card_bm(card_bm),
johnc@3463 1676 _expected_region_bm(expected_region_bm), _expected_card_bm(expected_card_bm),
johnc@3463 1677 _failures(0), _verbose(false),
johnc@3463 1678 _n_workers(0) {
johnc@3463 1679 assert(VerifyDuringGC, "don't call this otherwise");
johnc@3463 1680
johnc@3463 1681 // Use the value already set as the number of active threads
johnc@3463 1682 // in the call to run_task().
johnc@3463 1683 if (G1CollectedHeap::use_parallel_gc_threads()) {
johnc@3463 1684 assert( _g1h->workers()->active_workers() > 0,
johnc@3463 1685 "Should have been previously set");
johnc@3463 1686 _n_workers = _g1h->workers()->active_workers();
johnc@3463 1687 } else {
johnc@3463 1688 _n_workers = 1;
johnc@3463 1689 }
johnc@3463 1690
johnc@3463 1691 assert(_expected_card_bm->size() == _actual_card_bm->size(), "sanity");
johnc@3463 1692 assert(_expected_region_bm->size() == _actual_region_bm->size(), "sanity");
johnc@3463 1693
johnc@3463 1694 _verbose = _cm->verbose_medium();
johnc@3463 1695 }
johnc@3463 1696
johnc@3463 1697 void work(uint worker_id) {
johnc@3463 1698 assert(worker_id < _n_workers, "invariant");
johnc@3463 1699
johnc@4123 1700 VerifyLiveObjectDataHRClosure verify_cl(_g1h,
johnc@3463 1701 _actual_region_bm, _actual_card_bm,
johnc@3463 1702 _expected_region_bm,
johnc@3463 1703 _expected_card_bm,
johnc@3463 1704 _verbose);
johnc@3463 1705
johnc@3463 1706 if (G1CollectedHeap::use_parallel_gc_threads()) {
johnc@3463 1707 _g1h->heap_region_par_iterate_chunked(&verify_cl,
johnc@3463 1708 worker_id,
johnc@3463 1709 _n_workers,
johnc@3463 1710 HeapRegion::VerifyCountClaimValue);
johnc@3463 1711 } else {
johnc@3463 1712 _g1h->heap_region_iterate(&verify_cl);
johnc@3463 1713 }
johnc@3463 1714
johnc@3463 1715 Atomic::add(verify_cl.failures(), &_failures);
johnc@3463 1716 }
johnc@3463 1717
johnc@3463 1718 int failures() const { return _failures; }
johnc@3463 1719 };
johnc@3463 1720
johnc@3731 1721 // Closure that finalizes the liveness counting data.
johnc@3731 1722 // Used during the cleanup pause.
johnc@3731 1723 // Sets the bits corresponding to the interval [NTAMS, top]
johnc@3731 1724 // (which contains the implicitly live objects) in the
johnc@3731 1725 // card liveness bitmap. Also sets the bit for each region,
johnc@3731 1726 // containing live data, in the region liveness bitmap.
johnc@3731 1727
johnc@3731 1728 class FinalCountDataUpdateClosure: public CMCountDataClosureBase {
johnc@3463 1729 public:
johnc@4123 1730 FinalCountDataUpdateClosure(G1CollectedHeap* g1h,
johnc@3463 1731 BitMap* region_bm,
johnc@3463 1732 BitMap* card_bm) :
johnc@4123 1733 CMCountDataClosureBase(g1h, region_bm, card_bm) { }
johnc@3463 1734
johnc@3463 1735 bool doHeapRegion(HeapRegion* hr) {
johnc@3463 1736
johnc@3463 1737 if (hr->continuesHumongous()) {
johnc@3463 1738 // We will ignore these here and process them when their
johnc@3463 1739 // associated "starts humongous" region is processed (see
johnc@3463 1740 // set_bit_for_heap_region()). Note that we cannot rely on their
johnc@3463 1741 // associated "starts humongous" region to have their bit set to
johnc@3463 1742 // 1 since, due to the region chunking in the parallel region
johnc@3463 1743 // iteration, a "continues humongous" region might be visited
johnc@3463 1744 // before its associated "starts humongous".
johnc@3463 1745 return false;
johnc@3463 1746 }
johnc@3463 1747
johnc@3463 1748 HeapWord* ntams = hr->next_top_at_mark_start();
johnc@3463 1749 HeapWord* top = hr->top();
johnc@3463 1750
johnc@3731 1751 assert(hr->bottom() <= ntams && ntams <= hr->end(), "Preconditions.");
johnc@3463 1752
johnc@3463 1753 // Mark the allocated-since-marking portion...
johnc@3463 1754 if (ntams < top) {
johnc@3463 1755 // This definitely means the region has live objects.
johnc@3463 1756 set_bit_for_region(hr);
johnc@4123 1757
johnc@4123 1758 // Now set the bits in the card bitmap for [ntams, top)
johnc@4123 1759 BitMap::idx_t start_idx = _cm->card_bitmap_index_for(ntams);
johnc@4123 1760 BitMap::idx_t end_idx = _cm->card_bitmap_index_for(top);
johnc@4123 1761
johnc@4123 1762 // Note: if we're looking at the last region in heap - top
johnc@4123 1763 // could be actually just beyond the end of the heap; end_idx
johnc@4123 1764 // will then correspond to a (non-existent) card that is also
johnc@4123 1765 // just beyond the heap.
johnc@4123 1766 if (_g1h->is_in_g1_reserved(top) && !_ct_bs->is_card_aligned(top)) {
johnc@4123 1767 // end of object is not card aligned - increment to cover
johnc@4123 1768 // all the cards spanned by the object
johnc@4123 1769 end_idx += 1;
johnc@4123 1770 }
johnc@4123 1771
johnc@4123 1772 assert(end_idx <= _card_bm->size(),
johnc@4123 1773 err_msg("oob: end_idx= "SIZE_FORMAT", bitmap size= "SIZE_FORMAT,
johnc@4123 1774 end_idx, _card_bm->size()));
johnc@4123 1775 assert(start_idx < _card_bm->size(),
johnc@4123 1776 err_msg("oob: start_idx= "SIZE_FORMAT", bitmap size= "SIZE_FORMAT,
johnc@4123 1777 start_idx, _card_bm->size()));
johnc@4123 1778
johnc@4123 1779 _cm->set_card_bitmap_range(_card_bm, start_idx, end_idx, true /* is_par */);
coleenp@4037 1780 }
johnc@3463 1781
johnc@3463 1782 // Set the bit for the region if it contains live data
johnc@3463 1783 if (hr->next_marked_bytes() > 0) {
johnc@3463 1784 set_bit_for_region(hr);
johnc@3463 1785 }
johnc@3463 1786
johnc@3463 1787 return false;
johnc@3463 1788 }
johnc@3463 1789 };
ysr@777 1790
ysr@777 1791 class G1ParFinalCountTask: public AbstractGangTask {
ysr@777 1792 protected:
ysr@777 1793 G1CollectedHeap* _g1h;
johnc@3463 1794 ConcurrentMark* _cm;
johnc@3463 1795 BitMap* _actual_region_bm;
johnc@3463 1796 BitMap* _actual_card_bm;
johnc@3463 1797
jmasa@3357 1798 uint _n_workers;
johnc@3463 1799
ysr@777 1800 public:
johnc@3463 1801 G1ParFinalCountTask(G1CollectedHeap* g1h, BitMap* region_bm, BitMap* card_bm)
johnc@3463 1802 : AbstractGangTask("G1 final counting"),
johnc@3463 1803 _g1h(g1h), _cm(_g1h->concurrent_mark()),
johnc@3463 1804 _actual_region_bm(region_bm), _actual_card_bm(card_bm),
johnc@3463 1805 _n_workers(0) {
jmasa@3294 1806 // Use the value already set as the number of active threads
tonyp@3714 1807 // in the call to run_task().
jmasa@3294 1808 if (G1CollectedHeap::use_parallel_gc_threads()) {
jmasa@3294 1809 assert( _g1h->workers()->active_workers() > 0,
jmasa@3294 1810 "Should have been previously set");
jmasa@3294 1811 _n_workers = _g1h->workers()->active_workers();
tonyp@2973 1812 } else {
ysr@777 1813 _n_workers = 1;
tonyp@2973 1814 }
ysr@777 1815 }
ysr@777 1816
jmasa@3357 1817 void work(uint worker_id) {
johnc@3463 1818 assert(worker_id < _n_workers, "invariant");
johnc@3463 1819
johnc@4123 1820 FinalCountDataUpdateClosure final_update_cl(_g1h,
johnc@3463 1821 _actual_region_bm,
johnc@3463 1822 _actual_card_bm);
johnc@3463 1823
jmasa@2188 1824 if (G1CollectedHeap::use_parallel_gc_threads()) {
johnc@3463 1825 _g1h->heap_region_par_iterate_chunked(&final_update_cl,
johnc@3463 1826 worker_id,
johnc@3463 1827 _n_workers,
tonyp@790 1828 HeapRegion::FinalCountClaimValue);
ysr@777 1829 } else {
johnc@3463 1830 _g1h->heap_region_iterate(&final_update_cl);
ysr@777 1831 }
ysr@777 1832 }
ysr@777 1833 };
ysr@777 1834
ysr@777 1835 class G1ParNoteEndTask;
ysr@777 1836
ysr@777 1837 class G1NoteEndOfConcMarkClosure : public HeapRegionClosure {
ysr@777 1838 G1CollectedHeap* _g1;
ysr@777 1839 size_t _max_live_bytes;
tonyp@3713 1840 uint _regions_claimed;
ysr@777 1841 size_t _freed_bytes;
tonyp@2493 1842 FreeRegionList* _local_cleanup_list;
brutisso@6385 1843 HeapRegionSetCount _old_regions_removed;
brutisso@6385 1844 HeapRegionSetCount _humongous_regions_removed;
tonyp@2493 1845 HRRSCleanupTask* _hrrs_cleanup_task;
ysr@777 1846 double _claimed_region_time;
ysr@777 1847 double _max_region_time;
ysr@777 1848
ysr@777 1849 public:
ysr@777 1850 G1NoteEndOfConcMarkClosure(G1CollectedHeap* g1,
tonyp@2493 1851 FreeRegionList* local_cleanup_list,
johnc@3292 1852 HRRSCleanupTask* hrrs_cleanup_task) :
vkempik@6552 1853 _g1(g1),
johnc@3292 1854 _max_live_bytes(0), _regions_claimed(0),
johnc@3292 1855 _freed_bytes(0),
johnc@3292 1856 _claimed_region_time(0.0), _max_region_time(0.0),
johnc@3292 1857 _local_cleanup_list(local_cleanup_list),
brutisso@6385 1858 _old_regions_removed(),
brutisso@6385 1859 _humongous_regions_removed(),
johnc@3292 1860 _hrrs_cleanup_task(hrrs_cleanup_task) { }
johnc@3292 1861
ysr@777 1862 size_t freed_bytes() { return _freed_bytes; }
brutisso@6385 1863 const HeapRegionSetCount& old_regions_removed() { return _old_regions_removed; }
brutisso@6385 1864 const HeapRegionSetCount& humongous_regions_removed() { return _humongous_regions_removed; }
ysr@777 1865
johnc@3292 1866 bool doHeapRegion(HeapRegion *hr) {
tonyp@3957 1867 if (hr->continuesHumongous()) {
tonyp@3957 1868 return false;
tonyp@3957 1869 }
johnc@3292 1870 // We use a claim value of zero here because all regions
johnc@3292 1871 // were claimed with value 1 in the FinalCount task.
tonyp@3957 1872 _g1->reset_gc_time_stamps(hr);
tonyp@3957 1873 double start = os::elapsedTime();
tonyp@3957 1874 _regions_claimed++;
tonyp@3957 1875 hr->note_end_of_marking();
tonyp@3957 1876 _max_live_bytes += hr->max_live_bytes();
brutisso@6385 1877
brutisso@6385 1878 if (hr->used() > 0 && hr->max_live_bytes() == 0 && !hr->is_young()) {
brutisso@6385 1879 _freed_bytes += hr->used();
brutisso@6385 1880 hr->set_containing_set(NULL);
brutisso@6385 1881 if (hr->isHumongous()) {
brutisso@6385 1882 assert(hr->startsHumongous(), "we should only see starts humongous");
brutisso@6385 1883 _humongous_regions_removed.increment(1u, hr->capacity());
brutisso@6385 1884 _g1->free_humongous_region(hr, _local_cleanup_list, true);
brutisso@6385 1885 } else {
brutisso@6385 1886 _old_regions_removed.increment(1u, hr->capacity());
brutisso@6385 1887 _g1->free_region(hr, _local_cleanup_list, true);
brutisso@6385 1888 }
brutisso@6385 1889 } else {
brutisso@6385 1890 hr->rem_set()->do_cleanup_work(_hrrs_cleanup_task);
brutisso@6385 1891 }
brutisso@6385 1892
tonyp@3957 1893 double region_time = (os::elapsedTime() - start);
tonyp@3957 1894 _claimed_region_time += region_time;
tonyp@3957 1895 if (region_time > _max_region_time) {
tonyp@3957 1896 _max_region_time = region_time;
johnc@3292 1897 }
johnc@3292 1898 return false;
johnc@3292 1899 }
ysr@777 1900
ysr@777 1901 size_t max_live_bytes() { return _max_live_bytes; }
tonyp@3713 1902 uint regions_claimed() { return _regions_claimed; }
ysr@777 1903 double claimed_region_time_sec() { return _claimed_region_time; }
ysr@777 1904 double max_region_time_sec() { return _max_region_time; }
ysr@777 1905 };
ysr@777 1906
ysr@777 1907 class G1ParNoteEndTask: public AbstractGangTask {
ysr@777 1908 friend class G1NoteEndOfConcMarkClosure;
tonyp@2472 1909
ysr@777 1910 protected:
ysr@777 1911 G1CollectedHeap* _g1h;
ysr@777 1912 size_t _max_live_bytes;
ysr@777 1913 size_t _freed_bytes;
tonyp@2472 1914 FreeRegionList* _cleanup_list;
tonyp@2472 1915
ysr@777 1916 public:
ysr@777 1917 G1ParNoteEndTask(G1CollectedHeap* g1h,
tonyp@2472 1918 FreeRegionList* cleanup_list) :
ysr@777 1919 AbstractGangTask("G1 note end"), _g1h(g1h),
tonyp@2472 1920 _max_live_bytes(0), _freed_bytes(0), _cleanup_list(cleanup_list) { }
ysr@777 1921
jmasa@3357 1922 void work(uint worker_id) {
ysr@777 1923 double start = os::elapsedTime();
tonyp@2493 1924 FreeRegionList local_cleanup_list("Local Cleanup List");
tonyp@2493 1925 HRRSCleanupTask hrrs_cleanup_task;
vkempik@6552 1926 G1NoteEndOfConcMarkClosure g1_note_end(_g1h, &local_cleanup_list,
tonyp@2493 1927 &hrrs_cleanup_task);
jmasa@2188 1928 if (G1CollectedHeap::use_parallel_gc_threads()) {
jmasa@3357 1929 _g1h->heap_region_par_iterate_chunked(&g1_note_end, worker_id,
jmasa@3294 1930 _g1h->workers()->active_workers(),
tonyp@790 1931 HeapRegion::NoteEndClaimValue);
ysr@777 1932 } else {
ysr@777 1933 _g1h->heap_region_iterate(&g1_note_end);
ysr@777 1934 }
ysr@777 1935 assert(g1_note_end.complete(), "Shouldn't have yielded!");
ysr@777 1936
tonyp@2472 1937 // Now update the lists
brutisso@6385 1938 _g1h->remove_from_old_sets(g1_note_end.old_regions_removed(), g1_note_end.humongous_regions_removed());
ysr@777 1939 {
ysr@777 1940 MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);
brutisso@6385 1941 _g1h->decrement_summary_bytes(g1_note_end.freed_bytes());
ysr@777 1942 _max_live_bytes += g1_note_end.max_live_bytes();
ysr@777 1943 _freed_bytes += g1_note_end.freed_bytes();
tonyp@2472 1944
tonyp@2975 1945 // If we iterate over the global cleanup list at the end of
tonyp@2975 1946 // cleanup to do this printing we will not guarantee to only
tonyp@2975 1947 // generate output for the newly-reclaimed regions (the list
tonyp@2975 1948 // might not be empty at the beginning of cleanup; we might
tonyp@2975 1949 // still be working on its previous contents). So we do the
tonyp@2975 1950 // printing here, before we append the new regions to the global
tonyp@2975 1951 // cleanup list.
tonyp@2975 1952
tonyp@2975 1953 G1HRPrinter* hr_printer = _g1h->hr_printer();
tonyp@2975 1954 if (hr_printer->is_active()) {
brutisso@6385 1955 FreeRegionListIterator iter(&local_cleanup_list);
tonyp@2975 1956 while (iter.more_available()) {
tonyp@2975 1957 HeapRegion* hr = iter.get_next();
tonyp@2975 1958 hr_printer->cleanup(hr);
tonyp@2975 1959 }
tonyp@2975 1960 }
tonyp@2975 1961
jwilhelm@6422 1962 _cleanup_list->add_ordered(&local_cleanup_list);
tonyp@2493 1963 assert(local_cleanup_list.is_empty(), "post-condition");
tonyp@2493 1964
tonyp@2493 1965 HeapRegionRemSet::finish_cleanup_task(&hrrs_cleanup_task);
ysr@777 1966 }
ysr@777 1967 }
ysr@777 1968 size_t max_live_bytes() { return _max_live_bytes; }
ysr@777 1969 size_t freed_bytes() { return _freed_bytes; }
ysr@777 1970 };
ysr@777 1971
ysr@777 1972 class G1ParScrubRemSetTask: public AbstractGangTask {
ysr@777 1973 protected:
ysr@777 1974 G1RemSet* _g1rs;
ysr@777 1975 BitMap* _region_bm;
ysr@777 1976 BitMap* _card_bm;
ysr@777 1977 public:
ysr@777 1978 G1ParScrubRemSetTask(G1CollectedHeap* g1h,
ysr@777 1979 BitMap* region_bm, BitMap* card_bm) :
ysr@777 1980 AbstractGangTask("G1 ScrubRS"), _g1rs(g1h->g1_rem_set()),
johnc@3463 1981 _region_bm(region_bm), _card_bm(card_bm) { }
ysr@777 1982
jmasa@3357 1983 void work(uint worker_id) {
jmasa@2188 1984 if (G1CollectedHeap::use_parallel_gc_threads()) {
jmasa@3357 1985 _g1rs->scrub_par(_region_bm, _card_bm, worker_id,
tonyp@790 1986 HeapRegion::ScrubRemSetClaimValue);
ysr@777 1987 } else {
ysr@777 1988 _g1rs->scrub(_region_bm, _card_bm);
ysr@777 1989 }
ysr@777 1990 }
ysr@777 1991
ysr@777 1992 };
ysr@777 1993
ysr@777 1994 void ConcurrentMark::cleanup() {
ysr@777 1995 // world is stopped at this checkpoint
ysr@777 1996 assert(SafepointSynchronize::is_at_safepoint(),
ysr@777 1997 "world should be stopped");
ysr@777 1998 G1CollectedHeap* g1h = G1CollectedHeap::heap();
ysr@777 1999
ysr@777 2000 // If a full collection has happened, we shouldn't do this.
ysr@777 2001 if (has_aborted()) {
ysr@777 2002 g1h->set_marking_complete(); // So bitmap clearing isn't confused
ysr@777 2003 return;
ysr@777 2004 }
ysr@777 2005
tonyp@2472 2006 g1h->verify_region_sets_optional();
tonyp@2472 2007
ysr@1280 2008 if (VerifyDuringGC) {
ysr@1280 2009 HandleMark hm; // handle scope
ysr@1280 2010 Universe::heap()->prepare_for_verify();
stefank@5018 2011 Universe::verify(VerifyOption_G1UsePrevMarking,
stefank@5018 2012 " VerifyDuringGC:(before)");
ysr@1280 2013 }
brutisso@7005 2014 g1h->check_bitmaps("Cleanup Start");
ysr@1280 2015
ysr@777 2016 G1CollectorPolicy* g1p = G1CollectedHeap::heap()->g1_policy();
ysr@777 2017 g1p->record_concurrent_mark_cleanup_start();
ysr@777 2018
ysr@777 2019 double start = os::elapsedTime();
ysr@777 2020
tonyp@2493 2021 HeapRegionRemSet::reset_for_cleanup_tasks();
tonyp@2493 2022
jmasa@3357 2023 uint n_workers;
jmasa@3294 2024
ysr@777 2025 // Do counting once more with the world stopped for good measure.
johnc@3463 2026 G1ParFinalCountTask g1_par_count_task(g1h, &_region_bm, &_card_bm);
johnc@3463 2027
jmasa@2188 2028 if (G1CollectedHeap::use_parallel_gc_threads()) {
johnc@3463 2029 assert(g1h->check_heap_region_claim_values(HeapRegion::InitialClaimValue),
tonyp@790 2030 "sanity check");
tonyp@790 2031
johnc@3338 2032 g1h->set_par_threads();
johnc@3338 2033 n_workers = g1h->n_par_threads();
jmasa@3357 2034 assert(g1h->n_par_threads() == n_workers,
johnc@3338 2035 "Should not have been reset");
ysr@777 2036 g1h->workers()->run_task(&g1_par_count_task);
jmasa@3294 2037 // Done with the parallel phase so reset to 0.
ysr@777 2038 g1h->set_par_threads(0);
tonyp@790 2039
johnc@3463 2040 assert(g1h->check_heap_region_claim_values(HeapRegion::FinalCountClaimValue),
tonyp@790 2041 "sanity check");
ysr@777 2042 } else {
johnc@3338 2043 n_workers = 1;
ysr@777 2044 g1_par_count_task.work(0);
ysr@777 2045 }
ysr@777 2046
johnc@3463 2047 if (VerifyDuringGC) {
johnc@3463 2048 // Verify that the counting data accumulated during marking matches
johnc@3463 2049 // that calculated by walking the marking bitmap.
johnc@3463 2050
johnc@3463 2051 // Bitmaps to hold expected values
mgerdin@6977 2052 BitMap expected_region_bm(_region_bm.size(), true);
mgerdin@6977 2053 BitMap expected_card_bm(_card_bm.size(), true);
johnc@3463 2054
johnc@3463 2055 G1ParVerifyFinalCountTask g1_par_verify_task(g1h,
johnc@3463 2056 &_region_bm,
johnc@3463 2057 &_card_bm,
johnc@3463 2058 &expected_region_bm,
johnc@3463 2059 &expected_card_bm);
johnc@3463 2060
johnc@3463 2061 if (G1CollectedHeap::use_parallel_gc_threads()) {
johnc@3463 2062 g1h->set_par_threads((int)n_workers);
johnc@3463 2063 g1h->workers()->run_task(&g1_par_verify_task);
johnc@3463 2064 // Done with the parallel phase so reset to 0.
johnc@3463 2065 g1h->set_par_threads(0);
johnc@3463 2066
johnc@3463 2067 assert(g1h->check_heap_region_claim_values(HeapRegion::VerifyCountClaimValue),
johnc@3463 2068 "sanity check");
johnc@3463 2069 } else {
johnc@3463 2070 g1_par_verify_task.work(0);
johnc@3463 2071 }
johnc@3463 2072
johnc@3463 2073 guarantee(g1_par_verify_task.failures() == 0, "Unexpected accounting failures");
johnc@3463 2074 }
johnc@3463 2075
ysr@777 2076 size_t start_used_bytes = g1h->used();
ysr@777 2077 g1h->set_marking_complete();
ysr@777 2078
ysr@777 2079 double count_end = os::elapsedTime();
ysr@777 2080 double this_final_counting_time = (count_end - start);
ysr@777 2081 _total_counting_time += this_final_counting_time;
ysr@777 2082
tonyp@2717 2083 if (G1PrintRegionLivenessInfo) {
tonyp@2717 2084 G1PrintRegionLivenessInfoClosure cl(gclog_or_tty, "Post-Marking");
tonyp@2717 2085 _g1h->heap_region_iterate(&cl);
tonyp@2717 2086 }
tonyp@2717 2087
ysr@777 2088 // Install newly created mark bitMap as "prev".
ysr@777 2089 swapMarkBitMaps();
ysr@777 2090
ysr@777 2091 g1h->reset_gc_time_stamp();
ysr@777 2092
ysr@777 2093 // Note end of marking in all heap regions.
tonyp@2472 2094 G1ParNoteEndTask g1_par_note_end_task(g1h, &_cleanup_list);
jmasa@2188 2095 if (G1CollectedHeap::use_parallel_gc_threads()) {
jmasa@3294 2096 g1h->set_par_threads((int)n_workers);
ysr@777 2097 g1h->workers()->run_task(&g1_par_note_end_task);
ysr@777 2098 g1h->set_par_threads(0);
tonyp@790 2099
tonyp@790 2100 assert(g1h->check_heap_region_claim_values(HeapRegion::NoteEndClaimValue),
tonyp@790 2101 "sanity check");
ysr@777 2102 } else {
ysr@777 2103 g1_par_note_end_task.work(0);
ysr@777 2104 }
tonyp@3957 2105 g1h->check_gc_time_stamps();
tonyp@2472 2106
tonyp@2472 2107 if (!cleanup_list_is_empty()) {
tonyp@2472 2108 // The cleanup list is not empty, so we'll have to process it
tonyp@2472 2109 // concurrently. Notify anyone else that might be wanting free
tonyp@2472 2110 // regions that there will be more free regions coming soon.
tonyp@2472 2111 g1h->set_free_regions_coming();
tonyp@2472 2112 }
ysr@777 2113
ysr@777 2114 // call below, since it affects the metric by which we sort the heap
ysr@777 2115 // regions.
ysr@777 2116 if (G1ScrubRemSets) {
ysr@777 2117 double rs_scrub_start = os::elapsedTime();
ysr@777 2118 G1ParScrubRemSetTask g1_par_scrub_rs_task(g1h, &_region_bm, &_card_bm);
jmasa@2188 2119 if (G1CollectedHeap::use_parallel_gc_threads()) {
jmasa@3294 2120 g1h->set_par_threads((int)n_workers);
ysr@777 2121 g1h->workers()->run_task(&g1_par_scrub_rs_task);
ysr@777 2122 g1h->set_par_threads(0);
tonyp@790 2123
tonyp@790 2124 assert(g1h->check_heap_region_claim_values(
tonyp@790 2125 HeapRegion::ScrubRemSetClaimValue),
tonyp@790 2126 "sanity check");
ysr@777 2127 } else {
ysr@777 2128 g1_par_scrub_rs_task.work(0);
ysr@777 2129 }
ysr@777 2130
ysr@777 2131 double rs_scrub_end = os::elapsedTime();
ysr@777 2132 double this_rs_scrub_time = (rs_scrub_end - rs_scrub_start);
ysr@777 2133 _total_rs_scrub_time += this_rs_scrub_time;
ysr@777 2134 }
ysr@777 2135
ysr@777 2136 // this will also free any regions totally full of garbage objects,
ysr@777 2137 // and sort the regions.
jmasa@3294 2138 g1h->g1_policy()->record_concurrent_mark_cleanup_end((int)n_workers);
ysr@777 2139
ysr@777 2140 // Statistics.
ysr@777 2141 double end = os::elapsedTime();
ysr@777 2142 _cleanup_times.add((end - start) * 1000.0);
ysr@777 2143
brutisso@3710 2144 if (G1Log::fine()) {
ysr@777 2145 g1h->print_size_transition(gclog_or_tty,
ysr@777 2146 start_used_bytes,
ysr@777 2147 g1h->used(),
ysr@777 2148 g1h->capacity());
ysr@777 2149 }
ysr@777 2150
johnc@3175 2151 // Clean up will have freed any regions completely full of garbage.
johnc@3175 2152 // Update the soft reference policy with the new heap occupancy.
johnc@3175 2153 Universe::update_heap_info_at_gc();
johnc@3175 2154
johnc@1186 2155 if (VerifyDuringGC) {
ysr@1280 2156 HandleMark hm; // handle scope
ysr@1280 2157 Universe::heap()->prepare_for_verify();
stefank@5018 2158 Universe::verify(VerifyOption_G1UsePrevMarking,
stefank@5018 2159 " VerifyDuringGC:(after)");
ysr@777 2160 }
brutisso@7005 2161 g1h->check_bitmaps("Cleanup End");
tonyp@2472 2162
tonyp@2472 2163 g1h->verify_region_sets_optional();
stefank@6992 2164
stefank@6992 2165 // We need to make this be a "collection" so any collection pause that
stefank@6992 2166 // races with it goes around and waits for completeCleanup to finish.
stefank@6992 2167 g1h->increment_total_collections();
stefank@6992 2168
stefank@6992 2169 // Clean out dead classes and update Metaspace sizes.
stefank@6996 2170 if (ClassUnloadingWithConcurrentMark) {
stefank@6996 2171 ClassLoaderDataGraph::purge();
stefank@6996 2172 }
stefank@6992 2173 MetaspaceGC::compute_new_size();
stefank@6992 2174
stefank@6992 2175 // We reclaimed old regions so we should calculate the sizes to make
stefank@6992 2176 // sure we update the old gen/space data.
stefank@6992 2177 g1h->g1mm()->update_sizes();
sjohanss@7370 2178 g1h->allocation_context_stats().update_after_mark();
stefank@6992 2179
sla@5237 2180 g1h->trace_heap_after_concurrent_cycle();
ysr@777 2181 }
ysr@777 2182
ysr@777 2183 void ConcurrentMark::completeCleanup() {
ysr@777 2184 if (has_aborted()) return;
ysr@777 2185
tonyp@2472 2186 G1CollectedHeap* g1h = G1CollectedHeap::heap();
tonyp@2472 2187
jwilhelm@6549 2188 _cleanup_list.verify_optional();
tonyp@2643 2189 FreeRegionList tmp_free_list("Tmp Free List");
tonyp@2472 2190
tonyp@2472 2191 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 2192 gclog_or_tty->print_cr("G1ConcRegionFreeing [complete cleanup] : "
tonyp@3713 2193 "cleanup list has %u entries",
tonyp@2472 2194 _cleanup_list.length());
tonyp@2472 2195 }
tonyp@2472 2196
tschatzl@7051 2197 // No one else should be accessing the _cleanup_list at this point,
tschatzl@7051 2198 // so it is not necessary to take any locks
tonyp@2472 2199 while (!_cleanup_list.is_empty()) {
tschatzl@7050 2200 HeapRegion* hr = _cleanup_list.remove_region(true /* from_head */);
jwilhelm@6422 2201 assert(hr != NULL, "Got NULL from a non-empty list");
tonyp@2849 2202 hr->par_clear();
jwilhelm@6422 2203 tmp_free_list.add_ordered(hr);
tonyp@2472 2204
tonyp@2472 2205 // Instead of adding one region at a time to the secondary_free_list,
tonyp@2472 2206 // we accumulate them in the local list and move them a few at a
tonyp@2472 2207 // time. This also cuts down on the number of notify_all() calls
tonyp@2472 2208 // we do during this process. We'll also append the local list when
tonyp@2472 2209 // _cleanup_list is empty (which means we just removed the last
tonyp@2472 2210 // region from the _cleanup_list).
tonyp@2643 2211 if ((tmp_free_list.length() % G1SecondaryFreeListAppendLength == 0) ||
tonyp@2472 2212 _cleanup_list.is_empty()) {
tonyp@2472 2213 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 2214 gclog_or_tty->print_cr("G1ConcRegionFreeing [complete cleanup] : "
tonyp@3713 2215 "appending %u entries to the secondary_free_list, "
tonyp@3713 2216 "cleanup list still has %u entries",
tonyp@2643 2217 tmp_free_list.length(),
tonyp@2472 2218 _cleanup_list.length());
ysr@777 2219 }
tonyp@2472 2220
tonyp@2472 2221 {
tonyp@2472 2222 MutexLockerEx x(SecondaryFreeList_lock, Mutex::_no_safepoint_check_flag);
jwilhelm@6422 2223 g1h->secondary_free_list_add(&tmp_free_list);
tonyp@2472 2224 SecondaryFreeList_lock->notify_all();
tonyp@2472 2225 }
tonyp@2472 2226
tonyp@2472 2227 if (G1StressConcRegionFreeing) {
tonyp@2472 2228 for (uintx i = 0; i < G1StressConcRegionFreeingDelayMillis; ++i) {
tonyp@2472 2229 os::sleep(Thread::current(), (jlong) 1, false);
tonyp@2472 2230 }
tonyp@2472 2231 }
ysr@777 2232 }
ysr@777 2233 }
tonyp@2643 2234 assert(tmp_free_list.is_empty(), "post-condition");
ysr@777 2235 }
ysr@777 2236
johnc@4555 2237 // Supporting Object and Oop closures for reference discovery
johnc@4555 2238 // and processing in during marking
johnc@2494 2239
johnc@2379 2240 bool G1CMIsAliveClosure::do_object_b(oop obj) {
johnc@2379 2241 HeapWord* addr = (HeapWord*)obj;
johnc@2379 2242 return addr != NULL &&
johnc@2379 2243 (!_g1->is_in_g1_reserved(addr) || !_g1->is_obj_ill(obj));
johnc@2379 2244 }
ysr@777 2245
johnc@4555 2246 // 'Keep Alive' oop closure used by both serial parallel reference processing.
johnc@4555 2247 // Uses the CMTask associated with a worker thread (for serial reference
johnc@4555 2248 // processing the CMTask for worker 0 is used) to preserve (mark) and
johnc@4555 2249 // trace referent objects.
johnc@4555 2250 //
johnc@4555 2251 // Using the CMTask and embedded local queues avoids having the worker
johnc@4555 2252 // threads operating on the global mark stack. This reduces the risk
johnc@4555 2253 // of overflowing the stack - which we would rather avoid at this late
johnc@4555 2254 // state. Also using the tasks' local queues removes the potential
johnc@4555 2255 // of the workers interfering with each other that could occur if
johnc@4555 2256 // operating on the global stack.
johnc@4555 2257
johnc@4555 2258 class G1CMKeepAliveAndDrainClosure: public OopClosure {
johnc@4787 2259 ConcurrentMark* _cm;
johnc@4787 2260 CMTask* _task;
johnc@4787 2261 int _ref_counter_limit;
johnc@4787 2262 int _ref_counter;
johnc@4787 2263 bool _is_serial;
johnc@2494 2264 public:
johnc@4787 2265 G1CMKeepAliveAndDrainClosure(ConcurrentMark* cm, CMTask* task, bool is_serial) :
johnc@4787 2266 _cm(cm), _task(task), _is_serial(is_serial),
johnc@4787 2267 _ref_counter_limit(G1RefProcDrainInterval) {
johnc@2494 2268 assert(_ref_counter_limit > 0, "sanity");
johnc@4787 2269 assert(!_is_serial || _task->worker_id() == 0, "only task 0 for serial code");
johnc@2494 2270 _ref_counter = _ref_counter_limit;
johnc@2494 2271 }
johnc@2494 2272
johnc@2494 2273 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
johnc@2494 2274 virtual void do_oop( oop* p) { do_oop_work(p); }
johnc@2494 2275
johnc@2494 2276 template <class T> void do_oop_work(T* p) {
johnc@2494 2277 if (!_cm->has_overflown()) {
johnc@2494 2278 oop obj = oopDesc::load_decode_heap_oop(p);
tonyp@2973 2279 if (_cm->verbose_high()) {
johnc@4173 2280 gclog_or_tty->print_cr("\t[%u] we're looking at location "
johnc@2494 2281 "*"PTR_FORMAT" = "PTR_FORMAT,
drchase@6680 2282 _task->worker_id(), p2i(p), p2i((void*) obj));
tonyp@2973 2283 }
johnc@2494 2284
johnc@2494 2285 _task->deal_with_reference(obj);
johnc@2494 2286 _ref_counter--;
johnc@2494 2287
johnc@2494 2288 if (_ref_counter == 0) {
johnc@4555 2289 // We have dealt with _ref_counter_limit references, pushing them
johnc@4555 2290 // and objects reachable from them on to the local stack (and
johnc@4555 2291 // possibly the global stack). Call CMTask::do_marking_step() to
johnc@4555 2292 // process these entries.
johnc@4555 2293 //
johnc@4555 2294 // We call CMTask::do_marking_step() in a loop, which we'll exit if
johnc@4555 2295 // there's nothing more to do (i.e. we're done with the entries that
johnc@4555 2296 // were pushed as a result of the CMTask::deal_with_reference() calls
johnc@4555 2297 // above) or we overflow.
johnc@4555 2298 //
johnc@4555 2299 // Note: CMTask::do_marking_step() can set the CMTask::has_aborted()
johnc@4555 2300 // flag while there may still be some work to do. (See the comment at
johnc@4555 2301 // the beginning of CMTask::do_marking_step() for those conditions -
johnc@4555 2302 // one of which is reaching the specified time target.) It is only
johnc@4555 2303 // when CMTask::do_marking_step() returns without setting the
johnc@4555 2304 // has_aborted() flag that the marking step has completed.
johnc@2494 2305 do {
johnc@2494 2306 double mark_step_duration_ms = G1ConcMarkStepDurationMillis;
johnc@2494 2307 _task->do_marking_step(mark_step_duration_ms,
johnc@4787 2308 false /* do_termination */,
johnc@4787 2309 _is_serial);
johnc@2494 2310 } while (_task->has_aborted() && !_cm->has_overflown());
johnc@2494 2311 _ref_counter = _ref_counter_limit;
johnc@2494 2312 }
johnc@2494 2313 } else {
tonyp@2973 2314 if (_cm->verbose_high()) {
johnc@4173 2315 gclog_or_tty->print_cr("\t[%u] CM Overflow", _task->worker_id());
tonyp@2973 2316 }
johnc@2494 2317 }
johnc@2494 2318 }
johnc@2494 2319 };
johnc@2494 2320
johnc@4555 2321 // 'Drain' oop closure used by both serial and parallel reference processing.
johnc@4555 2322 // Uses the CMTask associated with a given worker thread (for serial
johnc@4555 2323 // reference processing the CMtask for worker 0 is used). Calls the
johnc@4555 2324 // do_marking_step routine, with an unbelievably large timeout value,
johnc@4555 2325 // to drain the marking data structures of the remaining entries
johnc@4555 2326 // added by the 'keep alive' oop closure above.
johnc@4555 2327
johnc@4555 2328 class G1CMDrainMarkingStackClosure: public VoidClosure {
johnc@2494 2329 ConcurrentMark* _cm;
johnc@4555 2330 CMTask* _task;
johnc@4787 2331 bool _is_serial;
johnc@2494 2332 public:
johnc@4787 2333 G1CMDrainMarkingStackClosure(ConcurrentMark* cm, CMTask* task, bool is_serial) :
johnc@4787 2334 _cm(cm), _task(task), _is_serial(is_serial) {
johnc@4787 2335 assert(!_is_serial || _task->worker_id() == 0, "only task 0 for serial code");
johnc@4555 2336 }
johnc@2494 2337
johnc@2494 2338 void do_void() {
johnc@2494 2339 do {
tonyp@2973 2340 if (_cm->verbose_high()) {
johnc@4787 2341 gclog_or_tty->print_cr("\t[%u] Drain: Calling do_marking_step - serial: %s",
johnc@4787 2342 _task->worker_id(), BOOL_TO_STR(_is_serial));
tonyp@2973 2343 }
johnc@2494 2344
johnc@4555 2345 // We call CMTask::do_marking_step() to completely drain the local
johnc@4555 2346 // and global marking stacks of entries pushed by the 'keep alive'
johnc@4555 2347 // oop closure (an instance of G1CMKeepAliveAndDrainClosure above).
johnc@4555 2348 //
johnc@4555 2349 // CMTask::do_marking_step() is called in a loop, which we'll exit
johnc@4555 2350 // if there's nothing more to do (i.e. we'completely drained the
johnc@4555 2351 // entries that were pushed as a a result of applying the 'keep alive'
johnc@4555 2352 // closure to the entries on the discovered ref lists) or we overflow
johnc@4555 2353 // the global marking stack.
johnc@4555 2354 //
johnc@4555 2355 // Note: CMTask::do_marking_step() can set the CMTask::has_aborted()
johnc@4555 2356 // flag while there may still be some work to do. (See the comment at
johnc@4555 2357 // the beginning of CMTask::do_marking_step() for those conditions -
johnc@4555 2358 // one of which is reaching the specified time target.) It is only
johnc@4555 2359 // when CMTask::do_marking_step() returns without setting the
johnc@4555 2360 // has_aborted() flag that the marking step has completed.
johnc@2494 2361
johnc@2494 2362 _task->do_marking_step(1000000000.0 /* something very large */,
johnc@4787 2363 true /* do_termination */,
johnc@4787 2364 _is_serial);
johnc@2494 2365 } while (_task->has_aborted() && !_cm->has_overflown());
johnc@2494 2366 }
johnc@2494 2367 };
johnc@2494 2368
johnc@3175 2369 // Implementation of AbstractRefProcTaskExecutor for parallel
johnc@3175 2370 // reference processing at the end of G1 concurrent marking
johnc@3175 2371
johnc@3175 2372 class G1CMRefProcTaskExecutor: public AbstractRefProcTaskExecutor {
johnc@2494 2373 private:
johnc@2494 2374 G1CollectedHeap* _g1h;
johnc@2494 2375 ConcurrentMark* _cm;
johnc@2494 2376 WorkGang* _workers;
johnc@2494 2377 int _active_workers;
johnc@2494 2378
johnc@2494 2379 public:
johnc@3175 2380 G1CMRefProcTaskExecutor(G1CollectedHeap* g1h,
johnc@2494 2381 ConcurrentMark* cm,
johnc@2494 2382 WorkGang* workers,
johnc@2494 2383 int n_workers) :
johnc@3292 2384 _g1h(g1h), _cm(cm),
johnc@3292 2385 _workers(workers), _active_workers(n_workers) { }
johnc@2494 2386
johnc@2494 2387 // Executes the given task using concurrent marking worker threads.
johnc@2494 2388 virtual void execute(ProcessTask& task);
johnc@2494 2389 virtual void execute(EnqueueTask& task);
johnc@2494 2390 };
johnc@2494 2391
johnc@3175 2392 class G1CMRefProcTaskProxy: public AbstractGangTask {
johnc@2494 2393 typedef AbstractRefProcTaskExecutor::ProcessTask ProcessTask;
johnc@2494 2394 ProcessTask& _proc_task;
johnc@2494 2395 G1CollectedHeap* _g1h;
johnc@2494 2396 ConcurrentMark* _cm;
johnc@2494 2397
johnc@2494 2398 public:
johnc@3175 2399 G1CMRefProcTaskProxy(ProcessTask& proc_task,
johnc@2494 2400 G1CollectedHeap* g1h,
johnc@3292 2401 ConcurrentMark* cm) :
johnc@2494 2402 AbstractGangTask("Process reference objects in parallel"),
johnc@4555 2403 _proc_task(proc_task), _g1h(g1h), _cm(cm) {
johnc@4787 2404 ReferenceProcessor* rp = _g1h->ref_processor_cm();
johnc@4787 2405 assert(rp->processing_is_mt(), "shouldn't be here otherwise");
johnc@4787 2406 }
johnc@2494 2407
jmasa@3357 2408 virtual void work(uint worker_id) {
mdoerr@7020 2409 ResourceMark rm;
mdoerr@7020 2410 HandleMark hm;
johnc@4787 2411 CMTask* task = _cm->task(worker_id);
johnc@2494 2412 G1CMIsAliveClosure g1_is_alive(_g1h);
johnc@4787 2413 G1CMKeepAliveAndDrainClosure g1_par_keep_alive(_cm, task, false /* is_serial */);
johnc@4787 2414 G1CMDrainMarkingStackClosure g1_par_drain(_cm, task, false /* is_serial */);
johnc@2494 2415
jmasa@3357 2416 _proc_task.work(worker_id, g1_is_alive, g1_par_keep_alive, g1_par_drain);
johnc@2494 2417 }
johnc@2494 2418 };
johnc@2494 2419
johnc@3175 2420 void G1CMRefProcTaskExecutor::execute(ProcessTask& proc_task) {
johnc@2494 2421 assert(_workers != NULL, "Need parallel worker threads.");
johnc@4555 2422 assert(_g1h->ref_processor_cm()->processing_is_mt(), "processing is not MT");
johnc@2494 2423
johnc@3292 2424 G1CMRefProcTaskProxy proc_task_proxy(proc_task, _g1h, _cm);
johnc@2494 2425
johnc@4788 2426 // We need to reset the concurrency level before each
johnc@4788 2427 // proxy task execution, so that the termination protocol
johnc@4788 2428 // and overflow handling in CMTask::do_marking_step() knows
johnc@4788 2429 // how many workers to wait for.
johnc@4788 2430 _cm->set_concurrency(_active_workers);
johnc@2494 2431 _g1h->set_par_threads(_active_workers);
johnc@2494 2432 _workers->run_task(&proc_task_proxy);
johnc@2494 2433 _g1h->set_par_threads(0);
johnc@2494 2434 }
johnc@2494 2435
johnc@3175 2436 class G1CMRefEnqueueTaskProxy: public AbstractGangTask {
johnc@2494 2437 typedef AbstractRefProcTaskExecutor::EnqueueTask EnqueueTask;
johnc@2494 2438 EnqueueTask& _enq_task;
johnc@2494 2439
johnc@2494 2440 public:
johnc@3175 2441 G1CMRefEnqueueTaskProxy(EnqueueTask& enq_task) :
johnc@2494 2442 AbstractGangTask("Enqueue reference objects in parallel"),
johnc@3292 2443 _enq_task(enq_task) { }
johnc@2494 2444
jmasa@3357 2445 virtual void work(uint worker_id) {
jmasa@3357 2446 _enq_task.work(worker_id);
johnc@2494 2447 }
johnc@2494 2448 };
johnc@2494 2449
johnc@3175 2450 void G1CMRefProcTaskExecutor::execute(EnqueueTask& enq_task) {
johnc@2494 2451 assert(_workers != NULL, "Need parallel worker threads.");
johnc@4555 2452 assert(_g1h->ref_processor_cm()->processing_is_mt(), "processing is not MT");
johnc@2494 2453
johnc@3175 2454 G1CMRefEnqueueTaskProxy enq_task_proxy(enq_task);
johnc@2494 2455
johnc@4788 2456 // Not strictly necessary but...
johnc@4788 2457 //
johnc@4788 2458 // We need to reset the concurrency level before each
johnc@4788 2459 // proxy task execution, so that the termination protocol
johnc@4788 2460 // and overflow handling in CMTask::do_marking_step() knows
johnc@4788 2461 // how many workers to wait for.
johnc@4788 2462 _cm->set_concurrency(_active_workers);
johnc@2494 2463 _g1h->set_par_threads(_active_workers);
johnc@2494 2464 _workers->run_task(&enq_task_proxy);
johnc@2494 2465 _g1h->set_par_threads(0);
johnc@2494 2466 }
johnc@2494 2467
stefank@6992 2468 void ConcurrentMark::weakRefsWorkParallelPart(BoolObjectClosure* is_alive, bool purged_classes) {
stefank@6992 2469 G1CollectedHeap::heap()->parallel_cleaning(is_alive, true, true, purged_classes);
stefank@6992 2470 }
stefank@6992 2471
stefank@6992 2472 // Helper class to get rid of some boilerplate code.
stefank@6992 2473 class G1RemarkGCTraceTime : public GCTraceTime {
stefank@6992 2474 static bool doit_and_prepend(bool doit) {
stefank@6992 2475 if (doit) {
stefank@6992 2476 gclog_or_tty->put(' ');
stefank@6992 2477 }
stefank@6992 2478 return doit;
stefank@6992 2479 }
stefank@6992 2480
stefank@6992 2481 public:
stefank@6992 2482 G1RemarkGCTraceTime(const char* title, bool doit)
stefank@6992 2483 : GCTraceTime(title, doit_and_prepend(doit), false, G1CollectedHeap::heap()->gc_timer_cm(),
stefank@6992 2484 G1CollectedHeap::heap()->concurrent_mark()->concurrent_gc_id()) {
stefank@6992 2485 }
stefank@6992 2486 };
stefank@6992 2487
ysr@777 2488 void ConcurrentMark::weakRefsWork(bool clear_all_soft_refs) {
johnc@4788 2489 if (has_overflown()) {
johnc@4788 2490 // Skip processing the discovered references if we have
johnc@4788 2491 // overflown the global marking stack. Reference objects
johnc@4788 2492 // only get discovered once so it is OK to not
johnc@4788 2493 // de-populate the discovered reference lists. We could have,
johnc@4788 2494 // but the only benefit would be that, when marking restarts,
johnc@4788 2495 // less reference objects are discovered.
johnc@4788 2496 return;
johnc@4788 2497 }
johnc@4788 2498
ysr@777 2499 ResourceMark rm;
ysr@777 2500 HandleMark hm;
johnc@3171 2501
johnc@3171 2502 G1CollectedHeap* g1h = G1CollectedHeap::heap();
johnc@3171 2503
johnc@3171 2504 // Is alive closure.
johnc@3171 2505 G1CMIsAliveClosure g1_is_alive(g1h);
johnc@3171 2506
johnc@3171 2507 // Inner scope to exclude the cleaning of the string and symbol
johnc@3171 2508 // tables from the displayed time.
johnc@3171 2509 {
brutisso@3710 2510 if (G1Log::finer()) {
johnc@3171 2511 gclog_or_tty->put(' ');
johnc@3171 2512 }
brutisso@6904 2513 GCTraceTime t("GC ref-proc", G1Log::finer(), false, g1h->gc_timer_cm(), concurrent_gc_id());
johnc@3171 2514
johnc@3175 2515 ReferenceProcessor* rp = g1h->ref_processor_cm();
johnc@3171 2516
johnc@3171 2517 // See the comment in G1CollectedHeap::ref_processing_init()
johnc@3171 2518 // about how reference processing currently works in G1.
johnc@3171 2519
johnc@4555 2520 // Set the soft reference policy
johnc@3171 2521 rp->setup_policy(clear_all_soft_refs);
johnc@3171 2522 assert(_markStack.isEmpty(), "mark stack should be empty");
johnc@3171 2523
johnc@4787 2524 // Instances of the 'Keep Alive' and 'Complete GC' closures used
johnc@4787 2525 // in serial reference processing. Note these closures are also
johnc@4787 2526 // used for serially processing (by the the current thread) the
johnc@4787 2527 // JNI references during parallel reference processing.
johnc@4787 2528 //
johnc@4787 2529 // These closures do not need to synchronize with the worker
johnc@4787 2530 // threads involved in parallel reference processing as these
johnc@4787 2531 // instances are executed serially by the current thread (e.g.
johnc@4787 2532 // reference processing is not multi-threaded and is thus
johnc@4787 2533 // performed by the current thread instead of a gang worker).
johnc@4787 2534 //
johnc@4787 2535 // The gang tasks involved in parallel reference procssing create
johnc@4787 2536 // their own instances of these closures, which do their own
johnc@4787 2537 // synchronization among themselves.
johnc@4787 2538 G1CMKeepAliveAndDrainClosure g1_keep_alive(this, task(0), true /* is_serial */);
johnc@4787 2539 G1CMDrainMarkingStackClosure g1_drain_mark_stack(this, task(0), true /* is_serial */);
johnc@4787 2540
johnc@4787 2541 // We need at least one active thread. If reference processing
johnc@4787 2542 // is not multi-threaded we use the current (VMThread) thread,
johnc@4787 2543 // otherwise we use the work gang from the G1CollectedHeap and
johnc@4787 2544 // we utilize all the worker threads we can.
johnc@4787 2545 bool processing_is_mt = rp->processing_is_mt() && g1h->workers() != NULL;
johnc@4787 2546 uint active_workers = (processing_is_mt ? g1h->workers()->active_workers() : 1U);
johnc@4173 2547 active_workers = MAX2(MIN2(active_workers, _max_worker_id), 1U);
johnc@3171 2548
johnc@4787 2549 // Parallel processing task executor.
johnc@3292 2550 G1CMRefProcTaskExecutor par_task_executor(g1h, this,
johnc@3175 2551 g1h->workers(), active_workers);
johnc@4787 2552 AbstractRefProcTaskExecutor* executor = (processing_is_mt ? &par_task_executor : NULL);
johnc@4555 2553
johnc@4788 2554 // Set the concurrency level. The phase was already set prior to
johnc@4788 2555 // executing the remark task.
johnc@4788 2556 set_concurrency(active_workers);
johnc@4788 2557
johnc@4555 2558 // Set the degree of MT processing here. If the discovery was done MT,
johnc@4555 2559 // the number of threads involved during discovery could differ from
johnc@4555 2560 // the number of active workers. This is OK as long as the discovered
johnc@4555 2561 // Reference lists are balanced (see balance_all_queues() and balance_queues()).
johnc@4555 2562 rp->set_active_mt_degree(active_workers);
johnc@4555 2563
johnc@4555 2564 // Process the weak references.
sla@5237 2565 const ReferenceProcessorStats& stats =
sla@5237 2566 rp->process_discovered_references(&g1_is_alive,
sla@5237 2567 &g1_keep_alive,
sla@5237 2568 &g1_drain_mark_stack,
sla@5237 2569 executor,
brutisso@6904 2570 g1h->gc_timer_cm(),
brutisso@6904 2571 concurrent_gc_id());
sla@5237 2572 g1h->gc_tracer_cm()->report_gc_reference_stats(stats);
johnc@4555 2573
johnc@4555 2574 // The do_oop work routines of the keep_alive and drain_marking_stack
johnc@4555 2575 // oop closures will set the has_overflown flag if we overflow the
johnc@4555 2576 // global marking stack.
johnc@3171 2577
johnc@3171 2578 assert(_markStack.overflow() || _markStack.isEmpty(),
johnc@3171 2579 "mark stack should be empty (unless it overflowed)");
johnc@4787 2580
johnc@3171 2581 if (_markStack.overflow()) {
johnc@4555 2582 // This should have been done already when we tried to push an
johnc@3171 2583 // entry on to the global mark stack. But let's do it again.
johnc@3171 2584 set_has_overflown();
johnc@3171 2585 }
johnc@3171 2586
johnc@4555 2587 assert(rp->num_q() == active_workers, "why not");
johnc@4555 2588
johnc@4555 2589 rp->enqueue_discovered_references(executor);
johnc@3171 2590
johnc@3171 2591 rp->verify_no_references_recorded();
johnc@3175 2592 assert(!rp->discovery_enabled(), "Post condition");
johnc@2494 2593 }
johnc@2494 2594
pliden@6399 2595 if (has_overflown()) {
pliden@6399 2596 // We can not trust g1_is_alive if the marking stack overflowed
pliden@6399 2597 return;
pliden@6399 2598 }
pliden@6399 2599
stefank@6992 2600 assert(_markStack.isEmpty(), "Marking should have completed");
stefank@6992 2601
stefank@6992 2602 // Unload Klasses, String, Symbols, Code Cache, etc.
stefank@6992 2603 {
stefank@6996 2604 G1RemarkGCTraceTime trace("Unloading", G1Log::finer());
stefank@6996 2605
stefank@6996 2606 if (ClassUnloadingWithConcurrentMark) {
stefank@7333 2607 // Cleaning of klasses depends on correct information from MetadataMarkOnStack. The CodeCache::mark_on_stack
stefank@7333 2608 // part is too slow to be done serially, so it is handled during the weakRefsWorkParallelPart phase.
stefank@7333 2609 // Defer the cleaning until we have complete on_stack data.
stefank@7333 2610 MetadataOnStackMark md_on_stack(false /* Don't visit the code cache at this point */);
stefank@7333 2611
stefank@6996 2612 bool purged_classes;
stefank@6996 2613
stefank@6996 2614 {
stefank@6996 2615 G1RemarkGCTraceTime trace("System Dictionary Unloading", G1Log::finest());
stefank@7333 2616 purged_classes = SystemDictionary::do_unloading(&g1_is_alive, false /* Defer klass cleaning */);
stefank@6996 2617 }
stefank@6996 2618
stefank@6996 2619 {
stefank@6996 2620 G1RemarkGCTraceTime trace("Parallel Unloading", G1Log::finest());
stefank@6996 2621 weakRefsWorkParallelPart(&g1_is_alive, purged_classes);
stefank@6996 2622 }
stefank@7333 2623
stefank@7333 2624 {
stefank@7333 2625 G1RemarkGCTraceTime trace("Deallocate Metadata", G1Log::finest());
stefank@7333 2626 ClassLoaderDataGraph::free_deallocate_lists();
stefank@7333 2627 }
stefank@6996 2628 }
stefank@6996 2629
stefank@6996 2630 if (G1StringDedup::is_enabled()) {
stefank@6996 2631 G1RemarkGCTraceTime trace("String Deduplication Unlink", G1Log::finest());
stefank@6996 2632 G1StringDedup::unlink(&g1_is_alive);
stefank@6996 2633 }
stefank@6992 2634 }
ysr@777 2635 }
ysr@777 2636
ysr@777 2637 void ConcurrentMark::swapMarkBitMaps() {
ysr@777 2638 CMBitMapRO* temp = _prevMarkBitMap;
ysr@777 2639 _prevMarkBitMap = (CMBitMapRO*)_nextMarkBitMap;
ysr@777 2640 _nextMarkBitMap = (CMBitMap*) temp;
ysr@777 2641 }
ysr@777 2642
stefank@6992 2643 class CMObjectClosure;
stefank@6992 2644
stefank@6992 2645 // Closure for iterating over objects, currently only used for
stefank@6992 2646 // processing SATB buffers.
stefank@6992 2647 class CMObjectClosure : public ObjectClosure {
stefank@6992 2648 private:
stefank@6992 2649 CMTask* _task;
stefank@6992 2650
stefank@6992 2651 public:
stefank@6992 2652 void do_object(oop obj) {
stefank@6992 2653 _task->deal_with_reference(obj);
stefank@6992 2654 }
stefank@6992 2655
stefank@6992 2656 CMObjectClosure(CMTask* task) : _task(task) { }
stefank@6992 2657 };
stefank@6992 2658
stefank@6992 2659 class G1RemarkThreadsClosure : public ThreadClosure {
stefank@6992 2660 CMObjectClosure _cm_obj;
stefank@6992 2661 G1CMOopClosure _cm_cl;
stefank@6992 2662 MarkingCodeBlobClosure _code_cl;
stefank@6992 2663 int _thread_parity;
stefank@6992 2664 bool _is_par;
stefank@6992 2665
stefank@6992 2666 public:
stefank@6992 2667 G1RemarkThreadsClosure(G1CollectedHeap* g1h, CMTask* task, bool is_par) :
stefank@6992 2668 _cm_obj(task), _cm_cl(g1h, g1h->concurrent_mark(), task), _code_cl(&_cm_cl, !CodeBlobToOopClosure::FixRelocations),
stefank@6992 2669 _thread_parity(SharedHeap::heap()->strong_roots_parity()), _is_par(is_par) {}
stefank@6992 2670
stefank@6992 2671 void do_thread(Thread* thread) {
stefank@6992 2672 if (thread->is_Java_thread()) {
stefank@6992 2673 if (thread->claim_oops_do(_is_par, _thread_parity)) {
stefank@6992 2674 JavaThread* jt = (JavaThread*)thread;
stefank@6992 2675
stefank@6992 2676 // In theory it should not be neccessary to explicitly walk the nmethods to find roots for concurrent marking
stefank@6992 2677 // however the liveness of oops reachable from nmethods have very complex lifecycles:
stefank@6992 2678 // * Alive if on the stack of an executing method
stefank@6992 2679 // * Weakly reachable otherwise
stefank@6992 2680 // Some objects reachable from nmethods, such as the class loader (or klass_holder) of the receiver should be
stefank@6992 2681 // live by the SATB invariant but other oops recorded in nmethods may behave differently.
stefank@6992 2682 jt->nmethods_do(&_code_cl);
stefank@6992 2683
stefank@6992 2684 jt->satb_mark_queue().apply_closure_and_empty(&_cm_obj);
stefank@6992 2685 }
stefank@6992 2686 } else if (thread->is_VM_thread()) {
stefank@6992 2687 if (thread->claim_oops_do(_is_par, _thread_parity)) {
stefank@6992 2688 JavaThread::satb_mark_queue_set().shared_satb_queue()->apply_closure_and_empty(&_cm_obj);
stefank@6992 2689 }
stefank@6992 2690 }
stefank@6992 2691 }
stefank@6992 2692 };
stefank@6992 2693
ysr@777 2694 class CMRemarkTask: public AbstractGangTask {
ysr@777 2695 private:
johnc@4787 2696 ConcurrentMark* _cm;
johnc@4787 2697 bool _is_serial;
ysr@777 2698 public:
jmasa@3357 2699 void work(uint worker_id) {
ysr@777 2700 // Since all available tasks are actually started, we should
ysr@777 2701 // only proceed if we're supposed to be actived.
jmasa@3357 2702 if (worker_id < _cm->active_tasks()) {
jmasa@3357 2703 CMTask* task = _cm->task(worker_id);
ysr@777 2704 task->record_start_time();
stefank@6992 2705 {
stefank@6992 2706 ResourceMark rm;
stefank@6992 2707 HandleMark hm;
stefank@6992 2708
stefank@6992 2709 G1RemarkThreadsClosure threads_f(G1CollectedHeap::heap(), task, !_is_serial);
stefank@6992 2710 Threads::threads_do(&threads_f);
stefank@6992 2711 }
stefank@6992 2712
ysr@777 2713 do {
johnc@2494 2714 task->do_marking_step(1000000000.0 /* something very large */,
johnc@4787 2715 true /* do_termination */,
johnc@4787 2716 _is_serial);
ysr@777 2717 } while (task->has_aborted() && !_cm->has_overflown());
ysr@777 2718 // If we overflow, then we do not want to restart. We instead
ysr@777 2719 // want to abort remark and do concurrent marking again.
ysr@777 2720 task->record_end_time();
ysr@777 2721 }
ysr@777 2722 }
ysr@777 2723
johnc@4787 2724 CMRemarkTask(ConcurrentMark* cm, int active_workers, bool is_serial) :
johnc@4787 2725 AbstractGangTask("Par Remark"), _cm(cm), _is_serial(is_serial) {
johnc@3338 2726 _cm->terminator()->reset_for_reuse(active_workers);
jmasa@3294 2727 }
ysr@777 2728 };
ysr@777 2729
ysr@777 2730 void ConcurrentMark::checkpointRootsFinalWork() {
ysr@777 2731 ResourceMark rm;
ysr@777 2732 HandleMark hm;
ysr@777 2733 G1CollectedHeap* g1h = G1CollectedHeap::heap();
ysr@777 2734
stefank@6992 2735 G1RemarkGCTraceTime trace("Finalize Marking", G1Log::finer());
stefank@6992 2736
ysr@777 2737 g1h->ensure_parsability(false);
ysr@777 2738
jmasa@2188 2739 if (G1CollectedHeap::use_parallel_gc_threads()) {
jrose@1424 2740 G1CollectedHeap::StrongRootsScope srs(g1h);
jmasa@3294 2741 // this is remark, so we'll use up all active threads
jmasa@3357 2742 uint active_workers = g1h->workers()->active_workers();
jmasa@3294 2743 if (active_workers == 0) {
jmasa@3294 2744 assert(active_workers > 0, "Should have been set earlier");
jmasa@3357 2745 active_workers = (uint) ParallelGCThreads;
jmasa@3294 2746 g1h->workers()->set_active_workers(active_workers);
jmasa@3294 2747 }
johnc@4788 2748 set_concurrency_and_phase(active_workers, false /* concurrent */);
jmasa@3294 2749 // Leave _parallel_marking_threads at it's
jmasa@3294 2750 // value originally calculated in the ConcurrentMark
jmasa@3294 2751 // constructor and pass values of the active workers
jmasa@3294 2752 // through the gang in the task.
ysr@777 2753
johnc@4787 2754 CMRemarkTask remarkTask(this, active_workers, false /* is_serial */);
johnc@4787 2755 // We will start all available threads, even if we decide that the
johnc@4787 2756 // active_workers will be fewer. The extra ones will just bail out
johnc@4787 2757 // immediately.
jmasa@3294 2758 g1h->set_par_threads(active_workers);
ysr@777 2759 g1h->workers()->run_task(&remarkTask);
ysr@777 2760 g1h->set_par_threads(0);
ysr@777 2761 } else {
jrose@1424 2762 G1CollectedHeap::StrongRootsScope srs(g1h);
jmasa@3357 2763 uint active_workers = 1;
johnc@4788 2764 set_concurrency_and_phase(active_workers, false /* concurrent */);
ysr@777 2765
johnc@4787 2766 // Note - if there's no work gang then the VMThread will be
johnc@4787 2767 // the thread to execute the remark - serially. We have
johnc@4787 2768 // to pass true for the is_serial parameter so that
johnc@4787 2769 // CMTask::do_marking_step() doesn't enter the sync
johnc@4787 2770 // barriers in the event of an overflow. Doing so will
johnc@4787 2771 // cause an assert that the current thread is not a
johnc@4787 2772 // concurrent GC thread.
johnc@4787 2773 CMRemarkTask remarkTask(this, active_workers, true /* is_serial*/);
ysr@777 2774 remarkTask.work(0);
ysr@777 2775 }
tonyp@1458 2776 SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();
johnc@4789 2777 guarantee(has_overflown() ||
johnc@4789 2778 satb_mq_set.completed_buffers_num() == 0,
johnc@4789 2779 err_msg("Invariant: has_overflown = %s, num buffers = %d",
johnc@4789 2780 BOOL_TO_STR(has_overflown()),
johnc@4789 2781 satb_mq_set.completed_buffers_num()));
ysr@777 2782
ysr@777 2783 print_stats();
ysr@777 2784 }
ysr@777 2785
tonyp@1479 2786 #ifndef PRODUCT
tonyp@1479 2787
tonyp@1823 2788 class PrintReachableOopClosure: public OopClosure {
ysr@777 2789 private:
ysr@777 2790 G1CollectedHeap* _g1h;
ysr@777 2791 outputStream* _out;
johnc@2969 2792 VerifyOption _vo;
tonyp@1823 2793 bool _all;
ysr@777 2794
ysr@777 2795 public:
johnc@2969 2796 PrintReachableOopClosure(outputStream* out,
johnc@2969 2797 VerifyOption vo,
tonyp@1823 2798 bool all) :
tonyp@1479 2799 _g1h(G1CollectedHeap::heap()),
johnc@2969 2800 _out(out), _vo(vo), _all(all) { }
ysr@777 2801
ysr@1280 2802 void do_oop(narrowOop* p) { do_oop_work(p); }
ysr@1280 2803 void do_oop( oop* p) { do_oop_work(p); }
ysr@1280 2804
ysr@1280 2805 template <class T> void do_oop_work(T* p) {
ysr@1280 2806 oop obj = oopDesc::load_decode_heap_oop(p);
ysr@777 2807 const char* str = NULL;
ysr@777 2808 const char* str2 = "";
ysr@777 2809
tonyp@1823 2810 if (obj == NULL) {
tonyp@1823 2811 str = "";
tonyp@1823 2812 } else if (!_g1h->is_in_g1_reserved(obj)) {
tonyp@1823 2813 str = " O";
tonyp@1823 2814 } else {
ysr@777 2815 HeapRegion* hr = _g1h->heap_region_containing(obj);
tonyp@3957 2816 bool over_tams = _g1h->allocated_since_marking(obj, hr, _vo);
tonyp@3957 2817 bool marked = _g1h->is_marked(obj, _vo);
tonyp@1479 2818
tonyp@1479 2819 if (over_tams) {
tonyp@1823 2820 str = " >";
tonyp@1823 2821 if (marked) {
ysr@777 2822 str2 = " AND MARKED";
tonyp@1479 2823 }
tonyp@1823 2824 } else if (marked) {
tonyp@1823 2825 str = " M";
tonyp@1479 2826 } else {
tonyp@1823 2827 str = " NOT";
tonyp@1479 2828 }
ysr@777 2829 }
ysr@777 2830
tonyp@1823 2831 _out->print_cr(" "PTR_FORMAT": "PTR_FORMAT"%s%s",
drchase@6680 2832 p2i(p), p2i((void*) obj), str, str2);
ysr@777 2833 }
ysr@777 2834 };
ysr@777 2835
tonyp@1823 2836 class PrintReachableObjectClosure : public ObjectClosure {
ysr@777 2837 private:
johnc@2969 2838 G1CollectedHeap* _g1h;
johnc@2969 2839 outputStream* _out;
johnc@2969 2840 VerifyOption _vo;
johnc@2969 2841 bool _all;
johnc@2969 2842 HeapRegion* _hr;
ysr@777 2843
ysr@777 2844 public:
johnc@2969 2845 PrintReachableObjectClosure(outputStream* out,
johnc@2969 2846 VerifyOption vo,
tonyp@1823 2847 bool all,
tonyp@1823 2848 HeapRegion* hr) :
johnc@2969 2849 _g1h(G1CollectedHeap::heap()),
johnc@2969 2850 _out(out), _vo(vo), _all(all), _hr(hr) { }
tonyp@1823 2851
tonyp@1823 2852 void do_object(oop o) {
tonyp@3957 2853 bool over_tams = _g1h->allocated_since_marking(o, _hr, _vo);
tonyp@3957 2854 bool marked = _g1h->is_marked(o, _vo);
tonyp@1823 2855 bool print_it = _all || over_tams || marked;
tonyp@1823 2856
tonyp@1823 2857 if (print_it) {
tonyp@1823 2858 _out->print_cr(" "PTR_FORMAT"%s",
drchase@6680 2859 p2i((void *)o), (over_tams) ? " >" : (marked) ? " M" : "");
johnc@2969 2860 PrintReachableOopClosure oopCl(_out, _vo, _all);
coleenp@4037 2861 o->oop_iterate_no_header(&oopCl);
tonyp@1823 2862 }
ysr@777 2863 }
ysr@777 2864 };
ysr@777 2865
tonyp@1823 2866 class PrintReachableRegionClosure : public HeapRegionClosure {
ysr@777 2867 private:
tonyp@3957 2868 G1CollectedHeap* _g1h;
tonyp@3957 2869 outputStream* _out;
tonyp@3957 2870 VerifyOption _vo;
tonyp@3957 2871 bool _all;
ysr@777 2872
ysr@777 2873 public:
ysr@777 2874 bool doHeapRegion(HeapRegion* hr) {
ysr@777 2875 HeapWord* b = hr->bottom();
ysr@777 2876 HeapWord* e = hr->end();
ysr@777 2877 HeapWord* t = hr->top();
tonyp@3957 2878 HeapWord* p = _g1h->top_at_mark_start(hr, _vo);
ysr@777 2879 _out->print_cr("** ["PTR_FORMAT", "PTR_FORMAT"] top: "PTR_FORMAT" "
drchase@6680 2880 "TAMS: " PTR_FORMAT, p2i(b), p2i(e), p2i(t), p2i(p));
tonyp@1823 2881 _out->cr();
tonyp@1823 2882
tonyp@1823 2883 HeapWord* from = b;
tonyp@1823 2884 HeapWord* to = t;
tonyp@1823 2885
tonyp@1823 2886 if (to > from) {
drchase@6680 2887 _out->print_cr("Objects in [" PTR_FORMAT ", " PTR_FORMAT "]", p2i(from), p2i(to));
tonyp@1823 2888 _out->cr();
johnc@2969 2889 PrintReachableObjectClosure ocl(_out, _vo, _all, hr);
tonyp@1823 2890 hr->object_iterate_mem_careful(MemRegion(from, to), &ocl);
tonyp@1823 2891 _out->cr();
tonyp@1823 2892 }
ysr@777 2893
ysr@777 2894 return false;
ysr@777 2895 }
ysr@777 2896
johnc@2969 2897 PrintReachableRegionClosure(outputStream* out,
johnc@2969 2898 VerifyOption vo,
tonyp@1823 2899 bool all) :
tonyp@3957 2900 _g1h(G1CollectedHeap::heap()), _out(out), _vo(vo), _all(all) { }
ysr@777 2901 };
ysr@777 2902
tonyp@1823 2903 void ConcurrentMark::print_reachable(const char* str,
johnc@2969 2904 VerifyOption vo,
tonyp@1823 2905 bool all) {
tonyp@1823 2906 gclog_or_tty->cr();
tonyp@1823 2907 gclog_or_tty->print_cr("== Doing heap dump... ");
tonyp@1479 2908
tonyp@1479 2909 if (G1PrintReachableBaseFile == NULL) {
tonyp@1479 2910 gclog_or_tty->print_cr(" #### error: no base file defined");
tonyp@1479 2911 return;
tonyp@1479 2912 }
tonyp@1479 2913
tonyp@1479 2914 if (strlen(G1PrintReachableBaseFile) + 1 + strlen(str) >
tonyp@1479 2915 (JVM_MAXPATHLEN - 1)) {
tonyp@1479 2916 gclog_or_tty->print_cr(" #### error: file name too long");
tonyp@1479 2917 return;
tonyp@1479 2918 }
tonyp@1479 2919
tonyp@1479 2920 char file_name[JVM_MAXPATHLEN];
tonyp@1479 2921 sprintf(file_name, "%s.%s", G1PrintReachableBaseFile, str);
tonyp@1479 2922 gclog_or_tty->print_cr(" dumping to file %s", file_name);
tonyp@1479 2923
tonyp@1479 2924 fileStream fout(file_name);
tonyp@1479 2925 if (!fout.is_open()) {
tonyp@1479 2926 gclog_or_tty->print_cr(" #### error: could not open file");
tonyp@1479 2927 return;
tonyp@1479 2928 }
tonyp@1479 2929
tonyp@1479 2930 outputStream* out = &fout;
tonyp@3957 2931 out->print_cr("-- USING %s", _g1h->top_at_mark_start_str(vo));
tonyp@1479 2932 out->cr();
tonyp@1479 2933
tonyp@1823 2934 out->print_cr("--- ITERATING OVER REGIONS");
tonyp@1479 2935 out->cr();
johnc@2969 2936 PrintReachableRegionClosure rcl(out, vo, all);
ysr@777 2937 _g1h->heap_region_iterate(&rcl);
tonyp@1479 2938 out->cr();
tonyp@1479 2939
tonyp@1479 2940 gclog_or_tty->print_cr(" done");
tonyp@1823 2941 gclog_or_tty->flush();
ysr@777 2942 }
ysr@777 2943
tonyp@1479 2944 #endif // PRODUCT
tonyp@1479 2945
tonyp@3416 2946 void ConcurrentMark::clearRangePrevBitmap(MemRegion mr) {
ysr@777 2947 // Note we are overriding the read-only view of the prev map here, via
ysr@777 2948 // the cast.
ysr@777 2949 ((CMBitMap*)_prevMarkBitMap)->clearRange(mr);
tonyp@3416 2950 }
tonyp@3416 2951
tonyp@3416 2952 void ConcurrentMark::clearRangeNextBitmap(MemRegion mr) {
ysr@777 2953 _nextMarkBitMap->clearRange(mr);
ysr@777 2954 }
ysr@777 2955
ysr@777 2956 HeapRegion*
johnc@4173 2957 ConcurrentMark::claim_region(uint worker_id) {
ysr@777 2958 // "checkpoint" the finger
ysr@777 2959 HeapWord* finger = _finger;
ysr@777 2960
ysr@777 2961 // _heap_end will not change underneath our feet; it only changes at
ysr@777 2962 // yield points.
ysr@777 2963 while (finger < _heap_end) {
tonyp@1458 2964 assert(_g1h->is_in_g1_reserved(finger), "invariant");
ysr@777 2965
tonyp@2968 2966 // Note on how this code handles humongous regions. In the
tonyp@2968 2967 // normal case the finger will reach the start of a "starts
tonyp@2968 2968 // humongous" (SH) region. Its end will either be the end of the
tonyp@2968 2969 // last "continues humongous" (CH) region in the sequence, or the
tonyp@2968 2970 // standard end of the SH region (if the SH is the only region in
tonyp@2968 2971 // the sequence). That way claim_region() will skip over the CH
tonyp@2968 2972 // regions. However, there is a subtle race between a CM thread
tonyp@2968 2973 // executing this method and a mutator thread doing a humongous
tonyp@2968 2974 // object allocation. The two are not mutually exclusive as the CM
tonyp@2968 2975 // thread does not need to hold the Heap_lock when it gets
tonyp@2968 2976 // here. So there is a chance that claim_region() will come across
tonyp@2968 2977 // a free region that's in the progress of becoming a SH or a CH
tonyp@2968 2978 // region. In the former case, it will either
tonyp@2968 2979 // a) Miss the update to the region's end, in which case it will
tonyp@2968 2980 // visit every subsequent CH region, will find their bitmaps
tonyp@2968 2981 // empty, and do nothing, or
tonyp@2968 2982 // b) Will observe the update of the region's end (in which case
tonyp@2968 2983 // it will skip the subsequent CH regions).
tonyp@2968 2984 // If it comes across a region that suddenly becomes CH, the
tonyp@2968 2985 // scenario will be similar to b). So, the race between
tonyp@2968 2986 // claim_region() and a humongous object allocation might force us
tonyp@2968 2987 // to do a bit of unnecessary work (due to some unnecessary bitmap
tonyp@2968 2988 // iterations) but it should not introduce and correctness issues.
tschatzl@7051 2989 HeapRegion* curr_region = _g1h->heap_region_containing_raw(finger);
tschatzl@7051 2990
tschatzl@7051 2991 // Above heap_region_containing_raw may return NULL as we always scan claim
tschatzl@7051 2992 // until the end of the heap. In this case, just jump to the next region.
tschatzl@7051 2993 HeapWord* end = curr_region != NULL ? curr_region->end() : finger + HeapRegion::GrainWords;
tonyp@2968 2994
tonyp@2968 2995 // Is the gap between reading the finger and doing the CAS too long?
tonyp@2968 2996 HeapWord* res = (HeapWord*) Atomic::cmpxchg_ptr(end, &_finger, finger);
tschatzl@7051 2997 if (res == finger && curr_region != NULL) {
ysr@777 2998 // we succeeded
tschatzl@7051 2999 HeapWord* bottom = curr_region->bottom();
tschatzl@7051 3000 HeapWord* limit = curr_region->next_top_at_mark_start();
tschatzl@7051 3001
tschatzl@7051 3002 if (verbose_low()) {
tschatzl@7051 3003 gclog_or_tty->print_cr("[%u] curr_region = "PTR_FORMAT" "
tschatzl@7051 3004 "["PTR_FORMAT", "PTR_FORMAT"), "
tschatzl@7051 3005 "limit = "PTR_FORMAT,
tschatzl@7051 3006 worker_id, p2i(curr_region), p2i(bottom), p2i(end), p2i(limit));
tschatzl@7051 3007 }
ysr@777 3008
ysr@777 3009 // notice that _finger == end cannot be guaranteed here since,
ysr@777 3010 // someone else might have moved the finger even further
tonyp@1458 3011 assert(_finger >= end, "the finger should have moved forward");
ysr@777 3012
tonyp@2973 3013 if (verbose_low()) {
johnc@4173 3014 gclog_or_tty->print_cr("[%u] we were successful with region = "
drchase@6680 3015 PTR_FORMAT, worker_id, p2i(curr_region));
tonyp@2973 3016 }
ysr@777 3017
ysr@777 3018 if (limit > bottom) {
tonyp@2973 3019 if (verbose_low()) {
johnc@4173 3020 gclog_or_tty->print_cr("[%u] region "PTR_FORMAT" is not empty, "
drchase@6680 3021 "returning it ", worker_id, p2i(curr_region));
tonyp@2973 3022 }
ysr@777 3023 return curr_region;
ysr@777 3024 } else {
tonyp@1458 3025 assert(limit == bottom,
tonyp@1458 3026 "the region limit should be at bottom");
tonyp@2973 3027 if (verbose_low()) {
johnc@4173 3028 gclog_or_tty->print_cr("[%u] region "PTR_FORMAT" is empty, "
drchase@6680 3029 "returning NULL", worker_id, p2i(curr_region));
tonyp@2973 3030 }
ysr@777 3031 // we return NULL and the caller should try calling
ysr@777 3032 // claim_region() again.
ysr@777 3033 return NULL;
ysr@777 3034 }
ysr@777 3035 } else {
tonyp@1458 3036 assert(_finger > finger, "the finger should have moved forward");
tonyp@2973 3037 if (verbose_low()) {
tschatzl@7051 3038 if (curr_region == NULL) {
tschatzl@7051 3039 gclog_or_tty->print_cr("[%u] found uncommitted region, moving finger, "
tschatzl@7051 3040 "global finger = "PTR_FORMAT", "
tschatzl@7051 3041 "our finger = "PTR_FORMAT,
tschatzl@7051 3042 worker_id, p2i(_finger), p2i(finger));
tschatzl@7051 3043 } else {
tschatzl@7051 3044 gclog_or_tty->print_cr("[%u] somebody else moved the finger, "
tschatzl@7051 3045 "global finger = "PTR_FORMAT", "
tschatzl@7051 3046 "our finger = "PTR_FORMAT,
tschatzl@7051 3047 worker_id, p2i(_finger), p2i(finger));
tschatzl@7051 3048 }
tonyp@2973 3049 }
ysr@777 3050
ysr@777 3051 // read it again
ysr@777 3052 finger = _finger;
ysr@777 3053 }
ysr@777 3054 }
ysr@777 3055
ysr@777 3056 return NULL;
ysr@777 3057 }
ysr@777 3058
tonyp@3416 3059 #ifndef PRODUCT
tonyp@3416 3060 enum VerifyNoCSetOopsPhase {
tonyp@3416 3061 VerifyNoCSetOopsStack,
tonyp@3416 3062 VerifyNoCSetOopsQueues,
tonyp@3416 3063 VerifyNoCSetOopsSATBCompleted,
tonyp@3416 3064 VerifyNoCSetOopsSATBThread
tonyp@3416 3065 };
tonyp@3416 3066
tonyp@3416 3067 class VerifyNoCSetOopsClosure : public OopClosure, public ObjectClosure {
tonyp@3416 3068 private:
tonyp@3416 3069 G1CollectedHeap* _g1h;
tonyp@3416 3070 VerifyNoCSetOopsPhase _phase;
tonyp@3416 3071 int _info;
tonyp@3416 3072
tonyp@3416 3073 const char* phase_str() {
tonyp@3416 3074 switch (_phase) {
tonyp@3416 3075 case VerifyNoCSetOopsStack: return "Stack";
tonyp@3416 3076 case VerifyNoCSetOopsQueues: return "Queue";
tonyp@3416 3077 case VerifyNoCSetOopsSATBCompleted: return "Completed SATB Buffers";
tonyp@3416 3078 case VerifyNoCSetOopsSATBThread: return "Thread SATB Buffers";
tonyp@3416 3079 default: ShouldNotReachHere();
tonyp@3416 3080 }
tonyp@3416 3081 return NULL;
ysr@777 3082 }
johnc@2190 3083
tonyp@3416 3084 void do_object_work(oop obj) {
tonyp@3416 3085 guarantee(!_g1h->obj_in_cs(obj),
tonyp@3416 3086 err_msg("obj: "PTR_FORMAT" in CSet, phase: %s, info: %d",
drchase@6680 3087 p2i((void*) obj), phase_str(), _info));
johnc@2190 3088 }
johnc@2190 3089
tonyp@3416 3090 public:
tonyp@3416 3091 VerifyNoCSetOopsClosure() : _g1h(G1CollectedHeap::heap()) { }
tonyp@3416 3092
tonyp@3416 3093 void set_phase(VerifyNoCSetOopsPhase phase, int info = -1) {
tonyp@3416 3094 _phase = phase;
tonyp@3416 3095 _info = info;
tonyp@3416 3096 }
tonyp@3416 3097
tonyp@3416 3098 virtual void do_oop(oop* p) {
tonyp@3416 3099 oop obj = oopDesc::load_decode_heap_oop(p);
tonyp@3416 3100 do_object_work(obj);
tonyp@3416 3101 }
tonyp@3416 3102
tonyp@3416 3103 virtual void do_oop(narrowOop* p) {
tonyp@3416 3104 // We should not come across narrow oops while scanning marking
tonyp@3416 3105 // stacks and SATB buffers.
tonyp@3416 3106 ShouldNotReachHere();
tonyp@3416 3107 }
tonyp@3416 3108
tonyp@3416 3109 virtual void do_object(oop obj) {
tonyp@3416 3110 do_object_work(obj);
tonyp@3416 3111 }
tonyp@3416 3112 };
tonyp@3416 3113
tonyp@3416 3114 void ConcurrentMark::verify_no_cset_oops(bool verify_stacks,
tonyp@3416 3115 bool verify_enqueued_buffers,
tonyp@3416 3116 bool verify_thread_buffers,
tonyp@3416 3117 bool verify_fingers) {
tonyp@3416 3118 assert(SafepointSynchronize::is_at_safepoint(), "should be at a safepoint");
tonyp@3416 3119 if (!G1CollectedHeap::heap()->mark_in_progress()) {
tonyp@3416 3120 return;
tonyp@3416 3121 }
tonyp@3416 3122
tonyp@3416 3123 VerifyNoCSetOopsClosure cl;
tonyp@3416 3124
tonyp@3416 3125 if (verify_stacks) {
tonyp@3416 3126 // Verify entries on the global mark stack
tonyp@3416 3127 cl.set_phase(VerifyNoCSetOopsStack);
tonyp@3416 3128 _markStack.oops_do(&cl);
tonyp@3416 3129
tonyp@3416 3130 // Verify entries on the task queues
johnc@4173 3131 for (uint i = 0; i < _max_worker_id; i += 1) {
tonyp@3416 3132 cl.set_phase(VerifyNoCSetOopsQueues, i);
johnc@4333 3133 CMTaskQueue* queue = _task_queues->queue(i);
tonyp@3416 3134 queue->oops_do(&cl);
tonyp@3416 3135 }
tonyp@3416 3136 }
tonyp@3416 3137
tonyp@3416 3138 SATBMarkQueueSet& satb_qs = JavaThread::satb_mark_queue_set();
tonyp@3416 3139
tonyp@3416 3140 // Verify entries on the enqueued SATB buffers
tonyp@3416 3141 if (verify_enqueued_buffers) {
tonyp@3416 3142 cl.set_phase(VerifyNoCSetOopsSATBCompleted);
tonyp@3416 3143 satb_qs.iterate_completed_buffers_read_only(&cl);
tonyp@3416 3144 }
tonyp@3416 3145
tonyp@3416 3146 // Verify entries on the per-thread SATB buffers
tonyp@3416 3147 if (verify_thread_buffers) {
tonyp@3416 3148 cl.set_phase(VerifyNoCSetOopsSATBThread);
tonyp@3416 3149 satb_qs.iterate_thread_buffers_read_only(&cl);
tonyp@3416 3150 }
tonyp@3416 3151
tonyp@3416 3152 if (verify_fingers) {
tonyp@3416 3153 // Verify the global finger
tonyp@3416 3154 HeapWord* global_finger = finger();
tonyp@3416 3155 if (global_finger != NULL && global_finger < _heap_end) {
tonyp@3416 3156 // The global finger always points to a heap region boundary. We
tonyp@3416 3157 // use heap_region_containing_raw() to get the containing region
tonyp@3416 3158 // given that the global finger could be pointing to a free region
tonyp@3416 3159 // which subsequently becomes continues humongous. If that
tonyp@3416 3160 // happens, heap_region_containing() will return the bottom of the
tonyp@3416 3161 // corresponding starts humongous region and the check below will
tonyp@3416 3162 // not hold any more.
tschatzl@7051 3163 // Since we always iterate over all regions, we might get a NULL HeapRegion
tschatzl@7051 3164 // here.
tonyp@3416 3165 HeapRegion* global_hr = _g1h->heap_region_containing_raw(global_finger);
tschatzl@7051 3166 guarantee(global_hr == NULL || global_finger == global_hr->bottom(),
tonyp@3416 3167 err_msg("global finger: "PTR_FORMAT" region: "HR_FORMAT,
drchase@6680 3168 p2i(global_finger), HR_FORMAT_PARAMS(global_hr)));
tonyp@3416 3169 }
tonyp@3416 3170
tonyp@3416 3171 // Verify the task fingers
johnc@4173 3172 assert(parallel_marking_threads() <= _max_worker_id, "sanity");
tonyp@3416 3173 for (int i = 0; i < (int) parallel_marking_threads(); i += 1) {
tonyp@3416 3174 CMTask* task = _tasks[i];
tonyp@3416 3175 HeapWord* task_finger = task->finger();
tonyp@3416 3176 if (task_finger != NULL && task_finger < _heap_end) {
tonyp@3416 3177 // See above note on the global finger verification.
tonyp@3416 3178 HeapRegion* task_hr = _g1h->heap_region_containing_raw(task_finger);
tschatzl@7051 3179 guarantee(task_hr == NULL || task_finger == task_hr->bottom() ||
tonyp@3416 3180 !task_hr->in_collection_set(),
tonyp@3416 3181 err_msg("task finger: "PTR_FORMAT" region: "HR_FORMAT,
drchase@6680 3182 p2i(task_finger), HR_FORMAT_PARAMS(task_hr)));
tonyp@3416 3183 }
tonyp@3416 3184 }
tonyp@3416 3185 }
ysr@777 3186 }
tonyp@3416 3187 #endif // PRODUCT
ysr@777 3188
johnc@3463 3189 // Aggregate the counting data that was constructed concurrently
johnc@3463 3190 // with marking.
johnc@3463 3191 class AggregateCountDataHRClosure: public HeapRegionClosure {
johnc@4123 3192 G1CollectedHeap* _g1h;
johnc@3463 3193 ConcurrentMark* _cm;
johnc@4123 3194 CardTableModRefBS* _ct_bs;
johnc@3463 3195 BitMap* _cm_card_bm;
johnc@4173 3196 uint _max_worker_id;
johnc@3463 3197
johnc@3463 3198 public:
johnc@4123 3199 AggregateCountDataHRClosure(G1CollectedHeap* g1h,
johnc@3463 3200 BitMap* cm_card_bm,
johnc@4173 3201 uint max_worker_id) :
johnc@4123 3202 _g1h(g1h), _cm(g1h->concurrent_mark()),
johnc@4123 3203 _ct_bs((CardTableModRefBS*) (g1h->barrier_set())),
johnc@4173 3204 _cm_card_bm(cm_card_bm), _max_worker_id(max_worker_id) { }
johnc@3463 3205
johnc@3463 3206 bool doHeapRegion(HeapRegion* hr) {
johnc@3463 3207 if (hr->continuesHumongous()) {
johnc@3463 3208 // We will ignore these here and process them when their
johnc@3463 3209 // associated "starts humongous" region is processed.
johnc@3463 3210 // Note that we cannot rely on their associated
johnc@3463 3211 // "starts humongous" region to have their bit set to 1
johnc@3463 3212 // since, due to the region chunking in the parallel region
johnc@3463 3213 // iteration, a "continues humongous" region might be visited
johnc@3463 3214 // before its associated "starts humongous".
johnc@3463 3215 return false;
johnc@3463 3216 }
johnc@3463 3217
johnc@3463 3218 HeapWord* start = hr->bottom();
johnc@3463 3219 HeapWord* limit = hr->next_top_at_mark_start();
johnc@3463 3220 HeapWord* end = hr->end();
johnc@3463 3221
johnc@3463 3222 assert(start <= limit && limit <= hr->top() && hr->top() <= hr->end(),
johnc@3463 3223 err_msg("Preconditions not met - "
johnc@3463 3224 "start: "PTR_FORMAT", limit: "PTR_FORMAT", "
johnc@3463 3225 "top: "PTR_FORMAT", end: "PTR_FORMAT,
drchase@6680 3226 p2i(start), p2i(limit), p2i(hr->top()), p2i(hr->end())));
johnc@3463 3227
johnc@3463 3228 assert(hr->next_marked_bytes() == 0, "Precondition");
johnc@3463 3229
johnc@3463 3230 if (start == limit) {
johnc@3463 3231 // NTAMS of this region has not been set so nothing to do.
johnc@3463 3232 return false;
johnc@3463 3233 }
johnc@3463 3234
johnc@4123 3235 // 'start' should be in the heap.
johnc@4123 3236 assert(_g1h->is_in_g1_reserved(start) && _ct_bs->is_card_aligned(start), "sanity");
johnc@4123 3237 // 'end' *may* be just beyone the end of the heap (if hr is the last region)
johnc@4123 3238 assert(!_g1h->is_in_g1_reserved(end) || _ct_bs->is_card_aligned(end), "sanity");
johnc@3463 3239
johnc@3463 3240 BitMap::idx_t start_idx = _cm->card_bitmap_index_for(start);
johnc@3463 3241 BitMap::idx_t limit_idx = _cm->card_bitmap_index_for(limit);
johnc@3463 3242 BitMap::idx_t end_idx = _cm->card_bitmap_index_for(end);
johnc@3463 3243
johnc@4123 3244 // If ntams is not card aligned then we bump card bitmap index
johnc@4123 3245 // for limit so that we get the all the cards spanned by
johnc@4123 3246 // the object ending at ntams.
johnc@4123 3247 // Note: if this is the last region in the heap then ntams
johnc@4123 3248 // could be actually just beyond the end of the the heap;
johnc@4123 3249 // limit_idx will then correspond to a (non-existent) card
johnc@4123 3250 // that is also outside the heap.
johnc@4123 3251 if (_g1h->is_in_g1_reserved(limit) && !_ct_bs->is_card_aligned(limit)) {
johnc@3463 3252 limit_idx += 1;
johnc@3463 3253 }
johnc@3463 3254
johnc@3463 3255 assert(limit_idx <= end_idx, "or else use atomics");
johnc@3463 3256
johnc@3463 3257 // Aggregate the "stripe" in the count data associated with hr.
tschatzl@7091 3258 uint hrm_index = hr->hrm_index();
johnc@3463 3259 size_t marked_bytes = 0;
johnc@3463 3260
johnc@4173 3261 for (uint i = 0; i < _max_worker_id; i += 1) {
johnc@3463 3262 size_t* marked_bytes_array = _cm->count_marked_bytes_array_for(i);
johnc@3463 3263 BitMap* task_card_bm = _cm->count_card_bitmap_for(i);
johnc@3463 3264
johnc@3463 3265 // Fetch the marked_bytes in this region for task i and
johnc@3463 3266 // add it to the running total for this region.
tschatzl@7091 3267 marked_bytes += marked_bytes_array[hrm_index];
johnc@3463 3268
johnc@4173 3269 // Now union the bitmaps[0,max_worker_id)[start_idx..limit_idx)
johnc@3463 3270 // into the global card bitmap.
johnc@3463 3271 BitMap::idx_t scan_idx = task_card_bm->get_next_one_offset(start_idx, limit_idx);
johnc@3463 3272
johnc@3463 3273 while (scan_idx < limit_idx) {
johnc@3463 3274 assert(task_card_bm->at(scan_idx) == true, "should be");
johnc@3463 3275 _cm_card_bm->set_bit(scan_idx);
johnc@3463 3276 assert(_cm_card_bm->at(scan_idx) == true, "should be");
johnc@3463 3277
johnc@3463 3278 // BitMap::get_next_one_offset() can handle the case when
johnc@3463 3279 // its left_offset parameter is greater than its right_offset
johnc@4123 3280 // parameter. It does, however, have an early exit if
johnc@3463 3281 // left_offset == right_offset. So let's limit the value
johnc@3463 3282 // passed in for left offset here.
johnc@3463 3283 BitMap::idx_t next_idx = MIN2(scan_idx + 1, limit_idx);
johnc@3463 3284 scan_idx = task_card_bm->get_next_one_offset(next_idx, limit_idx);
johnc@3463 3285 }
johnc@3463 3286 }
johnc@3463 3287
johnc@3463 3288 // Update the marked bytes for this region.
johnc@3463 3289 hr->add_to_marked_bytes(marked_bytes);
johnc@3463 3290
johnc@3463 3291 // Next heap region
johnc@3463 3292 return false;
johnc@3463 3293 }
johnc@3463 3294 };
johnc@3463 3295
johnc@3463 3296 class G1AggregateCountDataTask: public AbstractGangTask {
johnc@3463 3297 protected:
johnc@3463 3298 G1CollectedHeap* _g1h;
johnc@3463 3299 ConcurrentMark* _cm;
johnc@3463 3300 BitMap* _cm_card_bm;
johnc@4173 3301 uint _max_worker_id;
johnc@3463 3302 int _active_workers;
johnc@3463 3303
johnc@3463 3304 public:
johnc@3463 3305 G1AggregateCountDataTask(G1CollectedHeap* g1h,
johnc@3463 3306 ConcurrentMark* cm,
johnc@3463 3307 BitMap* cm_card_bm,
johnc@4173 3308 uint max_worker_id,
johnc@3463 3309 int n_workers) :
johnc@3463 3310 AbstractGangTask("Count Aggregation"),
johnc@3463 3311 _g1h(g1h), _cm(cm), _cm_card_bm(cm_card_bm),
johnc@4173 3312 _max_worker_id(max_worker_id),
johnc@3463 3313 _active_workers(n_workers) { }
johnc@3463 3314
johnc@3463 3315 void work(uint worker_id) {
johnc@4173 3316 AggregateCountDataHRClosure cl(_g1h, _cm_card_bm, _max_worker_id);
johnc@3463 3317
johnc@3463 3318 if (G1CollectedHeap::use_parallel_gc_threads()) {
johnc@3463 3319 _g1h->heap_region_par_iterate_chunked(&cl, worker_id,
johnc@3463 3320 _active_workers,
johnc@3463 3321 HeapRegion::AggregateCountClaimValue);
johnc@3463 3322 } else {
johnc@3463 3323 _g1h->heap_region_iterate(&cl);
johnc@3463 3324 }
johnc@3463 3325 }
johnc@3463 3326 };
johnc@3463 3327
johnc@3463 3328
johnc@3463 3329 void ConcurrentMark::aggregate_count_data() {
johnc@3463 3330 int n_workers = (G1CollectedHeap::use_parallel_gc_threads() ?
johnc@3463 3331 _g1h->workers()->active_workers() :
johnc@3463 3332 1);
johnc@3463 3333
johnc@3463 3334 G1AggregateCountDataTask g1_par_agg_task(_g1h, this, &_card_bm,
johnc@4173 3335 _max_worker_id, n_workers);
johnc@3463 3336
johnc@3463 3337 if (G1CollectedHeap::use_parallel_gc_threads()) {
johnc@3463 3338 assert(_g1h->check_heap_region_claim_values(HeapRegion::InitialClaimValue),
johnc@3463 3339 "sanity check");
johnc@3463 3340 _g1h->set_par_threads(n_workers);
johnc@3463 3341 _g1h->workers()->run_task(&g1_par_agg_task);
johnc@3463 3342 _g1h->set_par_threads(0);
johnc@3463 3343
johnc@3463 3344 assert(_g1h->check_heap_region_claim_values(HeapRegion::AggregateCountClaimValue),
johnc@3463 3345 "sanity check");
johnc@3463 3346 _g1h->reset_heap_region_claim_values();
johnc@3463 3347 } else {
johnc@3463 3348 g1_par_agg_task.work(0);
johnc@3463 3349 }
johnc@3463 3350 }
johnc@3463 3351
johnc@3463 3352 // Clear the per-worker arrays used to store the per-region counting data
johnc@3463 3353 void ConcurrentMark::clear_all_count_data() {
johnc@3463 3354 // Clear the global card bitmap - it will be filled during
johnc@3463 3355 // liveness count aggregation (during remark) and the
johnc@3463 3356 // final counting task.
johnc@3463 3357 _card_bm.clear();
johnc@3463 3358
johnc@3463 3359 // Clear the global region bitmap - it will be filled as part
johnc@3463 3360 // of the final counting task.
johnc@3463 3361 _region_bm.clear();
johnc@3463 3362
tonyp@3713 3363 uint max_regions = _g1h->max_regions();
johnc@4173 3364 assert(_max_worker_id > 0, "uninitialized");
johnc@4173 3365
johnc@4173 3366 for (uint i = 0; i < _max_worker_id; i += 1) {
johnc@3463 3367 BitMap* task_card_bm = count_card_bitmap_for(i);
johnc@3463 3368 size_t* marked_bytes_array = count_marked_bytes_array_for(i);
johnc@3463 3369
johnc@3463 3370 assert(task_card_bm->size() == _card_bm.size(), "size mismatch");
johnc@3463 3371 assert(marked_bytes_array != NULL, "uninitialized");
johnc@3463 3372
tonyp@3713 3373 memset(marked_bytes_array, 0, (size_t) max_regions * sizeof(size_t));
johnc@3463 3374 task_card_bm->clear();
johnc@3463 3375 }
johnc@3463 3376 }
johnc@3463 3377
ysr@777 3378 void ConcurrentMark::print_stats() {
ysr@777 3379 if (verbose_stats()) {
ysr@777 3380 gclog_or_tty->print_cr("---------------------------------------------------------------------");
ysr@777 3381 for (size_t i = 0; i < _active_tasks; ++i) {
ysr@777 3382 _tasks[i]->print_stats();
ysr@777 3383 gclog_or_tty->print_cr("---------------------------------------------------------------------");
ysr@777 3384 }
ysr@777 3385 }
ysr@777 3386 }
ysr@777 3387
ysr@777 3388 // abandon current marking iteration due to a Full GC
ysr@777 3389 void ConcurrentMark::abort() {
tschatzl@7016 3390 // Clear all marks in the next bitmap for the next marking cycle. This will allow us to skip the next
tschatzl@7016 3391 // concurrent bitmap clearing.
ysr@777 3392 _nextMarkBitMap->clearAll();
brutisso@7005 3393
brutisso@7005 3394 // Note we cannot clear the previous marking bitmap here
brutisso@7005 3395 // since VerifyDuringGC verifies the objects marked during
brutisso@7005 3396 // a full GC against the previous bitmap.
brutisso@7005 3397
johnc@3463 3398 // Clear the liveness counting data
johnc@3463 3399 clear_all_count_data();
ysr@777 3400 // Empty mark stack
johnc@4386 3401 reset_marking_state();
johnc@4173 3402 for (uint i = 0; i < _max_worker_id; ++i) {
ysr@777 3403 _tasks[i]->clear_region_fields();
johnc@2190 3404 }
pliden@6692 3405 _first_overflow_barrier_sync.abort();
pliden@6692 3406 _second_overflow_barrier_sync.abort();
brutisso@6904 3407 const GCId& gc_id = _g1h->gc_tracer_cm()->gc_id();
brutisso@6904 3408 if (!gc_id.is_undefined()) {
brutisso@6904 3409 // We can do multiple full GCs before ConcurrentMarkThread::run() gets a chance
brutisso@6904 3410 // to detect that it was aborted. Only keep track of the first GC id that we aborted.
brutisso@6904 3411 _aborted_gc_id = gc_id;
brutisso@6904 3412 }
ysr@777 3413 _has_aborted = true;
ysr@777 3414
ysr@777 3415 SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();
ysr@777 3416 satb_mq_set.abandon_partial_marking();
tonyp@1752 3417 // This can be called either during or outside marking, we'll read
tonyp@1752 3418 // the expected_active value from the SATB queue set.
tonyp@1752 3419 satb_mq_set.set_active_all_threads(
tonyp@1752 3420 false, /* new active value */
tonyp@1752 3421 satb_mq_set.is_active() /* expected_active */);
sla@5237 3422
sla@5237 3423 _g1h->trace_heap_after_concurrent_cycle();
sla@5237 3424 _g1h->register_concurrent_cycle_end();
ysr@777 3425 }
ysr@777 3426
brutisso@6904 3427 const GCId& ConcurrentMark::concurrent_gc_id() {
brutisso@6904 3428 if (has_aborted()) {
brutisso@6904 3429 return _aborted_gc_id;
brutisso@6904 3430 }
brutisso@6904 3431 return _g1h->gc_tracer_cm()->gc_id();
brutisso@6904 3432 }
brutisso@6904 3433
ysr@777 3434 static void print_ms_time_info(const char* prefix, const char* name,
ysr@777 3435 NumberSeq& ns) {
ysr@777 3436 gclog_or_tty->print_cr("%s%5d %12s: total time = %8.2f s (avg = %8.2f ms).",
ysr@777 3437 prefix, ns.num(), name, ns.sum()/1000.0, ns.avg());
ysr@777 3438 if (ns.num() > 0) {
ysr@777 3439 gclog_or_tty->print_cr("%s [std. dev = %8.2f ms, max = %8.2f ms]",
ysr@777 3440 prefix, ns.sd(), ns.maximum());
ysr@777 3441 }
ysr@777 3442 }
ysr@777 3443
ysr@777 3444 void ConcurrentMark::print_summary_info() {
ysr@777 3445 gclog_or_tty->print_cr(" Concurrent marking:");
ysr@777 3446 print_ms_time_info(" ", "init marks", _init_times);
ysr@777 3447 print_ms_time_info(" ", "remarks", _remark_times);
ysr@777 3448 {
ysr@777 3449 print_ms_time_info(" ", "final marks", _remark_mark_times);
ysr@777 3450 print_ms_time_info(" ", "weak refs", _remark_weak_ref_times);
ysr@777 3451
ysr@777 3452 }
ysr@777 3453 print_ms_time_info(" ", "cleanups", _cleanup_times);
ysr@777 3454 gclog_or_tty->print_cr(" Final counting total time = %8.2f s (avg = %8.2f ms).",
ysr@777 3455 _total_counting_time,
ysr@777 3456 (_cleanup_times.num() > 0 ? _total_counting_time * 1000.0 /
ysr@777 3457 (double)_cleanup_times.num()
ysr@777 3458 : 0.0));
ysr@777 3459 if (G1ScrubRemSets) {
ysr@777 3460 gclog_or_tty->print_cr(" RS scrub total time = %8.2f s (avg = %8.2f ms).",
ysr@777 3461 _total_rs_scrub_time,
ysr@777 3462 (_cleanup_times.num() > 0 ? _total_rs_scrub_time * 1000.0 /
ysr@777 3463 (double)_cleanup_times.num()
ysr@777 3464 : 0.0));
ysr@777 3465 }
ysr@777 3466 gclog_or_tty->print_cr(" Total stop_world time = %8.2f s.",
ysr@777 3467 (_init_times.sum() + _remark_times.sum() +
ysr@777 3468 _cleanup_times.sum())/1000.0);
ysr@777 3469 gclog_or_tty->print_cr(" Total concurrent time = %8.2f s "
johnc@3463 3470 "(%8.2f s marking).",
ysr@777 3471 cmThread()->vtime_accum(),
johnc@3463 3472 cmThread()->vtime_mark_accum());
ysr@777 3473 }
ysr@777 3474
tonyp@1454 3475 void ConcurrentMark::print_worker_threads_on(outputStream* st) const {
johnc@4549 3476 if (use_parallel_marking_threads()) {
johnc@4549 3477 _parallel_workers->print_worker_threads_on(st);
johnc@4549 3478 }
tonyp@1454 3479 }
tonyp@1454 3480
stefank@4904 3481 void ConcurrentMark::print_on_error(outputStream* st) const {
stefank@4904 3482 st->print_cr("Marking Bits (Prev, Next): (CMBitMap*) " PTR_FORMAT ", (CMBitMap*) " PTR_FORMAT,
drchase@6680 3483 p2i(_prevMarkBitMap), p2i(_nextMarkBitMap));
stefank@4904 3484 _prevMarkBitMap->print_on_error(st, " Prev Bits: ");
stefank@4904 3485 _nextMarkBitMap->print_on_error(st, " Next Bits: ");
stefank@4904 3486 }
stefank@4904 3487
ysr@777 3488 // We take a break if someone is trying to stop the world.
jmasa@3357 3489 bool ConcurrentMark::do_yield_check(uint worker_id) {
pliden@6906 3490 if (SuspendibleThreadSet::should_yield()) {
jmasa@3357 3491 if (worker_id == 0) {
ysr@777 3492 _g1h->g1_policy()->record_concurrent_pause();
tonyp@2973 3493 }
pliden@6906 3494 SuspendibleThreadSet::yield();
ysr@777 3495 return true;
ysr@777 3496 } else {
ysr@777 3497 return false;
ysr@777 3498 }
ysr@777 3499 }
ysr@777 3500
ysr@777 3501 #ifndef PRODUCT
ysr@777 3502 // for debugging purposes
ysr@777 3503 void ConcurrentMark::print_finger() {
ysr@777 3504 gclog_or_tty->print_cr("heap ["PTR_FORMAT", "PTR_FORMAT"), global finger = "PTR_FORMAT,
drchase@6680 3505 p2i(_heap_start), p2i(_heap_end), p2i(_finger));
johnc@4173 3506 for (uint i = 0; i < _max_worker_id; ++i) {
drchase@6680 3507 gclog_or_tty->print(" %u: " PTR_FORMAT, i, p2i(_tasks[i]->finger()));
ysr@777 3508 }
drchase@6680 3509 gclog_or_tty->cr();
ysr@777 3510 }
ysr@777 3511 #endif
ysr@777 3512
tonyp@2968 3513 void CMTask::scan_object(oop obj) {
tonyp@2968 3514 assert(_nextMarkBitMap->isMarked((HeapWord*) obj), "invariant");
tonyp@2968 3515
tonyp@2968 3516 if (_cm->verbose_high()) {
johnc@4173 3517 gclog_or_tty->print_cr("[%u] we're scanning object "PTR_FORMAT,
drchase@6680 3518 _worker_id, p2i((void*) obj));
tonyp@2968 3519 }
tonyp@2968 3520
tonyp@2968 3521 size_t obj_size = obj->size();
tonyp@2968 3522 _words_scanned += obj_size;
tonyp@2968 3523
tonyp@2968 3524 obj->oop_iterate(_cm_oop_closure);
tonyp@2968 3525 statsOnly( ++_objs_scanned );
tonyp@2968 3526 check_limits();
tonyp@2968 3527 }
tonyp@2968 3528
ysr@777 3529 // Closure for iteration over bitmaps
ysr@777 3530 class CMBitMapClosure : public BitMapClosure {
ysr@777 3531 private:
ysr@777 3532 // the bitmap that is being iterated over
ysr@777 3533 CMBitMap* _nextMarkBitMap;
ysr@777 3534 ConcurrentMark* _cm;
ysr@777 3535 CMTask* _task;
ysr@777 3536
ysr@777 3537 public:
tonyp@3691 3538 CMBitMapClosure(CMTask *task, ConcurrentMark* cm, CMBitMap* nextMarkBitMap) :
tonyp@3691 3539 _task(task), _cm(cm), _nextMarkBitMap(nextMarkBitMap) { }
ysr@777 3540
ysr@777 3541 bool do_bit(size_t offset) {
ysr@777 3542 HeapWord* addr = _nextMarkBitMap->offsetToHeapWord(offset);
tonyp@1458 3543 assert(_nextMarkBitMap->isMarked(addr), "invariant");
tonyp@1458 3544 assert( addr < _cm->finger(), "invariant");
ysr@777 3545
tonyp@3691 3546 statsOnly( _task->increase_objs_found_on_bitmap() );
tonyp@3691 3547 assert(addr >= _task->finger(), "invariant");
tonyp@3691 3548
tonyp@3691 3549 // We move that task's local finger along.
tonyp@3691 3550 _task->move_finger_to(addr);
ysr@777 3551
ysr@777 3552 _task->scan_object(oop(addr));
ysr@777 3553 // we only partially drain the local queue and global stack
ysr@777 3554 _task->drain_local_queue(true);
ysr@777 3555 _task->drain_global_stack(true);
ysr@777 3556
ysr@777 3557 // if the has_aborted flag has been raised, we need to bail out of
ysr@777 3558 // the iteration
ysr@777 3559 return !_task->has_aborted();
ysr@777 3560 }
ysr@777 3561 };
ysr@777 3562
tonyp@2968 3563 G1CMOopClosure::G1CMOopClosure(G1CollectedHeap* g1h,
tonyp@2968 3564 ConcurrentMark* cm,
tonyp@2968 3565 CMTask* task)
tonyp@2968 3566 : _g1h(g1h), _cm(cm), _task(task) {
tonyp@2968 3567 assert(_ref_processor == NULL, "should be initialized to NULL");
tonyp@2968 3568
tonyp@2968 3569 if (G1UseConcMarkReferenceProcessing) {
johnc@3175 3570 _ref_processor = g1h->ref_processor_cm();
tonyp@2968 3571 assert(_ref_processor != NULL, "should not be NULL");
ysr@777 3572 }
tonyp@2968 3573 }
ysr@777 3574
ysr@777 3575 void CMTask::setup_for_region(HeapRegion* hr) {
tonyp@1458 3576 assert(hr != NULL,
brutisso@7049 3577 "claim_region() should have filtered out NULL regions");
tonyp@1458 3578 assert(!hr->continuesHumongous(),
tonyp@1458 3579 "claim_region() should have filtered out continues humongous regions");
ysr@777 3580
tonyp@2973 3581 if (_cm->verbose_low()) {
johnc@4173 3582 gclog_or_tty->print_cr("[%u] setting up for region "PTR_FORMAT,
drchase@6680 3583 _worker_id, p2i(hr));
tonyp@2973 3584 }
ysr@777 3585
ysr@777 3586 _curr_region = hr;
ysr@777 3587 _finger = hr->bottom();
ysr@777 3588 update_region_limit();
ysr@777 3589 }
ysr@777 3590
ysr@777 3591 void CMTask::update_region_limit() {
ysr@777 3592 HeapRegion* hr = _curr_region;
ysr@777 3593 HeapWord* bottom = hr->bottom();
ysr@777 3594 HeapWord* limit = hr->next_top_at_mark_start();
ysr@777 3595
ysr@777 3596 if (limit == bottom) {
tonyp@2973 3597 if (_cm->verbose_low()) {
johnc@4173 3598 gclog_or_tty->print_cr("[%u] found an empty region "
ysr@777 3599 "["PTR_FORMAT", "PTR_FORMAT")",
drchase@6680 3600 _worker_id, p2i(bottom), p2i(limit));
tonyp@2973 3601 }
ysr@777 3602 // The region was collected underneath our feet.
ysr@777 3603 // We set the finger to bottom to ensure that the bitmap
ysr@777 3604 // iteration that will follow this will not do anything.
ysr@777 3605 // (this is not a condition that holds when we set the region up,
ysr@777 3606 // as the region is not supposed to be empty in the first place)
ysr@777 3607 _finger = bottom;
ysr@777 3608 } else if (limit >= _region_limit) {
tonyp@1458 3609 assert(limit >= _finger, "peace of mind");
ysr@777 3610 } else {
tonyp@1458 3611 assert(limit < _region_limit, "only way to get here");
ysr@777 3612 // This can happen under some pretty unusual circumstances. An
ysr@777 3613 // evacuation pause empties the region underneath our feet (NTAMS
ysr@777 3614 // at bottom). We then do some allocation in the region (NTAMS
ysr@777 3615 // stays at bottom), followed by the region being used as a GC
ysr@777 3616 // alloc region (NTAMS will move to top() and the objects
ysr@777 3617 // originally below it will be grayed). All objects now marked in
ysr@777 3618 // the region are explicitly grayed, if below the global finger,
ysr@777 3619 // and we do not need in fact to scan anything else. So, we simply
ysr@777 3620 // set _finger to be limit to ensure that the bitmap iteration
ysr@777 3621 // doesn't do anything.
ysr@777 3622 _finger = limit;
ysr@777 3623 }
ysr@777 3624
ysr@777 3625 _region_limit = limit;
ysr@777 3626 }
ysr@777 3627
ysr@777 3628 void CMTask::giveup_current_region() {
tonyp@1458 3629 assert(_curr_region != NULL, "invariant");
tonyp@2973 3630 if (_cm->verbose_low()) {
johnc@4173 3631 gclog_or_tty->print_cr("[%u] giving up region "PTR_FORMAT,
drchase@6680 3632 _worker_id, p2i(_curr_region));
tonyp@2973 3633 }
ysr@777 3634 clear_region_fields();
ysr@777 3635 }
ysr@777 3636
ysr@777 3637 void CMTask::clear_region_fields() {
ysr@777 3638 // Values for these three fields that indicate that we're not
ysr@777 3639 // holding on to a region.
ysr@777 3640 _curr_region = NULL;
ysr@777 3641 _finger = NULL;
ysr@777 3642 _region_limit = NULL;
ysr@777 3643 }
ysr@777 3644
tonyp@2968 3645 void CMTask::set_cm_oop_closure(G1CMOopClosure* cm_oop_closure) {
tonyp@2968 3646 if (cm_oop_closure == NULL) {
tonyp@2968 3647 assert(_cm_oop_closure != NULL, "invariant");
tonyp@2968 3648 } else {
tonyp@2968 3649 assert(_cm_oop_closure == NULL, "invariant");
tonyp@2968 3650 }
tonyp@2968 3651 _cm_oop_closure = cm_oop_closure;
tonyp@2968 3652 }
tonyp@2968 3653
ysr@777 3654 void CMTask::reset(CMBitMap* nextMarkBitMap) {
tonyp@1458 3655 guarantee(nextMarkBitMap != NULL, "invariant");
ysr@777 3656
tonyp@2973 3657 if (_cm->verbose_low()) {
johnc@4173 3658 gclog_or_tty->print_cr("[%u] resetting", _worker_id);
tonyp@2973 3659 }
ysr@777 3660
ysr@777 3661 _nextMarkBitMap = nextMarkBitMap;
ysr@777 3662 clear_region_fields();
ysr@777 3663
ysr@777 3664 _calls = 0;
ysr@777 3665 _elapsed_time_ms = 0.0;
ysr@777 3666 _termination_time_ms = 0.0;
ysr@777 3667 _termination_start_time_ms = 0.0;
ysr@777 3668
ysr@777 3669 #if _MARKING_STATS_
ysr@777 3670 _local_pushes = 0;
ysr@777 3671 _local_pops = 0;
ysr@777 3672 _local_max_size = 0;
ysr@777 3673 _objs_scanned = 0;
ysr@777 3674 _global_pushes = 0;
ysr@777 3675 _global_pops = 0;
ysr@777 3676 _global_max_size = 0;
ysr@777 3677 _global_transfers_to = 0;
ysr@777 3678 _global_transfers_from = 0;
ysr@777 3679 _regions_claimed = 0;
ysr@777 3680 _objs_found_on_bitmap = 0;
ysr@777 3681 _satb_buffers_processed = 0;
ysr@777 3682 _steal_attempts = 0;
ysr@777 3683 _steals = 0;
ysr@777 3684 _aborted = 0;
ysr@777 3685 _aborted_overflow = 0;
ysr@777 3686 _aborted_cm_aborted = 0;
ysr@777 3687 _aborted_yield = 0;
ysr@777 3688 _aborted_timed_out = 0;
ysr@777 3689 _aborted_satb = 0;
ysr@777 3690 _aborted_termination = 0;
ysr@777 3691 #endif // _MARKING_STATS_
ysr@777 3692 }
ysr@777 3693
ysr@777 3694 bool CMTask::should_exit_termination() {
ysr@777 3695 regular_clock_call();
ysr@777 3696 // This is called when we are in the termination protocol. We should
ysr@777 3697 // quit if, for some reason, this task wants to abort or the global
ysr@777 3698 // stack is not empty (this means that we can get work from it).
ysr@777 3699 return !_cm->mark_stack_empty() || has_aborted();
ysr@777 3700 }
ysr@777 3701
ysr@777 3702 void CMTask::reached_limit() {
tonyp@1458 3703 assert(_words_scanned >= _words_scanned_limit ||
tonyp@1458 3704 _refs_reached >= _refs_reached_limit ,
tonyp@1458 3705 "shouldn't have been called otherwise");
ysr@777 3706 regular_clock_call();
ysr@777 3707 }
ysr@777 3708
ysr@777 3709 void CMTask::regular_clock_call() {
tonyp@2973 3710 if (has_aborted()) return;
ysr@777 3711
ysr@777 3712 // First, we need to recalculate the words scanned and refs reached
ysr@777 3713 // limits for the next clock call.
ysr@777 3714 recalculate_limits();
ysr@777 3715
ysr@777 3716 // During the regular clock call we do the following
ysr@777 3717
ysr@777 3718 // (1) If an overflow has been flagged, then we abort.
ysr@777 3719 if (_cm->has_overflown()) {
ysr@777 3720 set_has_aborted();
ysr@777 3721 return;
ysr@777 3722 }
ysr@777 3723
ysr@777 3724 // If we are not concurrent (i.e. we're doing remark) we don't need
ysr@777 3725 // to check anything else. The other steps are only needed during
ysr@777 3726 // the concurrent marking phase.
tonyp@2973 3727 if (!concurrent()) return;
ysr@777 3728
ysr@777 3729 // (2) If marking has been aborted for Full GC, then we also abort.
ysr@777 3730 if (_cm->has_aborted()) {
ysr@777 3731 set_has_aborted();
ysr@777 3732 statsOnly( ++_aborted_cm_aborted );
ysr@777 3733 return;
ysr@777 3734 }
ysr@777 3735
ysr@777 3736 double curr_time_ms = os::elapsedVTime() * 1000.0;
ysr@777 3737
ysr@777 3738 // (3) If marking stats are enabled, then we update the step history.
ysr@777 3739 #if _MARKING_STATS_
tonyp@2973 3740 if (_words_scanned >= _words_scanned_limit) {
ysr@777 3741 ++_clock_due_to_scanning;
tonyp@2973 3742 }
tonyp@2973 3743 if (_refs_reached >= _refs_reached_limit) {
ysr@777 3744 ++_clock_due_to_marking;
tonyp@2973 3745 }
ysr@777 3746
ysr@777 3747 double last_interval_ms = curr_time_ms - _interval_start_time_ms;
ysr@777 3748 _interval_start_time_ms = curr_time_ms;
ysr@777 3749 _all_clock_intervals_ms.add(last_interval_ms);
ysr@777 3750
ysr@777 3751 if (_cm->verbose_medium()) {
johnc@4173 3752 gclog_or_tty->print_cr("[%u] regular clock, interval = %1.2lfms, "
tschatzl@7094 3753 "scanned = "SIZE_FORMAT"%s, refs reached = "SIZE_FORMAT"%s",
johnc@4173 3754 _worker_id, last_interval_ms,
tonyp@2973 3755 _words_scanned,
tonyp@2973 3756 (_words_scanned >= _words_scanned_limit) ? " (*)" : "",
tonyp@2973 3757 _refs_reached,
tonyp@2973 3758 (_refs_reached >= _refs_reached_limit) ? " (*)" : "");
ysr@777 3759 }
ysr@777 3760 #endif // _MARKING_STATS_
ysr@777 3761
ysr@777 3762 // (4) We check whether we should yield. If we have to, then we abort.
pliden@6906 3763 if (SuspendibleThreadSet::should_yield()) {
ysr@777 3764 // We should yield. To do this we abort the task. The caller is
ysr@777 3765 // responsible for yielding.
ysr@777 3766 set_has_aborted();
ysr@777 3767 statsOnly( ++_aborted_yield );
ysr@777 3768 return;
ysr@777 3769 }
ysr@777 3770
ysr@777 3771 // (5) We check whether we've reached our time quota. If we have,
ysr@777 3772 // then we abort.
ysr@777 3773 double elapsed_time_ms = curr_time_ms - _start_time_ms;
ysr@777 3774 if (elapsed_time_ms > _time_target_ms) {
ysr@777 3775 set_has_aborted();
johnc@2494 3776 _has_timed_out = true;
ysr@777 3777 statsOnly( ++_aborted_timed_out );
ysr@777 3778 return;
ysr@777 3779 }
ysr@777 3780
ysr@777 3781 // (6) Finally, we check whether there are enough completed STAB
ysr@777 3782 // buffers available for processing. If there are, we abort.
ysr@777 3783 SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();
ysr@777 3784 if (!_draining_satb_buffers && satb_mq_set.process_completed_buffers()) {
tonyp@2973 3785 if (_cm->verbose_low()) {
johnc@4173 3786 gclog_or_tty->print_cr("[%u] aborting to deal with pending SATB buffers",
johnc@4173 3787 _worker_id);
tonyp@2973 3788 }
ysr@777 3789 // we do need to process SATB buffers, we'll abort and restart
ysr@777 3790 // the marking task to do so
ysr@777 3791 set_has_aborted();
ysr@777 3792 statsOnly( ++_aborted_satb );
ysr@777 3793 return;
ysr@777 3794 }
ysr@777 3795 }
ysr@777 3796
ysr@777 3797 void CMTask::recalculate_limits() {
ysr@777 3798 _real_words_scanned_limit = _words_scanned + words_scanned_period;
ysr@777 3799 _words_scanned_limit = _real_words_scanned_limit;
ysr@777 3800
ysr@777 3801 _real_refs_reached_limit = _refs_reached + refs_reached_period;
ysr@777 3802 _refs_reached_limit = _real_refs_reached_limit;
ysr@777 3803 }
ysr@777 3804
ysr@777 3805 void CMTask::decrease_limits() {
ysr@777 3806 // This is called when we believe that we're going to do an infrequent
ysr@777 3807 // operation which will increase the per byte scanned cost (i.e. move
ysr@777 3808 // entries to/from the global stack). It basically tries to decrease the
ysr@777 3809 // scanning limit so that the clock is called earlier.
ysr@777 3810
tonyp@2973 3811 if (_cm->verbose_medium()) {
johnc@4173 3812 gclog_or_tty->print_cr("[%u] decreasing limits", _worker_id);
tonyp@2973 3813 }
ysr@777 3814
ysr@777 3815 _words_scanned_limit = _real_words_scanned_limit -
ysr@777 3816 3 * words_scanned_period / 4;
ysr@777 3817 _refs_reached_limit = _real_refs_reached_limit -
ysr@777 3818 3 * refs_reached_period / 4;
ysr@777 3819 }
ysr@777 3820
ysr@777 3821 void CMTask::move_entries_to_global_stack() {
ysr@777 3822 // local array where we'll store the entries that will be popped
ysr@777 3823 // from the local queue
ysr@777 3824 oop buffer[global_stack_transfer_size];
ysr@777 3825
ysr@777 3826 int n = 0;
ysr@777 3827 oop obj;
ysr@777 3828 while (n < global_stack_transfer_size && _task_queue->pop_local(obj)) {
ysr@777 3829 buffer[n] = obj;
ysr@777 3830 ++n;
ysr@777 3831 }
ysr@777 3832
ysr@777 3833 if (n > 0) {
ysr@777 3834 // we popped at least one entry from the local queue
ysr@777 3835
ysr@777 3836 statsOnly( ++_global_transfers_to; _local_pops += n );
ysr@777 3837
ysr@777 3838 if (!_cm->mark_stack_push(buffer, n)) {
tonyp@2973 3839 if (_cm->verbose_low()) {
johnc@4173 3840 gclog_or_tty->print_cr("[%u] aborting due to global stack overflow",
johnc@4173 3841 _worker_id);
tonyp@2973 3842 }
ysr@777 3843 set_has_aborted();
ysr@777 3844 } else {
ysr@777 3845 // the transfer was successful
ysr@777 3846
tonyp@2973 3847 if (_cm->verbose_medium()) {
johnc@4173 3848 gclog_or_tty->print_cr("[%u] pushed %d entries to the global stack",
johnc@4173 3849 _worker_id, n);
tonyp@2973 3850 }
ysr@777 3851 statsOnly( int tmp_size = _cm->mark_stack_size();
tonyp@2973 3852 if (tmp_size > _global_max_size) {
ysr@777 3853 _global_max_size = tmp_size;
tonyp@2973 3854 }
ysr@777 3855 _global_pushes += n );
ysr@777 3856 }
ysr@777 3857 }
ysr@777 3858
ysr@777 3859 // this operation was quite expensive, so decrease the limits
ysr@777 3860 decrease_limits();
ysr@777 3861 }
ysr@777 3862
ysr@777 3863 void CMTask::get_entries_from_global_stack() {
ysr@777 3864 // local array where we'll store the entries that will be popped
ysr@777 3865 // from the global stack.
ysr@777 3866 oop buffer[global_stack_transfer_size];
ysr@777 3867 int n;
ysr@777 3868 _cm->mark_stack_pop(buffer, global_stack_transfer_size, &n);
tonyp@1458 3869 assert(n <= global_stack_transfer_size,
tonyp@1458 3870 "we should not pop more than the given limit");
ysr@777 3871 if (n > 0) {
ysr@777 3872 // yes, we did actually pop at least one entry
ysr@777 3873
ysr@777 3874 statsOnly( ++_global_transfers_from; _global_pops += n );
tonyp@2973 3875 if (_cm->verbose_medium()) {
johnc@4173 3876 gclog_or_tty->print_cr("[%u] popped %d entries from the global stack",
johnc@4173 3877 _worker_id, n);
tonyp@2973 3878 }
ysr@777 3879 for (int i = 0; i < n; ++i) {
ysr@777 3880 bool success = _task_queue->push(buffer[i]);
ysr@777 3881 // We only call this when the local queue is empty or under a
ysr@777 3882 // given target limit. So, we do not expect this push to fail.
tonyp@1458 3883 assert(success, "invariant");
ysr@777 3884 }
ysr@777 3885
ysr@777 3886 statsOnly( int tmp_size = _task_queue->size();
tonyp@2973 3887 if (tmp_size > _local_max_size) {
ysr@777 3888 _local_max_size = tmp_size;
tonyp@2973 3889 }
ysr@777 3890 _local_pushes += n );
ysr@777 3891 }
ysr@777 3892
ysr@777 3893 // this operation was quite expensive, so decrease the limits
ysr@777 3894 decrease_limits();
ysr@777 3895 }
ysr@777 3896
ysr@777 3897 void CMTask::drain_local_queue(bool partially) {
tonyp@2973 3898 if (has_aborted()) return;
ysr@777 3899
ysr@777 3900 // Decide what the target size is, depending whether we're going to
ysr@777 3901 // drain it partially (so that other tasks can steal if they run out
ysr@777 3902 // of things to do) or totally (at the very end).
ysr@777 3903 size_t target_size;
tonyp@2973 3904 if (partially) {
ysr@777 3905 target_size = MIN2((size_t)_task_queue->max_elems()/3, GCDrainStackTargetSize);
tonyp@2973 3906 } else {
ysr@777 3907 target_size = 0;
tonyp@2973 3908 }
ysr@777 3909
ysr@777 3910 if (_task_queue->size() > target_size) {
tonyp@2973 3911 if (_cm->verbose_high()) {
drchase@6680 3912 gclog_or_tty->print_cr("[%u] draining local queue, target size = " SIZE_FORMAT,
johnc@4173 3913 _worker_id, target_size);
tonyp@2973 3914 }
ysr@777 3915
ysr@777 3916 oop obj;
ysr@777 3917 bool ret = _task_queue->pop_local(obj);
ysr@777 3918 while (ret) {
ysr@777 3919 statsOnly( ++_local_pops );
ysr@777 3920
tonyp@2973 3921 if (_cm->verbose_high()) {
johnc@4173 3922 gclog_or_tty->print_cr("[%u] popped "PTR_FORMAT, _worker_id,
drchase@6680 3923 p2i((void*) obj));
tonyp@2973 3924 }
ysr@777 3925
tonyp@1458 3926 assert(_g1h->is_in_g1_reserved((HeapWord*) obj), "invariant" );
tonyp@2643 3927 assert(!_g1h->is_on_master_free_list(
tonyp@2472 3928 _g1h->heap_region_containing((HeapWord*) obj)), "invariant");
ysr@777 3929
ysr@777 3930 scan_object(obj);
ysr@777 3931
tonyp@2973 3932 if (_task_queue->size() <= target_size || has_aborted()) {
ysr@777 3933 ret = false;
tonyp@2973 3934 } else {
ysr@777 3935 ret = _task_queue->pop_local(obj);
tonyp@2973 3936 }
ysr@777 3937 }
ysr@777 3938
tonyp@2973 3939 if (_cm->verbose_high()) {
johnc@4173 3940 gclog_or_tty->print_cr("[%u] drained local queue, size = %d",
johnc@4173 3941 _worker_id, _task_queue->size());
tonyp@2973 3942 }
ysr@777 3943 }
ysr@777 3944 }
ysr@777 3945
ysr@777 3946 void CMTask::drain_global_stack(bool partially) {
tonyp@2973 3947 if (has_aborted()) return;
ysr@777 3948
ysr@777 3949 // We have a policy to drain the local queue before we attempt to
ysr@777 3950 // drain the global stack.
tonyp@1458 3951 assert(partially || _task_queue->size() == 0, "invariant");
ysr@777 3952
ysr@777 3953 // Decide what the target size is, depending whether we're going to
ysr@777 3954 // drain it partially (so that other tasks can steal if they run out
ysr@777 3955 // of things to do) or totally (at the very end). Notice that,
ysr@777 3956 // because we move entries from the global stack in chunks or
ysr@777 3957 // because another task might be doing the same, we might in fact
ysr@777 3958 // drop below the target. But, this is not a problem.
ysr@777 3959 size_t target_size;
tonyp@2973 3960 if (partially) {
ysr@777 3961 target_size = _cm->partial_mark_stack_size_target();
tonyp@2973 3962 } else {
ysr@777 3963 target_size = 0;
tonyp@2973 3964 }
ysr@777 3965
ysr@777 3966 if (_cm->mark_stack_size() > target_size) {
tonyp@2973 3967 if (_cm->verbose_low()) {
drchase@6680 3968 gclog_or_tty->print_cr("[%u] draining global_stack, target size " SIZE_FORMAT,
johnc@4173 3969 _worker_id, target_size);
tonyp@2973 3970 }
ysr@777 3971
ysr@777 3972 while (!has_aborted() && _cm->mark_stack_size() > target_size) {
ysr@777 3973 get_entries_from_global_stack();
ysr@777 3974 drain_local_queue(partially);
ysr@777 3975 }
ysr@777 3976
tonyp@2973 3977 if (_cm->verbose_low()) {
drchase@6680 3978 gclog_or_tty->print_cr("[%u] drained global stack, size = " SIZE_FORMAT,
johnc@4173 3979 _worker_id, _cm->mark_stack_size());
tonyp@2973 3980 }
ysr@777 3981 }
ysr@777 3982 }
ysr@777 3983
ysr@777 3984 // SATB Queue has several assumptions on whether to call the par or
ysr@777 3985 // non-par versions of the methods. this is why some of the code is
ysr@777 3986 // replicated. We should really get rid of the single-threaded version
ysr@777 3987 // of the code to simplify things.
ysr@777 3988 void CMTask::drain_satb_buffers() {
tonyp@2973 3989 if (has_aborted()) return;
ysr@777 3990
ysr@777 3991 // We set this so that the regular clock knows that we're in the
ysr@777 3992 // middle of draining buffers and doesn't set the abort flag when it
ysr@777 3993 // notices that SATB buffers are available for draining. It'd be
ysr@777 3994 // very counter productive if it did that. :-)
ysr@777 3995 _draining_satb_buffers = true;
ysr@777 3996
ysr@777 3997 CMObjectClosure oc(this);
ysr@777 3998 SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();
tonyp@2973 3999 if (G1CollectedHeap::use_parallel_gc_threads()) {
johnc@4173 4000 satb_mq_set.set_par_closure(_worker_id, &oc);
tonyp@2973 4001 } else {
ysr@777 4002 satb_mq_set.set_closure(&oc);
tonyp@2973 4003 }
ysr@777 4004
ysr@777 4005 // This keeps claiming and applying the closure to completed buffers
ysr@777 4006 // until we run out of buffers or we need to abort.
jmasa@2188 4007 if (G1CollectedHeap::use_parallel_gc_threads()) {
ysr@777 4008 while (!has_aborted() &&
johnc@4173 4009 satb_mq_set.par_apply_closure_to_completed_buffer(_worker_id)) {
tonyp@2973 4010 if (_cm->verbose_medium()) {
johnc@4173 4011 gclog_or_tty->print_cr("[%u] processed an SATB buffer", _worker_id);
tonyp@2973 4012 }
ysr@777 4013 statsOnly( ++_satb_buffers_processed );
ysr@777 4014 regular_clock_call();
ysr@777 4015 }
ysr@777 4016 } else {
ysr@777 4017 while (!has_aborted() &&
ysr@777 4018 satb_mq_set.apply_closure_to_completed_buffer()) {
tonyp@2973 4019 if (_cm->verbose_medium()) {
johnc@4173 4020 gclog_or_tty->print_cr("[%u] processed an SATB buffer", _worker_id);
tonyp@2973 4021 }
ysr@777 4022 statsOnly( ++_satb_buffers_processed );
ysr@777 4023 regular_clock_call();
ysr@777 4024 }
ysr@777 4025 }
ysr@777 4026
ysr@777 4027 _draining_satb_buffers = false;
ysr@777 4028
tonyp@1458 4029 assert(has_aborted() ||
tonyp@1458 4030 concurrent() ||
tonyp@1458 4031 satb_mq_set.completed_buffers_num() == 0, "invariant");
ysr@777 4032
tonyp@2973 4033 if (G1CollectedHeap::use_parallel_gc_threads()) {
johnc@4173 4034 satb_mq_set.set_par_closure(_worker_id, NULL);
tonyp@2973 4035 } else {
ysr@777 4036 satb_mq_set.set_closure(NULL);
tonyp@2973 4037 }
ysr@777 4038
ysr@777 4039 // again, this was a potentially expensive operation, decrease the
ysr@777 4040 // limits to get the regular clock call early
ysr@777 4041 decrease_limits();
ysr@777 4042 }
ysr@777 4043
ysr@777 4044 void CMTask::print_stats() {
johnc@4173 4045 gclog_or_tty->print_cr("Marking Stats, task = %u, calls = %d",
johnc@4173 4046 _worker_id, _calls);
ysr@777 4047 gclog_or_tty->print_cr(" Elapsed time = %1.2lfms, Termination time = %1.2lfms",
ysr@777 4048 _elapsed_time_ms, _termination_time_ms);
ysr@777 4049 gclog_or_tty->print_cr(" Step Times (cum): num = %d, avg = %1.2lfms, sd = %1.2lfms",
ysr@777 4050 _step_times_ms.num(), _step_times_ms.avg(),
ysr@777 4051 _step_times_ms.sd());
ysr@777 4052 gclog_or_tty->print_cr(" max = %1.2lfms, total = %1.2lfms",
ysr@777 4053 _step_times_ms.maximum(), _step_times_ms.sum());
ysr@777 4054
ysr@777 4055 #if _MARKING_STATS_
ysr@777 4056 gclog_or_tty->print_cr(" Clock Intervals (cum): num = %d, avg = %1.2lfms, sd = %1.2lfms",
ysr@777 4057 _all_clock_intervals_ms.num(), _all_clock_intervals_ms.avg(),
ysr@777 4058 _all_clock_intervals_ms.sd());
ysr@777 4059 gclog_or_tty->print_cr(" max = %1.2lfms, total = %1.2lfms",
ysr@777 4060 _all_clock_intervals_ms.maximum(),
ysr@777 4061 _all_clock_intervals_ms.sum());
ysr@777 4062 gclog_or_tty->print_cr(" Clock Causes (cum): scanning = %d, marking = %d",
ysr@777 4063 _clock_due_to_scanning, _clock_due_to_marking);
ysr@777 4064 gclog_or_tty->print_cr(" Objects: scanned = %d, found on the bitmap = %d",
ysr@777 4065 _objs_scanned, _objs_found_on_bitmap);
ysr@777 4066 gclog_or_tty->print_cr(" Local Queue: pushes = %d, pops = %d, max size = %d",
ysr@777 4067 _local_pushes, _local_pops, _local_max_size);
ysr@777 4068 gclog_or_tty->print_cr(" Global Stack: pushes = %d, pops = %d, max size = %d",
ysr@777 4069 _global_pushes, _global_pops, _global_max_size);
ysr@777 4070 gclog_or_tty->print_cr(" transfers to = %d, transfers from = %d",
ysr@777 4071 _global_transfers_to,_global_transfers_from);
tonyp@3691 4072 gclog_or_tty->print_cr(" Regions: claimed = %d", _regions_claimed);
ysr@777 4073 gclog_or_tty->print_cr(" SATB buffers: processed = %d", _satb_buffers_processed);
ysr@777 4074 gclog_or_tty->print_cr(" Steals: attempts = %d, successes = %d",
ysr@777 4075 _steal_attempts, _steals);
ysr@777 4076 gclog_or_tty->print_cr(" Aborted: %d, due to", _aborted);
ysr@777 4077 gclog_or_tty->print_cr(" overflow: %d, global abort: %d, yield: %d",
ysr@777 4078 _aborted_overflow, _aborted_cm_aborted, _aborted_yield);
ysr@777 4079 gclog_or_tty->print_cr(" time out: %d, SATB: %d, termination: %d",
ysr@777 4080 _aborted_timed_out, _aborted_satb, _aborted_termination);
ysr@777 4081 #endif // _MARKING_STATS_
ysr@777 4082 }
ysr@777 4083
ysr@777 4084 /*****************************************************************************
ysr@777 4085
johnc@4787 4086 The do_marking_step(time_target_ms, ...) method is the building
johnc@4787 4087 block of the parallel marking framework. It can be called in parallel
ysr@777 4088 with other invocations of do_marking_step() on different tasks
ysr@777 4089 (but only one per task, obviously) and concurrently with the
ysr@777 4090 mutator threads, or during remark, hence it eliminates the need
ysr@777 4091 for two versions of the code. When called during remark, it will
ysr@777 4092 pick up from where the task left off during the concurrent marking
ysr@777 4093 phase. Interestingly, tasks are also claimable during evacuation
ysr@777 4094 pauses too, since do_marking_step() ensures that it aborts before
ysr@777 4095 it needs to yield.
ysr@777 4096
johnc@4787 4097 The data structures that it uses to do marking work are the
ysr@777 4098 following:
ysr@777 4099
ysr@777 4100 (1) Marking Bitmap. If there are gray objects that appear only
ysr@777 4101 on the bitmap (this happens either when dealing with an overflow
ysr@777 4102 or when the initial marking phase has simply marked the roots
ysr@777 4103 and didn't push them on the stack), then tasks claim heap
ysr@777 4104 regions whose bitmap they then scan to find gray objects. A
ysr@777 4105 global finger indicates where the end of the last claimed region
ysr@777 4106 is. A local finger indicates how far into the region a task has
ysr@777 4107 scanned. The two fingers are used to determine how to gray an
ysr@777 4108 object (i.e. whether simply marking it is OK, as it will be
ysr@777 4109 visited by a task in the future, or whether it needs to be also
ysr@777 4110 pushed on a stack).
ysr@777 4111
ysr@777 4112 (2) Local Queue. The local queue of the task which is accessed
ysr@777 4113 reasonably efficiently by the task. Other tasks can steal from
ysr@777 4114 it when they run out of work. Throughout the marking phase, a
ysr@777 4115 task attempts to keep its local queue short but not totally
ysr@777 4116 empty, so that entries are available for stealing by other
ysr@777 4117 tasks. Only when there is no more work, a task will totally
ysr@777 4118 drain its local queue.
ysr@777 4119
ysr@777 4120 (3) Global Mark Stack. This handles local queue overflow. During
ysr@777 4121 marking only sets of entries are moved between it and the local
ysr@777 4122 queues, as access to it requires a mutex and more fine-grain
ysr@777 4123 interaction with it which might cause contention. If it
ysr@777 4124 overflows, then the marking phase should restart and iterate
ysr@777 4125 over the bitmap to identify gray objects. Throughout the marking
ysr@777 4126 phase, tasks attempt to keep the global mark stack at a small
ysr@777 4127 length but not totally empty, so that entries are available for
ysr@777 4128 popping by other tasks. Only when there is no more work, tasks
ysr@777 4129 will totally drain the global mark stack.
ysr@777 4130
tonyp@3691 4131 (4) SATB Buffer Queue. This is where completed SATB buffers are
ysr@777 4132 made available. Buffers are regularly removed from this queue
ysr@777 4133 and scanned for roots, so that the queue doesn't get too
ysr@777 4134 long. During remark, all completed buffers are processed, as
ysr@777 4135 well as the filled in parts of any uncompleted buffers.
ysr@777 4136
ysr@777 4137 The do_marking_step() method tries to abort when the time target
ysr@777 4138 has been reached. There are a few other cases when the
ysr@777 4139 do_marking_step() method also aborts:
ysr@777 4140
ysr@777 4141 (1) When the marking phase has been aborted (after a Full GC).
ysr@777 4142
tonyp@3691 4143 (2) When a global overflow (on the global stack) has been
tonyp@3691 4144 triggered. Before the task aborts, it will actually sync up with
tonyp@3691 4145 the other tasks to ensure that all the marking data structures
johnc@4788 4146 (local queues, stacks, fingers etc.) are re-initialized so that
tonyp@3691 4147 when do_marking_step() completes, the marking phase can
tonyp@3691 4148 immediately restart.
ysr@777 4149
ysr@777 4150 (3) When enough completed SATB buffers are available. The
ysr@777 4151 do_marking_step() method only tries to drain SATB buffers right
ysr@777 4152 at the beginning. So, if enough buffers are available, the
ysr@777 4153 marking step aborts and the SATB buffers are processed at
ysr@777 4154 the beginning of the next invocation.
ysr@777 4155
ysr@777 4156 (4) To yield. when we have to yield then we abort and yield
ysr@777 4157 right at the end of do_marking_step(). This saves us from a lot
ysr@777 4158 of hassle as, by yielding we might allow a Full GC. If this
ysr@777 4159 happens then objects will be compacted underneath our feet, the
ysr@777 4160 heap might shrink, etc. We save checking for this by just
ysr@777 4161 aborting and doing the yield right at the end.
ysr@777 4162
ysr@777 4163 From the above it follows that the do_marking_step() method should
ysr@777 4164 be called in a loop (or, otherwise, regularly) until it completes.
ysr@777 4165
ysr@777 4166 If a marking step completes without its has_aborted() flag being
ysr@777 4167 true, it means it has completed the current marking phase (and
ysr@777 4168 also all other marking tasks have done so and have all synced up).
ysr@777 4169
ysr@777 4170 A method called regular_clock_call() is invoked "regularly" (in
ysr@777 4171 sub ms intervals) throughout marking. It is this clock method that
ysr@777 4172 checks all the abort conditions which were mentioned above and
ysr@777 4173 decides when the task should abort. A work-based scheme is used to
ysr@777 4174 trigger this clock method: when the number of object words the
ysr@777 4175 marking phase has scanned or the number of references the marking
ysr@777 4176 phase has visited reach a given limit. Additional invocations to
ysr@777 4177 the method clock have been planted in a few other strategic places
ysr@777 4178 too. The initial reason for the clock method was to avoid calling
ysr@777 4179 vtime too regularly, as it is quite expensive. So, once it was in
ysr@777 4180 place, it was natural to piggy-back all the other conditions on it
ysr@777 4181 too and not constantly check them throughout the code.
ysr@777 4182
johnc@4787 4183 If do_termination is true then do_marking_step will enter its
johnc@4787 4184 termination protocol.
johnc@4787 4185
johnc@4787 4186 The value of is_serial must be true when do_marking_step is being
johnc@4787 4187 called serially (i.e. by the VMThread) and do_marking_step should
johnc@4787 4188 skip any synchronization in the termination and overflow code.
johnc@4787 4189 Examples include the serial remark code and the serial reference
johnc@4787 4190 processing closures.
johnc@4787 4191
johnc@4787 4192 The value of is_serial must be false when do_marking_step is
johnc@4787 4193 being called by any of the worker threads in a work gang.
johnc@4787 4194 Examples include the concurrent marking code (CMMarkingTask),
johnc@4787 4195 the MT remark code, and the MT reference processing closures.
johnc@4787 4196
ysr@777 4197 *****************************************************************************/
ysr@777 4198
johnc@2494 4199 void CMTask::do_marking_step(double time_target_ms,
johnc@4787 4200 bool do_termination,
johnc@4787 4201 bool is_serial) {
tonyp@1458 4202 assert(time_target_ms >= 1.0, "minimum granularity is 1ms");
tonyp@1458 4203 assert(concurrent() == _cm->concurrent(), "they should be the same");
tonyp@1458 4204
ysr@777 4205 G1CollectorPolicy* g1_policy = _g1h->g1_policy();
tonyp@1458 4206 assert(_task_queues != NULL, "invariant");
tonyp@1458 4207 assert(_task_queue != NULL, "invariant");
johnc@4173 4208 assert(_task_queues->queue(_worker_id) == _task_queue, "invariant");
tonyp@1458 4209
tonyp@1458 4210 assert(!_claimed,
tonyp@1458 4211 "only one thread should claim this task at any one time");
ysr@777 4212
ysr@777 4213 // OK, this doesn't safeguard again all possible scenarios, as it is
ysr@777 4214 // possible for two threads to set the _claimed flag at the same
ysr@777 4215 // time. But it is only for debugging purposes anyway and it will
ysr@777 4216 // catch most problems.
ysr@777 4217 _claimed = true;
ysr@777 4218
ysr@777 4219 _start_time_ms = os::elapsedVTime() * 1000.0;
ysr@777 4220 statsOnly( _interval_start_time_ms = _start_time_ms );
ysr@777 4221
johnc@4787 4222 // If do_stealing is true then do_marking_step will attempt to
johnc@4787 4223 // steal work from the other CMTasks. It only makes sense to
johnc@4787 4224 // enable stealing when the termination protocol is enabled
johnc@4787 4225 // and do_marking_step() is not being called serially.
johnc@4787 4226 bool do_stealing = do_termination && !is_serial;
johnc@4787 4227
ysr@777 4228 double diff_prediction_ms =
ysr@777 4229 g1_policy->get_new_prediction(&_marking_step_diffs_ms);
ysr@777 4230 _time_target_ms = time_target_ms - diff_prediction_ms;
ysr@777 4231
ysr@777 4232 // set up the variables that are used in the work-based scheme to
ysr@777 4233 // call the regular clock method
ysr@777 4234 _words_scanned = 0;
ysr@777 4235 _refs_reached = 0;
ysr@777 4236 recalculate_limits();
ysr@777 4237
ysr@777 4238 // clear all flags
ysr@777 4239 clear_has_aborted();
johnc@2494 4240 _has_timed_out = false;
ysr@777 4241 _draining_satb_buffers = false;
ysr@777 4242
ysr@777 4243 ++_calls;
ysr@777 4244
tonyp@2973 4245 if (_cm->verbose_low()) {
johnc@4173 4246 gclog_or_tty->print_cr("[%u] >>>>>>>>>> START, call = %d, "
ysr@777 4247 "target = %1.2lfms >>>>>>>>>>",
johnc@4173 4248 _worker_id, _calls, _time_target_ms);
tonyp@2973 4249 }
ysr@777 4250
ysr@777 4251 // Set up the bitmap and oop closures. Anything that uses them is
ysr@777 4252 // eventually called from this method, so it is OK to allocate these
ysr@777 4253 // statically.
ysr@777 4254 CMBitMapClosure bitmap_closure(this, _cm, _nextMarkBitMap);
tonyp@2968 4255 G1CMOopClosure cm_oop_closure(_g1h, _cm, this);
tonyp@2968 4256 set_cm_oop_closure(&cm_oop_closure);
ysr@777 4257
ysr@777 4258 if (_cm->has_overflown()) {
tonyp@3691 4259 // This can happen if the mark stack overflows during a GC pause
tonyp@3691 4260 // and this task, after a yield point, restarts. We have to abort
tonyp@3691 4261 // as we need to get into the overflow protocol which happens
tonyp@3691 4262 // right at the end of this task.
ysr@777 4263 set_has_aborted();
ysr@777 4264 }
ysr@777 4265
ysr@777 4266 // First drain any available SATB buffers. After this, we will not
ysr@777 4267 // look at SATB buffers before the next invocation of this method.
ysr@777 4268 // If enough completed SATB buffers are queued up, the regular clock
ysr@777 4269 // will abort this task so that it restarts.
ysr@777 4270 drain_satb_buffers();
ysr@777 4271 // ...then partially drain the local queue and the global stack
ysr@777 4272 drain_local_queue(true);
ysr@777 4273 drain_global_stack(true);
ysr@777 4274
ysr@777 4275 do {
ysr@777 4276 if (!has_aborted() && _curr_region != NULL) {
ysr@777 4277 // This means that we're already holding on to a region.
tonyp@1458 4278 assert(_finger != NULL, "if region is not NULL, then the finger "
tonyp@1458 4279 "should not be NULL either");
ysr@777 4280
ysr@777 4281 // We might have restarted this task after an evacuation pause
ysr@777 4282 // which might have evacuated the region we're holding on to
ysr@777 4283 // underneath our feet. Let's read its limit again to make sure
ysr@777 4284 // that we do not iterate over a region of the heap that
ysr@777 4285 // contains garbage (update_region_limit() will also move
ysr@777 4286 // _finger to the start of the region if it is found empty).
ysr@777 4287 update_region_limit();
ysr@777 4288 // We will start from _finger not from the start of the region,
ysr@777 4289 // as we might be restarting this task after aborting half-way
ysr@777 4290 // through scanning this region. In this case, _finger points to
ysr@777 4291 // the address where we last found a marked object. If this is a
ysr@777 4292 // fresh region, _finger points to start().
ysr@777 4293 MemRegion mr = MemRegion(_finger, _region_limit);
ysr@777 4294
tonyp@2973 4295 if (_cm->verbose_low()) {
johnc@4173 4296 gclog_or_tty->print_cr("[%u] we're scanning part "
ysr@777 4297 "["PTR_FORMAT", "PTR_FORMAT") "
johnc@4580 4298 "of region "HR_FORMAT,
drchase@6680 4299 _worker_id, p2i(_finger), p2i(_region_limit),
johnc@4580 4300 HR_FORMAT_PARAMS(_curr_region));
tonyp@2973 4301 }
ysr@777 4302
johnc@4580 4303 assert(!_curr_region->isHumongous() || mr.start() == _curr_region->bottom(),
johnc@4580 4304 "humongous regions should go around loop once only");
johnc@4580 4305
johnc@4580 4306 // Some special cases:
johnc@4580 4307 // If the memory region is empty, we can just give up the region.
johnc@4580 4308 // If the current region is humongous then we only need to check
johnc@4580 4309 // the bitmap for the bit associated with the start of the object,
johnc@4580 4310 // scan the object if it's live, and give up the region.
johnc@4580 4311 // Otherwise, let's iterate over the bitmap of the part of the region
johnc@4580 4312 // that is left.
johnc@4575 4313 // If the iteration is successful, give up the region.
johnc@4580 4314 if (mr.is_empty()) {
johnc@4580 4315 giveup_current_region();
johnc@4580 4316 regular_clock_call();
johnc@4580 4317 } else if (_curr_region->isHumongous() && mr.start() == _curr_region->bottom()) {
johnc@4580 4318 if (_nextMarkBitMap->isMarked(mr.start())) {
johnc@4580 4319 // The object is marked - apply the closure
johnc@4580 4320 BitMap::idx_t offset = _nextMarkBitMap->heapWordToOffset(mr.start());
johnc@4580 4321 bitmap_closure.do_bit(offset);
johnc@4580 4322 }
johnc@4580 4323 // Even if this task aborted while scanning the humongous object
johnc@4580 4324 // we can (and should) give up the current region.
johnc@4580 4325 giveup_current_region();
johnc@4580 4326 regular_clock_call();
johnc@4580 4327 } else if (_nextMarkBitMap->iterate(&bitmap_closure, mr)) {
ysr@777 4328 giveup_current_region();
ysr@777 4329 regular_clock_call();
ysr@777 4330 } else {
tonyp@1458 4331 assert(has_aborted(), "currently the only way to do so");
ysr@777 4332 // The only way to abort the bitmap iteration is to return
ysr@777 4333 // false from the do_bit() method. However, inside the
ysr@777 4334 // do_bit() method we move the _finger to point to the
ysr@777 4335 // object currently being looked at. So, if we bail out, we
ysr@777 4336 // have definitely set _finger to something non-null.
tonyp@1458 4337 assert(_finger != NULL, "invariant");
ysr@777 4338
ysr@777 4339 // Region iteration was actually aborted. So now _finger
ysr@777 4340 // points to the address of the object we last scanned. If we
ysr@777 4341 // leave it there, when we restart this task, we will rescan
ysr@777 4342 // the object. It is easy to avoid this. We move the finger by
ysr@777 4343 // enough to point to the next possible object header (the
ysr@777 4344 // bitmap knows by how much we need to move it as it knows its
ysr@777 4345 // granularity).
apetrusenko@1749 4346 assert(_finger < _region_limit, "invariant");
tamao@4733 4347 HeapWord* new_finger = _nextMarkBitMap->nextObject(_finger);
apetrusenko@1749 4348 // Check if bitmap iteration was aborted while scanning the last object
apetrusenko@1749 4349 if (new_finger >= _region_limit) {
tonyp@3691 4350 giveup_current_region();
apetrusenko@1749 4351 } else {
tonyp@3691 4352 move_finger_to(new_finger);
apetrusenko@1749 4353 }
ysr@777 4354 }
ysr@777 4355 }
ysr@777 4356 // At this point we have either completed iterating over the
ysr@777 4357 // region we were holding on to, or we have aborted.
ysr@777 4358
ysr@777 4359 // We then partially drain the local queue and the global stack.
ysr@777 4360 // (Do we really need this?)
ysr@777 4361 drain_local_queue(true);
ysr@777 4362 drain_global_stack(true);
ysr@777 4363
ysr@777 4364 // Read the note on the claim_region() method on why it might
ysr@777 4365 // return NULL with potentially more regions available for
ysr@777 4366 // claiming and why we have to check out_of_regions() to determine
ysr@777 4367 // whether we're done or not.
ysr@777 4368 while (!has_aborted() && _curr_region == NULL && !_cm->out_of_regions()) {
ysr@777 4369 // We are going to try to claim a new region. We should have
ysr@777 4370 // given up on the previous one.
tonyp@1458 4371 // Separated the asserts so that we know which one fires.
tonyp@1458 4372 assert(_curr_region == NULL, "invariant");
tonyp@1458 4373 assert(_finger == NULL, "invariant");
tonyp@1458 4374 assert(_region_limit == NULL, "invariant");
tonyp@2973 4375 if (_cm->verbose_low()) {
johnc@4173 4376 gclog_or_tty->print_cr("[%u] trying to claim a new region", _worker_id);
tonyp@2973 4377 }
johnc@4173 4378 HeapRegion* claimed_region = _cm->claim_region(_worker_id);
ysr@777 4379 if (claimed_region != NULL) {
ysr@777 4380 // Yes, we managed to claim one
ysr@777 4381 statsOnly( ++_regions_claimed );
ysr@777 4382
tonyp@2973 4383 if (_cm->verbose_low()) {
johnc@4173 4384 gclog_or_tty->print_cr("[%u] we successfully claimed "
ysr@777 4385 "region "PTR_FORMAT,
drchase@6680 4386 _worker_id, p2i(claimed_region));
tonyp@2973 4387 }
ysr@777 4388
ysr@777 4389 setup_for_region(claimed_region);
tonyp@1458 4390 assert(_curr_region == claimed_region, "invariant");
ysr@777 4391 }
ysr@777 4392 // It is important to call the regular clock here. It might take
ysr@777 4393 // a while to claim a region if, for example, we hit a large
ysr@777 4394 // block of empty regions. So we need to call the regular clock
ysr@777 4395 // method once round the loop to make sure it's called
ysr@777 4396 // frequently enough.
ysr@777 4397 regular_clock_call();
ysr@777 4398 }
ysr@777 4399
ysr@777 4400 if (!has_aborted() && _curr_region == NULL) {
tonyp@1458 4401 assert(_cm->out_of_regions(),
tonyp@1458 4402 "at this point we should be out of regions");
ysr@777 4403 }
ysr@777 4404 } while ( _curr_region != NULL && !has_aborted());
ysr@777 4405
ysr@777 4406 if (!has_aborted()) {
ysr@777 4407 // We cannot check whether the global stack is empty, since other
tonyp@3691 4408 // tasks might be pushing objects to it concurrently.
tonyp@1458 4409 assert(_cm->out_of_regions(),
tonyp@1458 4410 "at this point we should be out of regions");
ysr@777 4411
tonyp@2973 4412 if (_cm->verbose_low()) {
johnc@4173 4413 gclog_or_tty->print_cr("[%u] all regions claimed", _worker_id);
tonyp@2973 4414 }
ysr@777 4415
ysr@777 4416 // Try to reduce the number of available SATB buffers so that
ysr@777 4417 // remark has less work to do.
ysr@777 4418 drain_satb_buffers();
ysr@777 4419 }
ysr@777 4420
ysr@777 4421 // Since we've done everything else, we can now totally drain the
ysr@777 4422 // local queue and global stack.
ysr@777 4423 drain_local_queue(false);
ysr@777 4424 drain_global_stack(false);
ysr@777 4425
ysr@777 4426 // Attempt at work stealing from other task's queues.
johnc@2494 4427 if (do_stealing && !has_aborted()) {
ysr@777 4428 // We have not aborted. This means that we have finished all that
ysr@777 4429 // we could. Let's try to do some stealing...
ysr@777 4430
ysr@777 4431 // We cannot check whether the global stack is empty, since other
tonyp@3691 4432 // tasks might be pushing objects to it concurrently.
tonyp@1458 4433 assert(_cm->out_of_regions() && _task_queue->size() == 0,
tonyp@1458 4434 "only way to reach here");
ysr@777 4435
tonyp@2973 4436 if (_cm->verbose_low()) {
johnc@4173 4437 gclog_or_tty->print_cr("[%u] starting to steal", _worker_id);
tonyp@2973 4438 }
ysr@777 4439
ysr@777 4440 while (!has_aborted()) {
ysr@777 4441 oop obj;
ysr@777 4442 statsOnly( ++_steal_attempts );
ysr@777 4443
johnc@4173 4444 if (_cm->try_stealing(_worker_id, &_hash_seed, obj)) {
tonyp@2973 4445 if (_cm->verbose_medium()) {
johnc@4173 4446 gclog_or_tty->print_cr("[%u] stolen "PTR_FORMAT" successfully",
drchase@6680 4447 _worker_id, p2i((void*) obj));
tonyp@2973 4448 }
ysr@777 4449
ysr@777 4450 statsOnly( ++_steals );
ysr@777 4451
tonyp@1458 4452 assert(_nextMarkBitMap->isMarked((HeapWord*) obj),
tonyp@1458 4453 "any stolen object should be marked");
ysr@777 4454 scan_object(obj);
ysr@777 4455
ysr@777 4456 // And since we're towards the end, let's totally drain the
ysr@777 4457 // local queue and global stack.
ysr@777 4458 drain_local_queue(false);
ysr@777 4459 drain_global_stack(false);
ysr@777 4460 } else {
ysr@777 4461 break;
ysr@777 4462 }
ysr@777 4463 }
ysr@777 4464 }
ysr@777 4465
tonyp@2848 4466 // If we are about to wrap up and go into termination, check if we
tonyp@2848 4467 // should raise the overflow flag.
tonyp@2848 4468 if (do_termination && !has_aborted()) {
tonyp@2848 4469 if (_cm->force_overflow()->should_force()) {
tonyp@2848 4470 _cm->set_has_overflown();
tonyp@2848 4471 regular_clock_call();
tonyp@2848 4472 }
tonyp@2848 4473 }
tonyp@2848 4474
ysr@777 4475 // We still haven't aborted. Now, let's try to get into the
ysr@777 4476 // termination protocol.
johnc@2494 4477 if (do_termination && !has_aborted()) {
ysr@777 4478 // We cannot check whether the global stack is empty, since other
tonyp@3691 4479 // tasks might be concurrently pushing objects on it.
tonyp@1458 4480 // Separated the asserts so that we know which one fires.
tonyp@1458 4481 assert(_cm->out_of_regions(), "only way to reach here");
tonyp@1458 4482 assert(_task_queue->size() == 0, "only way to reach here");
ysr@777 4483
tonyp@2973 4484 if (_cm->verbose_low()) {
johnc@4173 4485 gclog_or_tty->print_cr("[%u] starting termination protocol", _worker_id);
tonyp@2973 4486 }
ysr@777 4487
ysr@777 4488 _termination_start_time_ms = os::elapsedVTime() * 1000.0;
johnc@4787 4489
ysr@777 4490 // The CMTask class also extends the TerminatorTerminator class,
ysr@777 4491 // hence its should_exit_termination() method will also decide
ysr@777 4492 // whether to exit the termination protocol or not.
johnc@4787 4493 bool finished = (is_serial ||
johnc@4787 4494 _cm->terminator()->offer_termination(this));
ysr@777 4495 double termination_end_time_ms = os::elapsedVTime() * 1000.0;
ysr@777 4496 _termination_time_ms +=
ysr@777 4497 termination_end_time_ms - _termination_start_time_ms;
ysr@777 4498
ysr@777 4499 if (finished) {
ysr@777 4500 // We're all done.
ysr@777 4501
johnc@4173 4502 if (_worker_id == 0) {
ysr@777 4503 // let's allow task 0 to do this
ysr@777 4504 if (concurrent()) {
tonyp@1458 4505 assert(_cm->concurrent_marking_in_progress(), "invariant");
ysr@777 4506 // we need to set this to false before the next
ysr@777 4507 // safepoint. This way we ensure that the marking phase
ysr@777 4508 // doesn't observe any more heap expansions.
ysr@777 4509 _cm->clear_concurrent_marking_in_progress();
ysr@777 4510 }
ysr@777 4511 }
ysr@777 4512
ysr@777 4513 // We can now guarantee that the global stack is empty, since
tonyp@1458 4514 // all other tasks have finished. We separated the guarantees so
tonyp@1458 4515 // that, if a condition is false, we can immediately find out
tonyp@1458 4516 // which one.
tonyp@1458 4517 guarantee(_cm->out_of_regions(), "only way to reach here");
tonyp@1458 4518 guarantee(_cm->mark_stack_empty(), "only way to reach here");
tonyp@1458 4519 guarantee(_task_queue->size() == 0, "only way to reach here");
tonyp@1458 4520 guarantee(!_cm->has_overflown(), "only way to reach here");
tonyp@1458 4521 guarantee(!_cm->mark_stack_overflow(), "only way to reach here");
ysr@777 4522
tonyp@2973 4523 if (_cm->verbose_low()) {
johnc@4173 4524 gclog_or_tty->print_cr("[%u] all tasks terminated", _worker_id);
tonyp@2973 4525 }
ysr@777 4526 } else {
ysr@777 4527 // Apparently there's more work to do. Let's abort this task. It
ysr@777 4528 // will restart it and we can hopefully find more things to do.
ysr@777 4529
tonyp@2973 4530 if (_cm->verbose_low()) {
johnc@4173 4531 gclog_or_tty->print_cr("[%u] apparently there is more work to do",
johnc@4173 4532 _worker_id);
tonyp@2973 4533 }
ysr@777 4534
ysr@777 4535 set_has_aborted();
ysr@777 4536 statsOnly( ++_aborted_termination );
ysr@777 4537 }
ysr@777 4538 }
ysr@777 4539
ysr@777 4540 // Mainly for debugging purposes to make sure that a pointer to the
ysr@777 4541 // closure which was statically allocated in this frame doesn't
ysr@777 4542 // escape it by accident.
tonyp@2968 4543 set_cm_oop_closure(NULL);
ysr@777 4544 double end_time_ms = os::elapsedVTime() * 1000.0;
ysr@777 4545 double elapsed_time_ms = end_time_ms - _start_time_ms;
ysr@777 4546 // Update the step history.
ysr@777 4547 _step_times_ms.add(elapsed_time_ms);
ysr@777 4548
ysr@777 4549 if (has_aborted()) {
ysr@777 4550 // The task was aborted for some reason.
ysr@777 4551
ysr@777 4552 statsOnly( ++_aborted );
ysr@777 4553
johnc@2494 4554 if (_has_timed_out) {
ysr@777 4555 double diff_ms = elapsed_time_ms - _time_target_ms;
ysr@777 4556 // Keep statistics of how well we did with respect to hitting
ysr@777 4557 // our target only if we actually timed out (if we aborted for
ysr@777 4558 // other reasons, then the results might get skewed).
ysr@777 4559 _marking_step_diffs_ms.add(diff_ms);
ysr@777 4560 }
ysr@777 4561
ysr@777 4562 if (_cm->has_overflown()) {
ysr@777 4563 // This is the interesting one. We aborted because a global
ysr@777 4564 // overflow was raised. This means we have to restart the
ysr@777 4565 // marking phase and start iterating over regions. However, in
ysr@777 4566 // order to do this we have to make sure that all tasks stop
ysr@777 4567 // what they are doing and re-initialise in a safe manner. We
ysr@777 4568 // will achieve this with the use of two barrier sync points.
ysr@777 4569
tonyp@2973 4570 if (_cm->verbose_low()) {
johnc@4173 4571 gclog_or_tty->print_cr("[%u] detected overflow", _worker_id);
tonyp@2973 4572 }
ysr@777 4573
johnc@4787 4574 if (!is_serial) {
johnc@4787 4575 // We only need to enter the sync barrier if being called
johnc@4787 4576 // from a parallel context
johnc@4787 4577 _cm->enter_first_sync_barrier(_worker_id);
johnc@4787 4578
johnc@4787 4579 // When we exit this sync barrier we know that all tasks have
johnc@4787 4580 // stopped doing marking work. So, it's now safe to
johnc@4787 4581 // re-initialise our data structures. At the end of this method,
johnc@4787 4582 // task 0 will clear the global data structures.
johnc@4787 4583 }
ysr@777 4584
ysr@777 4585 statsOnly( ++_aborted_overflow );
ysr@777 4586
ysr@777 4587 // We clear the local state of this task...
ysr@777 4588 clear_region_fields();
ysr@777 4589
johnc@4787 4590 if (!is_serial) {
johnc@4787 4591 // ...and enter the second barrier.
johnc@4787 4592 _cm->enter_second_sync_barrier(_worker_id);
johnc@4787 4593 }
johnc@4788 4594 // At this point, if we're during the concurrent phase of
johnc@4788 4595 // marking, everything has been re-initialized and we're
ysr@777 4596 // ready to restart.
ysr@777 4597 }
ysr@777 4598
ysr@777 4599 if (_cm->verbose_low()) {
johnc@4173 4600 gclog_or_tty->print_cr("[%u] <<<<<<<<<< ABORTING, target = %1.2lfms, "
ysr@777 4601 "elapsed = %1.2lfms <<<<<<<<<<",
johnc@4173 4602 _worker_id, _time_target_ms, elapsed_time_ms);
tonyp@2973 4603 if (_cm->has_aborted()) {
johnc@4173 4604 gclog_or_tty->print_cr("[%u] ========== MARKING ABORTED ==========",
johnc@4173 4605 _worker_id);
tonyp@2973 4606 }
ysr@777 4607 }
ysr@777 4608 } else {
tonyp@2973 4609 if (_cm->verbose_low()) {
johnc@4173 4610 gclog_or_tty->print_cr("[%u] <<<<<<<<<< FINISHED, target = %1.2lfms, "
ysr@777 4611 "elapsed = %1.2lfms <<<<<<<<<<",
johnc@4173 4612 _worker_id, _time_target_ms, elapsed_time_ms);
tonyp@2973 4613 }
ysr@777 4614 }
ysr@777 4615
ysr@777 4616 _claimed = false;
ysr@777 4617 }
ysr@777 4618
johnc@4173 4619 CMTask::CMTask(uint worker_id,
ysr@777 4620 ConcurrentMark* cm,
johnc@3463 4621 size_t* marked_bytes,
johnc@3463 4622 BitMap* card_bm,
ysr@777 4623 CMTaskQueue* task_queue,
ysr@777 4624 CMTaskQueueSet* task_queues)
ysr@777 4625 : _g1h(G1CollectedHeap::heap()),
johnc@4173 4626 _worker_id(worker_id), _cm(cm),
ysr@777 4627 _claimed(false),
ysr@777 4628 _nextMarkBitMap(NULL), _hash_seed(17),
ysr@777 4629 _task_queue(task_queue),
ysr@777 4630 _task_queues(task_queues),
tonyp@2968 4631 _cm_oop_closure(NULL),
johnc@3463 4632 _marked_bytes_array(marked_bytes),
johnc@3463 4633 _card_bm(card_bm) {
tonyp@1458 4634 guarantee(task_queue != NULL, "invariant");
tonyp@1458 4635 guarantee(task_queues != NULL, "invariant");
ysr@777 4636
ysr@777 4637 statsOnly( _clock_due_to_scanning = 0;
ysr@777 4638 _clock_due_to_marking = 0 );
ysr@777 4639
ysr@777 4640 _marking_step_diffs_ms.add(0.5);
ysr@777 4641 }
tonyp@2717 4642
tonyp@2717 4643 // These are formatting macros that are used below to ensure
tonyp@2717 4644 // consistent formatting. The *_H_* versions are used to format the
tonyp@2717 4645 // header for a particular value and they should be kept consistent
tonyp@2717 4646 // with the corresponding macro. Also note that most of the macros add
tonyp@2717 4647 // the necessary white space (as a prefix) which makes them a bit
tonyp@2717 4648 // easier to compose.
tonyp@2717 4649
tonyp@2717 4650 // All the output lines are prefixed with this string to be able to
tonyp@2717 4651 // identify them easily in a large log file.
tonyp@2717 4652 #define G1PPRL_LINE_PREFIX "###"
tonyp@2717 4653
tonyp@2717 4654 #define G1PPRL_ADDR_BASE_FORMAT " "PTR_FORMAT"-"PTR_FORMAT
tonyp@2717 4655 #ifdef _LP64
tonyp@2717 4656 #define G1PPRL_ADDR_BASE_H_FORMAT " %37s"
tonyp@2717 4657 #else // _LP64
tonyp@2717 4658 #define G1PPRL_ADDR_BASE_H_FORMAT " %21s"
tonyp@2717 4659 #endif // _LP64
tonyp@2717 4660
tonyp@2717 4661 // For per-region info
tonyp@2717 4662 #define G1PPRL_TYPE_FORMAT " %-4s"
tonyp@2717 4663 #define G1PPRL_TYPE_H_FORMAT " %4s"
tonyp@2717 4664 #define G1PPRL_BYTE_FORMAT " "SIZE_FORMAT_W(9)
tonyp@2717 4665 #define G1PPRL_BYTE_H_FORMAT " %9s"
tonyp@2717 4666 #define G1PPRL_DOUBLE_FORMAT " %14.1f"
tonyp@2717 4667 #define G1PPRL_DOUBLE_H_FORMAT " %14s"
tonyp@2717 4668
tonyp@2717 4669 // For summary info
tonyp@2717 4670 #define G1PPRL_SUM_ADDR_FORMAT(tag) " "tag":"G1PPRL_ADDR_BASE_FORMAT
tonyp@2717 4671 #define G1PPRL_SUM_BYTE_FORMAT(tag) " "tag": "SIZE_FORMAT
tonyp@2717 4672 #define G1PPRL_SUM_MB_FORMAT(tag) " "tag": %1.2f MB"
tonyp@2717 4673 #define G1PPRL_SUM_MB_PERC_FORMAT(tag) G1PPRL_SUM_MB_FORMAT(tag)" / %1.2f %%"
tonyp@2717 4674
tonyp@2717 4675 G1PrintRegionLivenessInfoClosure::
tonyp@2717 4676 G1PrintRegionLivenessInfoClosure(outputStream* out, const char* phase_name)
tonyp@2717 4677 : _out(out),
tonyp@2717 4678 _total_used_bytes(0), _total_capacity_bytes(0),
tonyp@2717 4679 _total_prev_live_bytes(0), _total_next_live_bytes(0),
tonyp@2717 4680 _hum_used_bytes(0), _hum_capacity_bytes(0),
tschatzl@5122 4681 _hum_prev_live_bytes(0), _hum_next_live_bytes(0),
johnc@5548 4682 _total_remset_bytes(0), _total_strong_code_roots_bytes(0) {
tonyp@2717 4683 G1CollectedHeap* g1h = G1CollectedHeap::heap();
tonyp@2717 4684 MemRegion g1_reserved = g1h->g1_reserved();
tonyp@2717 4685 double now = os::elapsedTime();
tonyp@2717 4686
tonyp@2717 4687 // Print the header of the output.
tonyp@2717 4688 _out->cr();
tonyp@2717 4689 _out->print_cr(G1PPRL_LINE_PREFIX" PHASE %s @ %1.3f", phase_name, now);
tonyp@2717 4690 _out->print_cr(G1PPRL_LINE_PREFIX" HEAP"
tonyp@2717 4691 G1PPRL_SUM_ADDR_FORMAT("reserved")
tonyp@2717 4692 G1PPRL_SUM_BYTE_FORMAT("region-size"),
drchase@6680 4693 p2i(g1_reserved.start()), p2i(g1_reserved.end()),
johnc@3182 4694 HeapRegion::GrainBytes);
tonyp@2717 4695 _out->print_cr(G1PPRL_LINE_PREFIX);
tonyp@2717 4696 _out->print_cr(G1PPRL_LINE_PREFIX
tschatzl@5122 4697 G1PPRL_TYPE_H_FORMAT
tschatzl@5122 4698 G1PPRL_ADDR_BASE_H_FORMAT
tschatzl@5122 4699 G1PPRL_BYTE_H_FORMAT
tschatzl@5122 4700 G1PPRL_BYTE_H_FORMAT
tschatzl@5122 4701 G1PPRL_BYTE_H_FORMAT
tschatzl@5122 4702 G1PPRL_DOUBLE_H_FORMAT
johnc@5548 4703 G1PPRL_BYTE_H_FORMAT
tschatzl@5122 4704 G1PPRL_BYTE_H_FORMAT,
tschatzl@5122 4705 "type", "address-range",
johnc@5548 4706 "used", "prev-live", "next-live", "gc-eff",
johnc@5548 4707 "remset", "code-roots");
johnc@3173 4708 _out->print_cr(G1PPRL_LINE_PREFIX
tschatzl@5122 4709 G1PPRL_TYPE_H_FORMAT
tschatzl@5122 4710 G1PPRL_ADDR_BASE_H_FORMAT
tschatzl@5122 4711 G1PPRL_BYTE_H_FORMAT
tschatzl@5122 4712 G1PPRL_BYTE_H_FORMAT
tschatzl@5122 4713 G1PPRL_BYTE_H_FORMAT
tschatzl@5122 4714 G1PPRL_DOUBLE_H_FORMAT
johnc@5548 4715 G1PPRL_BYTE_H_FORMAT
tschatzl@5122 4716 G1PPRL_BYTE_H_FORMAT,
tschatzl@5122 4717 "", "",
johnc@5548 4718 "(bytes)", "(bytes)", "(bytes)", "(bytes/ms)",
johnc@5548 4719 "(bytes)", "(bytes)");
tonyp@2717 4720 }
tonyp@2717 4721
tonyp@2717 4722 // It takes as a parameter a reference to one of the _hum_* fields, it
tonyp@2717 4723 // deduces the corresponding value for a region in a humongous region
tonyp@2717 4724 // series (either the region size, or what's left if the _hum_* field
tonyp@2717 4725 // is < the region size), and updates the _hum_* field accordingly.
tonyp@2717 4726 size_t G1PrintRegionLivenessInfoClosure::get_hum_bytes(size_t* hum_bytes) {
tonyp@2717 4727 size_t bytes = 0;
tonyp@2717 4728 // The > 0 check is to deal with the prev and next live bytes which
tonyp@2717 4729 // could be 0.
tonyp@2717 4730 if (*hum_bytes > 0) {
johnc@3182 4731 bytes = MIN2(HeapRegion::GrainBytes, *hum_bytes);
tonyp@2717 4732 *hum_bytes -= bytes;
tonyp@2717 4733 }
tonyp@2717 4734 return bytes;
tonyp@2717 4735 }
tonyp@2717 4736
tonyp@2717 4737 // It deduces the values for a region in a humongous region series
tonyp@2717 4738 // from the _hum_* fields and updates those accordingly. It assumes
tonyp@2717 4739 // that that _hum_* fields have already been set up from the "starts
tonyp@2717 4740 // humongous" region and we visit the regions in address order.
tonyp@2717 4741 void G1PrintRegionLivenessInfoClosure::get_hum_bytes(size_t* used_bytes,
tonyp@2717 4742 size_t* capacity_bytes,
tonyp@2717 4743 size_t* prev_live_bytes,
tonyp@2717 4744 size_t* next_live_bytes) {
tonyp@2717 4745 assert(_hum_used_bytes > 0 && _hum_capacity_bytes > 0, "pre-condition");
tonyp@2717 4746 *used_bytes = get_hum_bytes(&_hum_used_bytes);
tonyp@2717 4747 *capacity_bytes = get_hum_bytes(&_hum_capacity_bytes);
tonyp@2717 4748 *prev_live_bytes = get_hum_bytes(&_hum_prev_live_bytes);
tonyp@2717 4749 *next_live_bytes = get_hum_bytes(&_hum_next_live_bytes);
tonyp@2717 4750 }
tonyp@2717 4751
tonyp@2717 4752 bool G1PrintRegionLivenessInfoClosure::doHeapRegion(HeapRegion* r) {
brutisso@7195 4753 const char* type = r->get_type_str();
tonyp@2717 4754 HeapWord* bottom = r->bottom();
tonyp@2717 4755 HeapWord* end = r->end();
tonyp@2717 4756 size_t capacity_bytes = r->capacity();
tonyp@2717 4757 size_t used_bytes = r->used();
tonyp@2717 4758 size_t prev_live_bytes = r->live_bytes();
tonyp@2717 4759 size_t next_live_bytes = r->next_live_bytes();
tonyp@2717 4760 double gc_eff = r->gc_efficiency();
tschatzl@5122 4761 size_t remset_bytes = r->rem_set()->mem_size();
johnc@5548 4762 size_t strong_code_roots_bytes = r->rem_set()->strong_code_roots_mem_size();
johnc@5548 4763
brutisso@7195 4764 if (r->startsHumongous()) {
tonyp@2717 4765 assert(_hum_used_bytes == 0 && _hum_capacity_bytes == 0 &&
tonyp@2717 4766 _hum_prev_live_bytes == 0 && _hum_next_live_bytes == 0,
tonyp@2717 4767 "they should have been zeroed after the last time we used them");
tonyp@2717 4768 // Set up the _hum_* fields.
tonyp@2717 4769 _hum_capacity_bytes = capacity_bytes;
tonyp@2717 4770 _hum_used_bytes = used_bytes;
tonyp@2717 4771 _hum_prev_live_bytes = prev_live_bytes;
tonyp@2717 4772 _hum_next_live_bytes = next_live_bytes;
tonyp@2717 4773 get_hum_bytes(&used_bytes, &capacity_bytes,
tonyp@2717 4774 &prev_live_bytes, &next_live_bytes);
tonyp@2717 4775 end = bottom + HeapRegion::GrainWords;
tonyp@2717 4776 } else if (r->continuesHumongous()) {
tonyp@2717 4777 get_hum_bytes(&used_bytes, &capacity_bytes,
tonyp@2717 4778 &prev_live_bytes, &next_live_bytes);
tonyp@2717 4779 assert(end == bottom + HeapRegion::GrainWords, "invariant");
tonyp@2717 4780 }
tonyp@2717 4781
tonyp@2717 4782 _total_used_bytes += used_bytes;
tonyp@2717 4783 _total_capacity_bytes += capacity_bytes;
tonyp@2717 4784 _total_prev_live_bytes += prev_live_bytes;
tonyp@2717 4785 _total_next_live_bytes += next_live_bytes;
tschatzl@5122 4786 _total_remset_bytes += remset_bytes;
johnc@5548 4787 _total_strong_code_roots_bytes += strong_code_roots_bytes;
tonyp@2717 4788
tonyp@2717 4789 // Print a line for this particular region.
tonyp@2717 4790 _out->print_cr(G1PPRL_LINE_PREFIX
tonyp@2717 4791 G1PPRL_TYPE_FORMAT
tonyp@2717 4792 G1PPRL_ADDR_BASE_FORMAT
tonyp@2717 4793 G1PPRL_BYTE_FORMAT
tonyp@2717 4794 G1PPRL_BYTE_FORMAT
tonyp@2717 4795 G1PPRL_BYTE_FORMAT
tschatzl@5122 4796 G1PPRL_DOUBLE_FORMAT
johnc@5548 4797 G1PPRL_BYTE_FORMAT
tschatzl@5122 4798 G1PPRL_BYTE_FORMAT,
drchase@6680 4799 type, p2i(bottom), p2i(end),
johnc@5548 4800 used_bytes, prev_live_bytes, next_live_bytes, gc_eff,
johnc@5548 4801 remset_bytes, strong_code_roots_bytes);
tonyp@2717 4802
tonyp@2717 4803 return false;
tonyp@2717 4804 }
tonyp@2717 4805
tonyp@2717 4806 G1PrintRegionLivenessInfoClosure::~G1PrintRegionLivenessInfoClosure() {
tschatzl@5122 4807 // add static memory usages to remembered set sizes
tschatzl@5122 4808 _total_remset_bytes += HeapRegionRemSet::fl_mem_size() + HeapRegionRemSet::static_mem_size();
tonyp@2717 4809 // Print the footer of the output.
tonyp@2717 4810 _out->print_cr(G1PPRL_LINE_PREFIX);
tonyp@2717 4811 _out->print_cr(G1PPRL_LINE_PREFIX
tonyp@2717 4812 " SUMMARY"
tonyp@2717 4813 G1PPRL_SUM_MB_FORMAT("capacity")
tonyp@2717 4814 G1PPRL_SUM_MB_PERC_FORMAT("used")
tonyp@2717 4815 G1PPRL_SUM_MB_PERC_FORMAT("prev-live")
tschatzl@5122 4816 G1PPRL_SUM_MB_PERC_FORMAT("next-live")
johnc@5548 4817 G1PPRL_SUM_MB_FORMAT("remset")
johnc@5548 4818 G1PPRL_SUM_MB_FORMAT("code-roots"),
tonyp@2717 4819 bytes_to_mb(_total_capacity_bytes),
tonyp@2717 4820 bytes_to_mb(_total_used_bytes),
tonyp@2717 4821 perc(_total_used_bytes, _total_capacity_bytes),
tonyp@2717 4822 bytes_to_mb(_total_prev_live_bytes),
tonyp@2717 4823 perc(_total_prev_live_bytes, _total_capacity_bytes),
tonyp@2717 4824 bytes_to_mb(_total_next_live_bytes),
tschatzl@5122 4825 perc(_total_next_live_bytes, _total_capacity_bytes),
johnc@5548 4826 bytes_to_mb(_total_remset_bytes),
johnc@5548 4827 bytes_to_mb(_total_strong_code_roots_bytes));
tonyp@2717 4828 _out->cr();
tonyp@2717 4829 }

mercurial