Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/oopRecorder.hpp
32285 views
/*1* Copyright (c) 1998, 2012, 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#ifndef SHARE_VM_CODE_OOPRECORDER_HPP25#define SHARE_VM_CODE_OOPRECORDER_HPP2627#include "memory/universe.hpp"28#include "runtime/handles.hpp"29#include "utilities/growableArray.hpp"3031// Recording and retrieval of either oop relocations or metadata in compiled code.3233class CodeBlob;3435template <class T> class ValueRecorder : public StackObj {36public:37// A two-way mapping from positive indexes to oop handles.38// The zero index is reserved for a constant (sharable) null.39// Indexes may not be negative.4041// Use the given arena to manage storage, if not NULL.42// By default, uses the current ResourceArea.43ValueRecorder(Arena* arena = NULL);4445// Generate a new index on which nmethod::oop_addr_at will work.46// allocate_index and find_index never return the same index,47// and allocate_index never returns the same index twice.48// In fact, two successive calls to allocate_index return successive ints.49int allocate_index(T h) {50return add_handle(h, false);51}5253// For a given jobject or Metadata*, this will return the same index54// repeatedly. The index can later be given to nmethod::oop_at or55// metadata_at to retrieve the oop.56// However, the oop must not be changed via nmethod::oop_addr_at.57int find_index(T h) {58int index = maybe_find_index(h);59if (index < 0) { // previously unallocated60index = add_handle(h, true);61}62return index;63}6465// returns the size of the generated oop/metadata table, for sizing the66// CodeBlob. Must be called after all oops are allocated!67int size();6869// Retrieve the value at a given index.70T at(int index);7172int count() {73if (_handles == NULL) return 0;74// there is always a NULL virtually present as first object75return _handles->length() + first_index;76}7778// Helper function; returns false for NULL or Universe::non_oop_word().79bool is_real(T h) {80return h != NULL && h != (T)Universe::non_oop_word();81}8283// copy the generated table to nmethod84void copy_values_to(nmethod* nm);8586bool is_unused() { return _handles == NULL && !_complete; }87#ifdef ASSERT88bool is_complete() { return _complete; }89#endif9091private:92// variant of find_index which does not allocate if not found (yields -1)93int maybe_find_index(T h);9495// leaky hash table of handle => index, to help detect duplicate insertion96template <class X> class IndexCache : public ResourceObj {97// This class is only used by the ValueRecorder class.98friend class ValueRecorder;99enum {100_log_cache_size = 9,101_cache_size = (1<<_log_cache_size),102// Index entries are ints. The LSBit is a collision indicator.103_collision_bit_shift = 0,104_collision_bit = 1,105_index_shift = _collision_bit_shift+1106};107int _cache[_cache_size];108static juint cache_index(X handle) {109juint ci = (int) (intptr_t) handle;110ci ^= ci >> (BitsPerByte*2);111ci += ci >> (BitsPerByte*1);112return ci & (_cache_size-1);113}114int* cache_location(X handle) {115return &_cache[ cache_index(handle) ];116}117static bool cache_location_collision(int* cloc) {118return ((*cloc) & _collision_bit) != 0;119}120static int cache_location_index(int* cloc) {121return (*cloc) >> _index_shift;122}123static void set_cache_location_index(int* cloc, int index) {124int cval0 = (*cloc);125int cval1 = (index << _index_shift);126if (cval0 != 0 && cval1 != cval0) cval1 += _collision_bit;127(*cloc) = cval1;128}129IndexCache();130};131132void maybe_initialize();133int add_handle(T h, bool make_findable);134135enum { null_index = 0, first_index = 1, index_cache_threshold = 20 };136137GrowableArray<T>* _handles; // ordered list (first is always NULL)138GrowableArray<int>* _no_finds; // all unfindable indexes; usually empty139IndexCache<T>* _indexes; // map: handle -> its probable index140Arena* _arena;141bool _complete;142143#ifdef ASSERT144static int _find_index_calls, _hit_indexes, _missed_indexes;145#endif146};147148class OopRecorder : public ResourceObj {149private:150ValueRecorder<jobject> _oops;151ValueRecorder<Metadata*> _metadata;152public:153OopRecorder(Arena* arena = NULL): _oops(arena), _metadata(arena) {}154155int allocate_oop_index(jobject h) {156return _oops.allocate_index(h);157}158int find_index(jobject h) {159return _oops.find_index(h);160}161jobject oop_at(int index) {162return _oops.at(index);163}164int oop_size() {165return _oops.size();166}167int oop_count() {168return _oops.count();169}170bool is_real(jobject h) {171return _oops.is_real(h);172}173174int allocate_metadata_index(Metadata* oop) {175return _metadata.allocate_index(oop);176}177int find_index(Metadata* h) {178return _metadata.find_index(h);179}180Metadata* metadata_at(int index) {181return _metadata.at(index);182}183int metadata_size() {184return _metadata.size();185}186int metadata_count() {187return _metadata.count();188}189bool is_real(Metadata* h) {190return _metadata.is_real(h);191}192193bool is_unused() {194return _oops.is_unused() && _metadata.is_unused();195}196197void freeze() {198_oops.size();199_metadata.size();200}201202void copy_values_to(nmethod* nm) {203if (!_oops.is_unused()) {204_oops.copy_values_to(nm);205}206if (!_metadata.is_unused()) {207_metadata.copy_values_to(nm);208}209}210211#ifdef ASSERT212bool is_complete() {213assert(_oops.is_complete() == _metadata.is_complete(), "must agree");214return _oops.is_complete();215}216#endif217};218219220#endif // SHARE_VM_CODE_OOPRECORDER_HPP221222223