Wed, 23 Sep 2009 23:56:15 -0700
Merge
duke@435 | 1 | /* |
xdono@631 | 2 | * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | # include "incls/_precompiled.incl" |
duke@435 | 26 | # include "incls/_freeList.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | // Free list. A FreeList is used to access a linked list of chunks |
duke@435 | 29 | // of space in the heap. The head and tail are maintained so that |
duke@435 | 30 | // items can be (as in the current implementation) added at the |
duke@435 | 31 | // at the tail of the list and removed from the head of the list to |
duke@435 | 32 | // maintain a FIFO queue. |
duke@435 | 33 | |
duke@435 | 34 | FreeList::FreeList() : |
duke@435 | 35 | _head(NULL), _tail(NULL) |
duke@435 | 36 | #ifdef ASSERT |
duke@435 | 37 | , _protecting_lock(NULL) |
duke@435 | 38 | #endif |
duke@435 | 39 | { |
duke@435 | 40 | _size = 0; |
duke@435 | 41 | _count = 0; |
duke@435 | 42 | _hint = 0; |
duke@435 | 43 | init_statistics(); |
duke@435 | 44 | } |
duke@435 | 45 | |
duke@435 | 46 | FreeList::FreeList(FreeChunk* fc) : |
duke@435 | 47 | _head(fc), _tail(fc) |
duke@435 | 48 | #ifdef ASSERT |
duke@435 | 49 | , _protecting_lock(NULL) |
duke@435 | 50 | #endif |
duke@435 | 51 | { |
duke@435 | 52 | _size = fc->size(); |
duke@435 | 53 | _count = 1; |
duke@435 | 54 | _hint = 0; |
duke@435 | 55 | init_statistics(); |
duke@435 | 56 | #ifndef PRODUCT |
duke@435 | 57 | _allocation_stats.set_returnedBytes(size() * HeapWordSize); |
duke@435 | 58 | #endif |
duke@435 | 59 | } |
duke@435 | 60 | |
duke@435 | 61 | FreeList::FreeList(HeapWord* addr, size_t size) : |
duke@435 | 62 | _head((FreeChunk*) addr), _tail((FreeChunk*) addr) |
duke@435 | 63 | #ifdef ASSERT |
duke@435 | 64 | , _protecting_lock(NULL) |
duke@435 | 65 | #endif |
duke@435 | 66 | { |
duke@435 | 67 | assert(size > sizeof(FreeChunk), "size is too small"); |
duke@435 | 68 | head()->setSize(size); |
duke@435 | 69 | _size = size; |
duke@435 | 70 | _count = 1; |
duke@435 | 71 | init_statistics(); |
duke@435 | 72 | #ifndef PRODUCT |
duke@435 | 73 | _allocation_stats.set_returnedBytes(_size * HeapWordSize); |
duke@435 | 74 | #endif |
duke@435 | 75 | } |
duke@435 | 76 | |
duke@435 | 77 | void FreeList::reset(size_t hint) { |
duke@435 | 78 | set_count(0); |
duke@435 | 79 | set_head(NULL); |
duke@435 | 80 | set_tail(NULL); |
duke@435 | 81 | set_hint(hint); |
duke@435 | 82 | } |
duke@435 | 83 | |
duke@435 | 84 | void FreeList::init_statistics() { |
duke@435 | 85 | _allocation_stats.initialize(); |
duke@435 | 86 | } |
duke@435 | 87 | |
duke@435 | 88 | FreeChunk* FreeList::getChunkAtHead() { |
duke@435 | 89 | assert_proper_lock_protection(); |
duke@435 | 90 | assert(head() == NULL || head()->prev() == NULL, "list invariant"); |
duke@435 | 91 | assert(tail() == NULL || tail()->next() == NULL, "list invariant"); |
duke@435 | 92 | FreeChunk* fc = head(); |
duke@435 | 93 | if (fc != NULL) { |
duke@435 | 94 | FreeChunk* nextFC = fc->next(); |
duke@435 | 95 | if (nextFC != NULL) { |
duke@435 | 96 | // The chunk fc being removed has a "next". Set the "next" to the |
duke@435 | 97 | // "prev" of fc. |
duke@435 | 98 | nextFC->linkPrev(NULL); |
duke@435 | 99 | } else { // removed tail of list |
duke@435 | 100 | link_tail(NULL); |
duke@435 | 101 | } |
duke@435 | 102 | link_head(nextFC); |
duke@435 | 103 | decrement_count(); |
duke@435 | 104 | } |
duke@435 | 105 | assert(head() == NULL || head()->prev() == NULL, "list invariant"); |
duke@435 | 106 | assert(tail() == NULL || tail()->next() == NULL, "list invariant"); |
duke@435 | 107 | return fc; |
duke@435 | 108 | } |
duke@435 | 109 | |
duke@435 | 110 | |
duke@435 | 111 | void FreeList::getFirstNChunksFromList(size_t n, FreeList* fl) { |
duke@435 | 112 | assert_proper_lock_protection(); |
duke@435 | 113 | assert(fl->count() == 0, "Precondition"); |
duke@435 | 114 | if (count() > 0) { |
duke@435 | 115 | int k = 1; |
duke@435 | 116 | fl->set_head(head()); n--; |
duke@435 | 117 | FreeChunk* tl = head(); |
duke@435 | 118 | while (tl->next() != NULL && n > 0) { |
duke@435 | 119 | tl = tl->next(); n--; k++; |
duke@435 | 120 | } |
duke@435 | 121 | assert(tl != NULL, "Loop Inv."); |
duke@435 | 122 | |
duke@435 | 123 | // First, fix up the list we took from. |
duke@435 | 124 | FreeChunk* new_head = tl->next(); |
duke@435 | 125 | set_head(new_head); |
duke@435 | 126 | set_count(count() - k); |
duke@435 | 127 | if (new_head == NULL) { |
duke@435 | 128 | set_tail(NULL); |
duke@435 | 129 | } else { |
duke@435 | 130 | new_head->linkPrev(NULL); |
duke@435 | 131 | } |
duke@435 | 132 | // Now we can fix up the tail. |
duke@435 | 133 | tl->linkNext(NULL); |
duke@435 | 134 | // And return the result. |
duke@435 | 135 | fl->set_tail(tl); |
duke@435 | 136 | fl->set_count(k); |
duke@435 | 137 | } |
duke@435 | 138 | } |
duke@435 | 139 | |
duke@435 | 140 | // Remove this chunk from the list |
duke@435 | 141 | void FreeList::removeChunk(FreeChunk*fc) { |
duke@435 | 142 | assert_proper_lock_protection(); |
duke@435 | 143 | assert(head() != NULL, "Remove from empty list"); |
duke@435 | 144 | assert(fc != NULL, "Remove a NULL chunk"); |
duke@435 | 145 | assert(size() == fc->size(), "Wrong list"); |
duke@435 | 146 | assert(head() == NULL || head()->prev() == NULL, "list invariant"); |
duke@435 | 147 | assert(tail() == NULL || tail()->next() == NULL, "list invariant"); |
duke@435 | 148 | |
duke@435 | 149 | FreeChunk* prevFC = fc->prev(); |
duke@435 | 150 | FreeChunk* nextFC = fc->next(); |
duke@435 | 151 | if (nextFC != NULL) { |
duke@435 | 152 | // The chunk fc being removed has a "next". Set the "next" to the |
duke@435 | 153 | // "prev" of fc. |
duke@435 | 154 | nextFC->linkPrev(prevFC); |
duke@435 | 155 | } else { // removed tail of list |
duke@435 | 156 | link_tail(prevFC); |
duke@435 | 157 | } |
duke@435 | 158 | if (prevFC == NULL) { // removed head of list |
duke@435 | 159 | link_head(nextFC); |
duke@435 | 160 | assert(nextFC == NULL || nextFC->prev() == NULL, |
duke@435 | 161 | "Prev of head should be NULL"); |
duke@435 | 162 | } else { |
duke@435 | 163 | prevFC->linkNext(nextFC); |
duke@435 | 164 | assert(tail() != prevFC || prevFC->next() == NULL, |
duke@435 | 165 | "Next of tail should be NULL"); |
duke@435 | 166 | } |
duke@435 | 167 | decrement_count(); |
duke@435 | 168 | #define TRAP_CODE 1 |
duke@435 | 169 | #if TRAP_CODE |
duke@435 | 170 | if (head() == NULL) { |
duke@435 | 171 | guarantee(tail() == NULL, "INVARIANT"); |
duke@435 | 172 | guarantee(count() == 0, "INVARIANT"); |
duke@435 | 173 | } |
duke@435 | 174 | #endif |
duke@435 | 175 | // clear next and prev fields of fc, debug only |
duke@435 | 176 | NOT_PRODUCT( |
duke@435 | 177 | fc->linkPrev(NULL); |
duke@435 | 178 | fc->linkNext(NULL); |
duke@435 | 179 | ) |
duke@435 | 180 | assert(fc->isFree(), "Should still be a free chunk"); |
duke@435 | 181 | assert(head() == NULL || head()->prev() == NULL, "list invariant"); |
duke@435 | 182 | assert(tail() == NULL || tail()->next() == NULL, "list invariant"); |
duke@435 | 183 | assert(head() == NULL || head()->size() == size(), "wrong item on list"); |
duke@435 | 184 | assert(tail() == NULL || tail()->size() == size(), "wrong item on list"); |
duke@435 | 185 | } |
duke@435 | 186 | |
duke@435 | 187 | // Add this chunk at the head of the list. |
duke@435 | 188 | void FreeList::returnChunkAtHead(FreeChunk* chunk, bool record_return) { |
duke@435 | 189 | assert_proper_lock_protection(); |
duke@435 | 190 | assert(chunk != NULL, "insert a NULL chunk"); |
duke@435 | 191 | assert(size() == chunk->size(), "Wrong size"); |
duke@435 | 192 | assert(head() == NULL || head()->prev() == NULL, "list invariant"); |
duke@435 | 193 | assert(tail() == NULL || tail()->next() == NULL, "list invariant"); |
duke@435 | 194 | |
duke@435 | 195 | FreeChunk* oldHead = head(); |
duke@435 | 196 | assert(chunk != oldHead, "double insertion"); |
duke@435 | 197 | chunk->linkAfter(oldHead); |
duke@435 | 198 | link_head(chunk); |
duke@435 | 199 | if (oldHead == NULL) { // only chunk in list |
duke@435 | 200 | assert(tail() == NULL, "inconsistent FreeList"); |
duke@435 | 201 | link_tail(chunk); |
duke@435 | 202 | } |
duke@435 | 203 | increment_count(); // of # of chunks in list |
duke@435 | 204 | DEBUG_ONLY( |
duke@435 | 205 | if (record_return) { |
duke@435 | 206 | increment_returnedBytes_by(size()*HeapWordSize); |
duke@435 | 207 | } |
duke@435 | 208 | ) |
duke@435 | 209 | assert(head() == NULL || head()->prev() == NULL, "list invariant"); |
duke@435 | 210 | assert(tail() == NULL || tail()->next() == NULL, "list invariant"); |
duke@435 | 211 | assert(head() == NULL || head()->size() == size(), "wrong item on list"); |
duke@435 | 212 | assert(tail() == NULL || tail()->size() == size(), "wrong item on list"); |
duke@435 | 213 | } |
duke@435 | 214 | |
duke@435 | 215 | void FreeList::returnChunkAtHead(FreeChunk* chunk) { |
duke@435 | 216 | assert_proper_lock_protection(); |
duke@435 | 217 | returnChunkAtHead(chunk, true); |
duke@435 | 218 | } |
duke@435 | 219 | |
duke@435 | 220 | // Add this chunk at the tail of the list. |
duke@435 | 221 | void FreeList::returnChunkAtTail(FreeChunk* chunk, bool record_return) { |
duke@435 | 222 | assert_proper_lock_protection(); |
duke@435 | 223 | assert(head() == NULL || head()->prev() == NULL, "list invariant"); |
duke@435 | 224 | assert(tail() == NULL || tail()->next() == NULL, "list invariant"); |
duke@435 | 225 | assert(chunk != NULL, "insert a NULL chunk"); |
duke@435 | 226 | assert(size() == chunk->size(), "wrong size"); |
duke@435 | 227 | |
duke@435 | 228 | FreeChunk* oldTail = tail(); |
duke@435 | 229 | assert(chunk != oldTail, "double insertion"); |
duke@435 | 230 | if (oldTail != NULL) { |
duke@435 | 231 | oldTail->linkAfter(chunk); |
duke@435 | 232 | } else { // only chunk in list |
duke@435 | 233 | assert(head() == NULL, "inconsistent FreeList"); |
duke@435 | 234 | link_head(chunk); |
duke@435 | 235 | } |
duke@435 | 236 | link_tail(chunk); |
duke@435 | 237 | increment_count(); // of # of chunks in list |
duke@435 | 238 | DEBUG_ONLY( |
duke@435 | 239 | if (record_return) { |
duke@435 | 240 | increment_returnedBytes_by(size()*HeapWordSize); |
duke@435 | 241 | } |
duke@435 | 242 | ) |
duke@435 | 243 | assert(head() == NULL || head()->prev() == NULL, "list invariant"); |
duke@435 | 244 | assert(tail() == NULL || tail()->next() == NULL, "list invariant"); |
duke@435 | 245 | assert(head() == NULL || head()->size() == size(), "wrong item on list"); |
duke@435 | 246 | assert(tail() == NULL || tail()->size() == size(), "wrong item on list"); |
duke@435 | 247 | } |
duke@435 | 248 | |
duke@435 | 249 | void FreeList::returnChunkAtTail(FreeChunk* chunk) { |
duke@435 | 250 | returnChunkAtTail(chunk, true); |
duke@435 | 251 | } |
duke@435 | 252 | |
duke@435 | 253 | void FreeList::prepend(FreeList* fl) { |
duke@435 | 254 | assert_proper_lock_protection(); |
duke@435 | 255 | if (fl->count() > 0) { |
duke@435 | 256 | if (count() == 0) { |
duke@435 | 257 | set_head(fl->head()); |
duke@435 | 258 | set_tail(fl->tail()); |
duke@435 | 259 | set_count(fl->count()); |
duke@435 | 260 | } else { |
duke@435 | 261 | // Both are non-empty. |
duke@435 | 262 | FreeChunk* fl_tail = fl->tail(); |
duke@435 | 263 | FreeChunk* this_head = head(); |
duke@435 | 264 | assert(fl_tail->next() == NULL, "Well-formedness of fl"); |
duke@435 | 265 | fl_tail->linkNext(this_head); |
duke@435 | 266 | this_head->linkPrev(fl_tail); |
duke@435 | 267 | set_head(fl->head()); |
duke@435 | 268 | set_count(count() + fl->count()); |
duke@435 | 269 | } |
duke@435 | 270 | fl->set_head(NULL); |
duke@435 | 271 | fl->set_tail(NULL); |
duke@435 | 272 | fl->set_count(0); |
duke@435 | 273 | } |
duke@435 | 274 | } |
duke@435 | 275 | |
duke@435 | 276 | // verifyChunkInFreeLists() is used to verify that an item is in this free list. |
duke@435 | 277 | // It is used as a debugging aid. |
duke@435 | 278 | bool FreeList::verifyChunkInFreeLists(FreeChunk* fc) const { |
duke@435 | 279 | // This is an internal consistency check, not part of the check that the |
duke@435 | 280 | // chunk is in the free lists. |
duke@435 | 281 | guarantee(fc->size() == size(), "Wrong list is being searched"); |
duke@435 | 282 | FreeChunk* curFC = head(); |
duke@435 | 283 | while (curFC) { |
duke@435 | 284 | // This is an internal consistency check. |
duke@435 | 285 | guarantee(size() == curFC->size(), "Chunk is in wrong list."); |
duke@435 | 286 | if (fc == curFC) { |
duke@435 | 287 | return true; |
duke@435 | 288 | } |
duke@435 | 289 | curFC = curFC->next(); |
duke@435 | 290 | } |
duke@435 | 291 | return false; |
duke@435 | 292 | } |
duke@435 | 293 | |
duke@435 | 294 | #ifndef PRODUCT |
duke@435 | 295 | void FreeList::assert_proper_lock_protection_work() const { |
duke@435 | 296 | #ifdef ASSERT |
duke@435 | 297 | if (_protecting_lock != NULL && |
duke@435 | 298 | SharedHeap::heap()->n_par_threads() > 0) { |
duke@435 | 299 | // Should become an assert. |
duke@435 | 300 | guarantee(_protecting_lock->owned_by_self(), "FreeList RACE DETECTED"); |
duke@435 | 301 | } |
duke@435 | 302 | #endif |
duke@435 | 303 | } |
duke@435 | 304 | #endif |
ysr@447 | 305 | |
ysr@447 | 306 | // Print the "label line" for free list stats. |
ysr@447 | 307 | void FreeList::print_labels_on(outputStream* st, const char* c) { |
ysr@447 | 308 | st->print("%16s\t", c); |
ysr@447 | 309 | st->print("%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" |
ysr@447 | 310 | "%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" "\n", |
ysr@447 | 311 | "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep", |
ysr@447 | 312 | "count", "cBirths", "cDeaths", "sBirths", "sDeaths"); |
ysr@447 | 313 | } |
ysr@447 | 314 | |
ysr@447 | 315 | // Print the AllocationStats for the given free list. If the second argument |
ysr@447 | 316 | // to the call is a non-null string, it is printed in the first column; |
ysr@447 | 317 | // otherwise, if the argument is null (the default), then the size of the |
ysr@447 | 318 | // (free list) block is printed in the first column. |
ysr@447 | 319 | void FreeList::print_on(outputStream* st, const char* c) const { |
ysr@447 | 320 | if (c != NULL) { |
ysr@447 | 321 | st->print("%16s", c); |
ysr@447 | 322 | } else { |
ysr@447 | 323 | st->print(SIZE_FORMAT_W(16), size()); |
ysr@447 | 324 | } |
ysr@447 | 325 | st->print("\t" |
ysr@447 | 326 | 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" |
ysr@447 | 327 | 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", |
ysr@447 | 328 | bfrSurp(), surplus(), desired(), prevSweep(), beforeSweep(), |
ysr@447 | 329 | count(), coalBirths(), coalDeaths(), splitBirths(), splitDeaths()); |
ysr@447 | 330 | } |