freeList.cpp 11.1 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25
#include "precompiled.hpp"
26 27
#include "memory/freeBlockDictionary.hpp"
#include "memory/freeList.hpp"
28
#include "memory/metachunk.hpp"
29 30 31 32
#include "memory/sharedHeap.hpp"
#include "runtime/globals.hpp"
#include "runtime/mutex.hpp"
#include "runtime/vmThread.hpp"
33
#include "utilities/macros.hpp"
D
duke 已提交
34

35
#if INCLUDE_ALL_GCS
36
#include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
37
#include "gc_implementation/g1/g1CodeCacheRemSet.hpp"
38
#endif // INCLUDE_ALL_GCS
39

D
duke 已提交
40 41 42 43 44 45
// Free list.  A FreeList is used to access a linked list of chunks
// of space in the heap.  The head and tail are maintained so that
// items can be (as in the current implementation) added at the
// at the tail of the list and removed from the head of the list to
// maintain a FIFO queue.

46 47
template <class Chunk>
FreeList<Chunk>::FreeList() :
D
duke 已提交
48 49 50 51 52 53 54 55 56
  _head(NULL), _tail(NULL)
#ifdef ASSERT
  , _protecting_lock(NULL)
#endif
{
  _size         = 0;
  _count        = 0;
}

57
template <class Chunk>
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
void FreeList<Chunk>::link_head(Chunk* v) {
  assert_proper_lock_protection();
  set_head(v);
  // If this method is not used (just set the head instead),
  // this check can be avoided.
  if (v != NULL) {
    v->link_prev(NULL);
  }
}



template <class Chunk>
void FreeList<Chunk>::reset() {
  // Don't set the _size to 0 because this method is
  // used with a existing list that has a size but which has
  // been emptied.
  // Don't clear the _protecting_lock of an existing list.
D
duke 已提交
76 77 78 79 80
  set_count(0);
  set_head(NULL);
  set_tail(NULL);
}

81
template <class Chunk>
82 83 84 85 86 87 88
void FreeList<Chunk>::initialize() {
#ifdef ASSERT
  // Needed early because it might be checked in other initializing code.
  set_protecting_lock(NULL);
#endif
  reset();
  set_size(0);
D
duke 已提交
89 90
}

91 92
template <class Chunk_t>
Chunk_t* FreeList<Chunk_t>::get_chunk_at_head() {
D
duke 已提交
93 94 95
  assert_proper_lock_protection();
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
96
  Chunk_t* fc = head();
D
duke 已提交
97
  if (fc != NULL) {
98
    Chunk_t* nextFC = fc->next();
D
duke 已提交
99 100 101
    if (nextFC != NULL) {
      // The chunk fc being removed has a "next".  Set the "next" to the
      // "prev" of fc.
102
      nextFC->link_prev(NULL);
D
duke 已提交
103 104 105 106 107 108 109 110 111 112 113 114
    } else { // removed tail of list
      link_tail(NULL);
    }
    link_head(nextFC);
    decrement_count();
  }
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
  return fc;
}


115 116
template <class Chunk>
void FreeList<Chunk>::getFirstNChunksFromList(size_t n, FreeList<Chunk>* fl) {
D
duke 已提交
117 118 119 120 121
  assert_proper_lock_protection();
  assert(fl->count() == 0, "Precondition");
  if (count() > 0) {
    int k = 1;
    fl->set_head(head()); n--;
122
    Chunk* tl = head();
D
duke 已提交
123 124 125 126 127 128
    while (tl->next() != NULL && n > 0) {
      tl = tl->next(); n--; k++;
    }
    assert(tl != NULL, "Loop Inv.");

    // First, fix up the list we took from.
129
    Chunk* new_head = tl->next();
D
duke 已提交
130 131 132 133 134
    set_head(new_head);
    set_count(count() - k);
    if (new_head == NULL) {
      set_tail(NULL);
    } else {
135
      new_head->link_prev(NULL);
D
duke 已提交
136 137
    }
    // Now we can fix up the tail.
138
    tl->link_next(NULL);
D
duke 已提交
139 140 141 142 143 144 145
    // And return the result.
    fl->set_tail(tl);
    fl->set_count(k);
  }
}

