< prev index next >

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

Print this page
rev 11782 : 8164230: Convert TestCodeCacheRemSet_test to GTest
Reviewed-by: duke


   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());


 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


   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.internal.hpp"
  29 #include "gc/g1/g1CodeCacheRemSet.hpp"
  30 #include "gc/g1/heapRegion.hpp"
  31 #include "memory/heap.hpp"
  32 #include "memory/iterator.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "utilities/hashtable.inline.hpp"
  35 #include "utilities/stack.inline.hpp"
  36 













































  37 CodeRootSetTable* volatile CodeRootSetTable::_purge_list = NULL;
  38 
  39 size_t CodeRootSetTable::mem_size() {
  40   return sizeof(CodeRootSetTable) + (entry_size() * number_of_entries()) + (sizeof(HashtableBucket<mtGC>) * table_size());
  41 }
  42 
  43 CodeRootSetTable::Entry* CodeRootSetTable::new_entry(nmethod* nm) {
  44   unsigned int hash = compute_hash(nm);
  45   Entry* entry = (Entry*) new_entry_free_list();
  46   if (entry == NULL) {
  47     entry = (Entry*) NEW_C_HEAP_ARRAY2(char, entry_size(), mtGC, CURRENT_PC);
  48   }
  49   entry->set_next(NULL);
  50   entry->set_hash(hash);
  51   entry->set_literal(nm);
  52   return entry;
  53 }
  54 
  55 void CodeRootSetTable::remove_entry(Entry* e, Entry* previous) {
  56   int index = hash_to_index(e->hash());


 287   CleanCallback(HeapRegion* hr) : _detector(hr), _blobs(&_detector, !CodeBlobToOopClosure::FixRelocations) {}
 288 
 289   bool operator() (nmethod* nm) {
 290     _detector._points_into = false;
 291     _blobs.do_code_blob(nm);
 292     return !_detector._points_into;
 293   }
 294 };
 295 
 296 void G1CodeRootSet::clean(HeapRegion* owner) {
 297   CleanCallback should_clean(owner);
 298   if (_table != NULL) {
 299     int removed = _table->remove_if(should_clean);
 300     assert((size_t)removed <= _length, "impossible");
 301     _length -= removed;
 302   }
 303   if (_length == 0) {
 304     clear();
 305   }
 306 }
































































< prev index next >