Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/oopRecorder.cpp
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#include "precompiled.hpp"25#include "ci/ciEnv.hpp"26#include "ci/ciInstance.hpp"27#include "ci/ciMetadata.hpp"28#include "code/oopRecorder.hpp"29#include "memory/allocation.inline.hpp"30#include "oops/oop.inline.hpp"3132#ifdef ASSERT33template <class T> int ValueRecorder<T>::_find_index_calls = 0;34template <class T> int ValueRecorder<T>::_hit_indexes = 0;35template <class T> int ValueRecorder<T>::_missed_indexes = 0;36#endif //ASSERT373839template <class T> ValueRecorder<T>::ValueRecorder(Arena* arena) {40_handles = NULL;41_indexes = NULL;42_arena = arena;43_complete = false;44}4546template <class T> template <class X> ValueRecorder<T>::IndexCache<X>::IndexCache() {47assert(first_index > 0, "initial zero state of cache must be invalid index");48Copy::zero_to_bytes(&_cache[0], sizeof(_cache));49}5051template <class T> int ValueRecorder<T>::size() {52_complete = true;53if (_handles == NULL) return 0;54return _handles->length() * sizeof(T);55}5657template <class T> void ValueRecorder<T>::copy_values_to(nmethod* nm) {58assert(_complete, "must be frozen");59maybe_initialize(); // get non-null handles, even if we have no oops60nm->copy_values(_handles);61}6263template <class T> void ValueRecorder<T>::maybe_initialize() {64if (_handles == NULL) {65if (_arena != NULL) {66_handles = new(_arena) GrowableArray<T>(_arena, 10, 0, 0);67_no_finds = new(_arena) GrowableArray<int>( _arena, 10, 0, 0);68} else {69_handles = new GrowableArray<T>(10, 0, 0);70_no_finds = new GrowableArray<int>( 10, 0, 0);71}72}73}747576template <class T> T ValueRecorder<T>::at(int index) {77// there is always a NULL virtually present as first object78if (index == null_index) return NULL;79return _handles->at(index - first_index);80}818283template <class T> int ValueRecorder<T>::add_handle(T h, bool make_findable) {84assert(!_complete, "cannot allocate more elements after size query");85maybe_initialize();86// indexing uses 1 as an origin--0 means null87int index = _handles->length() + first_index;88_handles->append(h);8990// Support correct operation of find_index().91assert(!(make_findable && !is_real(h)), "nulls are not findable");92if (make_findable) {93// This index may be returned from find_index().94if (_indexes != NULL) {95int* cloc = _indexes->cache_location(h);96_indexes->set_cache_location_index(cloc, index);97} else if (index == index_cache_threshold && _arena != NULL) {98_indexes = new(_arena) IndexCache<T>();99for (int i = 0; i < _handles->length(); i++) {100// Load the cache with pre-existing elements.101int index0 = i + first_index;102if (_no_finds->contains(index0)) continue;103int* cloc = _indexes->cache_location(_handles->at(i));104_indexes->set_cache_location_index(cloc, index0);105}106}107} else if (is_real(h)) {108// Remember that this index is not to be returned from find_index().109// This case is rare, because most or all uses of allocate_index pass110// an argument of NULL or Universe::non_oop_word.111// Thus, the expected length of _no_finds is zero.112_no_finds->append(index);113}114115return index;116}117118119template <class T> int ValueRecorder<T>::maybe_find_index(T h) {120debug_only(_find_index_calls++);121assert(!_complete, "cannot allocate more elements after size query");122maybe_initialize();123if (h == NULL) return null_index;124assert(is_real(h), "must be valid");125int* cloc = (_indexes == NULL)? NULL: _indexes->cache_location(h);126if (cloc != NULL) {127int cindex = _indexes->cache_location_index(cloc);128if (cindex == 0) {129return -1; // We know this handle is completely new.130}131if (cindex >= first_index && _handles->at(cindex - first_index) == h) {132debug_only(_hit_indexes++);133return cindex;134}135if (!_indexes->cache_location_collision(cloc)) {136return -1; // We know the current cache occupant is unique to that cloc.137}138}139140// Not found in cache, due to a cache collision. (Or, no cache at all.)141// Do a linear search, most recent to oldest.142for (int i = _handles->length() - 1; i >= 0; i--) {143if (_handles->at(i) == h) {144int findex = i + first_index;145if (_no_finds->contains(findex)) continue; // oops; skip this one146if (cloc != NULL) {147_indexes->set_cache_location_index(cloc, findex);148}149debug_only(_missed_indexes++);150return findex;151}152}153return -1;154}155156// Explicitly instantiate these types157template class ValueRecorder<Metadata*>;158template class ValueRecorder<jobject>;159160161