Path: blob/master/src/hotspot/share/code/oopRecorder.cpp
40931 views
/*1* Copyright (c) 1998, 2021, 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.inline.hpp"29#include "gc/shared/collectedHeap.hpp"30#include "memory/allocation.inline.hpp"31#include "oops/oop.inline.hpp"32#include "runtime/jniHandles.inline.hpp"33#include "utilities/copy.hpp"3435#ifdef ASSERT36template <class T> int ValueRecorder<T>::_find_index_calls = 0;37template <class T> int ValueRecorder<T>::_hit_indexes = 0;38template <class T> int ValueRecorder<T>::_missed_indexes = 0;39#endif //ASSERT404142template <class T> ValueRecorder<T>::ValueRecorder(Arena* arena) {43_handles = NULL;44_indexes = NULL;45_arena = arena;46_complete = false;47}4849template <class T> template <class X> ValueRecorder<T>::IndexCache<X>::IndexCache() {50assert(first_index > 0, "initial zero state of cache must be invalid index");51Copy::zero_to_bytes(&_cache[0], sizeof(_cache));52}5354template <class T> int ValueRecorder<T>::size() {55_complete = true;56if (_handles == NULL) return 0;57return _handles->length() * sizeof(T);58}5960template <class T> void ValueRecorder<T>::copy_values_to(nmethod* nm) {61assert(_complete, "must be frozen");62maybe_initialize(); // get non-null handles, even if we have no oops63nm->copy_values(_handles);64}6566template <class T> void ValueRecorder<T>::maybe_initialize() {67if (_handles == NULL) {68if (_arena != NULL) {69_handles = new(_arena) GrowableArray<T>(_arena, 10, 0, 0);70_no_finds = new(_arena) GrowableArray<int>( _arena, 10, 0, 0);71} else {72_handles = new GrowableArray<T>(10, 0, 0);73_no_finds = new GrowableArray<int>( 10, 0, 0);74}75}76}777879template <class T> T ValueRecorder<T>::at(int index) {80// there is always a NULL virtually present as first object81if (index == null_index) return NULL;82return _handles->at(index - first_index);83}848586template <class T> int ValueRecorder<T>::add_handle(T h, bool make_findable) {87assert(!_complete, "cannot allocate more elements after size query");88maybe_initialize();89// indexing uses 1 as an origin--0 means null90int index = _handles->length() + first_index;91_handles->append(h);9293// Support correct operation of find_index().94assert(!(make_findable && !is_real(h)), "nulls are not findable");95if (make_findable) {96// This index may be returned from find_index().97if (_indexes != NULL) {98int* cloc = _indexes->cache_location(h);99_indexes->set_cache_location_index(cloc, index);100} else if (index == index_cache_threshold && _arena != NULL) {101_indexes = new(_arena) IndexCache<T>();102for (int i = 0; i < _handles->length(); i++) {103// Load the cache with pre-existing elements.104int index0 = i + first_index;105if (_no_finds->contains(index0)) continue;106int* cloc = _indexes->cache_location(_handles->at(i));107_indexes->set_cache_location_index(cloc, index0);108}109}110} else if (is_real(h)) {111// Remember that this index is not to be returned from find_index().112// This case is rare, because most or all uses of allocate_index pass113// an argument of NULL or Universe::non_oop_word.114// Thus, the expected length of _no_finds is zero.115_no_finds->append(index);116}117118return index;119}120121122template <class T> int ValueRecorder<T>::maybe_find_index(T h) {123debug_only(_find_index_calls++);124assert(!_complete, "cannot allocate more elements after size query");125maybe_initialize();126if (h == NULL) return null_index;127assert(is_real(h), "must be valid");128int* cloc = (_indexes == NULL)? NULL: _indexes->cache_location(h);129if (cloc != NULL) {130int cindex = _indexes->cache_location_index(cloc);131if (cindex == 0) {132return -1; // We know this handle is completely new.133}134if (cindex >= first_index && _handles->at(cindex - first_index) == h) {135debug_only(_hit_indexes++);136return cindex;137}138if (!_indexes->cache_location_collision(cloc)) {139return -1; // We know the current cache occupant is unique to that cloc.140}141}142143// Not found in cache, due to a cache collision. (Or, no cache at all.)144// Do a linear search, most recent to oldest.145for (int i = _handles->length() - 1; i >= 0; i--) {146if (_handles->at(i) == h) {147int findex = i + first_index;148if (_no_finds->contains(findex)) continue; // oops; skip this one149if (cloc != NULL) {150_indexes->set_cache_location_index(cloc, findex);151}152debug_only(_missed_indexes++);153return findex;154}155}156return -1;157}158159// Explicitly instantiate these types160template class ValueRecorder<Metadata*>;161template class ValueRecorder<jobject>;162163oop ObjectLookup::ObjectEntry::oop_value() const { return JNIHandles::resolve(_value); }164165ObjectLookup::ObjectLookup(): _values(4), _gc_count(Universe::heap()->total_collections()) {}166167void ObjectLookup::maybe_resort() {168// The values are kept sorted by address which may be invalidated169// after a GC, so resort if a GC has occurred since last time.170if (_gc_count != Universe::heap()->total_collections()) {171_gc_count = Universe::heap()->total_collections();172_values.sort(sort_by_address);173}174}175176int ObjectLookup::sort_by_address(oop a, oop b) {177// oopDesc::compare returns the opposite of what this function returned178return -(oopDesc::compare(a, b));179}180181int ObjectLookup::sort_by_address(ObjectEntry* a, ObjectEntry* b) {182return sort_by_address(a->oop_value(), b->oop_value());183}184185int ObjectLookup::sort_oop_by_address(oop const& a, ObjectEntry const& b) {186return sort_by_address(a, b.oop_value());187}188189int ObjectLookup::find_index(jobject handle, OopRecorder* oop_recorder) {190if (handle == NULL) {191return 0;192}193oop object = JNIHandles::resolve(handle);194maybe_resort();195bool found;196int location = _values.find_sorted<oop, sort_oop_by_address>(object, found);197if (!found) {198jobject handle = JNIHandles::make_local(object);199ObjectEntry r(handle, oop_recorder->allocate_oop_index(handle));200_values.insert_before(location, r);201return r.index();202}203return _values.at(location).index();204}205206207