Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/interpreter/oopMapCache.cpp
32285 views
/*1* Copyright (c) 1997, 2018, 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 "memory/allocation.inline.hpp"27#include "memory/resourceArea.hpp"28#include "oops/oop.inline.hpp"29#include "prims/jvmtiRedefineClassesTrace.hpp"30#include "runtime/handles.inline.hpp"31#include "runtime/signature.hpp"3233PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC3435class OopMapCacheEntry: private InterpreterOopMap {36friend class InterpreterOopMap;37friend class OopMapForCacheEntry;38friend class OopMapCache;39friend class VerifyClosure;4041protected:42// Initialization43void fill(methodHandle method, int bci);44// fills the bit mask for native calls45void fill_for_native(methodHandle method);46void set_mask(CellTypeState* vars, CellTypeState* stack, int stack_top);4748// Deallocate bit masks and initialize fields49void flush();5051private:52void allocate_bit_mask(); // allocates the bit mask on C heap f necessary53void deallocate_bit_mask(); // allocates the bit mask on C heap f necessary54bool verify_mask(CellTypeState *vars, CellTypeState *stack, int max_locals, int stack_top);5556public:57OopMapCacheEntry() : InterpreterOopMap() {58#ifdef ASSERT59_resource_allocate_bit_mask = false;60#endif61}62};636465// Implementation of OopMapForCacheEntry66// (subclass of GenerateOopMap, initializes an OopMapCacheEntry for a given method and bci)6768class OopMapForCacheEntry: public GenerateOopMap {69OopMapCacheEntry *_entry;70int _bci;71int _stack_top;7273virtual bool report_results() const { return false; }74virtual bool possible_gc_point (BytecodeStream *bcs);75virtual void fill_stackmap_prolog (int nof_gc_points);76virtual void fill_stackmap_epilog ();77virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs,78CellTypeState* vars,79CellTypeState* stack,80int stack_top);81virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars);8283public:84OopMapForCacheEntry(methodHandle method, int bci, OopMapCacheEntry *entry);8586// Computes stack map for (method,bci) and initialize entry87void compute_map(TRAPS);88int size();89};909192OopMapForCacheEntry::OopMapForCacheEntry(methodHandle method, int bci, OopMapCacheEntry* entry) : GenerateOopMap(method) {93_bci = bci;94_entry = entry;95_stack_top = -1;96}979899void OopMapForCacheEntry::compute_map(TRAPS) {100assert(!method()->is_native(), "cannot compute oop map for native methods");101// First check if it is a method where the stackmap is always empty102if (method()->code_size() == 0 || method()->max_locals() + method()->max_stack() == 0) {103_entry->set_mask_size(0);104} else {105ResourceMark rm;106GenerateOopMap::compute_map(CATCH);107result_for_basicblock(_bci);108}109}110111112bool OopMapForCacheEntry::possible_gc_point(BytecodeStream *bcs) {113return false; // We are not reporting any result. We call result_for_basicblock directly114}115116117void OopMapForCacheEntry::fill_stackmap_prolog(int nof_gc_points) {118// Do nothing119}120121122void OopMapForCacheEntry::fill_stackmap_epilog() {123// Do nothing124}125126127void OopMapForCacheEntry::fill_init_vars(GrowableArray<intptr_t> *init_vars) {128// Do nothing129}130131132void OopMapForCacheEntry::fill_stackmap_for_opcodes(BytecodeStream *bcs,133CellTypeState* vars,134CellTypeState* stack,135int stack_top) {136// Only interested in one specific bci137if (bcs->bci() == _bci) {138_entry->set_mask(vars, stack, stack_top);139_stack_top = stack_top;140}141}142143144int OopMapForCacheEntry::size() {145assert(_stack_top != -1, "compute_map must be called first");146return ((method()->is_static()) ? 0 : 1) + method()->max_locals() + _stack_top;147}148149150// Implementation of InterpreterOopMap and OopMapCacheEntry151152class VerifyClosure : public OffsetClosure {153private:154OopMapCacheEntry* _entry;155bool _failed;156157public:158VerifyClosure(OopMapCacheEntry* entry) { _entry = entry; _failed = false; }159void offset_do(int offset) { if (!_entry->is_oop(offset)) _failed = true; }160bool failed() const { return _failed; }161};162163InterpreterOopMap::InterpreterOopMap() {164initialize();165#ifdef ASSERT166_resource_allocate_bit_mask = true;167#endif168}169170InterpreterOopMap::~InterpreterOopMap() {171// The expection is that the bit mask was allocated172// last in this resource area. That would make the free of the173// bit_mask effective (see how FREE_RESOURCE_ARRAY does a free).174// If it was not allocated last, there is not a correctness problem175// but the space for the bit_mask is not freed.176assert(_resource_allocate_bit_mask, "Trying to free C heap space");177if (mask_size() > small_mask_limit) {178FREE_RESOURCE_ARRAY(uintptr_t, _bit_mask[0], mask_word_size());179}180}181182bool InterpreterOopMap::is_empty() const {183bool result = _method == NULL;184assert(_method != NULL || (_bci == 0 &&185(_mask_size == 0 || _mask_size == USHRT_MAX) &&186_bit_mask[0] == 0), "Should be completely empty");187return result;188}189190void InterpreterOopMap::initialize() {191_method = NULL;192_mask_size = USHRT_MAX; // This value should cause a failure quickly193_bci = 0;194_expression_stack_size = 0;195for (int i = 0; i < N; i++) _bit_mask[i] = 0;196}197198void InterpreterOopMap::iterate_oop(OffsetClosure* oop_closure) const {199int n = number_of_entries();200int word_index = 0;201uintptr_t value = 0;202uintptr_t mask = 0;203// iterate over entries204for (int i = 0; i < n; i++, mask <<= bits_per_entry) {205// get current word206if (mask == 0) {207value = bit_mask()[word_index++];208mask = 1;209}210// test for oop211if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i);212}213}214215216#ifdef ENABLE_ZAP_DEAD_LOCALS217218void InterpreterOopMap::iterate_all(OffsetClosure* oop_closure, OffsetClosure* value_closure, OffsetClosure* dead_closure) {219int n = number_of_entries();220int word_index = 0;221uintptr_t value = 0;222uintptr_t mask = 0;223// iterate over entries224for (int i = 0; i < n; i++, mask <<= bits_per_entry) {225// get current word226if (mask == 0) {227value = bit_mask()[word_index++];228mask = 1;229}230// test for dead values & oops, and for live values231if ((value & (mask << dead_bit_number)) != 0) dead_closure->offset_do(i); // call this for all dead values or oops232else if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i); // call this for all live oops233else value_closure->offset_do(i); // call this for all live values234}235}236237#endif238239240void InterpreterOopMap::print() const {241int n = number_of_entries();242tty->print("oop map for ");243method()->print_value();244tty->print(" @ %d = [%d] { ", bci(), n);245for (int i = 0; i < n; i++) {246if (is_dead(i)) tty->print("%d+ ", i);247else248if (is_oop(i)) tty->print("%d ", i);249}250tty->print_cr("}");251}252253class MaskFillerForNative: public NativeSignatureIterator {254private:255uintptr_t * _mask; // the bit mask to be filled256int _size; // the mask size in bits257258void set_one(int i) {259i *= InterpreterOopMap::bits_per_entry;260assert(0 <= i && i < _size, "offset out of bounds");261_mask[i / BitsPerWord] |= (((uintptr_t) 1 << InterpreterOopMap::oop_bit_number) << (i % BitsPerWord));262}263264public:265void pass_int() { /* ignore */ }266void pass_long() { /* ignore */ }267void pass_float() { /* ignore */ }268void pass_double() { /* ignore */ }269void pass_object() { set_one(offset()); }270271MaskFillerForNative(methodHandle method, uintptr_t* mask, int size) : NativeSignatureIterator(method) {272_mask = mask;273_size = size;274// initialize with 0275int i = (size + BitsPerWord - 1) / BitsPerWord;276while (i-- > 0) _mask[i] = 0;277}278279void generate() {280NativeSignatureIterator::iterate();281}282};283284bool OopMapCacheEntry::verify_mask(CellTypeState* vars, CellTypeState* stack, int max_locals, int stack_top) {285// Check mask includes map286VerifyClosure blk(this);287iterate_oop(&blk);288if (blk.failed()) return false;289290// Check if map is generated correctly291// (Use ?: operator to make sure all 'true' & 'false' are represented exactly the same so we can use == afterwards)292if (TraceOopMapGeneration && Verbose) tty->print("Locals (%d): ", max_locals);293294for(int i = 0; i < max_locals; i++) {295bool v1 = is_oop(i) ? true : false;296bool v2 = vars[i].is_reference() ? true : false;297assert(v1 == v2, "locals oop mask generation error");298if (TraceOopMapGeneration && Verbose) tty->print("%d", v1 ? 1 : 0);299#ifdef ENABLE_ZAP_DEAD_LOCALS300bool v3 = is_dead(i) ? true : false;301bool v4 = !vars[i].is_live() ? true : false;302assert(v3 == v4, "locals live mask generation error");303assert(!(v1 && v3), "dead value marked as oop");304#endif305}306307if (TraceOopMapGeneration && Verbose) { tty->cr(); tty->print("Stack (%d): ", stack_top); }308for(int j = 0; j < stack_top; j++) {309bool v1 = is_oop(max_locals + j) ? true : false;310bool v2 = stack[j].is_reference() ? true : false;311assert(v1 == v2, "stack oop mask generation error");312if (TraceOopMapGeneration && Verbose) tty->print("%d", v1 ? 1 : 0);313#ifdef ENABLE_ZAP_DEAD_LOCALS314bool v3 = is_dead(max_locals + j) ? true : false;315bool v4 = !stack[j].is_live() ? true : false;316assert(v3 == v4, "stack live mask generation error");317assert(!(v1 && v3), "dead value marked as oop");318#endif319}320if (TraceOopMapGeneration && Verbose) tty->cr();321return true;322}323324void OopMapCacheEntry::allocate_bit_mask() {325if (mask_size() > small_mask_limit) {326assert(_bit_mask[0] == 0, "bit mask should be new or just flushed");327_bit_mask[0] = (intptr_t)328NEW_C_HEAP_ARRAY(uintptr_t, mask_word_size(), mtClass);329}330}331332void OopMapCacheEntry::deallocate_bit_mask() {333if (mask_size() > small_mask_limit && _bit_mask[0] != 0) {334assert(!Thread::current()->resource_area()->contains((void*)_bit_mask[0]),335"This bit mask should not be in the resource area");336FREE_C_HEAP_ARRAY(uintptr_t, _bit_mask[0], mtClass);337debug_only(_bit_mask[0] = 0;)338}339}340341342void OopMapCacheEntry::fill_for_native(methodHandle mh) {343assert(mh->is_native(), "method must be native method");344set_mask_size(mh->size_of_parameters() * bits_per_entry);345allocate_bit_mask();346// fill mask for parameters347MaskFillerForNative mf(mh, bit_mask(), mask_size());348mf.generate();349}350351352void OopMapCacheEntry::fill(methodHandle method, int bci) {353HandleMark hm;354// Flush entry to deallocate an existing entry355flush();356set_method(method());357set_bci(bci);358if (method->is_native()) {359// Native method activations have oops only among the parameters and one360// extra oop following the parameters (the mirror for static native methods).361fill_for_native(method);362} else {363EXCEPTION_MARK;364OopMapForCacheEntry gen(method, bci, this);365gen.compute_map(CATCH);366}367}368369370void OopMapCacheEntry::set_mask(CellTypeState *vars, CellTypeState *stack, int stack_top) {371// compute bit mask size372int max_locals = method()->max_locals();373int n_entries = max_locals + stack_top;374set_mask_size(n_entries * bits_per_entry);375allocate_bit_mask();376set_expression_stack_size(stack_top);377378// compute bits379int word_index = 0;380uintptr_t value = 0;381uintptr_t mask = 1;382383CellTypeState* cell = vars;384for (int entry_index = 0; entry_index < n_entries; entry_index++, mask <<= bits_per_entry, cell++) {385// store last word386if (mask == 0) {387bit_mask()[word_index++] = value;388value = 0;389mask = 1;390}391392// switch to stack when done with locals393if (entry_index == max_locals) {394cell = stack;395}396397// set oop bit398if ( cell->is_reference()) {399value |= (mask << oop_bit_number );400}401402// set dead bit403if (!cell->is_live()) {404value |= (mask << dead_bit_number);405assert(!cell->is_reference(), "dead value marked as oop");406}407}408409// make sure last word is stored410bit_mask()[word_index] = value;411412// verify bit mask413assert(verify_mask(vars, stack, max_locals, stack_top), "mask could not be verified");414415416}417418void OopMapCacheEntry::flush() {419deallocate_bit_mask();420initialize();421}422423424// Implementation of OopMapCache425426#ifndef PRODUCT427428static size_t _total_memory_usage = 0;429430size_t OopMapCache::memory_usage() {431return _total_memory_usage;432}433434#endif435436void InterpreterOopMap::resource_copy(OopMapCacheEntry* from) {437assert(_resource_allocate_bit_mask,438"Should not resource allocate the _bit_mask");439440set_method(from->method());441set_bci(from->bci());442set_mask_size(from->mask_size());443set_expression_stack_size(from->expression_stack_size());444445// Is the bit mask contained in the entry?446if (from->mask_size() <= small_mask_limit) {447memcpy((void *)_bit_mask, (void *)from->_bit_mask,448mask_word_size() * BytesPerWord);449} else {450// The expectation is that this InterpreterOopMap is a recently created451// and empty. It is used to get a copy of a cached entry.452// If the bit mask has a value, it should be in the453// resource area.454assert(_bit_mask[0] == 0 ||455Thread::current()->resource_area()->contains((void*)_bit_mask[0]),456"The bit mask should have been allocated from a resource area");457// Allocate the bit_mask from a Resource area for performance. Allocating458// from the C heap as is done for OopMapCache has a significant459// performance impact.460_bit_mask[0] = (uintptr_t) NEW_RESOURCE_ARRAY(uintptr_t, mask_word_size());461assert(_bit_mask[0] != 0, "bit mask was not allocated");462memcpy((void*) _bit_mask[0], (void*) from->_bit_mask[0],463mask_word_size() * BytesPerWord);464}465}466467inline unsigned int OopMapCache::hash_value_for(methodHandle method, int bci) const {468// We use method->code_size() rather than method->identity_hash() below since469// the mark may not be present if a pointer to the method is already reversed.470return ((unsigned int) bci)471^ ((unsigned int) method->max_locals() << 2)472^ ((unsigned int) method->code_size() << 4)473^ ((unsigned int) method->size_of_parameters() << 6);474}475476477OopMapCache::OopMapCache() :478_mut(Mutex::leaf, "An OopMapCache lock", true)479{480_array = NEW_C_HEAP_ARRAY(OopMapCacheEntry, _size, mtClass);481// Cannot call flush for initialization, since flush482// will check if memory should be deallocated483for(int i = 0; i < _size; i++) _array[i].initialize();484NOT_PRODUCT(_total_memory_usage += sizeof(OopMapCache) + (sizeof(OopMapCacheEntry) * _size);)485}486487488OopMapCache::~OopMapCache() {489assert(_array != NULL, "sanity check");490// Deallocate oop maps that are allocated out-of-line491flush();492// Deallocate array493NOT_PRODUCT(_total_memory_usage -= sizeof(OopMapCache) + (sizeof(OopMapCacheEntry) * _size);)494FREE_C_HEAP_ARRAY(OopMapCacheEntry, _array, mtClass);495}496497OopMapCacheEntry* OopMapCache::entry_at(int i) const {498return &_array[i % _size];499}500501void OopMapCache::flush() {502for (int i = 0; i < _size; i++) _array[i].flush();503}504505void OopMapCache::flush_obsolete_entries() {506for (int i = 0; i < _size; i++)507if (!_array[i].is_empty() && _array[i].method()->is_old()) {508// Cache entry is occupied by an old redefined method and we don't want509// to pin it down so flush the entry.510RC_TRACE(0x08000000, ("flush: %s(%s): cached entry @%d",511_array[i].method()->name()->as_C_string(),512_array[i].method()->signature()->as_C_string(), i));513514_array[i].flush();515}516}517518void OopMapCache::lookup(methodHandle method,519int bci,520InterpreterOopMap* entry_for) const {521MutexLocker x(&_mut);522523OopMapCacheEntry* entry = NULL;524int probe = hash_value_for(method, bci);525526// Search hashtable for match527int i;528for(i = 0; i < _probe_depth; i++) {529entry = entry_at(probe + i);530if (entry->match(method, bci)) {531entry_for->resource_copy(entry);532assert(!entry_for->is_empty(), "A non-empty oop map should be returned");533return;534}535}536537if (TraceOopMapGeneration) {538static int count = 0;539ResourceMark rm;540tty->print("%d - Computing oopmap at bci %d for ", ++count, bci);541method->print_value(); tty->cr();542}543544// Entry is not in hashtable.545// Compute entry and return it546547if (method->should_not_be_cached()) {548// It is either not safe or not a good idea to cache this Method*549// at this time. We give the caller of lookup() a copy of the550// interesting info via parameter entry_for, but we don't add it to551// the cache. See the gory details in Method*.cpp.552compute_one_oop_map(method, bci, entry_for);553return;554}555556// First search for an empty slot557for(i = 0; i < _probe_depth; i++) {558entry = entry_at(probe + i);559if (entry->is_empty()) {560entry->fill(method, bci);561entry_for->resource_copy(entry);562assert(!entry_for->is_empty(), "A non-empty oop map should be returned");563return;564}565}566567if (TraceOopMapGeneration) {568ResourceMark rm;569tty->print_cr("*** collision in oopmap cache - flushing item ***");570}571572// No empty slot (uncommon case). Use (some approximation of a) LRU algorithm573//entry_at(probe + _probe_depth - 1)->flush();574//for(i = _probe_depth - 1; i > 0; i--) {575// // Coping entry[i] = entry[i-1];576// OopMapCacheEntry *to = entry_at(probe + i);577// OopMapCacheEntry *from = entry_at(probe + i - 1);578// to->copy(from);579// }580581assert(method->is_method(), "gaga");582583entry = entry_at(probe + 0);584entry->fill(method, bci);585586// Copy the newly cached entry to input parameter587entry_for->resource_copy(entry);588589if (TraceOopMapGeneration) {590ResourceMark rm;591tty->print("Done with ");592method->print_value(); tty->cr();593}594assert(!entry_for->is_empty(), "A non-empty oop map should be returned");595596return;597}598599void OopMapCache::compute_one_oop_map(methodHandle method, int bci, InterpreterOopMap* entry) {600// Due to the invariants above it's tricky to allocate a temporary OopMapCacheEntry on the stack601OopMapCacheEntry* tmp = NEW_C_HEAP_ARRAY(OopMapCacheEntry, 1, mtClass);602tmp->initialize();603tmp->fill(method, bci);604entry->resource_copy(tmp);605FREE_C_HEAP_ARRAY(OopMapCacheEntry, tmp, mtInternal);606}607608609