duke@435: /* stefank@2314: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_INTERPRETER_OOPMAPCACHE_HPP stefank@2314: #define SHARE_VM_INTERPRETER_OOPMAPCACHE_HPP stefank@2314: stefank@2314: #include "oops/generateOopMap.hpp" stefank@2325: #include "runtime/mutex.hpp" stefank@2314: duke@435: // A Cache for storing (method, bci) -> oopMap. duke@435: // The memory management system uses the cache when locating object duke@435: // references in an interpreted frame. duke@435: // duke@435: // OopMapCache's are allocated lazily per instanceKlass. duke@435: duke@435: // The oopMap (InterpreterOopMap) is stored as a bit mask. If the duke@435: // bit_mask can fit into two words it is stored in duke@435: // the _bit_mask array, otherwise it is allocated on the heap. duke@435: // For OopMapCacheEntry the bit_mask is allocated in the C heap duke@435: // because these entries persist between garbage collections. duke@435: // For InterpreterOopMap the bit_mask is allocated in duke@435: // a resource area for better performance. InterpreterOopMap duke@435: // should only be created and deleted during same garbage collection. duke@435: // duke@435: // If ENABBLE_ZAP_DEAD_LOCALS is defined, two bits are used duke@435: // per entry instead of one. In all cases, duke@435: // the first bit is set to indicate oops as opposed to other duke@435: // values. If the second bit is available, duke@435: // it is set for dead values. We get the following encoding: duke@435: // duke@435: // 00 live value duke@435: // 01 live oop duke@435: // 10 dead value duke@435: // 11 (we cannot distinguish between dead oops or values with the current oop map generator) duke@435: duke@435: duke@435: class OffsetClosure { duke@435: public: duke@435: virtual void offset_do(int offset) = 0; duke@435: }; duke@435: duke@435: duke@435: class InterpreterOopMap: ResourceObj { duke@435: friend class OopMapCache; duke@435: duke@435: public: duke@435: enum { duke@435: N = 2, // the number of words reserved duke@435: // for inlined mask storage duke@435: small_mask_limit = N * BitsPerWord, // the maximum number of bits duke@435: // available for small masks, duke@435: // small_mask_limit can be set to 0 duke@435: // for testing bit_mask allocation duke@435: duke@435: #ifdef ENABLE_ZAP_DEAD_LOCALS duke@435: bits_per_entry = 2, duke@435: dead_bit_number = 1, duke@435: #else duke@435: bits_per_entry = 1, duke@435: #endif duke@435: oop_bit_number = 0 duke@435: }; duke@435: duke@435: private: duke@435: methodOop _method; // the method for which the mask is valid duke@435: unsigned short _bci; // the bci for which the mask is valid duke@435: int _mask_size; // the mask size in bits duke@435: int _expression_stack_size; // the size of the expression stack in slots duke@435: duke@435: protected: duke@435: intptr_t _bit_mask[N]; // the bit mask if duke@435: // mask_size <= small_mask_limit, duke@435: // ptr to bit mask otherwise duke@435: // "protected" so that sub classes can duke@435: // access it without using trickery in duke@435: // methd bit_mask(). duke@435: #ifdef ASSERT duke@435: bool _resource_allocate_bit_mask; duke@435: #endif duke@435: duke@435: // access methods duke@435: methodOop method() const { return _method; } duke@435: void set_method(methodOop v) { _method = v; } duke@435: int bci() const { return _bci; } duke@435: void set_bci(int v) { _bci = v; } duke@435: int mask_size() const { return _mask_size; } duke@435: void set_mask_size(int v) { _mask_size = v; } duke@435: int number_of_entries() const { return mask_size() / bits_per_entry; } duke@435: // Test bit mask size and return either the in-line bit mask or allocated duke@435: // bit mask. duke@435: uintptr_t* bit_mask() { return (uintptr_t*)(mask_size() <= small_mask_limit ? (intptr_t)_bit_mask : _bit_mask[0]); } duke@435: duke@435: // return the word size of_bit_mask. mask_size() <= 4 * MAX_USHORT duke@435: size_t mask_word_size() { duke@435: return (mask_size() + BitsPerWord - 1) / BitsPerWord; duke@435: } duke@435: duke@435: uintptr_t entry_at(int offset) { int i = offset * bits_per_entry; return bit_mask()[i / BitsPerWord] >> (i % BitsPerWord); } duke@435: duke@435: void set_expression_stack_size(int sz) { _expression_stack_size = sz; } duke@435: duke@435: #ifdef ENABLE_ZAP_DEAD_LOCALS duke@435: bool is_dead(int offset) { return (entry_at(offset) & (1 << dead_bit_number)) != 0; } duke@435: #endif duke@435: duke@435: // Lookup duke@435: bool match(methodHandle method, int bci) { return _method == method() && _bci == bci; } duke@435: bool is_empty(); duke@435: duke@435: // Initialization duke@435: void initialize(); duke@435: duke@435: public: duke@435: InterpreterOopMap(); duke@435: ~InterpreterOopMap(); duke@435: duke@435: // Copy the OopMapCacheEntry in parameter "from" into this duke@435: // InterpreterOopMap. If the _bit_mask[0] in "from" points to duke@435: // allocated space (i.e., the bit mask was to large to hold duke@435: // in-line), allocate the space from a Resource area. duke@435: void resource_copy(OopMapCacheEntry* from); duke@435: duke@435: void iterate_oop(OffsetClosure* oop_closure); duke@435: void oop_iterate(OopClosure * blk); duke@435: void oop_iterate(OopClosure * blk, MemRegion mr); duke@435: void verify(); duke@435: void print(); duke@435: duke@435: bool is_oop (int offset) { return (entry_at(offset) & (1 << oop_bit_number )) != 0; } duke@435: duke@435: int expression_stack_size() { return _expression_stack_size; } duke@435: duke@435: #ifdef ENABLE_ZAP_DEAD_LOCALS duke@435: void iterate_all(OffsetClosure* oop_closure, OffsetClosure* value_closure, OffsetClosure* dead_closure); duke@435: #endif duke@435: }; duke@435: duke@435: class OopMapCache : public CHeapObj { duke@435: private: duke@435: enum { _size = 32, // Use fixed size for now duke@435: _probe_depth = 3 // probe depth in case of collisions duke@435: }; duke@435: duke@435: OopMapCacheEntry* _array; duke@435: duke@435: unsigned int hash_value_for(methodHandle method, int bci); duke@435: OopMapCacheEntry* entry_at(int i) const; duke@435: duke@435: Mutex _mut; duke@435: duke@435: void flush(); duke@435: duke@435: public: duke@435: OopMapCache(); duke@435: ~OopMapCache(); // free up memory duke@435: duke@435: // flush cache entry is occupied by an obsolete method duke@435: void flush_obsolete_entries(); duke@435: duke@435: // Returns the oopMap for (method, bci) in parameter "entry". duke@435: // Returns false if an oop map was not found. duke@435: void lookup(methodHandle method, int bci, InterpreterOopMap* entry); duke@435: duke@435: // Compute an oop map without updating the cache or grabbing any locks (for debugging) duke@435: static void compute_one_oop_map(methodHandle method, int bci, InterpreterOopMap* entry); duke@435: duke@435: // Helpers duke@435: // Iterate over the entries in the cached OopMapCacheEntry's duke@435: void oop_iterate(OopClosure *blk); duke@435: void oop_iterate(OopClosure *blk, MemRegion mr); duke@435: void verify(); duke@435: duke@435: // Returns total no. of bytes allocated as part of OopMapCache's duke@435: static long memory_usage() PRODUCT_RETURN0; duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_INTERPRETER_OOPMAPCACHE_HPP