// Remove this chunk from the list
146
template <class Chunk>
147
void FreeList<Chunk>::remove_chunk(Chunk*fc) {
D
duke 已提交
148 149 150 151 152 153 154
   assert_proper_lock_protection();
   assert(head() != NULL, "Remove from empty list");
   assert(fc != NULL, "Remove a NULL chunk");
   assert(size() == fc->size(), "Wrong list");
   assert(head() == NULL || head()->prev() == NULL, "list invariant");
   assert(tail() == NULL || tail()->next() == NULL, "list invariant");

155 156
   Chunk* prevFC = fc->prev();
   Chunk* nextFC = fc->next();
D
duke 已提交
157 158 159
   if (nextFC != NULL) {
     // The chunk fc being removed has a "next".  Set the "next" to the
     // "prev" of fc.
160
     nextFC->link_prev(prevFC);
D
duke 已提交
161 162 163 164 165 166 167 168
   } else { // removed tail of list
     link_tail(prevFC);
   }
   if (prevFC == NULL) { // removed head of list
     link_head(nextFC);
     assert(nextFC == NULL || nextFC->prev() == NULL,
       "Prev of head should be NULL");
   } else {
169
     prevFC->link_next(nextFC);
D
duke 已提交
170 171 172 173
     assert(tail() != prevFC || prevFC->next() == NULL,
       "Next of tail should be NULL");
   }
   decrement_count();
174 175
   assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0,
          "H/T/C Inconsistency");
D
duke 已提交
176 177
   // clear next and prev fields of fc, debug only
   NOT_PRODUCT(
178 179
     fc->link_prev(NULL);
     fc->link_next(NULL);
D
duke 已提交
180
   )
181
   assert(fc->is_free(), "Should still be a free chunk");
D
duke 已提交
182 183 184 185 186 187 188
   assert(head() == NULL || head()->prev() == NULL, "list invariant");
   assert(tail() == NULL || tail()->next() == NULL, "list invariant");
   assert(head() == NULL || head()->size() == size(), "wrong item on list");
   assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
}

// Add this chunk at the head of the list.
189
template <class Chunk>
190
void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk, bool record_return) {
D
duke 已提交
191 192 193 194 195 196
  assert_proper_lock_protection();
  assert(chunk != NULL, "insert a NULL chunk");
  assert(size() == chunk->size(), "Wrong size");
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");

197
  Chunk* oldHead = head();
D
duke 已提交
198
  assert(chunk != oldHead, "double insertion");
199
  chunk->link_after(oldHead);
D
duke 已提交
200 201 202 203 204 205 206 207 208 209 210 211
  link_head(chunk);
  if (oldHead == NULL) { // only chunk in list
    assert(tail() == NULL, "inconsistent FreeList");
    link_tail(chunk);
  }
  increment_count(); // of # of chunks in list
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
  assert(head() == NULL || head()->size() == size(), "wrong item on list");
  assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
}

212
template <class Chunk>
213
void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk) {
D
duke 已提交
214
  assert_proper_lock_protection();
215
  return_chunk_at_head(chunk, true);
D
duke 已提交
216 217 218
}

// Add this chunk at the tail of the list.
219
template <class Chunk>
220
void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk, bool record_return) {
D
duke 已提交
221 222 223 224 225 226
  assert_proper_lock_protection();
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
  assert(chunk != NULL, "insert a NULL chunk");
  assert(size() == chunk->size(), "wrong size");

227
  Chunk* oldTail = tail();
D
duke 已提交
228 229
  assert(chunk != oldTail, "double insertion");
  if (oldTail != NULL) {
230
    oldTail->link_after(chunk);
D
duke 已提交
231 232 233 234 235 236 237 238 239 240 241 242
  } else { // only chunk in list
    assert(head() == NULL, "inconsistent FreeList");
    link_head(chunk);
  }
  link_tail(chunk);
  increment_count();  // of # of chunks in list
  assert(head() == NULL || head()->prev() == NULL, "list invariant");
  assert(tail() == NULL || tail()->next() == NULL, "list invariant");
  assert(head() == NULL || head()->size() == size(), "wrong item on list");
  assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
}

