1 /*
   2  * Copyright (c) 2001, 2015, 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/shared/collectedHeap.hpp"
  27 #include "memory/freeBlockDictionary.hpp"
  28 #include "memory/freeList.hpp"
  29 #include "memory/metachunk.hpp"
  30 #include "runtime/globals.hpp"
  31 #include "runtime/mutex.hpp"
  32 #include "runtime/vmThread.hpp"
  33 #include "utilities/macros.hpp"
  34 #if INCLUDE_ALL_GCS
  35 #include "gc/cms/freeChunk.hpp"
  36 #endif // INCLUDE_ALL_GCS
  37 
  38 // Free list.  A FreeList is used to access a linked list of chunks
  39 // of space in the heap.  The head and tail are maintained so that
  40 // items can be (as in the current implementation) added at the
  41 // at the tail of the list and removed from the head of the list to
  42 // maintain a FIFO queue.
  43 
  44 template <class Chunk>
  45 FreeList<Chunk>::FreeList() :
  46   _head(NULL), _tail(NULL)
  47 #ifdef ASSERT
  48   , _protecting_lock(NULL)
  49 #endif
  50 {
  51   _size         = 0;
  52   _count        = 0;
  53 }
  54 
  55 template <class Chunk>
  56 void FreeList<Chunk>::link_head(Chunk* v) {
  57   assert_proper_lock_protection();
  58   set_head(v);
  59   // If this method is not used (just set the head instead),
  60   // this check can be avoided.
  61   if (v != NULL) {
  62     v->link_prev(NULL);
  63   }
  64 }
  65 
  66 
  67 
  68 template <class Chunk>
  69 void FreeList<Chunk>::reset() {
  70   // Don't set the _size to 0 because this method is
  71   // used with a existing list that has a size but which has
  72   // been emptied.
  73   // Don't clear the _protecting_lock of an existing list.
  74   set_count(0);
  75   set_head(NULL);
  76   set_tail(NULL);
  77 }
  78 
  79 template <class Chunk>
  80 void FreeList<Chunk>::initialize() {
  81 #ifdef ASSERT
  82   // Needed early because it might be checked in other initializing code.
  83   set_protecting_lock(NULL);
  84 #endif
  85   reset();
  86   set_size(0);
  87 }
  88 
  89 template <class Chunk_t>
  90 Chunk_t* FreeList<Chunk_t>::get_chunk_at_head() {
  91   assert_proper_lock_protection();
  92   assert(head() == NULL || head()->prev() == NULL, "list invariant");
  93   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
  94   Chunk_t* fc = head();
  95   if (fc != NULL) {
  96     Chunk_t* nextFC = fc->next();
  97     if (nextFC != NULL) {
  98       // The chunk fc being removed has a "next".  Set the "next" to the
  99       // "prev" of fc.
 100       nextFC->link_prev(NULL);
 101     } else { // removed tail of list
 102       link_tail(NULL);
 103     }
 104     link_head(nextFC);
 105     decrement_count();
 106   }
 107   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 108   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 109   return fc;
 110 }
 111 
 112 
 113 template <class Chunk>
 114 void FreeList<Chunk>::getFirstNChunksFromList(size_t n, FreeList<Chunk>* fl) {
 115   assert_proper_lock_protection();
 116   assert(fl->count() == 0, "Precondition");
 117   if (count() > 0) {
 118     int k = 1;
 119     fl->set_head(head()); n--;
 120     Chunk* tl = head();
 121     while (tl->next() != NULL && n > 0) {
 122       tl = tl->next(); n--; k++;
 123     }
 124     assert(tl != NULL, "Loop Inv.");
 125 
 126     // First, fix up the list we took from.
 127     Chunk* new_head = tl->next();
 128     set_head(new_head);
 129     set_count(count() - k);
 130     if (new_head == NULL) {
 131       set_tail(NULL);
 132     } else {
 133       new_head->link_prev(NULL);
 134     }
 135     // Now we can fix up the tail.
 136     tl->link_next(NULL);
 137     // And return the result.
 138     fl->set_tail(tl);
 139     fl->set_count(k);
 140   }
 141 }
 142 
 143 // Remove this chunk from the list
 144 template <class Chunk>
 145 void FreeList<Chunk>::remove_chunk(Chunk*fc) {
 146    assert_proper_lock_protection();
 147    assert(head() != NULL, "Remove from empty list");
 148    assert(fc != NULL, "Remove a NULL chunk");
 149    assert(size() == fc->size(), "Wrong list");
 150    assert(head() == NULL || head()->prev() == NULL, "list invariant");
 151    assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 152 
 153    Chunk* prevFC = fc->prev();
 154    Chunk* nextFC = fc->next();
 155    if (nextFC != NULL) {
 156      // The chunk fc being removed has a "next".  Set the "next" to the
 157      // "prev" of fc.
 158      nextFC->link_prev(prevFC);
 159    } else { // removed tail of list
 160      link_tail(prevFC);
 161    }
 162    if (prevFC == NULL) { // removed head of list
 163      link_head(nextFC);
 164      assert(nextFC == NULL || nextFC->prev() == NULL,
 165        "Prev of head should be NULL");
 166    } else {
 167      prevFC->link_next(nextFC);
 168      assert(tail() != prevFC || prevFC->next() == NULL,
 169        "Next of tail should be NULL");
 170    }
 171    decrement_count();
 172    assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0,
 173           "H/T/C Inconsistency");
 174    // clear next and prev fields of fc, debug only
 175    NOT_PRODUCT(
 176      fc->link_prev(NULL);
 177      fc->link_next(NULL);
 178    )
 179    assert(fc->is_free(), "Should still be a free chunk");
 180    assert(head() == NULL || head()->prev() == NULL, "list invariant");
 181    assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 182    assert(head() == NULL || head()->size() == size(), "wrong item on list");
 183    assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
 184 }
 185 
 186 // Add this chunk at the head of the list.
 187 template <class Chunk>
 188 void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk, bool record_return) {
 189   assert_proper_lock_protection();
 190   assert(chunk != NULL, "insert a NULL chunk");
 191   assert(size() == chunk->size(), "Wrong size");
 192   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 193   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 194 
 195   Chunk* oldHead = head();
 196   assert(chunk != oldHead, "double insertion");
 197   chunk->link_after(oldHead);
 198   link_head(chunk);
 199   if (oldHead == NULL) { // only chunk in list
 200     assert(tail() == NULL, "inconsistent FreeList");
 201     link_tail(chunk);
 202   }
 203   increment_count(); // of # of chunks in list
 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 template <class Chunk>
 211 void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk) {
 212   assert_proper_lock_protection();
 213   return_chunk_at_head(chunk, true);
 214 }
 215 
 216 // Add this chunk at the tail of the list.
 217 template <class Chunk>
 218 void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk, bool record_return) {
 219   assert_proper_lock_protection();
 220   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 221   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 222   assert(chunk != NULL, "insert a NULL chunk");
 223   assert(size() == chunk->size(), "wrong size");
 224 
 225   Chunk* oldTail = tail();
 226   assert(chunk != oldTail, "double insertion");
 227   if (oldTail != NULL) {
 228     oldTail->link_after(chunk);
 229   } else { // only chunk in list
 230     assert(head() == NULL, "inconsistent FreeList");
 231     link_head(chunk);
 232   }
 233   link_tail(chunk);
 234   increment_count();  // of # of chunks in list
 235   assert(head() == NULL || head()->prev() == NULL, "list invariant");
 236   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
 237   assert(head() == NULL || head()->size() == size(), "wrong item on list");
 238   assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
 239 }
 240 
 241 template <class Chunk>
 242 void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk) {
 243   return_chunk_at_tail(chunk, true);
 244 }
 245 
 246 template <class Chunk>
 247 void FreeList<Chunk>::prepend(FreeList<Chunk>* fl) {
 248   assert_proper_lock_protection();
 249   if (fl->count() > 0) {
 250     if (count() == 0) {
 251       set_head(fl->head());
 252       set_tail(fl->tail());
 253       set_count(fl->count());
 254     } else {
 255       // Both are non-empty.
 256       Chunk* fl_tail = fl->tail();
 257       Chunk* this_head = head();
 258       assert(fl_tail->next() == NULL, "Well-formedness of fl");
 259       fl_tail->link_next(this_head);
 260       this_head->link_prev(fl_tail);
 261       set_head(fl->head());
 262       set_count(count() + fl->count());
 263     }
 264     fl->set_head(NULL);
 265     fl->set_tail(NULL);
 266     fl->set_count(0);
 267   }
 268 }
 269 
 270 // verify_chunk_in_free_lists() is used to verify that an item is in this free list.
 271 // It is used as a debugging aid.
 272 template <class Chunk>
 273 bool FreeList<Chunk>::verify_chunk_in_free_list(Chunk* 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   Chunk* 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 #ifdef ASSERT
 290 template <class Chunk>
 291 void FreeList<Chunk>::assert_proper_lock_protection_work() const {
 292   // Nothing to do if the list has no assigned protecting lock
 293   if (protecting_lock() == NULL) {
 294     return;
 295   }
 296 
 297   Thread* thr = Thread::current();
 298   if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) {
 299     // assert that we are holding the freelist lock
 300   } else if (thr->is_GC_task_thread()) {
 301     assert(protecting_lock()->owned_by_self(), "FreeList RACE DETECTED");
 302   } else if (thr->is_Java_thread()) {
 303     assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing");
 304   } else {
 305     ShouldNotReachHere();  // unaccounted thread type?
 306   }
 307 }
 308 #endif
 309 
 310 // Print the "label line" for free list stats.
 311 template <class Chunk>
 312 void FreeList<Chunk>::print_labels_on(outputStream* st, const char* c) {
 313   st->print("%16s\t", c);
 314   st->print("%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"
 315             "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "\n",
 316             "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep",
 317             "count",   "cBirths", "cDeaths", "sBirths", "sDeaths");
 318 }
 319 
 320 // Print the AllocationStats for the given free list. If the second argument
 321 // to the call is a non-null string, it is printed in the first column;
 322 // otherwise, if the argument is null (the default), then the size of the
 323 // (free list) block is printed in the first column.
 324 template <class Chunk_t>
 325 void FreeList<Chunk_t>::print_on(outputStream* st, const char* c) const {
 326   if (c != NULL) {
 327     st->print("%16s", c);
 328   } else {
 329     st->print(SIZE_FORMAT_W(16), size());
 330   }
 331 }
 332 
 333 template class FreeList<Metablock>;
 334 template class FreeList<Metachunk>;
 335 #if INCLUDE_ALL_GCS
 336 template class FreeList<FreeChunk>;
 337 #endif // INCLUDE_ALL_GCS