aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "interpreter/oopMapCache.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "prims/jvmtiRedefineClassesTrace.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/signature.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: class OopMapCacheEntry: private InterpreterOopMap { aoqi@0: friend class InterpreterOopMap; aoqi@0: friend class OopMapForCacheEntry; aoqi@0: friend class OopMapCache; aoqi@0: friend class VerifyClosure; aoqi@0: aoqi@0: protected: aoqi@0: // Initialization aoqi@0: void fill(methodHandle method, int bci); aoqi@0: // fills the bit mask for native calls aoqi@0: void fill_for_native(methodHandle method); aoqi@0: void set_mask(CellTypeState* vars, CellTypeState* stack, int stack_top); aoqi@0: aoqi@0: // Deallocate bit masks and initialize fields aoqi@0: void flush(); aoqi@0: aoqi@0: private: aoqi@0: void allocate_bit_mask(); // allocates the bit mask on C heap f necessary aoqi@0: void deallocate_bit_mask(); // allocates the bit mask on C heap f necessary aoqi@0: bool verify_mask(CellTypeState *vars, CellTypeState *stack, int max_locals, int stack_top); aoqi@0: aoqi@0: public: aoqi@0: OopMapCacheEntry() : InterpreterOopMap() { aoqi@0: #ifdef ASSERT aoqi@0: _resource_allocate_bit_mask = false; aoqi@0: #endif aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // Implementation of OopMapForCacheEntry aoqi@0: // (subclass of GenerateOopMap, initializes an OopMapCacheEntry for a given method and bci) aoqi@0: aoqi@0: class OopMapForCacheEntry: public GenerateOopMap { aoqi@0: OopMapCacheEntry *_entry; aoqi@0: int _bci; aoqi@0: int _stack_top; aoqi@0: aoqi@0: virtual bool report_results() const { return false; } aoqi@0: virtual bool possible_gc_point (BytecodeStream *bcs); aoqi@0: virtual void fill_stackmap_prolog (int nof_gc_points); aoqi@0: virtual void fill_stackmap_epilog (); aoqi@0: virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs, aoqi@0: CellTypeState* vars, aoqi@0: CellTypeState* stack, aoqi@0: int stack_top); aoqi@0: virtual void fill_init_vars (GrowableArray *init_vars); aoqi@0: aoqi@0: public: aoqi@0: OopMapForCacheEntry(methodHandle method, int bci, OopMapCacheEntry *entry); aoqi@0: aoqi@0: // Computes stack map for (method,bci) and initialize entry aoqi@0: void compute_map(TRAPS); aoqi@0: int size(); aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: OopMapForCacheEntry::OopMapForCacheEntry(methodHandle method, int bci, OopMapCacheEntry* entry) : GenerateOopMap(method) { aoqi@0: _bci = bci; aoqi@0: _entry = entry; aoqi@0: _stack_top = -1; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void OopMapForCacheEntry::compute_map(TRAPS) { aoqi@0: assert(!method()->is_native(), "cannot compute oop map for native methods"); aoqi@0: // First check if it is a method where the stackmap is always empty aoqi@0: if (method()->code_size() == 0 || method()->max_locals() + method()->max_stack() == 0) { aoqi@0: _entry->set_mask_size(0); aoqi@0: } else { aoqi@0: ResourceMark rm; aoqi@0: GenerateOopMap::compute_map(CATCH); aoqi@0: result_for_basicblock(_bci); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool OopMapForCacheEntry::possible_gc_point(BytecodeStream *bcs) { aoqi@0: return false; // We are not reporting any result. We call result_for_basicblock directly aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void OopMapForCacheEntry::fill_stackmap_prolog(int nof_gc_points) { aoqi@0: // Do nothing aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void OopMapForCacheEntry::fill_stackmap_epilog() { aoqi@0: // Do nothing aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void OopMapForCacheEntry::fill_init_vars(GrowableArray *init_vars) { aoqi@0: // Do nothing aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void OopMapForCacheEntry::fill_stackmap_for_opcodes(BytecodeStream *bcs, aoqi@0: CellTypeState* vars, aoqi@0: CellTypeState* stack, aoqi@0: int stack_top) { aoqi@0: // Only interested in one specific bci aoqi@0: if (bcs->bci() == _bci) { aoqi@0: _entry->set_mask(vars, stack, stack_top); aoqi@0: _stack_top = stack_top; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int OopMapForCacheEntry::size() { aoqi@0: assert(_stack_top != -1, "compute_map must be called first"); aoqi@0: return ((method()->is_static()) ? 0 : 1) + method()->max_locals() + _stack_top; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of InterpreterOopMap and OopMapCacheEntry aoqi@0: aoqi@0: class VerifyClosure : public OffsetClosure { aoqi@0: private: aoqi@0: OopMapCacheEntry* _entry; aoqi@0: bool _failed; aoqi@0: aoqi@0: public: aoqi@0: VerifyClosure(OopMapCacheEntry* entry) { _entry = entry; _failed = false; } aoqi@0: void offset_do(int offset) { if (!_entry->is_oop(offset)) _failed = true; } aoqi@0: bool failed() const { return _failed; } aoqi@0: }; aoqi@0: aoqi@0: InterpreterOopMap::InterpreterOopMap() { aoqi@0: initialize(); aoqi@0: #ifdef ASSERT aoqi@0: _resource_allocate_bit_mask = true; aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: InterpreterOopMap::~InterpreterOopMap() { aoqi@0: // The expection is that the bit mask was allocated aoqi@0: // last in this resource area. That would make the free of the aoqi@0: // bit_mask effective (see how FREE_RESOURCE_ARRAY does a free). aoqi@0: // If it was not allocated last, there is not a correctness problem aoqi@0: // but the space for the bit_mask is not freed. aoqi@0: assert(_resource_allocate_bit_mask, "Trying to free C heap space"); aoqi@0: if (mask_size() > small_mask_limit) { aoqi@0: FREE_RESOURCE_ARRAY(uintptr_t, _bit_mask[0], mask_word_size()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool InterpreterOopMap::is_empty() { aoqi@0: bool result = _method == NULL; aoqi@0: assert(_method != NULL || (_bci == 0 && aoqi@0: (_mask_size == 0 || _mask_size == USHRT_MAX) && aoqi@0: _bit_mask[0] == 0), "Should be completely empty"); aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: void InterpreterOopMap::initialize() { aoqi@0: _method = NULL; aoqi@0: _mask_size = USHRT_MAX; // This value should cause a failure quickly aoqi@0: _bci = 0; aoqi@0: _expression_stack_size = 0; aoqi@0: for (int i = 0; i < N; i++) _bit_mask[i] = 0; aoqi@0: } aoqi@0: aoqi@0: void InterpreterOopMap::iterate_oop(OffsetClosure* oop_closure) { aoqi@0: int n = number_of_entries(); aoqi@0: int word_index = 0; aoqi@0: uintptr_t value = 0; aoqi@0: uintptr_t mask = 0; aoqi@0: // iterate over entries aoqi@0: for (int i = 0; i < n; i++, mask <<= bits_per_entry) { aoqi@0: // get current word aoqi@0: if (mask == 0) { aoqi@0: value = bit_mask()[word_index++]; aoqi@0: mask = 1; aoqi@0: } aoqi@0: // test for oop aoqi@0: if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifdef ENABLE_ZAP_DEAD_LOCALS aoqi@0: aoqi@0: void InterpreterOopMap::iterate_all(OffsetClosure* oop_closure, OffsetClosure* value_closure, OffsetClosure* dead_closure) { aoqi@0: int n = number_of_entries(); aoqi@0: int word_index = 0; aoqi@0: uintptr_t value = 0; aoqi@0: uintptr_t mask = 0; aoqi@0: // iterate over entries aoqi@0: for (int i = 0; i < n; i++, mask <<= bits_per_entry) { aoqi@0: // get current word aoqi@0: if (mask == 0) { aoqi@0: value = bit_mask()[word_index++]; aoqi@0: mask = 1; aoqi@0: } aoqi@0: // test for dead values & oops, and for live values aoqi@0: if ((value & (mask << dead_bit_number)) != 0) dead_closure->offset_do(i); // call this for all dead values or oops aoqi@0: else if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i); // call this for all live oops aoqi@0: else value_closure->offset_do(i); // call this for all live values aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: void InterpreterOopMap::print() { aoqi@0: int n = number_of_entries(); aoqi@0: tty->print("oop map for "); aoqi@0: method()->print_value(); aoqi@0: tty->print(" @ %d = [%d] { ", bci(), n); aoqi@0: for (int i = 0; i < n; i++) { aoqi@0: #ifdef ENABLE_ZAP_DEAD_LOCALS aoqi@0: if (is_dead(i)) tty->print("%d+ ", i); aoqi@0: else aoqi@0: #endif aoqi@0: if (is_oop(i)) tty->print("%d ", i); aoqi@0: } aoqi@0: tty->print_cr("}"); aoqi@0: } aoqi@0: aoqi@0: class MaskFillerForNative: public NativeSignatureIterator { aoqi@0: private: aoqi@0: uintptr_t * _mask; // the bit mask to be filled aoqi@0: int _size; // the mask size in bits aoqi@0: aoqi@0: void set_one(int i) { aoqi@0: i *= InterpreterOopMap::bits_per_entry; aoqi@0: assert(0 <= i && i < _size, "offset out of bounds"); aoqi@0: _mask[i / BitsPerWord] |= (((uintptr_t) 1 << InterpreterOopMap::oop_bit_number) << (i % BitsPerWord)); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: void pass_int() { /* ignore */ } aoqi@0: void pass_long() { /* ignore */ } aoqi@0: void pass_float() { /* ignore */ } aoqi@0: void pass_double() { /* ignore */ } aoqi@0: void pass_object() { set_one(offset()); } aoqi@0: aoqi@0: MaskFillerForNative(methodHandle method, uintptr_t* mask, int size) : NativeSignatureIterator(method) { aoqi@0: _mask = mask; aoqi@0: _size = size; aoqi@0: // initialize with 0 aoqi@0: int i = (size + BitsPerWord - 1) / BitsPerWord; aoqi@0: while (i-- > 0) _mask[i] = 0; aoqi@0: } aoqi@0: aoqi@0: void generate() { aoqi@0: NativeSignatureIterator::iterate(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: bool OopMapCacheEntry::verify_mask(CellTypeState* vars, CellTypeState* stack, int max_locals, int stack_top) { aoqi@0: // Check mask includes map aoqi@0: VerifyClosure blk(this); aoqi@0: iterate_oop(&blk); aoqi@0: if (blk.failed()) return false; aoqi@0: aoqi@0: // Check if map is generated correctly aoqi@0: // (Use ?: operator to make sure all 'true' & 'false' are represented exactly the same so we can use == afterwards) aoqi@0: if (TraceOopMapGeneration && Verbose) tty->print("Locals (%d): ", max_locals); aoqi@0: aoqi@0: for(int i = 0; i < max_locals; i++) { aoqi@0: bool v1 = is_oop(i) ? true : false; aoqi@0: bool v2 = vars[i].is_reference() ? true : false; aoqi@0: assert(v1 == v2, "locals oop mask generation error"); aoqi@0: if (TraceOopMapGeneration && Verbose) tty->print("%d", v1 ? 1 : 0); aoqi@0: #ifdef ENABLE_ZAP_DEAD_LOCALS aoqi@0: bool v3 = is_dead(i) ? true : false; aoqi@0: bool v4 = !vars[i].is_live() ? true : false; aoqi@0: assert(v3 == v4, "locals live mask generation error"); aoqi@0: assert(!(v1 && v3), "dead value marked as oop"); aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: if (TraceOopMapGeneration && Verbose) { tty->cr(); tty->print("Stack (%d): ", stack_top); } aoqi@0: for(int j = 0; j < stack_top; j++) { aoqi@0: bool v1 = is_oop(max_locals + j) ? true : false; aoqi@0: bool v2 = stack[j].is_reference() ? true : false; aoqi@0: assert(v1 == v2, "stack oop mask generation error"); aoqi@0: if (TraceOopMapGeneration && Verbose) tty->print("%d", v1 ? 1 : 0); aoqi@0: #ifdef ENABLE_ZAP_DEAD_LOCALS aoqi@0: bool v3 = is_dead(max_locals + j) ? true : false; aoqi@0: bool v4 = !stack[j].is_live() ? true : false; aoqi@0: assert(v3 == v4, "stack live mask generation error"); aoqi@0: assert(!(v1 && v3), "dead value marked as oop"); aoqi@0: #endif aoqi@0: } aoqi@0: if (TraceOopMapGeneration && Verbose) tty->cr(); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void OopMapCacheEntry::allocate_bit_mask() { aoqi@0: if (mask_size() > small_mask_limit) { aoqi@0: assert(_bit_mask[0] == 0, "bit mask should be new or just flushed"); aoqi@0: _bit_mask[0] = (intptr_t) aoqi@0: NEW_C_HEAP_ARRAY(uintptr_t, mask_word_size(), mtClass); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void OopMapCacheEntry::deallocate_bit_mask() { aoqi@0: if (mask_size() > small_mask_limit && _bit_mask[0] != 0) { aoqi@0: assert(!Thread::current()->resource_area()->contains((void*)_bit_mask[0]), aoqi@0: "This bit mask should not be in the resource area"); aoqi@0: FREE_C_HEAP_ARRAY(uintptr_t, _bit_mask[0], mtClass); aoqi@0: debug_only(_bit_mask[0] = 0;) aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void OopMapCacheEntry::fill_for_native(methodHandle mh) { aoqi@0: assert(mh->is_native(), "method must be native method"); aoqi@0: set_mask_size(mh->size_of_parameters() * bits_per_entry); aoqi@0: allocate_bit_mask(); aoqi@0: // fill mask for parameters aoqi@0: MaskFillerForNative mf(mh, bit_mask(), mask_size()); aoqi@0: mf.generate(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void OopMapCacheEntry::fill(methodHandle method, int bci) { aoqi@0: HandleMark hm; aoqi@0: // Flush entry to deallocate an existing entry aoqi@0: flush(); aoqi@0: set_method(method()); aoqi@0: set_bci(bci); aoqi@0: if (method->is_native()) { aoqi@0: // Native method activations have oops only among the parameters and one aoqi@0: // extra oop following the parameters (the mirror for static native methods). aoqi@0: fill_for_native(method); aoqi@0: } else { aoqi@0: EXCEPTION_MARK; aoqi@0: OopMapForCacheEntry gen(method, bci, this); aoqi@0: gen.compute_map(CATCH); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void OopMapCacheEntry::set_mask(CellTypeState *vars, CellTypeState *stack, int stack_top) { aoqi@0: // compute bit mask size aoqi@0: int max_locals = method()->max_locals(); aoqi@0: int n_entries = max_locals + stack_top; aoqi@0: set_mask_size(n_entries * bits_per_entry); aoqi@0: allocate_bit_mask(); aoqi@0: set_expression_stack_size(stack_top); aoqi@0: aoqi@0: // compute bits aoqi@0: int word_index = 0; aoqi@0: uintptr_t value = 0; aoqi@0: uintptr_t mask = 1; aoqi@0: aoqi@0: CellTypeState* cell = vars; aoqi@0: for (int entry_index = 0; entry_index < n_entries; entry_index++, mask <<= bits_per_entry, cell++) { aoqi@0: // store last word aoqi@0: if (mask == 0) { aoqi@0: bit_mask()[word_index++] = value; aoqi@0: value = 0; aoqi@0: mask = 1; aoqi@0: } aoqi@0: aoqi@0: // switch to stack when done with locals aoqi@0: if (entry_index == max_locals) { aoqi@0: cell = stack; aoqi@0: } aoqi@0: aoqi@0: // set oop bit aoqi@0: if ( cell->is_reference()) { aoqi@0: value |= (mask << oop_bit_number ); aoqi@0: } aoqi@0: aoqi@0: #ifdef ENABLE_ZAP_DEAD_LOCALS aoqi@0: // set dead bit aoqi@0: if (!cell->is_live()) { aoqi@0: value |= (mask << dead_bit_number); aoqi@0: assert(!cell->is_reference(), "dead value marked as oop"); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: // make sure last word is stored aoqi@0: bit_mask()[word_index] = value; aoqi@0: aoqi@0: // verify bit mask aoqi@0: assert(verify_mask(vars, stack, max_locals, stack_top), "mask could not be verified"); aoqi@0: aoqi@0: aoqi@0: } aoqi@0: aoqi@0: void OopMapCacheEntry::flush() { aoqi@0: deallocate_bit_mask(); aoqi@0: initialize(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of OopMapCache aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: static long _total_memory_usage = 0; aoqi@0: aoqi@0: long OopMapCache::memory_usage() { aoqi@0: return _total_memory_usage; aoqi@0: } aoqi@0: aoqi@0: #endif aoqi@0: aoqi@0: void InterpreterOopMap::resource_copy(OopMapCacheEntry* from) { aoqi@0: assert(_resource_allocate_bit_mask, aoqi@0: "Should not resource allocate the _bit_mask"); aoqi@0: aoqi@0: set_method(from->method()); aoqi@0: set_bci(from->bci()); aoqi@0: set_mask_size(from->mask_size()); aoqi@0: set_expression_stack_size(from->expression_stack_size()); aoqi@0: aoqi@0: // Is the bit mask contained in the entry? aoqi@0: if (from->mask_size() <= small_mask_limit) { aoqi@0: memcpy((void *)_bit_mask, (void *)from->_bit_mask, aoqi@0: mask_word_size() * BytesPerWord); aoqi@0: } else { aoqi@0: // The expectation is that this InterpreterOopMap is a recently created aoqi@0: // and empty. It is used to get a copy of a cached entry. aoqi@0: // If the bit mask has a value, it should be in the aoqi@0: // resource area. aoqi@0: assert(_bit_mask[0] == 0 || aoqi@0: Thread::current()->resource_area()->contains((void*)_bit_mask[0]), aoqi@0: "The bit mask should have been allocated from a resource area"); aoqi@0: // Allocate the bit_mask from a Resource area for performance. Allocating aoqi@0: // from the C heap as is done for OopMapCache has a significant aoqi@0: // performance impact. aoqi@0: _bit_mask[0] = (uintptr_t) NEW_RESOURCE_ARRAY(uintptr_t, mask_word_size()); aoqi@0: assert(_bit_mask[0] != 0, "bit mask was not allocated"); aoqi@0: memcpy((void*) _bit_mask[0], (void*) from->_bit_mask[0], aoqi@0: mask_word_size() * BytesPerWord); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: inline unsigned int OopMapCache::hash_value_for(methodHandle method, int bci) { aoqi@0: // We use method->code_size() rather than method->identity_hash() below since aoqi@0: // the mark may not be present if a pointer to the method is already reversed. aoqi@0: return ((unsigned int) bci) aoqi@0: ^ ((unsigned int) method->max_locals() << 2) aoqi@0: ^ ((unsigned int) method->code_size() << 4) aoqi@0: ^ ((unsigned int) method->size_of_parameters() << 6); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: OopMapCache::OopMapCache() : aoqi@0: _mut(Mutex::leaf, "An OopMapCache lock", true) aoqi@0: { aoqi@0: _array = NEW_C_HEAP_ARRAY(OopMapCacheEntry, _size, mtClass); aoqi@0: // Cannot call flush for initialization, since flush aoqi@0: // will check if memory should be deallocated aoqi@0: for(int i = 0; i < _size; i++) _array[i].initialize(); aoqi@0: NOT_PRODUCT(_total_memory_usage += sizeof(OopMapCache) + (sizeof(OopMapCacheEntry) * _size);) aoqi@0: } aoqi@0: aoqi@0: aoqi@0: OopMapCache::~OopMapCache() { aoqi@0: assert(_array != NULL, "sanity check"); aoqi@0: // Deallocate oop maps that are allocated out-of-line aoqi@0: flush(); aoqi@0: // Deallocate array aoqi@0: NOT_PRODUCT(_total_memory_usage -= sizeof(OopMapCache) + (sizeof(OopMapCacheEntry) * _size);) aoqi@0: FREE_C_HEAP_ARRAY(OopMapCacheEntry, _array, mtClass); aoqi@0: } aoqi@0: aoqi@0: OopMapCacheEntry* OopMapCache::entry_at(int i) const { aoqi@0: return &_array[i % _size]; aoqi@0: } aoqi@0: aoqi@0: void OopMapCache::flush() { aoqi@0: for (int i = 0; i < _size; i++) _array[i].flush(); aoqi@0: } aoqi@0: aoqi@0: void OopMapCache::flush_obsolete_entries() { aoqi@0: for (int i = 0; i < _size; i++) aoqi@0: if (!_array[i].is_empty() && _array[i].method()->is_old()) { aoqi@0: // Cache entry is occupied by an old redefined method and we don't want aoqi@0: // to pin it down so flush the entry. aoqi@0: RC_TRACE(0x08000000, ("flush: %s(%s): cached entry @%d", aoqi@0: _array[i].method()->name()->as_C_string(), aoqi@0: _array[i].method()->signature()->as_C_string(), i)); aoqi@0: aoqi@0: _array[i].flush(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void OopMapCache::lookup(methodHandle method, aoqi@0: int bci, aoqi@0: InterpreterOopMap* entry_for) { aoqi@0: MutexLocker x(&_mut); aoqi@0: aoqi@0: OopMapCacheEntry* entry = NULL; aoqi@0: int probe = hash_value_for(method, bci); aoqi@0: aoqi@0: // Search hashtable for match aoqi@0: int i; aoqi@0: for(i = 0; i < _probe_depth; i++) { aoqi@0: entry = entry_at(probe + i); aoqi@0: if (entry->match(method, bci)) { aoqi@0: entry_for->resource_copy(entry); aoqi@0: assert(!entry_for->is_empty(), "A non-empty oop map should be returned"); aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (TraceOopMapGeneration) { aoqi@0: static int count = 0; aoqi@0: ResourceMark rm; aoqi@0: tty->print("%d - Computing oopmap at bci %d for ", ++count, bci); aoqi@0: method->print_value(); tty->cr(); aoqi@0: } aoqi@0: aoqi@0: // Entry is not in hashtable. aoqi@0: // Compute entry and return it aoqi@0: aoqi@0: if (method->should_not_be_cached()) { aoqi@0: // It is either not safe or not a good idea to cache this Method* aoqi@0: // at this time. We give the caller of lookup() a copy of the aoqi@0: // interesting info via parameter entry_for, but we don't add it to aoqi@0: // the cache. See the gory details in Method*.cpp. aoqi@0: compute_one_oop_map(method, bci, entry_for); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // First search for an empty slot aoqi@0: for(i = 0; i < _probe_depth; i++) { aoqi@0: entry = entry_at(probe + i); aoqi@0: if (entry->is_empty()) { aoqi@0: entry->fill(method, bci); aoqi@0: entry_for->resource_copy(entry); aoqi@0: assert(!entry_for->is_empty(), "A non-empty oop map should be returned"); aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (TraceOopMapGeneration) { aoqi@0: ResourceMark rm; aoqi@0: tty->print_cr("*** collision in oopmap cache - flushing item ***"); aoqi@0: } aoqi@0: aoqi@0: // No empty slot (uncommon case). Use (some approximation of a) LRU algorithm aoqi@0: //entry_at(probe + _probe_depth - 1)->flush(); aoqi@0: //for(i = _probe_depth - 1; i > 0; i--) { aoqi@0: // // Coping entry[i] = entry[i-1]; aoqi@0: // OopMapCacheEntry *to = entry_at(probe + i); aoqi@0: // OopMapCacheEntry *from = entry_at(probe + i - 1); aoqi@0: // to->copy(from); aoqi@0: // } aoqi@0: aoqi@0: assert(method->is_method(), "gaga"); aoqi@0: aoqi@0: entry = entry_at(probe + 0); aoqi@0: entry->fill(method, bci); aoqi@0: aoqi@0: // Copy the newly cached entry to input parameter aoqi@0: entry_for->resource_copy(entry); aoqi@0: aoqi@0: if (TraceOopMapGeneration) { aoqi@0: ResourceMark rm; aoqi@0: tty->print("Done with "); aoqi@0: method->print_value(); tty->cr(); aoqi@0: } aoqi@0: assert(!entry_for->is_empty(), "A non-empty oop map should be returned"); aoqi@0: aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: void OopMapCache::compute_one_oop_map(methodHandle method, int bci, InterpreterOopMap* entry) { aoqi@0: // Due to the invariants above it's tricky to allocate a temporary OopMapCacheEntry on the stack aoqi@0: OopMapCacheEntry* tmp = NEW_C_HEAP_ARRAY(OopMapCacheEntry, 1, mtClass); aoqi@0: tmp->initialize(); aoqi@0: tmp->fill(method, bci); aoqi@0: entry->resource_copy(tmp); aoqi@0: FREE_C_HEAP_ARRAY(OopMapCacheEntry, tmp, mtInternal); aoqi@0: }