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