1 /*
   2  * Copyright (c) 2017, 2019, 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 #include "precompiled.hpp"
  25 #include "code/relocInfo.hpp"
  26 #include "code/nmethod.hpp"
  27 #include "code/icBuffer.hpp"
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shared/barrierSetNMethod.hpp"
  30 #include "gc/z/zGlobals.hpp"
  31 #include "gc/z/zHash.inline.hpp"
  32 #include "gc/z/zLock.inline.hpp"
  33 #include "gc/z/zNMethodData.hpp"
  34 #include "gc/z/zNMethodTable.hpp"
  35 #include "gc/z/zNMethodTableEntry.hpp"
  36 #include "gc/z/zNMethodTableIteration.hpp"
  37 #include "gc/z/zOopClosures.inline.hpp"
  38 #include "gc/z/zSafeDelete.inline.hpp"
  39 #include "gc/z/zTask.hpp"
  40 #include "gc/z/zWorkers.hpp"
  41 #include "logging/log.hpp"
  42 #include "memory/allocation.hpp"
  43 #include "memory/iterator.hpp"
  44 #include "memory/resourceArea.hpp"
  45 #include "utilities/debug.hpp"
  46 
  47 ZNMethodTableEntry* ZNMethodTable::_table = NULL;
  48 size_t ZNMethodTable::_size = 0;
  49 size_t ZNMethodTable::_nregistered = 0;
  50 size_t ZNMethodTable::_nunregistered = 0;
  51 ZNMethodTableIteration ZNMethodTable::_iteration;
  52 ZSafeDeleteNoLock<ZNMethodTableEntry[]> ZNMethodTable::_safe_delete;
  53 
  54 size_t ZNMethodTable::first_index(const nmethod* nm, size_t size) {
  55   assert(is_power_of_2(size), "Invalid size");
  56   const size_t mask = size - 1;
  57   const size_t hash = ZHash::address_to_uint32((uintptr_t)nm);
  58   return hash & mask;
  59 }
  60 
  61 size_t ZNMethodTable::next_index(size_t prev_index, size_t size) {
  62   assert(is_power_of_2(size), "Invalid size");
  63   const size_t mask = size - 1;
  64   return (prev_index + 1) & mask;
  65 }
  66 
  67 bool ZNMethodTable::register_entry(ZNMethodTableEntry* table, size_t size, nmethod* nm) {
  68   const ZNMethodTableEntry entry(nm);
  69   size_t index = first_index(nm, size);
  70 
  71   for (;;) {
  72     const ZNMethodTableEntry table_entry = table[index];
  73 
  74     if (!table_entry.registered() && !table_entry.unregistered()) {
  75       // Insert new entry
  76       table[index] = entry;
  77       return true;
  78     }
  79 
  80     if (table_entry.registered() && table_entry.method() == nm) {
  81       // Replace existing entry
  82       table[index] = entry;
  83       return false;
  84     }
  85 
  86     index = next_index(index, size);
  87   }
  88 }
  89 
  90 void ZNMethodTable::unregister_entry(ZNMethodTableEntry* table, size_t size, nmethod* nm) {
  91   size_t index = first_index(nm, size);
  92 
  93   for (;;) {
  94     const ZNMethodTableEntry table_entry = table[index];
  95     assert(table_entry.registered() || table_entry.unregistered(), "Entry not found");
  96 
  97     if (table_entry.registered() && table_entry.method() == nm) {
  98       // Remove entry
  99       table[index] = ZNMethodTableEntry(true /* unregistered */);
 100       return;
 101     }
 102 
 103     index = next_index(index, size);
 104   }
 105 }
 106 
 107 void ZNMethodTable::rebuild(size_t new_size) {
 108   assert(CodeCache_lock->owned_by_self(), "Lock must be held");
 109 
 110   assert(is_power_of_2(new_size), "Invalid size");
 111 
 112   log_debug(gc, nmethod)("Rebuilding NMethod Table: "
 113                          SIZE_FORMAT "->" SIZE_FORMAT " entries, "
 114                          SIZE_FORMAT "(%.0f%%->%.0f%%) registered, "
 115                          SIZE_FORMAT "(%.0f%%->%.0f%%) unregistered",
 116                          _size, new_size,
 117                          _nregistered, percent_of(_nregistered, _size), percent_of(_nregistered, new_size),
 118                          _nunregistered, percent_of(_nunregistered, _size), 0.0);
 119 
 120   // Allocate new table
 121   ZNMethodTableEntry* const new_table = new ZNMethodTableEntry[new_size];
 122 
 123   // Transfer all registered entries
 124   for (size_t i = 0; i < _size; i++) {
 125     const ZNMethodTableEntry entry = _table[i];
 126     if (entry.registered()) {
 127       register_entry(new_table, new_size, entry.method());
 128     }
 129   }
 130 
 131   // Free old table
 132   _safe_delete(_table);
 133 
 134   // Install new table
 135   _table = new_table;
 136   _size = new_size;
 137   _nunregistered = 0;
 138 }
 139 
 140 void ZNMethodTable::rebuild_if_needed() {
 141   // The hash table uses linear probing. To avoid wasting memory while
 142   // at the same time maintaining good hash collision behavior we want
 143   // to keep the table occupancy between 30% and 70%. The table always
 144   // grows/shrinks by doubling/halving its size. Pruning of unregistered
 145   // entries is done by rebuilding the table with or without resizing it.
 146   const size_t min_size = 1024;
 147   const size_t shrink_threshold = _size * 0.30;
 148   const size_t prune_threshold = _size * 0.65;
 149   const size_t grow_threshold = _size * 0.70;
 150 
 151   if (_size == 0) {
 152     // Initialize table
 153     rebuild(min_size);
 154   } else if (_nregistered < shrink_threshold && _size > min_size) {
 155     // Shrink table
 156     rebuild(_size / 2);
 157   } else if (_nregistered + _nunregistered > grow_threshold) {
 158     // Prune or grow table
 159     if (_nregistered < prune_threshold) {
 160       // Prune table
 161       rebuild(_size);
 162     } else {
 163       // Grow table
 164       rebuild(_size * 2);
 165     }
 166   }
 167 }
 168 
 169 size_t ZNMethodTable::registered_nmethods() {
 170   return _nregistered;
 171 }
 172 
 173 size_t ZNMethodTable::unregistered_nmethods() {
 174   return _nunregistered;
 175 }
 176 
 177 void ZNMethodTable::register_nmethod(nmethod* nm) {
 178   assert(CodeCache_lock->owned_by_self(), "Lock must be held");
 179 
 180   // Grow/Shrink/Prune table if needed
 181   rebuild_if_needed();
 182 
 183   // Insert new entry
 184   if (register_entry(_table, _size, nm)) {
 185     // New entry registered. When register_entry() instead returns
 186     // false the nmethod was already in the table so we do not want
 187     // to increase number of registered entries in that case.
 188     _nregistered++;
 189   }
 190 }
 191 
 192 void ZNMethodTable::wait_until_iteration_done() {
 193   assert(CodeCache_lock->owned_by_self(), "Lock must be held");
 194 
 195   while (_iteration.in_progress()) {
 196     CodeCache_lock->wait_without_safepoint_check();
 197   }
 198 }
 199 
 200 void ZNMethodTable::unregister_nmethod(nmethod* nm) {
 201   assert(CodeCache_lock->owned_by_self(), "Lock must be held");
 202 
 203   // Remove entry
 204   unregister_entry(_table, _size, nm);
 205   _nunregistered++;
 206   _nregistered--;
 207 }
 208 
 209 void ZNMethodTable::nmethods_do_begin() {
 210   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 211 
 212   // Do not allow the table to be deleted while iterating
 213   _safe_delete.enable_deferred_delete();
 214 
 215   // Prepare iteration
 216   _iteration.nmethods_do_begin(_table, _size);
 217 }
 218 
 219 void ZNMethodTable::nmethods_do_end() {
 220   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 221 
 222   // Finish iteration
 223   _iteration.nmethods_do_end();
 224 
 225   // Allow the table to be deleted
 226   _safe_delete.disable_deferred_delete();
 227 
 228   // Notify iteration done
 229   CodeCache_lock->notify_all();
 230 }
 231 
 232 void ZNMethodTable::nmethods_do(NMethodClosure* cl) {
 233   _iteration.nmethods_do(cl);
 234 }