src/share/vm/code/oopRecorder.hpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_CODE_OOPRECORDER_HPP
aoqi@0 26 #define SHARE_VM_CODE_OOPRECORDER_HPP
aoqi@0 27
aoqi@0 28 #include "memory/universe.hpp"
aoqi@0 29 #include "runtime/handles.hpp"
aoqi@0 30 #include "utilities/growableArray.hpp"
aoqi@0 31
aoqi@0 32 // Recording and retrieval of either oop relocations or metadata in compiled code.
aoqi@0 33
aoqi@0 34 class CodeBlob;
aoqi@0 35
aoqi@0 36 template <class T> class ValueRecorder : public StackObj {
aoqi@0 37 public:
aoqi@0 38 // A two-way mapping from positive indexes to oop handles.
aoqi@0 39 // The zero index is reserved for a constant (sharable) null.
aoqi@0 40 // Indexes may not be negative.
aoqi@0 41
aoqi@0 42 // Use the given arena to manage storage, if not NULL.
aoqi@0 43 // By default, uses the current ResourceArea.
aoqi@0 44 ValueRecorder(Arena* arena = NULL);
aoqi@0 45
aoqi@0 46 // Generate a new index on which nmethod::oop_addr_at will work.
aoqi@0 47 // allocate_index and find_index never return the same index,
aoqi@0 48 // and allocate_index never returns the same index twice.
aoqi@0 49 // In fact, two successive calls to allocate_index return successive ints.
aoqi@0 50 int allocate_index(T h) {
aoqi@0 51 return add_handle(h, false);
aoqi@0 52 }
aoqi@0 53
aoqi@0 54 // For a given jobject or Metadata*, this will return the same index
aoqi@0 55 // repeatedly. The index can later be given to nmethod::oop_at or
aoqi@0 56 // metadata_at to retrieve the oop.
aoqi@0 57 // However, the oop must not be changed via nmethod::oop_addr_at.
aoqi@0 58 int find_index(T h) {
aoqi@0 59 int index = maybe_find_index(h);
aoqi@0 60 if (index < 0) { // previously unallocated
aoqi@0 61 index = add_handle(h, true);
aoqi@0 62 }
aoqi@0 63 return index;
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 // returns the size of the generated oop/metadata table, for sizing the
aoqi@0 67 // CodeBlob. Must be called after all oops are allocated!
aoqi@0 68 int size();
aoqi@0 69
aoqi@0 70 // Retrieve the value at a given index.
aoqi@0 71 T at(int index);
aoqi@0 72
aoqi@0 73 int count() {
aoqi@0 74 if (_handles == NULL) return 0;
aoqi@0 75 // there is always a NULL virtually present as first object
aoqi@0 76 return _handles->length() + first_index;
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 // Helper function; returns false for NULL or Universe::non_oop_word().
aoqi@0 80 bool is_real(T h) {
aoqi@0 81 return h != NULL && h != (T)Universe::non_oop_word();
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 // copy the generated table to nmethod
aoqi@0 85 void copy_values_to(nmethod* nm);
aoqi@0 86
aoqi@0 87 bool is_unused() { return _handles == NULL && !_complete; }
aoqi@0 88 #ifdef ASSERT
aoqi@0 89 bool is_complete() { return _complete; }
aoqi@0 90 #endif
aoqi@0 91
aoqi@0 92 private:
aoqi@0 93 // variant of find_index which does not allocate if not found (yields -1)
aoqi@0 94 int maybe_find_index(T h);
aoqi@0 95
aoqi@0 96 // leaky hash table of handle => index, to help detect duplicate insertion
aoqi@0 97 template <class X> class IndexCache : public ResourceObj {
aoqi@0 98 // This class is only used by the ValueRecorder class.
aoqi@0 99 friend class ValueRecorder;
aoqi@0 100 enum {
aoqi@0 101 _log_cache_size = 9,
aoqi@0 102 _cache_size = (1<<_log_cache_size),
aoqi@0 103 // Index entries are ints. The LSBit is a collision indicator.
aoqi@0 104 _collision_bit_shift = 0,
aoqi@0 105 _collision_bit = 1,
aoqi@0 106 _index_shift = _collision_bit_shift+1
aoqi@0 107 };
aoqi@0 108 int _cache[_cache_size];
aoqi@0 109 static juint cache_index(X handle) {
aoqi@0 110 juint ci = (int) (intptr_t) handle;
aoqi@0 111 ci ^= ci >> (BitsPerByte*2);
aoqi@0 112 ci += ci >> (BitsPerByte*1);
aoqi@0 113 return ci & (_cache_size-1);
aoqi@0 114 }
aoqi@0 115 int* cache_location(X handle) {
aoqi@0 116 return &_cache[ cache_index(handle) ];
aoqi@0 117 }
aoqi@0 118 static bool cache_location_collision(int* cloc) {
aoqi@0 119 return ((*cloc) & _collision_bit) != 0;
aoqi@0 120 }
aoqi@0 121 static int cache_location_index(int* cloc) {
aoqi@0 122 return (*cloc) >> _index_shift;
aoqi@0 123 }
aoqi@0 124 static void set_cache_location_index(int* cloc, int index) {
aoqi@0 125 int cval0 = (*cloc);
aoqi@0 126 int cval1 = (index << _index_shift);
aoqi@0 127 if (cval0 != 0 && cval1 != cval0) cval1 += _collision_bit;
aoqi@0 128 (*cloc) = cval1;
aoqi@0 129 }
aoqi@0 130 IndexCache();
aoqi@0 131 };
aoqi@0 132
aoqi@0 133 void maybe_initialize();
aoqi@0 134 int add_handle(T h, bool make_findable);
aoqi@0 135
aoqi@0 136 enum { null_index = 0, first_index = 1, index_cache_threshold = 20 };
aoqi@0 137
aoqi@0 138 GrowableArray<T>* _handles; // ordered list (first is always NULL)
aoqi@0 139 GrowableArray<int>* _no_finds; // all unfindable indexes; usually empty
aoqi@0 140 IndexCache<T>* _indexes; // map: handle -> its probable index
aoqi@0 141 Arena* _arena;
aoqi@0 142 bool _complete;
aoqi@0 143
aoqi@0 144 #ifdef ASSERT
aoqi@0 145 static int _find_index_calls, _hit_indexes, _missed_indexes;
aoqi@0 146 #endif
aoqi@0 147 };
aoqi@0 148
aoqi@0 149 class OopRecorder : public ResourceObj {
aoqi@0 150 private:
aoqi@0 151 ValueRecorder<jobject> _oops;
aoqi@0 152 ValueRecorder<Metadata*> _metadata;
aoqi@0 153 public:
aoqi@0 154 OopRecorder(Arena* arena = NULL): _oops(arena), _metadata(arena) {}
aoqi@0 155
aoqi@0 156 int allocate_oop_index(jobject h) {
aoqi@0 157 return _oops.allocate_index(h);
aoqi@0 158 }
aoqi@0 159 int find_index(jobject h) {
aoqi@0 160 return _oops.find_index(h);
aoqi@0 161 }
aoqi@0 162 jobject oop_at(int index) {
aoqi@0 163 return _oops.at(index);
aoqi@0 164 }
aoqi@0 165 int oop_size() {
aoqi@0 166 return _oops.size();
aoqi@0 167 }
aoqi@0 168 int oop_count() {
aoqi@0 169 return _oops.count();
aoqi@0 170 }
aoqi@0 171 bool is_real(jobject h) {
aoqi@0 172 return _oops.is_real(h);
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 int allocate_metadata_index(Metadata* oop) {
aoqi@0 176 return _metadata.allocate_index(oop);
aoqi@0 177 }
aoqi@0 178 int find_index(Metadata* h) {
aoqi@0 179 return _metadata.find_index(h);
aoqi@0 180 }
aoqi@0 181 Metadata* metadata_at(int index) {
aoqi@0 182 return _metadata.at(index);
aoqi@0 183 }
aoqi@0 184 int metadata_size() {
aoqi@0 185 return _metadata.size();
aoqi@0 186 }
aoqi@0 187 int metadata_count() {
aoqi@0 188 return _metadata.count();
aoqi@0 189 }
aoqi@0 190 bool is_real(Metadata* h) {
aoqi@0 191 return _metadata.is_real(h);
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 bool is_unused() {
aoqi@0 195 return _oops.is_unused() && _metadata.is_unused();
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 void freeze() {
aoqi@0 199 _oops.size();
aoqi@0 200 _metadata.size();
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 void copy_values_to(nmethod* nm) {
aoqi@0 204 if (!_oops.is_unused()) {
aoqi@0 205 _oops.copy_values_to(nm);
aoqi@0 206 }
aoqi@0 207 if (!_metadata.is_unused()) {
aoqi@0 208 _metadata.copy_values_to(nm);
aoqi@0 209 }
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 #ifdef ASSERT
aoqi@0 213 bool is_complete() {
aoqi@0 214 assert(_oops.is_complete() == _metadata.is_complete(), "must agree");
aoqi@0 215 return _oops.is_complete();
aoqi@0 216 }
aoqi@0 217 #endif
aoqi@0 218 };
aoqi@0 219
aoqi@0 220
aoqi@0 221 #endif // SHARE_VM_CODE_OOPRECORDER_HPP

mercurial