1 /* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "code/codeCache.hpp" 27 #include "code/nmethod.hpp" 28 #include "gc/g1/g1CodeCacheRemSet.hpp" 29 #include "gc/g1/heapRegion.hpp" 30 #include "memory/heap.hpp" 31 #include "memory/iterator.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "utilities/hashtable.inline.hpp" 34 #include "utilities/stack.inline.hpp" 35 36 class CodeRootSetTable : public Hashtable<nmethod*, mtGC> { 37 friend class G1CodeRootSetTest; 38 typedef HashtableEntry<nmethod*, mtGC> Entry; 39 40 static CodeRootSetTable* volatile _purge_list; 41 42 CodeRootSetTable* _purge_next; 43 44 unsigned int compute_hash(nmethod* nm) { 45 uintptr_t hash = (uintptr_t)nm; 46 return hash ^ (hash >> 7); // code heap blocks are 128byte aligned 47 } 48 49 void remove_entry(Entry* e, Entry* previous); 50 Entry* new_entry(nmethod* nm); 51 52 public: 53 CodeRootSetTable(int size) : Hashtable<nmethod*, mtGC>(size, sizeof(Entry)), _purge_next(NULL) {} 54 ~CodeRootSetTable(); 55 56 // Needs to be protected locks 57 bool add(nmethod* nm); 58 bool remove(nmethod* nm); 59 60 // Can be called without locking 61 bool contains(nmethod* nm); 62 63 int entry_size() const { return BasicHashtable<mtGC>::entry_size(); } 64 65 void copy_to(CodeRootSetTable* new_table); 66 void nmethods_do(CodeBlobClosure* blk); 67 68 template<typename CB> 69 int remove_if(CB& should_remove); 70 71 static void purge_list_append(CodeRootSetTable* tbl); 72 static void purge(); 73 74 static size_t static_mem_size() { 75 return sizeof(_purge_list); 76 } 77 78 size_t mem_size(); 79 }; 80 81 CodeRootSetTable* volatile CodeRootSetTable::_purge_list = NULL; 82 83 size_t CodeRootSetTable::mem_size() { 84 return sizeof(CodeRootSetTable) + (entry_size() * number_of_entries()) + (sizeof(HashtableBucket<mtGC>) * table_size()); 85 } 86 87 CodeRootSetTable::Entry* CodeRootSetTable::new_entry(nmethod* nm) { 88 unsigned int hash = compute_hash(nm); 89 Entry* entry = (Entry*) new_entry_free_list(); 90 if (entry == NULL) { 91 entry = (Entry*) NEW_C_HEAP_ARRAY2(char, entry_size(), mtGC, CURRENT_PC); 92 } 93 entry->set_next(NULL); 94 entry->set_hash(hash); 95 entry->set_literal(nm); 96 return entry; 97 } 98 99 void CodeRootSetTable::remove_entry(Entry* e, Entry* previous) { 100 int index = hash_to_index(e->hash()); 101 assert((e == bucket(index)) == (previous == NULL), "if e is the first entry then previous should be null"); 102 103 if (previous == NULL) { 104 set_entry(index, e->next()); 105 } else { 106 previous->set_next(e->next()); 107 } 108 free_entry(e); 109 } 110 111 CodeRootSetTable::~CodeRootSetTable() { 112 for (int index = 0; index < table_size(); ++index) { 113 for (Entry* e = bucket(index); e != NULL; ) { 114 Entry* to_remove = e; 115 // read next before freeing. 116 e = e->next(); 117 unlink_entry(to_remove); 118 FREE_C_HEAP_ARRAY(char, to_remove); 119 } 120 } 121 assert(number_of_entries() == 0, "should have removed all entries"); 122 free_buckets(); 123 for (BasicHashtableEntry<mtGC>* e = new_entry_free_list(); e != NULL; e = new_entry_free_list()) { 124 FREE_C_HEAP_ARRAY(char, e); 125 } 126 } 127 128 bool CodeRootSetTable::add(nmethod* nm) { 129 if (!contains(nm)) { 130 Entry* e = new_entry(nm); 131 int index = hash_to_index(e->hash()); 132 add_entry(index, e); 133 return true; 134 } 135 return false; 136 } 137 138 bool CodeRootSetTable::contains(nmethod* nm) { 139 int index = hash_to_index(compute_hash(nm)); 140 for (Entry* e = bucket(index); e != NULL; e = e->next()) { 141 if (e->literal() == nm) { 142 return true; 143 } 144 } 145 return false; 146 } 147 148 bool CodeRootSetTable::remove(nmethod* nm) { 149 int index = hash_to_index(compute_hash(nm)); 150 Entry* previous = NULL; 151 for (Entry* e = bucket(index); e != NULL; previous = e, e = e->next()) { 152 if (e->literal() == nm) { 153 remove_entry(e, previous); 154 return true; 155 } 156 } 157 return false; 158 } 159 160 void CodeRootSetTable::copy_to(CodeRootSetTable* new_table) { 161 for (int index = 0; index < table_size(); ++index) { 162 for (Entry* e = bucket(index); e != NULL; e = e->next()) { 163 new_table->add(e->literal()); 164 } 165 } 166 new_table->copy_freelist(this); 167 } 168 169 void CodeRootSetTable::nmethods_do(CodeBlobClosure* blk) { 170 for (int index = 0; index < table_size(); ++index) { 171 for (Entry* e = bucket(index); e != NULL; e = e->next()) { 172 blk->do_code_blob(e->literal()); 173 } 174 } 175 } 176 177 template<typename CB> 178 int CodeRootSetTable::remove_if(CB& should_remove) { 179 int num_removed = 0; 180 for (int index = 0; index < table_size(); ++index) { 181 Entry* previous = NULL; 182 Entry* e = bucket(index); 183 while (e != NULL) { 184 Entry* next = e->next(); 185 if (should_remove(e->literal())) { 186 remove_entry(e, previous); 187 ++num_removed; 188 } else { 189 previous = e; 190 } 191 e = next; 192 } 193 } 194 return num_removed; 195 } 196 197 G1CodeRootSet::~G1CodeRootSet() { 198 delete _table; 199 } 200 201 CodeRootSetTable* G1CodeRootSet::load_acquire_table() { 202 return (CodeRootSetTable*) OrderAccess::load_ptr_acquire(&_table); 203 } 204 205 void G1CodeRootSet::allocate_small_table() { 206 CodeRootSetTable* temp = new CodeRootSetTable(SmallSize); 207 208 OrderAccess::release_store_ptr(&_table, temp); 209 } 210 211 void CodeRootSetTable::purge_list_append(CodeRootSetTable* table) { 212 for (;;) { 213 table->_purge_next = _purge_list; 214 CodeRootSetTable* old = (CodeRootSetTable*) Atomic::cmpxchg_ptr(table, &_purge_list, table->_purge_next); 215 if (old == table->_purge_next) { 216 break; 217 } 218 } 219 } 220 221 void CodeRootSetTable::purge() { 222 CodeRootSetTable* table = _purge_list; 223 _purge_list = NULL; 224 while (table != NULL) { 225 CodeRootSetTable* to_purge = table; 226 table = table->_purge_next; 227 delete to_purge; 228 } 229 } 230 231 void G1CodeRootSet::move_to_large() { 232 CodeRootSetTable* temp = new CodeRootSetTable(LargeSize); 233 234 _table->copy_to(temp); 235 236 CodeRootSetTable::purge_list_append(_table); 237 238 OrderAccess::release_store_ptr(&_table, temp); 239 } 240 241 void G1CodeRootSet::purge() { 242 CodeRootSetTable::purge(); 243 } 244 245 size_t G1CodeRootSet::static_mem_size() { 246 return CodeRootSetTable::static_mem_size(); 247 } 248 249 void G1CodeRootSet::add(nmethod* method) { 250 bool added = false; 251 if (is_empty()) { 252 allocate_small_table(); 253 } 254 added = _table->add(method); 255 if (added) { 256 if (_length == Threshold) { 257 move_to_large(); 258 } 259 ++_length; 260 } 261 assert(_length == (size_t)_table->number_of_entries(), "sizes should match"); 262 } 263 264 bool G1CodeRootSet::remove(nmethod* method) { 265 bool removed = false; 266 if (_table != NULL) { 267 removed = _table->remove(method); 268 } 269 if (removed) { 270 _length--; 271 if (_length == 0) { 272 clear(); 273 } 274 } 275 assert((_length == 0 && _table == NULL) || 276 (_length == (size_t)_table->number_of_entries()), "sizes should match"); 277 return removed; 278 } 279 280 bool G1CodeRootSet::contains(nmethod* method) { 281 CodeRootSetTable* table = load_acquire_table(); // contains() may be called outside of lock, so ensure mem sync. 282 if (table != NULL) { 283 return table->contains(method); 284 } 285 return false; 286 } 287 288 void G1CodeRootSet::clear() { 289 delete _table; 290 _table = NULL; 291 _length = 0; 292 } 293 294 size_t G1CodeRootSet::mem_size() { 295 return sizeof(*this) + (_table != NULL ? _table->mem_size() : 0); 296 } 297 298 void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const { 299 if (_table != NULL) { 300 _table->nmethods_do(blk); 301 } 302 } 303 304 class CleanCallback : public StackObj { 305 class PointsIntoHRDetectionClosure : public OopClosure { 306 HeapRegion* _hr; 307 public: 308 bool _points_into; 309 PointsIntoHRDetectionClosure(HeapRegion* hr) : _hr(hr), _points_into(false) {} 310 311 void do_oop(narrowOop* o) { 312 do_oop_work(o); 313 } 314 315 void do_oop(oop* o) { 316 do_oop_work(o); 317 } 318 319 template <typename T> 320 void do_oop_work(T* p) { 321 if (_hr->is_in(oopDesc::load_decode_heap_oop(p))) { 322 _points_into = true; 323 } 324 } 325 }; 326 327 PointsIntoHRDetectionClosure _detector; 328 CodeBlobToOopClosure _blobs; 329 330 public: 331 CleanCallback(HeapRegion* hr) : _detector(hr), _blobs(&_detector, !CodeBlobToOopClosure::FixRelocations) {} 332 333 bool operator() (nmethod* nm) { 334 _detector._points_into = false; 335 _blobs.do_code_blob(nm); 336 return !_detector._points_into; 337 } 338 }; 339 340 void G1CodeRootSet::clean(HeapRegion* owner) { 341 CleanCallback should_clean(owner); 342 if (_table != NULL) { 343 int removed = _table->remove_if(should_clean); 344 assert((size_t)removed <= _length, "impossible"); 345 _length -= removed; 346 } 347 if (_length == 0) { 348 clear(); 349 } 350 } 351 352 #ifndef PRODUCT 353 354 class G1CodeRootSetTest { 355 public: 356 static void test() { 357 { 358 G1CodeRootSet set1; 359 assert(set1.is_empty(), "Code root set must be initially empty but is not."); 360 361 assert(G1CodeRootSet::static_mem_size() == sizeof(void*), 362 "The code root set's static memory usage is incorrect, " SIZE_FORMAT " bytes", G1CodeRootSet::static_mem_size()); 363 364 set1.add((nmethod*)1); 365 assert(set1.length() == 1, "Added exactly one element, but set contains " 366 SIZE_FORMAT " elements", set1.length()); 367 368 const size_t num_to_add = (size_t)G1CodeRootSet::Threshold + 1; 369 370 for (size_t i = 1; i <= num_to_add; i++) { 371 set1.add((nmethod*)1); 372 } 373 assert(set1.length() == 1, 374 "Duplicate detection should not have increased the set size but " 375 "is " SIZE_FORMAT, set1.length()); 376 377 for (size_t i = 2; i <= num_to_add; i++) { 378 set1.add((nmethod*)(uintptr_t)(i)); 379 } 380 assert(set1.length() == num_to_add, 381 "After adding in total " SIZE_FORMAT " distinct code roots, they " 382 "need to be in the set, but there are only " SIZE_FORMAT, 383 num_to_add, set1.length()); 384 385 assert(CodeRootSetTable::_purge_list != NULL, "should have grown to large hashtable"); 386 387 size_t num_popped = 0; 388 for (size_t i = 1; i <= num_to_add; i++) { 389 bool removed = set1.remove((nmethod*)i); 390 if (removed) { 391 num_popped += 1; 392 } else { 393 break; 394 } 395 } 396 assert(num_popped == num_to_add, 397 "Managed to pop " SIZE_FORMAT " code roots, but only " SIZE_FORMAT " " 398 "were added", num_popped, num_to_add); 399 assert(CodeRootSetTable::_purge_list != NULL, "should have grown to large hashtable"); 400 401 G1CodeRootSet::purge(); 402 403 assert(CodeRootSetTable::_purge_list == NULL, "should have purged old small tables"); 404 405 } 406 407 } 408 }; 409 410 void TestCodeCacheRemSet_test() { 411 G1CodeRootSetTest::test(); 412 } 413 414 #endif