1/* 2 * Copyright (c) 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 "gc/shared/gcLocker.hpp" 27#include "memory/allocation.hpp" 28#include "oops/oop.inline.hpp" 29#include "oops/method.hpp" 30#include "oops/symbol.hpp" 31#include "prims/resolvedMethodTable.hpp" 32#include "runtime/handles.inline.hpp" 33#include "runtime/mutexLocker.hpp" 34#include "utilities/hashtable.inline.hpp" 35#include "utilities/macros.hpp" 36#if INCLUDE_ALL_GCS 37#include "gc/g1/g1SATBCardTableModRefBS.hpp" 38#endif 39 40 41ResolvedMethodTable::ResolvedMethodTable() 42 : Hashtable<oop, mtClass>(_table_size, sizeof(ResolvedMethodEntry)) { } 43 44oop ResolvedMethodTable::lookup(int index, unsigned int hash, Method* method) { 45 for (ResolvedMethodEntry* p = bucket(index); p != NULL; p = p->next()) { 46 if (p->hash() == hash) { 47 oop target = p->literal(); 48 // The method is in the table as a target already 49 if (java_lang_invoke_ResolvedMethodName::vmtarget(target) == method) { 50 ResourceMark rm; 51 log_debug(membername, table) ("ResolvedMethod entry found for %s index %d", 52 method->name_and_sig_as_C_string(), index); 53 return target; 54 } 55 } 56 } 57 return NULL; 58} 59 60unsigned int ResolvedMethodTable::compute_hash(Method* method) { 61 unsigned int name_hash = method->name()->identity_hash(); 62 unsigned int signature_hash = method->signature()->identity_hash(); 63 return name_hash ^ signature_hash; 64} 65 66 67oop ResolvedMethodTable::lookup(Method* method) { 68 unsigned int hash = compute_hash(method); 69 int index = hash_to_index(hash); 70 return lookup(index, hash, method); 71} 72 73// Tell the GC that this oop was looked up in the table 74static void ensure_oop_alive(oop mname) { 75 // A lookup in the ResolvedMethodTable could return an object that was previously 76 // considered dead. The SATB part of G1 needs to get notified about this 77 // potential resurrection, otherwise the marking might not find the object. 78#if INCLUDE_ALL_GCS 79 if (UseG1GC && mname != NULL) { 80 G1SATBCardTableModRefBS::enqueue(mname); 81 } 82#endif 83} 84 85oop ResolvedMethodTable::basic_add(Method* method, oop rmethod_name) { 86 assert_locked_or_safepoint(ResolvedMethodTable_lock); 87 88 unsigned int hash = compute_hash(method); 89 int index = hash_to_index(hash); 90 91 // One was added while aquiring the lock 92 oop entry = lookup(index, hash, method); 93 if (entry != NULL) { 94 ensure_oop_alive(entry); 95 return entry; 96 } 97 98 ResolvedMethodEntry* p = (ResolvedMethodEntry*) Hashtable<oop, mtClass>::new_entry(hash, rmethod_name); 99 Hashtable<oop, mtClass>::add_entry(index, p); 100 ResourceMark rm; 101 log_debug(membername, table) ("ResolvedMethod entry added for %s index %d", 102 method->name_and_sig_as_C_string(), index); 103 return p->literal(); 104} 105 106ResolvedMethodTable* ResolvedMethodTable::_the_table = NULL; 107 108oop ResolvedMethodTable::find_method(Method* method) { 109 oop entry = _the_table->lookup(method); 110 ensure_oop_alive(entry); 111 return entry; 112} 113 114oop ResolvedMethodTable::add_method(Handle resolved_method_name) { 115 MutexLocker ml(ResolvedMethodTable_lock); 116 DEBUG_ONLY(NoSafepointVerifier nsv); 117 118 // Check if method has been redefined while taking out ResolvedMethodTable_lock, if so 119 // use new method. 120 Method* method = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(resolved_method_name()); 121 assert(method->is_method(), "must be method"); 122 if (method->is_old()) { 123 // Replace method with redefined version 124 InstanceKlass* holder = method->method_holder(); 125 method = holder->method_with_idnum(method->method_idnum()); 126 java_lang_invoke_ResolvedMethodName::set_vmtarget(resolved_method_name(), method); 127 } 128 // Set flag in class to indicate this InstanceKlass has entries in the table 129 // to avoid walking table during redefinition if none of the redefined classes 130 // have any membernames in the table. 131 method->method_holder()->set_has_resolved_methods(); 132 133 return _the_table->basic_add(method, resolved_method_name()); 134} 135 136// Removing entries 137int ResolvedMethodTable::_oops_removed = 0; 138int ResolvedMethodTable::_oops_counted = 0; 139 140// Serially invoke removed unused oops from the table. 141// This is done late during GC. 142void ResolvedMethodTable::unlink(BoolObjectClosure* is_alive) { 143 _oops_removed = 0; 144 _oops_counted = 0; 145 for (int i = 0; i < _the_table->table_size(); ++i) { 146 ResolvedMethodEntry** p = _the_table->bucket_addr(i); 147 ResolvedMethodEntry* entry = _the_table->bucket(i); 148 while (entry != NULL) { 149 _oops_counted++; 150 if (is_alive->do_object_b(entry->literal())) { 151 p = entry->next_addr(); 152 } else { 153 _oops_removed++; 154 if (log_is_enabled(Debug, membername, table)) { 155 Method* m = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(entry->literal()); 156 ResourceMark rm; 157 log_debug(membername, table) ("ResolvedMethod entry removed for %s index %d", 158 m->name_and_sig_as_C_string(), i); 159 } 160 *p = entry->next(); 161 _the_table->free_entry(entry); 162 } 163 // get next entry 164 entry = (ResolvedMethodEntry*)HashtableEntry<oop, mtClass>::make_ptr(*p); 165 } 166 } 167 log_debug(membername, table) ("ResolvedMethod entries counted %d removed %d", 168 _oops_counted, _oops_removed); 169} 170 171// Serially invoke "f->do_oop" on the locations of all oops in the table. 172void ResolvedMethodTable::oops_do(OopClosure* f) { 173 for (int i = 0; i < _the_table->table_size(); ++i) { 174 ResolvedMethodEntry* entry = _the_table->bucket(i); 175 while (entry != NULL) { 176 f->do_oop(entry->literal_addr()); 177 entry = entry->next(); 178 } 179 } 180} 181 182#ifndef PRODUCT 183void ResolvedMethodTable::print() { 184 for (int i = 0; i < table_size(); ++i) { 185 ResolvedMethodEntry* entry = bucket(i); 186 while (entry != NULL) { 187 tty->print("%d : ", i); 188 oop rmethod_name = entry->literal(); 189 rmethod_name->print(); 190 Method* m = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(rmethod_name); 191 m->print(); 192 entry = entry->next(); 193 } 194 } 195} 196#endif // PRODUCT 197 198#if INCLUDE_JVMTI 199// It is called at safepoint only for RedefineClasses 200void ResolvedMethodTable::adjust_method_entries(bool * trace_name_printed) { 201 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); 202 // For each entry in RMT, change to new method 203 for (int i = 0; i < _the_table->table_size(); ++i) { 204 ResolvedMethodEntry* entry = _the_table->bucket(i); 205 while (entry != NULL) { 206 207 oop mem_name = entry->literal(); 208 Method* old_method = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(mem_name); 209 210 if (old_method->is_old()) { 211 212 if (old_method->is_deleted()) { 213 // leave deleted method in ResolvedMethod for now (this is a bug that we don't mark 214 // these on_stack) 215 continue; 216 } 217 218 InstanceKlass* holder = old_method->method_holder(); 219 Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum()); 220 assert(holder == new_method->method_holder(), "call after swapping redefined guts"); 221 assert(new_method != NULL, "method_with_idnum() should not be NULL"); 222 assert(old_method != new_method, "sanity check"); 223 224 java_lang_invoke_ResolvedMethodName::set_vmtarget(mem_name, new_method); 225 226 ResourceMark rm; 227 if (!(*trace_name_printed)) { 228 log_info(redefine, class, update)("adjust: name=%s", old_method->method_holder()->external_name()); 229 *trace_name_printed = true; 230 } 231 log_debug(redefine, class, update, constantpool) 232 ("ResolvedMethod method update: %s(%s)", 233 new_method->name()->as_C_string(), new_method->signature()->as_C_string()); 234 } 235 entry = entry->next(); 236 } 237 } 238} 239#endif // INCLUDE_JVMTI 240