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