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