1 /*
   2  * Copyright (c) 2012, 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/adaptiveFreeList.hpp"
  27 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
  28 #include "memory/freeBlockDictionary.hpp"
  29 #include "memory/sharedHeap.hpp"
  30 #include "runtime/globals.hpp"
  31 #include "runtime/mutex.hpp"
  32 #include "runtime/vmThread.hpp"
  33 
  34 template <>
  35 void AdaptiveFreeList<FreeChunk>::print_on(outputStream* st, const char* c) const {
  36   if (c != NULL) {
  37     st->print("%16s", c);
  38   } else {
  39     st->print(SIZE_FORMAT_W(16), size());
  40   }
  41   st->print("\t"
  42            SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t"
  43            SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\n",
  44            bfr_surp(),             surplus(),             desired(),             prev_sweep(),           before_sweep(),
  45            count(),               coal_births(),          coal_deaths(),          split_births(),         split_deaths());
  46 }
  47 
  48 template <class Chunk>
  49 AdaptiveFreeList<Chunk>::AdaptiveFreeList() : FreeList<Chunk>(), _hint(0) {
  50   init_statistics();
  51 }
  52 
  53 template <class Chunk>
  54 AdaptiveFreeList<Chunk>::AdaptiveFreeList(Chunk* fc) : FreeList<Chunk>(fc), _hint(0) {
  55   init_statistics();
  56 #ifndef PRODUCT
  57   _allocation_stats.set_returned_bytes(size() * HeapWordSize);
  58 #endif
  59 }
  60 
  61 template <class Chunk>
  62 void AdaptiveFreeList<Chunk>::initialize() {
  63   FreeList<Chunk>::initialize();
  64   set_hint(0);
  65   init_statistics(true /* split_birth */);
  66 }
  67 
  68 template <class Chunk>
  69 void AdaptiveFreeList<Chunk>::reset(size_t hint) {
  70   FreeList<Chunk>::reset();
  71   set_hint(hint);
  72 }
  73 
  74 #ifndef PRODUCT
  75 template <class Chunk>
  76 void AdaptiveFreeList<Chunk>::assert_proper_lock_protection_work() const {
  77   assert(protecting_lock() != NULL, "Don't call this directly");
  78   assert(ParallelGCThreads > 0, "Don't call this directly");
  79   Thread* thr = Thread::current();
  80   if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) {
  81     // assert that we are holding the freelist lock
  82   } else if (thr->is_GC_task_thread()) {
  83     assert(protecting_lock()->owned_by_self(), "FreeList RACE DETECTED");
  84   } else if (thr->is_Java_thread()) {
  85     assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing");
  86   } else {
  87     ShouldNotReachHere();  // unaccounted thread type?
  88   }
  89 }
  90 #endif
  91 template <class Chunk>
  92 void AdaptiveFreeList<Chunk>::init_statistics(bool split_birth) {
  93   _allocation_stats.initialize(split_birth);
  94 }
  95 
  96 template <class Chunk>
  97 size_t AdaptiveFreeList<Chunk>::get_better_size() {
  98 
  99   // A candidate chunk has been found.  If it is already under
 100   // populated and there is a hinT, REturn the hint().  Else
 101   // return the size of this chunk.
 102   if (surplus() <= 0) {
 103     if (hint() != 0) {
 104       return hint();
 105     } else {
 106       return size();
 107     }
 108   } else {
 109     // This list has a surplus so use it.
 110     return size();
 111   }
 112 }
 113 
 114 
 115 template <class Chunk>
 116 void AdaptiveFreeList<Chunk>::return_chunk_at_head(Chunk* chunk) {
 117   assert_proper_lock_protection();
 118   return_chunk_at_head(chunk, true);
 119 }
 120 
 121 template <class Chunk>
 122 void AdaptiveFreeList<Chunk>::return_chunk_at_head(Chunk* chunk, bool record_return) {
 123   FreeList<Chunk>::return_chunk_at_head(chunk, record_return);
 124 #ifdef ASSERT
 125   if (record_return) {
 126     increment_returned_bytes_by(size()*HeapWordSize);
 127   }
 128 #endif
 129 }
 130 
 131 template <class Chunk>
 132 void AdaptiveFreeList<Chunk>::return_chunk_at_tail(Chunk* chunk) {
 133   return_chunk_at_tail(chunk, true);
 134 }
 135 
 136 template <class Chunk>
 137 void AdaptiveFreeList<Chunk>::return_chunk_at_tail(Chunk* chunk, bool record_return) {
 138   FreeList<Chunk>::return_chunk_at_tail(chunk, record_return);
 139 #ifdef ASSERT
 140   if (record_return) {
 141     increment_returned_bytes_by(size()*HeapWordSize);
 142   }
 143 #endif
 144 }
 145 
 146 #ifndef PRODUCT
 147 template <class Chunk>
 148 void AdaptiveFreeList<Chunk>::verify_stats() const {
 149   // The +1 of the LH comparand is to allow some "looseness" in
 150   // checking: we usually call this interface when adding a block
 151   // and we'll subsequently update the stats; we cannot update the
 152   // stats beforehand because in the case of the large-block BT
 153   // dictionary for example, this might be the first block and
 154   // in that case there would be no place that we could record
 155   // the stats (which are kept in the block itself).
 156   assert((_allocation_stats.prev_sweep() + _allocation_stats.split_births()
 157           + _allocation_stats.coal_births() + 1)   // Total Production Stock + 1
 158          >= (_allocation_stats.split_deaths() + _allocation_stats.coal_deaths()
 159              + (ssize_t)count()),                // Total Current Stock + depletion
 160          err_msg("FreeList " PTR_FORMAT " of size " SIZE_FORMAT
 161                  " violates Conservation Principle: "
 162                  "prev_sweep(" SIZE_FORMAT ")"
 163                  " + split_births(" SIZE_FORMAT ")"
 164                  " + coal_births(" SIZE_FORMAT ") + 1 >= "
 165                  " split_deaths(" SIZE_FORMAT ")"
 166                  " coal_deaths(" SIZE_FORMAT ")"
 167                  " + count(" SSIZE_FORMAT ")",
 168                  this, size(), _allocation_stats.prev_sweep(), _allocation_stats.split_births(),
 169                  _allocation_stats.split_births(), _allocation_stats.split_deaths(),
 170                  _allocation_stats.coal_deaths(), count()));
 171 }
 172 #endif
 173 
 174 // Needs to be after the definitions have been seen.
 175 template class AdaptiveFreeList<FreeChunk>;