src/share/vm/memory/freeList.cpp

Tue, 24 Feb 2015 15:04:52 -0500

author
dlong
date
Tue, 24 Feb 2015 15:04:52 -0500
changeset 7598
ddce0b7cee93
parent 7208
7baf47cb97cb
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

8072383: resolve conflicts between open and closed ports
Summary: refactor close to remove references to closed ports
Reviewed-by: kvn, simonis, sgehwolf, dholmes

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

mercurial