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

Fri, 29 Aug 2014 13:12:21 +0200

author
mgerdin
date
Fri, 29 Aug 2014 13:12:21 +0200
changeset 7208
7baf47cb97cb
parent 6993
870c03421152
child 7209
58925d1f325e
permissions
-rw-r--r--

8048268: G1 Code Root Migration performs poorly
Summary: Replace G1CodeRootSet with a Hashtable based implementation, merge Code Root Migration phase into Code Root Scanning
Reviewed-by: jmasa, brutisso, tschatzl

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 #include "precompiled.hpp"
mgerdin@7208 26 #include "code/codeCache.hpp"
tschatzl@6402 27 #include "code/nmethod.hpp"
tschatzl@6402 28 #include "gc_implementation/g1/g1CodeCacheRemSet.hpp"
mgerdin@7208 29 #include "gc_implementation/g1/heapRegion.hpp"
mgerdin@7208 30 #include "memory/heap.hpp"
tschatzl@6402 31 #include "memory/iterator.hpp"
mgerdin@7208 32 #include "oops/oop.inline.hpp"
mgerdin@7208 33 #include "utilities/hashtable.inline.hpp"
mgerdin@7208 34 #include "utilities/stack.inline.hpp"
tschatzl@6402 35
drchase@6680 36 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 37
mgerdin@7208 38 class CodeRootSetTable : public Hashtable<nmethod*, mtGC> {
mgerdin@7208 39 friend class G1CodeRootSetTest;
mgerdin@7208 40 typedef HashtableEntry<nmethod*, mtGC> Entry;
mgerdin@7208 41
mgerdin@7208 42 static CodeRootSetTable* volatile _purge_list;
mgerdin@7208 43
mgerdin@7208 44 CodeRootSetTable* _purge_next;
mgerdin@7208 45
mgerdin@7208 46 unsigned int compute_hash(nmethod* nm) {
mgerdin@7208 47 uintptr_t hash = (uintptr_t)nm;
mgerdin@7208 48 return hash ^ (hash >> 7); // code heap blocks are 128byte aligned
mgerdin@7208 49 }
mgerdin@7208 50
mgerdin@7208 51 Entry* new_entry(nmethod* nm);
mgerdin@7208 52
mgerdin@7208 53 public:
mgerdin@7208 54 CodeRootSetTable(int size) : Hashtable<nmethod*, mtGC>(size, sizeof(Entry)), _purge_next(NULL) {}
mgerdin@7208 55 ~CodeRootSetTable();
mgerdin@7208 56
mgerdin@7208 57 // Needs to be protected locks
mgerdin@7208 58 bool add(nmethod* nm);
mgerdin@7208 59 bool remove(nmethod* nm);
mgerdin@7208 60
mgerdin@7208 61 // Can be called without locking
mgerdin@7208 62 bool contains(nmethod* nm);
mgerdin@7208 63
mgerdin@7208 64 int entry_size() const { return BasicHashtable<mtGC>::entry_size(); }
mgerdin@7208 65
mgerdin@7208 66 void copy_to(CodeRootSetTable* new_table);
mgerdin@7208 67 void nmethods_do(CodeBlobClosure* blk);
mgerdin@7208 68
mgerdin@7208 69 template<typename CB>
mgerdin@7208 70 void remove_if(CB& should_remove);
mgerdin@7208 71
mgerdin@7208 72 static void purge_list_append(CodeRootSetTable* tbl);
mgerdin@7208 73 static void purge();
mgerdin@7208 74
mgerdin@7208 75 static size_t static_mem_size() {
mgerdin@7208 76 return sizeof(_purge_list);
mgerdin@7208 77 }
mgerdin@7208 78 };
mgerdin@7208 79
mgerdin@7208 80 CodeRootSetTable* volatile CodeRootSetTable::_purge_list = NULL;
mgerdin@7208 81
mgerdin@7208 82 CodeRootSetTable::Entry* CodeRootSetTable::new_entry(nmethod* nm) {
mgerdin@7208 83 unsigned int hash = compute_hash(nm);
mgerdin@7208 84 Entry* entry = (Entry*) new_entry_free_list();
mgerdin@7208 85 if (entry == NULL) {
mgerdin@7208 86 entry = (Entry*) NEW_C_HEAP_ARRAY2(char, entry_size(), mtGC, CURRENT_PC);
mgerdin@7208 87 }
mgerdin@7208 88 entry->set_next(NULL);
mgerdin@7208 89 entry->set_hash(hash);
mgerdin@7208 90 entry->set_literal(nm);
mgerdin@7208 91 return entry;
tschatzl@6402 92 }
tschatzl@6402 93
mgerdin@7208 94 CodeRootSetTable::~CodeRootSetTable() {
mgerdin@7208 95 for (int index = 0; index < table_size(); ++index) {
mgerdin@7208 96 for (Entry* e = bucket(index); e != NULL; ) {
mgerdin@7208 97 Entry* to_remove = e;
mgerdin@7208 98 // read next before freeing.
mgerdin@7208 99 e = e->next();
mgerdin@7208 100 unlink_entry(to_remove);
mgerdin@7208 101 FREE_C_HEAP_ARRAY(char, to_remove, mtGC);
stefank@6992 102 }
mgerdin@7208 103 }
mgerdin@7208 104 assert(number_of_entries() == 0, "should have removed all entries");
mgerdin@7208 105 free_buckets();
mgerdin@7208 106 for (BasicHashtableEntry<mtGC>* e = new_entry_free_list(); e != NULL; e = new_entry_free_list()) {
mgerdin@7208 107 FREE_C_HEAP_ARRAY(char, e, mtGC);
tschatzl@6402 108 }
tschatzl@6402 109 }
tschatzl@6402 110
mgerdin@7208 111 bool CodeRootSetTable::add(nmethod* nm) {
mgerdin@7208 112 if (!contains(nm)) {
mgerdin@7208 113 Entry* e = new_entry(nm);
mgerdin@7208 114 int index = hash_to_index(e->hash());
mgerdin@7208 115 add_entry(index, e);
mgerdin@7208 116 return true;
mgerdin@7208 117 }
mgerdin@7208 118 return false;
mgerdin@7208 119 }
stefank@6992 120
mgerdin@7208 121 bool CodeRootSetTable::contains(nmethod* nm) {
mgerdin@7208 122 int index = hash_to_index(compute_hash(nm));
mgerdin@7208 123 for (Entry* e = bucket(index); e != NULL; e = e->next()) {
mgerdin@7208 124 if (e->literal() == nm) {
stefank@6992 125 return true;
stefank@6992 126 }
stefank@6992 127 }
stefank@6992 128 return false;
stefank@6992 129 }
stefank@6992 130
mgerdin@7208 131 bool CodeRootSetTable::remove(nmethod* nm) {
mgerdin@7208 132 int index = hash_to_index(compute_hash(nm));
mgerdin@7208 133 Entry* previous = NULL;
mgerdin@7208 134 for (Entry* e = bucket(index); e != NULL; previous = e, e = e->next()) {
mgerdin@7208 135 if (e->literal() == nm) {
mgerdin@7208 136 if (previous != NULL) {
mgerdin@7208 137 previous->set_next(e->next());
mgerdin@7208 138 } else {
mgerdin@7208 139 set_entry(index, e->next());
mgerdin@7208 140 }
mgerdin@7208 141 free_entry(e);
mgerdin@7208 142 return true;
mgerdin@7208 143 }
mgerdin@7208 144 }
mgerdin@7208 145 return false;
tschatzl@6402 146 }
tschatzl@6402 147
mgerdin@7208 148 void CodeRootSetTable::copy_to(CodeRootSetTable* new_table) {
mgerdin@7208 149 for (int index = 0; index < table_size(); ++index) {
mgerdin@7208 150 for (Entry* e = bucket(index); e != NULL; e = e->next()) {
mgerdin@7208 151 new_table->add(e->literal());
mgerdin@7208 152 }
mgerdin@7208 153 }
mgerdin@7208 154 new_table->copy_freelist(this);
tschatzl@6402 155 }
tschatzl@6402 156
mgerdin@7208 157 void CodeRootSetTable::nmethods_do(CodeBlobClosure* blk) {
mgerdin@7208 158 for (int index = 0; index < table_size(); ++index) {
mgerdin@7208 159 for (Entry* e = bucket(index); e != NULL; e = e->next()) {
mgerdin@7208 160 blk->do_code_blob(e->literal());
mgerdin@7208 161 }
tschatzl@6402 162 }
tschatzl@6402 163 }
tschatzl@6402 164
mgerdin@7208 165 template<typename CB>
mgerdin@7208 166 void CodeRootSetTable::remove_if(CB& should_remove) {
mgerdin@7208 167 for (int index = 0; index < table_size(); ++index) {
mgerdin@7208 168 Entry* previous = NULL;
mgerdin@7208 169 Entry* e = bucket(index);
mgerdin@7208 170 while (e != NULL) {
mgerdin@7208 171 Entry* next = e->next();
mgerdin@7208 172 if (should_remove(e->literal())) {
mgerdin@7208 173 if (previous != NULL) {
mgerdin@7208 174 previous->set_next(next);
mgerdin@7208 175 } else {
mgerdin@7208 176 set_entry(index, next);
mgerdin@7208 177 }
mgerdin@7208 178 free_entry(e);
mgerdin@7208 179 } else {
mgerdin@7208 180 previous = e;
mgerdin@7208 181 }
mgerdin@7208 182 e = next;
mgerdin@7208 183 }
mgerdin@7208 184 }
tschatzl@6402 185 }
tschatzl@6402 186
mgerdin@7208 187 G1CodeRootSet::~G1CodeRootSet() {
mgerdin@7208 188 delete _table;
mgerdin@7208 189 }
tschatzl@6925 190
mgerdin@7208 191 CodeRootSetTable* G1CodeRootSet::load_acquire_table() {
mgerdin@7208 192 return (CodeRootSetTable*) OrderAccess::load_ptr_acquire(&_table);
mgerdin@7208 193 }
mgerdin@7208 194
mgerdin@7208 195 void G1CodeRootSet::allocate_small_table() {
mgerdin@7208 196 _table = new CodeRootSetTable(SmallSize);
mgerdin@7208 197 }
mgerdin@7208 198
mgerdin@7208 199 void CodeRootSetTable::purge_list_append(CodeRootSetTable* table) {
mgerdin@7208 200 for (;;) {
mgerdin@7208 201 table->_purge_next = _purge_list;
mgerdin@7208 202 CodeRootSetTable* old = (CodeRootSetTable*) Atomic::cmpxchg_ptr(table, &_purge_list, table->_purge_next);
mgerdin@7208 203 if (old == table->_purge_next) {
mgerdin@7208 204 break;
mgerdin@7208 205 }
tschatzl@6925 206 }
mgerdin@7208 207 }
mgerdin@7208 208
mgerdin@7208 209 void CodeRootSetTable::purge() {
mgerdin@7208 210 CodeRootSetTable* table = _purge_list;
mgerdin@7208 211 _purge_list = NULL;
mgerdin@7208 212 while (table != NULL) {
mgerdin@7208 213 CodeRootSetTable* to_purge = table;
mgerdin@7208 214 table = table->_purge_next;
mgerdin@7208 215 delete to_purge;
mgerdin@7208 216 }
mgerdin@7208 217 }
mgerdin@7208 218
mgerdin@7208 219 void G1CodeRootSet::move_to_large() {
mgerdin@7208 220 CodeRootSetTable* temp = new CodeRootSetTable(LargeSize);
mgerdin@7208 221
mgerdin@7208 222 _table->copy_to(temp);
mgerdin@7208 223
mgerdin@7208 224 CodeRootSetTable::purge_list_append(_table);
mgerdin@7208 225
mgerdin@7208 226 OrderAccess::release_store_ptr(&_table, temp);
mgerdin@7208 227 }
mgerdin@7208 228
mgerdin@7208 229
mgerdin@7208 230 void G1CodeRootSet::purge() {
mgerdin@7208 231 CodeRootSetTable::purge();
mgerdin@7208 232 }
mgerdin@7208 233
mgerdin@7208 234 size_t G1CodeRootSet::static_mem_size() {
mgerdin@7208 235 return CodeRootSetTable::static_mem_size();
mgerdin@7208 236 }
mgerdin@7208 237
mgerdin@7208 238 void G1CodeRootSet::add(nmethod* method) {
mgerdin@7208 239 bool added = false;
mgerdin@7208 240 if (is_empty()) {
mgerdin@7208 241 allocate_small_table();
mgerdin@7208 242 }
mgerdin@7208 243 added = _table->add(method);
mgerdin@7208 244 if (_length == Threshold) {
mgerdin@7208 245 move_to_large();
mgerdin@7208 246 }
mgerdin@7208 247 if (added) {
mgerdin@7208 248 ++_length;
mgerdin@7208 249 }
mgerdin@7208 250 }
mgerdin@7208 251
mgerdin@7208 252 bool G1CodeRootSet::remove(nmethod* method) {
mgerdin@7208 253 bool removed = false;
mgerdin@7208 254 if (_table != NULL) {
mgerdin@7208 255 removed = _table->remove(method);
mgerdin@7208 256 }
mgerdin@7208 257 if (removed) {
mgerdin@7208 258 _length--;
mgerdin@7208 259 if (_length == 0) {
mgerdin@7208 260 clear();
mgerdin@7208 261 }
mgerdin@7208 262 }
mgerdin@7208 263 return removed;
mgerdin@7208 264 }
mgerdin@7208 265
mgerdin@7208 266 bool G1CodeRootSet::contains(nmethod* method) {
mgerdin@7208 267 CodeRootSetTable* table = load_acquire_table();
mgerdin@7208 268 if (table != NULL) {
mgerdin@7208 269 return table->contains(method);
mgerdin@7208 270 }
mgerdin@7208 271 return false;
mgerdin@7208 272 }
mgerdin@7208 273
mgerdin@7208 274 void G1CodeRootSet::clear() {
mgerdin@7208 275 delete _table;
mgerdin@7208 276 _table = NULL;
mgerdin@7208 277 _length = 0;
mgerdin@7208 278 }
mgerdin@7208 279
mgerdin@7208 280 size_t G1CodeRootSet::mem_size() {
mgerdin@7208 281 return sizeof(*this) +
mgerdin@7208 282 (_table != NULL ? sizeof(CodeRootSetTable) + _table->entry_size() * _length : 0);
mgerdin@7208 283 }
mgerdin@7208 284
mgerdin@7208 285 void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const {
mgerdin@7208 286 if (_table != NULL) {
mgerdin@7208 287 _table->nmethods_do(blk);
mgerdin@7208 288 }
mgerdin@7208 289 }
mgerdin@7208 290
mgerdin@7208 291 class CleanCallback : public StackObj {
mgerdin@7208 292 class PointsIntoHRDetectionClosure : public OopClosure {
mgerdin@7208 293 HeapRegion* _hr;
mgerdin@7208 294 public:
mgerdin@7208 295 bool _points_into;
mgerdin@7208 296 PointsIntoHRDetectionClosure(HeapRegion* hr) : _hr(hr), _points_into(false) {}
mgerdin@7208 297
mgerdin@7208 298 void do_oop(narrowOop* o) {
mgerdin@7208 299 do_oop_work(o);
mgerdin@7208 300 }
mgerdin@7208 301
mgerdin@7208 302 void do_oop(oop* o) {
mgerdin@7208 303 do_oop_work(o);
mgerdin@7208 304 }
mgerdin@7208 305
mgerdin@7208 306 template <typename T>
mgerdin@7208 307 void do_oop_work(T* p) {
mgerdin@7208 308 if (_hr->is_in(oopDesc::load_decode_heap_oop(p))) {
mgerdin@7208 309 _points_into = true;
mgerdin@7208 310 }
mgerdin@7208 311 }
mgerdin@7208 312 };
mgerdin@7208 313
mgerdin@7208 314 PointsIntoHRDetectionClosure _detector;
mgerdin@7208 315 CodeBlobToOopClosure _blobs;
mgerdin@7208 316
mgerdin@7208 317 public:
mgerdin@7208 318 CleanCallback(HeapRegion* hr) : _detector(hr), _blobs(&_detector, !CodeBlobToOopClosure::FixRelocations) {}
mgerdin@7208 319
mgerdin@7208 320 bool operator() (nmethod* nm) {
mgerdin@7208 321 _detector._points_into = false;
mgerdin@7208 322 _blobs.do_code_blob(nm);
mgerdin@7208 323 return _detector._points_into;
mgerdin@7208 324 }
mgerdin@7208 325 };
mgerdin@7208 326
mgerdin@7208 327 void G1CodeRootSet::clean(HeapRegion* owner) {
mgerdin@7208 328 CleanCallback should_clean(owner);
mgerdin@7208 329 if (_table != NULL) {
mgerdin@7208 330 _table->remove_if(should_clean);
mgerdin@7208 331 }
tschatzl@6402 332 }
tschatzl@6402 333
tschatzl@6925 334 #ifndef PRODUCT
tschatzl@6925 335
mgerdin@7208 336 class G1CodeRootSetTest {
mgerdin@7208 337 public:
mgerdin@7208 338 static void test() {
mgerdin@7208 339 {
mgerdin@7208 340 G1CodeRootSet set1;
mgerdin@7208 341 assert(set1.is_empty(), "Code root set must be initially empty but is not.");
tschatzl@6402 342
mgerdin@7208 343 assert(G1CodeRootSet::static_mem_size() == sizeof(void*),
mgerdin@7208 344 err_msg("The code root set's static memory usage is incorrect, "SIZE_FORMAT" bytes", G1CodeRootSet::static_mem_size()));
mgerdin@7208 345
mgerdin@7208 346 set1.add((nmethod*)1);
mgerdin@7208 347 assert(set1.length() == 1, err_msg("Added exactly one element, but set contains "
mgerdin@7208 348 SIZE_FORMAT" elements", set1.length()));
mgerdin@7208 349
mgerdin@7208 350 const size_t num_to_add = (size_t)G1CodeRootSet::Threshold + 1;
mgerdin@7208 351
mgerdin@7208 352 for (size_t i = 1; i <= num_to_add; i++) {
mgerdin@7208 353 set1.add((nmethod*)1);
mgerdin@7208 354 }
mgerdin@7208 355 assert(set1.length() == 1,
mgerdin@7208 356 err_msg("Duplicate detection should not have increased the set size but "
mgerdin@7208 357 "is "SIZE_FORMAT, set1.length()));
mgerdin@7208 358
mgerdin@7208 359 for (size_t i = 2; i <= num_to_add; i++) {
mgerdin@7208 360 set1.add((nmethod*)(uintptr_t)(i));
mgerdin@7208 361 }
mgerdin@7208 362 assert(set1.length() == num_to_add,
mgerdin@7208 363 err_msg("After adding in total "SIZE_FORMAT" distinct code roots, they "
mgerdin@7208 364 "need to be in the set, but there are only "SIZE_FORMAT,
mgerdin@7208 365 num_to_add, set1.length()));
mgerdin@7208 366
mgerdin@7208 367 assert(CodeRootSetTable::_purge_list != NULL, "should have grown to large hashtable");
mgerdin@7208 368
mgerdin@7208 369 size_t num_popped = 0;
mgerdin@7208 370 for (size_t i = 1; i <= num_to_add; i++) {
mgerdin@7208 371 bool removed = set1.remove((nmethod*)i);
mgerdin@7208 372 if (removed) {
mgerdin@7208 373 num_popped += 1;
mgerdin@7208 374 } else {
mgerdin@7208 375 break;
mgerdin@7208 376 }
mgerdin@7208 377 }
mgerdin@7208 378 assert(num_popped == num_to_add,
mgerdin@7208 379 err_msg("Managed to pop "SIZE_FORMAT" code roots, but only "SIZE_FORMAT" "
mgerdin@7208 380 "were added", num_popped, num_to_add));
mgerdin@7208 381 assert(CodeRootSetTable::_purge_list != NULL, "should have grown to large hashtable");
mgerdin@7208 382
mgerdin@7208 383 G1CodeRootSet::purge();
mgerdin@7208 384
mgerdin@7208 385 assert(CodeRootSetTable::_purge_list == NULL, "should have purged old small tables");
mgerdin@7208 386
mgerdin@7208 387 }
mgerdin@7208 388
mgerdin@7208 389 }
mgerdin@7208 390 };
mgerdin@7208 391
mgerdin@7208 392 void TestCodeCacheRemSet_test() {
mgerdin@7208 393 G1CodeRootSetTest::test();
tschatzl@6925 394 }
tschatzl@6925 395
tschatzl@6925 396 #endif

mercurial