1 /*
   2  * Copyright (c) 2001, 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/cmsLockVerifier.hpp"
  27 #include "gc_implementation/concurrentMarkSweep/compactibleFreeListSpace.hpp"
  28 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"
  29 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
  30 #include "gc_implementation/shared/liveRange.hpp"
  31 #include "gc_implementation/shared/spaceDecorator.hpp"
  32 #include "gc_interface/collectedHeap.inline.hpp"
  33 #include "memory/allocation.inline.hpp"
  34 #include "memory/blockOffsetTable.inline.hpp"
  35 #include "memory/resourceArea.hpp"
  36 #include "memory/space.inline.hpp"
  37 #include "memory/universe.inline.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "runtime/globals.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 #include "runtime/init.hpp"
  42 #include "runtime/java.hpp"
  43 #include "runtime/orderAccess.inline.hpp"
  44 #include "runtime/vmThread.hpp"
  45 #include "utilities/copy.hpp"
  46 
  47 /////////////////////////////////////////////////////////////////////////
  48 //// CompactibleFreeListSpace
  49 /////////////////////////////////////////////////////////////////////////
  50 
  51 // highest ranked  free list lock rank
  52 int CompactibleFreeListSpace::_lockRank = Mutex::leaf + 3;
  53 
  54 // Defaults are 0 so things will break badly if incorrectly initialized.
  55 size_t CompactibleFreeListSpace::IndexSetStart  = 0;
  56 size_t CompactibleFreeListSpace::IndexSetStride = 0;
  57 
  58 size_t MinChunkSize = 0;
  59 
  60 void CompactibleFreeListSpace::set_cms_values() {
  61   // Set CMS global values
  62   assert(MinChunkSize == 0, "already set");
  63 
  64   // MinChunkSize should be a multiple of MinObjAlignment and be large enough
  65   // for chunks to contain a FreeChunk.
  66   size_t min_chunk_size_in_bytes = align_size_up(sizeof(FreeChunk), MinObjAlignmentInBytes);
  67   MinChunkSize = min_chunk_size_in_bytes / BytesPerWord;
  68 
  69   assert(IndexSetStart == 0 && IndexSetStride == 0, "already set");
  70   IndexSetStart  = MinChunkSize;
  71   IndexSetStride = MinObjAlignment;
  72 }
  73 
  74 // Constructor
  75 CompactibleFreeListSpace::CompactibleFreeListSpace(BlockOffsetSharedArray* bs,
  76   MemRegion mr, bool use_adaptive_freelists,
  77   FreeBlockDictionary<FreeChunk>::DictionaryChoice dictionaryChoice) :
  78   _dictionaryChoice(dictionaryChoice),
  79   _adaptive_freelists(use_adaptive_freelists),
  80   _bt(bs, mr),
  81   // free list locks are in the range of values taken by _lockRank
  82   // This range currently is [_leaf+2, _leaf+3]
  83   // Note: this requires that CFLspace c'tors
  84   // are called serially in the order in which the locks are
  85   // are acquired in the program text. This is true today.
  86   _freelistLock(_lockRank--, "CompactibleFreeListSpace._lock", true,
  87                 Monitor::_safepoint_check_sometimes),
  88   _parDictionaryAllocLock(Mutex::leaf - 1,  // == rank(ExpandHeap_lock) - 1
  89                           "CompactibleFreeListSpace._dict_par_lock", true,
  90                           Monitor::_safepoint_check_never),
  91   _rescan_task_size(CardTableModRefBS::card_size_in_words * BitsPerWord *
  92                     CMSRescanMultiple),
  93   _marking_task_size(CardTableModRefBS::card_size_in_words * BitsPerWord *
  94                     CMSConcMarkMultiple),
  95   _collector(NULL)
  96 {
  97   assert(sizeof(FreeChunk) / BytesPerWord <= MinChunkSize,
  98          "FreeChunk is larger than expected");
  99   _bt.set_space(this);
 100   initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle);
 101   // We have all of "mr", all of which we place in the dictionary
 102   // as one big chunk. We'll need to decide here which of several
 103   // possible alternative dictionary implementations to use. For
 104   // now the choice is easy, since we have only one working
 105   // implementation, namely, the simple binary tree (splaying
 106   // temporarily disabled).
 107   switch (dictionaryChoice) {
 108     case FreeBlockDictionary<FreeChunk>::dictionaryBinaryTree:
 109       _dictionary = new AFLBinaryTreeDictionary(mr);
 110       break;
 111     case FreeBlockDictionary<FreeChunk>::dictionarySplayTree:
 112     case FreeBlockDictionary<FreeChunk>::dictionarySkipList:
 113     default:
 114       warning("dictionaryChoice: selected option not understood; using"
 115               " default BinaryTreeDictionary implementation instead.");
 116   }
 117   assert(_dictionary != NULL, "CMS dictionary initialization");
 118   // The indexed free lists are initially all empty and are lazily
 119   // filled in on demand. Initialize the array elements to NULL.
 120   initializeIndexedFreeListArray();
 121 
 122   // Not using adaptive free lists assumes that allocation is first
 123   // from the linAB's.  Also a cms perm gen which can be compacted
 124   // has to have the klass's klassKlass allocated at a lower
 125   // address in the heap than the klass so that the klassKlass is
 126   // moved to its new location before the klass is moved.
 127   // Set the _refillSize for the linear allocation blocks
 128   if (!use_adaptive_freelists) {
 129     FreeChunk* fc = _dictionary->get_chunk(mr.word_size(),
 130                                            FreeBlockDictionary<FreeChunk>::atLeast);
 131     // The small linAB initially has all the space and will allocate
 132     // a chunk of any size.
 133     HeapWord* addr = (HeapWord*) fc;
 134     _smallLinearAllocBlock.set(addr, fc->size() ,
 135       1024*SmallForLinearAlloc, fc->size());
 136     // Note that _unallocated_block is not updated here.
 137     // Allocations from the linear allocation block should
 138     // update it.
 139   } else {
 140     _smallLinearAllocBlock.set(0, 0, 1024*SmallForLinearAlloc,
 141                                SmallForLinearAlloc);
 142   }
 143   // CMSIndexedFreeListReplenish should be at least 1
 144   CMSIndexedFreeListReplenish = MAX2((uintx)1, CMSIndexedFreeListReplenish);
 145   _promoInfo.setSpace(this);
 146   if (UseCMSBestFit) {
 147     _fitStrategy = FreeBlockBestFitFirst;
 148   } else {
 149     _fitStrategy = FreeBlockStrategyNone;
 150   }
 151   check_free_list_consistency();
 152 
 153   // Initialize locks for parallel case.
 154 
 155   if (CollectedHeap::use_parallel_gc_threads()) {
 156     for (size_t i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
 157       _indexedFreeListParLocks[i] = new Mutex(Mutex::leaf - 1, // == ExpandHeap_lock - 1
 158                                               "freelist par lock", true, Mutex::_safepoint_check_sometimes);
 159       DEBUG_ONLY(
 160         _indexedFreeList[i].set_protecting_lock(_indexedFreeListParLocks[i]);
 161       )
 162     }
 163     _dictionary->set_par_lock(&_parDictionaryAllocLock);
 164   }
 165 }
 166 
 167 // Like CompactibleSpace forward() but always calls cross_threshold() to
 168 // update the block offset table.  Removed initialize_threshold call because
 169 // CFLS does not use a block offset array for contiguous spaces.
 170 HeapWord* CompactibleFreeListSpace::forward(oop q, size_t size,
 171                                     CompactPoint* cp, HeapWord* compact_top) {
 172   // q is alive
 173   // First check if we should switch compaction space
 174   assert(this == cp->space, "'this' should be current compaction space.");
 175   size_t compaction_max_size = pointer_delta(end(), compact_top);
 176   assert(adjustObjectSize(size) == cp->space->adjust_object_size_v(size),
 177     "virtual adjustObjectSize_v() method is not correct");
 178   size_t adjusted_size = adjustObjectSize(size);
 179   assert(compaction_max_size >= MinChunkSize || compaction_max_size == 0,
 180          "no small fragments allowed");
 181   assert(minimum_free_block_size() == MinChunkSize,
 182          "for de-virtualized reference below");
 183   // Can't leave a nonzero size, residual fragment smaller than MinChunkSize
 184   if (adjusted_size + MinChunkSize > compaction_max_size &&
 185       adjusted_size != compaction_max_size) {
 186     do {
 187       // switch to next compaction space
 188       cp->space->set_compaction_top(compact_top);
 189       cp->space = cp->space->next_compaction_space();
 190       if (cp->space == NULL) {
 191         cp->gen = GenCollectedHeap::heap()->prev_gen(cp->gen);
 192         assert(cp->gen != NULL, "compaction must succeed");
 193         cp->space = cp->gen->first_compaction_space();
 194         assert(cp->space != NULL, "generation must have a first compaction space");
 195       }
 196       compact_top = cp->space->bottom();
 197       cp->space->set_compaction_top(compact_top);
 198       // The correct adjusted_size may not be the same as that for this method
 199       // (i.e., cp->space may no longer be "this" so adjust the size again.
 200       // Use the virtual method which is not used above to save the virtual
 201       // dispatch.
 202       adjusted_size = cp->space->adjust_object_size_v(size);
 203       compaction_max_size = pointer_delta(cp->space->end(), compact_top);
 204       assert(cp->space->minimum_free_block_size() == 0, "just checking");
 205     } while (adjusted_size > compaction_max_size);
 206   }
 207 
 208   // store the forwarding pointer into the mark word
 209   if ((HeapWord*)q != compact_top) {
 210     q->forward_to(oop(compact_top));
 211     assert(q->is_gc_marked(), "encoding the pointer should preserve the mark");
 212   } else {
 213     // if the object isn't moving we can just set the mark to the default
 214     // mark and handle it specially later on.
 215     q->init_mark();
 216     assert(q->forwardee() == NULL, "should be forwarded to NULL");
 217   }
 218 
 219   compact_top += adjusted_size;
 220 
 221   // we need to update the offset table so that the beginnings of objects can be
 222   // found during scavenge.  Note that we are updating the offset table based on
 223   // where the object will be once the compaction phase finishes.
 224 
 225   // Always call cross_threshold().  A contiguous space can only call it when
 226   // the compaction_top exceeds the current threshold but not for an
 227   // non-contiguous space.
 228   cp->threshold =
 229     cp->space->cross_threshold(compact_top - adjusted_size, compact_top);
 230   return compact_top;
 231 }
 232 
 233 // A modified copy of OffsetTableContigSpace::cross_threshold() with _offsets -> _bt
 234 // and use of single_block instead of alloc_block.  The name here is not really
 235 // appropriate - maybe a more general name could be invented for both the
 236 // contiguous and noncontiguous spaces.
 237 
 238 HeapWord* CompactibleFreeListSpace::cross_threshold(HeapWord* start, HeapWord* the_end) {
 239   _bt.single_block(start, the_end);
 240   return end();
 241 }
 242 
 243 // Initialize them to NULL.
 244 void CompactibleFreeListSpace::initializeIndexedFreeListArray() {
 245   for (size_t i = 0; i < IndexSetSize; i++) {
 246     // Note that on platforms where objects are double word aligned,
 247     // the odd array elements are not used.  It is convenient, however,
 248     // to map directly from the object size to the array element.
 249     _indexedFreeList[i].reset(IndexSetSize);
 250     _indexedFreeList[i].set_size(i);
 251     assert(_indexedFreeList[i].count() == 0, "reset check failed");
 252     assert(_indexedFreeList[i].head() == NULL, "reset check failed");
 253     assert(_indexedFreeList[i].tail() == NULL, "reset check failed");
 254     assert(_indexedFreeList[i].hint() == IndexSetSize, "reset check failed");
 255   }
 256 }
 257 
 258 void CompactibleFreeListSpace::resetIndexedFreeListArray() {
 259   for (size_t i = 1; i < IndexSetSize; i++) {
 260     assert(_indexedFreeList[i].size() == (size_t) i,
 261       "Indexed free list sizes are incorrect");
 262     _indexedFreeList[i].reset(IndexSetSize);
 263     assert(_indexedFreeList[i].count() == 0, "reset check failed");
 264     assert(_indexedFreeList[i].head() == NULL, "reset check failed");
 265     assert(_indexedFreeList[i].tail() == NULL, "reset check failed");
 266     assert(_indexedFreeList[i].hint() == IndexSetSize, "reset check failed");
 267   }
 268 }
 269 
 270 void CompactibleFreeListSpace::reset(MemRegion mr) {
 271   resetIndexedFreeListArray();
 272   dictionary()->reset();
 273   if (BlockOffsetArrayUseUnallocatedBlock) {
 274     assert(end() == mr.end(), "We are compacting to the bottom of CMS gen");
 275     // Everything's allocated until proven otherwise.
 276     _bt.set_unallocated_block(end());
 277   }
 278   if (!mr.is_empty()) {
 279     assert(mr.word_size() >= MinChunkSize, "Chunk size is too small");
 280     _bt.single_block(mr.start(), mr.word_size());
 281     FreeChunk* fc = (FreeChunk*) mr.start();
 282     fc->set_size(mr.word_size());
 283     if (mr.word_size() >= IndexSetSize ) {
 284       returnChunkToDictionary(fc);
 285     } else {
 286       _bt.verify_not_unallocated((HeapWord*)fc, fc->size());
 287       _indexedFreeList[mr.word_size()].return_chunk_at_head(fc);
 288     }
 289     coalBirth(mr.word_size());
 290   }
 291   _promoInfo.reset();
 292   _smallLinearAllocBlock._ptr = NULL;
 293   _smallLinearAllocBlock._word_size = 0;
 294 }
 295 
 296 void CompactibleFreeListSpace::reset_after_compaction() {
 297   // Reset the space to the new reality - one free chunk.
 298   MemRegion mr(compaction_top(), end());
 299   reset(mr);
 300   // Now refill the linear allocation block(s) if possible.
 301   if (_adaptive_freelists) {
 302     refillLinearAllocBlocksIfNeeded();
 303   } else {
 304     // Place as much of mr in the linAB as we can get,
 305     // provided it was big enough to go into the dictionary.
 306     FreeChunk* fc = dictionary()->find_largest_dict();
 307     if (fc != NULL) {
 308       assert(fc->size() == mr.word_size(),
 309              "Why was the chunk broken up?");
 310       removeChunkFromDictionary(fc);
 311       HeapWord* addr = (HeapWord*) fc;
 312       _smallLinearAllocBlock.set(addr, fc->size() ,
 313         1024*SmallForLinearAlloc, fc->size());
 314       // Note that _unallocated_block is not updated here.
 315     }
 316   }
 317 }
 318 
 319 // Walks the entire dictionary, returning a coterminal
 320 // chunk, if it exists. Use with caution since it involves
 321 // a potentially complete walk of a potentially large tree.
 322 FreeChunk* CompactibleFreeListSpace::find_chunk_at_end() {
 323 
 324   assert_lock_strong(&_freelistLock);
 325 
 326   return dictionary()->find_chunk_ends_at(end());
 327 }
 328 
 329 
 330 #ifndef PRODUCT
 331 void CompactibleFreeListSpace::initializeIndexedFreeListArrayReturnedBytes() {
 332   for (size_t i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
 333     _indexedFreeList[i].allocation_stats()->set_returned_bytes(0);
 334   }
 335 }
 336 
 337 size_t CompactibleFreeListSpace::sumIndexedFreeListArrayReturnedBytes() {
 338   size_t sum = 0;
 339   for (size_t i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
 340     sum += _indexedFreeList[i].allocation_stats()->returned_bytes();
 341   }
 342   return sum;
 343 }
 344 
 345 size_t CompactibleFreeListSpace::totalCountInIndexedFreeLists() const {
 346   size_t count = 0;
 347   for (size_t i = IndexSetStart; i < IndexSetSize; i++) {
 348     debug_only(
 349       ssize_t total_list_count = 0;
 350       for (FreeChunk* fc = _indexedFreeList[i].head(); fc != NULL;
 351          fc = fc->next()) {
 352         total_list_count++;
 353       }
 354       assert(total_list_count ==  _indexedFreeList[i].count(),
 355         "Count in list is incorrect");
 356     )
 357     count += _indexedFreeList[i].count();
 358   }
 359   return count;
 360 }
 361 
 362 size_t CompactibleFreeListSpace::totalCount() {
 363   size_t num = totalCountInIndexedFreeLists();
 364   num +=  dictionary()->total_count();
 365   if (_smallLinearAllocBlock._word_size != 0) {
 366     num++;
 367   }
 368   return num;
 369 }
 370 #endif
 371 
 372 bool CompactibleFreeListSpace::is_free_block(const HeapWord* p) const {
 373   FreeChunk* fc = (FreeChunk*) p;
 374   return fc->is_free();
 375 }
 376 
 377 size_t CompactibleFreeListSpace::used() const {
 378   return capacity() - free();
 379 }
 380 
 381 size_t CompactibleFreeListSpace::free() const {
 382   // "MT-safe, but not MT-precise"(TM), if you will: i.e.
 383   // if you do this while the structures are in flux you
 384   // may get an approximate answer only; for instance
 385   // because there is concurrent allocation either
 386   // directly by mutators or for promotion during a GC.
 387   // It's "MT-safe", however, in the sense that you are guaranteed
 388   // not to crash and burn, for instance, because of walking
 389   // pointers that could disappear as you were walking them.
 390   // The approximation is because the various components
 391   // that are read below are not read atomically (and
 392   // further the computation of totalSizeInIndexedFreeLists()
 393   // is itself a non-atomic computation. The normal use of
 394   // this is during a resize operation at the end of GC
 395   // and at that time you are guaranteed to get the
 396   // correct actual value. However, for instance, this is
 397   // also read completely asynchronously by the "perf-sampler"
 398   // that supports jvmstat, and you are apt to see the values
 399   // flicker in such cases.
 400   assert(_dictionary != NULL, "No _dictionary?");
 401   return (_dictionary->total_chunk_size(DEBUG_ONLY(freelistLock())) +
 402           totalSizeInIndexedFreeLists() +
 403           _smallLinearAllocBlock._word_size) * HeapWordSize;
 404 }
 405 
 406 size_t CompactibleFreeListSpace::max_alloc_in_words() const {
 407   assert(_dictionary != NULL, "No _dictionary?");
 408   assert_locked();
 409   size_t res = _dictionary->max_chunk_size();
 410   res = MAX2(res, MIN2(_smallLinearAllocBlock._word_size,
 411                        (size_t) SmallForLinearAlloc - 1));
 412   // XXX the following could potentially be pretty slow;
 413   // should one, pessimistically for the rare cases when res
 414   // calculated above is less than IndexSetSize,
 415   // just return res calculated above? My reasoning was that
 416   // those cases will be so rare that the extra time spent doesn't
 417   // really matter....
 418   // Note: do not change the loop test i >= res + IndexSetStride
 419   // to i > res below, because i is unsigned and res may be zero.
 420   for (size_t i = IndexSetSize - 1; i >= res + IndexSetStride;
 421        i -= IndexSetStride) {
 422     if (_indexedFreeList[i].head() != NULL) {
 423       assert(_indexedFreeList[i].count() != 0, "Inconsistent FreeList");
 424       return i;
 425     }
 426   }
 427   return res;
 428 }
 429 
 430 void LinearAllocBlock::print_on(outputStream* st) const {
 431   st->print_cr(" LinearAllocBlock: ptr = " PTR_FORMAT ", word_size = " SIZE_FORMAT
 432             ", refillsize = " SIZE_FORMAT ", allocation_size_limit = " SIZE_FORMAT,
 433             p2i(_ptr), _word_size, _refillSize, _allocation_size_limit);
 434 }
 435 
 436 void CompactibleFreeListSpace::print_on(outputStream* st) const {
 437   st->print_cr("COMPACTIBLE FREELIST SPACE");
 438   st->print_cr(" Space:");
 439   Space::print_on(st);
 440 
 441   st->print_cr("promoInfo:");
 442   _promoInfo.print_on(st);
 443 
 444   st->print_cr("_smallLinearAllocBlock");
 445   _smallLinearAllocBlock.print_on(st);
 446 
 447   // dump_memory_block(_smallLinearAllocBlock->_ptr, 128);
 448 
 449   st->print_cr(" _fitStrategy = %s, _adaptive_freelists = %s",
 450                _fitStrategy?"true":"false", _adaptive_freelists?"true":"false");
 451 }
 452 
 453 void CompactibleFreeListSpace::print_indexed_free_lists(outputStream* st)
 454 const {
 455   reportIndexedFreeListStatistics();
 456   gclog_or_tty->print_cr("Layout of Indexed Freelists");
 457   gclog_or_tty->print_cr("---------------------------");
 458   AdaptiveFreeList<FreeChunk>::print_labels_on(st, "size");
 459   for (size_t i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
 460     _indexedFreeList[i].print_on(gclog_or_tty);
 461     for (FreeChunk* fc = _indexedFreeList[i].head(); fc != NULL;
 462          fc = fc->next()) {
 463       gclog_or_tty->print_cr("\t[" PTR_FORMAT "," PTR_FORMAT ")  %s",
 464                           p2i(fc), p2i((HeapWord*)fc + i),
 465                           fc->cantCoalesce() ? "\t CC" : "");
 466     }
 467   }
 468 }
 469 
 470 void CompactibleFreeListSpace::print_promo_info_blocks(outputStream* st)
 471 const {
 472   _promoInfo.print_on(st);
 473 }
 474 
 475 void CompactibleFreeListSpace::print_dictionary_free_lists(outputStream* st)
 476 const {
 477   _dictionary->report_statistics();
 478   st->print_cr("Layout of Freelists in Tree");
 479   st->print_cr("---------------------------");
 480   _dictionary->print_free_lists(st);
 481 }
 482 
 483 class BlkPrintingClosure: public BlkClosure {
 484   const CMSCollector*             _collector;
 485   const CompactibleFreeListSpace* _sp;
 486   const CMSBitMap*                _live_bit_map;
 487   const bool                      _post_remark;
 488   outputStream*                   _st;
 489 public:
 490   BlkPrintingClosure(const CMSCollector* collector,
 491                      const CompactibleFreeListSpace* sp,
 492                      const CMSBitMap* live_bit_map,
 493                      outputStream* st):
 494     _collector(collector),
 495     _sp(sp),
 496     _live_bit_map(live_bit_map),
 497     _post_remark(collector->abstract_state() > CMSCollector::FinalMarking),
 498     _st(st) { }
 499   size_t do_blk(HeapWord* addr);
 500 };
 501 
 502 size_t BlkPrintingClosure::do_blk(HeapWord* addr) {
 503   size_t sz = _sp->block_size_no_stall(addr, _collector);
 504   assert(sz != 0, "Should always be able to compute a size");
 505   if (_sp->block_is_obj(addr)) {
 506     const bool dead = _post_remark && !_live_bit_map->isMarked(addr);
 507     _st->print_cr(PTR_FORMAT ": %s object of size " SIZE_FORMAT "%s",
 508       p2i(addr),
 509       dead ? "dead" : "live",
 510       sz,
 511       (!dead && CMSPrintObjectsInDump) ? ":" : ".");
 512     if (CMSPrintObjectsInDump && !dead) {
 513       oop(addr)->print_on(_st);
 514       _st->print_cr("--------------------------------------");
 515     }
 516   } else { // free block
 517     _st->print_cr(PTR_FORMAT ": free block of size " SIZE_FORMAT "%s",
 518       p2i(addr), sz, CMSPrintChunksInDump ? ":" : ".");
 519     if (CMSPrintChunksInDump) {
 520       ((FreeChunk*)addr)->print_on(_st);
 521       _st->print_cr("--------------------------------------");
 522     }
 523   }
 524   return sz;
 525 }
 526 
 527 void CompactibleFreeListSpace::dump_at_safepoint_with_locks(CMSCollector* c,
 528   outputStream* st) {
 529   st->print_cr("\n=========================");
 530   st->print_cr("Block layout in CMS Heap:");
 531   st->print_cr("=========================");
 532   BlkPrintingClosure  bpcl(c, this, c->markBitMap(), st);
 533   blk_iterate(&bpcl);
 534 
 535   st->print_cr("\n=======================================");
 536   st->print_cr("Order & Layout of Promotion Info Blocks");
 537   st->print_cr("=======================================");
 538   print_promo_info_blocks(st);
 539 
 540   st->print_cr("\n===========================");
 541   st->print_cr("Order of Indexed Free Lists");
 542   st->print_cr("=========================");
 543   print_indexed_free_lists(st);
 544 
 545   st->print_cr("\n=================================");
 546   st->print_cr("Order of Free Lists in Dictionary");
 547   st->print_cr("=================================");
 548   print_dictionary_free_lists(st);
 549 }
 550 
 551 
 552 void CompactibleFreeListSpace::reportFreeListStatistics() const {
 553   assert_lock_strong(&_freelistLock);
 554   assert(PrintFLSStatistics != 0, "Reporting error");
 555   _dictionary->report_statistics();
 556   if (PrintFLSStatistics > 1) {
 557     reportIndexedFreeListStatistics();
 558     size_t total_size = totalSizeInIndexedFreeLists() +
 559                        _dictionary->total_chunk_size(DEBUG_ONLY(freelistLock()));
 560     gclog_or_tty->print(" free=" SIZE_FORMAT " frag=%1.4f\n", total_size, flsFrag());
 561   }
 562 }
 563 
 564 void CompactibleFreeListSpace::reportIndexedFreeListStatistics() const {
 565   assert_lock_strong(&_freelistLock);
 566   gclog_or_tty->print("Statistics for IndexedFreeLists:\n"
 567                       "--------------------------------\n");
 568   size_t total_size = totalSizeInIndexedFreeLists();
 569   size_t   free_blocks = numFreeBlocksInIndexedFreeLists();
 570   gclog_or_tty->print("Total Free Space: " SIZE_FORMAT "\n", total_size);
 571   gclog_or_tty->print("Max   Chunk Size: " SIZE_FORMAT "\n", maxChunkSizeInIndexedFreeLists());
 572   gclog_or_tty->print("Number of Blocks: " SIZE_FORMAT "\n", free_blocks);
 573   if (free_blocks != 0) {
 574     gclog_or_tty->print("Av.  Block  Size: " SIZE_FORMAT "\n", total_size/free_blocks);
 575   }
 576 }
 577 
 578 size_t CompactibleFreeListSpace::numFreeBlocksInIndexedFreeLists() const {
 579   size_t res = 0;
 580   for (size_t i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
 581     debug_only(
 582       ssize_t recount = 0;
 583       for (FreeChunk* fc = _indexedFreeList[i].head(); fc != NULL;
 584          fc = fc->next()) {
 585         recount += 1;
 586       }
 587       assert(recount == _indexedFreeList[i].count(),
 588         "Incorrect count in list");
 589     )
 590     res += _indexedFreeList[i].count();
 591   }
 592   return res;
 593 }
 594 
 595 size_t CompactibleFreeListSpace::maxChunkSizeInIndexedFreeLists() const {
 596   for (size_t i = IndexSetSize - 1; i != 0; i -= IndexSetStride) {
 597     if (_indexedFreeList[i].head() != NULL) {
 598       assert(_indexedFreeList[i].count() != 0, "Inconsistent FreeList");
 599       return (size_t)i;
 600     }
 601   }
 602   return 0;
 603 }
 604 
 605 void CompactibleFreeListSpace::set_end(HeapWord* value) {
 606   HeapWord* prevEnd = end();
 607   assert(prevEnd != value, "unnecessary set_end call");
 608   assert(prevEnd == NULL || !BlockOffsetArrayUseUnallocatedBlock || value >= unallocated_block(),
 609         "New end is below unallocated block");
 610   _end = value;
 611   if (prevEnd != NULL) {
 612     // Resize the underlying block offset table.
 613     _bt.resize(pointer_delta(value, bottom()));
 614     if (value <= prevEnd) {
 615       assert(!BlockOffsetArrayUseUnallocatedBlock || value >= unallocated_block(),
 616              "New end is below unallocated block");
 617     } else {
 618       // Now, take this new chunk and add it to the free blocks.
 619       // Note that the BOT has not yet been updated for this block.
 620       size_t newFcSize = pointer_delta(value, prevEnd);
 621       // XXX This is REALLY UGLY and should be fixed up. XXX
 622       if (!_adaptive_freelists && _smallLinearAllocBlock._ptr == NULL) {
 623         // Mark the boundary of the new block in BOT
 624         _bt.mark_block(prevEnd, value);
 625         // put it all in the linAB
 626         if (ParallelGCThreads == 0) {
 627           _smallLinearAllocBlock._ptr = prevEnd;
 628           _smallLinearAllocBlock._word_size = newFcSize;
 629           repairLinearAllocBlock(&_smallLinearAllocBlock);
 630         } else { // ParallelGCThreads > 0
 631           MutexLockerEx x(parDictionaryAllocLock(),
 632                           Mutex::_no_safepoint_check_flag);
 633           _smallLinearAllocBlock._ptr = prevEnd;
 634           _smallLinearAllocBlock._word_size = newFcSize;
 635           repairLinearAllocBlock(&_smallLinearAllocBlock);
 636         }
 637         // Births of chunks put into a LinAB are not recorded.  Births
 638         // of chunks as they are allocated out of a LinAB are.
 639       } else {
 640         // Add the block to the free lists, if possible coalescing it
 641         // with the last free block, and update the BOT and census data.
 642         addChunkToFreeListsAtEndRecordingStats(prevEnd, newFcSize);
 643       }
 644     }
 645   }
 646 }
 647 
 648 class FreeListSpace_DCTOC : public Filtering_DCTOC {
 649   CompactibleFreeListSpace* _cfls;
 650   CMSCollector* _collector;
 651 protected:
 652   // Override.
 653 #define walk_mem_region_with_cl_DECL(ClosureType)                       \
 654   virtual void walk_mem_region_with_cl(MemRegion mr,                    \
 655                                        HeapWord* bottom, HeapWord* top, \
 656                                        ClosureType* cl);                \
 657       void walk_mem_region_with_cl_par(MemRegion mr,                    \
 658                                        HeapWord* bottom, HeapWord* top, \
 659                                        ClosureType* cl);                \
 660     void walk_mem_region_with_cl_nopar(MemRegion mr,                    \
 661                                        HeapWord* bottom, HeapWord* top, \
 662                                        ClosureType* cl)
 663   walk_mem_region_with_cl_DECL(ExtendedOopClosure);
 664   walk_mem_region_with_cl_DECL(FilteringClosure);
 665 
 666 public:
 667   FreeListSpace_DCTOC(CompactibleFreeListSpace* sp,
 668                       CMSCollector* collector,
 669                       ExtendedOopClosure* cl,
 670                       CardTableModRefBS::PrecisionStyle precision,
 671                       HeapWord* boundary) :
 672     Filtering_DCTOC(sp, cl, precision, boundary),
 673     _cfls(sp), _collector(collector) {}
 674 };
 675 
 676 // We de-virtualize the block-related calls below, since we know that our
 677 // space is a CompactibleFreeListSpace.
 678 
 679 #define FreeListSpace_DCTOC__walk_mem_region_with_cl_DEFN(ClosureType)          \
 680 void FreeListSpace_DCTOC::walk_mem_region_with_cl(MemRegion mr,                 \
 681                                                  HeapWord* bottom,              \
 682                                                  HeapWord* top,                 \
 683                                                  ClosureType* cl) {             \
 684    bool is_par = SharedHeap::heap()->n_par_threads() > 0;                       \
 685    if (is_par) {                                                                \
 686      assert(SharedHeap::heap()->n_par_threads() ==                              \
 687             SharedHeap::heap()->workers()->active_workers(), "Mismatch");       \
 688      walk_mem_region_with_cl_par(mr, bottom, top, cl);                          \
 689    } else {                                                                     \
 690      walk_mem_region_with_cl_nopar(mr, bottom, top, cl);                        \
 691    }                                                                            \
 692 }                                                                               \
 693 void FreeListSpace_DCTOC::walk_mem_region_with_cl_par(MemRegion mr,             \
 694                                                       HeapWord* bottom,         \
 695                                                       HeapWord* top,            \
 696                                                       ClosureType* cl) {        \
 697   /* Skip parts that are before "mr", in case "block_start" sent us             \
 698      back too far. */                                                           \
 699   HeapWord* mr_start = mr.start();                                              \
 700   size_t bot_size = _cfls->CompactibleFreeListSpace::block_size(bottom);        \
 701   HeapWord* next = bottom + bot_size;                                           \
 702   while (next < mr_start) {                                                     \
 703     bottom = next;                                                              \
 704     bot_size = _cfls->CompactibleFreeListSpace::block_size(bottom);             \
 705     next = bottom + bot_size;                                                   \
 706   }                                                                             \
 707                                                                                 \
 708   while (bottom < top) {                                                        \
 709     if (_cfls->CompactibleFreeListSpace::block_is_obj(bottom) &&                \
 710         !_cfls->CompactibleFreeListSpace::obj_allocated_since_save_marks(       \
 711                     oop(bottom)) &&                                             \
 712         !_collector->CMSCollector::is_dead_obj(oop(bottom))) {                  \
 713       size_t word_sz = oop(bottom)->oop_iterate(cl, mr);                        \
 714       bottom += _cfls->adjustObjectSize(word_sz);                               \
 715     } else {                                                                    \
 716       bottom += _cfls->CompactibleFreeListSpace::block_size(bottom);            \
 717     }                                                                           \
 718   }                                                                             \
 719 }                                                                               \
 720 void FreeListSpace_DCTOC::walk_mem_region_with_cl_nopar(MemRegion mr,           \
 721                                                         HeapWord* bottom,       \
 722                                                         HeapWord* top,          \
 723                                                         ClosureType* cl) {      \
 724   /* Skip parts that are before "mr", in case "block_start" sent us             \
 725      back too far. */                                                           \
 726   HeapWord* mr_start = mr.start();                                              \
 727   size_t bot_size = _cfls->CompactibleFreeListSpace::block_size_nopar(bottom);  \
 728   HeapWord* next = bottom + bot_size;                                           \
 729   while (next < mr_start) {                                                     \
 730     bottom = next;                                                              \
 731     bot_size = _cfls->CompactibleFreeListSpace::block_size_nopar(bottom);       \
 732     next = bottom + bot_size;                                                   \
 733   }                                                                             \
 734                                                                                 \
 735   while (bottom < top) {                                                        \
 736     if (_cfls->CompactibleFreeListSpace::block_is_obj_nopar(bottom) &&          \
 737         !_cfls->CompactibleFreeListSpace::obj_allocated_since_save_marks(       \
 738                     oop(bottom)) &&                                             \
 739         !_collector->CMSCollector::is_dead_obj(oop(bottom))) {                  \
 740       size_t word_sz = oop(bottom)->oop_iterate(cl, mr);                        \
 741       bottom += _cfls->adjustObjectSize(word_sz);                               \
 742     } else {                                                                    \
 743       bottom += _cfls->CompactibleFreeListSpace::block_size_nopar(bottom);      \
 744     }                                                                           \
 745   }                                                                             \
 746 }
 747 
 748 // (There are only two of these, rather than N, because the split is due
 749 // only to the introduction of the FilteringClosure, a local part of the
 750 // impl of this abstraction.)
 751 FreeListSpace_DCTOC__walk_mem_region_with_cl_DEFN(ExtendedOopClosure)
 752 FreeListSpace_DCTOC__walk_mem_region_with_cl_DEFN(FilteringClosure)
 753 
 754 DirtyCardToOopClosure*
 755 CompactibleFreeListSpace::new_dcto_cl(ExtendedOopClosure* cl,
 756                                       CardTableModRefBS::PrecisionStyle precision,
 757                                       HeapWord* boundary) {
 758   return new FreeListSpace_DCTOC(this, _collector, cl, precision, boundary);
 759 }
 760 
 761 
 762 // Note on locking for the space iteration functions:
 763 // since the collector's iteration activities are concurrent with
 764 // allocation activities by mutators, absent a suitable mutual exclusion
 765 // mechanism the iterators may go awry. For instance a block being iterated
 766 // may suddenly be allocated or divided up and part of it allocated and
 767 // so on.
 768 
 769 // Apply the given closure to each block in the space.
 770 void CompactibleFreeListSpace::blk_iterate_careful(BlkClosureCareful* cl) {
 771   assert_lock_strong(freelistLock());
 772   HeapWord *cur, *limit;
 773   for (cur = bottom(), limit = end(); cur < limit;
 774        cur += cl->do_blk_careful(cur));
 775 }
 776 
 777 // Apply the given closure to each block in the space.
 778 void CompactibleFreeListSpace::blk_iterate(BlkClosure* cl) {
 779   assert_lock_strong(freelistLock());
 780   HeapWord *cur, *limit;
 781   for (cur = bottom(), limit = end(); cur < limit;
 782        cur += cl->do_blk(cur));
 783 }
 784 
 785 // Apply the given closure to each oop in the space.
 786 void CompactibleFreeListSpace::oop_iterate(ExtendedOopClosure* cl) {
 787   assert_lock_strong(freelistLock());
 788   HeapWord *cur, *limit;
 789   size_t curSize;
 790   for (cur = bottom(), limit = end(); cur < limit;
 791        cur += curSize) {
 792     curSize = block_size(cur);
 793     if (block_is_obj(cur)) {
 794       oop(cur)->oop_iterate(cl);
 795     }
 796   }
 797 }
 798 
 799 // NOTE: In the following methods, in order to safely be able to
 800 // apply the closure to an object, we need to be sure that the
 801 // object has been initialized. We are guaranteed that an object
 802 // is initialized if we are holding the Heap_lock with the
 803 // world stopped.
 804 void CompactibleFreeListSpace::verify_objects_initialized() const {
 805   if (is_init_completed()) {
 806     assert_locked_or_safepoint(Heap_lock);
 807     if (Universe::is_fully_initialized()) {
 808       guarantee(SafepointSynchronize::is_at_safepoint(),
 809                 "Required for objects to be initialized");
 810     }
 811   } // else make a concession at vm start-up
 812 }
 813 
 814 // Apply the given closure to each object in the space
 815 void CompactibleFreeListSpace::object_iterate(ObjectClosure* blk) {
 816   assert_lock_strong(freelistLock());
 817   NOT_PRODUCT(verify_objects_initialized());
 818   HeapWord *cur, *limit;
 819   size_t curSize;
 820   for (cur = bottom(), limit = end(); cur < limit;
 821        cur += curSize) {
 822     curSize = block_size(cur);
 823     if (block_is_obj(cur)) {
 824       blk->do_object(oop(cur));
 825     }
 826   }
 827 }
 828 
 829 // Apply the given closure to each live object in the space
 830 //   The usage of CompactibleFreeListSpace
 831 // by the ConcurrentMarkSweepGeneration for concurrent GC's allows
 832 // objects in the space with references to objects that are no longer
 833 // valid.  For example, an object may reference another object
 834 // that has already been sweep up (collected).  This method uses
 835 // obj_is_alive() to determine whether it is safe to apply the closure to
 836 // an object.  See obj_is_alive() for details on how liveness of an
 837 // object is decided.
 838 
 839 void CompactibleFreeListSpace::safe_object_iterate(ObjectClosure* blk) {
 840   assert_lock_strong(freelistLock());
 841   NOT_PRODUCT(verify_objects_initialized());
 842   HeapWord *cur, *limit;
 843   size_t curSize;
 844   for (cur = bottom(), limit = end(); cur < limit;
 845        cur += curSize) {
 846     curSize = block_size(cur);
 847     if (block_is_obj(cur) && obj_is_alive(cur)) {
 848       blk->do_object(oop(cur));
 849     }
 850   }
 851 }
 852 
 853 void CompactibleFreeListSpace::object_iterate_mem(MemRegion mr,
 854                                                   UpwardsObjectClosure* cl) {
 855   assert_locked(freelistLock());
 856   NOT_PRODUCT(verify_objects_initialized());
 857   assert(!mr.is_empty(), "Should be non-empty");
 858   // We use MemRegion(bottom(), end()) rather than used_region() below
 859   // because the two are not necessarily equal for some kinds of
 860   // spaces, in particular, certain kinds of free list spaces.
 861   // We could use the more complicated but more precise:
 862   // MemRegion(used_region().start(), round_to(used_region().end(), CardSize))
 863   // but the slight imprecision seems acceptable in the assertion check.
 864   assert(MemRegion(bottom(), end()).contains(mr),
 865          "Should be within used space");
 866   HeapWord* prev = cl->previous();   // max address from last time
 867   if (prev >= mr.end()) { // nothing to do
 868     return;
 869   }
 870   // This assert will not work when we go from cms space to perm
 871   // space, and use same closure. Easy fix deferred for later. XXX YSR
 872   // assert(prev == NULL || contains(prev), "Should be within space");
 873 
 874   bool last_was_obj_array = false;
 875   HeapWord *blk_start_addr, *region_start_addr;
 876   if (prev > mr.start()) {
 877     region_start_addr = prev;
 878     blk_start_addr    = prev;
 879     // The previous invocation may have pushed "prev" beyond the
 880     // last allocated block yet there may be still be blocks
 881     // in this region due to a particular coalescing policy.
 882     // Relax the assertion so that the case where the unallocated
 883     // block is maintained and "prev" is beyond the unallocated
 884     // block does not cause the assertion to fire.
 885     assert((BlockOffsetArrayUseUnallocatedBlock &&
 886             (!is_in(prev))) ||
 887            (blk_start_addr == block_start(region_start_addr)), "invariant");
 888   } else {
 889     region_start_addr = mr.start();
 890     blk_start_addr    = block_start(region_start_addr);
 891   }
 892   HeapWord* region_end_addr = mr.end();
 893   MemRegion derived_mr(region_start_addr, region_end_addr);
 894   while (blk_start_addr < region_end_addr) {
 895     const size_t size = block_size(blk_start_addr);
 896     if (block_is_obj(blk_start_addr)) {
 897       last_was_obj_array = cl->do_object_bm(oop(blk_start_addr), derived_mr);
 898     } else {
 899       last_was_obj_array = false;
 900     }
 901     blk_start_addr += size;
 902   }
 903   if (!last_was_obj_array) {
 904     assert((bottom() <= blk_start_addr) && (blk_start_addr <= end()),
 905            "Should be within (closed) used space");
 906     assert(blk_start_addr > prev, "Invariant");
 907     cl->set_previous(blk_start_addr); // min address for next time
 908   }
 909 }
 910 
 911 
 912 // Callers of this iterator beware: The closure application should
 913 // be robust in the face of uninitialized objects and should (always)
 914 // return a correct size so that the next addr + size below gives us a
 915 // valid block boundary. [See for instance,
 916 // ScanMarkedObjectsAgainCarefullyClosure::do_object_careful()
 917 // in ConcurrentMarkSweepGeneration.cpp.]
 918 HeapWord*
 919 CompactibleFreeListSpace::object_iterate_careful_m(MemRegion mr,
 920   ObjectClosureCareful* cl) {
 921   assert_lock_strong(freelistLock());
 922   // Can't use used_region() below because it may not necessarily
 923   // be the same as [bottom(),end()); although we could
 924   // use [used_region().start(),round_to(used_region().end(),CardSize)),
 925   // that appears too cumbersome, so we just do the simpler check
 926   // in the assertion below.
 927   assert(!mr.is_empty() && MemRegion(bottom(),end()).contains(mr),
 928          "mr should be non-empty and within used space");
 929   HeapWord *addr, *end;
 930   size_t size;
 931   for (addr = block_start_careful(mr.start()), end  = mr.end();
 932        addr < end; addr += size) {
 933     FreeChunk* fc = (FreeChunk*)addr;
 934     if (fc->is_free()) {
 935       // Since we hold the free list lock, which protects direct
 936       // allocation in this generation by mutators, a free object
 937       // will remain free throughout this iteration code.
 938       size = fc->size();
 939     } else {
 940       // Note that the object need not necessarily be initialized,
 941       // because (for instance) the free list lock does NOT protect
 942       // object initialization. The closure application below must
 943       // therefore be correct in the face of uninitialized objects.
 944       size = cl->do_object_careful_m(oop(addr), mr);
 945       if (size == 0) {
 946         // An unparsable object found. Signal early termination.
 947         return addr;
 948       }
 949     }
 950   }
 951   return NULL;
 952 }
 953 
 954 
 955 HeapWord* CompactibleFreeListSpace::block_start_const(const void* p) const {
 956   NOT_PRODUCT(verify_objects_initialized());
 957   return _bt.block_start(p);
 958 }
 959 
 960 HeapWord* CompactibleFreeListSpace::block_start_careful(const void* p) const {
 961   return _bt.block_start_careful(p);
 962 }
 963 
 964 size_t CompactibleFreeListSpace::block_size(const HeapWord* p) const {
 965   NOT_PRODUCT(verify_objects_initialized());
 966   // This must be volatile, or else there is a danger that the compiler
 967   // will compile the code below into a sometimes-infinite loop, by keeping
 968   // the value read the first time in a register.
 969   while (true) {
 970     // We must do this until we get a consistent view of the object.
 971     if (FreeChunk::indicatesFreeChunk(p)) {
 972       volatile FreeChunk* fc = (volatile FreeChunk*)p;
 973       size_t res = fc->size();
 974 
 975       // Bugfix for systems with weak memory model (PPC64/IA64). The
 976       // block's free bit was set and we have read the size of the
 977       // block. Acquire and check the free bit again. If the block is
 978       // still free, the read size is correct.
 979       OrderAccess::acquire();
 980 
 981       // If the object is still a free chunk, return the size, else it
 982       // has been allocated so try again.
 983       if (FreeChunk::indicatesFreeChunk(p)) {
 984         assert(res != 0, "Block size should not be 0");
 985         return res;
 986       }
 987     } else {
 988       // must read from what 'p' points to in each loop.
 989       Klass* k = ((volatile oopDesc*)p)->klass_or_null();
 990       if (k != NULL) {
 991         assert(k->is_klass(), "Should really be klass oop.");
 992         oop o = (oop)p;
 993         assert(o->is_oop(true /* ignore mark word */), "Should be an oop.");
 994 
 995         // Bugfix for systems with weak memory model (PPC64/IA64).
 996         // The object o may be an array. Acquire to make sure that the array
 997         // size (third word) is consistent.
 998         OrderAccess::acquire();
 999 
1000         size_t res = o->size_given_klass(k);
1001         res = adjustObjectSize(res);
1002         assert(res != 0, "Block size should not be 0");
1003         return res;
1004       }
1005     }
1006   }
1007 }
1008 
1009 // TODO: Now that is_parsable is gone, we should combine these two functions.
1010 // A variant of the above that uses the Printezis bits for
1011 // unparsable but allocated objects. This avoids any possible
1012 // stalls waiting for mutators to initialize objects, and is
1013 // thus potentially faster than the variant above. However,
1014 // this variant may return a zero size for a block that is
1015 // under mutation and for which a consistent size cannot be
1016 // inferred without stalling; see CMSCollector::block_size_if_printezis_bits().
1017 size_t CompactibleFreeListSpace::block_size_no_stall(HeapWord* p,
1018                                                      const CMSCollector* c)
1019 const {
1020   assert(MemRegion(bottom(), end()).contains(p), "p not in space");
1021   // This must be volatile, or else there is a danger that the compiler
1022   // will compile the code below into a sometimes-infinite loop, by keeping
1023   // the value read the first time in a register.
1024   DEBUG_ONLY(uint loops = 0;)
1025   while (true) {
1026     // We must do this until we get a consistent view of the object.
1027     if (FreeChunk::indicatesFreeChunk(p)) {
1028       volatile FreeChunk* fc = (volatile FreeChunk*)p;
1029       size_t res = fc->size();
1030 
1031       // Bugfix for systems with weak memory model (PPC64/IA64). The
1032       // free bit of the block was set and we have read the size of
1033       // the block. Acquire and check the free bit again. If the
1034       // block is still free, the read size is correct.
1035       OrderAccess::acquire();
1036 
1037       if (FreeChunk::indicatesFreeChunk(p)) {
1038         assert(res != 0, "Block size should not be 0");
1039         assert(loops == 0, "Should be 0");
1040         return res;
1041       }
1042     } else {
1043       // must read from what 'p' points to in each loop.
1044       Klass* k = ((volatile oopDesc*)p)->klass_or_null();
1045       // We trust the size of any object that has a non-NULL
1046       // klass and (for those in the perm gen) is parsable
1047       // -- irrespective of its conc_safe-ty.
1048       if (k != NULL) {
1049         assert(k->is_klass(), "Should really be klass oop.");
1050         oop o = (oop)p;
1051         assert(o->is_oop(), "Should be an oop");
1052 
1053         // Bugfix for systems with weak memory model (PPC64/IA64).
1054         // The object o may be an array. Acquire to make sure that the array
1055         // size (third word) is consistent.
1056         OrderAccess::acquire();
1057 
1058         size_t res = o->size_given_klass(k);
1059         res = adjustObjectSize(res);
1060         assert(res != 0, "Block size should not be 0");
1061         return res;
1062       } else {
1063         // May return 0 if P-bits not present.
1064         return c->block_size_if_printezis_bits(p);
1065       }
1066     }
1067     assert(loops == 0, "Can loop at most once");
1068     DEBUG_ONLY(loops++;)
1069   }
1070 }
1071 
1072 size_t CompactibleFreeListSpace::block_size_nopar(const HeapWord* p) const {
1073   NOT_PRODUCT(verify_objects_initialized());
1074   assert(MemRegion(bottom(), end()).contains(p), "p not in space");
1075   FreeChunk* fc = (FreeChunk*)p;
1076   if (fc->is_free()) {
1077     return fc->size();
1078   } else {
1079     // Ignore mark word because this may be a recently promoted
1080     // object whose mark word is used to chain together grey
1081     // objects (the last one would have a null value).
1082     assert(oop(p)->is_oop(true), "Should be an oop");
1083     return adjustObjectSize(oop(p)->size());
1084   }
1085 }
1086 
1087 // This implementation assumes that the property of "being an object" is
1088 // stable.  But being a free chunk may not be (because of parallel
1089 // promotion.)
1090 bool CompactibleFreeListSpace::block_is_obj(const HeapWord* p) const {
1091   FreeChunk* fc = (FreeChunk*)p;
1092   assert(is_in_reserved(p), "Should be in space");
1093   // When doing a mark-sweep-compact of the CMS generation, this
1094   // assertion may fail because prepare_for_compaction() uses
1095   // space that is garbage to maintain information on ranges of
1096   // live objects so that these live ranges can be moved as a whole.
1097   // Comment out this assertion until that problem can be solved
1098   // (i.e., that the block start calculation may look at objects
1099   // at address below "p" in finding the object that contains "p"
1100   // and those objects (if garbage) may have been modified to hold
1101   // live range information.
1102   // assert(CollectedHeap::use_parallel_gc_threads() || _bt.block_start(p) == p,
1103   //        "Should be a block boundary");
1104   if (FreeChunk::indicatesFreeChunk(p)) return false;
1105   Klass* k = oop(p)->klass_or_null();
1106   if (k != NULL) {
1107     // Ignore mark word because it may have been used to
1108     // chain together promoted objects (the last one
1109     // would have a null value).
1110     assert(oop(p)->is_oop(true), "Should be an oop");
1111     return true;
1112   } else {
1113     return false;  // Was not an object at the start of collection.
1114   }
1115 }
1116 
1117 // Check if the object is alive. This fact is checked either by consulting
1118 // the main marking bitmap in the sweeping phase or, if it's a permanent
1119 // generation and we're not in the sweeping phase, by checking the
1120 // perm_gen_verify_bit_map where we store the "deadness" information if
1121 // we did not sweep the perm gen in the most recent previous GC cycle.
1122 bool CompactibleFreeListSpace::obj_is_alive(const HeapWord* p) const {
1123   assert(SafepointSynchronize::is_at_safepoint() || !is_init_completed(),
1124          "Else races are possible");
1125   assert(block_is_obj(p), "The address should point to an object");
1126 
1127   // If we're sweeping, we use object liveness information from the main bit map
1128   // for both perm gen and old gen.
1129   // We don't need to lock the bitmap (live_map or dead_map below), because
1130   // EITHER we are in the middle of the sweeping phase, and the
1131   // main marking bit map (live_map below) is locked,
1132   // OR we're in other phases and perm_gen_verify_bit_map (dead_map below)
1133   // is stable, because it's mutated only in the sweeping phase.
1134   // NOTE: This method is also used by jmap where, if class unloading is
1135   // off, the results can return "false" for legitimate perm objects,
1136   // when we are not in the midst of a sweeping phase, which can result
1137   // in jmap not reporting certain perm gen objects. This will be moot
1138   // if/when the perm gen goes away in the future.
1139   if (_collector->abstract_state() == CMSCollector::Sweeping) {
1140     CMSBitMap* live_map = _collector->markBitMap();
1141     return live_map->par_isMarked((HeapWord*) p);
1142   }
1143   return true;
1144 }
1145 
1146 bool CompactibleFreeListSpace::block_is_obj_nopar(const HeapWord* p) const {
1147   FreeChunk* fc = (FreeChunk*)p;
1148   assert(is_in_reserved(p), "Should be in space");
1149   assert(_bt.block_start(p) == p, "Should be a block boundary");
1150   if (!fc->is_free()) {
1151     // Ignore mark word because it may have been used to
1152     // chain together promoted objects (the last one
1153     // would have a null value).
1154     assert(oop(p)->is_oop(true), "Should be an oop");
1155     return true;
1156   }
1157   return false;
1158 }
1159 
1160 // "MT-safe but not guaranteed MT-precise" (TM); you may get an
1161 // approximate answer if you don't hold the freelistlock when you call this.
1162 size_t CompactibleFreeListSpace::totalSizeInIndexedFreeLists() const {
1163   size_t size = 0;
1164   for (size_t i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
1165     debug_only(
1166       // We may be calling here without the lock in which case we
1167       // won't do this modest sanity check.
1168       if (freelistLock()->owned_by_self()) {
1169         size_t total_list_size = 0;
1170         for (FreeChunk* fc = _indexedFreeList[i].head(); fc != NULL;
1171           fc = fc->next()) {
1172           total_list_size += i;
1173         }
1174         assert(total_list_size == i * _indexedFreeList[i].count(),
1175                "Count in list is incorrect");
1176       }
1177     )
1178     size += i * _indexedFreeList[i].count();
1179   }
1180   return size;
1181 }
1182 
1183 HeapWord* CompactibleFreeListSpace::par_allocate(size_t size) {
1184   MutexLockerEx x(freelistLock(), Mutex::_no_safepoint_check_flag);
1185   return allocate(size);
1186 }
1187 
1188 HeapWord*
1189 CompactibleFreeListSpace::getChunkFromSmallLinearAllocBlockRemainder(size_t size) {
1190   return getChunkFromLinearAllocBlockRemainder(&_smallLinearAllocBlock, size);
1191 }
1192 
1193 HeapWord* CompactibleFreeListSpace::allocate(size_t size) {
1194   assert_lock_strong(freelistLock());
1195   HeapWord* res = NULL;
1196   assert(size == adjustObjectSize(size),
1197          "use adjustObjectSize() before calling into allocate()");
1198 
1199   if (_adaptive_freelists) {
1200     res = allocate_adaptive_freelists(size);
1201   } else {  // non-adaptive free lists
1202     res = allocate_non_adaptive_freelists(size);
1203   }
1204 
1205   if (res != NULL) {
1206     // check that res does lie in this space!
1207     assert(is_in_reserved(res), "Not in this space!");
1208     assert(is_aligned((void*)res), "alignment check");
1209 
1210     FreeChunk* fc = (FreeChunk*)res;
1211     fc->markNotFree();
1212     assert(!fc->is_free(), "shouldn't be marked free");
1213     assert(oop(fc)->klass_or_null() == NULL, "should look uninitialized");
1214     // Verify that the block offset table shows this to
1215     // be a single block, but not one which is unallocated.
1216     _bt.verify_single_block(res, size);
1217     _bt.verify_not_unallocated(res, size);
1218     // mangle a just allocated object with a distinct pattern.
1219     debug_only(fc->mangleAllocated(size));
1220   }
1221 
1222   return res;
1223 }
1224 
1225 HeapWord* CompactibleFreeListSpace::allocate_non_adaptive_freelists(size_t size) {
1226   HeapWord* res = NULL;
1227   // try and use linear allocation for smaller blocks
1228   if (size < _smallLinearAllocBlock._allocation_size_limit) {
1229     // if successful, the following also adjusts block offset table
1230     res = getChunkFromSmallLinearAllocBlock(size);
1231   }
1232   // Else triage to indexed lists for smaller sizes
1233   if (res == NULL) {
1234     if (size < SmallForDictionary) {
1235       res = (HeapWord*) getChunkFromIndexedFreeList(size);
1236     } else {
1237       // else get it from the big dictionary; if even this doesn't
1238       // work we are out of luck.
1239       res = (HeapWord*)getChunkFromDictionaryExact(size);
1240     }
1241   }
1242 
1243   return res;
1244 }
1245 
1246 HeapWord* CompactibleFreeListSpace::allocate_adaptive_freelists(size_t size) {
1247   assert_lock_strong(freelistLock());
1248   HeapWord* res = NULL;
1249   assert(size == adjustObjectSize(size),
1250          "use adjustObjectSize() before calling into allocate()");
1251 
1252   // Strategy
1253   //   if small
1254   //     exact size from small object indexed list if small
1255   //     small or large linear allocation block (linAB) as appropriate
1256   //     take from lists of greater sized chunks
1257   //   else
1258   //     dictionary
1259   //     small or large linear allocation block if it has the space
1260   // Try allocating exact size from indexTable first
1261   if (size < IndexSetSize) {
1262     res = (HeapWord*) getChunkFromIndexedFreeList(size);
1263     if(res != NULL) {
1264       assert(res != (HeapWord*)_indexedFreeList[size].head(),
1265         "Not removed from free list");
1266       // no block offset table adjustment is necessary on blocks in
1267       // the indexed lists.
1268 
1269     // Try allocating from the small LinAB
1270     } else if (size < _smallLinearAllocBlock._allocation_size_limit &&
1271         (res = getChunkFromSmallLinearAllocBlock(size)) != NULL) {
1272         // if successful, the above also adjusts block offset table
1273         // Note that this call will refill the LinAB to
1274         // satisfy the request.  This is different that
1275         // evm.
1276         // Don't record chunk off a LinAB?  smallSplitBirth(size);
1277     } else {
1278       // Raid the exact free lists larger than size, even if they are not
1279       // overpopulated.
1280       res = (HeapWord*) getChunkFromGreater(size);
1281     }
1282   } else {
1283     // Big objects get allocated directly from the dictionary.
1284     res = (HeapWord*) getChunkFromDictionaryExact(size);
1285     if (res == NULL) {
1286       // Try hard not to fail since an allocation failure will likely
1287       // trigger a synchronous GC.  Try to get the space from the
1288       // allocation blocks.
1289       res = getChunkFromSmallLinearAllocBlockRemainder(size);
1290     }
1291   }
1292 
1293   return res;
1294 }
1295 
1296 // A worst-case estimate of the space required (in HeapWords) to expand the heap
1297 // when promoting obj.
1298 size_t CompactibleFreeListSpace::expansionSpaceRequired(size_t obj_size) const {
1299   // Depending on the object size, expansion may require refilling either a
1300   // bigLAB or a smallLAB plus refilling a PromotionInfo object.  MinChunkSize
1301   // is added because the dictionary may over-allocate to avoid fragmentation.
1302   size_t space = obj_size;
1303   if (!_adaptive_freelists) {
1304     space = MAX2(space, _smallLinearAllocBlock._refillSize);
1305   }
1306   space += _promoInfo.refillSize() + 2 * MinChunkSize;
1307   return space;
1308 }
1309 
1310 FreeChunk* CompactibleFreeListSpace::getChunkFromGreater(size_t numWords) {
1311   FreeChunk* ret;
1312 
1313   assert(numWords >= MinChunkSize, "Size is less than minimum");
1314   assert(linearAllocationWouldFail() || bestFitFirst(),
1315     "Should not be here");
1316 
1317   size_t i;
1318   size_t currSize = numWords + MinChunkSize;
1319   assert(currSize % MinObjAlignment == 0, "currSize should be aligned");
1320   for (i = currSize; i < IndexSetSize; i += IndexSetStride) {
1321     AdaptiveFreeList<FreeChunk>* fl = &_indexedFreeList[i];
1322     if (fl->head()) {
1323       ret = getFromListGreater(fl, numWords);
1324       assert(ret == NULL || ret->is_free(), "Should be returning a free chunk");
1325       return ret;
1326     }
1327   }
1328 
1329   currSize = MAX2((size_t)SmallForDictionary,
1330                   (size_t)(numWords + MinChunkSize));
1331 
1332   /* Try to get a chunk that satisfies request, while avoiding
1333      fragmentation that can't be handled. */
1334   {
1335     ret =  dictionary()->get_chunk(currSize);
1336     if (ret != NULL) {
1337       assert(ret->size() - numWords >= MinChunkSize,
1338              "Chunk is too small");
1339       _bt.allocated((HeapWord*)ret, ret->size());
1340       /* Carve returned chunk. */
1341       (void) splitChunkAndReturnRemainder(ret, numWords);
1342       /* Label this as no longer a free chunk. */
1343       assert(ret->is_free(), "This chunk should be free");
1344       ret->link_prev(NULL);
1345     }
1346     assert(ret == NULL || ret->is_free(), "Should be returning a free chunk");
1347     return ret;
1348   }
1349   ShouldNotReachHere();
1350 }
1351 
1352 bool CompactibleFreeListSpace::verifyChunkInIndexedFreeLists(FreeChunk* fc) const {
1353   assert(fc->size() < IndexSetSize, "Size of chunk is too large");
1354   return _indexedFreeList[fc->size()].verify_chunk_in_free_list(fc);
1355 }
1356 
1357 bool CompactibleFreeListSpace::verify_chunk_is_linear_alloc_block(FreeChunk* fc) const {
1358   assert((_smallLinearAllocBlock._ptr != (HeapWord*)fc) ||
1359          (_smallLinearAllocBlock._word_size == fc->size()),
1360          "Linear allocation block shows incorrect size");
1361   return ((_smallLinearAllocBlock._ptr == (HeapWord*)fc) &&
1362           (_smallLinearAllocBlock._word_size == fc->size()));
1363 }
1364 
1365 // Check if the purported free chunk is present either as a linear
1366 // allocation block, the size-indexed table of (smaller) free blocks,
1367 // or the larger free blocks kept in the binary tree dictionary.
1368 bool CompactibleFreeListSpace::verify_chunk_in_free_list(FreeChunk* fc) const {
1369   if (verify_chunk_is_linear_alloc_block(fc)) {
1370     return true;
1371   } else if (fc->size() < IndexSetSize) {
1372     return verifyChunkInIndexedFreeLists(fc);
1373   } else {
1374     return dictionary()->verify_chunk_in_free_list(fc);
1375   }
1376 }
1377 
1378 #ifndef PRODUCT
1379 void CompactibleFreeListSpace::assert_locked() const {
1380   CMSLockVerifier::assert_locked(freelistLock(), parDictionaryAllocLock());
1381 }
1382 
1383 void CompactibleFreeListSpace::assert_locked(const Mutex* lock) const {
1384   CMSLockVerifier::assert_locked(lock);
1385 }
1386 #endif
1387 
1388 FreeChunk* CompactibleFreeListSpace::allocateScratch(size_t size) {
1389   // In the parallel case, the main thread holds the free list lock
1390   // on behalf the parallel threads.
1391   FreeChunk* fc;
1392   {
1393     // If GC is parallel, this might be called by several threads.
1394     // This should be rare enough that the locking overhead won't affect
1395     // the sequential code.
1396     MutexLockerEx x(parDictionaryAllocLock(),
1397                     Mutex::_no_safepoint_check_flag);
1398     fc = getChunkFromDictionary(size);
1399   }
1400   if (fc != NULL) {
1401     fc->dontCoalesce();
1402     assert(fc->is_free(), "Should be free, but not coalescable");
1403     // Verify that the block offset table shows this to
1404     // be a single block, but not one which is unallocated.
1405     _bt.verify_single_block((HeapWord*)fc, fc->size());
1406     _bt.verify_not_unallocated((HeapWord*)fc, fc->size());
1407   }
1408   return fc;
1409 }
1410 
1411 oop CompactibleFreeListSpace::promote(oop obj, size_t obj_size) {
1412   assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");
1413   assert_locked();
1414 
1415   // if we are tracking promotions, then first ensure space for
1416   // promotion (including spooling space for saving header if necessary).
1417   // then allocate and copy, then track promoted info if needed.
1418   // When tracking (see PromotionInfo::track()), the mark word may
1419   // be displaced and in this case restoration of the mark word
1420   // occurs in the (oop_since_save_marks_)iterate phase.
1421   if (_promoInfo.tracking() && !_promoInfo.ensure_spooling_space()) {
1422     return NULL;
1423   }
1424   // Call the allocate(size_t, bool) form directly to avoid the
1425   // additional call through the allocate(size_t) form.  Having
1426   // the compile inline the call is problematic because allocate(size_t)
1427   // is a virtual method.
1428   HeapWord* res = allocate(adjustObjectSize(obj_size));
1429   if (res != NULL) {
1430     Copy::aligned_disjoint_words((HeapWord*)obj, res, obj_size);
1431     // if we should be tracking promotions, do so.
1432     if (_promoInfo.tracking()) {
1433         _promoInfo.track((PromotedObject*)res);
1434     }
1435   }
1436   return oop(res);
1437 }
1438 
1439 HeapWord*
1440 CompactibleFreeListSpace::getChunkFromSmallLinearAllocBlock(size_t size) {
1441   assert_locked();
1442   assert(size >= MinChunkSize, "minimum chunk size");
1443   assert(size <  _smallLinearAllocBlock._allocation_size_limit,
1444     "maximum from smallLinearAllocBlock");
1445   return getChunkFromLinearAllocBlock(&_smallLinearAllocBlock, size);
1446 }
1447 
1448 HeapWord*
1449 CompactibleFreeListSpace::getChunkFromLinearAllocBlock(LinearAllocBlock *blk,
1450                                                        size_t size) {
1451   assert_locked();
1452   assert(size >= MinChunkSize, "too small");
1453   HeapWord* res = NULL;
1454   // Try to do linear allocation from blk, making sure that
1455   if (blk->_word_size == 0) {
1456     // We have probably been unable to fill this either in the prologue or
1457     // when it was exhausted at the last linear allocation. Bail out until
1458     // next time.
1459     assert(blk->_ptr == NULL, "consistency check");
1460     return NULL;
1461   }
1462   assert(blk->_word_size != 0 && blk->_ptr != NULL, "consistency check");
1463   res = getChunkFromLinearAllocBlockRemainder(blk, size);
1464   if (res != NULL) return res;
1465 
1466   // about to exhaust this linear allocation block
1467   if (blk->_word_size == size) { // exactly satisfied
1468     res = blk->_ptr;
1469     _bt.allocated(res, blk->_word_size);
1470   } else if (size + MinChunkSize <= blk->_refillSize) {
1471     size_t sz = blk->_word_size;
1472     // Update _unallocated_block if the size is such that chunk would be
1473     // returned to the indexed free list.  All other chunks in the indexed
1474     // free lists are allocated from the dictionary so that _unallocated_block
1475     // has already been adjusted for them.  Do it here so that the cost
1476     // for all chunks added back to the indexed free lists.
1477     if (sz < SmallForDictionary) {
1478       _bt.allocated(blk->_ptr, sz);
1479     }
1480     // Return the chunk that isn't big enough, and then refill below.
1481     addChunkToFreeLists(blk->_ptr, sz);
1482     split_birth(sz);
1483     // Don't keep statistics on adding back chunk from a LinAB.
1484   } else {
1485     // A refilled block would not satisfy the request.
1486     return NULL;
1487   }
1488 
1489   blk->_ptr = NULL; blk->_word_size = 0;
1490   refillLinearAllocBlock(blk);
1491   assert(blk->_ptr == NULL || blk->_word_size >= size + MinChunkSize,
1492          "block was replenished");
1493   if (res != NULL) {
1494     split_birth(size);
1495     repairLinearAllocBlock(blk);
1496   } else if (blk->_ptr != NULL) {
1497     res = blk->_ptr;
1498     size_t blk_size = blk->_word_size;
1499     blk->_word_size -= size;
1500     blk->_ptr  += size;
1501     split_birth(size);
1502     repairLinearAllocBlock(blk);
1503     // Update BOT last so that other (parallel) GC threads see a consistent
1504     // view of the BOT and free blocks.
1505     // Above must occur before BOT is updated below.
1506     OrderAccess::storestore();
1507     _bt.split_block(res, blk_size, size);  // adjust block offset table
1508   }
1509   return res;
1510 }
1511 
1512 HeapWord*  CompactibleFreeListSpace::getChunkFromLinearAllocBlockRemainder(
1513                                         LinearAllocBlock* blk,
1514                                         size_t size) {
1515   assert_locked();
1516   assert(size >= MinChunkSize, "too small");
1517 
1518   HeapWord* res = NULL;
1519   // This is the common case.  Keep it simple.
1520   if (blk->_word_size >= size + MinChunkSize) {
1521     assert(blk->_ptr != NULL, "consistency check");
1522     res = blk->_ptr;
1523     // Note that the BOT is up-to-date for the linAB before allocation.  It
1524     // indicates the start of the linAB.  The split_block() updates the
1525     // BOT for the linAB after the allocation (indicates the start of the
1526     // next chunk to be allocated).
1527     size_t blk_size = blk->_word_size;
1528     blk->_word_size -= size;
1529     blk->_ptr  += size;
1530     split_birth(size);
1531     repairLinearAllocBlock(blk);
1532     // Update BOT last so that other (parallel) GC threads see a consistent
1533     // view of the BOT and free blocks.
1534     // Above must occur before BOT is updated below.
1535     OrderAccess::storestore();
1536     _bt.split_block(res, blk_size, size);  // adjust block offset table
1537     _bt.allocated(res, size);
1538   }
1539   return res;
1540 }
1541 
1542 FreeChunk*
1543 CompactibleFreeListSpace::getChunkFromIndexedFreeList(size_t size) {
1544   assert_locked();
1545   assert(size < SmallForDictionary, "just checking");
1546   FreeChunk* res;
1547   res = _indexedFreeList[size].get_chunk_at_head();
1548   if (res == NULL) {
1549     res = getChunkFromIndexedFreeListHelper(size);
1550   }
1551   _bt.verify_not_unallocated((HeapWord*) res, size);
1552   assert(res == NULL || res->size() == size, "Incorrect block size");
1553   return res;
1554 }
1555 
1556 FreeChunk*
1557 CompactibleFreeListSpace::getChunkFromIndexedFreeListHelper(size_t size,
1558   bool replenish) {
1559   assert_locked();
1560   FreeChunk* fc = NULL;
1561   if (size < SmallForDictionary) {
1562     assert(_indexedFreeList[size].head() == NULL ||
1563       _indexedFreeList[size].surplus() <= 0,
1564       "List for this size should be empty or under populated");
1565     // Try best fit in exact lists before replenishing the list
1566     if (!bestFitFirst() || (fc = bestFitSmall(size)) == NULL) {
1567       // Replenish list.
1568       //
1569       // Things tried that failed.
1570       //   Tried allocating out of the two LinAB's first before
1571       // replenishing lists.
1572       //   Tried small linAB of size 256 (size in indexed list)
1573       // and replenishing indexed lists from the small linAB.
1574       //
1575       FreeChunk* newFc = NULL;
1576       const size_t replenish_size = CMSIndexedFreeListReplenish * size;
1577       if (replenish_size < SmallForDictionary) {
1578         // Do not replenish from an underpopulated size.
1579         if (_indexedFreeList[replenish_size].surplus() > 0 &&
1580             _indexedFreeList[replenish_size].head() != NULL) {
1581           newFc = _indexedFreeList[replenish_size].get_chunk_at_head();
1582         } else if (bestFitFirst()) {
1583           newFc = bestFitSmall(replenish_size);
1584         }
1585       }
1586       if (newFc == NULL && replenish_size > size) {
1587         assert(CMSIndexedFreeListReplenish > 1, "ctl pt invariant");
1588         newFc = getChunkFromIndexedFreeListHelper(replenish_size, false);
1589       }
1590       // Note: The stats update re split-death of block obtained above
1591       // will be recorded below precisely when we know we are going to
1592       // be actually splitting it into more than one pieces below.
1593       if (newFc != NULL) {
1594         if  (replenish || CMSReplenishIntermediate) {
1595           // Replenish this list and return one block to caller.
1596           size_t i;
1597           FreeChunk *curFc, *nextFc;
1598           size_t num_blk = newFc->size() / size;
1599           assert(num_blk >= 1, "Smaller than requested?");
1600           assert(newFc->size() % size == 0, "Should be integral multiple of request");
1601           if (num_blk > 1) {
1602             // we are sure we will be splitting the block just obtained
1603             // into multiple pieces; record the split-death of the original
1604             splitDeath(replenish_size);
1605           }
1606           // carve up and link blocks 0, ..., num_blk - 2
1607           // The last chunk is not added to the lists but is returned as the
1608           // free chunk.
1609           for (curFc = newFc, nextFc = (FreeChunk*)((HeapWord*)curFc + size),
1610                i = 0;
1611                i < (num_blk - 1);
1612                curFc = nextFc, nextFc = (FreeChunk*)((HeapWord*)nextFc + size),
1613                i++) {
1614             curFc->set_size(size);
1615             // Don't record this as a return in order to try and
1616             // determine the "returns" from a GC.
1617             _bt.verify_not_unallocated((HeapWord*) fc, size);
1618             _indexedFreeList[size].return_chunk_at_tail(curFc, false);
1619             _bt.mark_block((HeapWord*)curFc, size);
1620             split_birth(size);
1621             // Don't record the initial population of the indexed list
1622             // as a split birth.
1623           }
1624 
1625           // check that the arithmetic was OK above
1626           assert((HeapWord*)nextFc == (HeapWord*)newFc + num_blk*size,
1627             "inconsistency in carving newFc");
1628           curFc->set_size(size);
1629           _bt.mark_block((HeapWord*)curFc, size);
1630           split_birth(size);
1631           fc = curFc;
1632         } else {
1633           // Return entire block to caller
1634           fc = newFc;
1635         }
1636       }
1637     }
1638   } else {
1639     // Get a free chunk from the free chunk dictionary to be returned to
1640     // replenish the indexed free list.
1641     fc = getChunkFromDictionaryExact(size);
1642   }
1643   // assert(fc == NULL || fc->is_free(), "Should be returning a free chunk");
1644   return fc;
1645 }
1646 
1647 FreeChunk*
1648 CompactibleFreeListSpace::getChunkFromDictionary(size_t size) {
1649   assert_locked();
1650   FreeChunk* fc = _dictionary->get_chunk(size,
1651                                          FreeBlockDictionary<FreeChunk>::atLeast);
1652   if (fc == NULL) {
1653     return NULL;
1654   }
1655   _bt.allocated((HeapWord*)fc, fc->size());
1656   if (fc->size() >= size + MinChunkSize) {
1657     fc = splitChunkAndReturnRemainder(fc, size);
1658   }
1659   assert(fc->size() >= size, "chunk too small");
1660   assert(fc->size() < size + MinChunkSize, "chunk too big");
1661   _bt.verify_single_block((HeapWord*)fc, fc->size());
1662   return fc;
1663 }
1664 
1665 FreeChunk*
1666 CompactibleFreeListSpace::getChunkFromDictionaryExact(size_t size) {
1667   assert_locked();
1668   FreeChunk* fc = _dictionary->get_chunk(size,
1669                                          FreeBlockDictionary<FreeChunk>::atLeast);
1670   if (fc == NULL) {
1671     return fc;
1672   }
1673   _bt.allocated((HeapWord*)fc, fc->size());
1674   if (fc->size() == size) {
1675     _bt.verify_single_block((HeapWord*)fc, size);
1676     return fc;
1677   }
1678   assert(fc->size() > size, "get_chunk() guarantee");
1679   if (fc->size() < size + MinChunkSize) {
1680     // Return the chunk to the dictionary and go get a bigger one.
1681     returnChunkToDictionary(fc);
1682     fc = _dictionary->get_chunk(size + MinChunkSize,
1683                                 FreeBlockDictionary<FreeChunk>::atLeast);
1684     if (fc == NULL) {
1685       return NULL;
1686     }
1687     _bt.allocated((HeapWord*)fc, fc->size());
1688   }
1689   assert(fc->size() >= size + MinChunkSize, "tautology");
1690   fc = splitChunkAndReturnRemainder(fc, size);
1691   assert(fc->size() == size, "chunk is wrong size");
1692   _bt.verify_single_block((HeapWord*)fc, size);
1693   return fc;
1694 }
1695 
1696 void
1697 CompactibleFreeListSpace::returnChunkToDictionary(FreeChunk* chunk) {
1698   assert_locked();
1699 
1700   size_t size = chunk->size();
1701   _bt.verify_single_block((HeapWord*)chunk, size);
1702   // adjust _unallocated_block downward, as necessary
1703   _bt.freed((HeapWord*)chunk, size);
1704   _dictionary->return_chunk(chunk);
1705 #ifndef PRODUCT
1706   if (CMSCollector::abstract_state() != CMSCollector::Sweeping) {
1707     TreeChunk<FreeChunk, AdaptiveFreeList<FreeChunk> >* tc = TreeChunk<FreeChunk, AdaptiveFreeList<FreeChunk> >::as_TreeChunk(chunk);
1708     TreeList<FreeChunk, AdaptiveFreeList<FreeChunk> >* tl = tc->list();
1709     tl->verify_stats();
1710   }
1711 #endif // PRODUCT
1712 }
1713 
1714 void
1715 CompactibleFreeListSpace::returnChunkToFreeList(FreeChunk* fc) {
1716   assert_locked();
1717   size_t size = fc->size();
1718   _bt.verify_single_block((HeapWord*) fc, size);
1719   _bt.verify_not_unallocated((HeapWord*) fc, size);
1720   if (_adaptive_freelists) {
1721     _indexedFreeList[size].return_chunk_at_tail(fc);
1722   } else {
1723     _indexedFreeList[size].return_chunk_at_head(fc);
1724   }
1725 #ifndef PRODUCT
1726   if (CMSCollector::abstract_state() != CMSCollector::Sweeping) {
1727      _indexedFreeList[size].verify_stats();
1728   }
1729 #endif // PRODUCT
1730 }
1731 
1732 // Add chunk to end of last block -- if it's the largest
1733 // block -- and update BOT and census data. We would
1734 // of course have preferred to coalesce it with the
1735 // last block, but it's currently less expensive to find the
1736 // largest block than it is to find the last.
1737 void
1738 CompactibleFreeListSpace::addChunkToFreeListsAtEndRecordingStats(
1739   HeapWord* chunk, size_t     size) {
1740   // check that the chunk does lie in this space!
1741   assert(chunk != NULL && is_in_reserved(chunk), "Not in this space!");
1742   // One of the parallel gc task threads may be here
1743   // whilst others are allocating.
1744   Mutex* lock = NULL;
1745   if (ParallelGCThreads != 0) {
1746     lock = &_parDictionaryAllocLock;
1747   }
1748   FreeChunk* ec;
1749   {
1750     MutexLockerEx x(lock, Mutex::_no_safepoint_check_flag);
1751     ec = dictionary()->find_largest_dict();  // get largest block
1752     if (ec != NULL && ec->end() == (uintptr_t*) chunk) {
1753       // It's a coterminal block - we can coalesce.
1754       size_t old_size = ec->size();
1755       coalDeath(old_size);
1756       removeChunkFromDictionary(ec);
1757       size += old_size;
1758     } else {
1759       ec = (FreeChunk*)chunk;
1760     }
1761   }
1762   ec->set_size(size);
1763   debug_only(ec->mangleFreed(size));
1764   if (size < SmallForDictionary && ParallelGCThreads != 0) {
1765     lock = _indexedFreeListParLocks[size];
1766   }
1767   MutexLockerEx x(lock, Mutex::_no_safepoint_check_flag);
1768   addChunkAndRepairOffsetTable((HeapWord*)ec, size, true);
1769   // record the birth under the lock since the recording involves
1770   // manipulation of the list on which the chunk lives and
1771   // if the chunk is allocated and is the last on the list,
1772   // the list can go away.
1773   coalBirth(size);
1774 }
1775 
1776 void
1777 CompactibleFreeListSpace::addChunkToFreeLists(HeapWord* chunk,
1778                                               size_t     size) {
1779   // check that the chunk does lie in this space!
1780   assert(chunk != NULL && is_in_reserved(chunk), "Not in this space!");
1781   assert_locked();
1782   _bt.verify_single_block(chunk, size);
1783 
1784   FreeChunk* fc = (FreeChunk*) chunk;
1785   fc->set_size(size);
1786   debug_only(fc->mangleFreed(size));
1787   if (size < SmallForDictionary) {
1788     returnChunkToFreeList(fc);
1789   } else {
1790     returnChunkToDictionary(fc);
1791   }
1792 }
1793 
1794 void
1795 CompactibleFreeListSpace::addChunkAndRepairOffsetTable(HeapWord* chunk,
1796   size_t size, bool coalesced) {
1797   assert_locked();
1798   assert(chunk != NULL, "null chunk");
1799   if (coalesced) {
1800     // repair BOT
1801     _bt.single_block(chunk, size);
1802   }
1803   addChunkToFreeLists(chunk, size);
1804 }
1805 
1806 // We _must_ find the purported chunk on our free lists;
1807 // we assert if we don't.
1808 void
1809 CompactibleFreeListSpace::removeFreeChunkFromFreeLists(FreeChunk* fc) {
1810   size_t size = fc->size();
1811   assert_locked();
1812   debug_only(verifyFreeLists());
1813   if (size < SmallForDictionary) {
1814     removeChunkFromIndexedFreeList(fc);
1815   } else {
1816     removeChunkFromDictionary(fc);
1817   }
1818   _bt.verify_single_block((HeapWord*)fc, size);
1819   debug_only(verifyFreeLists());
1820 }
1821 
1822 void
1823 CompactibleFreeListSpace::removeChunkFromDictionary(FreeChunk* fc) {
1824   size_t size = fc->size();
1825   assert_locked();
1826   assert(fc != NULL, "null chunk");
1827   _bt.verify_single_block((HeapWord*)fc, size);
1828   _dictionary->remove_chunk(fc);
1829   // adjust _unallocated_block upward, as necessary
1830   _bt.allocated((HeapWord*)fc, size);
1831 }
1832 
1833 void
1834 CompactibleFreeListSpace::removeChunkFromIndexedFreeList(FreeChunk* fc) {
1835   assert_locked();
1836   size_t size = fc->size();
1837   _bt.verify_single_block((HeapWord*)fc, size);
1838   NOT_PRODUCT(
1839     if (FLSVerifyIndexTable) {
1840       verifyIndexedFreeList(size);
1841     }
1842   )
1843   _indexedFreeList[size].remove_chunk(fc);
1844   NOT_PRODUCT(
1845     if (FLSVerifyIndexTable) {
1846       verifyIndexedFreeList(size);
1847     }
1848   )
1849 }
1850 
1851 FreeChunk* CompactibleFreeListSpace::bestFitSmall(size_t numWords) {
1852   /* A hint is the next larger size that has a surplus.
1853      Start search at a size large enough to guarantee that
1854      the excess is >= MIN_CHUNK. */
1855   size_t start = align_object_size(numWords + MinChunkSize);
1856   if (start < IndexSetSize) {
1857     AdaptiveFreeList<FreeChunk>* it   = _indexedFreeList;
1858     size_t    hint = _indexedFreeList[start].hint();
1859     while (hint < IndexSetSize) {
1860       assert(hint % MinObjAlignment == 0, "hint should be aligned");
1861       AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[hint];
1862       if (fl->surplus() > 0 && fl->head() != NULL) {
1863         // Found a list with surplus, reset original hint
1864         // and split out a free chunk which is returned.
1865         _indexedFreeList[start].set_hint(hint);
1866         FreeChunk* res = getFromListGreater(fl, numWords);
1867         assert(res == NULL || res->is_free(),
1868           "Should be returning a free chunk");
1869         return res;
1870       }
1871       hint = fl->hint(); /* keep looking */
1872     }
1873     /* None found. */
1874     it[start].set_hint(IndexSetSize);
1875   }
1876   return NULL;
1877 }
1878 
1879 /* Requires fl->size >= numWords + MinChunkSize */
1880 FreeChunk* CompactibleFreeListSpace::getFromListGreater(AdaptiveFreeList<FreeChunk>* fl,
1881   size_t numWords) {
1882   FreeChunk *curr = fl->head();
1883   size_t oldNumWords = curr->size();
1884   assert(numWords >= MinChunkSize, "Word size is too small");
1885   assert(curr != NULL, "List is empty");
1886   assert(oldNumWords >= numWords + MinChunkSize,
1887         "Size of chunks in the list is too small");
1888 
1889   fl->remove_chunk(curr);
1890   // recorded indirectly by splitChunkAndReturnRemainder -
1891   // smallSplit(oldNumWords, numWords);
1892   FreeChunk* new_chunk = splitChunkAndReturnRemainder(curr, numWords);
1893   // Does anything have to be done for the remainder in terms of
1894   // fixing the card table?
1895   assert(new_chunk == NULL || new_chunk->is_free(),
1896     "Should be returning a free chunk");
1897   return new_chunk;
1898 }
1899 
1900 FreeChunk*
1901 CompactibleFreeListSpace::splitChunkAndReturnRemainder(FreeChunk* chunk,
1902   size_t new_size) {
1903   assert_locked();
1904   size_t size = chunk->size();
1905   assert(size > new_size, "Split from a smaller block?");
1906   assert(is_aligned(chunk), "alignment problem");
1907   assert(size == adjustObjectSize(size), "alignment problem");
1908   size_t rem_sz = size - new_size;
1909   assert(rem_sz == adjustObjectSize(rem_sz), "alignment problem");
1910   assert(rem_sz >= MinChunkSize, "Free chunk smaller than minimum");
1911   FreeChunk* ffc = (FreeChunk*)((HeapWord*)chunk + new_size);
1912   assert(is_aligned(ffc), "alignment problem");
1913   ffc->set_size(rem_sz);
1914   ffc->link_next(NULL);
1915   ffc->link_prev(NULL); // Mark as a free block for other (parallel) GC threads.
1916   // Above must occur before BOT is updated below.
1917   // adjust block offset table
1918   OrderAccess::storestore();
1919   assert(chunk->is_free() && ffc->is_free(), "Error");
1920   _bt.split_block((HeapWord*)chunk, chunk->size(), new_size);
1921   if (rem_sz < SmallForDictionary) {
1922     bool is_par = (SharedHeap::heap()->n_par_threads() > 0);
1923     if (is_par) _indexedFreeListParLocks[rem_sz]->lock();
1924     assert(!is_par ||
1925            (SharedHeap::heap()->n_par_threads() ==
1926             SharedHeap::heap()->workers()->active_workers()), "Mismatch");
1927     returnChunkToFreeList(ffc);
1928     split(size, rem_sz);
1929     if (is_par) _indexedFreeListParLocks[rem_sz]->unlock();
1930   } else {
1931     returnChunkToDictionary(ffc);
1932     split(size, rem_sz);
1933   }
1934   chunk->set_size(new_size);
1935   return chunk;
1936 }
1937 
1938 void
1939 CompactibleFreeListSpace::sweep_completed() {
1940   // Now that space is probably plentiful, refill linear
1941   // allocation blocks as needed.
1942   refillLinearAllocBlocksIfNeeded();
1943 }
1944 
1945 void
1946 CompactibleFreeListSpace::gc_prologue() {
1947   assert_locked();
1948   if (PrintFLSStatistics != 0) {
1949     gclog_or_tty->print("Before GC:\n");
1950     reportFreeListStatistics();
1951   }
1952   refillLinearAllocBlocksIfNeeded();
1953 }
1954 
1955 void
1956 CompactibleFreeListSpace::gc_epilogue() {
1957   assert_locked();
1958   if (PrintGCDetails && Verbose && !_adaptive_freelists) {
1959     if (_smallLinearAllocBlock._word_size == 0)
1960       warning("CompactibleFreeListSpace(epilogue):: Linear allocation failure");
1961   }
1962   assert(_promoInfo.noPromotions(), "_promoInfo inconsistency");
1963   _promoInfo.stopTrackingPromotions();
1964   repairLinearAllocationBlocks();
1965   // Print Space's stats
1966   if (PrintFLSStatistics != 0) {
1967     gclog_or_tty->print("After GC:\n");
1968     reportFreeListStatistics();
1969   }
1970 }
1971 
1972 // Iteration support, mostly delegated from a CMS generation
1973 
1974 void CompactibleFreeListSpace::save_marks() {
1975   assert(Thread::current()->is_VM_thread(),
1976          "Global variable should only be set when single-threaded");
1977   // Mark the "end" of the used space at the time of this call;
1978   // note, however, that promoted objects from this point
1979   // on are tracked in the _promoInfo below.
1980   set_saved_mark_word(unallocated_block());
1981 #ifdef ASSERT
1982   // Check the sanity of save_marks() etc.
1983   MemRegion ur    = used_region();
1984   MemRegion urasm = used_region_at_save_marks();
1985   assert(ur.contains(urasm),
1986          err_msg(" Error at save_marks(): [" PTR_FORMAT "," PTR_FORMAT ")"
1987                  " should contain [" PTR_FORMAT "," PTR_FORMAT ")",
1988                  p2i(ur.start()), p2i(ur.end()), p2i(urasm.start()), p2i(urasm.end())));
1989 #endif
1990   // inform allocator that promotions should be tracked.
1991   assert(_promoInfo.noPromotions(), "_promoInfo inconsistency");
1992   _promoInfo.startTrackingPromotions();
1993 }
1994 
1995 bool CompactibleFreeListSpace::no_allocs_since_save_marks() {
1996   assert(_promoInfo.tracking(), "No preceding save_marks?");
1997   assert(SharedHeap::heap()->n_par_threads() == 0,
1998          "Shouldn't be called if using parallel gc.");
1999   return _promoInfo.noPromotions();
2000 }
2001 
2002 #define CFLS_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix)           \
2003                                                                             \
2004 void CompactibleFreeListSpace::                                             \
2005 oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) {              \
2006   assert(SharedHeap::heap()->n_par_threads() == 0,                          \
2007          "Shouldn't be called (yet) during parallel part of gc.");          \
2008   _promoInfo.promoted_oops_iterate##nv_suffix(blk);                         \
2009   /*                                                                        \
2010    * This also restores any displaced headers and removes the elements from \
2011    * the iteration set as they are processed, so that we have a clean slate \
2012    * at the end of the iteration. Note, thus, that if new objects are       \
2013    * promoted as a result of the iteration they are iterated over as well.  \
2014    */                                                                       \
2015   assert(_promoInfo.noPromotions(), "_promoInfo inconsistency");            \
2016 }
2017 
2018 ALL_SINCE_SAVE_MARKS_CLOSURES(CFLS_OOP_SINCE_SAVE_MARKS_DEFN)
2019 
2020 bool CompactibleFreeListSpace::linearAllocationWouldFail() const {
2021   return _smallLinearAllocBlock._word_size == 0;
2022 }
2023 
2024 void CompactibleFreeListSpace::repairLinearAllocationBlocks() {
2025   // Fix up linear allocation blocks to look like free blocks
2026   repairLinearAllocBlock(&_smallLinearAllocBlock);
2027 }
2028 
2029 void CompactibleFreeListSpace::repairLinearAllocBlock(LinearAllocBlock* blk) {
2030   assert_locked();
2031   if (blk->_ptr != NULL) {
2032     assert(blk->_word_size != 0 && blk->_word_size >= MinChunkSize,
2033            "Minimum block size requirement");
2034     FreeChunk* fc = (FreeChunk*)(blk->_ptr);
2035     fc->set_size(blk->_word_size);
2036     fc->link_prev(NULL);   // mark as free
2037     fc->dontCoalesce();
2038     assert(fc->is_free(), "just marked it free");
2039     assert(fc->cantCoalesce(), "just marked it uncoalescable");
2040   }
2041 }
2042 
2043 void CompactibleFreeListSpace::refillLinearAllocBlocksIfNeeded() {
2044   assert_locked();
2045   if (_smallLinearAllocBlock._ptr == NULL) {
2046     assert(_smallLinearAllocBlock._word_size == 0,
2047       "Size of linAB should be zero if the ptr is NULL");
2048     // Reset the linAB refill and allocation size limit.
2049     _smallLinearAllocBlock.set(0, 0, 1024*SmallForLinearAlloc, SmallForLinearAlloc);
2050   }
2051   refillLinearAllocBlockIfNeeded(&_smallLinearAllocBlock);
2052 }
2053 
2054 void
2055 CompactibleFreeListSpace::refillLinearAllocBlockIfNeeded(LinearAllocBlock* blk) {
2056   assert_locked();
2057   assert((blk->_ptr == NULL && blk->_word_size == 0) ||
2058          (blk->_ptr != NULL && blk->_word_size >= MinChunkSize),
2059          "blk invariant");
2060   if (blk->_ptr == NULL) {
2061     refillLinearAllocBlock(blk);
2062   }
2063   if (PrintMiscellaneous && Verbose) {
2064     if (blk->_word_size == 0) {
2065       warning("CompactibleFreeListSpace(prologue):: Linear allocation failure");
2066     }
2067   }
2068 }
2069 
2070 void
2071 CompactibleFreeListSpace::refillLinearAllocBlock(LinearAllocBlock* blk) {
2072   assert_locked();
2073   assert(blk->_word_size == 0 && blk->_ptr == NULL,
2074          "linear allocation block should be empty");
2075   FreeChunk* fc;
2076   if (blk->_refillSize < SmallForDictionary &&
2077       (fc = getChunkFromIndexedFreeList(blk->_refillSize)) != NULL) {
2078     // A linAB's strategy might be to use small sizes to reduce
2079     // fragmentation but still get the benefits of allocation from a
2080     // linAB.
2081   } else {
2082     fc = getChunkFromDictionary(blk->_refillSize);
2083   }
2084   if (fc != NULL) {
2085     blk->_ptr  = (HeapWord*)fc;
2086     blk->_word_size = fc->size();
2087     fc->dontCoalesce();   // to prevent sweeper from sweeping us up
2088   }
2089 }
2090 
2091 // Support for concurrent collection policy decisions.
2092 bool CompactibleFreeListSpace::should_concurrent_collect() const {
2093   // In the future we might want to add in fragmentation stats --
2094   // including erosion of the "mountain" into this decision as well.
2095   return !adaptive_freelists() && linearAllocationWouldFail();
2096 }
2097 
2098 // Support for compaction
2099 
2100 void CompactibleFreeListSpace::prepare_for_compaction(CompactPoint* cp) {
2101   SCAN_AND_FORWARD(cp,end,block_is_obj,block_size);
2102   // Prepare_for_compaction() uses the space between live objects
2103   // so that later phase can skip dead space quickly.  So verification
2104   // of the free lists doesn't work after.
2105 }
2106 
2107 #define obj_size(q) adjustObjectSize(oop(q)->size())
2108 #define adjust_obj_size(s) adjustObjectSize(s)
2109 
2110 void CompactibleFreeListSpace::adjust_pointers() {
2111   // In other versions of adjust_pointers(), a bail out
2112   // based on the amount of live data in the generation
2113   // (i.e., if 0, bail out) may be used.
2114   // Cannot test used() == 0 here because the free lists have already
2115   // been mangled by the compaction.
2116 
2117   SCAN_AND_ADJUST_POINTERS(adjust_obj_size);
2118   // See note about verification in prepare_for_compaction().
2119 }
2120 
2121 void CompactibleFreeListSpace::compact() {
2122   SCAN_AND_COMPACT(obj_size);
2123 }
2124 
2125 // Fragmentation metric = 1 - [sum of (fbs**2) / (sum of fbs)**2]
2126 // where fbs is free block sizes
2127 double CompactibleFreeListSpace::flsFrag() const {
2128   size_t itabFree = totalSizeInIndexedFreeLists();
2129   double frag = 0.0;
2130   size_t i;
2131 
2132   for (i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
2133     double sz  = i;
2134     frag      += _indexedFreeList[i].count() * (sz * sz);
2135   }
2136 
2137   double totFree = itabFree +
2138                    _dictionary->total_chunk_size(DEBUG_ONLY(freelistLock()));
2139   if (totFree > 0) {
2140     frag = ((frag + _dictionary->sum_of_squared_block_sizes()) /
2141             (totFree * totFree));
2142     frag = (double)1.0  - frag;
2143   } else {
2144     assert(frag == 0.0, "Follows from totFree == 0");
2145   }
2146   return frag;
2147 }
2148 
2149 void CompactibleFreeListSpace::beginSweepFLCensus(
2150   float inter_sweep_current,
2151   float inter_sweep_estimate,
2152   float intra_sweep_estimate) {
2153   assert_locked();
2154   size_t i;
2155   for (i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
2156     AdaptiveFreeList<FreeChunk>* fl    = &_indexedFreeList[i];
2157     if (PrintFLSStatistics > 1) {
2158       gclog_or_tty->print("size[" SIZE_FORMAT "] : ", i);
2159     }
2160     fl->compute_desired(inter_sweep_current, inter_sweep_estimate, intra_sweep_estimate);
2161     fl->set_coal_desired((ssize_t)((double)fl->desired() * CMSSmallCoalSurplusPercent));
2162     fl->set_before_sweep(fl->count());
2163     fl->set_bfr_surp(fl->surplus());
2164   }
2165   _dictionary->begin_sweep_dict_census(CMSLargeCoalSurplusPercent,
2166                                     inter_sweep_current,
2167                                     inter_sweep_estimate,
2168                                     intra_sweep_estimate);
2169 }
2170 
2171 void CompactibleFreeListSpace::setFLSurplus() {
2172   assert_locked();
2173   size_t i;
2174   for (i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
2175     AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[i];
2176     fl->set_surplus(fl->count() -
2177                     (ssize_t)((double)fl->desired() * CMSSmallSplitSurplusPercent));
2178   }
2179 }
2180 
2181 void CompactibleFreeListSpace::setFLHints() {
2182   assert_locked();
2183   size_t i;
2184   size_t h = IndexSetSize;
2185   for (i = IndexSetSize - 1; i != 0; i -= IndexSetStride) {
2186     AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[i];
2187     fl->set_hint(h);
2188     if (fl->surplus() > 0) {
2189       h = i;
2190     }
2191   }
2192 }
2193 
2194 void CompactibleFreeListSpace::clearFLCensus() {
2195   assert_locked();
2196   size_t i;
2197   for (i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
2198     AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[i];
2199     fl->set_prev_sweep(fl->count());
2200     fl->set_coal_births(0);
2201     fl->set_coal_deaths(0);
2202     fl->set_split_births(0);
2203     fl->set_split_deaths(0);
2204   }
2205 }
2206 
2207 void CompactibleFreeListSpace::endSweepFLCensus(size_t sweep_count) {
2208   if (PrintFLSStatistics > 0) {
2209     HeapWord* largestAddr = (HeapWord*) dictionary()->find_largest_dict();
2210     gclog_or_tty->print_cr("CMS: Large block " PTR_FORMAT,
2211                            p2i(largestAddr));
2212   }
2213   setFLSurplus();
2214   setFLHints();
2215   if (PrintGC && PrintFLSCensus > 0) {
2216     printFLCensus(sweep_count);
2217   }
2218   clearFLCensus();
2219   assert_locked();
2220   _dictionary->end_sweep_dict_census(CMSLargeSplitSurplusPercent);
2221 }
2222 
2223 bool CompactibleFreeListSpace::coalOverPopulated(size_t size) {
2224   if (size < SmallForDictionary) {
2225     AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[size];
2226     return (fl->coal_desired() < 0) ||
2227            ((int)fl->count() > fl->coal_desired());
2228   } else {
2229     return dictionary()->coal_dict_over_populated(size);
2230   }
2231 }
2232 
2233 void CompactibleFreeListSpace::smallCoalBirth(size_t size) {
2234   assert(size < SmallForDictionary, "Size too large for indexed list");
2235   AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[size];
2236   fl->increment_coal_births();
2237   fl->increment_surplus();
2238 }
2239 
2240 void CompactibleFreeListSpace::smallCoalDeath(size_t size) {
2241   assert(size < SmallForDictionary, "Size too large for indexed list");
2242   AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[size];
2243   fl->increment_coal_deaths();
2244   fl->decrement_surplus();
2245 }
2246 
2247 void CompactibleFreeListSpace::coalBirth(size_t size) {
2248   if (size  < SmallForDictionary) {
2249     smallCoalBirth(size);
2250   } else {
2251     dictionary()->dict_census_update(size,
2252                                    false /* split */,
2253                                    true /* birth */);
2254   }
2255 }
2256 
2257 void CompactibleFreeListSpace::coalDeath(size_t size) {
2258   if(size  < SmallForDictionary) {
2259     smallCoalDeath(size);
2260   } else {
2261     dictionary()->dict_census_update(size,
2262                                    false /* split */,
2263                                    false /* birth */);
2264   }
2265 }
2266 
2267 void CompactibleFreeListSpace::smallSplitBirth(size_t size) {
2268   assert(size < SmallForDictionary, "Size too large for indexed list");
2269   AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[size];
2270   fl->increment_split_births();
2271   fl->increment_surplus();
2272 }
2273 
2274 void CompactibleFreeListSpace::smallSplitDeath(size_t size) {
2275   assert(size < SmallForDictionary, "Size too large for indexed list");
2276   AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[size];
2277   fl->increment_split_deaths();
2278   fl->decrement_surplus();
2279 }
2280 
2281 void CompactibleFreeListSpace::split_birth(size_t size) {
2282   if (size  < SmallForDictionary) {
2283     smallSplitBirth(size);
2284   } else {
2285     dictionary()->dict_census_update(size,
2286                                    true /* split */,
2287                                    true /* birth */);
2288   }
2289 }
2290 
2291 void CompactibleFreeListSpace::splitDeath(size_t size) {
2292   if (size  < SmallForDictionary) {
2293     smallSplitDeath(size);
2294   } else {
2295     dictionary()->dict_census_update(size,
2296                                    true /* split */,
2297                                    false /* birth */);
2298   }
2299 }
2300 
2301 void CompactibleFreeListSpace::split(size_t from, size_t to1) {
2302   size_t to2 = from - to1;
2303   splitDeath(from);
2304   split_birth(to1);
2305   split_birth(to2);
2306 }
2307 
2308 void CompactibleFreeListSpace::print() const {
2309   print_on(tty);
2310 }
2311 
2312 void CompactibleFreeListSpace::prepare_for_verify() {
2313   assert_locked();
2314   repairLinearAllocationBlocks();
2315   // Verify that the SpoolBlocks look like free blocks of
2316   // appropriate sizes... To be done ...
2317 }
2318 
2319 class VerifyAllBlksClosure: public BlkClosure {
2320  private:
2321   const CompactibleFreeListSpace* _sp;
2322   const MemRegion                 _span;
2323   HeapWord*                       _last_addr;
2324   size_t                          _last_size;
2325   bool                            _last_was_obj;
2326   bool                            _last_was_live;
2327 
2328  public:
2329   VerifyAllBlksClosure(const CompactibleFreeListSpace* sp,
2330     MemRegion span) :  _sp(sp), _span(span),
2331                        _last_addr(NULL), _last_size(0),
2332                        _last_was_obj(false), _last_was_live(false) { }
2333 
2334   virtual size_t do_blk(HeapWord* addr) {
2335     size_t res;
2336     bool   was_obj  = false;
2337     bool   was_live = false;
2338     if (_sp->block_is_obj(addr)) {
2339       was_obj = true;
2340       oop p = oop(addr);
2341       guarantee(p->is_oop(), "Should be an oop");
2342       res = _sp->adjustObjectSize(p->size());
2343       if (_sp->obj_is_alive(addr)) {
2344         was_live = true;
2345         p->verify();
2346       }
2347     } else {
2348       FreeChunk* fc = (FreeChunk*)addr;
2349       res = fc->size();
2350       if (FLSVerifyLists && !fc->cantCoalesce()) {
2351         guarantee(_sp->verify_chunk_in_free_list(fc),
2352                   "Chunk should be on a free list");
2353       }
2354     }
2355     if (res == 0) {
2356       gclog_or_tty->print_cr("Livelock: no rank reduction!");
2357       gclog_or_tty->print_cr(
2358         " Current:  addr = " PTR_FORMAT ", size = " SIZE_FORMAT ", obj = %s, live = %s \n"
2359         " Previous: addr = " PTR_FORMAT ", size = " SIZE_FORMAT ", obj = %s, live = %s \n",
2360         p2i(addr),       res,        was_obj      ?"true":"false", was_live      ?"true":"false",
2361         p2i(_last_addr), _last_size, _last_was_obj?"true":"false", _last_was_live?"true":"false");
2362       _sp->print_on(gclog_or_tty);
2363       guarantee(false, "Seppuku!");
2364     }
2365     _last_addr = addr;
2366     _last_size = res;
2367     _last_was_obj  = was_obj;
2368     _last_was_live = was_live;
2369     return res;
2370   }
2371 };
2372 
2373 class VerifyAllOopsClosure: public OopClosure {
2374  private:
2375   const CMSCollector*             _collector;
2376   const CompactibleFreeListSpace* _sp;
2377   const MemRegion                 _span;
2378   const bool                      _past_remark;
2379   const CMSBitMap*                _bit_map;
2380 
2381  protected:
2382   void do_oop(void* p, oop obj) {
2383     if (_span.contains(obj)) { // the interior oop points into CMS heap
2384       if (!_span.contains(p)) { // reference from outside CMS heap
2385         // Should be a valid object; the first disjunct below allows
2386         // us to sidestep an assertion in block_is_obj() that insists
2387         // that p be in _sp. Note that several generations (and spaces)
2388         // are spanned by _span (CMS heap) above.
2389         guarantee(!_sp->is_in_reserved(obj) ||
2390                   _sp->block_is_obj((HeapWord*)obj),
2391                   "Should be an object");
2392         guarantee(obj->is_oop(), "Should be an oop");
2393         obj->verify();
2394         if (_past_remark) {
2395           // Remark has been completed, the object should be marked
2396           _bit_map->isMarked((HeapWord*)obj);
2397         }
2398       } else { // reference within CMS heap
2399         if (_past_remark) {
2400           // Remark has been completed -- so the referent should have
2401           // been marked, if referring object is.
2402           if (_bit_map->isMarked(_collector->block_start(p))) {
2403             guarantee(_bit_map->isMarked((HeapWord*)obj), "Marking error?");
2404           }
2405         }
2406       }
2407     } else if (_sp->is_in_reserved(p)) {
2408       // the reference is from FLS, and points out of FLS
2409       guarantee(obj->is_oop(), "Should be an oop");
2410       obj->verify();
2411     }
2412   }
2413 
2414   template <class T> void do_oop_work(T* p) {
2415     T heap_oop = oopDesc::load_heap_oop(p);
2416     if (!oopDesc::is_null(heap_oop)) {
2417       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
2418       do_oop(p, obj);
2419     }
2420   }
2421 
2422  public:
2423   VerifyAllOopsClosure(const CMSCollector* collector,
2424     const CompactibleFreeListSpace* sp, MemRegion span,
2425     bool past_remark, CMSBitMap* bit_map) :
2426     _collector(collector), _sp(sp), _span(span),
2427     _past_remark(past_remark), _bit_map(bit_map) { }
2428 
2429   virtual void do_oop(oop* p)       { VerifyAllOopsClosure::do_oop_work(p); }
2430   virtual void do_oop(narrowOop* p) { VerifyAllOopsClosure::do_oop_work(p); }
2431 };
2432 
2433 void CompactibleFreeListSpace::verify() const {
2434   assert_lock_strong(&_freelistLock);
2435   verify_objects_initialized();
2436   MemRegion span = _collector->_span;
2437   bool past_remark = (_collector->abstract_state() ==
2438                       CMSCollector::Sweeping);
2439 
2440   ResourceMark rm;
2441   HandleMark  hm;
2442 
2443   // Check integrity of CFL data structures
2444   _promoInfo.verify();
2445   _dictionary->verify();
2446   if (FLSVerifyIndexTable) {
2447     verifyIndexedFreeLists();
2448   }
2449   // Check integrity of all objects and free blocks in space
2450   {
2451     VerifyAllBlksClosure cl(this, span);
2452     ((CompactibleFreeListSpace*)this)->blk_iterate(&cl);  // cast off const
2453   }
2454   // Check that all references in the heap to FLS
2455   // are to valid objects in FLS or that references in
2456   // FLS are to valid objects elsewhere in the heap
2457   if (FLSVerifyAllHeapReferences)
2458   {
2459     VerifyAllOopsClosure cl(_collector, this, span, past_remark,
2460       _collector->markBitMap());
2461     CollectedHeap* ch = Universe::heap();
2462 
2463     // Iterate over all oops in the heap. Uses the _no_header version
2464     // since we are not interested in following the klass pointers.
2465     ch->oop_iterate_no_header(&cl);
2466   }
2467 
2468   if (VerifyObjectStartArray) {
2469     // Verify the block offset table
2470     _bt.verify();
2471   }
2472 }
2473 
2474 #ifndef PRODUCT
2475 void CompactibleFreeListSpace::verifyFreeLists() const {
2476   if (FLSVerifyLists) {
2477     _dictionary->verify();
2478     verifyIndexedFreeLists();
2479   } else {
2480     if (FLSVerifyDictionary) {
2481       _dictionary->verify();
2482     }
2483     if (FLSVerifyIndexTable) {
2484       verifyIndexedFreeLists();
2485     }
2486   }
2487 }
2488 #endif
2489 
2490 void CompactibleFreeListSpace::verifyIndexedFreeLists() const {
2491   size_t i = 0;
2492   for (; i < IndexSetStart; i++) {
2493     guarantee(_indexedFreeList[i].head() == NULL, "should be NULL");
2494   }
2495   for (; i < IndexSetSize; i++) {
2496     verifyIndexedFreeList(i);
2497   }
2498 }
2499 
2500 void CompactibleFreeListSpace::verifyIndexedFreeList(size_t size) const {
2501   FreeChunk* fc   =  _indexedFreeList[size].head();
2502   FreeChunk* tail =  _indexedFreeList[size].tail();
2503   size_t    num = _indexedFreeList[size].count();
2504   size_t      n = 0;
2505   guarantee(((size >= IndexSetStart) && (size % IndexSetStride == 0)) || fc == NULL,
2506             "Slot should have been empty");
2507   for (; fc != NULL; fc = fc->next(), n++) {
2508     guarantee(fc->size() == size, "Size inconsistency");
2509     guarantee(fc->is_free(), "!free?");
2510     guarantee(fc->next() == NULL || fc->next()->prev() == fc, "Broken list");
2511     guarantee((fc->next() == NULL) == (fc == tail), "Incorrect tail");
2512   }
2513   guarantee(n == num, "Incorrect count");
2514 }
2515 
2516 #ifndef PRODUCT
2517 void CompactibleFreeListSpace::check_free_list_consistency() const {
2518   assert((TreeChunk<FreeChunk, AdaptiveFreeList<FreeChunk> >::min_size() <= IndexSetSize),
2519     "Some sizes can't be allocated without recourse to"
2520     " linear allocation buffers");
2521   assert((TreeChunk<FreeChunk, AdaptiveFreeList<FreeChunk> >::min_size()*HeapWordSize == sizeof(TreeChunk<FreeChunk, AdaptiveFreeList<FreeChunk> >)),
2522     "else MIN_TREE_CHUNK_SIZE is wrong");
2523   assert(IndexSetStart != 0, "IndexSetStart not initialized");
2524   assert(IndexSetStride != 0, "IndexSetStride not initialized");
2525 }
2526 #endif
2527 
2528 void CompactibleFreeListSpace::printFLCensus(size_t sweep_count) const {
2529   assert_lock_strong(&_freelistLock);
2530   AdaptiveFreeList<FreeChunk> total;
2531   gclog_or_tty->print("end sweep# " SIZE_FORMAT "\n", sweep_count);
2532   AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, "size");
2533   size_t total_free = 0;
2534   for (size_t i = IndexSetStart; i < IndexSetSize; i += IndexSetStride) {
2535     const AdaptiveFreeList<FreeChunk> *fl = &_indexedFreeList[i];
2536     total_free += fl->count() * fl->size();
2537     if (i % (40*IndexSetStride) == 0) {
2538       AdaptiveFreeList<FreeChunk>::print_labels_on(gclog_or_tty, "size");
2539     }
2540     fl->print_on(gclog_or_tty);
2541     total.set_bfr_surp(    total.bfr_surp()     + fl->bfr_surp()    );
2542     total.set_surplus(    total.surplus()     + fl->surplus()    );
2543     total.set_desired(    total.desired()     + fl->desired()    );
2544     total.set_prev_sweep(  total.prev_sweep()   + fl->prev_sweep()  );
2545     total.set_before_sweep(total.before_sweep() + fl->before_sweep());
2546     total.set_count(      total.count()       + fl->count()      );
2547     total.set_coal_births( total.coal_births()  + fl->coal_births() );
2548     total.set_coal_deaths( total.coal_deaths()  + fl->coal_deaths() );
2549     total.set_split_births(total.split_births() + fl->split_births());
2550     total.set_split_deaths(total.split_deaths() + fl->split_deaths());
2551   }
2552   total.print_on(gclog_or_tty, "TOTAL");
2553   gclog_or_tty->print_cr("Total free in indexed lists "
2554                          SIZE_FORMAT " words", total_free);
2555   gclog_or_tty->print("growth: %8.5f  deficit: %8.5f\n",
2556     (double)(total.split_births()+total.coal_births()-total.split_deaths()-total.coal_deaths())/
2557             (total.prev_sweep() != 0 ? (double)total.prev_sweep() : 1.0),
2558     (double)(total.desired() - total.count())/(total.desired() != 0 ? (double)total.desired() : 1.0));
2559   _dictionary->print_dict_census();
2560 }
2561 
2562 ///////////////////////////////////////////////////////////////////////////
2563 // CFLS_LAB
2564 ///////////////////////////////////////////////////////////////////////////
2565 
2566 #define VECTOR_257(x)                                                                                  \
2567   /* 1  2  3  4  5  6  7  8  9 1x 11 12 13 14 15 16 17 18 19 2x 21 22 23 24 25 26 27 28 29 3x 31 32 */ \
2568   {  x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,   \
2569      x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,   \
2570      x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,   \
2571      x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,   \
2572      x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,   \
2573      x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,   \
2574      x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,   \
2575      x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,   \
2576      x }
2577 
2578 // Initialize with default setting of CMSParPromoteBlocksToClaim, _not_
2579 // OldPLABSize, whose static default is different; if overridden at the
2580 // command-line, this will get reinitialized via a call to
2581 // modify_initialization() below.
2582 AdaptiveWeightedAverage CFLS_LAB::_blocks_to_claim[]    =
2583   VECTOR_257(AdaptiveWeightedAverage(OldPLABWeight, (float)CMSParPromoteBlocksToClaim));
2584 size_t CFLS_LAB::_global_num_blocks[]  = VECTOR_257(0);
2585 uint   CFLS_LAB::_global_num_workers[] = VECTOR_257(0);
2586 
2587 CFLS_LAB::CFLS_LAB(CompactibleFreeListSpace* cfls) :
2588   _cfls(cfls)
2589 {
2590   assert(CompactibleFreeListSpace::IndexSetSize == 257, "Modify VECTOR_257() macro above");
2591   for (size_t i = CompactibleFreeListSpace::IndexSetStart;
2592        i < CompactibleFreeListSpace::IndexSetSize;
2593        i += CompactibleFreeListSpace::IndexSetStride) {
2594     _indexedFreeList[i].set_size(i);
2595     _num_blocks[i] = 0;
2596   }
2597 }
2598 
2599 static bool _CFLS_LAB_modified = false;
2600 
2601 void CFLS_LAB::modify_initialization(size_t n, unsigned wt) {
2602   assert(!_CFLS_LAB_modified, "Call only once");
2603   _CFLS_LAB_modified = true;
2604   for (size_t i = CompactibleFreeListSpace::IndexSetStart;
2605        i < CompactibleFreeListSpace::IndexSetSize;
2606        i += CompactibleFreeListSpace::IndexSetStride) {
2607     _blocks_to_claim[i].modify(n, wt, true /* force */);
2608   }
2609 }
2610 
2611 HeapWord* CFLS_LAB::alloc(size_t word_sz) {
2612   FreeChunk* res;
2613   assert(word_sz == _cfls->adjustObjectSize(word_sz), "Error");
2614   if (word_sz >=  CompactibleFreeListSpace::IndexSetSize) {
2615     // This locking manages sync with other large object allocations.
2616     MutexLockerEx x(_cfls->parDictionaryAllocLock(),
2617                     Mutex::_no_safepoint_check_flag);
2618     res = _cfls->getChunkFromDictionaryExact(word_sz);
2619     if (res == NULL) return NULL;
2620   } else {
2621     AdaptiveFreeList<FreeChunk>* fl = &_indexedFreeList[word_sz];
2622     if (fl->count() == 0) {
2623       // Attempt to refill this local free list.
2624       get_from_global_pool(word_sz, fl);
2625       // If it didn't work, give up.
2626       if (fl->count() == 0) return NULL;
2627     }
2628     res = fl->get_chunk_at_head();
2629     assert(res != NULL, "Why was count non-zero?");
2630   }
2631   res->markNotFree();
2632   assert(!res->is_free(), "shouldn't be marked free");
2633   assert(oop(res)->klass_or_null() == NULL, "should look uninitialized");
2634   // mangle a just allocated object with a distinct pattern.
2635   debug_only(res->mangleAllocated(word_sz));
2636   return (HeapWord*)res;
2637 }
2638 
2639 // Get a chunk of blocks of the right size and update related
2640 // book-keeping stats
2641 void CFLS_LAB::get_from_global_pool(size_t word_sz, AdaptiveFreeList<FreeChunk>* fl) {
2642   // Get the #blocks we want to claim
2643   size_t n_blks = (size_t)_blocks_to_claim[word_sz].average();
2644   assert(n_blks > 0, "Error");
2645   assert(ResizePLAB || n_blks == OldPLABSize, "Error");
2646   // In some cases, when the application has a phase change,
2647   // there may be a sudden and sharp shift in the object survival
2648   // profile, and updating the counts at the end of a scavenge
2649   // may not be quick enough, giving rise to large scavenge pauses
2650   // during these phase changes. It is beneficial to detect such
2651   // changes on-the-fly during a scavenge and avoid such a phase-change
2652   // pothole. The following code is a heuristic attempt to do that.
2653   // It is protected by a product flag until we have gained
2654   // enough experience with this heuristic and fine-tuned its behavior.
2655   // WARNING: This might increase fragmentation if we overreact to
2656   // small spikes, so some kind of historical smoothing based on
2657   // previous experience with the greater reactivity might be useful.
2658   // Lacking sufficient experience, CMSOldPLABResizeQuicker is disabled by
2659   // default.
2660   if (ResizeOldPLAB && CMSOldPLABResizeQuicker) {
2661     size_t multiple = _num_blocks[word_sz]/(CMSOldPLABToleranceFactor*CMSOldPLABNumRefills*n_blks);
2662     n_blks +=  CMSOldPLABReactivityFactor*multiple*n_blks;
2663     n_blks = MIN2(n_blks, CMSOldPLABMax);
2664   }
2665   assert(n_blks > 0, "Error");
2666   _cfls->par_get_chunk_of_blocks(word_sz, n_blks, fl);
2667   // Update stats table entry for this block size
2668   _num_blocks[word_sz] += fl->count();
2669 }
2670 
2671 void CFLS_LAB::compute_desired_plab_size() {
2672   for (size_t i =  CompactibleFreeListSpace::IndexSetStart;
2673        i < CompactibleFreeListSpace::IndexSetSize;
2674        i += CompactibleFreeListSpace::IndexSetStride) {
2675     assert((_global_num_workers[i] == 0) == (_global_num_blocks[i] == 0),
2676            "Counter inconsistency");
2677     if (_global_num_workers[i] > 0) {
2678       // Need to smooth wrt historical average
2679       if (ResizeOldPLAB) {
2680         _blocks_to_claim[i].sample(
2681           MAX2((size_t)CMSOldPLABMin,
2682           MIN2((size_t)CMSOldPLABMax,
2683                _global_num_blocks[i]/(_global_num_workers[i]*CMSOldPLABNumRefills))));
2684       }
2685       // Reset counters for next round
2686       _global_num_workers[i] = 0;
2687       _global_num_blocks[i] = 0;
2688       if (PrintOldPLAB) {
2689         gclog_or_tty->print_cr("[" SIZE_FORMAT "]: " SIZE_FORMAT,
2690                                i, (size_t)_blocks_to_claim[i].average());
2691       }
2692     }
2693   }
2694 }
2695 
2696 // If this is changed in the future to allow parallel
2697 // access, one would need to take the FL locks and,
2698 // depending on how it is used, stagger access from
2699 // parallel threads to reduce contention.
2700 void CFLS_LAB::retire(int tid) {
2701   // We run this single threaded with the world stopped;
2702   // so no need for locks and such.
2703   NOT_PRODUCT(Thread* t = Thread::current();)
2704   assert(Thread::current()->is_VM_thread(), "Error");
2705   for (size_t i =  CompactibleFreeListSpace::IndexSetStart;
2706        i < CompactibleFreeListSpace::IndexSetSize;
2707        i += CompactibleFreeListSpace::IndexSetStride) {
2708     assert(_num_blocks[i] >= (size_t)_indexedFreeList[i].count(),
2709            "Can't retire more than what we obtained");
2710     if (_num_blocks[i] > 0) {
2711       size_t num_retire =  _indexedFreeList[i].count();
2712       assert(_num_blocks[i] > num_retire, "Should have used at least one");
2713       {
2714         // MutexLockerEx x(_cfls->_indexedFreeListParLocks[i],
2715         //                Mutex::_no_safepoint_check_flag);
2716 
2717         // Update globals stats for num_blocks used
2718         _global_num_blocks[i] += (_num_blocks[i] - num_retire);
2719         _global_num_workers[i]++;
2720         assert(_global_num_workers[i] <= ParallelGCThreads, "Too big");
2721         if (num_retire > 0) {
2722           _cfls->_indexedFreeList[i].prepend(&_indexedFreeList[i]);
2723           // Reset this list.
2724           _indexedFreeList[i] = AdaptiveFreeList<FreeChunk>();
2725           _indexedFreeList[i].set_size(i);
2726         }
2727       }
2728       if (PrintOldPLAB) {
2729         gclog_or_tty->print_cr("%d[" SIZE_FORMAT "]: " SIZE_FORMAT "/" SIZE_FORMAT "/" SIZE_FORMAT,
2730                                tid, i, num_retire, _num_blocks[i], (size_t)_blocks_to_claim[i].average());
2731       }
2732       // Reset stats for next round
2733       _num_blocks[i]         = 0;
2734     }
2735   }
2736 }
2737 
2738 // Used by par_get_chunk_of_blocks() for the chunks from the
2739 // indexed_free_lists.  Looks for a chunk with size that is a multiple
2740 // of "word_sz" and if found, splits it into "word_sz" chunks and add
2741 // to the free list "fl".  "n" is the maximum number of chunks to
2742 // be added to "fl".
2743 bool CompactibleFreeListSpace:: par_get_chunk_of_blocks_IFL(size_t word_sz, size_t n, AdaptiveFreeList<FreeChunk>* fl) {
2744 
2745   // We'll try all multiples of word_sz in the indexed set, starting with
2746   // word_sz itself and, if CMSSplitIndexedFreeListBlocks, try larger multiples,
2747   // then try getting a big chunk and splitting it.
2748   {
2749     bool found;
2750     int  k;
2751     size_t cur_sz;
2752     for (k = 1, cur_sz = k * word_sz, found = false;
2753          (cur_sz < CompactibleFreeListSpace::IndexSetSize) &&
2754          (CMSSplitIndexedFreeListBlocks || k <= 1);
2755          k++, cur_sz = k * word_sz) {
2756       AdaptiveFreeList<FreeChunk> fl_for_cur_sz;  // Empty.
2757       fl_for_cur_sz.set_size(cur_sz);
2758       {
2759         MutexLockerEx x(_indexedFreeListParLocks[cur_sz],
2760                         Mutex::_no_safepoint_check_flag);
2761         AdaptiveFreeList<FreeChunk>* gfl = &_indexedFreeList[cur_sz];
2762         if (gfl->count() != 0) {
2763           // nn is the number of chunks of size cur_sz that
2764           // we'd need to split k-ways each, in order to create
2765           // "n" chunks of size word_sz each.
2766           const size_t nn = MAX2(n/k, (size_t)1);
2767           gfl->getFirstNChunksFromList(nn, &fl_for_cur_sz);
2768           found = true;
2769           if (k > 1) {
2770             // Update split death stats for the cur_sz-size blocks list:
2771             // we increment the split death count by the number of blocks
2772             // we just took from the cur_sz-size blocks list and which
2773             // we will be splitting below.
2774             ssize_t deaths = gfl->split_deaths() +
2775                              fl_for_cur_sz.count();
2776             gfl->set_split_deaths(deaths);
2777           }
2778         }
2779       }
2780       // Now transfer fl_for_cur_sz to fl.  Common case, we hope, is k = 1.
2781       if (found) {
2782         if (k == 1) {
2783           fl->prepend(&fl_for_cur_sz);
2784         } else {
2785           // Divide each block on fl_for_cur_sz up k ways.
2786           FreeChunk* fc;
2787           while ((fc = fl_for_cur_sz.get_chunk_at_head()) != NULL) {
2788             // Must do this in reverse order, so that anybody attempting to
2789             // access the main chunk sees it as a single free block until we
2790             // change it.
2791             size_t fc_size = fc->size();
2792             assert(fc->is_free(), "Error");
2793             for (int i = k-1; i >= 0; i--) {
2794               FreeChunk* ffc = (FreeChunk*)((HeapWord*)fc + i * word_sz);
2795               assert((i != 0) ||
2796                         ((fc == ffc) && ffc->is_free() &&
2797                          (ffc->size() == k*word_sz) && (fc_size == word_sz)),
2798                         "Counting error");
2799               ffc->set_size(word_sz);
2800               ffc->link_prev(NULL); // Mark as a free block for other (parallel) GC threads.
2801               ffc->link_next(NULL);
2802               // Above must occur before BOT is updated below.
2803               OrderAccess::storestore();
2804               // splitting from the right, fc_size == i * word_sz
2805               _bt.mark_block((HeapWord*)ffc, word_sz, true /* reducing */);
2806               fc_size -= word_sz;
2807               assert(fc_size == i*word_sz, "Error");
2808               _bt.verify_not_unallocated((HeapWord*)ffc, word_sz);
2809               _bt.verify_single_block((HeapWord*)fc, fc_size);
2810               _bt.verify_single_block((HeapWord*)ffc, word_sz);
2811               // Push this on "fl".
2812               fl->return_chunk_at_head(ffc);
2813             }
2814             // TRAP
2815             assert(fl->tail()->next() == NULL, "List invariant.");
2816           }
2817         }
2818         // Update birth stats for this block size.
2819         size_t num = fl->count();
2820         MutexLockerEx x(_indexedFreeListParLocks[word_sz],
2821                         Mutex::_no_safepoint_check_flag);
2822         ssize_t births = _indexedFreeList[word_sz].split_births() + num;
2823         _indexedFreeList[word_sz].set_split_births(births);
2824         return true;
2825       }
2826     }
2827     return found;
2828   }
2829 }
2830 
2831 FreeChunk* CompactibleFreeListSpace::get_n_way_chunk_to_split(size_t word_sz, size_t n) {
2832 
2833   FreeChunk* fc = NULL;
2834   FreeChunk* rem_fc = NULL;
2835   size_t rem;
2836   {
2837     MutexLockerEx x(parDictionaryAllocLock(),
2838                     Mutex::_no_safepoint_check_flag);
2839     while (n > 0) {
2840       fc = dictionary()->get_chunk(MAX2(n * word_sz, _dictionary->min_size()),
2841                                   FreeBlockDictionary<FreeChunk>::atLeast);
2842       if (fc != NULL) {
2843         break;
2844       } else {
2845         n--;
2846       }
2847     }
2848     if (fc == NULL) return NULL;
2849     // Otherwise, split up that block.
2850     assert((ssize_t)n >= 1, "Control point invariant");
2851     assert(fc->is_free(), "Error: should be a free block");
2852     _bt.verify_single_block((HeapWord*)fc, fc->size());
2853     const size_t nn = fc->size() / word_sz;
2854     n = MIN2(nn, n);
2855     assert((ssize_t)n >= 1, "Control point invariant");
2856     rem = fc->size() - n * word_sz;
2857     // If there is a remainder, and it's too small, allocate one fewer.
2858     if (rem > 0 && rem < MinChunkSize) {
2859       n--; rem += word_sz;
2860     }
2861     // Note that at this point we may have n == 0.
2862     assert((ssize_t)n >= 0, "Control point invariant");
2863 
2864     // If n is 0, the chunk fc that was found is not large
2865     // enough to leave a viable remainder.  We are unable to
2866     // allocate even one block.  Return fc to the
2867     // dictionary and return, leaving "fl" empty.
2868     if (n == 0) {
2869       returnChunkToDictionary(fc);
2870       return NULL;
2871     }
2872 
2873     _bt.allocated((HeapWord*)fc, fc->size(), true /* reducing */);  // update _unallocated_blk
2874     dictionary()->dict_census_update(fc->size(),
2875                                      true /*split*/,
2876                                      false /*birth*/);
2877 
2878     // First return the remainder, if any.
2879     // Note that we hold the lock until we decide if we're going to give
2880     // back the remainder to the dictionary, since a concurrent allocation
2881     // may otherwise see the heap as empty.  (We're willing to take that
2882     // hit if the block is a small block.)
2883     if (rem > 0) {
2884       size_t prefix_size = n * word_sz;
2885       rem_fc = (FreeChunk*)((HeapWord*)fc + prefix_size);
2886       rem_fc->set_size(rem);
2887       rem_fc->link_prev(NULL); // Mark as a free block for other (parallel) GC threads.
2888       rem_fc->link_next(NULL);
2889       // Above must occur before BOT is updated below.
2890       assert((ssize_t)n > 0 && prefix_size > 0 && rem_fc > fc, "Error");
2891       OrderAccess::storestore();
2892       _bt.split_block((HeapWord*)fc, fc->size(), prefix_size);
2893       assert(fc->is_free(), "Error");
2894       fc->set_size(prefix_size);
2895       if (rem >= IndexSetSize) {
2896         returnChunkToDictionary(rem_fc);
2897         dictionary()->dict_census_update(rem, true /*split*/, true /*birth*/);
2898         rem_fc = NULL;
2899       }
2900       // Otherwise, return it to the small list below.
2901     }
2902   }
2903   if (rem_fc != NULL) {
2904     MutexLockerEx x(_indexedFreeListParLocks[rem],
2905                     Mutex::_no_safepoint_check_flag);
2906     _bt.verify_not_unallocated((HeapWord*)rem_fc, rem_fc->size());
2907     _indexedFreeList[rem].return_chunk_at_head(rem_fc);
2908     smallSplitBirth(rem);
2909   }
2910   assert(n * word_sz == fc->size(),
2911     err_msg("Chunk size " SIZE_FORMAT " is not exactly splittable by "
2912     SIZE_FORMAT " sized chunks of size " SIZE_FORMAT,
2913     fc->size(), n, word_sz));
2914   return fc;
2915 }
2916 
2917 void CompactibleFreeListSpace:: par_get_chunk_of_blocks_dictionary(size_t word_sz, size_t targetted_number_of_chunks, AdaptiveFreeList<FreeChunk>* fl) {
2918 
2919   FreeChunk* fc = get_n_way_chunk_to_split(word_sz, targetted_number_of_chunks);
2920 
2921   if (fc == NULL) {
2922     return;
2923   }
2924 
2925   size_t n = fc->size() / word_sz;
2926 
2927   assert((ssize_t)n > 0, "Consistency");
2928   // Now do the splitting up.
2929   // Must do this in reverse order, so that anybody attempting to
2930   // access the main chunk sees it as a single free block until we
2931   // change it.
2932   size_t fc_size = n * word_sz;
2933   // All but first chunk in this loop
2934   for (ssize_t i = n-1; i > 0; i--) {
2935     FreeChunk* ffc = (FreeChunk*)((HeapWord*)fc + i * word_sz);
2936     ffc->set_size(word_sz);
2937     ffc->link_prev(NULL); // Mark as a free block for other (parallel) GC threads.
2938     ffc->link_next(NULL);
2939     // Above must occur before BOT is updated below.
2940     OrderAccess::storestore();
2941     // splitting from the right, fc_size == (n - i + 1) * wordsize
2942     _bt.mark_block((HeapWord*)ffc, word_sz, true /* reducing */);
2943     fc_size -= word_sz;
2944     _bt.verify_not_unallocated((HeapWord*)ffc, ffc->size());
2945     _bt.verify_single_block((HeapWord*)ffc, ffc->size());
2946     _bt.verify_single_block((HeapWord*)fc, fc_size);
2947     // Push this on "fl".
2948     fl->return_chunk_at_head(ffc);
2949   }
2950   // First chunk
2951   assert(fc->is_free() && fc->size() == n*word_sz, "Error: should still be a free block");
2952   // The blocks above should show their new sizes before the first block below
2953   fc->set_size(word_sz);
2954   fc->link_prev(NULL);    // idempotent wrt free-ness, see assert above
2955   fc->link_next(NULL);
2956   _bt.verify_not_unallocated((HeapWord*)fc, fc->size());
2957   _bt.verify_single_block((HeapWord*)fc, fc->size());
2958   fl->return_chunk_at_head(fc);
2959 
2960   assert((ssize_t)n > 0 && (ssize_t)n == fl->count(), "Incorrect number of blocks");
2961   {
2962     // Update the stats for this block size.
2963     MutexLockerEx x(_indexedFreeListParLocks[word_sz],
2964                     Mutex::_no_safepoint_check_flag);
2965     const ssize_t births = _indexedFreeList[word_sz].split_births() + n;
2966     _indexedFreeList[word_sz].set_split_births(births);
2967     // ssize_t new_surplus = _indexedFreeList[word_sz].surplus() + n;
2968     // _indexedFreeList[word_sz].set_surplus(new_surplus);
2969   }
2970 
2971   // TRAP
2972   assert(fl->tail()->next() == NULL, "List invariant.");
2973 }
2974 
2975 void CompactibleFreeListSpace:: par_get_chunk_of_blocks(size_t word_sz, size_t n, AdaptiveFreeList<FreeChunk>* fl) {
2976   assert(fl->count() == 0, "Precondition.");
2977   assert(word_sz < CompactibleFreeListSpace::IndexSetSize,
2978          "Precondition");
2979 
2980   if (par_get_chunk_of_blocks_IFL(word_sz, n, fl)) {
2981     // Got it
2982     return;
2983   }
2984 
2985   // Otherwise, we'll split a block from the dictionary.
2986   par_get_chunk_of_blocks_dictionary(word_sz, n, fl);
2987 }
2988 
2989 // Set up the space's par_seq_tasks structure for work claiming
2990 // for parallel rescan. See CMSParRemarkTask where this is currently used.
2991 // XXX Need to suitably abstract and generalize this and the next
2992 // method into one.
2993 void
2994 CompactibleFreeListSpace::
2995 initialize_sequential_subtasks_for_rescan(int n_threads) {
2996   // The "size" of each task is fixed according to rescan_task_size.
2997   assert(n_threads > 0, "Unexpected n_threads argument");
2998   const size_t task_size = rescan_task_size();
2999   size_t n_tasks = (used_region().word_size() + task_size - 1)/task_size;
3000   assert((n_tasks == 0) == used_region().is_empty(), "n_tasks incorrect");
3001   assert(n_tasks == 0 ||
3002          ((used_region().start() + (n_tasks - 1)*task_size < used_region().end()) &&
3003           (used_region().start() + n_tasks*task_size >= used_region().end())),
3004          "n_tasks calculation incorrect");
3005   SequentialSubTasksDone* pst = conc_par_seq_tasks();
3006   assert(!pst->valid(), "Clobbering existing data?");
3007   // Sets the condition for completion of the subtask (how many threads
3008   // need to finish in order to be done).
3009   pst->set_n_threads(n_threads);
3010   pst->set_n_tasks((int)n_tasks);
3011 }
3012 
3013 // Set up the space's par_seq_tasks structure for work claiming
3014 // for parallel concurrent marking. See CMSConcMarkTask where this is currently used.
3015 void
3016 CompactibleFreeListSpace::
3017 initialize_sequential_subtasks_for_marking(int n_threads,
3018                                            HeapWord* low) {
3019   // The "size" of each task is fixed according to rescan_task_size.
3020   assert(n_threads > 0, "Unexpected n_threads argument");
3021   const size_t task_size = marking_task_size();
3022   assert(task_size > CardTableModRefBS::card_size_in_words &&
3023          (task_size %  CardTableModRefBS::card_size_in_words == 0),
3024          "Otherwise arithmetic below would be incorrect");
3025   MemRegion span = _gen->reserved();
3026   if (low != NULL) {
3027     if (span.contains(low)) {
3028       // Align low down to  a card boundary so that
3029       // we can use block_offset_careful() on span boundaries.
3030       HeapWord* aligned_low = (HeapWord*)align_size_down((uintptr_t)low,
3031                                  CardTableModRefBS::card_size);
3032       // Clip span prefix at aligned_low
3033       span = span.intersection(MemRegion(aligned_low, span.end()));
3034     } else if (low > span.end()) {
3035       span = MemRegion(low, low);  // Null region
3036     } // else use entire span
3037   }
3038   assert(span.is_empty() ||
3039          ((uintptr_t)span.start() %  CardTableModRefBS::card_size == 0),
3040         "span should start at a card boundary");
3041   size_t n_tasks = (span.word_size() + task_size - 1)/task_size;
3042   assert((n_tasks == 0) == span.is_empty(), "Inconsistency");
3043   assert(n_tasks == 0 ||
3044          ((span.start() + (n_tasks - 1)*task_size < span.end()) &&
3045           (span.start() + n_tasks*task_size >= span.end())),
3046          "n_tasks calculation incorrect");
3047   SequentialSubTasksDone* pst = conc_par_seq_tasks();
3048   assert(!pst->valid(), "Clobbering existing data?");
3049   // Sets the condition for completion of the subtask (how many threads
3050   // need to finish in order to be done).
3051   pst->set_n_threads(n_threads);
3052   pst->set_n_tasks((int)n_tasks);
3053 }