1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/code/oopRecorder.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,156 @@ 1.4 +/* 1.5 + * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_oopRecorder.cpp.incl" 1.30 + 1.31 +#ifdef ASSERT 1.32 +int OopRecorder::_find_index_calls = 0; 1.33 +int OopRecorder::_hit_indexes = 0; 1.34 +int OopRecorder::_missed_indexes = 0; 1.35 +#endif //ASSERT 1.36 + 1.37 + 1.38 +OopRecorder::OopRecorder(Arena* arena) { 1.39 + _handles = NULL; 1.40 + _indexes = NULL; 1.41 + _arena = arena; 1.42 + _complete = false; 1.43 +} 1.44 + 1.45 +OopRecorder::IndexCache::IndexCache() { 1.46 + assert(first_index > 0, "initial zero state of cache must be invalid index"); 1.47 + Copy::zero_to_bytes(&_cache[0], sizeof(_cache)); 1.48 +} 1.49 + 1.50 +int OopRecorder::oop_size() { 1.51 + _complete = true; 1.52 + if (_handles == NULL) return 0; 1.53 + return _handles->length() * sizeof(oop); 1.54 +} 1.55 + 1.56 +void OopRecorder::copy_to(CodeBlob* code) { 1.57 + assert(_complete, "must be frozen"); 1.58 + maybe_initialize(); // get non-null handles, even if we have no oops 1.59 + code->copy_oops(_handles); 1.60 +} 1.61 + 1.62 +void OopRecorder::maybe_initialize() { 1.63 + if (_handles == NULL) { 1.64 + if (_arena != NULL) { 1.65 + _handles = new(_arena) GrowableArray<jobject>(_arena, 10, 0, 0); 1.66 + _no_finds = new(_arena) GrowableArray<int>( _arena, 10, 0, 0); 1.67 + } else { 1.68 + _handles = new GrowableArray<jobject>(10, 0, 0); 1.69 + _no_finds = new GrowableArray<int>( 10, 0, 0); 1.70 + } 1.71 + } 1.72 +} 1.73 + 1.74 + 1.75 +jobject OopRecorder::handle_at(int index) { 1.76 + // there is always a NULL virtually present as first object 1.77 + if (index == null_index) return NULL; 1.78 + return _handles->at(index - first_index); 1.79 +} 1.80 + 1.81 + 1.82 +// Local definition. Used only in this module. 1.83 +inline bool OopRecorder::is_real_jobject(jobject h) { 1.84 + return h != NULL && h != (jobject)Universe::non_oop_word(); 1.85 +} 1.86 + 1.87 + 1.88 +int OopRecorder::add_handle(jobject h, bool make_findable) { 1.89 + assert(!_complete, "cannot allocate more elements after size query"); 1.90 + maybe_initialize(); 1.91 + // indexing uses 1 as an origin--0 means null 1.92 + int index = _handles->length() + first_index; 1.93 + _handles->append(h); 1.94 + 1.95 + // Support correct operation of find_index(). 1.96 + assert(!(make_findable && !is_real_jobject(h)), "nulls are not findable"); 1.97 + if (make_findable) { 1.98 + // This index may be returned from find_index(). 1.99 + if (_indexes != NULL) { 1.100 + int* cloc = _indexes->cache_location(h); 1.101 + _indexes->set_cache_location_index(cloc, index); 1.102 + } else if (index == index_cache_threshold && _arena != NULL) { 1.103 + _indexes = new(_arena) IndexCache(); 1.104 + for (int i = 0; i < _handles->length(); i++) { 1.105 + // Load the cache with pre-existing elements. 1.106 + int index0 = i + first_index; 1.107 + if (_no_finds->contains(index0)) continue; 1.108 + int* cloc = _indexes->cache_location(_handles->at(i)); 1.109 + _indexes->set_cache_location_index(cloc, index0); 1.110 + } 1.111 + } 1.112 + } else if (is_real_jobject(h)) { 1.113 + // Remember that this index is not to be returned from find_index(). 1.114 + // This case is rare, because most or all uses of allocate_index pass 1.115 + // a jobject argument of NULL or Universe::non_oop_word. 1.116 + // Thus, the expected length of _no_finds is zero. 1.117 + _no_finds->append(index); 1.118 + } 1.119 + 1.120 + return index; 1.121 +} 1.122 + 1.123 + 1.124 +int OopRecorder::maybe_find_index(jobject h) { 1.125 + debug_only(_find_index_calls++); 1.126 + assert(!_complete, "cannot allocate more elements after size query"); 1.127 + maybe_initialize(); 1.128 + if (h == NULL) return null_index; 1.129 + assert(is_real_jobject(h), "must be valid jobject"); 1.130 + int* cloc = (_indexes == NULL)? NULL: _indexes->cache_location(h); 1.131 + if (cloc != NULL) { 1.132 + int cindex = _indexes->cache_location_index(cloc); 1.133 + if (cindex == 0) { 1.134 + return -1; // We know this handle is completely new. 1.135 + } 1.136 + if (cindex >= first_index && _handles->at(cindex - first_index) == h) { 1.137 + debug_only(_hit_indexes++); 1.138 + return cindex; 1.139 + } 1.140 + if (!_indexes->cache_location_collision(cloc)) { 1.141 + return -1; // We know the current cache occupant is unique to that cloc. 1.142 + } 1.143 + } 1.144 + 1.145 + // Not found in cache, due to a cache collision. (Or, no cache at all.) 1.146 + // Do a linear search, most recent to oldest. 1.147 + for (int i = _handles->length() - 1; i >= 0; i--) { 1.148 + if (_handles->at(i) == h) { 1.149 + int findex = i + first_index; 1.150 + if (_no_finds->contains(findex)) continue; // oops; skip this one 1.151 + if (cloc != NULL) { 1.152 + _indexes->set_cache_location_index(cloc, findex); 1.153 + } 1.154 + debug_only(_missed_indexes++); 1.155 + return findex; 1.156 + } 1.157 + } 1.158 + return -1; 1.159 +}