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