1 /* 2 * Copyright (c) 2010, 2017, 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/cms/compactibleFreeListSpace.hpp" 27 #include "gc/cms/promotionInfo.hpp" 28 #include "gc/shared/genOopClosures.hpp" 29 #include "oops/compressedOops.inline.hpp" 30 #include "oops/markOop.inline.hpp" 31 #include "oops/oop.hpp" 32 33 ///////////////////////////////////////////////////////////////////////// 34 //// PromotionInfo 35 ///////////////////////////////////////////////////////////////////////// 36 37 38 PromotedObject* PromotedObject::next() const { 39 assert(!((FreeChunk*)this)->is_free(), "Error"); 40 PromotedObject* res; 41 if (UseCompressedOops) { 42 // The next pointer is a compressed oop stored in the top 32 bits 43 res = (PromotedObject*)CompressedOops::decode(_data._narrow_next); 44 } else { 45 res = (PromotedObject*)(_next & next_mask); 46 } 47 assert(oopDesc::is_oop_or_null(oop(res), true /* ignore mark word */), "Expected an oop or NULL at " PTR_FORMAT, p2i(oop(res))); 48 return res; 49 } 50 51 inline void PromotedObject::setNext(PromotedObject* x) { 52 assert(((intptr_t)x & ~next_mask) == 0, "Conflict in bit usage, " 53 "or insufficient alignment of objects"); 54 if (UseCompressedOops) { 55 assert(_data._narrow_next == 0, "Overwrite?"); 56 _data._narrow_next = CompressedOops::encode(oop(x)); 57 } else { 58 _next |= (intptr_t)x; 59 } 60 assert(!((FreeChunk*)this)->is_free(), "Error"); 61 } 62 63 ////////////////////////////////////////////////////////////////////////////// 64 // We go over the list of promoted objects, removing each from the list, 65 // and applying the closure (this may, in turn, add more elements to 66 // the tail of the promoted list, and these newly added objects will 67 // also be processed) until the list is empty. 68 // To aid verification and debugging, in the non-product builds 69 // we actually forward _promoHead each time we process a promoted oop. 70 // Note that this is not necessary in general (i.e. when we don't need to 71 // call PromotionInfo::verify()) because oop_iterate can only add to the 72 // end of _promoTail, and never needs to look at _promoHead. 73 74 #define PROMOTED_OOPS_ITERATE_DEFN(OopClosureType, nv_suffix) \ 75 \ 76 void PromotionInfo::promoted_oops_iterate##nv_suffix(OopClosureType* cl) { \ 77 NOT_PRODUCT(verify()); \ 78 PromotedObject *curObj, *nextObj; \ 79 for (curObj = _promoHead; curObj != NULL; curObj = nextObj) { \ 80 if ((nextObj = curObj->next()) == NULL) { \ 81 /* protect ourselves against additions due to closure application \ 82 below by resetting the list. */ \ 83 assert(_promoTail == curObj, "Should have been the tail"); \ 84 _promoHead = _promoTail = NULL; \ 85 } \ 86 if (curObj->hasDisplacedMark()) { \ 87 /* restore displaced header */ \ 88 oop(curObj)->set_mark(nextDisplacedHeader()); \ 89 } else { \ 90 /* restore prototypical header */ \ 91 oop(curObj)->init_mark(); \ 92 } \ 93 /* The "promoted_mark" should now not be set */ \ 94 assert(!curObj->hasPromotedMark(), \ 95 "Should have been cleared by restoring displaced mark-word"); \ 96 NOT_PRODUCT(_promoHead = nextObj); \ 97 if (cl != NULL) oop(curObj)->oop_iterate(cl); \ 98 if (nextObj == NULL) { /* start at head of list reset above */ \ 99 nextObj = _promoHead; \ 100 } \ 101 } \ 102 assert(noPromotions(), "post-condition violation"); \ 103 assert(_promoHead == NULL && _promoTail == NULL, "emptied promoted list");\ 104 assert(_spoolHead == _spoolTail, "emptied spooling buffers"); \ 105 assert(_firstIndex == _nextIndex, "empty buffer"); \ 106 } 107 108 // This should have been ALL_SINCE_...() just like the others, 109 // but, because the body of the method above is somehwat longer, 110 // the MSVC compiler cannot cope; as a workaround, we split the 111 // macro into its 3 constituent parts below (see original macro 112 // definition in specializedOopClosures.hpp). 113 SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES_YOUNG(PROMOTED_OOPS_ITERATE_DEFN) 114 PROMOTED_OOPS_ITERATE_DEFN(OopsInGenClosure,_v) 115 116 117 // Return the next displaced header, incrementing the pointer and 118 // recycling spool area as necessary. 119 markOop PromotionInfo::nextDisplacedHeader() { 120 assert(_spoolHead != NULL, "promotionInfo inconsistency"); 121 assert(_spoolHead != _spoolTail || _firstIndex < _nextIndex, 122 "Empty spool space: no displaced header can be fetched"); 123 assert(_spoolHead->bufferSize > _firstIndex, "Off by one error at head?"); 124 markOop hdr = _spoolHead->displacedHdr[_firstIndex]; 125 // Spool forward 126 if (++_firstIndex == _spoolHead->bufferSize) { // last location in this block 127 // forward to next block, recycling this block into spare spool buffer 128 SpoolBlock* tmp = _spoolHead->nextSpoolBlock; 129 assert(_spoolHead != _spoolTail, "Spooling storage mix-up"); 130 _spoolHead->nextSpoolBlock = _spareSpool; 131 _spareSpool = _spoolHead; 132 _spoolHead = tmp; 133 _firstIndex = 1; 134 NOT_PRODUCT( 135 if (_spoolHead == NULL) { // all buffers fully consumed 136 assert(_spoolTail == NULL && _nextIndex == 1, 137 "spool buffers processing inconsistency"); 138 } 139 ) 140 } 141 return hdr; 142 } 143 144 void PromotionInfo::track(PromotedObject* trackOop) { 145 track(trackOop, oop(trackOop)->klass()); 146 } 147 148 void PromotionInfo::track(PromotedObject* trackOop, Klass* klassOfOop) { 149 // make a copy of header as it may need to be spooled 150 markOop mark = oop(trackOop)->mark(); 151 trackOop->clear_next(); 152 if (mark->must_be_preserved_for_cms_scavenge(klassOfOop)) { 153 // save non-prototypical header, and mark oop 154 saveDisplacedHeader(mark); 155 trackOop->setDisplacedMark(); 156 } else { 157 // we'd like to assert something like the following: 158 // assert(mark == markOopDesc::prototype(), "consistency check"); 159 // ... but the above won't work because the age bits have not (yet) been 160 // cleared. The remainder of the check would be identical to the 161 // condition checked in must_be_preserved() above, so we don't really 162 // have anything useful to check here! 163 } 164 if (_promoTail != NULL) { 165 assert(_promoHead != NULL, "List consistency"); 166 _promoTail->setNext(trackOop); 167 _promoTail = trackOop; 168 } else { 169 assert(_promoHead == NULL, "List consistency"); 170 _promoHead = _promoTail = trackOop; 171 } 172 // Mask as newly promoted, so we can skip over such objects 173 // when scanning dirty cards 174 assert(!trackOop->hasPromotedMark(), "Should not have been marked"); 175 trackOop->setPromotedMark(); 176 } 177 178 // Save the given displaced header, incrementing the pointer and 179 // obtaining more spool area as necessary. 180 void PromotionInfo::saveDisplacedHeader(markOop hdr) { 181 assert(_spoolHead != NULL && _spoolTail != NULL, 182 "promotionInfo inconsistency"); 183 assert(_spoolTail->bufferSize > _nextIndex, "Off by one error at tail?"); 184 _spoolTail->displacedHdr[_nextIndex] = hdr; 185 // Spool forward 186 if (++_nextIndex == _spoolTail->bufferSize) { // last location in this block 187 // get a new spooling block 188 assert(_spoolTail->nextSpoolBlock == NULL, "tail should terminate spool list"); 189 _splice_point = _spoolTail; // save for splicing 190 _spoolTail->nextSpoolBlock = getSpoolBlock(); // might fail 191 _spoolTail = _spoolTail->nextSpoolBlock; // might become NULL ... 192 // ... but will attempt filling before next promotion attempt 193 _nextIndex = 1; 194 } 195 } 196 197 // Ensure that spooling space exists. Return false if spooling space 198 // could not be obtained. 199 bool PromotionInfo::ensure_spooling_space_work() { 200 assert(!has_spooling_space(), "Only call when there is no spooling space"); 201 // Try and obtain more spooling space 202 SpoolBlock* newSpool = getSpoolBlock(); 203 assert(newSpool == NULL || 204 (newSpool->bufferSize != 0 && newSpool->nextSpoolBlock == NULL), 205 "getSpoolBlock() sanity check"); 206 if (newSpool == NULL) { 207 return false; 208 } 209 _nextIndex = 1; 210 if (_spoolTail == NULL) { 211 _spoolTail = newSpool; 212 if (_spoolHead == NULL) { 213 _spoolHead = newSpool; 214 _firstIndex = 1; 215 } else { 216 assert(_splice_point != NULL && _splice_point->nextSpoolBlock == NULL, 217 "Splice point invariant"); 218 // Extra check that _splice_point is connected to list 219 #ifdef ASSERT 220 { 221 SpoolBlock* blk = _spoolHead; 222 for (; blk->nextSpoolBlock != NULL; 223 blk = blk->nextSpoolBlock); 224 assert(blk != NULL && blk == _splice_point, 225 "Splice point incorrect"); 226 } 227 #endif // ASSERT 228 _splice_point->nextSpoolBlock = newSpool; 229 } 230 } else { 231 assert(_spoolHead != NULL, "spool list consistency"); 232 _spoolTail->nextSpoolBlock = newSpool; 233 _spoolTail = newSpool; 234 } 235 return true; 236 } 237 238 // Get a free spool buffer from the free pool, getting a new block 239 // from the heap if necessary. 240 SpoolBlock* PromotionInfo::getSpoolBlock() { 241 SpoolBlock* res; 242 if ((res = _spareSpool) != NULL) { 243 _spareSpool = _spareSpool->nextSpoolBlock; 244 res->nextSpoolBlock = NULL; 245 } else { // spare spool exhausted, get some from heap 246 res = (SpoolBlock*)(space()->allocateScratch(refillSize())); 247 if (res != NULL) { 248 res->init(); 249 } 250 } 251 assert(res == NULL || res->nextSpoolBlock == NULL, "postcondition"); 252 return res; 253 } 254 255 void PromotionInfo::startTrackingPromotions() { 256 assert(noPromotions(), "sanity"); 257 assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex, 258 "spooling inconsistency?"); 259 _firstIndex = _nextIndex = 1; 260 _tracking = true; 261 } 262 263 void PromotionInfo::stopTrackingPromotions() { 264 assert(noPromotions(), "we should have torn down the lists by now"); 265 assert(_spoolHead == _spoolTail && _firstIndex == _nextIndex, 266 "spooling inconsistency?"); 267 _firstIndex = _nextIndex = 1; 268 _tracking = false; 269 } 270 271 // When _spoolTail is not NULL, then the slot <_spoolTail, _nextIndex> 272 // points to the next slot available for filling. 273 // The set of slots holding displaced headers are then all those in the 274 // right-open interval denoted by: 275 // 276 // [ <_spoolHead, _firstIndex>, <_spoolTail, _nextIndex> ) 277 // 278 // When _spoolTail is NULL, then the set of slots with displaced headers 279 // is all those starting at the slot <_spoolHead, _firstIndex> and 280 // going up to the last slot of last block in the linked list. 281 // In this latter case, _splice_point points to the tail block of 282 // this linked list of blocks holding displaced headers. 283 void PromotionInfo::verify() const { 284 // Verify the following: 285 // 1. the number of displaced headers matches the number of promoted 286 // objects that have displaced headers 287 // 2. each promoted object lies in this space 288 debug_only( 289 PromotedObject* junk = NULL; 290 assert(junk->next_addr() == (void*)(oop(junk)->mark_addr()), 291 "Offset of PromotedObject::_next is expected to align with " 292 " the OopDesc::_mark within OopDesc"); 293 ) 294 // FIXME: guarantee???? 295 guarantee(_spoolHead == NULL || _spoolTail != NULL || 296 _splice_point != NULL, "list consistency"); 297 guarantee(_promoHead == NULL || _promoTail != NULL, "list consistency"); 298 // count the number of objects with displaced headers 299 size_t numObjsWithDisplacedHdrs = 0; 300 for (PromotedObject* curObj = _promoHead; curObj != NULL; curObj = curObj->next()) { 301 guarantee(space()->is_in_reserved((HeapWord*)curObj), "Containment"); 302 // the last promoted object may fail the mark() != NULL test of is_oop(). 303 guarantee(curObj->next() == NULL || oopDesc::is_oop(oop(curObj)), "must be an oop"); 304 if (curObj->hasDisplacedMark()) { 305 numObjsWithDisplacedHdrs++; 306 } 307 } 308 // Count the number of displaced headers 309 size_t numDisplacedHdrs = 0; 310 for (SpoolBlock* curSpool = _spoolHead; 311 curSpool != _spoolTail && curSpool != NULL; 312 curSpool = curSpool->nextSpoolBlock) { 313 // the first entry is just a self-pointer; indices 1 through 314 // bufferSize - 1 are occupied (thus, bufferSize - 1 slots). 315 guarantee((void*)curSpool->displacedHdr == (void*)&curSpool->displacedHdr, 316 "first entry of displacedHdr should be self-referential"); 317 numDisplacedHdrs += curSpool->bufferSize - 1; 318 } 319 guarantee((_spoolHead == _spoolTail) == (numDisplacedHdrs == 0), 320 "internal consistency"); 321 guarantee(_spoolTail != NULL || _nextIndex == 1, 322 "Inconsistency between _spoolTail and _nextIndex"); 323 // We overcounted (_firstIndex-1) worth of slots in block 324 // _spoolHead and we undercounted (_nextIndex-1) worth of 325 // slots in block _spoolTail. We make an appropriate 326 // adjustment by subtracting the first and adding the 327 // second: - (_firstIndex - 1) + (_nextIndex - 1) 328 numDisplacedHdrs += (_nextIndex - _firstIndex); 329 guarantee(numDisplacedHdrs == numObjsWithDisplacedHdrs, "Displaced hdr count"); 330 } 331 332 void PromotionInfo::print_on(outputStream* st) const { 333 SpoolBlock* curSpool = NULL; 334 size_t i = 0; 335 st->print_cr(" start & end indices: [" SIZE_FORMAT ", " SIZE_FORMAT ")", 336 _firstIndex, _nextIndex); 337 for (curSpool = _spoolHead; curSpool != _spoolTail && curSpool != NULL; 338 curSpool = curSpool->nextSpoolBlock) { 339 curSpool->print_on(st); 340 st->print_cr(" active "); 341 i++; 342 } 343 for (curSpool = _spoolTail; curSpool != NULL; 344 curSpool = curSpool->nextSpoolBlock) { 345 curSpool->print_on(st); 346 st->print_cr(" inactive "); 347 i++; 348 } 349 for (curSpool = _spareSpool; curSpool != NULL; 350 curSpool = curSpool->nextSpoolBlock) { 351 curSpool->print_on(st); 352 st->print_cr(" free "); 353 i++; 354 } 355 st->print_cr(" " SIZE_FORMAT " header spooling blocks", i); 356 } 357 358 void SpoolBlock::print_on(outputStream* st) const { 359 st->print("[" PTR_FORMAT "," PTR_FORMAT "), " SIZE_FORMAT " HeapWords -> " PTR_FORMAT, 360 p2i(this), p2i((HeapWord*)displacedHdr + bufferSize), 361 bufferSize, p2i(nextSpoolBlock)); 362 }