duke@435: /* coleenp@4037: * Copyright (c) 1998, 2012, 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_CODE_OOPRECORDER_HPP stefank@2314: #define SHARE_VM_CODE_OOPRECORDER_HPP stefank@2314: coleenp@4037: #include "memory/universe.hpp" stefank@2314: #include "runtime/handles.hpp" stefank@2314: #include "utilities/growableArray.hpp" stefank@2314: coleenp@4037: // Recording and retrieval of either oop relocations or metadata in compiled code. duke@435: duke@435: class CodeBlob; duke@435: coleenp@4037: template class ValueRecorder : public StackObj { duke@435: public: duke@435: // A two-way mapping from positive indexes to oop handles. duke@435: // The zero index is reserved for a constant (sharable) null. duke@435: // Indexes may not be negative. duke@435: duke@435: // Use the given arena to manage storage, if not NULL. duke@435: // By default, uses the current ResourceArea. coleenp@4037: ValueRecorder(Arena* arena = NULL); duke@435: coleenp@4037: // Generate a new index on which nmethod::oop_addr_at will work. duke@435: // allocate_index and find_index never return the same index, duke@435: // and allocate_index never returns the same index twice. duke@435: // In fact, two successive calls to allocate_index return successive ints. coleenp@4037: int allocate_index(T h) { duke@435: return add_handle(h, false); duke@435: } duke@435: coleenp@4037: // For a given jobject or Metadata*, this will return the same index coleenp@4037: // repeatedly. The index can later be given to nmethod::oop_at or coleenp@4037: // metadata_at to retrieve the oop. coleenp@4037: // However, the oop must not be changed via nmethod::oop_addr_at. coleenp@4037: int find_index(T h) { duke@435: int index = maybe_find_index(h); duke@435: if (index < 0) { // previously unallocated duke@435: index = add_handle(h, true); duke@435: } duke@435: return index; duke@435: } duke@435: coleenp@4037: // returns the size of the generated oop/metadata table, for sizing the coleenp@4037: // CodeBlob. Must be called after all oops are allocated! coleenp@4037: int size(); duke@435: coleenp@4037: // Retrieve the value at a given index. coleenp@4037: T at(int index); duke@435: coleenp@4037: int count() { coleenp@4037: if (_handles == NULL) return 0; duke@435: // there is always a NULL virtually present as first object duke@435: return _handles->length() + first_index; duke@435: } duke@435: coleenp@4037: // Helper function; returns false for NULL or Universe::non_oop_word(). coleenp@4037: bool is_real(T h) { coleenp@4037: return h != NULL && h != (T)Universe::non_oop_word(); coleenp@4037: } coleenp@4037: coleenp@4037: // copy the generated table to nmethod coleenp@4037: void copy_values_to(nmethod* nm); duke@435: duke@435: bool is_unused() { return _handles == NULL && !_complete; } duke@435: #ifdef ASSERT duke@435: bool is_complete() { return _complete; } duke@435: #endif duke@435: duke@435: private: coleenp@4037: // variant of find_index which does not allocate if not found (yields -1) coleenp@4037: int maybe_find_index(T h); coleenp@4037: duke@435: // leaky hash table of handle => index, to help detect duplicate insertion coleenp@4037: template class IndexCache : public ResourceObj { coleenp@4037: // This class is only used by the ValueRecorder class. coleenp@4037: friend class ValueRecorder; duke@435: enum { duke@435: _log_cache_size = 9, duke@435: _cache_size = (1<<_log_cache_size), duke@435: // Index entries are ints. The LSBit is a collision indicator. duke@435: _collision_bit_shift = 0, duke@435: _collision_bit = 1, duke@435: _index_shift = _collision_bit_shift+1 duke@435: }; duke@435: int _cache[_cache_size]; coleenp@4037: static juint cache_index(X handle) { duke@435: juint ci = (int) (intptr_t) handle; duke@435: ci ^= ci >> (BitsPerByte*2); duke@435: ci += ci >> (BitsPerByte*1); duke@435: return ci & (_cache_size-1); duke@435: } coleenp@4037: int* cache_location(X handle) { duke@435: return &_cache[ cache_index(handle) ]; duke@435: } duke@435: static bool cache_location_collision(int* cloc) { duke@435: return ((*cloc) & _collision_bit) != 0; duke@435: } duke@435: static int cache_location_index(int* cloc) { duke@435: return (*cloc) >> _index_shift; duke@435: } duke@435: static void set_cache_location_index(int* cloc, int index) { duke@435: int cval0 = (*cloc); duke@435: int cval1 = (index << _index_shift); duke@435: if (cval0 != 0 && cval1 != cval0) cval1 += _collision_bit; duke@435: (*cloc) = cval1; duke@435: } duke@435: IndexCache(); duke@435: }; duke@435: duke@435: void maybe_initialize(); coleenp@4037: int add_handle(T h, bool make_findable); duke@435: duke@435: enum { null_index = 0, first_index = 1, index_cache_threshold = 20 }; duke@435: coleenp@4037: GrowableArray* _handles; // ordered list (first is always NULL) duke@435: GrowableArray* _no_finds; // all unfindable indexes; usually empty coleenp@4037: IndexCache* _indexes; // map: handle -> its probable index duke@435: Arena* _arena; duke@435: bool _complete; duke@435: duke@435: #ifdef ASSERT duke@435: static int _find_index_calls, _hit_indexes, _missed_indexes; duke@435: #endif duke@435: }; stefank@2314: coleenp@4037: class OopRecorder : public ResourceObj { coleenp@4037: private: coleenp@4037: ValueRecorder _oops; coleenp@4037: ValueRecorder _metadata; coleenp@4037: public: coleenp@4037: OopRecorder(Arena* arena = NULL): _oops(arena), _metadata(arena) {} coleenp@4037: coleenp@4037: int allocate_oop_index(jobject h) { coleenp@4037: return _oops.allocate_index(h); coleenp@4037: } coleenp@4037: int find_index(jobject h) { coleenp@4037: return _oops.find_index(h); coleenp@4037: } coleenp@4037: jobject oop_at(int index) { coleenp@4037: return _oops.at(index); coleenp@4037: } coleenp@4037: int oop_size() { coleenp@4037: return _oops.size(); coleenp@4037: } coleenp@4037: int oop_count() { coleenp@4037: return _oops.count(); coleenp@4037: } coleenp@4037: bool is_real(jobject h) { coleenp@4037: return _oops.is_real(h); coleenp@4037: } coleenp@4037: coleenp@4037: int allocate_metadata_index(Metadata* oop) { coleenp@4037: return _metadata.allocate_index(oop); coleenp@4037: } coleenp@4037: int find_index(Metadata* h) { coleenp@4037: return _metadata.find_index(h); coleenp@4037: } coleenp@4037: Metadata* metadata_at(int index) { coleenp@4037: return _metadata.at(index); coleenp@4037: } coleenp@4037: int metadata_size() { coleenp@4037: return _metadata.size(); coleenp@4037: } coleenp@4037: int metadata_count() { coleenp@4037: return _metadata.count(); coleenp@4037: } coleenp@4037: bool is_real(Metadata* h) { coleenp@4037: return _metadata.is_real(h); coleenp@4037: } coleenp@4037: coleenp@4037: bool is_unused() { coleenp@4037: return _oops.is_unused() && _metadata.is_unused(); coleenp@4037: } coleenp@4037: coleenp@4037: void freeze() { coleenp@4037: _oops.size(); coleenp@4037: _metadata.size(); coleenp@4037: } coleenp@4037: coleenp@4037: void copy_values_to(nmethod* nm) { coleenp@4037: if (!_oops.is_unused()) { coleenp@4037: _oops.copy_values_to(nm); coleenp@4037: } coleenp@4037: if (!_metadata.is_unused()) { coleenp@4037: _metadata.copy_values_to(nm); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: #ifdef ASSERT coleenp@4037: bool is_complete() { coleenp@4037: assert(_oops.is_complete() == _metadata.is_complete(), "must agree"); coleenp@4037: return _oops.is_complete(); coleenp@4037: } coleenp@4037: #endif coleenp@4037: }; coleenp@4037: coleenp@4037: stefank@2314: #endif // SHARE_VM_CODE_OOPRECORDER_HPP