src/share/vm/gc_implementation/g1/g1CodeCacheRemSet.cpp

Mon, 07 Jul 2014 10:12:40 +0200

author
stefank
date
Mon, 07 Jul 2014 10:12:40 +0200
changeset 6992
2c6ef90f030a
parent 6932
828056cf311f
child 6993
870c03421152
permissions
-rw-r--r--

8049421: G1 Class Unloading after completing a concurrent mark cycle
Reviewed-by: tschatzl, ehelin, brutisso, coleenp, roland, iveresov
Contributed-by: stefan.karlsson@oracle.com, mikael.gerdin@oracle.com

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
stefank@6992 33 G1CodeRootChunk::G1CodeRootChunk() : _top(NULL), _next(NULL), _prev(NULL), _free(NULL) {
tschatzl@6402 34 _top = bottom();
tschatzl@6402 35 }
tschatzl@6402 36
tschatzl@6402 37 void G1CodeRootChunk::reset() {
tschatzl@6402 38 _next = _prev = NULL;
stefank@6992 39 _free = NULL;
tschatzl@6402 40 _top = bottom();
tschatzl@6402 41 }
tschatzl@6402 42
tschatzl@6402 43 void G1CodeRootChunk::nmethods_do(CodeBlobClosure* cl) {
stefank@6992 44 NmethodOrLink* cur = bottom();
tschatzl@6402 45 while (cur != _top) {
stefank@6992 46 if (is_nmethod(cur)) {
stefank@6992 47 cl->do_code_blob(cur->_nmethod);
stefank@6992 48 }
tschatzl@6402 49 cur++;
tschatzl@6402 50 }
tschatzl@6402 51 }
tschatzl@6402 52
stefank@6992 53 bool G1CodeRootChunk::remove_lock_free(nmethod* method) {
stefank@6992 54 NmethodOrLink* cur = bottom();
stefank@6992 55
stefank@6992 56 for (NmethodOrLink* cur = bottom(); cur != _top; cur++) {
stefank@6992 57 if (cur->_nmethod == method) {
stefank@6992 58 bool result = Atomic::cmpxchg_ptr(NULL, &cur->_nmethod, method) == method;
stefank@6992 59
stefank@6992 60 if (!result) {
stefank@6992 61 // Someone else cleared out this entry.
stefank@6992 62 return false;
stefank@6992 63 }
stefank@6992 64
stefank@6992 65 // The method was cleared. Time to link it into the free list.
stefank@6992 66 NmethodOrLink* prev_free;
stefank@6992 67 do {
stefank@6992 68 prev_free = (NmethodOrLink*)_free;
stefank@6992 69 cur->_link = prev_free;
stefank@6992 70 } while (Atomic::cmpxchg_ptr(cur, &_free, prev_free) != prev_free);
stefank@6992 71
stefank@6992 72 return true;
stefank@6992 73 }
stefank@6992 74 }
stefank@6992 75
stefank@6992 76 return false;
stefank@6992 77 }
stefank@6992 78
tschatzl@6925 79 G1CodeRootChunkManager::G1CodeRootChunkManager() : _free_list(), _num_chunks_handed_out(0) {
tschatzl@6925 80 _free_list.initialize();
tschatzl@6925 81 _free_list.set_size(G1CodeRootChunk::word_size());
tschatzl@6402 82 }
tschatzl@6402 83
tschatzl@6925 84 size_t G1CodeRootChunkManager::fl_mem_size() {
tschatzl@6925 85 return _free_list.count() * _free_list.size();
tschatzl@6402 86 }
tschatzl@6402 87
tschatzl@6925 88 void G1CodeRootChunkManager::free_all_chunks(FreeList<G1CodeRootChunk>* list) {
tschatzl@6925 89 _num_chunks_handed_out -= list->count();
tschatzl@6402 90 _free_list.prepend(list);
tschatzl@6402 91 }
tschatzl@6402 92
tschatzl@6925 93 void G1CodeRootChunkManager::free_chunk(G1CodeRootChunk* chunk) {
tschatzl@6925 94 _free_list.return_chunk_at_head(chunk);
tschatzl@6925 95 _num_chunks_handed_out--;
tschatzl@6925 96 }
tschatzl@6402 97
tschatzl@6925 98 void G1CodeRootChunkManager::purge_chunks(size_t keep_ratio) {
tschatzl@6925 99 size_t keep = _num_chunks_handed_out * keep_ratio / 100;
tschatzl@6402 100 if (keep >= (size_t)_free_list.count()) {
tschatzl@6402 101 return;
tschatzl@6402 102 }
tschatzl@6402 103
tschatzl@6402 104 FreeList<G1CodeRootChunk> temp;
tschatzl@6402 105 temp.initialize();
tschatzl@6402 106 temp.set_size(G1CodeRootChunk::word_size());
tschatzl@6402 107
tschatzl@6402 108 _free_list.getFirstNChunksFromList((size_t)_free_list.count() - keep, &temp);
tschatzl@6402 109
tschatzl@6402 110 G1CodeRootChunk* cur = temp.get_chunk_at_head();
tschatzl@6402 111 while (cur != NULL) {
tschatzl@6402 112 delete cur;
tschatzl@6402 113 cur = temp.get_chunk_at_head();
tschatzl@6402 114 }
tschatzl@6402 115 }
tschatzl@6402 116
tschatzl@6925 117 size_t G1CodeRootChunkManager::static_mem_size() {
tschatzl@6932 118 return sizeof(G1CodeRootChunkManager);
tschatzl@6402 119 }
tschatzl@6402 120
tschatzl@6925 121
tschatzl@6925 122 G1CodeRootChunk* G1CodeRootChunkManager::new_chunk() {
tschatzl@6925 123 G1CodeRootChunk* result = _free_list.get_chunk_at_head();
tschatzl@6925 124 if (result == NULL) {
tschatzl@6925 125 result = new G1CodeRootChunk();
tschatzl@6925 126 }
tschatzl@6925 127 _num_chunks_handed_out++;
tschatzl@6925 128 result->reset();
tschatzl@6925 129 return result;
tschatzl@6402 130 }
tschatzl@6402 131
tschatzl@6925 132 #ifndef PRODUCT
tschatzl@6925 133
tschatzl@6925 134 size_t G1CodeRootChunkManager::num_chunks_handed_out() const {
tschatzl@6925 135 return _num_chunks_handed_out;
tschatzl@6402 136 }
tschatzl@6402 137
tschatzl@6925 138 size_t G1CodeRootChunkManager::num_free_chunks() const {
tschatzl@6925 139 return (size_t)_free_list.count();
tschatzl@6925 140 }
tschatzl@6925 141
tschatzl@6925 142 #endif
tschatzl@6925 143
tschatzl@6925 144 G1CodeRootChunkManager G1CodeRootSet::_default_chunk_manager;
tschatzl@6925 145
tschatzl@6925 146 void G1CodeRootSet::purge_chunks(size_t keep_ratio) {
tschatzl@6925 147 _default_chunk_manager.purge_chunks(keep_ratio);
tschatzl@6925 148 }
tschatzl@6925 149
tschatzl@6932 150 size_t G1CodeRootSet::free_chunks_static_mem_size() {
tschatzl@6925 151 return _default_chunk_manager.static_mem_size();
tschatzl@6925 152 }
tschatzl@6925 153
tschatzl@6925 154 size_t G1CodeRootSet::free_chunks_mem_size() {
tschatzl@6925 155 return _default_chunk_manager.fl_mem_size();
tschatzl@6925 156 }
tschatzl@6925 157
tschatzl@6925 158 G1CodeRootSet::G1CodeRootSet(G1CodeRootChunkManager* manager) : _manager(manager), _list(), _length(0) {
tschatzl@6925 159 if (_manager == NULL) {
tschatzl@6925 160 _manager = &_default_chunk_manager;
tschatzl@6925 161 }
tschatzl@6402 162 _list.initialize();
tschatzl@6402 163 _list.set_size(G1CodeRootChunk::word_size());
tschatzl@6402 164 }
tschatzl@6402 165
tschatzl@6402 166 G1CodeRootSet::~G1CodeRootSet() {
tschatzl@6402 167 clear();
tschatzl@6402 168 }
tschatzl@6402 169
tschatzl@6402 170 void G1CodeRootSet::add(nmethod* method) {
tschatzl@6402 171 if (!contains(method)) {
stefank@6992 172 // Find the first chunk thatisn't full.
stefank@6992 173 G1CodeRootChunk* cur = _list.head();
stefank@6992 174 while (cur != NULL) {
stefank@6992 175 if (!cur->is_full()) {
stefank@6992 176 break;
stefank@6992 177 }
stefank@6992 178 cur = cur->next();
stefank@6992 179 }
stefank@6992 180
stefank@6992 181 // All chunks are full, get a new chunk.
stefank@6992 182 if (cur == NULL) {
stefank@6992 183 cur = new_chunk();
tschatzl@6402 184 _list.return_chunk_at_head(cur);
tschatzl@6402 185 }
stefank@6992 186
stefank@6992 187 // Add the nmethod.
stefank@6992 188 bool result = cur->add(method);
stefank@6992 189
tschatzl@6402 190 guarantee(result, err_msg("Not able to add nmethod "PTR_FORMAT" to newly allocated chunk.", method));
stefank@6992 191
tschatzl@6402 192 _length++;
tschatzl@6402 193 }
tschatzl@6402 194 }
tschatzl@6402 195
stefank@6992 196 void G1CodeRootSet::remove_lock_free(nmethod* method) {
tschatzl@6402 197 G1CodeRootChunk* found = find(method);
tschatzl@6402 198 if (found != NULL) {
stefank@6992 199 bool result = found->remove_lock_free(method);
stefank@6992 200 if (result) {
stefank@6992 201 Atomic::dec_ptr((volatile intptr_t*)&_length);
tschatzl@6402 202 }
tschatzl@6402 203 }
tschatzl@6402 204 assert(!contains(method), err_msg(PTR_FORMAT" still contains nmethod "PTR_FORMAT, this, method));
tschatzl@6402 205 }
tschatzl@6402 206
tschatzl@6402 207 nmethod* G1CodeRootSet::pop() {
stefank@6992 208 while (true) {
tschatzl@6402 209 G1CodeRootChunk* cur = _list.head();
tschatzl@6402 210 if (cur == NULL) {
tschatzl@6402 211 assert(_length == 0, "when there are no chunks, there should be no elements");
tschatzl@6402 212 return NULL;
tschatzl@6402 213 }
tschatzl@6402 214 nmethod* result = cur->pop();
tschatzl@6402 215 if (result != NULL) {
tschatzl@6402 216 _length--;
tschatzl@6402 217 return result;
tschatzl@6402 218 } else {
tschatzl@6402 219 free(_list.get_chunk_at_head());
tschatzl@6402 220 }
stefank@6992 221 }
tschatzl@6402 222 }
tschatzl@6402 223
tschatzl@6402 224 G1CodeRootChunk* G1CodeRootSet::find(nmethod* method) {
tschatzl@6402 225 G1CodeRootChunk* cur = _list.head();
tschatzl@6402 226 while (cur != NULL) {
tschatzl@6402 227 if (cur->contains(method)) {
tschatzl@6402 228 return cur;
tschatzl@6402 229 }
tschatzl@6402 230 cur = (G1CodeRootChunk*)cur->next();
tschatzl@6402 231 }
tschatzl@6402 232 return NULL;
tschatzl@6402 233 }
tschatzl@6402 234
tschatzl@6402 235 void G1CodeRootSet::free(G1CodeRootChunk* chunk) {
tschatzl@6402 236 free_chunk(chunk);
tschatzl@6402 237 }
tschatzl@6402 238
tschatzl@6402 239 bool G1CodeRootSet::contains(nmethod* method) {
tschatzl@6402 240 return find(method) != NULL;
tschatzl@6402 241 }
tschatzl@6402 242
tschatzl@6402 243 void G1CodeRootSet::clear() {
tschatzl@6402 244 free_all_chunks(&_list);
tschatzl@6402 245 _length = 0;
tschatzl@6402 246 }
tschatzl@6402 247
tschatzl@6402 248 void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const {
tschatzl@6402 249 G1CodeRootChunk* cur = _list.head();
tschatzl@6402 250 while (cur != NULL) {
tschatzl@6402 251 cur->nmethods_do(blk);
tschatzl@6402 252 cur = (G1CodeRootChunk*)cur->next();
tschatzl@6402 253 }
tschatzl@6402 254 }
tschatzl@6402 255
tschatzl@6932 256 size_t G1CodeRootSet::static_mem_size() {
tschatzl@6932 257 return sizeof(G1CodeRootSet);
tschatzl@6932 258 }
tschatzl@6932 259
tschatzl@6402 260 size_t G1CodeRootSet::mem_size() {
tschatzl@6932 261 return G1CodeRootSet::static_mem_size() + _list.count() * _list.size();
tschatzl@6402 262 }
tschatzl@6402 263
tschatzl@6402 264 #ifndef PRODUCT
tschatzl@6402 265
tschatzl@6402 266 void G1CodeRootSet::test() {
tschatzl@6925 267 G1CodeRootChunkManager mgr;
tschatzl@6402 268
tschatzl@6925 269 assert(mgr.num_chunks_handed_out() == 0, "Must not have handed out chunks yet");
tschatzl@6402 270
tschatzl@6932 271 assert(G1CodeRootChunkManager::static_mem_size() > sizeof(void*),
tschatzl@6932 272 err_msg("The chunk manager's static memory usage seems too small, is only "SIZE_FORMAT" bytes.", G1CodeRootChunkManager::static_mem_size()));
tschatzl@6932 273
tschatzl@6402 274 // The number of chunks that we allocate for purge testing.
tschatzl@6402 275 size_t const num_chunks = 10;
tschatzl@6925 276
tschatzl@6402 277 {
tschatzl@6925 278 G1CodeRootSet set1(&mgr);
tschatzl@6402 279 assert(set1.is_empty(), "Code root set must be initially empty but is not.");
tschatzl@6402 280
tschatzl@6932 281 assert(G1CodeRootSet::static_mem_size() > sizeof(void*),
tschatzl@6932 282 err_msg("The code root set's static memory usage seems too small, is only "SIZE_FORMAT" bytes", G1CodeRootSet::static_mem_size()));
tschatzl@6932 283
tschatzl@6402 284 set1.add((nmethod*)1);
tschatzl@6925 285 assert(mgr.num_chunks_handed_out() == 1,
tschatzl@6402 286 err_msg("Must have allocated and handed out one chunk, but handed out "
tschatzl@6925 287 SIZE_FORMAT" chunks", mgr.num_chunks_handed_out()));
tschatzl@6402 288 assert(set1.length() == 1, err_msg("Added exactly one element, but set contains "
tschatzl@6402 289 SIZE_FORMAT" elements", set1.length()));
tschatzl@6402 290
tschatzl@6402 291 // G1CodeRootChunk::word_size() is larger than G1CodeRootChunk::num_entries which
tschatzl@6402 292 // we cannot access.
tschatzl@6402 293 for (uint i = 0; i < G1CodeRootChunk::word_size() + 1; i++) {
tschatzl@6402 294 set1.add((nmethod*)1);
tschatzl@6402 295 }
tschatzl@6925 296 assert(mgr.num_chunks_handed_out() == 1,
tschatzl@6402 297 err_msg("Duplicate detection must have prevented allocation of further "
tschatzl@6925 298 "chunks but allocated "SIZE_FORMAT, mgr.num_chunks_handed_out()));
tschatzl@6402 299 assert(set1.length() == 1,
tschatzl@6402 300 err_msg("Duplicate detection should not have increased the set size but "
tschatzl@6402 301 "is "SIZE_FORMAT, set1.length()));
tschatzl@6402 302
tschatzl@6402 303 size_t num_total_after_add = G1CodeRootChunk::word_size() + 1;
tschatzl@6402 304 for (size_t i = 0; i < num_total_after_add - 1; i++) {
tschatzl@6925 305 set1.add((nmethod*)(uintptr_t)(2 + i));
tschatzl@6402 306 }
tschatzl@6925 307 assert(mgr.num_chunks_handed_out() > 1,
tschatzl@6925 308 "After adding more code roots, more than one additional chunk should have been handed out");
tschatzl@6402 309 assert(set1.length() == num_total_after_add,
tschatzl@6402 310 err_msg("After adding in total "SIZE_FORMAT" distinct code roots, they "
tschatzl@6402 311 "need to be in the set, but there are only "SIZE_FORMAT,
tschatzl@6402 312 num_total_after_add, set1.length()));
tschatzl@6402 313
tschatzl@6402 314 size_t num_popped = 0;
tschatzl@6402 315 while (set1.pop() != NULL) {
tschatzl@6402 316 num_popped++;
tschatzl@6402 317 }
tschatzl@6402 318 assert(num_popped == num_total_after_add,
tschatzl@6402 319 err_msg("Managed to pop "SIZE_FORMAT" code roots, but only "SIZE_FORMAT" "
tschatzl@6402 320 "were added", num_popped, num_total_after_add));
tschatzl@6925 321 assert(mgr.num_chunks_handed_out() == 0,
tschatzl@6402 322 err_msg("After popping all elements, all chunks must have been returned "
tschatzl@6925 323 "but there are still "SIZE_FORMAT" additional", mgr.num_chunks_handed_out()));
tschatzl@6402 324
tschatzl@6925 325 mgr.purge_chunks(0);
tschatzl@6925 326 assert(mgr.num_free_chunks() == 0,
tschatzl@6402 327 err_msg("After purging everything, the free list must be empty but still "
tschatzl@6925 328 "contains "SIZE_FORMAT" chunks", mgr.num_free_chunks()));
tschatzl@6402 329
tschatzl@6402 330 // Add some more handed out chunks.
tschatzl@6402 331 size_t i = 0;
tschatzl@6925 332 while (mgr.num_chunks_handed_out() < num_chunks) {
tschatzl@6402 333 set1.add((nmethod*)i);
tschatzl@6402 334 i++;
tschatzl@6402 335 }
tschatzl@6402 336
tschatzl@6402 337 {
tschatzl@6402 338 // Generate chunks on the free list.
tschatzl@6925 339 G1CodeRootSet set2(&mgr);
tschatzl@6402 340 size_t i = 0;
tschatzl@6925 341 while (mgr.num_chunks_handed_out() < (num_chunks * 2)) {
tschatzl@6402 342 set2.add((nmethod*)i);
tschatzl@6402 343 i++;
tschatzl@6402 344 }
tschatzl@6402 345 // Exit of the scope of the set2 object will call the destructor that generates
tschatzl@6402 346 // num_chunks elements on the free list.
tschatzl@6402 347 }
tschatzl@6402 348
tschatzl@6925 349 assert(mgr.num_chunks_handed_out() == num_chunks,
tschatzl@6402 350 err_msg("Deletion of the second set must have resulted in giving back "
tschatzl@6925 351 "those, but there are still "SIZE_FORMAT" additional handed out, expecting "
tschatzl@6925 352 SIZE_FORMAT, mgr.num_chunks_handed_out(), num_chunks));
tschatzl@6925 353 assert(mgr.num_free_chunks() == num_chunks,
tschatzl@6402 354 err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list "
tschatzl@6925 355 "but there are only "SIZE_FORMAT, num_chunks, mgr.num_free_chunks()));
tschatzl@6402 356
tschatzl@6402 357 size_t const test_percentage = 50;
tschatzl@6925 358 mgr.purge_chunks(test_percentage);
tschatzl@6925 359 assert(mgr.num_chunks_handed_out() == num_chunks,
tschatzl@6402 360 err_msg("Purging must not hand out chunks but there are "SIZE_FORMAT,
tschatzl@6925 361 mgr.num_chunks_handed_out()));
tschatzl@6925 362 assert(mgr.num_free_chunks() == (size_t)(mgr.num_chunks_handed_out() * test_percentage / 100),
tschatzl@6402 363 err_msg("Must have purged "SIZE_FORMAT" percent of "SIZE_FORMAT" chunks"
tschatzl@6925 364 "but there are "SIZE_FORMAT, test_percentage, num_chunks,
tschatzl@6925 365 mgr.num_free_chunks()));
tschatzl@6402 366 // Purge the remainder of the chunks on the free list.
tschatzl@6925 367 mgr.purge_chunks(0);
tschatzl@6925 368 assert(mgr.num_free_chunks() == 0, "Free List must be empty");
tschatzl@6925 369 assert(mgr.num_chunks_handed_out() == num_chunks,
tschatzl@6402 370 err_msg("Expected to be "SIZE_FORMAT" chunks handed out from the first set "
tschatzl@6925 371 "but there are "SIZE_FORMAT, num_chunks, mgr.num_chunks_handed_out()));
tschatzl@6402 372
tschatzl@6402 373 // Exit of the scope of the set1 object will call the destructor that generates
tschatzl@6402 374 // num_chunks additional elements on the free list.
tschatzl@6925 375 }
tschatzl@6402 376
tschatzl@6925 377 assert(mgr.num_chunks_handed_out() == 0,
tschatzl@6402 378 err_msg("Deletion of the only set must have resulted in no chunks handed "
tschatzl@6925 379 "out, but there is still "SIZE_FORMAT" handed out", mgr.num_chunks_handed_out()));
tschatzl@6925 380 assert(mgr.num_free_chunks() == num_chunks,
tschatzl@6402 381 err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list "
tschatzl@6925 382 "but there are only "SIZE_FORMAT, num_chunks, mgr.num_free_chunks()));
tschatzl@6402 383
tschatzl@6402 384 // Restore initial state.
tschatzl@6925 385 mgr.purge_chunks(0);
tschatzl@6925 386 assert(mgr.num_free_chunks() == 0, "Free List must be empty");
tschatzl@6925 387 assert(mgr.num_chunks_handed_out() == 0, "No additional elements must have been handed out yet");
tschatzl@6402 388 }
tschatzl@6402 389
tschatzl@6402 390 void TestCodeCacheRemSet_test() {
tschatzl@6402 391 G1CodeRootSet::test();
tschatzl@6402 392 }
tschatzl@6402 393 #endif

mercurial