Fri, 29 Jan 2010 08:33:24 -0800
6921339: backout 6917766
Reviewed-by: mr
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | # include "incls/_precompiled.incl" |
duke@435 | 26 | # include "incls/_oopRecorder.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | #ifdef ASSERT |
duke@435 | 29 | int OopRecorder::_find_index_calls = 0; |
duke@435 | 30 | int OopRecorder::_hit_indexes = 0; |
duke@435 | 31 | int OopRecorder::_missed_indexes = 0; |
duke@435 | 32 | #endif //ASSERT |
duke@435 | 33 | |
duke@435 | 34 | |
duke@435 | 35 | OopRecorder::OopRecorder(Arena* arena) { |
duke@435 | 36 | _handles = NULL; |
duke@435 | 37 | _indexes = NULL; |
duke@435 | 38 | _arena = arena; |
duke@435 | 39 | _complete = false; |
duke@435 | 40 | } |
duke@435 | 41 | |
duke@435 | 42 | OopRecorder::IndexCache::IndexCache() { |
duke@435 | 43 | assert(first_index > 0, "initial zero state of cache must be invalid index"); |
duke@435 | 44 | Copy::zero_to_bytes(&_cache[0], sizeof(_cache)); |
duke@435 | 45 | } |
duke@435 | 46 | |
duke@435 | 47 | int OopRecorder::oop_size() { |
duke@435 | 48 | _complete = true; |
duke@435 | 49 | if (_handles == NULL) return 0; |
duke@435 | 50 | return _handles->length() * sizeof(oop); |
duke@435 | 51 | } |
duke@435 | 52 | |
duke@435 | 53 | void OopRecorder::copy_to(CodeBlob* code) { |
duke@435 | 54 | assert(_complete, "must be frozen"); |
duke@435 | 55 | maybe_initialize(); // get non-null handles, even if we have no oops |
duke@435 | 56 | code->copy_oops(_handles); |
duke@435 | 57 | } |
duke@435 | 58 | |
duke@435 | 59 | void OopRecorder::maybe_initialize() { |
duke@435 | 60 | if (_handles == NULL) { |
duke@435 | 61 | if (_arena != NULL) { |
duke@435 | 62 | _handles = new(_arena) GrowableArray<jobject>(_arena, 10, 0, 0); |
duke@435 | 63 | _no_finds = new(_arena) GrowableArray<int>( _arena, 10, 0, 0); |
duke@435 | 64 | } else { |
duke@435 | 65 | _handles = new GrowableArray<jobject>(10, 0, 0); |
duke@435 | 66 | _no_finds = new GrowableArray<int>( 10, 0, 0); |
duke@435 | 67 | } |
duke@435 | 68 | } |
duke@435 | 69 | } |
duke@435 | 70 | |
duke@435 | 71 | |
duke@435 | 72 | jobject OopRecorder::handle_at(int index) { |
duke@435 | 73 | // there is always a NULL virtually present as first object |
duke@435 | 74 | if (index == null_index) return NULL; |
duke@435 | 75 | return _handles->at(index - first_index); |
duke@435 | 76 | } |
duke@435 | 77 | |
duke@435 | 78 | |
duke@435 | 79 | // Local definition. Used only in this module. |
duke@435 | 80 | inline bool OopRecorder::is_real_jobject(jobject h) { |
duke@435 | 81 | return h != NULL && h != (jobject)Universe::non_oop_word(); |
duke@435 | 82 | } |
duke@435 | 83 | |
duke@435 | 84 | |
duke@435 | 85 | int OopRecorder::add_handle(jobject h, bool make_findable) { |
duke@435 | 86 | assert(!_complete, "cannot allocate more elements after size query"); |
duke@435 | 87 | maybe_initialize(); |
duke@435 | 88 | // indexing uses 1 as an origin--0 means null |
duke@435 | 89 | int index = _handles->length() + first_index; |
duke@435 | 90 | _handles->append(h); |
duke@435 | 91 | |
duke@435 | 92 | // Support correct operation of find_index(). |
duke@435 | 93 | assert(!(make_findable && !is_real_jobject(h)), "nulls are not findable"); |
duke@435 | 94 | if (make_findable) { |
duke@435 | 95 | // This index may be returned from find_index(). |
duke@435 | 96 | if (_indexes != NULL) { |
duke@435 | 97 | int* cloc = _indexes->cache_location(h); |
duke@435 | 98 | _indexes->set_cache_location_index(cloc, index); |
duke@435 | 99 | } else if (index == index_cache_threshold && _arena != NULL) { |
duke@435 | 100 | _indexes = new(_arena) IndexCache(); |
duke@435 | 101 | for (int i = 0; i < _handles->length(); i++) { |
duke@435 | 102 | // Load the cache with pre-existing elements. |
duke@435 | 103 | int index0 = i + first_index; |
duke@435 | 104 | if (_no_finds->contains(index0)) continue; |
duke@435 | 105 | int* cloc = _indexes->cache_location(_handles->at(i)); |
duke@435 | 106 | _indexes->set_cache_location_index(cloc, index0); |
duke@435 | 107 | } |
duke@435 | 108 | } |
duke@435 | 109 | } else if (is_real_jobject(h)) { |
duke@435 | 110 | // Remember that this index is not to be returned from find_index(). |
duke@435 | 111 | // This case is rare, because most or all uses of allocate_index pass |
duke@435 | 112 | // a jobject argument of NULL or Universe::non_oop_word. |
duke@435 | 113 | // Thus, the expected length of _no_finds is zero. |
duke@435 | 114 | _no_finds->append(index); |
duke@435 | 115 | } |
duke@435 | 116 | |
duke@435 | 117 | return index; |
duke@435 | 118 | } |
duke@435 | 119 | |
duke@435 | 120 | |
duke@435 | 121 | int OopRecorder::maybe_find_index(jobject h) { |
duke@435 | 122 | debug_only(_find_index_calls++); |
duke@435 | 123 | assert(!_complete, "cannot allocate more elements after size query"); |
duke@435 | 124 | maybe_initialize(); |
duke@435 | 125 | if (h == NULL) return null_index; |
duke@435 | 126 | assert(is_real_jobject(h), "must be valid jobject"); |
duke@435 | 127 | int* cloc = (_indexes == NULL)? NULL: _indexes->cache_location(h); |
duke@435 | 128 | if (cloc != NULL) { |
duke@435 | 129 | int cindex = _indexes->cache_location_index(cloc); |
duke@435 | 130 | if (cindex == 0) { |
duke@435 | 131 | return -1; // We know this handle is completely new. |
duke@435 | 132 | } |
duke@435 | 133 | if (cindex >= first_index && _handles->at(cindex - first_index) == h) { |
duke@435 | 134 | debug_only(_hit_indexes++); |
duke@435 | 135 | return cindex; |
duke@435 | 136 | } |
duke@435 | 137 | if (!_indexes->cache_location_collision(cloc)) { |
duke@435 | 138 | return -1; // We know the current cache occupant is unique to that cloc. |
duke@435 | 139 | } |
duke@435 | 140 | } |
duke@435 | 141 | |
duke@435 | 142 | // Not found in cache, due to a cache collision. (Or, no cache at all.) |
duke@435 | 143 | // Do a linear search, most recent to oldest. |
duke@435 | 144 | for (int i = _handles->length() - 1; i >= 0; i--) { |
duke@435 | 145 | if (_handles->at(i) == h) { |
duke@435 | 146 | int findex = i + first_index; |
duke@435 | 147 | if (_no_finds->contains(findex)) continue; // oops; skip this one |
duke@435 | 148 | if (cloc != NULL) { |
duke@435 | 149 | _indexes->set_cache_location_index(cloc, findex); |
duke@435 | 150 | } |
duke@435 | 151 | debug_only(_missed_indexes++); |
duke@435 | 152 | return findex; |
duke@435 | 153 | } |
duke@435 | 154 | } |
duke@435 | 155 | return -1; |
duke@435 | 156 | } |