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