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/g1CodeRootSetTable.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 G1CodeRootSetTable* volatile G1CodeRootSetTable::_purge_list = NULL;
  38 
  39 size_t G1CodeRootSetTable::mem_size() {
  40   return sizeof(G1CodeRootSetTable) + (entry_size() * number_of_entries()) + (sizeof(HashtableBucket<mtGC>) * table_size());
  41 }
  42 
  43 G1CodeRootSetTable::Entry* G1CodeRootSetTable::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 G1CodeRootSetTable::remove_entry(Entry* e, Entry* previous) {
  56   int index = hash_to_index(e->hash());
  57   assert((e == bucket(index)) == (previous == NULL), "if e is the first entry then previous should be null");
  58 
  59   if (previous == NULL) {
  60     set_entry(index, e->next());
  61   } else {
  62     previous->set_next(e->next());
  63   }
  64   free_entry(e);
  65 }
  66 
  67 G1CodeRootSetTable::~G1CodeRootSetTable() {
  68   for (int index = 0; index < table_size(); ++index) {
  69     for (Entry* e = bucket(index); e != NULL; ) {
  70       Entry* to_remove = e;
  71       // read next before freeing.
  72       e = e->next();
  73       unlink_entry(to_remove);
  74       FREE_C_HEAP_ARRAY(char, to_remove);
  75     }
  76   }
  77   assert(number_of_entries() == 0, "should have removed all entries");
  78   free_buckets();
  79   for (BasicHashtableEntry<mtGC>* e = new_entry_free_list(); e != NULL; e = new_entry_free_list()) {
  80     FREE_C_HEAP_ARRAY(char, e);
  81   }
  82 }
  83 
  84 bool G1CodeRootSetTable::add(nmethod* nm) {
  85   if (!contains(nm)) {
  86     Entry* e = new_entry(nm);
  87     int index = hash_to_index(e->hash());
  88     add_entry(index, e);
  89     return true;
  90   }
  91   return false;
  92 }
  93 
  94 bool G1CodeRootSetTable::contains(nmethod* nm) {
  95   int index = hash_to_index(compute_hash(nm));
  96   for (Entry* e = bucket(index); e != NULL; e = e->next()) {
  97     if (e->literal() == nm) {
  98       return true;
  99     }
 100   }
 101   return false;
 102 }
 103 
 104 bool G1CodeRootSetTable::remove(nmethod* nm) {
 105   int index = hash_to_index(compute_hash(nm));
 106   Entry* previous = NULL;
 107   for (Entry* e = bucket(index); e != NULL; previous = e, e = e->next()) {
 108     if (e->literal() == nm) {
 109       remove_entry(e, previous);
 110       return true;
 111     }
 112   }
 113   return false;
 114 }
 115 
 116 void G1CodeRootSetTable::copy_to(G1CodeRootSetTable* new_table) {
 117   for (int index = 0; index < table_size(); ++index) {
 118     for (Entry* e = bucket(index); e != NULL; e = e->next()) {
 119       new_table->add(e->literal());
 120     }
 121   }
 122   new_table->copy_freelist(this);
 123 }
 124 
 125 void G1CodeRootSetTable::nmethods_do(CodeBlobClosure* blk) {
 126   for (int index = 0; index < table_size(); ++index) {
 127     for (Entry* e = bucket(index); e != NULL; e = e->next()) {
 128       blk->do_code_blob(e->literal());
 129     }
 130   }
 131 }
 132 
 133 template<typename CB>
 134 int G1CodeRootSetTable::remove_if(CB& should_remove) {
 135   int num_removed = 0;
 136   for (int index = 0; index < table_size(); ++index) {
 137     Entry* previous = NULL;
 138     Entry* e = bucket(index);
 139     while (e != NULL) {
 140       Entry* next = e->next();
 141       if (should_remove(e->literal())) {
 142         remove_entry(e, previous);
 143         ++num_removed;
 144       } else {
 145         previous = e;
 146       }
 147       e = next;
 148     }
 149   }
 150   return num_removed;
 151 }
 152 
 153 G1CodeRootSet::~G1CodeRootSet() {
 154   delete _table;
 155 }
 156 
 157 G1CodeRootSetTable* G1CodeRootSet::load_acquire_table() {
 158   return (G1CodeRootSetTable*) OrderAccess::load_ptr_acquire(&_table);
 159 }
 160 
 161 void G1CodeRootSet::allocate_small_table() {
 162   G1CodeRootSetTable* temp = new G1CodeRootSetTable(SmallSize);
 163 
 164   OrderAccess::release_store_ptr(&_table, temp);
 165 }
 166 
 167 void G1CodeRootSetTable::purge_list_append(G1CodeRootSetTable* table) {
 168   for (;;) {
 169     table->_purge_next = _purge_list;
 170     G1CodeRootSetTable* old = (G1CodeRootSetTable*) Atomic::cmpxchg_ptr(table, &_purge_list, table->_purge_next);
 171     if (old == table->_purge_next) {
 172       break;
 173     }
 174   }
 175 }
 176 
 177 void G1CodeRootSetTable::purge() {
 178   G1CodeRootSetTable* table = _purge_list;
 179   _purge_list = NULL;
 180   while (table != NULL) {
 181     G1CodeRootSetTable* to_purge = table;
 182     table = table->_purge_next;
 183     delete to_purge;
 184   }
 185 }
 186 
 187 void G1CodeRootSet::move_to_large() {
 188   G1CodeRootSetTable* temp = new G1CodeRootSetTable(LargeSize);
 189 
 190   _table->copy_to(temp);
 191 
 192   G1CodeRootSetTable::purge_list_append(_table);
 193 
 194   OrderAccess::release_store_ptr(&_table, temp);
 195 }
 196 
 197 void G1CodeRootSet::purge() {
 198   G1CodeRootSetTable::purge();
 199 }
 200 
 201 size_t G1CodeRootSet::static_mem_size() {
 202   return G1CodeRootSetTable::static_mem_size();
 203 }
 204 
 205 void G1CodeRootSet::add(nmethod* method) {
 206   bool added = false;
 207   if (is_empty()) {
 208     allocate_small_table();
 209   }
 210   added = _table->add(method);
 211   if (added) {
 212     if (_length == Threshold) {
 213       move_to_large();
 214     }
 215     ++_length;
 216   }
 217   assert(_length == (size_t)_table->number_of_entries(), "sizes should match");
 218 }
 219 
 220 bool G1CodeRootSet::remove(nmethod* method) {
 221   bool removed = false;
 222   if (_table != NULL) {
 223     removed = _table->remove(method);
 224   }
 225   if (removed) {
 226     _length--;
 227     if (_length == 0) {
 228       clear();
 229     }
 230   }
 231   assert((_length == 0 && _table == NULL) ||
 232          (_length == (size_t)_table->number_of_entries()), "sizes should match");
 233   return removed;
 234 }
 235 
 236 bool G1CodeRootSet::contains(nmethod* method) {
 237   G1CodeRootSetTable* table = load_acquire_table(); // contains() may be called outside of lock, so ensure mem sync.
 238   if (table != NULL) {
 239     return table->contains(method);
 240   }
 241   return false;
 242 }
 243 
 244 void G1CodeRootSet::clear() {
 245   delete _table;
 246   _table = NULL;
 247   _length = 0;
 248 }
 249 
 250 size_t G1CodeRootSet::mem_size() {
 251   return sizeof(*this) + (_table != NULL ? _table->mem_size() : 0);
 252 }
 253 
 254 void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const {
 255   if (_table != NULL) {
 256     _table->nmethods_do(blk);
 257   }
 258 }
 259 
 260 class CleanCallback : public StackObj {
 261   class PointsIntoHRDetectionClosure : public OopClosure {
 262     HeapRegion* _hr;
 263    public:
 264     bool _points_into;
 265     PointsIntoHRDetectionClosure(HeapRegion* hr) : _hr(hr), _points_into(false) {}
 266 
 267     void do_oop(narrowOop* o) {
 268       do_oop_work(o);
 269     }
 270 
 271     void do_oop(oop* o) {
 272       do_oop_work(o);
 273     }
 274 
 275     template <typename T>
 276     void do_oop_work(T* p) {
 277       if (_hr->is_in(oopDesc::load_decode_heap_oop(p))) {
 278         _points_into = true;
 279       }
 280     }
 281   };
 282 
 283   PointsIntoHRDetectionClosure _detector;
 284   CodeBlobToOopClosure _blobs;
 285 
 286  public:
 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 }