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