src/share/vm/memory/freeList.cpp

Tue, 11 Sep 2012 14:59:23 +0200

author
stefank
date
Tue, 11 Sep 2012 14:59:23 +0200
changeset 4050
ec98e58952b2
parent 3732
f69a5d43dc19
child 4153
b9a9ed0f8eeb
permissions
-rw-r--r--

7197350: NPG: jvmtiHeapReferenceCallback receives incorrect reference_kind for system class roots
Summary: Fix the iteration over the system classes and report the correct reference kind.
Reviewed-by: coleenp, rbackman

duke@435 1 /*
stefank@2314 2 * Copyright (c) 2001, 2010, 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"
stefank@2314 28 #include "memory/sharedHeap.hpp"
stefank@2314 29 #include "runtime/globals.hpp"
stefank@2314 30 #include "runtime/mutex.hpp"
stefank@2314 31 #include "runtime/vmThread.hpp"
duke@435 32
jmasa@3730 33 #ifndef SERIALGC
jmasa@3730 34 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
jmasa@3730 35 #endif // SERIALGC
jmasa@3730 36
duke@435 37 // Free list. A FreeList is used to access a linked list of chunks
duke@435 38 // of space in the heap. The head and tail are maintained so that
duke@435 39 // items can be (as in the current implementation) added at the
duke@435 40 // at the tail of the list and removed from the head of the list to
duke@435 41 // maintain a FIFO queue.
duke@435 42
jmasa@3730 43 template <class Chunk>
jmasa@3730 44 FreeList<Chunk>::FreeList() :
duke@435 45 _head(NULL), _tail(NULL)
duke@435 46 #ifdef ASSERT
duke@435 47 , _protecting_lock(NULL)
duke@435 48 #endif
duke@435 49 {
duke@435 50 _size = 0;
duke@435 51 _count = 0;
duke@435 52 _hint = 0;
duke@435 53 init_statistics();
duke@435 54 }
duke@435 55
jmasa@3730 56 template <class Chunk>
jmasa@3730 57 FreeList<Chunk>::FreeList(Chunk* fc) :
duke@435 58 _head(fc), _tail(fc)
duke@435 59 #ifdef ASSERT
duke@435 60 , _protecting_lock(NULL)
duke@435 61 #endif
duke@435 62 {
duke@435 63 _size = fc->size();
duke@435 64 _count = 1;
duke@435 65 _hint = 0;
duke@435 66 init_statistics();
duke@435 67 #ifndef PRODUCT
jmasa@3732 68 _allocation_stats.set_returned_bytes(size() * HeapWordSize);
duke@435 69 #endif
duke@435 70 }
duke@435 71
jmasa@3730 72 template <class Chunk>
jmasa@3730 73 void FreeList<Chunk>::reset(size_t hint) {
duke@435 74 set_count(0);
duke@435 75 set_head(NULL);
duke@435 76 set_tail(NULL);
duke@435 77 set_hint(hint);
duke@435 78 }
duke@435 79
jmasa@3730 80 template <class Chunk>
jmasa@3730 81 void FreeList<Chunk>::init_statistics(bool split_birth) {
ysr@1580 82 _allocation_stats.initialize(split_birth);
duke@435 83 }
duke@435 84
jmasa@3730 85 template <class Chunk>
jmasa@3732 86 Chunk* FreeList<Chunk>::get_chunk_at_head() {
duke@435 87 assert_proper_lock_protection();
duke@435 88 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 89 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
jmasa@3730 90 Chunk* fc = head();
duke@435 91 if (fc != NULL) {
jmasa@3730 92 Chunk* nextFC = fc->next();
duke@435 93 if (nextFC != NULL) {
duke@435 94 // The chunk fc being removed has a "next". Set the "next" to the
duke@435 95 // "prev" of fc.
jmasa@3732 96 nextFC->link_prev(NULL);
duke@435 97 } else { // removed tail of list
duke@435 98 link_tail(NULL);
duke@435 99 }
duke@435 100 link_head(nextFC);
duke@435 101 decrement_count();
duke@435 102 }
duke@435 103 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 104 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 105 return fc;
duke@435 106 }
duke@435 107
duke@435 108
jmasa@3730 109 template <class Chunk>
jmasa@3730 110 void FreeList<Chunk>::getFirstNChunksFromList(size_t n, FreeList<Chunk>* fl) {
duke@435 111 assert_proper_lock_protection();
duke@435 112 assert(fl->count() == 0, "Precondition");
duke@435 113 if (count() > 0) {
duke@435 114 int k = 1;
duke@435 115 fl->set_head(head()); n--;
jmasa@3730 116 Chunk* tl = head();
duke@435 117 while (tl->next() != NULL && n > 0) {
duke@435 118 tl = tl->next(); n--; k++;
duke@435 119 }
duke@435 120 assert(tl != NULL, "Loop Inv.");
duke@435 121
duke@435 122 // First, fix up the list we took from.
jmasa@3730 123 Chunk* new_head = tl->next();
duke@435 124 set_head(new_head);
duke@435 125 set_count(count() - k);
duke@435 126 if (new_head == NULL) {
duke@435 127 set_tail(NULL);
duke@435 128 } else {
jmasa@3732 129 new_head->link_prev(NULL);
duke@435 130 }
duke@435 131 // Now we can fix up the tail.
jmasa@3732 132 tl->link_next(NULL);
duke@435 133 // And return the result.
duke@435 134 fl->set_tail(tl);
duke@435 135 fl->set_count(k);
duke@435 136 }
duke@435 137 }
duke@435 138
duke@435 139 // Remove this chunk from the list
jmasa@3730 140 template <class Chunk>
jmasa@3732 141 void FreeList<Chunk>::remove_chunk(Chunk*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
jmasa@3730 149 Chunk* prevFC = fc->prev();
jmasa@3730 150 Chunk* 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.
jmasa@3732 154 nextFC->link_prev(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 {
jmasa@3732 163 prevFC->link_next(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();
ysr@2132 168 assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0,
ysr@2132 169 "H/T/C Inconsistency");
duke@435 170 // clear next and prev fields of fc, debug only
duke@435 171 NOT_PRODUCT(
jmasa@3732 172 fc->link_prev(NULL);
jmasa@3732 173 fc->link_next(NULL);
duke@435 174 )
jmasa@3732 175 assert(fc->is_free(), "Should still be a free chunk");
duke@435 176 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 177 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 178 assert(head() == NULL || head()->size() == size(), "wrong item on list");
duke@435 179 assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
duke@435 180 }
duke@435 181
duke@435 182 // Add this chunk at the head of the list.
jmasa@3730 183 template <class Chunk>
jmasa@3732 184 void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk, bool record_return) {
duke@435 185 assert_proper_lock_protection();
duke@435 186 assert(chunk != NULL, "insert a NULL chunk");
duke@435 187 assert(size() == chunk->size(), "Wrong size");
duke@435 188 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 189 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 190
jmasa@3730 191 Chunk* oldHead = head();
duke@435 192 assert(chunk != oldHead, "double insertion");
jmasa@3732 193 chunk->link_after(oldHead);
duke@435 194 link_head(chunk);
duke@435 195 if (oldHead == NULL) { // only chunk in list
duke@435 196 assert(tail() == NULL, "inconsistent FreeList");
duke@435 197 link_tail(chunk);
duke@435 198 }
duke@435 199 increment_count(); // of # of chunks in list
duke@435 200 DEBUG_ONLY(
duke@435 201 if (record_return) {
jmasa@3732 202 increment_returned_bytes_by(size()*HeapWordSize);
duke@435 203 }
duke@435 204 )
duke@435 205 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 206 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 207 assert(head() == NULL || head()->size() == size(), "wrong item on list");
duke@435 208 assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
duke@435 209 }
duke@435 210
jmasa@3730 211 template <class Chunk>
jmasa@3732 212 void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk) {
duke@435 213 assert_proper_lock_protection();
jmasa@3732 214 return_chunk_at_head(chunk, true);
duke@435 215 }
duke@435 216
duke@435 217 // Add this chunk at the tail of the list.
jmasa@3730 218 template <class Chunk>
jmasa@3732 219 void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk, bool record_return) {
duke@435 220 assert_proper_lock_protection();
duke@435 221 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 222 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 223 assert(chunk != NULL, "insert a NULL chunk");
duke@435 224 assert(size() == chunk->size(), "wrong size");
duke@435 225
jmasa@3730 226 Chunk* oldTail = tail();
duke@435 227 assert(chunk != oldTail, "double insertion");
duke@435 228 if (oldTail != NULL) {
jmasa@3732 229 oldTail->link_after(chunk);
duke@435 230 } else { // only chunk in list
duke@435 231 assert(head() == NULL, "inconsistent FreeList");
duke@435 232 link_head(chunk);
duke@435 233 }
duke@435 234 link_tail(chunk);
duke@435 235 increment_count(); // of # of chunks in list
duke@435 236 DEBUG_ONLY(
duke@435 237 if (record_return) {
jmasa@3732 238 increment_returned_bytes_by(size()*HeapWordSize);
duke@435 239 }
duke@435 240 )
duke@435 241 assert(head() == NULL || head()->prev() == NULL, "list invariant");
duke@435 242 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
duke@435 243 assert(head() == NULL || head()->size() == size(), "wrong item on list");
duke@435 244 assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
duke@435 245 }
duke@435 246
jmasa@3730 247 template <class Chunk>
jmasa@3732 248 void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk) {
jmasa@3732 249 return_chunk_at_tail(chunk, true);
duke@435 250 }
duke@435 251
jmasa@3730 252 template <class Chunk>
jmasa@3730 253 void FreeList<Chunk>::prepend(FreeList<Chunk>* 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.
jmasa@3730 262 Chunk* fl_tail = fl->tail();
jmasa@3730 263 Chunk* this_head = head();
duke@435 264 assert(fl_tail->next() == NULL, "Well-formedness of fl");
jmasa@3732 265 fl_tail->link_next(this_head);
jmasa@3732 266 this_head->link_prev(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
jmasa@3732 276 // verify_chunk_in_free_list() is used to verify that an item is in this free list.
duke@435 277 // It is used as a debugging aid.
jmasa@3730 278 template <class Chunk>
jmasa@3732 279 bool FreeList<Chunk>::verify_chunk_in_free_list(Chunk* fc) const {
duke@435 280 // This is an internal consistency check, not part of the check that the
duke@435 281 // chunk is in the free lists.
duke@435 282 guarantee(fc->size() == size(), "Wrong list is being searched");
jmasa@3730 283 Chunk* curFC = head();
duke@435 284 while (curFC) {
duke@435 285 // This is an internal consistency check.
duke@435 286 guarantee(size() == curFC->size(), "Chunk is in wrong list.");
duke@435 287 if (fc == curFC) {
duke@435 288 return true;
duke@435 289 }
duke@435 290 curFC = curFC->next();
duke@435 291 }
duke@435 292 return false;
duke@435 293 }
duke@435 294
duke@435 295 #ifndef PRODUCT
jmasa@3730 296 template <class Chunk>
jmasa@3730 297 void FreeList<Chunk>::verify_stats() const {
ysr@1580 298 // The +1 of the LH comparand is to allow some "looseness" in
ysr@1580 299 // checking: we usually call this interface when adding a block
ysr@1580 300 // and we'll subsequently update the stats; we cannot update the
ysr@1580 301 // stats beforehand because in the case of the large-block BT
ysr@1580 302 // dictionary for example, this might be the first block and
ysr@1580 303 // in that case there would be no place that we could record
ysr@1580 304 // the stats (which are kept in the block itself).
jmasa@3732 305 assert((_allocation_stats.prev_sweep() + _allocation_stats.split_births()
jmasa@3732 306 + _allocation_stats.coal_births() + 1) // Total Production Stock + 1
jmasa@3732 307 >= (_allocation_stats.split_deaths() + _allocation_stats.coal_deaths()
ysr@2972 308 + (ssize_t)count()), // Total Current Stock + depletion
ysr@2972 309 err_msg("FreeList " PTR_FORMAT " of size " SIZE_FORMAT
ysr@2972 310 " violates Conservation Principle: "
jmasa@3732 311 "prev_sweep(" SIZE_FORMAT ")"
jmasa@3732 312 " + split_births(" SIZE_FORMAT ")"
jmasa@3732 313 " + coal_births(" SIZE_FORMAT ") + 1 >= "
jmasa@3732 314 " split_deaths(" SIZE_FORMAT ")"
jmasa@3732 315 " coal_deaths(" SIZE_FORMAT ")"
ysr@2972 316 " + count(" SSIZE_FORMAT ")",
jmasa@3732 317 this, _size, _allocation_stats.prev_sweep(), _allocation_stats.split_births(),
jmasa@3732 318 _allocation_stats.split_births(), _allocation_stats.split_deaths(),
jmasa@3732 319 _allocation_stats.coal_deaths(), count()));
ysr@1580 320 }
ysr@1580 321
jmasa@3730 322 template <class Chunk>
jmasa@3730 323 void FreeList<Chunk>::assert_proper_lock_protection_work() const {
ysr@1580 324 assert(_protecting_lock != NULL, "Don't call this directly");
ysr@1580 325 assert(ParallelGCThreads > 0, "Don't call this directly");
ysr@1580 326 Thread* thr = Thread::current();
ysr@1580 327 if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) {
ysr@1580 328 // assert that we are holding the freelist lock
ysr@1580 329 } else if (thr->is_GC_task_thread()) {
ysr@1580 330 assert(_protecting_lock->owned_by_self(), "FreeList RACE DETECTED");
ysr@1580 331 } else if (thr->is_Java_thread()) {
ysr@1580 332 assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing");
ysr@1580 333 } else {
ysr@1580 334 ShouldNotReachHere(); // unaccounted thread type?
duke@435 335 }
duke@435 336 }
duke@435 337 #endif
ysr@447 338
ysr@447 339 // Print the "label line" for free list stats.
jmasa@3730 340 template <class Chunk>
jmasa@3730 341 void FreeList<Chunk>::print_labels_on(outputStream* st, const char* c) {
ysr@447 342 st->print("%16s\t", c);
ysr@447 343 st->print("%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t"
ysr@447 344 "%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" "\n",
ysr@447 345 "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep",
ysr@447 346 "count", "cBirths", "cDeaths", "sBirths", "sDeaths");
ysr@447 347 }
ysr@447 348
ysr@447 349 // Print the AllocationStats for the given free list. If the second argument
ysr@447 350 // to the call is a non-null string, it is printed in the first column;
ysr@447 351 // otherwise, if the argument is null (the default), then the size of the
ysr@447 352 // (free list) block is printed in the first column.
jmasa@3730 353 template <class Chunk>
jmasa@3730 354 void FreeList<Chunk>::print_on(outputStream* st, const char* c) const {
ysr@447 355 if (c != NULL) {
ysr@447 356 st->print("%16s", c);
ysr@447 357 } else {
ysr@447 358 st->print(SIZE_FORMAT_W(16), size());
ysr@447 359 }
ysr@447 360 st->print("\t"
ysr@447 361 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 362 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",
jmasa@3732 363 bfr_surp(), surplus(), desired(), prev_sweep(), before_sweep(),
jmasa@3732 364 count(), coal_births(), coal_deaths(), split_births(), split_deaths());
ysr@447 365 }
jmasa@3730 366
jmasa@3730 367 #ifndef SERIALGC
jmasa@3730 368 // Needs to be after the definitions have been seen.
jmasa@3730 369 template class FreeList<FreeChunk>;
jmasa@3730 370 #endif // SERIALGC

mercurial