src/share/vm/memory/freeList.cpp

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

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

mercurial