Path: blob/master/src/hotspot/share/interpreter/oopMapCache.cpp
40949 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "interpreter/oopMapCache.hpp"26#include "logging/log.hpp"27#include "logging/logStream.hpp"28#include "memory/allocation.inline.hpp"29#include "memory/resourceArea.hpp"30#include "oops/oop.inline.hpp"31#include "runtime/atomic.hpp"32#include "runtime/handles.inline.hpp"33#include "runtime/safepoint.hpp"34#include "runtime/signature.hpp"3536class OopMapCacheEntry: private InterpreterOopMap {37friend class InterpreterOopMap;38friend class OopMapForCacheEntry;39friend class OopMapCache;40friend class VerifyClosure;4142private:43OopMapCacheEntry* _next;4445protected:46// Initialization47void fill(const methodHandle& method, int bci);48// fills the bit mask for native calls49void fill_for_native(const methodHandle& method);50void set_mask(CellTypeState* vars, CellTypeState* stack, int stack_top);5152// Deallocate bit masks and initialize fields53void flush();5455private:56void allocate_bit_mask(); // allocates the bit mask on C heap f necessary57void deallocate_bit_mask(); // allocates the bit mask on C heap f necessary58bool verify_mask(CellTypeState *vars, CellTypeState *stack, int max_locals, int stack_top);5960public:61OopMapCacheEntry() : InterpreterOopMap() {62_next = NULL;63#ifdef ASSERT64_resource_allocate_bit_mask = false;65#endif66}67};686970// Implementation of OopMapForCacheEntry71// (subclass of GenerateOopMap, initializes an OopMapCacheEntry for a given method and bci)7273class OopMapForCacheEntry: public GenerateOopMap {74OopMapCacheEntry *_entry;75int _bci;76int _stack_top;7778virtual bool report_results() const { return false; }79virtual bool possible_gc_point (BytecodeStream *bcs);80virtual void fill_stackmap_prolog (int nof_gc_points);81virtual void fill_stackmap_epilog ();82virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs,83CellTypeState* vars,84CellTypeState* stack,85int stack_top);86virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars);8788public:89OopMapForCacheEntry(const methodHandle& method, int bci, OopMapCacheEntry *entry);9091// Computes stack map for (method,bci) and initialize entry92bool compute_map(Thread* current);93int size();94};959697OopMapForCacheEntry::OopMapForCacheEntry(const methodHandle& method, int bci, OopMapCacheEntry* entry) : GenerateOopMap(method) {98_bci = bci;99_entry = entry;100_stack_top = -1;101}102103104bool OopMapForCacheEntry::compute_map(Thread* current) {105assert(!method()->is_native(), "cannot compute oop map for native methods");106// First check if it is a method where the stackmap is always empty107if (method()->code_size() == 0 || method()->max_locals() + method()->max_stack() == 0) {108_entry->set_mask_size(0);109} else {110ResourceMark rm;111if (!GenerateOopMap::compute_map(current)) {112fatal("Unrecoverable verification or out-of-memory error");113return false;114}115result_for_basicblock(_bci);116}117return true;118}119120121bool OopMapForCacheEntry::possible_gc_point(BytecodeStream *bcs) {122return false; // We are not reporting any result. We call result_for_basicblock directly123}124125126void OopMapForCacheEntry::fill_stackmap_prolog(int nof_gc_points) {127// Do nothing128}129130131void OopMapForCacheEntry::fill_stackmap_epilog() {132// Do nothing133}134135136void OopMapForCacheEntry::fill_init_vars(GrowableArray<intptr_t> *init_vars) {137// Do nothing138}139140141void OopMapForCacheEntry::fill_stackmap_for_opcodes(BytecodeStream *bcs,142CellTypeState* vars,143CellTypeState* stack,144int stack_top) {145// Only interested in one specific bci146if (bcs->bci() == _bci) {147_entry->set_mask(vars, stack, stack_top);148_stack_top = stack_top;149}150}151152153int OopMapForCacheEntry::size() {154assert(_stack_top != -1, "compute_map must be called first");155return ((method()->is_static()) ? 0 : 1) + method()->max_locals() + _stack_top;156}157158159// Implementation of InterpreterOopMap and OopMapCacheEntry160161class VerifyClosure : public OffsetClosure {162private:163OopMapCacheEntry* _entry;164bool _failed;165166public:167VerifyClosure(OopMapCacheEntry* entry) { _entry = entry; _failed = false; }168void offset_do(int offset) { if (!_entry->is_oop(offset)) _failed = true; }169bool failed() const { return _failed; }170};171172InterpreterOopMap::InterpreterOopMap() {173initialize();174#ifdef ASSERT175_resource_allocate_bit_mask = true;176#endif177}178179InterpreterOopMap::~InterpreterOopMap() {180// The expection is that the bit mask was allocated181// last in this resource area. That would make the free of the182// bit_mask effective (see how FREE_RESOURCE_ARRAY does a free).183// If it was not allocated last, there is not a correctness problem184// but the space for the bit_mask is not freed.185assert(_resource_allocate_bit_mask, "Trying to free C heap space");186if (mask_size() > small_mask_limit) {187FREE_RESOURCE_ARRAY(uintptr_t, _bit_mask[0], mask_word_size());188}189}190191bool InterpreterOopMap::is_empty() const {192bool result = _method == NULL;193assert(_method != NULL || (_bci == 0 &&194(_mask_size == 0 || _mask_size == USHRT_MAX) &&195_bit_mask[0] == 0), "Should be completely empty");196return result;197}198199void InterpreterOopMap::initialize() {200_method = NULL;201_mask_size = USHRT_MAX; // This value should cause a failure quickly202_bci = 0;203_expression_stack_size = 0;204for (int i = 0; i < N; i++) _bit_mask[i] = 0;205}206207void InterpreterOopMap::iterate_oop(OffsetClosure* oop_closure) const {208int n = number_of_entries();209int word_index = 0;210uintptr_t value = 0;211uintptr_t mask = 0;212// iterate over entries213for (int i = 0; i < n; i++, mask <<= bits_per_entry) {214// get current word215if (mask == 0) {216value = bit_mask()[word_index++];217mask = 1;218}219// test for oop220if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i);221}222}223224void InterpreterOopMap::print() const {225int n = number_of_entries();226tty->print("oop map for ");227method()->print_value();228tty->print(" @ %d = [%d] { ", bci(), n);229for (int i = 0; i < n; i++) {230if (is_dead(i)) tty->print("%d+ ", i);231else232if (is_oop(i)) tty->print("%d ", i);233}234tty->print_cr("}");235}236237class MaskFillerForNative: public NativeSignatureIterator {238private:239uintptr_t * _mask; // the bit mask to be filled240int _size; // the mask size in bits241242void set_one(int i) {243i *= InterpreterOopMap::bits_per_entry;244assert(0 <= i && i < _size, "offset out of bounds");245_mask[i / BitsPerWord] |= (((uintptr_t) 1 << InterpreterOopMap::oop_bit_number) << (i % BitsPerWord));246}247248public:249void pass_byte() { /* ignore */ }250void pass_short() { /* ignore */ }251void pass_int() { /* ignore */ }252void pass_long() { /* ignore */ }253void pass_float() { /* ignore */ }254void pass_double() { /* ignore */ }255void pass_object() { set_one(offset()); }256257MaskFillerForNative(const methodHandle& method, uintptr_t* mask, int size) : NativeSignatureIterator(method) {258_mask = mask;259_size = size;260// initialize with 0261int i = (size + BitsPerWord - 1) / BitsPerWord;262while (i-- > 0) _mask[i] = 0;263}264265void generate() {266iterate();267}268};269270bool OopMapCacheEntry::verify_mask(CellTypeState* vars, CellTypeState* stack, int max_locals, int stack_top) {271// Check mask includes map272VerifyClosure blk(this);273iterate_oop(&blk);274if (blk.failed()) return false;275276// Check if map is generated correctly277// (Use ?: operator to make sure all 'true' & 'false' are represented exactly the same so we can use == afterwards)278Log(interpreter, oopmap) logv;279LogStream st(logv.trace());280281st.print("Locals (%d): ", max_locals);282for(int i = 0; i < max_locals; i++) {283bool v1 = is_oop(i) ? true : false;284bool v2 = vars[i].is_reference() ? true : false;285assert(v1 == v2, "locals oop mask generation error");286st.print("%d", v1 ? 1 : 0);287}288st.cr();289290st.print("Stack (%d): ", stack_top);291for(int j = 0; j < stack_top; j++) {292bool v1 = is_oop(max_locals + j) ? true : false;293bool v2 = stack[j].is_reference() ? true : false;294assert(v1 == v2, "stack oop mask generation error");295st.print("%d", v1 ? 1 : 0);296}297st.cr();298return true;299}300301void OopMapCacheEntry::allocate_bit_mask() {302if (mask_size() > small_mask_limit) {303assert(_bit_mask[0] == 0, "bit mask should be new or just flushed");304_bit_mask[0] = (intptr_t)305NEW_C_HEAP_ARRAY(uintptr_t, mask_word_size(), mtClass);306}307}308309void OopMapCacheEntry::deallocate_bit_mask() {310if (mask_size() > small_mask_limit && _bit_mask[0] != 0) {311assert(!Thread::current()->resource_area()->contains((void*)_bit_mask[0]),312"This bit mask should not be in the resource area");313FREE_C_HEAP_ARRAY(uintptr_t, _bit_mask[0]);314debug_only(_bit_mask[0] = 0;)315}316}317318319void OopMapCacheEntry::fill_for_native(const methodHandle& mh) {320assert(mh->is_native(), "method must be native method");321set_mask_size(mh->size_of_parameters() * bits_per_entry);322allocate_bit_mask();323// fill mask for parameters324MaskFillerForNative mf(mh, bit_mask(), mask_size());325mf.generate();326}327328329void OopMapCacheEntry::fill(const methodHandle& method, int bci) {330// Flush entry to deallocate an existing entry331flush();332set_method(method());333set_bci(bci);334if (method->is_native()) {335// Native method activations have oops only among the parameters and one336// extra oop following the parameters (the mirror for static native methods).337fill_for_native(method);338} else {339OopMapForCacheEntry gen(method, bci, this);340if (!gen.compute_map(Thread::current())) {341fatal("Unrecoverable verification or out-of-memory error");342}343}344}345346347void OopMapCacheEntry::set_mask(CellTypeState *vars, CellTypeState *stack, int stack_top) {348// compute bit mask size349int max_locals = method()->max_locals();350int n_entries = max_locals + stack_top;351set_mask_size(n_entries * bits_per_entry);352allocate_bit_mask();353set_expression_stack_size(stack_top);354355// compute bits356int word_index = 0;357uintptr_t value = 0;358uintptr_t mask = 1;359360CellTypeState* cell = vars;361for (int entry_index = 0; entry_index < n_entries; entry_index++, mask <<= bits_per_entry, cell++) {362// store last word363if (mask == 0) {364bit_mask()[word_index++] = value;365value = 0;366mask = 1;367}368369// switch to stack when done with locals370if (entry_index == max_locals) {371cell = stack;372}373374// set oop bit375if ( cell->is_reference()) {376value |= (mask << oop_bit_number );377}378379// set dead bit380if (!cell->is_live()) {381value |= (mask << dead_bit_number);382assert(!cell->is_reference(), "dead value marked as oop");383}384}385386// make sure last word is stored387bit_mask()[word_index] = value;388389// verify bit mask390assert(verify_mask(vars, stack, max_locals, stack_top), "mask could not be verified");391}392393void OopMapCacheEntry::flush() {394deallocate_bit_mask();395initialize();396}397398399// Implementation of OopMapCache400401void InterpreterOopMap::resource_copy(OopMapCacheEntry* from) {402assert(_resource_allocate_bit_mask,403"Should not resource allocate the _bit_mask");404405set_method(from->method());406set_bci(from->bci());407set_mask_size(from->mask_size());408set_expression_stack_size(from->expression_stack_size());409410// Is the bit mask contained in the entry?411if (from->mask_size() <= small_mask_limit) {412memcpy((void *)_bit_mask, (void *)from->_bit_mask,413mask_word_size() * BytesPerWord);414} else {415// The expectation is that this InterpreterOopMap is a recently created416// and empty. It is used to get a copy of a cached entry.417// If the bit mask has a value, it should be in the418// resource area.419assert(_bit_mask[0] == 0 ||420Thread::current()->resource_area()->contains((void*)_bit_mask[0]),421"The bit mask should have been allocated from a resource area");422// Allocate the bit_mask from a Resource area for performance. Allocating423// from the C heap as is done for OopMapCache has a significant424// performance impact.425_bit_mask[0] = (uintptr_t) NEW_RESOURCE_ARRAY(uintptr_t, mask_word_size());426assert(_bit_mask[0] != 0, "bit mask was not allocated");427memcpy((void*) _bit_mask[0], (void*) from->_bit_mask[0],428mask_word_size() * BytesPerWord);429}430}431432inline unsigned int OopMapCache::hash_value_for(const methodHandle& method, int bci) const {433// We use method->code_size() rather than method->identity_hash() below since434// the mark may not be present if a pointer to the method is already reversed.435return ((unsigned int) bci)436^ ((unsigned int) method->max_locals() << 2)437^ ((unsigned int) method->code_size() << 4)438^ ((unsigned int) method->size_of_parameters() << 6);439}440441OopMapCacheEntry* volatile OopMapCache::_old_entries = NULL;442443OopMapCache::OopMapCache() {444_array = NEW_C_HEAP_ARRAY(OopMapCacheEntry*, _size, mtClass);445for(int i = 0; i < _size; i++) _array[i] = NULL;446}447448449OopMapCache::~OopMapCache() {450assert(_array != NULL, "sanity check");451// Deallocate oop maps that are allocated out-of-line452flush();453// Deallocate array454FREE_C_HEAP_ARRAY(OopMapCacheEntry*, _array);455}456457OopMapCacheEntry* OopMapCache::entry_at(int i) const {458return Atomic::load_acquire(&(_array[i % _size]));459}460461bool OopMapCache::put_at(int i, OopMapCacheEntry* entry, OopMapCacheEntry* old) {462return Atomic::cmpxchg(&_array[i % _size], old, entry) == old;463}464465void OopMapCache::flush() {466for (int i = 0; i < _size; i++) {467OopMapCacheEntry* entry = _array[i];468if (entry != NULL) {469_array[i] = NULL; // no barrier, only called in OopMapCache destructor470entry->flush();471FREE_C_HEAP_OBJ(entry);472}473}474}475476void OopMapCache::flush_obsolete_entries() {477assert(SafepointSynchronize::is_at_safepoint(), "called by RedefineClasses in a safepoint");478for (int i = 0; i < _size; i++) {479OopMapCacheEntry* entry = _array[i];480if (entry != NULL && !entry->is_empty() && entry->method()->is_old()) {481// Cache entry is occupied by an old redefined method and we don't want482// to pin it down so flush the entry.483if (log_is_enabled(Debug, redefine, class, oopmap)) {484ResourceMark rm;485log_debug(redefine, class, interpreter, oopmap)486("flush: %s(%s): cached entry @%d",487entry->method()->name()->as_C_string(), entry->method()->signature()->as_C_string(), i);488}489_array[i] = NULL;490entry->flush();491FREE_C_HEAP_OBJ(entry);492}493}494}495496// Called by GC for thread root scan during a safepoint only. The other interpreted frame oopmaps497// are generated locally and not cached.498void OopMapCache::lookup(const methodHandle& method,499int bci,500InterpreterOopMap* entry_for) {501assert(SafepointSynchronize::is_at_safepoint(), "called by GC in a safepoint");502int probe = hash_value_for(method, bci);503int i;504OopMapCacheEntry* entry = NULL;505506if (log_is_enabled(Debug, interpreter, oopmap)) {507static int count = 0;508ResourceMark rm;509log_debug(interpreter, oopmap)510("%d - Computing oopmap at bci %d for %s at hash %d", ++count, bci,511method()->name_and_sig_as_C_string(), probe);512}513514// Search hashtable for match515for(i = 0; i < _probe_depth; i++) {516entry = entry_at(probe + i);517if (entry != NULL && !entry->is_empty() && entry->match(method, bci)) {518entry_for->resource_copy(entry);519assert(!entry_for->is_empty(), "A non-empty oop map should be returned");520log_debug(interpreter, oopmap)("- found at hash %d", probe + i);521return;522}523}524525// Entry is not in hashtable.526// Compute entry527528OopMapCacheEntry* tmp = NEW_C_HEAP_OBJ(OopMapCacheEntry, mtClass);529tmp->initialize();530tmp->fill(method, bci);531entry_for->resource_copy(tmp);532533if (method->should_not_be_cached()) {534// It is either not safe or not a good idea to cache this Method*535// at this time. We give the caller of lookup() a copy of the536// interesting info via parameter entry_for, but we don't add it to537// the cache. See the gory details in Method*.cpp.538FREE_C_HEAP_OBJ(tmp);539return;540}541542// First search for an empty slot543for(i = 0; i < _probe_depth; i++) {544entry = entry_at(probe + i);545if (entry == NULL) {546if (put_at(probe + i, tmp, NULL)) {547assert(!entry_for->is_empty(), "A non-empty oop map should be returned");548return;549}550}551}552553log_debug(interpreter, oopmap)("*** collision in oopmap cache - flushing item ***");554555// No empty slot (uncommon case). Use (some approximation of a) LRU algorithm556// where the first entry in the collision array is replaced with the new one.557OopMapCacheEntry* old = entry_at(probe + 0);558if (put_at(probe + 0, tmp, old)) {559enqueue_for_cleanup(old);560} else {561enqueue_for_cleanup(tmp);562}563564assert(!entry_for->is_empty(), "A non-empty oop map should be returned");565return;566}567568void OopMapCache::enqueue_for_cleanup(OopMapCacheEntry* entry) {569bool success = false;570OopMapCacheEntry* head;571do {572head = _old_entries;573entry->_next = head;574success = Atomic::cmpxchg(&_old_entries, head, entry) == head;575} while (!success);576577if (log_is_enabled(Debug, interpreter, oopmap)) {578ResourceMark rm;579log_debug(interpreter, oopmap)("enqueue %s at bci %d for cleanup",580entry->method()->name_and_sig_as_C_string(), entry->bci());581}582}583584// This is called after GC threads are done and nothing is accessing the old_entries585// list, so no synchronization needed.586void OopMapCache::cleanup_old_entries() {587OopMapCacheEntry* entry = _old_entries;588_old_entries = NULL;589while (entry != NULL) {590if (log_is_enabled(Debug, interpreter, oopmap)) {591ResourceMark rm;592log_debug(interpreter, oopmap)("cleanup entry %s at bci %d",593entry->method()->name_and_sig_as_C_string(), entry->bci());594}595OopMapCacheEntry* next = entry->_next;596entry->flush();597FREE_C_HEAP_OBJ(entry);598entry = next;599}600}601602void OopMapCache::compute_one_oop_map(const methodHandle& method, int bci, InterpreterOopMap* entry) {603// Due to the invariants above it's tricky to allocate a temporary OopMapCacheEntry on the stack604OopMapCacheEntry* tmp = NEW_C_HEAP_OBJ(OopMapCacheEntry, mtClass);605tmp->initialize();606tmp->fill(method, bci);607entry->resource_copy(tmp);608FREE_C_HEAP_OBJ(tmp);609}610611612