Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/concurrentMarkSweep/promotionInfo.cpp
38921 views
/*1* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc_implementation/concurrentMarkSweep/compactibleFreeListSpace.hpp"26#include "gc_implementation/concurrentMarkSweep/promotionInfo.hpp"27#include "oops/markOop.inline.hpp"28#include "oops/oop.inline.hpp"2930PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC3132/////////////////////////////////////////////////////////////////////////33//// PromotionInfo34/////////////////////////////////////////////////////////////////////////353637//////////////////////////////////////////////////////////////////////////////38// We go over the list of promoted objects, removing each from the list,39// and applying the closure (this may, in turn, add more elements to40// the tail of the promoted list, and these newly added objects will41// also be processed) until the list is empty.42// To aid verification and debugging, in the non-product builds43// we actually forward _promoHead each time we process a promoted oop.44// Note that this is not necessary in general (i.e. when we don't need to45// call PromotionInfo::verify()) because oop_iterate can only add to the46// end of _promoTail, and never needs to look at _promoHead.4748#define PROMOTED_OOPS_ITERATE_DEFN(OopClosureType, nv_suffix) \49\50void PromotionInfo::promoted_oops_iterate##nv_suffix(OopClosureType* cl) { \51NOT_PRODUCT(verify()); \52PromotedObject *curObj, *nextObj; \53for (curObj = _promoHead; curObj != NULL; curObj = nextObj) { \54if ((nextObj = curObj->next()) == NULL) { \55/* protect ourselves against additions due to closure application \56below by resetting the list. */ \57assert(_promoTail == curObj, "Should have been the tail"); \58_promoHead = _promoTail = NULL; \59} \60if (curObj->hasDisplacedMark()) { \61/* restore displaced header */ \62oop(curObj)->set_mark(nextDisplacedHeader()); \63} else { \64/* restore prototypical header */ \65oop(curObj)->init_mark(); \66} \67/* The "promoted_mark" should now not be set */ \68assert(!curObj->hasPromotedMark(), \69"Should have been cleared by restoring displaced mark-word"); \70NOT_PRODUCT(_promoHead = nextObj); \71if (cl != NULL) oop(curObj)->oop_iterate(cl); \72if (nextObj == NULL) { /* start at head of list reset above */ \73nextObj = _promoHead; \74} \75} \76assert(noPromotions(), "post-condition violation"); \77assert(_promoHead == NULL && _promoTail == NULL, "emptied promoted list");\78assert(_spoolHead == _spoolTail, "emptied spooling buffers"); \79assert(_firstIndex == _nextIndex, "empty buffer"); \80}8182// This should have been ALL_SINCE_...() just like the others,83// but, because the body of the method above is somehwat longer,84// the MSVC compiler cannot cope; as a workaround, we split the85// macro into its 3 constituent parts below (see original macro86// definition in specializedOopClosures.hpp).87SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG(PROMOTED_OOPS_ITERATE_DEFN)88PROMOTED_OOPS_ITERATE_DEFN(OopsInGenClosure,_v)899091// Return the next displaced header, incrementing the pointer and92// recycling spool area as necessary.93markOop PromotionInfo::nextDisplacedHeader() {94assert(_spoolHead != NULL, "promotionInfo inconsistency");95assert(_spoolHead != _spoolTail || _firstIndex < _nextIndex,96"Empty spool space: no displaced header can be fetched");97assert(_spoolHead->bufferSize > _firstIndex, "Off by one error at head?");98markOop hdr = _spoolHead->displacedHdr[_firstIndex];99// Spool forward100if (++_firstIndex == _spoolHead->bufferSize) { // last location in this block101// forward to next block, recycling this block into spare spool buffer102SpoolBlock* tmp = _spoolHead->nextSpoolBlock;103assert(_spoolHead != _spoolTail, "Spooling storage mix-up");104_spoolHead->nextSpoolBlock = _spareSpool;105_spareSpool = _spoolHead;106_spoolHead = tmp;107_firstIndex = 1;108NOT_PRODUCT(109if (_spoolHead == NULL) { // all buffers fully consumed110assert(_spoolTail == NULL && _nextIndex == 1,111"spool buffers processing inconsistency");112}113)114}115return hdr;116}117118void PromotionInfo::track(PromotedObject* trackOop) {119track(trackOop, oop(trackOop)->klass());120}121122void PromotionInfo::track(PromotedObject* trackOop, Klass* klassOfOop) {123// make a copy of header as it may need to be spooled124markOop mark = oop(trackOop)->mark();125trackOop->clear_next();126if (mark->must_be_preserved_for_cms_scavenge(klassOfOop)) {127// save non-prototypical header, and mark oop128saveDisplacedHeader(mark);129trackOop->setDisplacedMark();130} else {131// we'd like to assert something like the following:132// assert(mark == markOopDesc::prototype(), "consistency check");133// ... but the above won't work because the age bits have not (yet) been134// cleared. The remainder of the check would be identical to the135// condition checked in must_be_preserved() above, so we don't really136// have anything useful to check here!137}138if (_promoTail != NULL) {139assert(_promoHead != NULL, "List consistency");140_promoTail->setNext(trackOop);141_promoTail = trackOop;142} else {143assert(_promoHead == NULL, "List consistency");144_promoHead = _promoTail = trackOop;145}146// Mask as newly promoted, so we can skip over such objects147// when scanning dirty cards148assert(!trackOop->hasPromotedMark(), "Should not have been marked");149trackOop->setPromotedMark();150}151152// Save the given displaced header, incrementing the pointer and153// obtaining more spool area as necessary.154void PromotionInfo::saveDisplacedHeader(markOop hdr) {155assert(_spoolHead != NULL && _spoolTail != NULL,156"promotionInfo inconsistency");157assert(_spoolTail->bufferSize > _nextIndex, "Off by one error at tail?");158_spoolTail->displacedHdr[_nextIndex] = hdr;159// Spool forward160if (++_nextIndex == _spoolTail->bufferSize) { // last location in this block161// get a new spooling block162assert(_spoolTail->nextSpoolBlock == NULL, "tail should terminate spool list");163_splice_point = _spoolTail; // save for splicing164_spoolTail->nextSpoolBlock = getSpoolBlock(); // might fail165_spoolTail = _spoolTail->nextSpoolBlock; // might become NULL ...166// ... but will attempt filling before next promotion attempt167_nextIndex = 1;168}169}170171// Ensure that spooling space exists. Return false if spooling space172// could not be obtained.173bool PromotionInfo::ensure_spooling_space_work() {174assert(!has_spooling_space(), "Only call when there is no spooling space");175// Try and obtain more spooling space176SpoolBlock* newSpool = getSpoolBlock();177assert(newSpool == NULL ||178(newSpool->bufferSize != 0 && newSpool->nextSpoolBlock == NULL),179"getSpoolBlock() sanity check");180if (newSpool == NULL) {181return false;182}183_nextIndex = 1;184if (_spoolTail == NULL) {185_spoolTail = newSpool;186if (_spoolHead == NULL) {187_spoolHead = newSpool;188_firstIndex = 1;189} else {190assert(_splice_point != NULL && _splice_point->nextSpoolBlock == NULL,191"Splice point invariant");192// Extra check that _splice_point is connected to list193#ifdef ASSERT194{195SpoolBlock* blk = _spoolHead;196for (; blk->nextSpoolBlock != NULL;197blk = blk->nextSpoolBlock);198assert(blk != NULL && blk == _splice_point,199"Splice point incorrect");200}201#endif // ASSERT202_splice_point->nextSpoolBlock = newSpool;203}204} else {205assert(_spoolHead != NULL, "spool list consistency");206_spoolTail->nextSpoolBlock = newSpool;207_spoolTail = newSpool;208}209return true;210}211212// Get a free spool buffer from the free pool, getting a new block213// from the heap if necessary.214SpoolBlock* PromotionInfo::getSpoolBlock() {215SpoolBlock* res;216if ((res = _spareSpool) != NULL) {217_spareSpool = _spareSpool->nextSpoolBlock;218res->nextSpoolBlock = NULL;219} else { // spare spool exhausted, get some from heap220res = (SpoolBlock*)(space()->allocateScratch(refillSize()));221if (res != NULL) {222res->init();223}224}225assert(res == NULL || res->nextSpoolBlock == NULL, "postcondition");226return res;227}228229void PromotionInfo::startTrackingPromotions() {230assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex,231"spooling inconsistency?");232_firstIndex = _nextIndex = 1;233_tracking = true;234}235236#define CMSPrintPromoBlockInfo 1237238void PromotionInfo::stopTrackingPromotions(uint worker_id) {239assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex,240"spooling inconsistency?");241_firstIndex = _nextIndex = 1;242_tracking = false;243if (CMSPrintPromoBlockInfo > 1) {244print_statistics(worker_id);245}246}247248void PromotionInfo::print_statistics(uint worker_id) const {249assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex,250"Else will undercount");251assert(CMSPrintPromoBlockInfo > 0, "Else unnecessary call");252// Count the number of blocks and slots in the free pool253size_t slots = 0;254size_t blocks = 0;255for (SpoolBlock* cur_spool = _spareSpool;256cur_spool != NULL;257cur_spool = cur_spool->nextSpoolBlock) {258// the first entry is just a self-pointer; indices 1 through259// bufferSize - 1 are occupied (thus, bufferSize - 1 slots).260assert((void*)cur_spool->displacedHdr == (void*)&cur_spool->displacedHdr,261"first entry of displacedHdr should be self-referential");262slots += cur_spool->bufferSize - 1;263blocks++;264}265if (_spoolHead != NULL) {266slots += _spoolHead->bufferSize - 1;267blocks++;268}269gclog_or_tty->print_cr(" [worker %d] promo_blocks = %d, promo_slots = %d ",270worker_id, blocks, slots);271}272273// When _spoolTail is not NULL, then the slot <_spoolTail, _nextIndex>274// points to the next slot available for filling.275// The set of slots holding displaced headers are then all those in the276// right-open interval denoted by:277//278// [ <_spoolHead, _firstIndex>, <_spoolTail, _nextIndex> )279//280// When _spoolTail is NULL, then the set of slots with displaced headers281// is all those starting at the slot <_spoolHead, _firstIndex> and282// going up to the last slot of last block in the linked list.283// In this lartter case, _splice_point points to the tail block of284// this linked list of blocks holding displaced headers.285void PromotionInfo::verify() const {286// Verify the following:287// 1. the number of displaced headers matches the number of promoted288// objects that have displaced headers289// 2. each promoted object lies in this space290debug_only(291PromotedObject* junk = NULL;292assert(junk->next_addr() == (void*)(oop(junk)->mark_addr()),293"Offset of PromotedObject::_next is expected to align with "294" the OopDesc::_mark within OopDesc");295)296// FIXME: guarantee????297guarantee(_spoolHead == NULL || _spoolTail != NULL ||298_splice_point != NULL, "list consistency");299guarantee(_promoHead == NULL || _promoTail != NULL, "list consistency");300// count the number of objects with displaced headers301size_t numObjsWithDisplacedHdrs = 0;302for (PromotedObject* curObj = _promoHead; curObj != NULL; curObj = curObj->next()) {303guarantee(space()->is_in_reserved((HeapWord*)curObj), "Containment");304// the last promoted object may fail the mark() != NULL test of is_oop().305guarantee(curObj->next() == NULL || oop(curObj)->is_oop(), "must be an oop");306if (curObj->hasDisplacedMark()) {307numObjsWithDisplacedHdrs++;308}309}310// Count the number of displaced headers311size_t numDisplacedHdrs = 0;312for (SpoolBlock* curSpool = _spoolHead;313curSpool != _spoolTail && curSpool != NULL;314curSpool = curSpool->nextSpoolBlock) {315// the first entry is just a self-pointer; indices 1 through316// bufferSize - 1 are occupied (thus, bufferSize - 1 slots).317guarantee((void*)curSpool->displacedHdr == (void*)&curSpool->displacedHdr,318"first entry of displacedHdr should be self-referential");319numDisplacedHdrs += curSpool->bufferSize - 1;320}321guarantee((_spoolHead == _spoolTail) == (numDisplacedHdrs == 0),322"internal consistency");323guarantee(_spoolTail != NULL || _nextIndex == 1,324"Inconsistency between _spoolTail and _nextIndex");325// We overcounted (_firstIndex-1) worth of slots in block326// _spoolHead and we undercounted (_nextIndex-1) worth of327// slots in block _spoolTail. We make an appropriate328// adjustment by subtracting the first and adding the329// second: - (_firstIndex - 1) + (_nextIndex - 1)330numDisplacedHdrs += (_nextIndex - _firstIndex);331guarantee(numDisplacedHdrs == numObjsWithDisplacedHdrs, "Displaced hdr count");332}333334void PromotionInfo::print_on(outputStream* st) const {335SpoolBlock* curSpool = NULL;336size_t i = 0;337st->print_cr(" start & end indices: [" SIZE_FORMAT ", " SIZE_FORMAT ")",338_firstIndex, _nextIndex);339for (curSpool = _spoolHead; curSpool != _spoolTail && curSpool != NULL;340curSpool = curSpool->nextSpoolBlock) {341curSpool->print_on(st);342st->print_cr(" active ");343i++;344}345for (curSpool = _spoolTail; curSpool != NULL;346curSpool = curSpool->nextSpoolBlock) {347curSpool->print_on(st);348st->print_cr(" inactive ");349i++;350}351for (curSpool = _spareSpool; curSpool != NULL;352curSpool = curSpool->nextSpoolBlock) {353curSpool->print_on(st);354st->print_cr(" free ");355i++;356}357st->print_cr(" " SIZE_FORMAT " header spooling blocks", i);358}359360void SpoolBlock::print_on(outputStream* st) const {361st->print("[" PTR_FORMAT "," PTR_FORMAT "), " SIZE_FORMAT " HeapWords -> " PTR_FORMAT,362this, (HeapWord*)displacedHdr + bufferSize,363bufferSize, nextSpoolBlock);364}365366367