Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/prims/jvmtiCodeBlobEvents.cpp
41145 views
1
/*
2
* Copyright (c) 2003, 2020, 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 "code/codeBlob.hpp"
27
#include "code/codeCache.hpp"
28
#include "code/scopeDesc.hpp"
29
#include "code/vtableStubs.hpp"
30
#include "memory/allocation.inline.hpp"
31
#include "memory/resourceArea.hpp"
32
#include "oops/oop.inline.hpp"
33
#include "prims/jvmtiCodeBlobEvents.hpp"
34
#include "prims/jvmtiExport.hpp"
35
#include "prims/jvmtiThreadState.inline.hpp"
36
#include "runtime/handles.inline.hpp"
37
#include "runtime/safepointVerifiers.hpp"
38
#include "runtime/stubCodeGenerator.hpp"
39
#include "runtime/vmThread.hpp"
40
41
// Support class to collect a list of the non-nmethod CodeBlobs in
42
// the CodeCache.
43
//
44
// This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc
45
// describes a single CodeBlob in the CodeCache. Note that collection is
46
// done to a static list - this is because CodeCache::blobs_do is defined
47
// as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires
48
// a C or static method.
49
//
50
// Usage :-
51
//
52
// CodeBlobCollector collector;
53
//
54
// collector.collect();
55
// JvmtiCodeBlobDesc* blob = collector.first();
56
// while (blob != NULL) {
57
// :
58
// blob = collector.next();
59
// }
60
//
61
62
class CodeBlobCollector : StackObj {
63
private:
64
GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected blobs
65
int _pos; // iterator position
66
67
// used during a collection
68
static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs;
69
static void do_blob(CodeBlob* cb);
70
static void do_vtable_stub(VtableStub* vs);
71
public:
72
CodeBlobCollector() {
73
_code_blobs = NULL;
74
_pos = -1;
75
}
76
~CodeBlobCollector() {
77
if (_code_blobs != NULL) {
78
for (int i=0; i<_code_blobs->length(); i++) {
79
FreeHeap(_code_blobs->at(i));
80
}
81
delete _code_blobs;
82
}
83
}
84
85
// collect list of code blobs in the cache
86
void collect();
87
88
// iteration support - return first code blob
89
JvmtiCodeBlobDesc* first() {
90
assert(_code_blobs != NULL, "not collected");
91
if (_code_blobs->length() == 0) {
92
return NULL;
93
}
94
_pos = 0;
95
return _code_blobs->at(0);
96
}
97
98
// iteration support - return next code blob
99
JvmtiCodeBlobDesc* next() {
100
assert(_pos >= 0, "iteration not started");
101
if (_pos+1 >= _code_blobs->length()) {
102
return NULL;
103
}
104
return _code_blobs->at(++_pos);
105
}
106
107
};
108
109
// used during collection
110
GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs;
111
112
113
// called for each CodeBlob in the CodeCache
114
//
115
// This function filters out nmethods as it is only interested in
116
// other CodeBlobs. This function also filters out CodeBlobs that have
117
// a duplicate starting address as previous blobs. This is needed to
118
// handle the case where multiple stubs are generated into a single
119
// BufferBlob.
120
121
void CodeBlobCollector::do_blob(CodeBlob* cb) {
122
123
// ignore nmethods
124
if (cb->is_nmethod()) {
125
return;
126
}
127
// exclude VtableStubs, which are processed separately
128
if (cb->is_buffer_blob() && strcmp(cb->name(), "vtable chunks") == 0) {
129
return;
130
}
131
132
// check if this starting address has been seen already - the
133
// assumption is that stubs are inserted into the list before the
134
// enclosing BufferBlobs.
135
address addr = cb->code_begin();
136
for (int i=0; i<_global_code_blobs->length(); i++) {
137
JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i);
138
if (addr == scb->code_begin()) {
139
return;
140
}
141
}
142
143
// record the CodeBlob details as a JvmtiCodeBlobDesc
144
JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end());
145
_global_code_blobs->append(scb);
146
}
147
148
// called for each VtableStub in VtableStubs
149
150
void CodeBlobCollector::do_vtable_stub(VtableStub* vs) {
151
JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(vs->is_vtable_stub() ? "vtable stub" : "itable stub",
152
vs->code_begin(), vs->code_end());
153
_global_code_blobs->append(scb);
154
}
155
156
// collects a list of CodeBlobs in the CodeCache.
157
//
158
// The created list is growable array of JvmtiCodeBlobDesc - each one describes
159
// a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do
160
// requires a a C or static function so we can't use an instance function. This
161
// isn't a problem as the iteration is serial anyway as we need the CodeCache_lock
162
// to iterate over the code cache.
163
//
164
// Note that the CodeBlobs in the CodeCache will include BufferBlobs that may
165
// contain multiple stubs. As a profiler is interested in the stubs rather than
166
// the enclosing container we first iterate over the stub code descriptors so
167
// that the stubs go into the list first. do_blob will then filter out the
168
// enclosing blobs if the starting address of the enclosing blobs matches the
169
// starting address of first stub generated in the enclosing blob.
170
171
void CodeBlobCollector::collect() {
172
assert_locked_or_safepoint(CodeCache_lock);
173
assert(_global_code_blobs == NULL, "checking");
174
175
// create the global list
176
_global_code_blobs = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<JvmtiCodeBlobDesc*>(50, mtServiceability);
177
178
// iterate over the stub code descriptors and put them in the list first.
179
for (StubCodeDesc* desc = StubCodeDesc::first(); desc != NULL; desc = StubCodeDesc::next(desc)) {
180
_global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
181
}
182
183
// Vtable stubs are not described with StubCodeDesc,
184
// process them separately
185
VtableStubs::vtable_stub_do(do_vtable_stub);
186
187
// next iterate over all the non-nmethod code blobs and add them to
188
// the list - as noted above this will filter out duplicates and
189
// enclosing blobs.
190
CodeCache::blobs_do(do_blob);
191
192
// make the global list the instance list so that it can be used
193
// for other iterations.
194
_code_blobs = _global_code_blobs;
195
_global_code_blobs = NULL;
196
}
197
198
199
// Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.
200
201
jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {
202
CodeBlobCollector collector;
203
204
// First collect all the code blobs. This has to be done in a
205
// single pass over the code cache with CodeCache_lock held because
206
// there isn't any safe way to iterate over regular CodeBlobs since
207
// they can be freed at any point.
208
{
209
MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
210
collector.collect();
211
}
212
213
// iterate over the collected list and post an event for each blob
214
JvmtiCodeBlobDesc* blob = collector.first();
215
while (blob != NULL) {
216
JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());
217
blob = collector.next();
218
}
219
return JVMTI_ERROR_NONE;
220
}
221
222
223
// Generate a COMPILED_METHOD_LOAD event for each nnmethod
224
jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {
225
JavaThread* java_thread = JavaThread::current();
226
JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
227
{
228
NoSafepointVerifier nsv; // safepoints are not safe while collecting methods to post.
229
{
230
// Walk the CodeCache notifying for live nmethods, don't release the CodeCache_lock
231
// because the sweeper may be running concurrently.
232
// Save events to the queue for posting outside the CodeCache_lock.
233
MutexLocker mu(java_thread, CodeCache_lock, Mutex::_no_safepoint_check_flag);
234
// Iterate over non-profiled and profiled nmethods
235
NMethodIterator iter(NMethodIterator::only_alive_and_not_unloading);
236
while(iter.next()) {
237
nmethod* current = iter.method();
238
current->post_compiled_method_load_event(state);
239
}
240
}
241
242
// Enter nmethod barrier code if present outside CodeCache_lock
243
state->run_nmethod_entry_barriers();
244
}
245
246
// Now post all the events outside the CodeCache_lock.
247
// If there's a safepoint, the queued events will be kept alive.
248
// Adding these events to the service thread to post is something that
249
// should work, but the service thread doesn't keep up in stress scenarios and
250
// the os eventually kills the process with OOM.
251
// We want this thread to wait until the events are all posted.
252
state->post_events(env);
253
return JVMTI_ERROR_NONE;
254
}
255
256
257
// create a C-heap allocated address location map for an nmethod
258
void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,
259
jvmtiAddrLocationMap** map_ptr,
260
jint *map_length_ptr)
261
{
262
ResourceMark rm;
263
jvmtiAddrLocationMap* map = NULL;
264
jint map_length = 0;
265
266
267
// Generate line numbers using PcDesc and ScopeDesc info
268
methodHandle mh(Thread::current(), nm->method());
269
270
if (!mh->is_native()) {
271
PcDesc *pcd;
272
int pcds_in_method;
273
274
pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());
275
map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method, mtInternal);
276
277
address scopes_data = nm->scopes_data_begin();
278
for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {
279
ScopeDesc sc0(nm, pcd, true);
280
ScopeDesc *sd = &sc0;
281
while( !sd->is_top() ) { sd = sd->sender(); }
282
int bci = sd->bci();
283
if (bci >= 0) {
284
assert(map_length < pcds_in_method, "checking");
285
map[map_length].start_address = (const void*)pcd->real_pc(nm);
286
map[map_length].location = bci;
287
++map_length;
288
}
289
}
290
}
291
292
*map_ptr = map;
293
*map_length_ptr = map_length;
294
}
295
296