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