aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, 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: #ifndef SHARE_VM_INTERPRETER_OOPMAPCACHE_HPP aoqi@0: #define SHARE_VM_INTERPRETER_OOPMAPCACHE_HPP aoqi@0: aoqi@0: #include "oops/generateOopMap.hpp" aoqi@0: #include "runtime/mutex.hpp" aoqi@0: aoqi@0: // A Cache for storing (method, bci) -> oopMap. aoqi@0: // The memory management system uses the cache when locating object aoqi@0: // references in an interpreted frame. aoqi@0: // aoqi@0: // OopMapCache's are allocated lazily per InstanceKlass. aoqi@0: aoqi@0: // The oopMap (InterpreterOopMap) is stored as a bit mask. If the aoqi@0: // bit_mask can fit into two words it is stored in aoqi@0: // the _bit_mask array, otherwise it is allocated on the heap. aoqi@0: // For OopMapCacheEntry the bit_mask is allocated in the C heap aoqi@0: // because these entries persist between garbage collections. aoqi@0: // For InterpreterOopMap the bit_mask is allocated in aoqi@0: // a resource area for better performance. InterpreterOopMap aoqi@0: // should only be created and deleted during same garbage collection. aoqi@0: // aoqi@0: // If ENABBLE_ZAP_DEAD_LOCALS is defined, two bits are used aoqi@0: // per entry instead of one. In all cases, aoqi@0: // the first bit is set to indicate oops as opposed to other aoqi@0: // values. If the second bit is available, aoqi@0: // it is set for dead values. We get the following encoding: aoqi@0: // aoqi@0: // 00 live value aoqi@0: // 01 live oop aoqi@0: // 10 dead value aoqi@0: // 11 (we cannot distinguish between dead oops or values with the current oop map generator) aoqi@0: aoqi@0: aoqi@0: class OffsetClosure { aoqi@0: public: aoqi@0: virtual void offset_do(int offset) = 0; aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class InterpreterOopMap: ResourceObj { aoqi@0: friend class OopMapCache; aoqi@0: aoqi@0: public: aoqi@0: enum { aoqi@0: N = 2, // the number of words reserved aoqi@0: // for inlined mask storage aoqi@0: small_mask_limit = N * BitsPerWord, // the maximum number of bits aoqi@0: // available for small masks, aoqi@0: // small_mask_limit can be set to 0 aoqi@0: // for testing bit_mask allocation aoqi@0: aoqi@0: #ifdef ENABLE_ZAP_DEAD_LOCALS aoqi@0: bits_per_entry = 2, aoqi@0: dead_bit_number = 1, aoqi@0: #else aoqi@0: bits_per_entry = 1, aoqi@0: #endif aoqi@0: oop_bit_number = 0 aoqi@0: }; aoqi@0: aoqi@0: private: aoqi@0: Method* _method; // the method for which the mask is valid aoqi@0: unsigned short _bci; // the bci for which the mask is valid aoqi@0: int _mask_size; // the mask size in bits aoqi@0: int _expression_stack_size; // the size of the expression stack in slots aoqi@0: aoqi@0: protected: aoqi@0: intptr_t _bit_mask[N]; // the bit mask if aoqi@0: // mask_size <= small_mask_limit, aoqi@0: // ptr to bit mask otherwise aoqi@0: // "protected" so that sub classes can aoqi@0: // access it without using trickery in aoqi@0: // methd bit_mask(). aoqi@0: #ifdef ASSERT aoqi@0: bool _resource_allocate_bit_mask; aoqi@0: #endif aoqi@0: aoqi@0: // access methods aoqi@0: Method* method() const { return _method; } aoqi@0: void set_method(Method* v) { _method = v; } aoqi@0: int bci() const { return _bci; } aoqi@0: void set_bci(int v) { _bci = v; } aoqi@0: int mask_size() const { return _mask_size; } aoqi@0: void set_mask_size(int v) { _mask_size = v; } aoqi@0: int number_of_entries() const { return mask_size() / bits_per_entry; } aoqi@0: // Test bit mask size and return either the in-line bit mask or allocated aoqi@0: // bit mask. aoqi@0: uintptr_t* bit_mask() { return (uintptr_t*)(mask_size() <= small_mask_limit ? (intptr_t)_bit_mask : _bit_mask[0]); } aoqi@0: aoqi@0: // return the word size of_bit_mask. mask_size() <= 4 * MAX_USHORT aoqi@0: size_t mask_word_size() { aoqi@0: return (mask_size() + BitsPerWord - 1) / BitsPerWord; aoqi@0: } aoqi@0: aoqi@0: uintptr_t entry_at(int offset) { int i = offset * bits_per_entry; return bit_mask()[i / BitsPerWord] >> (i % BitsPerWord); } aoqi@0: aoqi@0: void set_expression_stack_size(int sz) { _expression_stack_size = sz; } aoqi@0: aoqi@0: #ifdef ENABLE_ZAP_DEAD_LOCALS aoqi@0: bool is_dead(int offset) { return (entry_at(offset) & (1 << dead_bit_number)) != 0; } aoqi@0: #endif aoqi@0: aoqi@0: // Lookup aoqi@0: bool match(methodHandle method, int bci) { return _method == method() && _bci == bci; } aoqi@0: bool is_empty(); aoqi@0: aoqi@0: // Initialization aoqi@0: void initialize(); aoqi@0: aoqi@0: public: aoqi@0: InterpreterOopMap(); aoqi@0: ~InterpreterOopMap(); aoqi@0: aoqi@0: // Copy the OopMapCacheEntry in parameter "from" into this aoqi@0: // InterpreterOopMap. If the _bit_mask[0] in "from" points to aoqi@0: // allocated space (i.e., the bit mask was to large to hold aoqi@0: // in-line), allocate the space from a Resource area. aoqi@0: void resource_copy(OopMapCacheEntry* from); aoqi@0: aoqi@0: void iterate_oop(OffsetClosure* oop_closure); aoqi@0: void print(); aoqi@0: aoqi@0: bool is_oop (int offset) { return (entry_at(offset) & (1 << oop_bit_number )) != 0; } aoqi@0: aoqi@0: int expression_stack_size() { return _expression_stack_size; } aoqi@0: aoqi@0: #ifdef ENABLE_ZAP_DEAD_LOCALS aoqi@0: void iterate_all(OffsetClosure* oop_closure, OffsetClosure* value_closure, OffsetClosure* dead_closure); aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: class OopMapCache : public CHeapObj { aoqi@0: private: aoqi@0: enum { _size = 32, // Use fixed size for now aoqi@0: _probe_depth = 3 // probe depth in case of collisions aoqi@0: }; aoqi@0: aoqi@0: OopMapCacheEntry* _array; aoqi@0: aoqi@0: unsigned int hash_value_for(methodHandle method, int bci); aoqi@0: OopMapCacheEntry* entry_at(int i) const; aoqi@0: aoqi@0: Mutex _mut; aoqi@0: aoqi@0: void flush(); aoqi@0: aoqi@0: public: aoqi@0: OopMapCache(); aoqi@0: ~OopMapCache(); // free up memory aoqi@0: aoqi@0: // flush cache entry is occupied by an obsolete method aoqi@0: void flush_obsolete_entries(); aoqi@0: aoqi@0: // Returns the oopMap for (method, bci) in parameter "entry". aoqi@0: // Returns false if an oop map was not found. aoqi@0: void lookup(methodHandle method, int bci, InterpreterOopMap* entry); aoqi@0: aoqi@0: // Compute an oop map without updating the cache or grabbing any locks (for debugging) aoqi@0: static void compute_one_oop_map(methodHandle method, int bci, InterpreterOopMap* entry); aoqi@0: aoqi@0: // Returns total no. of bytes allocated as part of OopMapCache's aoqi@0: static long memory_usage() PRODUCT_RETURN0; aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_INTERPRETER_OOPMAPCACHE_HPP