Tue, 19 Aug 2014 02:05:49 -0700
8044406: JVM crash with JDK8 (build 1.8.0-b132) with G1 GC
Summary: Fill the last card that has been allocated into with a dummy object
Reviewed-by: tschatzl, mgerdin
tschatzl@6402 | 1 | /* |
tschatzl@6402 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
tschatzl@6402 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
tschatzl@6402 | 4 | * |
tschatzl@6402 | 5 | * This code is free software; you can redistribute it and/or modify it |
tschatzl@6402 | 6 | * under the terms of the GNU General Public License version 2 only, as |
tschatzl@6402 | 7 | * published by the Free Software Foundation. |
tschatzl@6402 | 8 | * |
tschatzl@6402 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
tschatzl@6402 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
tschatzl@6402 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
tschatzl@6402 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
tschatzl@6402 | 13 | * accompanied this code). |
tschatzl@6402 | 14 | * |
tschatzl@6402 | 15 | * You should have received a copy of the GNU General Public License version |
tschatzl@6402 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
tschatzl@6402 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
tschatzl@6402 | 18 | * |
tschatzl@6402 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
tschatzl@6402 | 20 | * or visit www.oracle.com if you need additional information or have any |
tschatzl@6402 | 21 | * questions. |
tschatzl@6402 | 22 | * |
tschatzl@6402 | 23 | */ |
tschatzl@6402 | 24 | |
tschatzl@6402 | 25 | |
tschatzl@6402 | 26 | #include "precompiled.hpp" |
tschatzl@6402 | 27 | #include "code/nmethod.hpp" |
tschatzl@6402 | 28 | #include "gc_implementation/g1/g1CodeCacheRemSet.hpp" |
tschatzl@6402 | 29 | #include "memory/iterator.hpp" |
tschatzl@6402 | 30 | |
drchase@6680 | 31 | PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
drchase@6680 | 32 | |
tschatzl@6402 | 33 | G1CodeRootChunk::G1CodeRootChunk() : _top(NULL), _next(NULL), _prev(NULL) { |
tschatzl@6402 | 34 | _top = bottom(); |
tschatzl@6402 | 35 | } |
tschatzl@6402 | 36 | |
tschatzl@6402 | 37 | void G1CodeRootChunk::reset() { |
tschatzl@6402 | 38 | _next = _prev = NULL; |
tschatzl@6402 | 39 | _top = bottom(); |
tschatzl@6402 | 40 | } |
tschatzl@6402 | 41 | |
tschatzl@6402 | 42 | void G1CodeRootChunk::nmethods_do(CodeBlobClosure* cl) { |
tschatzl@6402 | 43 | nmethod** cur = bottom(); |
tschatzl@6402 | 44 | while (cur != _top) { |
tschatzl@6402 | 45 | cl->do_code_blob(*cur); |
tschatzl@6402 | 46 | cur++; |
tschatzl@6402 | 47 | } |
tschatzl@6402 | 48 | } |
tschatzl@6402 | 49 | |
tschatzl@6402 | 50 | FreeList<G1CodeRootChunk> G1CodeRootSet::_free_list; |
tschatzl@6402 | 51 | size_t G1CodeRootSet::_num_chunks_handed_out = 0; |
tschatzl@6402 | 52 | |
tschatzl@6402 | 53 | G1CodeRootChunk* G1CodeRootSet::new_chunk() { |
tschatzl@6402 | 54 | G1CodeRootChunk* result = _free_list.get_chunk_at_head(); |
tschatzl@6402 | 55 | if (result == NULL) { |
tschatzl@6402 | 56 | result = new G1CodeRootChunk(); |
tschatzl@6402 | 57 | } |
tschatzl@6402 | 58 | G1CodeRootSet::_num_chunks_handed_out++; |
tschatzl@6402 | 59 | result->reset(); |
tschatzl@6402 | 60 | return result; |
tschatzl@6402 | 61 | } |
tschatzl@6402 | 62 | |
tschatzl@6402 | 63 | void G1CodeRootSet::free_chunk(G1CodeRootChunk* chunk) { |
tschatzl@6402 | 64 | _free_list.return_chunk_at_head(chunk); |
tschatzl@6402 | 65 | G1CodeRootSet::_num_chunks_handed_out--; |
tschatzl@6402 | 66 | } |
tschatzl@6402 | 67 | |
tschatzl@6402 | 68 | void G1CodeRootSet::free_all_chunks(FreeList<G1CodeRootChunk>* list) { |
tschatzl@6402 | 69 | G1CodeRootSet::_num_chunks_handed_out -= list->count(); |
tschatzl@6402 | 70 | _free_list.prepend(list); |
tschatzl@6402 | 71 | } |
tschatzl@6402 | 72 | |
tschatzl@6402 | 73 | void G1CodeRootSet::purge_chunks(size_t keep_ratio) { |
tschatzl@6402 | 74 | size_t keep = G1CodeRootSet::_num_chunks_handed_out * keep_ratio / 100; |
tschatzl@6402 | 75 | |
tschatzl@6402 | 76 | if (keep >= (size_t)_free_list.count()) { |
tschatzl@6402 | 77 | return; |
tschatzl@6402 | 78 | } |
tschatzl@6402 | 79 | |
tschatzl@6402 | 80 | FreeList<G1CodeRootChunk> temp; |
tschatzl@6402 | 81 | temp.initialize(); |
tschatzl@6402 | 82 | temp.set_size(G1CodeRootChunk::word_size()); |
tschatzl@6402 | 83 | |
tschatzl@6402 | 84 | _free_list.getFirstNChunksFromList((size_t)_free_list.count() - keep, &temp); |
tschatzl@6402 | 85 | |
tschatzl@6402 | 86 | G1CodeRootChunk* cur = temp.get_chunk_at_head(); |
tschatzl@6402 | 87 | while (cur != NULL) { |
tschatzl@6402 | 88 | delete cur; |
tschatzl@6402 | 89 | cur = temp.get_chunk_at_head(); |
tschatzl@6402 | 90 | } |
tschatzl@6402 | 91 | } |
tschatzl@6402 | 92 | |
tschatzl@6402 | 93 | size_t G1CodeRootSet::static_mem_size() { |
tschatzl@6402 | 94 | return sizeof(_free_list) + sizeof(_num_chunks_handed_out); |
tschatzl@6402 | 95 | } |
tschatzl@6402 | 96 | |
tschatzl@6402 | 97 | size_t G1CodeRootSet::fl_mem_size() { |
tschatzl@6402 | 98 | return _free_list.count() * _free_list.size(); |
tschatzl@6402 | 99 | } |
tschatzl@6402 | 100 | |
tschatzl@6402 | 101 | void G1CodeRootSet::initialize() { |
tschatzl@6402 | 102 | _free_list.initialize(); |
tschatzl@6402 | 103 | _free_list.set_size(G1CodeRootChunk::word_size()); |
tschatzl@6402 | 104 | } |
tschatzl@6402 | 105 | |
tschatzl@6402 | 106 | G1CodeRootSet::G1CodeRootSet() : _list(), _length(0) { |
tschatzl@6402 | 107 | _list.initialize(); |
tschatzl@6402 | 108 | _list.set_size(G1CodeRootChunk::word_size()); |
tschatzl@6402 | 109 | } |
tschatzl@6402 | 110 | |
tschatzl@6402 | 111 | G1CodeRootSet::~G1CodeRootSet() { |
tschatzl@6402 | 112 | clear(); |
tschatzl@6402 | 113 | } |
tschatzl@6402 | 114 | |
tschatzl@6402 | 115 | void G1CodeRootSet::add(nmethod* method) { |
tschatzl@6402 | 116 | if (!contains(method)) { |
tschatzl@6402 | 117 | // Try to add the nmethod. If there is not enough space, get a new chunk. |
tschatzl@6402 | 118 | if (_list.head() == NULL || _list.head()->is_full()) { |
tschatzl@6402 | 119 | G1CodeRootChunk* cur = new_chunk(); |
tschatzl@6402 | 120 | _list.return_chunk_at_head(cur); |
tschatzl@6402 | 121 | } |
tschatzl@6402 | 122 | bool result = _list.head()->add(method); |
tschatzl@6402 | 123 | guarantee(result, err_msg("Not able to add nmethod "PTR_FORMAT" to newly allocated chunk.", method)); |
tschatzl@6402 | 124 | _length++; |
tschatzl@6402 | 125 | } |
tschatzl@6402 | 126 | } |
tschatzl@6402 | 127 | |
tschatzl@6402 | 128 | void G1CodeRootSet::remove(nmethod* method) { |
tschatzl@6402 | 129 | G1CodeRootChunk* found = find(method); |
tschatzl@6402 | 130 | if (found != NULL) { |
tschatzl@6402 | 131 | bool result = found->remove(method); |
tschatzl@6402 | 132 | guarantee(result, err_msg("could not find nmethod "PTR_FORMAT" during removal although we previously found it", method)); |
tschatzl@6402 | 133 | // eventually free completely emptied chunk |
tschatzl@6402 | 134 | if (found->is_empty()) { |
tschatzl@6402 | 135 | _list.remove_chunk(found); |
tschatzl@6402 | 136 | free(found); |
tschatzl@6402 | 137 | } |
tschatzl@6402 | 138 | _length--; |
tschatzl@6402 | 139 | } |
tschatzl@6402 | 140 | assert(!contains(method), err_msg(PTR_FORMAT" still contains nmethod "PTR_FORMAT, this, method)); |
tschatzl@6402 | 141 | } |
tschatzl@6402 | 142 | |
tschatzl@6402 | 143 | nmethod* G1CodeRootSet::pop() { |
tschatzl@6402 | 144 | do { |
tschatzl@6402 | 145 | G1CodeRootChunk* cur = _list.head(); |
tschatzl@6402 | 146 | if (cur == NULL) { |
tschatzl@6402 | 147 | assert(_length == 0, "when there are no chunks, there should be no elements"); |
tschatzl@6402 | 148 | return NULL; |
tschatzl@6402 | 149 | } |
tschatzl@6402 | 150 | nmethod* result = cur->pop(); |
tschatzl@6402 | 151 | if (result != NULL) { |
tschatzl@6402 | 152 | _length--; |
tschatzl@6402 | 153 | return result; |
tschatzl@6402 | 154 | } else { |
tschatzl@6402 | 155 | free(_list.get_chunk_at_head()); |
tschatzl@6402 | 156 | } |
tschatzl@6402 | 157 | } while (true); |
tschatzl@6402 | 158 | } |
tschatzl@6402 | 159 | |
tschatzl@6402 | 160 | G1CodeRootChunk* G1CodeRootSet::find(nmethod* method) { |
tschatzl@6402 | 161 | G1CodeRootChunk* cur = _list.head(); |
tschatzl@6402 | 162 | while (cur != NULL) { |
tschatzl@6402 | 163 | if (cur->contains(method)) { |
tschatzl@6402 | 164 | return cur; |
tschatzl@6402 | 165 | } |
tschatzl@6402 | 166 | cur = (G1CodeRootChunk*)cur->next(); |
tschatzl@6402 | 167 | } |
tschatzl@6402 | 168 | return NULL; |
tschatzl@6402 | 169 | } |
tschatzl@6402 | 170 | |
tschatzl@6402 | 171 | void G1CodeRootSet::free(G1CodeRootChunk* chunk) { |
tschatzl@6402 | 172 | free_chunk(chunk); |
tschatzl@6402 | 173 | } |
tschatzl@6402 | 174 | |
tschatzl@6402 | 175 | bool G1CodeRootSet::contains(nmethod* method) { |
tschatzl@6402 | 176 | return find(method) != NULL; |
tschatzl@6402 | 177 | } |
tschatzl@6402 | 178 | |
tschatzl@6402 | 179 | void G1CodeRootSet::clear() { |
tschatzl@6402 | 180 | free_all_chunks(&_list); |
tschatzl@6402 | 181 | _length = 0; |
tschatzl@6402 | 182 | } |
tschatzl@6402 | 183 | |
tschatzl@6402 | 184 | void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const { |
tschatzl@6402 | 185 | G1CodeRootChunk* cur = _list.head(); |
tschatzl@6402 | 186 | while (cur != NULL) { |
tschatzl@6402 | 187 | cur->nmethods_do(blk); |
tschatzl@6402 | 188 | cur = (G1CodeRootChunk*)cur->next(); |
tschatzl@6402 | 189 | } |
tschatzl@6402 | 190 | } |
tschatzl@6402 | 191 | |
tschatzl@6402 | 192 | size_t G1CodeRootSet::mem_size() { |
tschatzl@6402 | 193 | return sizeof(this) + _list.count() * _list.size(); |
tschatzl@6402 | 194 | } |
tschatzl@6402 | 195 | |
tschatzl@6402 | 196 | #ifndef PRODUCT |
tschatzl@6402 | 197 | |
tschatzl@6402 | 198 | void G1CodeRootSet::test() { |
tschatzl@6402 | 199 | initialize(); |
tschatzl@6402 | 200 | |
tschatzl@6402 | 201 | assert(_free_list.count() == 0, "Free List must be empty"); |
tschatzl@6402 | 202 | assert(_num_chunks_handed_out == 0, "No elements must have been handed out yet"); |
tschatzl@6402 | 203 | |
tschatzl@6402 | 204 | // The number of chunks that we allocate for purge testing. |
tschatzl@6402 | 205 | size_t const num_chunks = 10; |
tschatzl@6402 | 206 | { |
tschatzl@6402 | 207 | G1CodeRootSet set1; |
tschatzl@6402 | 208 | assert(set1.is_empty(), "Code root set must be initially empty but is not."); |
tschatzl@6402 | 209 | |
tschatzl@6402 | 210 | set1.add((nmethod*)1); |
tschatzl@6402 | 211 | assert(_num_chunks_handed_out == 1, |
tschatzl@6402 | 212 | err_msg("Must have allocated and handed out one chunk, but handed out " |
tschatzl@6402 | 213 | SIZE_FORMAT" chunks", _num_chunks_handed_out)); |
tschatzl@6402 | 214 | assert(set1.length() == 1, err_msg("Added exactly one element, but set contains " |
tschatzl@6402 | 215 | SIZE_FORMAT" elements", set1.length())); |
tschatzl@6402 | 216 | |
tschatzl@6402 | 217 | // G1CodeRootChunk::word_size() is larger than G1CodeRootChunk::num_entries which |
tschatzl@6402 | 218 | // we cannot access. |
tschatzl@6402 | 219 | for (uint i = 0; i < G1CodeRootChunk::word_size() + 1; i++) { |
tschatzl@6402 | 220 | set1.add((nmethod*)1); |
tschatzl@6402 | 221 | } |
tschatzl@6402 | 222 | assert(_num_chunks_handed_out == 1, |
tschatzl@6402 | 223 | err_msg("Duplicate detection must have prevented allocation of further " |
tschatzl@6402 | 224 | "chunks but contains "SIZE_FORMAT, _num_chunks_handed_out)); |
tschatzl@6402 | 225 | assert(set1.length() == 1, |
tschatzl@6402 | 226 | err_msg("Duplicate detection should not have increased the set size but " |
tschatzl@6402 | 227 | "is "SIZE_FORMAT, set1.length())); |
tschatzl@6402 | 228 | |
tschatzl@6402 | 229 | size_t num_total_after_add = G1CodeRootChunk::word_size() + 1; |
tschatzl@6402 | 230 | for (size_t i = 0; i < num_total_after_add - 1; i++) { |
tschatzl@6402 | 231 | set1.add((nmethod*)(2 + i)); |
tschatzl@6402 | 232 | } |
tschatzl@6402 | 233 | assert(_num_chunks_handed_out > 1, |
tschatzl@6402 | 234 | "After adding more code roots, more than one chunks should have been handed out"); |
tschatzl@6402 | 235 | assert(set1.length() == num_total_after_add, |
tschatzl@6402 | 236 | err_msg("After adding in total "SIZE_FORMAT" distinct code roots, they " |
tschatzl@6402 | 237 | "need to be in the set, but there are only "SIZE_FORMAT, |
tschatzl@6402 | 238 | num_total_after_add, set1.length())); |
tschatzl@6402 | 239 | |
tschatzl@6402 | 240 | size_t num_popped = 0; |
tschatzl@6402 | 241 | while (set1.pop() != NULL) { |
tschatzl@6402 | 242 | num_popped++; |
tschatzl@6402 | 243 | } |
tschatzl@6402 | 244 | assert(num_popped == num_total_after_add, |
tschatzl@6402 | 245 | err_msg("Managed to pop "SIZE_FORMAT" code roots, but only "SIZE_FORMAT" " |
tschatzl@6402 | 246 | "were added", num_popped, num_total_after_add)); |
tschatzl@6402 | 247 | assert(_num_chunks_handed_out == 0, |
tschatzl@6402 | 248 | err_msg("After popping all elements, all chunks must have been returned " |
tschatzl@6402 | 249 | "but are still "SIZE_FORMAT, _num_chunks_handed_out)); |
tschatzl@6402 | 250 | |
tschatzl@6402 | 251 | purge_chunks(0); |
tschatzl@6402 | 252 | assert(_free_list.count() == 0, |
tschatzl@6402 | 253 | err_msg("After purging everything, the free list must be empty but still " |
tschatzl@6402 | 254 | "contains "SIZE_FORMAT" chunks", _free_list.count())); |
tschatzl@6402 | 255 | |
tschatzl@6402 | 256 | // Add some more handed out chunks. |
tschatzl@6402 | 257 | size_t i = 0; |
tschatzl@6402 | 258 | while (_num_chunks_handed_out < num_chunks) { |
tschatzl@6402 | 259 | set1.add((nmethod*)i); |
tschatzl@6402 | 260 | i++; |
tschatzl@6402 | 261 | } |
tschatzl@6402 | 262 | |
tschatzl@6402 | 263 | { |
tschatzl@6402 | 264 | // Generate chunks on the free list. |
tschatzl@6402 | 265 | G1CodeRootSet set2; |
tschatzl@6402 | 266 | size_t i = 0; |
tschatzl@6402 | 267 | while (_num_chunks_handed_out < num_chunks * 2) { |
tschatzl@6402 | 268 | set2.add((nmethod*)i); |
tschatzl@6402 | 269 | i++; |
tschatzl@6402 | 270 | } |
tschatzl@6402 | 271 | // Exit of the scope of the set2 object will call the destructor that generates |
tschatzl@6402 | 272 | // num_chunks elements on the free list. |
tschatzl@6402 | 273 | } |
tschatzl@6402 | 274 | |
tschatzl@6402 | 275 | assert(_num_chunks_handed_out == num_chunks, |
tschatzl@6402 | 276 | err_msg("Deletion of the second set must have resulted in giving back " |
tschatzl@6402 | 277 | "those, but there is still "SIZE_FORMAT" handed out, expecting " |
tschatzl@6402 | 278 | SIZE_FORMAT, _num_chunks_handed_out, num_chunks)); |
tschatzl@6402 | 279 | assert((size_t)_free_list.count() == num_chunks, |
tschatzl@6402 | 280 | err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list " |
tschatzl@6402 | 281 | "but there are only "SIZE_FORMAT, num_chunks, _free_list.count())); |
tschatzl@6402 | 282 | |
tschatzl@6402 | 283 | size_t const test_percentage = 50; |
tschatzl@6402 | 284 | purge_chunks(test_percentage); |
tschatzl@6402 | 285 | assert(_num_chunks_handed_out == num_chunks, |
tschatzl@6402 | 286 | err_msg("Purging must not hand out chunks but there are "SIZE_FORMAT, |
tschatzl@6402 | 287 | _num_chunks_handed_out)); |
tschatzl@6402 | 288 | assert((size_t)_free_list.count() == (ssize_t)(num_chunks * test_percentage / 100), |
tschatzl@6402 | 289 | err_msg("Must have purged "SIZE_FORMAT" percent of "SIZE_FORMAT" chunks" |
tschatzl@6402 | 290 | "but there are "SSIZE_FORMAT, test_percentage, num_chunks, |
tschatzl@6402 | 291 | _free_list.count())); |
tschatzl@6402 | 292 | // Purge the remainder of the chunks on the free list. |
tschatzl@6402 | 293 | purge_chunks(0); |
tschatzl@6402 | 294 | assert(_free_list.count() == 0, "Free List must be empty"); |
tschatzl@6402 | 295 | assert(_num_chunks_handed_out == num_chunks, |
tschatzl@6402 | 296 | err_msg("Expected to be "SIZE_FORMAT" chunks handed out from the first set " |
tschatzl@6402 | 297 | "but there are "SIZE_FORMAT, num_chunks, _num_chunks_handed_out)); |
tschatzl@6402 | 298 | |
tschatzl@6402 | 299 | // Exit of the scope of the set1 object will call the destructor that generates |
tschatzl@6402 | 300 | // num_chunks additional elements on the free list. |
tschatzl@6402 | 301 | } |
tschatzl@6402 | 302 | |
tschatzl@6402 | 303 | assert(_num_chunks_handed_out == 0, |
tschatzl@6402 | 304 | err_msg("Deletion of the only set must have resulted in no chunks handed " |
tschatzl@6402 | 305 | "out, but there is still "SIZE_FORMAT" handed out", _num_chunks_handed_out)); |
tschatzl@6402 | 306 | assert((size_t)_free_list.count() == num_chunks, |
tschatzl@6402 | 307 | err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list " |
tschatzl@6402 | 308 | "but there are only "SSIZE_FORMAT, num_chunks, _free_list.count())); |
tschatzl@6402 | 309 | |
tschatzl@6402 | 310 | // Restore initial state. |
tschatzl@6402 | 311 | purge_chunks(0); |
tschatzl@6402 | 312 | assert(_free_list.count() == 0, "Free List must be empty"); |
tschatzl@6402 | 313 | assert(_num_chunks_handed_out == 0, "No elements must have been handed out yet"); |
tschatzl@6402 | 314 | } |
tschatzl@6402 | 315 | |
tschatzl@6402 | 316 | void TestCodeCacheRemSet_test() { |
tschatzl@6402 | 317 | G1CodeRootSet::test(); |
tschatzl@6402 | 318 | } |
tschatzl@6402 | 319 | #endif |