Fri, 08 Nov 2013 01:13:11 -0800
8023037: Race between ciEnv::register_method and nmethod::make_not_entrant_or_zombie
Reviewed-by: kvn, iveresov
duke@435 | 1 | /* |
coleenp@4037 | 2 | * Copyright (c) 1998, 2012, Oracle and/or its affiliates. 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 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
coleenp@4037 | 26 | #include "ci/ciEnv.hpp" |
coleenp@4037 | 27 | #include "ci/ciInstance.hpp" |
coleenp@4037 | 28 | #include "ci/ciMetadata.hpp" |
stefank@2314 | 29 | #include "code/oopRecorder.hpp" |
stefank@2314 | 30 | #include "memory/allocation.inline.hpp" |
stefank@2314 | 31 | #include "oops/oop.inline.hpp" |
duke@435 | 32 | |
duke@435 | 33 | #ifdef ASSERT |
coleenp@4037 | 34 | template <class T> int ValueRecorder<T>::_find_index_calls = 0; |
coleenp@4037 | 35 | template <class T> int ValueRecorder<T>::_hit_indexes = 0; |
coleenp@4037 | 36 | template <class T> int ValueRecorder<T>::_missed_indexes = 0; |
duke@435 | 37 | #endif //ASSERT |
duke@435 | 38 | |
duke@435 | 39 | |
coleenp@4037 | 40 | template <class T> ValueRecorder<T>::ValueRecorder(Arena* arena) { |
duke@435 | 41 | _handles = NULL; |
duke@435 | 42 | _indexes = NULL; |
duke@435 | 43 | _arena = arena; |
duke@435 | 44 | _complete = false; |
duke@435 | 45 | } |
duke@435 | 46 | |
coleenp@4037 | 47 | template <class T> template <class X> ValueRecorder<T>::IndexCache<X>::IndexCache() { |
duke@435 | 48 | assert(first_index > 0, "initial zero state of cache must be invalid index"); |
duke@435 | 49 | Copy::zero_to_bytes(&_cache[0], sizeof(_cache)); |
duke@435 | 50 | } |
duke@435 | 51 | |
coleenp@4037 | 52 | template <class T> int ValueRecorder<T>::size() { |
duke@435 | 53 | _complete = true; |
duke@435 | 54 | if (_handles == NULL) return 0; |
coleenp@4037 | 55 | return _handles->length() * sizeof(T); |
duke@435 | 56 | } |
duke@435 | 57 | |
coleenp@4037 | 58 | template <class T> void ValueRecorder<T>::copy_values_to(nmethod* nm) { |
duke@435 | 59 | assert(_complete, "must be frozen"); |
duke@435 | 60 | maybe_initialize(); // get non-null handles, even if we have no oops |
coleenp@4037 | 61 | nm->copy_values(_handles); |
duke@435 | 62 | } |
duke@435 | 63 | |
coleenp@4037 | 64 | template <class T> void ValueRecorder<T>::maybe_initialize() { |
duke@435 | 65 | if (_handles == NULL) { |
duke@435 | 66 | if (_arena != NULL) { |
coleenp@4037 | 67 | _handles = new(_arena) GrowableArray<T>(_arena, 10, 0, 0); |
duke@435 | 68 | _no_finds = new(_arena) GrowableArray<int>( _arena, 10, 0, 0); |
duke@435 | 69 | } else { |
coleenp@4037 | 70 | _handles = new GrowableArray<T>(10, 0, 0); |
duke@435 | 71 | _no_finds = new GrowableArray<int>( 10, 0, 0); |
duke@435 | 72 | } |
duke@435 | 73 | } |
duke@435 | 74 | } |
duke@435 | 75 | |
duke@435 | 76 | |
coleenp@4037 | 77 | template <class T> T ValueRecorder<T>::at(int index) { |
duke@435 | 78 | // there is always a NULL virtually present as first object |
duke@435 | 79 | if (index == null_index) return NULL; |
duke@435 | 80 | return _handles->at(index - first_index); |
duke@435 | 81 | } |
duke@435 | 82 | |
duke@435 | 83 | |
coleenp@4037 | 84 | template <class T> int ValueRecorder<T>::add_handle(T h, bool make_findable) { |
duke@435 | 85 | assert(!_complete, "cannot allocate more elements after size query"); |
duke@435 | 86 | maybe_initialize(); |
duke@435 | 87 | // indexing uses 1 as an origin--0 means null |
duke@435 | 88 | int index = _handles->length() + first_index; |
duke@435 | 89 | _handles->append(h); |
duke@435 | 90 | |
duke@435 | 91 | // Support correct operation of find_index(). |
coleenp@4037 | 92 | assert(!(make_findable && !is_real(h)), "nulls are not findable"); |
duke@435 | 93 | if (make_findable) { |
duke@435 | 94 | // This index may be returned from find_index(). |
duke@435 | 95 | if (_indexes != NULL) { |
duke@435 | 96 | int* cloc = _indexes->cache_location(h); |
duke@435 | 97 | _indexes->set_cache_location_index(cloc, index); |
duke@435 | 98 | } else if (index == index_cache_threshold && _arena != NULL) { |
coleenp@4037 | 99 | _indexes = new(_arena) IndexCache<T>(); |
duke@435 | 100 | for (int i = 0; i < _handles->length(); i++) { |
duke@435 | 101 | // Load the cache with pre-existing elements. |
duke@435 | 102 | int index0 = i + first_index; |
duke@435 | 103 | if (_no_finds->contains(index0)) continue; |
duke@435 | 104 | int* cloc = _indexes->cache_location(_handles->at(i)); |
duke@435 | 105 | _indexes->set_cache_location_index(cloc, index0); |
duke@435 | 106 | } |
duke@435 | 107 | } |
coleenp@4037 | 108 | } else if (is_real(h)) { |
duke@435 | 109 | // Remember that this index is not to be returned from find_index(). |
duke@435 | 110 | // This case is rare, because most or all uses of allocate_index pass |
coleenp@4037 | 111 | // an argument of NULL or Universe::non_oop_word. |
duke@435 | 112 | // Thus, the expected length of _no_finds is zero. |
duke@435 | 113 | _no_finds->append(index); |
duke@435 | 114 | } |
duke@435 | 115 | |
duke@435 | 116 | return index; |
duke@435 | 117 | } |
duke@435 | 118 | |
duke@435 | 119 | |
coleenp@4037 | 120 | template <class T> int ValueRecorder<T>::maybe_find_index(T h) { |
duke@435 | 121 | debug_only(_find_index_calls++); |
duke@435 | 122 | assert(!_complete, "cannot allocate more elements after size query"); |
duke@435 | 123 | maybe_initialize(); |
duke@435 | 124 | if (h == NULL) return null_index; |
coleenp@4037 | 125 | assert(is_real(h), "must be valid"); |
duke@435 | 126 | int* cloc = (_indexes == NULL)? NULL: _indexes->cache_location(h); |
duke@435 | 127 | if (cloc != NULL) { |
duke@435 | 128 | int cindex = _indexes->cache_location_index(cloc); |
duke@435 | 129 | if (cindex == 0) { |
duke@435 | 130 | return -1; // We know this handle is completely new. |
duke@435 | 131 | } |
duke@435 | 132 | if (cindex >= first_index && _handles->at(cindex - first_index) == h) { |
duke@435 | 133 | debug_only(_hit_indexes++); |
duke@435 | 134 | return cindex; |
duke@435 | 135 | } |
duke@435 | 136 | if (!_indexes->cache_location_collision(cloc)) { |
duke@435 | 137 | return -1; // We know the current cache occupant is unique to that cloc. |
duke@435 | 138 | } |
duke@435 | 139 | } |
duke@435 | 140 | |
duke@435 | 141 | // Not found in cache, due to a cache collision. (Or, no cache at all.) |
duke@435 | 142 | // Do a linear search, most recent to oldest. |
duke@435 | 143 | for (int i = _handles->length() - 1; i >= 0; i--) { |
duke@435 | 144 | if (_handles->at(i) == h) { |
duke@435 | 145 | int findex = i + first_index; |
duke@435 | 146 | if (_no_finds->contains(findex)) continue; // oops; skip this one |
duke@435 | 147 | if (cloc != NULL) { |
duke@435 | 148 | _indexes->set_cache_location_index(cloc, findex); |
duke@435 | 149 | } |
duke@435 | 150 | debug_only(_missed_indexes++); |
duke@435 | 151 | return findex; |
duke@435 | 152 | } |
duke@435 | 153 | } |
duke@435 | 154 | return -1; |
duke@435 | 155 | } |
coleenp@4037 | 156 | |
coleenp@4037 | 157 | // Explicitly instantiate these types |
coleenp@4037 | 158 | template class ValueRecorder<Metadata*>; |
coleenp@4037 | 159 | template class ValueRecorder<jobject>; |