Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/code/oopRecorder.cpp
40931 views
1
/*
2
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "ci/ciEnv.hpp"
27
#include "ci/ciInstance.hpp"
28
#include "ci/ciMetadata.hpp"
29
#include "code/oopRecorder.inline.hpp"
30
#include "gc/shared/collectedHeap.hpp"
31
#include "memory/allocation.inline.hpp"
32
#include "oops/oop.inline.hpp"
33
#include "runtime/jniHandles.inline.hpp"
34
#include "utilities/copy.hpp"
35
36
#ifdef ASSERT
37
template <class T> int ValueRecorder<T>::_find_index_calls = 0;
38
template <class T> int ValueRecorder<T>::_hit_indexes = 0;
39
template <class T> int ValueRecorder<T>::_missed_indexes = 0;
40
#endif //ASSERT
41
42
43
template <class T> ValueRecorder<T>::ValueRecorder(Arena* arena) {
44
_handles = NULL;
45
_indexes = NULL;
46
_arena = arena;
47
_complete = false;
48
}
49
50
template <class T> template <class X> ValueRecorder<T>::IndexCache<X>::IndexCache() {
51
assert(first_index > 0, "initial zero state of cache must be invalid index");
52
Copy::zero_to_bytes(&_cache[0], sizeof(_cache));
53
}
54
55
template <class T> int ValueRecorder<T>::size() {
56
_complete = true;
57
if (_handles == NULL) return 0;
58
return _handles->length() * sizeof(T);
59
}
60
61
template <class T> void ValueRecorder<T>::copy_values_to(nmethod* nm) {
62
assert(_complete, "must be frozen");
63
maybe_initialize(); // get non-null handles, even if we have no oops
64
nm->copy_values(_handles);
65
}
66
67
template <class T> void ValueRecorder<T>::maybe_initialize() {
68
if (_handles == NULL) {
69
if (_arena != NULL) {
70
_handles = new(_arena) GrowableArray<T>(_arena, 10, 0, 0);
71
_no_finds = new(_arena) GrowableArray<int>( _arena, 10, 0, 0);
72
} else {
73
_handles = new GrowableArray<T>(10, 0, 0);
74
_no_finds = new GrowableArray<int>( 10, 0, 0);
75
}
76
}
77
}
78
79
80
template <class T> T ValueRecorder<T>::at(int index) {
81
// there is always a NULL virtually present as first object
82
if (index == null_index) return NULL;
83
return _handles->at(index - first_index);
84
}
85
86
87
template <class T> int ValueRecorder<T>::add_handle(T h, bool make_findable) {
88
assert(!_complete, "cannot allocate more elements after size query");
89
maybe_initialize();
90
// indexing uses 1 as an origin--0 means null
91
int index = _handles->length() + first_index;
92
_handles->append(h);
93
94
// Support correct operation of find_index().
95
assert(!(make_findable && !is_real(h)), "nulls are not findable");
96
if (make_findable) {
97
// This index may be returned from find_index().
98
if (_indexes != NULL) {
99
int* cloc = _indexes->cache_location(h);
100
_indexes->set_cache_location_index(cloc, index);
101
} else if (index == index_cache_threshold && _arena != NULL) {
102
_indexes = new(_arena) IndexCache<T>();
103
for (int i = 0; i < _handles->length(); i++) {
104
// Load the cache with pre-existing elements.
105
int index0 = i + first_index;
106
if (_no_finds->contains(index0)) continue;
107
int* cloc = _indexes->cache_location(_handles->at(i));
108
_indexes->set_cache_location_index(cloc, index0);
109
}
110
}
111
} else if (is_real(h)) {
112
// Remember that this index is not to be returned from find_index().
113
// This case is rare, because most or all uses of allocate_index pass
114
// an argument of NULL or Universe::non_oop_word.
115
// Thus, the expected length of _no_finds is zero.
116
_no_finds->append(index);
117
}
118
119
return index;
120
}
121
122
123
template <class T> int ValueRecorder<T>::maybe_find_index(T h) {
124
debug_only(_find_index_calls++);
125
assert(!_complete, "cannot allocate more elements after size query");
126
maybe_initialize();
127
if (h == NULL) return null_index;
128
assert(is_real(h), "must be valid");
129
int* cloc = (_indexes == NULL)? NULL: _indexes->cache_location(h);
130
if (cloc != NULL) {
131
int cindex = _indexes->cache_location_index(cloc);
132
if (cindex == 0) {
133
return -1; // We know this handle is completely new.
134
}
135
if (cindex >= first_index && _handles->at(cindex - first_index) == h) {
136
debug_only(_hit_indexes++);
137
return cindex;
138
}
139
if (!_indexes->cache_location_collision(cloc)) {
140
return -1; // We know the current cache occupant is unique to that cloc.
141
}
142
}
143
144
// Not found in cache, due to a cache collision. (Or, no cache at all.)
145
// Do a linear search, most recent to oldest.
146
for (int i = _handles->length() - 1; i >= 0; i--) {
147
if (_handles->at(i) == h) {
148
int findex = i + first_index;
149
if (_no_finds->contains(findex)) continue; // oops; skip this one
150
if (cloc != NULL) {
151
_indexes->set_cache_location_index(cloc, findex);
152
}
153
debug_only(_missed_indexes++);
154
return findex;
155
}
156
}
157
return -1;
158
}
159
160
// Explicitly instantiate these types
161
template class ValueRecorder<Metadata*>;
162
template class ValueRecorder<jobject>;
163
164
oop ObjectLookup::ObjectEntry::oop_value() const { return JNIHandles::resolve(_value); }
165
166
ObjectLookup::ObjectLookup(): _values(4), _gc_count(Universe::heap()->total_collections()) {}
167
168
void ObjectLookup::maybe_resort() {
169
// The values are kept sorted by address which may be invalidated
170
// after a GC, so resort if a GC has occurred since last time.
171
if (_gc_count != Universe::heap()->total_collections()) {
172
_gc_count = Universe::heap()->total_collections();
173
_values.sort(sort_by_address);
174
}
175
}
176
177
int ObjectLookup::sort_by_address(oop a, oop b) {
178
// oopDesc::compare returns the opposite of what this function returned
179
return -(oopDesc::compare(a, b));
180
}
181
182
int ObjectLookup::sort_by_address(ObjectEntry* a, ObjectEntry* b) {
183
return sort_by_address(a->oop_value(), b->oop_value());
184
}
185
186
int ObjectLookup::sort_oop_by_address(oop const& a, ObjectEntry const& b) {
187
return sort_by_address(a, b.oop_value());
188
}
189
190
int ObjectLookup::find_index(jobject handle, OopRecorder* oop_recorder) {
191
if (handle == NULL) {
192
return 0;
193
}
194
oop object = JNIHandles::resolve(handle);
195
maybe_resort();
196
bool found;
197
int location = _values.find_sorted<oop, sort_oop_by_address>(object, found);
198
if (!found) {
199
jobject handle = JNIHandles::make_local(object);
200
ObjectEntry r(handle, oop_recorder->allocate_oop_index(handle));
201
_values.insert_before(location, r);
202
return r.index();
203
}
204
return _values.at(location).index();
205
}
206
207