1 /* 2 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "gc_implementation/concurrentMarkSweep/compactibleFreeListSpace.hpp" 27 #include "gc_implementation/concurrentMarkSweep/promotionInfo.hpp" 28 #include "oops/markOop.inline.hpp" 29 #include "oops/oop.inline.hpp" 30 31 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 32 33 ///////////////////////////////////////////////////////////////////////// 34 //// PromotionInfo 35 ///////////////////////////////////////////////////////////////////////// 36 37 void PromotionInfo::track(PromotedObject* trackOop) { 38 track(trackOop, oop(trackOop)->klass()); 39 } 40 41 void PromotionInfo::track(PromotedObject* trackOop, Klass* klassOfOop) { 42 // make a copy of header as it may need to be spooled 43 markOop mark = oop(trackOop)->mark(); 44 trackOop->clear_next(); 45 if (mark->must_be_preserved_for_cms_scavenge(klassOfOop)) { 46 // save non-prototypical header, and mark oop 47 saveDisplacedHeader(mark); 48 trackOop->setDisplacedMark(); 49 } else { 50 // we'd like to assert something like the following: 51 // assert(mark == markOopDesc::prototype(), "consistency check"); 52 // ... but the above won't work because the age bits have not (yet) been 53 // cleared. The remainder of the check would be identical to the 54 // condition checked in must_be_preserved() above, so we don't really 55 // have anything useful to check here! 56 } 57 if (_promoTail != NULL) { 58 assert(_promoHead != NULL, "List consistency"); 59 _promoTail->setNext(trackOop); 60 _promoTail = trackOop; 61 } else { 62 assert(_promoHead == NULL, "List consistency"); 63 _promoHead = _promoTail = trackOop; 64 } 65 // Mask as newly promoted, so we can skip over such objects 66 // when scanning dirty cards 67 assert(!trackOop->hasPromotedMark(), "Should not have been marked"); 68 trackOop->setPromotedMark(); 69 } 70 71 // Save the given displaced header, incrementing the pointer and 72 // obtaining more spool area as necessary. 73 void PromotionInfo::saveDisplacedHeader(markOop hdr) { 74 assert(_spoolHead != NULL && _spoolTail != NULL, 75 "promotionInfo inconsistency"); 76 assert(_spoolTail->bufferSize > _nextIndex, "Off by one error at tail?"); 77 _spoolTail->displacedHdr[_nextIndex] = hdr; 78 // Spool forward 79 if (++_nextIndex == _spoolTail->bufferSize) { // last location in this block 80 // get a new spooling block 81 assert(_spoolTail->nextSpoolBlock == NULL, "tail should terminate spool list"); 82 _splice_point = _spoolTail; // save for splicing 83 _spoolTail->nextSpoolBlock = getSpoolBlock(); // might fail 84 _spoolTail = _spoolTail->nextSpoolBlock; // might become NULL ... 85 // ... but will attempt filling before next promotion attempt 86 _nextIndex = 1; 87 } 88 } 89 90 // Ensure that spooling space exists. Return false if spooling space 91 // could not be obtained. 92 bool PromotionInfo::ensure_spooling_space_work() { 93 assert(!has_spooling_space(), "Only call when there is no spooling space"); 94 // Try and obtain more spooling space 95 SpoolBlock* newSpool = getSpoolBlock(); 96 assert(newSpool == NULL || 97 (newSpool->bufferSize != 0 && newSpool->nextSpoolBlock == NULL), 98 "getSpoolBlock() sanity check"); 99 if (newSpool == NULL) { 100 return false; 101 } 102 _nextIndex = 1; 103 if (_spoolTail == NULL) { 104 _spoolTail = newSpool; 105 if (_spoolHead == NULL) { 106 _spoolHead = newSpool; 107 _firstIndex = 1; 108 } else { 109 assert(_splice_point != NULL && _splice_point->nextSpoolBlock == NULL, 110 "Splice point invariant"); 111 // Extra check that _splice_point is connected to list 112 #ifdef ASSERT 113 { 114 SpoolBlock* blk = _spoolHead; 115 for (; blk->nextSpoolBlock != NULL; 116 blk = blk->nextSpoolBlock); 117 assert(blk != NULL && blk == _splice_point, 118 "Splice point incorrect"); 119 } 120 #endif // ASSERT 121 _splice_point->nextSpoolBlock = newSpool; 122 } 123 } else { 124 assert(_spoolHead != NULL, "spool list consistency"); 125 _spoolTail->nextSpoolBlock = newSpool; 126 _spoolTail = newSpool; 127 } 128 return true; 129 } 130 131 // Get a free spool buffer from the free pool, getting a new block 132 // from the heap if necessary. 133 SpoolBlock* PromotionInfo::getSpoolBlock() { 134 SpoolBlock* res; 135 if ((res = _spareSpool) != NULL) { 136 _spareSpool = _spareSpool->nextSpoolBlock; 137 res->nextSpoolBlock = NULL; 138 } else { // spare spool exhausted, get some from heap 139 res = (SpoolBlock*)(space()->allocateScratch(refillSize())); 140 if (res != NULL) { 141 res->init(); 142 } 143 } 144 assert(res == NULL || res->nextSpoolBlock == NULL, "postcondition"); 145 return res; 146 } 147 148 void PromotionInfo::startTrackingPromotions() { 149 assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex, 150 "spooling inconsistency?"); 151 _firstIndex = _nextIndex = 1; 152 _tracking = true; 153 } 154 155 #define CMSPrintPromoBlockInfo 1 156 157 void PromotionInfo::stopTrackingPromotions(uint worker_id) { 158 assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex, 159 "spooling inconsistency?"); 160 _firstIndex = _nextIndex = 1; 161 _tracking = false; 162 if (CMSPrintPromoBlockInfo > 1) { 163 print_statistics(worker_id); 164 } 165 } 166 167 void PromotionInfo::print_statistics(uint worker_id) const { 168 assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex, 169 "Else will undercount"); 170 assert(CMSPrintPromoBlockInfo > 0, "Else unnecessary call"); 171 // Count the number of blocks and slots in the free pool 172 size_t slots = 0; 173 size_t blocks = 0; 174 for (SpoolBlock* cur_spool = _spareSpool; 175 cur_spool != NULL; 176 cur_spool = cur_spool->nextSpoolBlock) { 177 // the first entry is just a self-pointer; indices 1 through 178 // bufferSize - 1 are occupied (thus, bufferSize - 1 slots). 179 assert((void*)cur_spool->displacedHdr == (void*)&cur_spool->displacedHdr, 180 "first entry of displacedHdr should be self-referential"); 181 slots += cur_spool->bufferSize - 1; 182 blocks++; 183 } 184 if (_spoolHead != NULL) { 185 slots += _spoolHead->bufferSize - 1; 186 blocks++; 187 } 188 gclog_or_tty->print_cr(" [worker %d] promo_blocks = " SIZE_FORMAT ", promo_slots = " SIZE_FORMAT, 189 worker_id, blocks, slots); 190 } 191 192 // When _spoolTail is not NULL, then the slot <_spoolTail, _nextIndex> 193 // points to the next slot available for filling. 194 // The set of slots holding displaced headers are then all those in the 195 // right-open interval denoted by: 196 // 197 // [ <_spoolHead, _firstIndex>, <_spoolTail, _nextIndex> ) 198 // 199 // When _spoolTail is NULL, then the set of slots with displaced headers 200 // is all those starting at the slot <_spoolHead, _firstIndex> and 201 // going up to the last slot of last block in the linked list. 202 // In this latter case, _splice_point points to the tail block of 203 // this linked list of blocks holding displaced headers. 204 void PromotionInfo::verify() const { 205 // Verify the following: 206 // 1. the number of displaced headers matches the number of promoted 207 // objects that have displaced headers 208 // 2. each promoted object lies in this space 209 debug_only( 210 PromotedObject* junk = NULL; 211 assert(junk->next_addr() == (void*)(oop(junk)->mark_addr()), 212 "Offset of PromotedObject::_next is expected to align with " 213 " the OopDesc::_mark within OopDesc"); 214 ) 215 // FIXME: guarantee???? 216 guarantee(_spoolHead == NULL || _spoolTail != NULL || 217 _splice_point != NULL, "list consistency"); 218 guarantee(_promoHead == NULL || _promoTail != NULL, "list consistency"); 219 // count the number of objects with displaced headers 220 size_t numObjsWithDisplacedHdrs = 0; 221 for (PromotedObject* curObj = _promoHead; curObj != NULL; curObj = curObj->next()) { 222 guarantee(space()->is_in_reserved((HeapWord*)curObj), "Containment"); 223 // the last promoted object may fail the mark() != NULL test of is_oop(). 224 guarantee(curObj->next() == NULL || oop(curObj)->is_oop(), "must be an oop"); 225 if (curObj->hasDisplacedMark()) { 226 numObjsWithDisplacedHdrs++; 227 } 228 } 229 // Count the number of displaced headers 230 size_t numDisplacedHdrs = 0; 231 for (SpoolBlock* curSpool = _spoolHead; 232 curSpool != _spoolTail && curSpool != NULL; 233 curSpool = curSpool->nextSpoolBlock) { 234 // the first entry is just a self-pointer; indices 1 through 235 // bufferSize - 1 are occupied (thus, bufferSize - 1 slots). 236 guarantee((void*)curSpool->displacedHdr == (void*)&curSpool->displacedHdr, 237 "first entry of displacedHdr should be self-referential"); 238 numDisplacedHdrs += curSpool->bufferSize - 1; 239 } 240 guarantee((_spoolHead == _spoolTail) == (numDisplacedHdrs == 0), 241 "internal consistency"); 242 guarantee(_spoolTail != NULL || _nextIndex == 1, 243 "Inconsistency between _spoolTail and _nextIndex"); 244 // We overcounted (_firstIndex-1) worth of slots in block 245 // _spoolHead and we undercounted (_nextIndex-1) worth of 246 // slots in block _spoolTail. We make an appropriate 247 // adjustment by subtracting the first and adding the 248 // second: - (_firstIndex - 1) + (_nextIndex - 1) 249 numDisplacedHdrs += (_nextIndex - _firstIndex); 250 guarantee(numDisplacedHdrs == numObjsWithDisplacedHdrs, "Displaced hdr count"); 251 } 252 253 void PromotionInfo::print_on(outputStream* st) const { 254 SpoolBlock* curSpool = NULL; 255 size_t i = 0; 256 st->print_cr(" start & end indices: [" SIZE_FORMAT ", " SIZE_FORMAT ")", 257 _firstIndex, _nextIndex); 258 for (curSpool = _spoolHead; curSpool != _spoolTail && curSpool != NULL; 259 curSpool = curSpool->nextSpoolBlock) { 260 curSpool->print_on(st); 261 st->print_cr(" active "); 262 i++; 263 } 264 for (curSpool = _spoolTail; curSpool != NULL; 265 curSpool = curSpool->nextSpoolBlock) { 266 curSpool->print_on(st); 267 st->print_cr(" inactive "); 268 i++; 269 } 270 for (curSpool = _spareSpool; curSpool != NULL; 271 curSpool = curSpool->nextSpoolBlock) { 272 curSpool->print_on(st); 273 st->print_cr(" free "); 274 i++; 275 } 276 st->print_cr(" " SIZE_FORMAT " header spooling blocks", i); 277 } 278 279 void SpoolBlock::print_on(outputStream* st) const { 280 st->print("[" PTR_FORMAT "," PTR_FORMAT "), " SIZE_FORMAT " HeapWords -> " PTR_FORMAT, 281 this, (HeapWord*)displacedHdr + bufferSize, 282 bufferSize, nextSpoolBlock); 283 }