243
template <class Chunk>
244 245
void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk) {
  return_chunk_at_tail(chunk, true);
D
duke 已提交
246 247
}

248 249
template <class Chunk>
void FreeList<Chunk>::prepend(FreeList<Chunk>* fl) {
D
duke 已提交
250 251 252 253 254 255 256 257
  assert_proper_lock_protection();
  if (fl->count() > 0) {
    if (count() == 0) {
      set_head(fl->head());
      set_tail(fl->tail());
      set_count(fl->count());
    } else {
      // Both are non-empty.
258 259
      Chunk* fl_tail = fl->tail();
      Chunk* this_head = head();
D
duke 已提交
260
      assert(fl_tail->next() == NULL, "Well-formedness of fl");
261 262
      fl_tail->link_next(this_head);
      this_head->link_prev(fl_tail);
D
duke 已提交
263 264 265 266 267 268 269 270 271
      set_head(fl->head());
      set_count(count() + fl->count());
    }
    fl->set_head(NULL);
    fl->set_tail(NULL);
    fl->set_count(0);
  }
}

272
// verify_chunk_in_free_lists() is used to verify that an item is in this free list.
D
duke 已提交
273
// It is used as a debugging aid.
274
template <class Chunk>
275
bool FreeList<Chunk>::verify_chunk_in_free_list(Chunk* fc) const {
D
duke 已提交
276 277 278
  // This is an internal consistency check, not part of the check that the
  // chunk is in the free lists.
  guarantee(fc->size() == size(), "Wrong list is being searched");
279
  Chunk* curFC = head();
D
duke 已提交
280 281 282 283 284 285 286 287 288 289 290 291
  while (curFC) {
    // This is an internal consistency check.
    guarantee(size() == curFC->size(), "Chunk is in wrong list.");
    if (fc == curFC) {
      return true;
    }
    curFC = curFC->next();
  }
  return false;
}

#ifndef PRODUCT
292 293
template <class Chunk>
void FreeList<Chunk>::assert_proper_lock_protection_work() const {
294
  assert(protecting_lock() != NULL, "Don't call this directly");
295 296 297 298 299
  assert(ParallelGCThreads > 0, "Don't call this directly");
  Thread* thr = Thread::current();
  if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) {
    // assert that we are holding the freelist lock
  } else if (thr->is_GC_task_thread()) {
300
    assert(protecting_lock()->owned_by_self(), "FreeList RACE DETECTED");
301 302 303 304
  } else if (thr->is_Java_thread()) {
    assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing");
  } else {
    ShouldNotReachHere();  // unaccounted thread type?
D
duke 已提交
305 306 307
  }
}
#endif
308 309

// Print the "label line" for free list stats.
310 311
template <class Chunk>
void FreeList<Chunk>::print_labels_on(outputStream* st, const char* c) {
312 313 314 315 316 317 318 319 320 321 322
  st->print("%16s\t", c);
  st->print("%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"
            "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "%14s\t"    "\n",
            "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep",
            "count",   "cBirths", "cDeaths", "sBirths", "sDeaths");
}

// Print the AllocationStats for the given free list. If the second argument
// to the call is a non-null string, it is printed in the first column;
// otherwise, if the argument is null (the default), then the size of the
// (free list) block is printed in the first column.
323 324
template <class Chunk_t>
void FreeList<Chunk_t>::print_on(outputStream* st, const char* c) const {
325 326 327 328 329 330
  if (c != NULL) {
    st->print("%16s", c);
  } else {
    st->print(SIZE_FORMAT_W(16), size());
  }
}
331

332 333
template class FreeList<Metablock>;
template class FreeList<Metachunk>;
334
#if INCLUDE_ALL_GCS
335
template class FreeList<FreeChunk>;
336
template class FreeList<G1CodeRootChunk>;
337
#endif // INCLUDE_ALL_GCS