ysr@1876: /* coleenp@4037: * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. ysr@1876: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@1876: * ysr@1876: * This code is free software; you can redistribute it and/or modify it ysr@1876: * under the terms of the GNU General Public License version 2 only, as ysr@1876: * published by the Free Software Foundation. ysr@1876: * ysr@1876: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@1876: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@1876: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@1876: * version 2 for more details (a copy is included in the LICENSE file that ysr@1876: * accompanied this code). ysr@1876: * ysr@1876: * You should have received a copy of the GNU General Public License version ysr@1876: * 2 along with this work; if not, write to the Free Software Foundation, ysr@1876: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@1876: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@1876: * ysr@1876: */ ysr@1876: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/concurrentMarkSweep/compactibleFreeListSpace.hpp" stefank@2314: #include "gc_implementation/concurrentMarkSweep/promotionInfo.hpp" stefank@2314: #include "oops/markOop.inline.hpp" stefank@2314: #include "oops/oop.inline.hpp" ysr@1876: ysr@1876: ///////////////////////////////////////////////////////////////////////// ysr@1876: //// PromotionInfo ysr@1876: ///////////////////////////////////////////////////////////////////////// ysr@1876: ysr@1876: ysr@1876: ////////////////////////////////////////////////////////////////////////////// ysr@1876: // We go over the list of promoted objects, removing each from the list, ysr@1876: // and applying the closure (this may, in turn, add more elements to ysr@1876: // the tail of the promoted list, and these newly added objects will ysr@1876: // also be processed) until the list is empty. ysr@1876: // To aid verification and debugging, in the non-product builds ysr@1876: // we actually forward _promoHead each time we process a promoted oop. ysr@1876: // Note that this is not necessary in general (i.e. when we don't need to ysr@1876: // call PromotionInfo::verify()) because oop_iterate can only add to the ysr@1876: // end of _promoTail, and never needs to look at _promoHead. ysr@1876: ysr@1876: #define PROMOTED_OOPS_ITERATE_DEFN(OopClosureType, nv_suffix) \ ysr@1876: \ ysr@1876: void PromotionInfo::promoted_oops_iterate##nv_suffix(OopClosureType* cl) { \ ysr@1876: NOT_PRODUCT(verify()); \ ysr@1876: PromotedObject *curObj, *nextObj; \ ysr@1876: for (curObj = _promoHead; curObj != NULL; curObj = nextObj) { \ ysr@1876: if ((nextObj = curObj->next()) == NULL) { \ ysr@1876: /* protect ourselves against additions due to closure application \ ysr@1876: below by resetting the list. */ \ ysr@1876: assert(_promoTail == curObj, "Should have been the tail"); \ ysr@1876: _promoHead = _promoTail = NULL; \ ysr@1876: } \ ysr@1876: if (curObj->hasDisplacedMark()) { \ ysr@1876: /* restore displaced header */ \ ysr@1876: oop(curObj)->set_mark(nextDisplacedHeader()); \ ysr@1876: } else { \ ysr@1876: /* restore prototypical header */ \ ysr@1876: oop(curObj)->init_mark(); \ ysr@1876: } \ ysr@1876: /* The "promoted_mark" should now not be set */ \ ysr@1876: assert(!curObj->hasPromotedMark(), \ ysr@1876: "Should have been cleared by restoring displaced mark-word"); \ ysr@1876: NOT_PRODUCT(_promoHead = nextObj); \ ysr@1876: if (cl != NULL) oop(curObj)->oop_iterate(cl); \ ysr@1876: if (nextObj == NULL) { /* start at head of list reset above */ \ ysr@1876: nextObj = _promoHead; \ ysr@1876: } \ ysr@1876: } \ ysr@1876: assert(noPromotions(), "post-condition violation"); \ ysr@1876: assert(_promoHead == NULL && _promoTail == NULL, "emptied promoted list");\ ysr@1876: assert(_spoolHead == _spoolTail, "emptied spooling buffers"); \ ysr@1876: assert(_firstIndex == _nextIndex, "empty buffer"); \ ysr@1876: } ysr@1876: ysr@1876: // This should have been ALL_SINCE_...() just like the others, ysr@1876: // but, because the body of the method above is somehwat longer, ysr@1876: // the MSVC compiler cannot cope; as a workaround, we split the ysr@1876: // macro into its 3 constituent parts below (see original macro ysr@1876: // definition in specializedOopClosures.hpp). ysr@1876: SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG(PROMOTED_OOPS_ITERATE_DEFN) ysr@1876: PROMOTED_OOPS_ITERATE_DEFN(OopsInGenClosure,_v) ysr@1876: ysr@1876: ysr@1876: // Return the next displaced header, incrementing the pointer and ysr@1876: // recycling spool area as necessary. ysr@1876: markOop PromotionInfo::nextDisplacedHeader() { ysr@1876: assert(_spoolHead != NULL, "promotionInfo inconsistency"); ysr@1876: assert(_spoolHead != _spoolTail || _firstIndex < _nextIndex, ysr@1876: "Empty spool space: no displaced header can be fetched"); ysr@1876: assert(_spoolHead->bufferSize > _firstIndex, "Off by one error at head?"); ysr@1876: markOop hdr = _spoolHead->displacedHdr[_firstIndex]; ysr@1876: // Spool forward ysr@1876: if (++_firstIndex == _spoolHead->bufferSize) { // last location in this block ysr@1876: // forward to next block, recycling this block into spare spool buffer ysr@1876: SpoolBlock* tmp = _spoolHead->nextSpoolBlock; ysr@1876: assert(_spoolHead != _spoolTail, "Spooling storage mix-up"); ysr@1876: _spoolHead->nextSpoolBlock = _spareSpool; ysr@1876: _spareSpool = _spoolHead; ysr@1876: _spoolHead = tmp; ysr@1876: _firstIndex = 1; ysr@1876: NOT_PRODUCT( ysr@1876: if (_spoolHead == NULL) { // all buffers fully consumed ysr@1876: assert(_spoolTail == NULL && _nextIndex == 1, ysr@1876: "spool buffers processing inconsistency"); ysr@1876: } ysr@1876: ) ysr@1876: } ysr@1876: return hdr; ysr@1876: } ysr@1876: ysr@1876: void PromotionInfo::track(PromotedObject* trackOop) { ysr@1876: track(trackOop, oop(trackOop)->klass()); ysr@1876: } ysr@1876: coleenp@4037: void PromotionInfo::track(PromotedObject* trackOop, Klass* klassOfOop) { ysr@1876: // make a copy of header as it may need to be spooled ysr@1876: markOop mark = oop(trackOop)->mark(); jmasa@3732: trackOop->clear_next(); ysr@1876: if (mark->must_be_preserved_for_cms_scavenge(klassOfOop)) { ysr@1876: // save non-prototypical header, and mark oop ysr@1876: saveDisplacedHeader(mark); ysr@1876: trackOop->setDisplacedMark(); ysr@1876: } else { ysr@1876: // we'd like to assert something like the following: ysr@1876: // assert(mark == markOopDesc::prototype(), "consistency check"); ysr@1876: // ... but the above won't work because the age bits have not (yet) been ysr@1876: // cleared. The remainder of the check would be identical to the ysr@1876: // condition checked in must_be_preserved() above, so we don't really ysr@1876: // have anything useful to check here! ysr@1876: } ysr@1876: if (_promoTail != NULL) { ysr@1876: assert(_promoHead != NULL, "List consistency"); ysr@1876: _promoTail->setNext(trackOop); ysr@1876: _promoTail = trackOop; ysr@1876: } else { ysr@1876: assert(_promoHead == NULL, "List consistency"); ysr@1876: _promoHead = _promoTail = trackOop; ysr@1876: } ysr@1876: // Mask as newly promoted, so we can skip over such objects ysr@1876: // when scanning dirty cards ysr@1876: assert(!trackOop->hasPromotedMark(), "Should not have been marked"); ysr@1876: trackOop->setPromotedMark(); ysr@1876: } ysr@1876: ysr@1876: // Save the given displaced header, incrementing the pointer and ysr@1876: // obtaining more spool area as necessary. ysr@1876: void PromotionInfo::saveDisplacedHeader(markOop hdr) { ysr@1876: assert(_spoolHead != NULL && _spoolTail != NULL, ysr@1876: "promotionInfo inconsistency"); ysr@1876: assert(_spoolTail->bufferSize > _nextIndex, "Off by one error at tail?"); ysr@1876: _spoolTail->displacedHdr[_nextIndex] = hdr; ysr@1876: // Spool forward ysr@1876: if (++_nextIndex == _spoolTail->bufferSize) { // last location in this block ysr@1876: // get a new spooling block ysr@1876: assert(_spoolTail->nextSpoolBlock == NULL, "tail should terminate spool list"); ysr@1876: _splice_point = _spoolTail; // save for splicing ysr@1876: _spoolTail->nextSpoolBlock = getSpoolBlock(); // might fail ysr@1876: _spoolTail = _spoolTail->nextSpoolBlock; // might become NULL ... ysr@1876: // ... but will attempt filling before next promotion attempt ysr@1876: _nextIndex = 1; ysr@1876: } ysr@1876: } ysr@1876: ysr@1876: // Ensure that spooling space exists. Return false if spooling space ysr@1876: // could not be obtained. ysr@1876: bool PromotionInfo::ensure_spooling_space_work() { ysr@1876: assert(!has_spooling_space(), "Only call when there is no spooling space"); ysr@1876: // Try and obtain more spooling space ysr@1876: SpoolBlock* newSpool = getSpoolBlock(); ysr@1876: assert(newSpool == NULL || ysr@1876: (newSpool->bufferSize != 0 && newSpool->nextSpoolBlock == NULL), ysr@1876: "getSpoolBlock() sanity check"); ysr@1876: if (newSpool == NULL) { ysr@1876: return false; ysr@1876: } ysr@1876: _nextIndex = 1; ysr@1876: if (_spoolTail == NULL) { ysr@1876: _spoolTail = newSpool; ysr@1876: if (_spoolHead == NULL) { ysr@1876: _spoolHead = newSpool; ysr@1876: _firstIndex = 1; ysr@1876: } else { ysr@1876: assert(_splice_point != NULL && _splice_point->nextSpoolBlock == NULL, ysr@1876: "Splice point invariant"); ysr@1876: // Extra check that _splice_point is connected to list ysr@1876: #ifdef ASSERT ysr@1876: { ysr@1876: SpoolBlock* blk = _spoolHead; ysr@1876: for (; blk->nextSpoolBlock != NULL; ysr@1876: blk = blk->nextSpoolBlock); ysr@1876: assert(blk != NULL && blk == _splice_point, ysr@1876: "Splice point incorrect"); ysr@1876: } ysr@1876: #endif // ASSERT ysr@1876: _splice_point->nextSpoolBlock = newSpool; ysr@1876: } ysr@1876: } else { ysr@1876: assert(_spoolHead != NULL, "spool list consistency"); ysr@1876: _spoolTail->nextSpoolBlock = newSpool; ysr@1876: _spoolTail = newSpool; ysr@1876: } ysr@1876: return true; ysr@1876: } ysr@1876: ysr@1876: // Get a free spool buffer from the free pool, getting a new block ysr@1876: // from the heap if necessary. ysr@1876: SpoolBlock* PromotionInfo::getSpoolBlock() { ysr@1876: SpoolBlock* res; ysr@1876: if ((res = _spareSpool) != NULL) { ysr@1876: _spareSpool = _spareSpool->nextSpoolBlock; ysr@1876: res->nextSpoolBlock = NULL; ysr@1876: } else { // spare spool exhausted, get some from heap ysr@1876: res = (SpoolBlock*)(space()->allocateScratch(refillSize())); ysr@1876: if (res != NULL) { ysr@1876: res->init(); ysr@1876: } ysr@1876: } ysr@1876: assert(res == NULL || res->nextSpoolBlock == NULL, "postcondition"); ysr@1876: return res; ysr@1876: } ysr@1876: ysr@1876: void PromotionInfo::startTrackingPromotions() { ysr@1876: assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex, ysr@1876: "spooling inconsistency?"); ysr@1876: _firstIndex = _nextIndex = 1; ysr@1876: _tracking = true; ysr@1876: } ysr@1876: ysr@1876: #define CMSPrintPromoBlockInfo 1 ysr@1876: ysr@1876: void PromotionInfo::stopTrackingPromotions(uint worker_id) { ysr@1876: assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex, ysr@1876: "spooling inconsistency?"); ysr@1876: _firstIndex = _nextIndex = 1; ysr@1876: _tracking = false; ysr@1876: if (CMSPrintPromoBlockInfo > 1) { ysr@1876: print_statistics(worker_id); ysr@1876: } ysr@1876: } ysr@1876: ysr@1876: void PromotionInfo::print_statistics(uint worker_id) const { ysr@1876: assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex, ysr@1876: "Else will undercount"); ysr@1876: assert(CMSPrintPromoBlockInfo > 0, "Else unnecessary call"); ysr@1876: // Count the number of blocks and slots in the free pool ysr@1876: size_t slots = 0; ysr@1876: size_t blocks = 0; ysr@1876: for (SpoolBlock* cur_spool = _spareSpool; ysr@1876: cur_spool != NULL; ysr@1876: cur_spool = cur_spool->nextSpoolBlock) { ysr@1876: // the first entry is just a self-pointer; indices 1 through ysr@1876: // bufferSize - 1 are occupied (thus, bufferSize - 1 slots). ysr@2132: assert((void*)cur_spool->displacedHdr == (void*)&cur_spool->displacedHdr, ysr@2132: "first entry of displacedHdr should be self-referential"); ysr@1876: slots += cur_spool->bufferSize - 1; ysr@1876: blocks++; ysr@1876: } ysr@1876: if (_spoolHead != NULL) { ysr@1876: slots += _spoolHead->bufferSize - 1; ysr@1876: blocks++; ysr@1876: } ysr@1876: gclog_or_tty->print_cr(" [worker %d] promo_blocks = %d, promo_slots = %d ", ysr@1876: worker_id, blocks, slots); ysr@1876: } ysr@1876: ysr@1876: // When _spoolTail is not NULL, then the slot <_spoolTail, _nextIndex> ysr@1876: // points to the next slot available for filling. ysr@1876: // The set of slots holding displaced headers are then all those in the ysr@1876: // right-open interval denoted by: ysr@1876: // ysr@1876: // [ <_spoolHead, _firstIndex>, <_spoolTail, _nextIndex> ) ysr@1876: // ysr@1876: // When _spoolTail is NULL, then the set of slots with displaced headers ysr@1876: // is all those starting at the slot <_spoolHead, _firstIndex> and ysr@1876: // going up to the last slot of last block in the linked list. ysr@1876: // In this lartter case, _splice_point points to the tail block of ysr@1876: // this linked list of blocks holding displaced headers. ysr@1876: void PromotionInfo::verify() const { ysr@1876: // Verify the following: ysr@1876: // 1. the number of displaced headers matches the number of promoted ysr@1876: // objects that have displaced headers ysr@1876: // 2. each promoted object lies in this space ysr@1876: debug_only( ysr@1876: PromotedObject* junk = NULL; ysr@1876: assert(junk->next_addr() == (void*)(oop(junk)->mark_addr()), ysr@1876: "Offset of PromotedObject::_next is expected to align with " ysr@1876: " the OopDesc::_mark within OopDesc"); ysr@1876: ) ysr@1876: // FIXME: guarantee???? ysr@1876: guarantee(_spoolHead == NULL || _spoolTail != NULL || ysr@1876: _splice_point != NULL, "list consistency"); ysr@1876: guarantee(_promoHead == NULL || _promoTail != NULL, "list consistency"); ysr@1876: // count the number of objects with displaced headers ysr@1876: size_t numObjsWithDisplacedHdrs = 0; ysr@1876: for (PromotedObject* curObj = _promoHead; curObj != NULL; curObj = curObj->next()) { ysr@1876: guarantee(space()->is_in_reserved((HeapWord*)curObj), "Containment"); ysr@1876: // the last promoted object may fail the mark() != NULL test of is_oop(). ysr@1876: guarantee(curObj->next() == NULL || oop(curObj)->is_oop(), "must be an oop"); ysr@1876: if (curObj->hasDisplacedMark()) { ysr@1876: numObjsWithDisplacedHdrs++; ysr@1876: } ysr@1876: } ysr@1876: // Count the number of displaced headers ysr@1876: size_t numDisplacedHdrs = 0; ysr@1876: for (SpoolBlock* curSpool = _spoolHead; ysr@1876: curSpool != _spoolTail && curSpool != NULL; ysr@1876: curSpool = curSpool->nextSpoolBlock) { ysr@1876: // the first entry is just a self-pointer; indices 1 through ysr@1876: // bufferSize - 1 are occupied (thus, bufferSize - 1 slots). ysr@1876: guarantee((void*)curSpool->displacedHdr == (void*)&curSpool->displacedHdr, ysr@1876: "first entry of displacedHdr should be self-referential"); ysr@1876: numDisplacedHdrs += curSpool->bufferSize - 1; ysr@1876: } ysr@1876: guarantee((_spoolHead == _spoolTail) == (numDisplacedHdrs == 0), ysr@1876: "internal consistency"); ysr@1876: guarantee(_spoolTail != NULL || _nextIndex == 1, ysr@1876: "Inconsistency between _spoolTail and _nextIndex"); ysr@1876: // We overcounted (_firstIndex-1) worth of slots in block ysr@1876: // _spoolHead and we undercounted (_nextIndex-1) worth of ysr@1876: // slots in block _spoolTail. We make an appropriate ysr@1876: // adjustment by subtracting the first and adding the ysr@1876: // second: - (_firstIndex - 1) + (_nextIndex - 1) ysr@1876: numDisplacedHdrs += (_nextIndex - _firstIndex); ysr@1876: guarantee(numDisplacedHdrs == numObjsWithDisplacedHdrs, "Displaced hdr count"); ysr@1876: } ysr@1876: ysr@1876: void PromotionInfo::print_on(outputStream* st) const { ysr@1876: SpoolBlock* curSpool = NULL; ysr@1876: size_t i = 0; ysr@2071: st->print_cr(" start & end indices: [" SIZE_FORMAT ", " SIZE_FORMAT ")", ysr@1876: _firstIndex, _nextIndex); ysr@1876: for (curSpool = _spoolHead; curSpool != _spoolTail && curSpool != NULL; ysr@1876: curSpool = curSpool->nextSpoolBlock) { ysr@1876: curSpool->print_on(st); ysr@1876: st->print_cr(" active "); ysr@1876: i++; ysr@1876: } ysr@1876: for (curSpool = _spoolTail; curSpool != NULL; ysr@1876: curSpool = curSpool->nextSpoolBlock) { ysr@1876: curSpool->print_on(st); ysr@1876: st->print_cr(" inactive "); ysr@1876: i++; ysr@1876: } ysr@1876: for (curSpool = _spareSpool; curSpool != NULL; ysr@1876: curSpool = curSpool->nextSpoolBlock) { ysr@1876: curSpool->print_on(st); ysr@1876: st->print_cr(" free "); ysr@1876: i++; ysr@1876: } ysr@2071: st->print_cr(" " SIZE_FORMAT " header spooling blocks", i); ysr@1876: } ysr@1876: ysr@1876: void SpoolBlock::print_on(outputStream* st) const { ysr@1876: st->print("[" PTR_FORMAT "," PTR_FORMAT "), " SIZE_FORMAT " HeapWords -> " PTR_FORMAT, ysr@1876: this, (HeapWord*)displacedHdr + bufferSize, ysr@1876: bufferSize, nextSpoolBlock); ysr@1876: }