Fri, 28 Sep 2012 10:16:29 -0700
Merge
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 | #ifndef SHARE_VM_CODE_OOPRECORDER_HPP |
stefank@2314 | 26 | #define SHARE_VM_CODE_OOPRECORDER_HPP |
stefank@2314 | 27 | |
coleenp@4037 | 28 | #include "memory/universe.hpp" |
stefank@2314 | 29 | #include "runtime/handles.hpp" |
stefank@2314 | 30 | #include "utilities/growableArray.hpp" |
stefank@2314 | 31 | |
coleenp@4037 | 32 | // Recording and retrieval of either oop relocations or metadata in compiled code. |
duke@435 | 33 | |
duke@435 | 34 | class CodeBlob; |
duke@435 | 35 | |
coleenp@4037 | 36 | template <class T> class ValueRecorder : public StackObj { |
duke@435 | 37 | public: |
duke@435 | 38 | // A two-way mapping from positive indexes to oop handles. |
duke@435 | 39 | // The zero index is reserved for a constant (sharable) null. |
duke@435 | 40 | // Indexes may not be negative. |
duke@435 | 41 | |
duke@435 | 42 | // Use the given arena to manage storage, if not NULL. |
duke@435 | 43 | // By default, uses the current ResourceArea. |
coleenp@4037 | 44 | ValueRecorder(Arena* arena = NULL); |
duke@435 | 45 | |
coleenp@4037 | 46 | // Generate a new index on which nmethod::oop_addr_at will work. |
duke@435 | 47 | // allocate_index and find_index never return the same index, |
duke@435 | 48 | // and allocate_index never returns the same index twice. |
duke@435 | 49 | // In fact, two successive calls to allocate_index return successive ints. |
coleenp@4037 | 50 | int allocate_index(T h) { |
duke@435 | 51 | return add_handle(h, false); |
duke@435 | 52 | } |
duke@435 | 53 | |
coleenp@4037 | 54 | // For a given jobject or Metadata*, this will return the same index |
coleenp@4037 | 55 | // repeatedly. The index can later be given to nmethod::oop_at or |
coleenp@4037 | 56 | // metadata_at to retrieve the oop. |
coleenp@4037 | 57 | // However, the oop must not be changed via nmethod::oop_addr_at. |
coleenp@4037 | 58 | int find_index(T h) { |
duke@435 | 59 | int index = maybe_find_index(h); |
duke@435 | 60 | if (index < 0) { // previously unallocated |
duke@435 | 61 | index = add_handle(h, true); |
duke@435 | 62 | } |
duke@435 | 63 | return index; |
duke@435 | 64 | } |
duke@435 | 65 | |
coleenp@4037 | 66 | // returns the size of the generated oop/metadata table, for sizing the |
coleenp@4037 | 67 | // CodeBlob. Must be called after all oops are allocated! |
coleenp@4037 | 68 | int size(); |
duke@435 | 69 | |
coleenp@4037 | 70 | // Retrieve the value at a given index. |
coleenp@4037 | 71 | T at(int index); |
duke@435 | 72 | |
coleenp@4037 | 73 | int count() { |
coleenp@4037 | 74 | if (_handles == NULL) return 0; |
duke@435 | 75 | // there is always a NULL virtually present as first object |
duke@435 | 76 | return _handles->length() + first_index; |
duke@435 | 77 | } |
duke@435 | 78 | |
coleenp@4037 | 79 | // Helper function; returns false for NULL or Universe::non_oop_word(). |
coleenp@4037 | 80 | bool is_real(T h) { |
coleenp@4037 | 81 | return h != NULL && h != (T)Universe::non_oop_word(); |
coleenp@4037 | 82 | } |
coleenp@4037 | 83 | |
coleenp@4037 | 84 | // copy the generated table to nmethod |
coleenp@4037 | 85 | void copy_values_to(nmethod* nm); |
duke@435 | 86 | |
duke@435 | 87 | bool is_unused() { return _handles == NULL && !_complete; } |
duke@435 | 88 | #ifdef ASSERT |
duke@435 | 89 | bool is_complete() { return _complete; } |
duke@435 | 90 | #endif |
duke@435 | 91 | |
duke@435 | 92 | private: |
coleenp@4037 | 93 | // variant of find_index which does not allocate if not found (yields -1) |
coleenp@4037 | 94 | int maybe_find_index(T h); |
coleenp@4037 | 95 | |
duke@435 | 96 | // leaky hash table of handle => index, to help detect duplicate insertion |
coleenp@4037 | 97 | template <class X> class IndexCache : public ResourceObj { |
coleenp@4037 | 98 | // This class is only used by the ValueRecorder class. |
coleenp@4037 | 99 | friend class ValueRecorder; |
duke@435 | 100 | enum { |
duke@435 | 101 | _log_cache_size = 9, |
duke@435 | 102 | _cache_size = (1<<_log_cache_size), |
duke@435 | 103 | // Index entries are ints. The LSBit is a collision indicator. |
duke@435 | 104 | _collision_bit_shift = 0, |
duke@435 | 105 | _collision_bit = 1, |
duke@435 | 106 | _index_shift = _collision_bit_shift+1 |
duke@435 | 107 | }; |
duke@435 | 108 | int _cache[_cache_size]; |
coleenp@4037 | 109 | static juint cache_index(X handle) { |
duke@435 | 110 | juint ci = (int) (intptr_t) handle; |
duke@435 | 111 | ci ^= ci >> (BitsPerByte*2); |
duke@435 | 112 | ci += ci >> (BitsPerByte*1); |
duke@435 | 113 | return ci & (_cache_size-1); |
duke@435 | 114 | } |
coleenp@4037 | 115 | int* cache_location(X handle) { |
duke@435 | 116 | return &_cache[ cache_index(handle) ]; |
duke@435 | 117 | } |
duke@435 | 118 | static bool cache_location_collision(int* cloc) { |
duke@435 | 119 | return ((*cloc) & _collision_bit) != 0; |
duke@435 | 120 | } |
duke@435 | 121 | static int cache_location_index(int* cloc) { |
duke@435 | 122 | return (*cloc) >> _index_shift; |
duke@435 | 123 | } |
duke@435 | 124 | static void set_cache_location_index(int* cloc, int index) { |
duke@435 | 125 | int cval0 = (*cloc); |
duke@435 | 126 | int cval1 = (index << _index_shift); |
duke@435 | 127 | if (cval0 != 0 && cval1 != cval0) cval1 += _collision_bit; |
duke@435 | 128 | (*cloc) = cval1; |
duke@435 | 129 | } |
duke@435 | 130 | IndexCache(); |
duke@435 | 131 | }; |
duke@435 | 132 | |
duke@435 | 133 | void maybe_initialize(); |
coleenp@4037 | 134 | int add_handle(T h, bool make_findable); |
duke@435 | 135 | |
duke@435 | 136 | enum { null_index = 0, first_index = 1, index_cache_threshold = 20 }; |
duke@435 | 137 | |
coleenp@4037 | 138 | GrowableArray<T>* _handles; // ordered list (first is always NULL) |
duke@435 | 139 | GrowableArray<int>* _no_finds; // all unfindable indexes; usually empty |
coleenp@4037 | 140 | IndexCache<T>* _indexes; // map: handle -> its probable index |
duke@435 | 141 | Arena* _arena; |
duke@435 | 142 | bool _complete; |
duke@435 | 143 | |
duke@435 | 144 | #ifdef ASSERT |
duke@435 | 145 | static int _find_index_calls, _hit_indexes, _missed_indexes; |
duke@435 | 146 | #endif |
duke@435 | 147 | }; |
stefank@2314 | 148 | |
coleenp@4037 | 149 | class OopRecorder : public ResourceObj { |
coleenp@4037 | 150 | private: |
coleenp@4037 | 151 | ValueRecorder<jobject> _oops; |
coleenp@4037 | 152 | ValueRecorder<Metadata*> _metadata; |
coleenp@4037 | 153 | public: |
coleenp@4037 | 154 | OopRecorder(Arena* arena = NULL): _oops(arena), _metadata(arena) {} |
coleenp@4037 | 155 | |
coleenp@4037 | 156 | int allocate_oop_index(jobject h) { |
coleenp@4037 | 157 | return _oops.allocate_index(h); |
coleenp@4037 | 158 | } |
coleenp@4037 | 159 | int find_index(jobject h) { |
coleenp@4037 | 160 | return _oops.find_index(h); |
coleenp@4037 | 161 | } |
coleenp@4037 | 162 | jobject oop_at(int index) { |
coleenp@4037 | 163 | return _oops.at(index); |
coleenp@4037 | 164 | } |
coleenp@4037 | 165 | int oop_size() { |
coleenp@4037 | 166 | return _oops.size(); |
coleenp@4037 | 167 | } |
coleenp@4037 | 168 | int oop_count() { |
coleenp@4037 | 169 | return _oops.count(); |
coleenp@4037 | 170 | } |
coleenp@4037 | 171 | bool is_real(jobject h) { |
coleenp@4037 | 172 | return _oops.is_real(h); |
coleenp@4037 | 173 | } |
coleenp@4037 | 174 | |
coleenp@4037 | 175 | int allocate_metadata_index(Metadata* oop) { |
coleenp@4037 | 176 | return _metadata.allocate_index(oop); |
coleenp@4037 | 177 | } |
coleenp@4037 | 178 | int find_index(Metadata* h) { |
coleenp@4037 | 179 | return _metadata.find_index(h); |
coleenp@4037 | 180 | } |
coleenp@4037 | 181 | Metadata* metadata_at(int index) { |
coleenp@4037 | 182 | return _metadata.at(index); |
coleenp@4037 | 183 | } |
coleenp@4037 | 184 | int metadata_size() { |
coleenp@4037 | 185 | return _metadata.size(); |
coleenp@4037 | 186 | } |
coleenp@4037 | 187 | int metadata_count() { |
coleenp@4037 | 188 | return _metadata.count(); |
coleenp@4037 | 189 | } |
coleenp@4037 | 190 | bool is_real(Metadata* h) { |
coleenp@4037 | 191 | return _metadata.is_real(h); |
coleenp@4037 | 192 | } |
coleenp@4037 | 193 | |
coleenp@4037 | 194 | bool is_unused() { |
coleenp@4037 | 195 | return _oops.is_unused() && _metadata.is_unused(); |
coleenp@4037 | 196 | } |
coleenp@4037 | 197 | |
coleenp@4037 | 198 | void freeze() { |
coleenp@4037 | 199 | _oops.size(); |
coleenp@4037 | 200 | _metadata.size(); |
coleenp@4037 | 201 | } |
coleenp@4037 | 202 | |
coleenp@4037 | 203 | void copy_values_to(nmethod* nm) { |
coleenp@4037 | 204 | if (!_oops.is_unused()) { |
coleenp@4037 | 205 | _oops.copy_values_to(nm); |
coleenp@4037 | 206 | } |
coleenp@4037 | 207 | if (!_metadata.is_unused()) { |
coleenp@4037 | 208 | _metadata.copy_values_to(nm); |
coleenp@4037 | 209 | } |
coleenp@4037 | 210 | } |
coleenp@4037 | 211 | |
coleenp@4037 | 212 | #ifdef ASSERT |
coleenp@4037 | 213 | bool is_complete() { |
coleenp@4037 | 214 | assert(_oops.is_complete() == _metadata.is_complete(), "must agree"); |
coleenp@4037 | 215 | return _oops.is_complete(); |
coleenp@4037 | 216 | } |
coleenp@4037 | 217 | #endif |
coleenp@4037 | 218 | }; |
coleenp@4037 | 219 | |
coleenp@4037 | 220 | |
stefank@2314 | 221 | #endif // SHARE_VM_CODE_OOPRECORDER_HPP |