1 /*
   2  * Copyright (c) 2001, 2008, 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 "incls/_precompiled.incl"
  26 # include "incls/_freeList.cpp.incl"
  27 
  28 // Free list.  A FreeList is used to access a linked list of chunks
  29 // of space in the heap.  The head and tail are maintained so that
  30 // items can be (as in the current implementation) added at the
  31 // at the tail of the list and removed from the head of the list to
  32 // maintain a FIFO queue.
  33 
  34 FreeList::FreeList() :
  35   _head(NULL), _tail(NULL)
  36 #ifdef ASSERT
  37   , _protecting_lock(NULL)
  38 #endif
  39 {
  40   _size         = 0;
  41   _count        = 0;
  42   _hint         = 0;
  43   init_statistics();
  44 }
  45 
  46 FreeList::FreeList(FreeChunk* fc) :
  47   _head(fc), _tail(fc)
  48 #ifdef ASSERT
  49   , _protecting_lock(NULL)
  50 #endif
  51 {
  52   _size         = fc->size();
  53   _count        = 1;
  54   _hint         = 0;
  55   init_statistics();
  56 #ifndef PRODUCT
  57   _allocation_stats.set_returnedBytes(size() * HeapWordSize);
  58 #endif
  59 }
  60 
  61 FreeList::FreeList(HeapWord* addr, size_t size) :
  62   _head((FreeChunk*) addr), _tail((FreeChunk*) addr)
  63 #ifdef ASSERT
  64   , _protecting_lock(NULL)
  65 #endif
  66 {
  67   assert(size > sizeof(FreeChunk), "size is too small");
  68   head()->setSize(size);
  69   _size         = size;
  70   _count        = 1;
  71   init_statistics();
  72 #ifndef PRODUCT
  73   _allocation_stats.set_returnedBytes(_size * HeapWordSize);
  74 #endif
  75 }
  76 
  77 void FreeList::reset(size_t hint) {
  78   set_count(0);
  79   set_head(NULL);
  80   set_tail(NULL);
  81   set_hint(hint);
  82 }
  83 
  84 void FreeList::init_statistics(bool split_birth) {
  85   _allocation_stats.initialize(split_birth);
  86 }
  87 
  88 FreeChunk* FreeList::getChunkAtHead() {
  89   assert_proper_lock_protection();
  90   assert(head() == NULL || head()->prev() == NULL, "list invariant");
  91   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
  92   FreeChunk* fc = head();
  93   if (fc != NULL) {
  94     FreeChunk* nextFC = fc->next();
  95     if (nextFC != NULL) {
  96       // The chunk fc being removed has a "next".  Set the "next" to the
  97       // "prev" of fc.
  98       nextFC->linkPrev(NULL);
  99     } else { // removed tail of list
 100       link_tail(NULL);
 101     }
 102     link_head(nextFC);
 103     decrement_count();
 104   }
 105   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 106   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 107   return fc;
 108 }
 109 
 110 
 111 void FreeList::getFirstNChunksFromList(size_t n, FreeList* fl) {
 112   assert_proper_lock_protection();
 113   assert(fl->count() == 0, "Precondition");
 114   if (count() > 0) {
 115     int k = 1;
 116     fl->set_head(head()); n--;
 117     FreeChunk* tl = head();
 118     while (tl->next() != NULL && n > 0) {
 119       tl = tl->next(); n--; k++;
 120     }
 121     assert(tl != NULL, "Loop Inv.");
 122 
 123     // First, fix up the list we took from.
 124     FreeChunk* new_head = tl->next();
 125     set_head(new_head);
 126     set_count(count() - k);
 127     if (new_head == NULL) {
 128       set_tail(NULL);
 129     } else {
 130       new_head->linkPrev(NULL);
 131     }
 132     // Now we can fix up the tail.
 133     tl->linkNext(NULL);
 134     // And return the result.
 135     fl->set_tail(tl);
 136     fl->set_count(k);
 137   }
 138 }
 139 
 140 // Remove this chunk from the list
 141 void FreeList::removeChunk(FreeChunk*fc) {
 142    assert_proper_lock_protection();
 143    assert(head() != NULL, "Remove from empty list");
 144    assert(fc != NULL, "Remove a NULL chunk");
 145    assert(size() == fc->size(), "Wrong list");
 146    assert(head() == NULL || head()->prev() == NULL, "list invariant");
 147    assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 148 
 149    FreeChunk* prevFC = fc->prev();
 150    FreeChunk* nextFC = fc->next();
 151    if (nextFC != NULL) {
 152      // The chunk fc being removed has a "next".  Set the "next" to the
 153      // "prev" of fc.
 154      nextFC->linkPrev(prevFC);
 155    } else { // removed tail of list
 156      link_tail(prevFC);
 157    }
 158    if (prevFC == NULL) { // removed head of list
 159      link_head(nextFC);
 160      assert(nextFC == NULL || nextFC->prev() == NULL,
 161        "Prev of head should be NULL");
 162    } else {
 163      prevFC->linkNext(nextFC);
 164      assert(tail() != prevFC || prevFC->next() == NULL,
 165        "Next of tail should be NULL");
 166    }
 167    decrement_count();
 168    assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0,
 169           "H/T/C Inconsistency");
 170    // clear next and prev fields of fc, debug only
 171    NOT_PRODUCT(
 172      fc->linkPrev(NULL);
 173      fc->linkNext(NULL);
 174    )
 175    assert(fc->isFree(), "Should still be a free chunk");
 176    assert(head() == NULL || head()->prev() == NULL, "list invariant");
 177    assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 178    assert(head() == NULL || head()->size() == size(), "wrong item on list");
 179    assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
 180 }
 181 
 182 // Add this chunk at the head of the list.
 183 void FreeList::returnChunkAtHead(FreeChunk* chunk, bool record_return) {
 184   assert_proper_lock_protection();
 185   assert(chunk != NULL, "insert a NULL chunk");
 186   assert(size() == chunk->size(), "Wrong size");
 187   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 188   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 189 
 190   FreeChunk* oldHead = head();
 191   assert(chunk != oldHead, "double insertion");
 192   chunk->linkAfter(oldHead);
 193   link_head(chunk);
 194   if (oldHead == NULL) { // only chunk in list
 195     assert(tail() == NULL, "inconsistent FreeList");
 196     link_tail(chunk);
 197   }
 198   increment_count(); // of # of chunks in list
 199   DEBUG_ONLY(
 200     if (record_return) {
 201       increment_returnedBytes_by(size()*HeapWordSize);
 202     }
 203   )
 204   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 205   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 206   assert(head() == NULL || head()->size() == size(), "wrong item on list");
 207   assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
 208 }
 209 
 210 void FreeList::returnChunkAtHead(FreeChunk* chunk) {
 211   assert_proper_lock_protection();
 212   returnChunkAtHead(chunk, true);
 213 }
 214 
 215 // Add this chunk at the tail of the list.
 216 void FreeList::returnChunkAtTail(FreeChunk* chunk, bool record_return) {
 217   assert_proper_lock_protection();
 218   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 219   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 220   assert(chunk != NULL, "insert a NULL chunk");
 221   assert(size() == chunk->size(), "wrong size");
 222 
 223   FreeChunk* oldTail = tail();
 224   assert(chunk != oldTail, "double insertion");
 225   if (oldTail != NULL) {
 226     oldTail->linkAfter(chunk);
 227   } else { // only chunk in list
 228     assert(head() == NULL, "inconsistent FreeList");
 229     link_head(chunk);
 230   }
 231   link_tail(chunk);
 232   increment_count();  // of # of chunks in list
 233   DEBUG_ONLY(
 234     if (record_return) {
 235       increment_returnedBytes_by(size()*HeapWordSize);
 236     }
 237   )
 238   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 239   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 240   assert(head() == NULL || head()->size() == size(), "wrong item on list");
 241   assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
 242 }
 243 
 244 void FreeList::returnChunkAtTail(FreeChunk* chunk) {
 245   returnChunkAtTail(chunk, true);
 246 }
 247 
 248 void FreeList::prepend(FreeList* fl) {
 249   assert_proper_lock_protection();
 250   if (fl->count() > 0) {
 251     if (count() == 0) {
 252       set_head(fl->head());
 253       set_tail(fl->tail());
 254       set_count(fl->count());
 255     } else {
 256       // Both are non-empty.
 257       FreeChunk* fl_tail = fl->tail();
 258       FreeChunk* this_head = head();
 259       assert(fl_tail->next() == NULL, "Well-formedness of fl");
 260       fl_tail->linkNext(this_head);
 261       this_head->linkPrev(fl_tail);
 262       set_head(fl->head());
 263       set_count(count() + fl->count());
 264     }
 265     fl->set_head(NULL);
 266     fl->set_tail(NULL);
 267     fl->set_count(0);
 268   }
 269 }
 270 
 271 // verifyChunkInFreeLists() is used to verify that an item is in this free list.
 272 // It is used as a debugging aid.
 273 bool FreeList::verifyChunkInFreeLists(FreeChunk* fc) const {
 274   // This is an internal consistency check, not part of the check that the
 275   // chunk is in the free lists.
 276   guarantee(fc->size() == size(), "Wrong list is being searched");
 277   FreeChunk* curFC = head();
 278   while (curFC) {
 279     // This is an internal consistency check.
 280     guarantee(size() == curFC->size(), "Chunk is in wrong list.");
 281     if (fc == curFC) {
 282       return true;
 283     }
 284     curFC = curFC->next();
 285   }
 286   return false;
 287 }
 288 
 289 #ifndef PRODUCT
 290 void FreeList::verify_stats() const {
 291   // The +1 of the LH comparand is to allow some "looseness" in
 292   // checking: we usually call this interface when adding a block
 293   // and we'll subsequently update the stats; we cannot update the
 294   // stats beforehand because in the case of the large-block BT
 295   // dictionary for example, this might be the first block and
 296   // in that case there would be no place that we could record
 297   // the stats (which are kept in the block itself).
 298   assert(_allocation_stats.prevSweep() + _allocation_stats.splitBirths() + 1   // Total Stock + 1
 299           >= _allocation_stats.splitDeaths() + (ssize_t)count(), "Conservation Principle");
 300 }
 301 
 302 void FreeList::assert_proper_lock_protection_work() const {
 303   assert(_protecting_lock != NULL, "Don't call this directly");
 304   assert(ParallelGCThreads > 0, "Don't call this directly");
 305   Thread* thr = Thread::current();
 306   if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) {
 307     // assert that we are holding the freelist lock
 308   } else if (thr->is_GC_task_thread()) {
 309     assert(_protecting_lock->owned_by_self(), "FreeList RACE DETECTED");
 310   } else if (thr->is_Java_thread()) {
 311     assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing");
 312   } else {
 313     ShouldNotReachHere();  // unaccounted thread type?
 314   }
 315 }
 316 #endif
 317 
 318 // Print the "label line" for free list stats.
 319 void FreeList::print_labels_on(outputStream* st, const char* c) {
 320   st->print("%16s\t", c);
 321   st->print("%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"
 322             "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "\n",
 323             "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep",
 324             "count",   "cBirths", "cDeaths", "sBirths", "sDeaths");
 325 }
 326 
 327 // Print the AllocationStats for the given free list. If the second argument
 328 // to the call is a non-null string, it is printed in the first column;
 329 // otherwise, if the argument is null (the default), then the size of the
 330 // (free list) block is printed in the first column.
 331 void FreeList::print_on(outputStream* st, const char* c) const {
 332   if (c != NULL) {
 333     st->print("%16s", c);
 334   } else {
 335     st->print(SIZE_FORMAT_W(16), size());
 336   }
 337   st->print("\t"
 338            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"
 339            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",
 340            bfrSurp(),             surplus(),             desired(),             prevSweep(),           beforeSweep(),
 341            count(),               coalBirths(),          coalDeaths(),          splitBirths(),         splitDeaths());
 342 }