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