duke@435: /* jrose@1934: * Copyright (c) 1998, 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: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_oopRecorder.cpp.incl" duke@435: duke@435: #ifdef ASSERT duke@435: int OopRecorder::_find_index_calls = 0; duke@435: int OopRecorder::_hit_indexes = 0; duke@435: int OopRecorder::_missed_indexes = 0; duke@435: #endif //ASSERT duke@435: duke@435: duke@435: OopRecorder::OopRecorder(Arena* arena) { duke@435: _handles = NULL; duke@435: _indexes = NULL; duke@435: _arena = arena; duke@435: _complete = false; duke@435: } duke@435: duke@435: OopRecorder::IndexCache::IndexCache() { duke@435: assert(first_index > 0, "initial zero state of cache must be invalid index"); duke@435: Copy::zero_to_bytes(&_cache[0], sizeof(_cache)); duke@435: } duke@435: duke@435: int OopRecorder::oop_size() { duke@435: _complete = true; duke@435: if (_handles == NULL) return 0; duke@435: return _handles->length() * sizeof(oop); duke@435: } duke@435: twisti@1918: void OopRecorder::copy_to(nmethod* nm) { duke@435: assert(_complete, "must be frozen"); duke@435: maybe_initialize(); // get non-null handles, even if we have no oops twisti@1918: nm->copy_oops(_handles); duke@435: } duke@435: duke@435: void OopRecorder::maybe_initialize() { duke@435: if (_handles == NULL) { duke@435: if (_arena != NULL) { duke@435: _handles = new(_arena) GrowableArray(_arena, 10, 0, 0); duke@435: _no_finds = new(_arena) GrowableArray( _arena, 10, 0, 0); duke@435: } else { duke@435: _handles = new GrowableArray(10, 0, 0); duke@435: _no_finds = new GrowableArray( 10, 0, 0); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: jobject OopRecorder::handle_at(int index) { duke@435: // there is always a NULL virtually present as first object duke@435: if (index == null_index) return NULL; duke@435: return _handles->at(index - first_index); duke@435: } duke@435: duke@435: duke@435: // Local definition. Used only in this module. duke@435: inline bool OopRecorder::is_real_jobject(jobject h) { duke@435: return h != NULL && h != (jobject)Universe::non_oop_word(); duke@435: } duke@435: duke@435: duke@435: int OopRecorder::add_handle(jobject h, bool make_findable) { duke@435: assert(!_complete, "cannot allocate more elements after size query"); duke@435: maybe_initialize(); duke@435: // indexing uses 1 as an origin--0 means null duke@435: int index = _handles->length() + first_index; duke@435: _handles->append(h); duke@435: duke@435: // Support correct operation of find_index(). duke@435: assert(!(make_findable && !is_real_jobject(h)), "nulls are not findable"); duke@435: if (make_findable) { duke@435: // This index may be returned from find_index(). duke@435: if (_indexes != NULL) { duke@435: int* cloc = _indexes->cache_location(h); duke@435: _indexes->set_cache_location_index(cloc, index); duke@435: } else if (index == index_cache_threshold && _arena != NULL) { duke@435: _indexes = new(_arena) IndexCache(); duke@435: for (int i = 0; i < _handles->length(); i++) { duke@435: // Load the cache with pre-existing elements. duke@435: int index0 = i + first_index; duke@435: if (_no_finds->contains(index0)) continue; duke@435: int* cloc = _indexes->cache_location(_handles->at(i)); duke@435: _indexes->set_cache_location_index(cloc, index0); duke@435: } duke@435: } duke@435: } else if (is_real_jobject(h)) { duke@435: // Remember that this index is not to be returned from find_index(). duke@435: // This case is rare, because most or all uses of allocate_index pass duke@435: // a jobject argument of NULL or Universe::non_oop_word. duke@435: // Thus, the expected length of _no_finds is zero. duke@435: _no_finds->append(index); duke@435: } duke@435: duke@435: return index; duke@435: } duke@435: duke@435: duke@435: int OopRecorder::maybe_find_index(jobject h) { duke@435: debug_only(_find_index_calls++); duke@435: assert(!_complete, "cannot allocate more elements after size query"); duke@435: maybe_initialize(); duke@435: if (h == NULL) return null_index; duke@435: assert(is_real_jobject(h), "must be valid jobject"); duke@435: int* cloc = (_indexes == NULL)? NULL: _indexes->cache_location(h); duke@435: if (cloc != NULL) { duke@435: int cindex = _indexes->cache_location_index(cloc); duke@435: if (cindex == 0) { duke@435: return -1; // We know this handle is completely new. duke@435: } duke@435: if (cindex >= first_index && _handles->at(cindex - first_index) == h) { duke@435: debug_only(_hit_indexes++); duke@435: return cindex; duke@435: } duke@435: if (!_indexes->cache_location_collision(cloc)) { duke@435: return -1; // We know the current cache occupant is unique to that cloc. duke@435: } duke@435: } duke@435: duke@435: // Not found in cache, due to a cache collision. (Or, no cache at all.) duke@435: // Do a linear search, most recent to oldest. duke@435: for (int i = _handles->length() - 1; i >= 0; i--) { duke@435: if (_handles->at(i) == h) { duke@435: int findex = i + first_index; duke@435: if (_no_finds->contains(findex)) continue; // oops; skip this one duke@435: if (cloc != NULL) { duke@435: _indexes->set_cache_location_index(cloc, findex); duke@435: } duke@435: debug_only(_missed_indexes++); duke@435: return findex; duke@435: } duke@435: } duke@435: return -1; duke@435: }