Tue, 18 Mar 2014 19:07:22 +0100
8029075: String deduplication in G1
Summary: Implementation of JEP 192, http://openjdk.java.net/jeps/192
Reviewed-by: brutisso, tschatzl, coleenp
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 | |
tschatzl@6402 | 31 | G1CodeRootChunk::G1CodeRootChunk() : _top(NULL), _next(NULL), _prev(NULL) { |
tschatzl@6402 | 32 | _top = bottom(); |
tschatzl@6402 | 33 | } |
tschatzl@6402 | 34 | |
tschatzl@6402 | 35 | void G1CodeRootChunk::reset() { |
tschatzl@6402 | 36 | _next = _prev = NULL; |
tschatzl@6402 | 37 | _top = bottom(); |
tschatzl@6402 | 38 | } |
tschatzl@6402 | 39 | |
tschatzl@6402 | 40 | void G1CodeRootChunk::nmethods_do(CodeBlobClosure* cl) { |
tschatzl@6402 | 41 | nmethod** cur = bottom(); |
tschatzl@6402 | 42 | while (cur != _top) { |
tschatzl@6402 | 43 | cl->do_code_blob(*cur); |
tschatzl@6402 | 44 | cur++; |
tschatzl@6402 | 45 | } |
tschatzl@6402 | 46 | } |
tschatzl@6402 | 47 | |
tschatzl@6402 | 48 | FreeList<G1CodeRootChunk> G1CodeRootSet::_free_list; |
tschatzl@6402 | 49 | size_t G1CodeRootSet::_num_chunks_handed_out = 0; |
tschatzl@6402 | 50 | |
tschatzl@6402 | 51 | G1CodeRootChunk* G1CodeRootSet::new_chunk() { |
tschatzl@6402 | 52 | G1CodeRootChunk* result = _free_list.get_chunk_at_head(); |
tschatzl@6402 | 53 | if (result == NULL) { |
tschatzl@6402 | 54 | result = new G1CodeRootChunk(); |
tschatzl@6402 | 55 | } |
tschatzl@6402 | 56 | G1CodeRootSet::_num_chunks_handed_out++; |
tschatzl@6402 | 57 | result->reset(); |
tschatzl@6402 | 58 | return result; |
tschatzl@6402 | 59 | } |
tschatzl@6402 | 60 | |
tschatzl@6402 | 61 | void G1CodeRootSet::free_chunk(G1CodeRootChunk* chunk) { |
tschatzl@6402 | 62 | _free_list.return_chunk_at_head(chunk); |
tschatzl@6402 | 63 | G1CodeRootSet::_num_chunks_handed_out--; |
tschatzl@6402 | 64 | } |
tschatzl@6402 | 65 | |
tschatzl@6402 | 66 | void G1CodeRootSet::free_all_chunks(FreeList<G1CodeRootChunk>* list) { |
tschatzl@6402 | 67 | G1CodeRootSet::_num_chunks_handed_out -= list->count(); |
tschatzl@6402 | 68 | _free_list.prepend(list); |
tschatzl@6402 | 69 | } |
tschatzl@6402 | 70 | |
tschatzl@6402 | 71 | void G1CodeRootSet::purge_chunks(size_t keep_ratio) { |
tschatzl@6402 | 72 | size_t keep = G1CodeRootSet::_num_chunks_handed_out * keep_ratio / 100; |
tschatzl@6402 | 73 | |
tschatzl@6402 | 74 | if (keep >= (size_t)_free_list.count()) { |
tschatzl@6402 | 75 | return; |
tschatzl@6402 | 76 | } |
tschatzl@6402 | 77 | |
tschatzl@6402 | 78 | FreeList<G1CodeRootChunk> temp; |
tschatzl@6402 | 79 | temp.initialize(); |
tschatzl@6402 | 80 | temp.set_size(G1CodeRootChunk::word_size()); |
tschatzl@6402 | 81 | |
tschatzl@6402 | 82 | _free_list.getFirstNChunksFromList((size_t)_free_list.count() - keep, &temp); |
tschatzl@6402 | 83 | |
tschatzl@6402 | 84 | G1CodeRootChunk* cur = temp.get_chunk_at_head(); |
tschatzl@6402 | 85 | while (cur != NULL) { |
tschatzl@6402 | 86 | delete cur; |
tschatzl@6402 | 87 | cur = temp.get_chunk_at_head(); |
tschatzl@6402 | 88 | } |
tschatzl@6402 | 89 | } |
tschatzl@6402 | 90 | |
tschatzl@6402 | 91 | size_t G1CodeRootSet::static_mem_size() { |
tschatzl@6402 | 92 | return sizeof(_free_list) + sizeof(_num_chunks_handed_out); |
tschatzl@6402 | 93 | } |
tschatzl@6402 | 94 | |
tschatzl@6402 | 95 | size_t G1CodeRootSet::fl_mem_size() { |
tschatzl@6402 | 96 | return _free_list.count() * _free_list.size(); |
tschatzl@6402 | 97 | } |
tschatzl@6402 | 98 | |
tschatzl@6402 | 99 | void G1CodeRootSet::initialize() { |
tschatzl@6402 | 100 | _free_list.initialize(); |
tschatzl@6402 | 101 | _free_list.set_size(G1CodeRootChunk::word_size()); |
tschatzl@6402 | 102 | } |
tschatzl@6402 | 103 | |
tschatzl@6402 | 104 | G1CodeRootSet::G1CodeRootSet() : _list(), _length(0) { |
tschatzl@6402 | 105 | _list.initialize(); |
tschatzl@6402 | 106 | _list.set_size(G1CodeRootChunk::word_size()); |
tschatzl@6402 | 107 | } |
tschatzl@6402 | 108 | |
tschatzl@6402 | 109 | G1CodeRootSet::~G1CodeRootSet() { |
tschatzl@6402 | 110 | clear(); |
tschatzl@6402 | 111 | } |
tschatzl@6402 | 112 | |
tschatzl@6402 | 113 | void G1CodeRootSet::add(nmethod* method) { |
tschatzl@6402 | 114 | if (!contains(method)) { |
tschatzl@6402 | 115 | // Try to add the nmethod. If there is not enough space, get a new chunk. |
tschatzl@6402 | 116 | if (_list.head() == NULL || _list.head()->is_full()) { |
tschatzl@6402 | 117 | G1CodeRootChunk* cur = new_chunk(); |
tschatzl@6402 | 118 | _list.return_chunk_at_head(cur); |
tschatzl@6402 | 119 | } |
tschatzl@6402 | 120 | bool result = _list.head()->add(method); |
tschatzl@6402 | 121 | guarantee(result, err_msg("Not able to add nmethod "PTR_FORMAT" to newly allocated chunk.", method)); |
tschatzl@6402 | 122 | _length++; |
tschatzl@6402 | 123 | } |
tschatzl@6402 | 124 | } |
tschatzl@6402 | 125 | |
tschatzl@6402 | 126 | void G1CodeRootSet::remove(nmethod* method) { |
tschatzl@6402 | 127 | G1CodeRootChunk* found = find(method); |
tschatzl@6402 | 128 | if (found != NULL) { |
tschatzl@6402 | 129 | bool result = found->remove(method); |
tschatzl@6402 | 130 | guarantee(result, err_msg("could not find nmethod "PTR_FORMAT" during removal although we previously found it", method)); |
tschatzl@6402 | 131 | // eventually free completely emptied chunk |
tschatzl@6402 | 132 | if (found->is_empty()) { |
tschatzl@6402 | 133 | _list.remove_chunk(found); |
tschatzl@6402 | 134 | free(found); |
tschatzl@6402 | 135 | } |
tschatzl@6402 | 136 | _length--; |
tschatzl@6402 | 137 | } |
tschatzl@6402 | 138 | assert(!contains(method), err_msg(PTR_FORMAT" still contains nmethod "PTR_FORMAT, this, method)); |
tschatzl@6402 | 139 | } |
tschatzl@6402 | 140 | |
tschatzl@6402 | 141 | nmethod* G1CodeRootSet::pop() { |
tschatzl@6402 | 142 | do { |
tschatzl@6402 | 143 | G1CodeRootChunk* cur = _list.head(); |
tschatzl@6402 | 144 | if (cur == NULL) { |
tschatzl@6402 | 145 | assert(_length == 0, "when there are no chunks, there should be no elements"); |
tschatzl@6402 | 146 | return NULL; |
tschatzl@6402 | 147 | } |
tschatzl@6402 | 148 | nmethod* result = cur->pop(); |
tschatzl@6402 | 149 | if (result != NULL) { |
tschatzl@6402 | 150 | _length--; |
tschatzl@6402 | 151 | return result; |
tschatzl@6402 | 152 | } else { |
tschatzl@6402 | 153 | free(_list.get_chunk_at_head()); |
tschatzl@6402 | 154 | } |
tschatzl@6402 | 155 | } while (true); |
tschatzl@6402 | 156 | } |
tschatzl@6402 | 157 | |
tschatzl@6402 | 158 | G1CodeRootChunk* G1CodeRootSet::find(nmethod* method) { |
tschatzl@6402 | 159 | G1CodeRootChunk* cur = _list.head(); |
tschatzl@6402 | 160 | while (cur != NULL) { |
tschatzl@6402 | 161 | if (cur->contains(method)) { |
tschatzl@6402 | 162 | return cur; |
tschatzl@6402 | 163 | } |
tschatzl@6402 | 164 | cur = (G1CodeRootChunk*)cur->next(); |
tschatzl@6402 | 165 | } |
tschatzl@6402 | 166 | return NULL; |
tschatzl@6402 | 167 | } |
tschatzl@6402 | 168 | |
tschatzl@6402 | 169 | void G1CodeRootSet::free(G1CodeRootChunk* chunk) { |
tschatzl@6402 | 170 | free_chunk(chunk); |
tschatzl@6402 | 171 | } |
tschatzl@6402 | 172 | |
tschatzl@6402 | 173 | bool G1CodeRootSet::contains(nmethod* method) { |
tschatzl@6402 | 174 | return find(method) != NULL; |
tschatzl@6402 | 175 | } |
tschatzl@6402 | 176 | |
tschatzl@6402 | 177 | void G1CodeRootSet::clear() { |
tschatzl@6402 | 178 | free_all_chunks(&_list); |
tschatzl@6402 | 179 | _length = 0; |
tschatzl@6402 | 180 | } |
tschatzl@6402 | 181 | |
tschatzl@6402 | 182 | void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const { |
tschatzl@6402 | 183 | G1CodeRootChunk* cur = _list.head(); |
tschatzl@6402 | 184 | while (cur != NULL) { |
tschatzl@6402 | 185 | cur->nmethods_do(blk); |
tschatzl@6402 | 186 | cur = (G1CodeRootChunk*)cur->next(); |
tschatzl@6402 | 187 | } |
tschatzl@6402 | 188 | } |
tschatzl@6402 | 189 | |
tschatzl@6402 | 190 | size_t G1CodeRootSet::mem_size() { |
tschatzl@6402 | 191 | return sizeof(this) + _list.count() * _list.size(); |
tschatzl@6402 | 192 | } |
tschatzl@6402 | 193 | |
tschatzl@6402 | 194 | #ifndef PRODUCT |
tschatzl@6402 | 195 | |
tschatzl@6402 | 196 | void G1CodeRootSet::test() { |
tschatzl@6402 | 197 | initialize(); |
tschatzl@6402 | 198 | |
tschatzl@6402 | 199 | assert(_free_list.count() == 0, "Free List must be empty"); |
tschatzl@6402 | 200 | assert(_num_chunks_handed_out == 0, "No elements must have been handed out yet"); |
tschatzl@6402 | 201 | |
tschatzl@6402 | 202 | // The number of chunks that we allocate for purge testing. |
tschatzl@6402 | 203 | size_t const num_chunks = 10; |
tschatzl@6402 | 204 | { |
tschatzl@6402 | 205 | G1CodeRootSet set1; |
tschatzl@6402 | 206 | assert(set1.is_empty(), "Code root set must be initially empty but is not."); |
tschatzl@6402 | 207 | |
tschatzl@6402 | 208 | set1.add((nmethod*)1); |
tschatzl@6402 | 209 | assert(_num_chunks_handed_out == 1, |
tschatzl@6402 | 210 | err_msg("Must have allocated and handed out one chunk, but handed out " |
tschatzl@6402 | 211 | SIZE_FORMAT" chunks", _num_chunks_handed_out)); |
tschatzl@6402 | 212 | assert(set1.length() == 1, err_msg("Added exactly one element, but set contains " |
tschatzl@6402 | 213 | SIZE_FORMAT" elements", set1.length())); |
tschatzl@6402 | 214 | |
tschatzl@6402 | 215 | // G1CodeRootChunk::word_size() is larger than G1CodeRootChunk::num_entries which |
tschatzl@6402 | 216 | // we cannot access. |
tschatzl@6402 | 217 | for (uint i = 0; i < G1CodeRootChunk::word_size() + 1; i++) { |
tschatzl@6402 | 218 | set1.add((nmethod*)1); |
tschatzl@6402 | 219 | } |
tschatzl@6402 | 220 | assert(_num_chunks_handed_out == 1, |
tschatzl@6402 | 221 | err_msg("Duplicate detection must have prevented allocation of further " |
tschatzl@6402 | 222 | "chunks but contains "SIZE_FORMAT, _num_chunks_handed_out)); |
tschatzl@6402 | 223 | assert(set1.length() == 1, |
tschatzl@6402 | 224 | err_msg("Duplicate detection should not have increased the set size but " |
tschatzl@6402 | 225 | "is "SIZE_FORMAT, set1.length())); |
tschatzl@6402 | 226 | |
tschatzl@6402 | 227 | size_t num_total_after_add = G1CodeRootChunk::word_size() + 1; |
tschatzl@6402 | 228 | for (size_t i = 0; i < num_total_after_add - 1; i++) { |
tschatzl@6402 | 229 | set1.add((nmethod*)(2 + i)); |
tschatzl@6402 | 230 | } |
tschatzl@6402 | 231 | assert(_num_chunks_handed_out > 1, |
tschatzl@6402 | 232 | "After adding more code roots, more than one chunks should have been handed out"); |
tschatzl@6402 | 233 | assert(set1.length() == num_total_after_add, |
tschatzl@6402 | 234 | err_msg("After adding in total "SIZE_FORMAT" distinct code roots, they " |
tschatzl@6402 | 235 | "need to be in the set, but there are only "SIZE_FORMAT, |
tschatzl@6402 | 236 | num_total_after_add, set1.length())); |
tschatzl@6402 | 237 | |
tschatzl@6402 | 238 | size_t num_popped = 0; |
tschatzl@6402 | 239 | while (set1.pop() != NULL) { |
tschatzl@6402 | 240 | num_popped++; |
tschatzl@6402 | 241 | } |
tschatzl@6402 | 242 | assert(num_popped == num_total_after_add, |
tschatzl@6402 | 243 | err_msg("Managed to pop "SIZE_FORMAT" code roots, but only "SIZE_FORMAT" " |
tschatzl@6402 | 244 | "were added", num_popped, num_total_after_add)); |
tschatzl@6402 | 245 | assert(_num_chunks_handed_out == 0, |
tschatzl@6402 | 246 | err_msg("After popping all elements, all chunks must have been returned " |
tschatzl@6402 | 247 | "but are still "SIZE_FORMAT, _num_chunks_handed_out)); |
tschatzl@6402 | 248 | |
tschatzl@6402 | 249 | purge_chunks(0); |
tschatzl@6402 | 250 | assert(_free_list.count() == 0, |
tschatzl@6402 | 251 | err_msg("After purging everything, the free list must be empty but still " |
tschatzl@6402 | 252 | "contains "SIZE_FORMAT" chunks", _free_list.count())); |
tschatzl@6402 | 253 | |
tschatzl@6402 | 254 | // Add some more handed out chunks. |
tschatzl@6402 | 255 | size_t i = 0; |
tschatzl@6402 | 256 | while (_num_chunks_handed_out < num_chunks) { |
tschatzl@6402 | 257 | set1.add((nmethod*)i); |
tschatzl@6402 | 258 | i++; |
tschatzl@6402 | 259 | } |
tschatzl@6402 | 260 | |
tschatzl@6402 | 261 | { |
tschatzl@6402 | 262 | // Generate chunks on the free list. |
tschatzl@6402 | 263 | G1CodeRootSet set2; |
tschatzl@6402 | 264 | size_t i = 0; |
tschatzl@6402 | 265 | while (_num_chunks_handed_out < num_chunks * 2) { |
tschatzl@6402 | 266 | set2.add((nmethod*)i); |
tschatzl@6402 | 267 | i++; |
tschatzl@6402 | 268 | } |
tschatzl@6402 | 269 | // Exit of the scope of the set2 object will call the destructor that generates |
tschatzl@6402 | 270 | // num_chunks elements on the free list. |
tschatzl@6402 | 271 | } |
tschatzl@6402 | 272 | |
tschatzl@6402 | 273 | assert(_num_chunks_handed_out == num_chunks, |
tschatzl@6402 | 274 | err_msg("Deletion of the second set must have resulted in giving back " |
tschatzl@6402 | 275 | "those, but there is still "SIZE_FORMAT" handed out, expecting " |
tschatzl@6402 | 276 | SIZE_FORMAT, _num_chunks_handed_out, num_chunks)); |
tschatzl@6402 | 277 | assert((size_t)_free_list.count() == num_chunks, |
tschatzl@6402 | 278 | err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list " |
tschatzl@6402 | 279 | "but there are only "SIZE_FORMAT, num_chunks, _free_list.count())); |
tschatzl@6402 | 280 | |
tschatzl@6402 | 281 | size_t const test_percentage = 50; |
tschatzl@6402 | 282 | purge_chunks(test_percentage); |
tschatzl@6402 | 283 | assert(_num_chunks_handed_out == num_chunks, |
tschatzl@6402 | 284 | err_msg("Purging must not hand out chunks but there are "SIZE_FORMAT, |
tschatzl@6402 | 285 | _num_chunks_handed_out)); |
tschatzl@6402 | 286 | assert((size_t)_free_list.count() == (ssize_t)(num_chunks * test_percentage / 100), |
tschatzl@6402 | 287 | err_msg("Must have purged "SIZE_FORMAT" percent of "SIZE_FORMAT" chunks" |
tschatzl@6402 | 288 | "but there are "SSIZE_FORMAT, test_percentage, num_chunks, |
tschatzl@6402 | 289 | _free_list.count())); |
tschatzl@6402 | 290 | // Purge the remainder of the chunks on the free list. |
tschatzl@6402 | 291 | purge_chunks(0); |
tschatzl@6402 | 292 | assert(_free_list.count() == 0, "Free List must be empty"); |
tschatzl@6402 | 293 | assert(_num_chunks_handed_out == num_chunks, |
tschatzl@6402 | 294 | err_msg("Expected to be "SIZE_FORMAT" chunks handed out from the first set " |
tschatzl@6402 | 295 | "but there are "SIZE_FORMAT, num_chunks, _num_chunks_handed_out)); |
tschatzl@6402 | 296 | |
tschatzl@6402 | 297 | // Exit of the scope of the set1 object will call the destructor that generates |
tschatzl@6402 | 298 | // num_chunks additional elements on the free list. |
tschatzl@6402 | 299 | } |
tschatzl@6402 | 300 | |
tschatzl@6402 | 301 | assert(_num_chunks_handed_out == 0, |
tschatzl@6402 | 302 | err_msg("Deletion of the only set must have resulted in no chunks handed " |
tschatzl@6402 | 303 | "out, but there is still "SIZE_FORMAT" handed out", _num_chunks_handed_out)); |
tschatzl@6402 | 304 | assert((size_t)_free_list.count() == num_chunks, |
tschatzl@6402 | 305 | err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list " |
tschatzl@6402 | 306 | "but there are only "SSIZE_FORMAT, num_chunks, _free_list.count())); |
tschatzl@6402 | 307 | |
tschatzl@6402 | 308 | // Restore initial state. |
tschatzl@6402 | 309 | purge_chunks(0); |
tschatzl@6402 | 310 | assert(_free_list.count() == 0, "Free List must be empty"); |
tschatzl@6402 | 311 | assert(_num_chunks_handed_out == 0, "No elements must have been handed out yet"); |
tschatzl@6402 | 312 | } |
tschatzl@6402 | 313 | |
tschatzl@6402 | 314 | void TestCodeCacheRemSet_test() { |
tschatzl@6402 | 315 | G1CodeRootSet::test(); |
tschatzl@6402 | 316 | } |
tschatzl@6402 | 317 | #endif |