author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 59259 | 127ca611f19b |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
53418
bc2bb4eee477
8217442: Optimize native accesses to String.value
redestad
parents:
51467
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 "classfile/javaClasses.inline.hpp" |
|
27 |
#include "classfile/modules.hpp" |
|
28 |
#include "classfile/symbolTable.hpp" |
|
29 |
#include "classfile/systemDictionary.hpp" |
|
30 |
#include "classfile/vmSymbols.hpp" |
|
31 |
#include "jfr/jni/jfrJavaCall.hpp" |
|
32 |
#include "jfr/jni/jfrJavaSupport.hpp" |
|
33 |
#include "jfr/support/jfrThreadId.hpp" |
|
34 |
#include "logging/log.hpp" |
|
35 |
#include "memory/resourceArea.hpp" |
|
36 |
#include "oops/instanceOop.hpp" |
|
37 |
#include "oops/oop.inline.hpp" |
|
38 |
#include "oops/objArrayKlass.hpp" |
|
39 |
#include "oops/objArrayOop.inline.hpp" |
|
40 |
#include "runtime/handles.inline.hpp" |
|
51467
12997ebbc0d8
8209647: constantPoolHandle::constantPoolHandle(ConstantPool*) when precompiled header is disabled
iklam
parents:
50388
diff
changeset
|
41 |
#include "runtime/fieldDescriptor.inline.hpp" |
50113 | 42 |
#include "runtime/java.hpp" |
43 |
#include "runtime/jniHandles.inline.hpp" |
|
58863 | 44 |
#include "runtime/semaphore.inline.hpp" |
50113 | 45 |
#include "runtime/synchronizer.hpp" |
46 |
#include "runtime/thread.inline.hpp" |
|
47 |
#include "runtime/threadSMR.hpp" |
|
58863 | 48 |
#include "utilities/growableArray.hpp" |
50113 | 49 |
|
50 |
#ifdef ASSERT |
|
51 |
void JfrJavaSupport::check_java_thread_in_vm(Thread* t) { |
|
52 |
assert(t != NULL, "invariant"); |
|
53 |
assert(t->is_Java_thread(), "invariant"); |
|
54 |
assert(((JavaThread*)t)->thread_state() == _thread_in_vm, "invariant"); |
|
55 |
} |
|
56 |
||
57 |
void JfrJavaSupport::check_java_thread_in_native(Thread* t) { |
|
58 |
assert(t != NULL, "invariant"); |
|
59 |
assert(t->is_Java_thread(), "invariant"); |
|
60 |
assert(((JavaThread*)t)->thread_state() == _thread_in_native, "invariant"); |
|
61 |
} |
|
58863 | 62 |
|
63 |
static void check_new_unstarted_java_thread(Thread* t) { |
|
64 |
assert(t != NULL, "invariant"); |
|
65 |
assert(t->is_Java_thread(), "invariant"); |
|
66 |
assert(((JavaThread*)t)->thread_state() == _thread_new, "invariant"); |
|
67 |
} |
|
50113 | 68 |
#endif |
69 |
||
70 |
/* |
|
71 |
* Handles and references |
|
72 |
*/ |
|
73 |
jobject JfrJavaSupport::local_jni_handle(const oop obj, Thread* t) { |
|
74 |
DEBUG_ONLY(check_java_thread_in_vm(t)); |
|
75 |
return t->active_handles()->allocate_handle(obj); |
|
76 |
} |
|
77 |
||
78 |
jobject JfrJavaSupport::local_jni_handle(const jobject handle, Thread* t) { |
|
79 |
DEBUG_ONLY(check_java_thread_in_vm(t)); |
|
80 |
const oop obj = JNIHandles::resolve(handle); |
|
81 |
return obj == NULL ? NULL : local_jni_handle(obj, t); |
|
82 |
} |
|
83 |
||
84 |
void JfrJavaSupport::destroy_local_jni_handle(jobject handle) { |
|
85 |
JNIHandles::destroy_local(handle); |
|
86 |
} |
|
87 |
||
88 |
jobject JfrJavaSupport::global_jni_handle(const oop obj, Thread* t) { |
|
89 |
DEBUG_ONLY(check_java_thread_in_vm(t)); |
|
90 |
HandleMark hm(t); |
|
91 |
return JNIHandles::make_global(Handle(t, obj)); |
|
92 |
} |
|
93 |
||
94 |
jobject JfrJavaSupport::global_jni_handle(const jobject handle, Thread* t) { |
|
95 |
const oop obj = JNIHandles::resolve(handle); |
|
96 |
return obj == NULL ? NULL : global_jni_handle(obj, t); |
|
97 |
} |
|
98 |
||
99 |
void JfrJavaSupport::destroy_global_jni_handle(jobject handle) { |
|
100 |
JNIHandles::destroy_global(handle); |
|
101 |
} |
|
102 |
||
58863 | 103 |
jweak JfrJavaSupport::global_weak_jni_handle(const oop obj, Thread* t) { |
104 |
DEBUG_ONLY(check_java_thread_in_vm(t)); |
|
105 |
HandleMark hm(t); |
|
106 |
return JNIHandles::make_weak_global(Handle(t, obj)); |
|
107 |
} |
|
108 |
||
109 |
jweak JfrJavaSupport::global_weak_jni_handle(const jobject handle, Thread* t) { |
|
110 |
const oop obj = JNIHandles::resolve(handle); |
|
111 |
return obj == NULL ? NULL : global_weak_jni_handle(obj, t); |
|
112 |
} |
|
113 |
||
114 |
void JfrJavaSupport::destroy_global_weak_jni_handle(jweak handle) { |
|
115 |
JNIHandles::destroy_weak_global(handle); |
|
116 |
} |
|
117 |
||
50113 | 118 |
oop JfrJavaSupport::resolve_non_null(jobject obj) { |
119 |
return JNIHandles::resolve_non_null(obj); |
|
120 |
} |
|
121 |
||
122 |
/* |
|
123 |
* Method invocation |
|
124 |
*/ |
|
125 |
void JfrJavaSupport::call_static(JfrJavaArguments* args, TRAPS) { |
|
126 |
JfrJavaCall::call_static(args, THREAD); |
|
127 |
} |
|
128 |
||
129 |
void JfrJavaSupport::call_special(JfrJavaArguments* args, TRAPS) { |
|
130 |
JfrJavaCall::call_special(args, THREAD); |
|
131 |
} |
|
132 |
||
133 |
void JfrJavaSupport::call_virtual(JfrJavaArguments* args, TRAPS) { |
|
134 |
JfrJavaCall::call_virtual(args, THREAD); |
|
135 |
} |
|
136 |
||
137 |
void JfrJavaSupport::notify_all(jobject object, TRAPS) { |
|
138 |
assert(object != NULL, "invariant"); |
|
139 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
140 |
HandleMark hm(THREAD); |
|
141 |
Handle h_obj(THREAD, resolve_non_null(object)); |
|
142 |
assert(h_obj.not_null(), "invariant"); |
|
143 |
ObjectSynchronizer::jni_enter(h_obj, THREAD); |
|
144 |
ObjectSynchronizer::notifyall(h_obj, THREAD); |
|
145 |
ObjectSynchronizer::jni_exit(h_obj(), THREAD); |
|
146 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
147 |
} |
|
148 |
||
149 |
/* |
|
150 |
* Object construction |
|
151 |
*/ |
|
152 |
static void object_construction(JfrJavaArguments* args, JavaValue* result, InstanceKlass* klass, TRAPS) { |
|
153 |
assert(args != NULL, "invariant"); |
|
154 |
assert(result != NULL, "invariant"); |
|
155 |
assert(klass != NULL, "invariant"); |
|
156 |
assert(klass->is_initialized(), "invariant"); |
|
157 |
||
158 |
HandleMark hm(THREAD); |
|
159 |
instanceOop obj = klass->allocate_instance(CHECK); |
|
160 |
instanceHandle h_obj(THREAD, obj); |
|
161 |
assert(h_obj.not_null(), "invariant"); |
|
162 |
args->set_receiver(h_obj); |
|
163 |
result->set_type(T_VOID); // constructor result type |
|
164 |
JfrJavaSupport::call_special(args, CHECK); |
|
165 |
result->set_type(T_OBJECT); // set back to original result type |
|
166 |
result->set_jobject((jobject)h_obj()); |
|
167 |
} |
|
168 |
||
169 |
static void array_construction(JfrJavaArguments* args, JavaValue* result, InstanceKlass* klass, int array_length, TRAPS) { |
|
170 |
assert(args != NULL, "invariant"); |
|
171 |
assert(result != NULL, "invariant"); |
|
172 |
assert(klass != NULL, "invariant"); |
|
173 |
assert(klass->is_initialized(), "invariant"); |
|
174 |
||
175 |
Klass* const ak = klass->array_klass(THREAD); |
|
176 |
ObjArrayKlass::cast(ak)->initialize(THREAD); |
|
177 |
HandleMark hm(THREAD); |
|
178 |
objArrayOop arr = ObjArrayKlass::cast(ak)->allocate(array_length, CHECK); |
|
179 |
result->set_jobject((jobject)arr); |
|
180 |
} |
|
181 |
||
182 |
static void create_object(JfrJavaArguments* args, JavaValue* result, TRAPS) { |
|
183 |
assert(args != NULL, "invariant"); |
|
184 |
assert(result != NULL, "invariant"); |
|
185 |
assert(result->get_type() == T_OBJECT, "invariant"); |
|
186 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
|
187 |
||
188 |
InstanceKlass* const klass = static_cast<InstanceKlass*>(args->klass()); |
|
189 |
klass->initialize(CHECK); |
|
190 |
||
191 |
const int array_length = args->array_length(); |
|
192 |
||
55302
686dedba1d9a
8216064: -XX:StartFlightRecording:settings= doesn't work properly
egahlin
parents:
54847
diff
changeset
|
193 |
if (array_length >= 0) { |
50113 | 194 |
array_construction(args, result, klass, array_length, CHECK); |
195 |
} else { |
|
196 |
object_construction(args, result, klass, THREAD); |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
static void handle_result(JavaValue* result, bool global_ref, Thread* t) { |
|
201 |
assert(result != NULL, "invariant"); |
|
202 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(t)); |
|
203 |
const oop result_oop = (const oop)result->get_jobject(); |
|
204 |
if (result_oop == NULL) { |
|
205 |
return; |
|
206 |
} |
|
207 |
result->set_jobject(global_ref ? |
|
208 |
JfrJavaSupport::global_jni_handle(result_oop, t) : |
|
209 |
JfrJavaSupport::local_jni_handle(result_oop, t)); |
|
210 |
} |
|
211 |
||
212 |
void JfrJavaSupport::new_object(JfrJavaArguments* args, TRAPS) { |
|
213 |
assert(args != NULL, "invariant"); |
|
214 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
215 |
create_object(args, args->result(), THREAD); |
|
216 |
} |
|
217 |
||
218 |
void JfrJavaSupport::new_object_local_ref(JfrJavaArguments* args, TRAPS) { |
|
219 |
assert(args != NULL, "invariant"); |
|
220 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
221 |
JavaValue* const result = args->result(); |
|
222 |
assert(result != NULL, "invariant"); |
|
223 |
create_object(args, result, CHECK); |
|
224 |
handle_result(result, false, THREAD); |
|
225 |
} |
|
226 |
||
227 |
void JfrJavaSupport::new_object_global_ref(JfrJavaArguments* args, TRAPS) { |
|
228 |
assert(args != NULL, "invariant"); |
|
229 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
230 |
JavaValue* const result = args->result(); |
|
231 |
assert(result != NULL, "invariant"); |
|
232 |
create_object(args, result, CHECK); |
|
233 |
handle_result(result, true, THREAD); |
|
234 |
} |
|
235 |
||
236 |
jstring JfrJavaSupport::new_string(const char* c_str, TRAPS) { |
|
237 |
assert(c_str != NULL, "invariant"); |
|
238 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
239 |
const oop result = java_lang_String::create_oop_from_str(c_str, THREAD); |
|
240 |
return (jstring)local_jni_handle(result, THREAD); |
|
241 |
} |
|
242 |
||
243 |
jobjectArray JfrJavaSupport::new_string_array(int length, TRAPS) { |
|
244 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
245 |
JavaValue result(T_OBJECT); |
|
246 |
JfrJavaArguments args(&result, "java/lang/String", "<init>", "()V", CHECK_NULL); |
|
247 |
args.set_array_length(length); |
|
248 |
new_object_local_ref(&args, THREAD); |
|
249 |
return (jobjectArray)args.result()->get_jobject(); |
|
250 |
} |
|
251 |
||
252 |
jobject JfrJavaSupport::new_java_lang_Boolean(bool value, TRAPS) { |
|
253 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
254 |
JavaValue result(T_OBJECT); |
|
255 |
JfrJavaArguments args(&result, "java/lang/Boolean", "<init>", "(Z)V", CHECK_NULL); |
|
256 |
args.push_int(value ? (jint)JNI_TRUE : (jint)JNI_FALSE); |
|
257 |
new_object_local_ref(&args, THREAD); |
|
258 |
return args.result()->get_jobject(); |
|
259 |
} |
|
260 |
||
261 |
jobject JfrJavaSupport::new_java_lang_Integer(jint value, TRAPS) { |
|
262 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
263 |
JavaValue result(T_OBJECT); |
|
264 |
JfrJavaArguments args(&result, "java/lang/Integer", "<init>", "(I)V", CHECK_NULL); |
|
265 |
args.push_int(value); |
|
266 |
new_object_local_ref(&args, THREAD); |
|
267 |
return args.result()->get_jobject(); |
|
268 |
} |
|
269 |
||
270 |
jobject JfrJavaSupport::new_java_lang_Long(jlong value, TRAPS) { |
|
271 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
272 |
JavaValue result(T_OBJECT); |
|
273 |
JfrJavaArguments args(&result, "java/lang/Long", "<init>", "(J)V", CHECK_NULL); |
|
274 |
args.push_long(value); |
|
275 |
new_object_local_ref(&args, THREAD); |
|
276 |
return args.result()->get_jobject(); |
|
277 |
} |
|
278 |
||
279 |
void JfrJavaSupport::set_array_element(jobjectArray arr, jobject element, int index, Thread* t) { |
|
280 |
assert(arr != NULL, "invariant"); |
|
281 |
DEBUG_ONLY(check_java_thread_in_vm(t)); |
|
282 |
HandleMark hm(t); |
|
283 |
objArrayHandle a(t, (objArrayOop)resolve_non_null(arr)); |
|
284 |
a->obj_at_put(index, resolve_non_null(element)); |
|
285 |
} |
|
286 |
||
287 |
/* |
|
288 |
* Field access |
|
289 |
*/ |
|
290 |
static void write_int_field(const Handle& h_oop, fieldDescriptor* fd, jint value) { |
|
291 |
assert(h_oop.not_null(), "invariant"); |
|
292 |
assert(fd != NULL, "invariant"); |
|
293 |
h_oop->int_field_put(fd->offset(), value); |
|
294 |
} |
|
295 |
||
296 |
static void write_float_field(const Handle& h_oop, fieldDescriptor* fd, jfloat value) { |
|
297 |
assert(h_oop.not_null(), "invariant"); |
|
298 |
assert(fd != NULL, "invariant"); |
|
299 |
h_oop->float_field_put(fd->offset(), value); |
|
300 |
} |
|
301 |
||
302 |
static void write_double_field(const Handle& h_oop, fieldDescriptor* fd, jdouble value) { |
|
303 |
assert(h_oop.not_null(), "invariant"); |
|
304 |
assert(fd != NULL, "invariant"); |
|
305 |
h_oop->double_field_put(fd->offset(), value); |
|
306 |
} |
|
307 |
||
308 |
static void write_long_field(const Handle& h_oop, fieldDescriptor* fd, jlong value) { |
|
309 |
assert(h_oop.not_null(), "invariant"); |
|
310 |
assert(fd != NULL, "invariant"); |
|
311 |
h_oop->long_field_put(fd->offset(), value); |
|
312 |
} |
|
313 |
||
314 |
static void write_oop_field(const Handle& h_oop, fieldDescriptor* fd, const oop value) { |
|
315 |
assert(h_oop.not_null(), "invariant"); |
|
316 |
assert(fd != NULL, "invariant"); |
|
317 |
h_oop->obj_field_put(fd->offset(), value); |
|
318 |
} |
|
319 |
||
320 |
static void write_specialized_field(JfrJavaArguments* args, const Handle& h_oop, fieldDescriptor* fd, bool static_field) { |
|
321 |
assert(args != NULL, "invariant"); |
|
322 |
assert(h_oop.not_null(), "invariant"); |
|
323 |
assert(fd != NULL, "invariant"); |
|
324 |
assert(fd->offset() > 0, "invariant"); |
|
325 |
assert(args->length() >= 1, "invariant"); |
|
326 |
||
327 |
// attempt must set a real value |
|
328 |
assert(args->param(1).get_type() != T_VOID, "invariant"); |
|
329 |
||
330 |
switch(fd->field_type()) { |
|
331 |
case T_BOOLEAN: |
|
332 |
case T_CHAR: |
|
333 |
case T_SHORT: |
|
334 |
case T_INT: |
|
335 |
write_int_field(h_oop, fd, args->param(1).get_jint()); |
|
336 |
break; |
|
337 |
case T_FLOAT: |
|
338 |
write_float_field(h_oop, fd, args->param(1).get_jfloat()); |
|
339 |
break; |
|
340 |
case T_DOUBLE: |
|
341 |
write_double_field(h_oop, fd, args->param(1).get_jdouble()); |
|
342 |
break; |
|
343 |
case T_LONG: |
|
344 |
write_long_field(h_oop, fd, args->param(1).get_jlong()); |
|
345 |
break; |
|
346 |
case T_OBJECT: |
|
347 |
write_oop_field(h_oop, fd, (oop)args->param(1).get_jobject()); |
|
348 |
break; |
|
349 |
case T_ADDRESS: |
|
350 |
write_oop_field(h_oop, fd, JfrJavaSupport::resolve_non_null(args->param(1).get_jobject())); |
|
351 |
break; |
|
352 |
default: |
|
353 |
ShouldNotReachHere(); |
|
354 |
} |
|
355 |
} |
|
356 |
||
357 |
static void read_specialized_field(JavaValue* result, const Handle& h_oop, fieldDescriptor* fd) { |
|
358 |
assert(result != NULL, "invariant"); |
|
359 |
assert(h_oop.not_null(), "invariant"); |
|
360 |
assert(fd != NULL, "invariant"); |
|
361 |
assert(fd->offset() > 0, "invariant"); |
|
362 |
||
363 |
switch(fd->field_type()) { |
|
364 |
case T_BOOLEAN: |
|
365 |
case T_CHAR: |
|
366 |
case T_SHORT: |
|
367 |
case T_INT: |
|
368 |
result->set_jint(h_oop->int_field(fd->offset())); |
|
369 |
break; |
|
370 |
case T_FLOAT: |
|
371 |
result->set_jfloat(h_oop->float_field(fd->offset())); |
|
372 |
break; |
|
373 |
case T_DOUBLE: |
|
374 |
result->set_jdouble(h_oop->double_field(fd->offset())); |
|
375 |
break; |
|
376 |
case T_LONG: |
|
377 |
result->set_jlong(h_oop->long_field(fd->offset())); |
|
378 |
break; |
|
379 |
case T_OBJECT: |
|
380 |
result->set_jobject((jobject)h_oop->obj_field(fd->offset())); |
|
381 |
break; |
|
382 |
default: |
|
383 |
ShouldNotReachHere(); |
|
384 |
} |
|
385 |
} |
|
386 |
||
387 |
static bool find_field(InstanceKlass* ik, |
|
388 |
Symbol* name_symbol, |
|
389 |
Symbol* signature_symbol, |
|
390 |
fieldDescriptor* fd, |
|
391 |
bool is_static = false, |
|
392 |
bool allow_super = false) { |
|
393 |
if (allow_super || is_static) { |
|
394 |
return ik->find_field(name_symbol, signature_symbol, is_static, fd) != NULL; |
|
395 |
} |
|
396 |
return ik->find_local_field(name_symbol, signature_symbol, fd); |
|
397 |
} |
|
398 |
||
399 |
static void lookup_field(JfrJavaArguments* args, InstanceKlass* klass, fieldDescriptor* fd, bool static_field) { |
|
400 |
assert(args != NULL, "invariant"); |
|
401 |
assert(klass != NULL, "invariant"); |
|
402 |
assert(klass->is_initialized(), "invariant"); |
|
403 |
assert(fd != NULL, "invariant"); |
|
404 |
find_field(klass, args->name(), args->signature(), fd, static_field, true); |
|
405 |
} |
|
406 |
||
407 |
static void read_field(JfrJavaArguments* args, JavaValue* result, TRAPS) { |
|
408 |
assert(args != NULL, "invariant"); |
|
409 |
assert(result != NULL, "invariant"); |
|
410 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
|
411 |
||
412 |
InstanceKlass* const klass = static_cast<InstanceKlass*>(args->klass()); |
|
413 |
klass->initialize(CHECK); |
|
414 |
const bool static_field = !args->has_receiver(); |
|
415 |
fieldDescriptor fd; |
|
416 |
lookup_field(args, klass, &fd, static_field); |
|
417 |
assert(fd.offset() > 0, "invariant"); |
|
418 |
||
419 |
HandleMark hm(THREAD); |
|
420 |
Handle h_oop(static_field ? Handle(THREAD, klass->java_mirror()) : Handle(THREAD, args->receiver())); |
|
421 |
read_specialized_field(result, h_oop, &fd); |
|
422 |
} |
|
423 |
||
424 |
static void write_field(JfrJavaArguments* args, JavaValue* result, TRAPS) { |
|
425 |
assert(args != NULL, "invariant"); |
|
426 |
assert(result != NULL, "invariant"); |
|
427 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
|
428 |
||
429 |
InstanceKlass* const klass = static_cast<InstanceKlass*>(args->klass()); |
|
430 |
klass->initialize(CHECK); |
|
431 |
||
432 |
const bool static_field = !args->has_receiver(); |
|
433 |
fieldDescriptor fd; |
|
434 |
lookup_field(args, klass, &fd, static_field); |
|
435 |
assert(fd.offset() > 0, "invariant"); |
|
436 |
||
437 |
HandleMark hm(THREAD); |
|
438 |
Handle h_oop(static_field ? Handle(THREAD, klass->java_mirror()) : Handle(THREAD, args->receiver())); |
|
439 |
write_specialized_field(args, h_oop, &fd, static_field); |
|
440 |
} |
|
441 |
||
442 |
void JfrJavaSupport::set_field(JfrJavaArguments* args, TRAPS) { |
|
443 |
assert(args != NULL, "invariant"); |
|
444 |
write_field(args, args->result(), THREAD); |
|
445 |
} |
|
446 |
||
447 |
void JfrJavaSupport::get_field(JfrJavaArguments* args, TRAPS) { |
|
448 |
assert(args != NULL, "invariant"); |
|
449 |
read_field(args, args->result(), THREAD); |
|
450 |
} |
|
451 |
||
452 |
void JfrJavaSupport::get_field_local_ref(JfrJavaArguments* args, TRAPS) { |
|
453 |
assert(args != NULL, "invariant"); |
|
454 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
455 |
||
456 |
JavaValue* const result = args->result(); |
|
457 |
assert(result != NULL, "invariant"); |
|
458 |
assert(result->get_type() == T_OBJECT, "invariant"); |
|
459 |
||
460 |
read_field(args, result, CHECK); |
|
461 |
const oop obj = (const oop)result->get_jobject(); |
|
462 |
||
463 |
if (obj != NULL) { |
|
464 |
result->set_jobject(local_jni_handle(obj, THREAD)); |
|
465 |
} |
|
466 |
} |
|
467 |
||
468 |
void JfrJavaSupport::get_field_global_ref(JfrJavaArguments* args, TRAPS) { |
|
469 |
assert(args != NULL, "invariant"); |
|
470 |
DEBUG_ONLY(check_java_thread_in_vm(THREAD)); |
|
471 |
||
472 |
JavaValue* const result = args->result(); |
|
473 |
assert(result != NULL, "invariant"); |
|
474 |
assert(result->get_type() == T_OBJECT, "invariant"); |
|
475 |
read_field(args, result, CHECK); |
|
476 |
const oop obj = (const oop)result->get_jobject(); |
|
477 |
if (obj != NULL) { |
|
478 |
result->set_jobject(global_jni_handle(obj, THREAD)); |
|
479 |
} |
|
480 |
} |
|
481 |
||
482 |
/* |
|
483 |
* Misc |
|
484 |
*/ |
|
485 |
Klass* JfrJavaSupport::klass(const jobject handle) { |
|
486 |
const oop obj = resolve_non_null(handle); |
|
487 |
assert(obj != NULL, "invariant"); |
|
488 |
return obj->klass(); |
|
489 |
} |
|
490 |
||
491 |
// caller needs ResourceMark |
|
492 |
const char* JfrJavaSupport::c_str(jstring string, Thread* t) { |
|
493 |
DEBUG_ONLY(check_java_thread_in_vm(t)); |
|
494 |
if (string == NULL) { |
|
495 |
return NULL; |
|
496 |
} |
|
497 |
const char* temp = NULL; |
|
498 |
const oop java_string = resolve_non_null(string); |
|
53418
bc2bb4eee477
8217442: Optimize native accesses to String.value
redestad
parents:
51467
diff
changeset
|
499 |
const typeArrayOop value = java_lang_String::value(java_string); |
bc2bb4eee477
8217442: Optimize native accesses to String.value
redestad
parents:
51467
diff
changeset
|
500 |
if (value != NULL) { |
bc2bb4eee477
8217442: Optimize native accesses to String.value
redestad
parents:
51467
diff
changeset
|
501 |
const size_t length = java_lang_String::utf8_length(java_string, value); |
50113 | 502 |
temp = NEW_RESOURCE_ARRAY_IN_THREAD(t, const char, (length + 1)); |
503 |
if (temp == NULL) { |
|
504 |
JfrJavaSupport::throw_out_of_memory_error("Unable to allocate thread local native memory", t); |
|
505 |
return NULL; |
|
506 |
} |
|
507 |
assert(temp != NULL, "invariant"); |
|
53418
bc2bb4eee477
8217442: Optimize native accesses to String.value
redestad
parents:
51467
diff
changeset
|
508 |
java_lang_String::as_utf8_string(java_string, value, const_cast<char*>(temp), (int) length + 1); |
50113 | 509 |
} |
510 |
return temp; |
|
511 |
} |
|
512 |
||
513 |
/* |
|
514 |
* Exceptions and errors |
|
515 |
*/ |
|
516 |
static void create_and_throw(Symbol* name, const char* message, TRAPS) { |
|
517 |
assert(name != NULL, "invariant"); |
|
518 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
|
519 |
assert(!HAS_PENDING_EXCEPTION, "invariant"); |
|
520 |
THROW_MSG(name, message); |
|
521 |
} |
|
522 |
||
523 |
void JfrJavaSupport::throw_illegal_state_exception(const char* message, TRAPS) { |
|
524 |
create_and_throw(vmSymbols::java_lang_IllegalStateException(), message, THREAD); |
|
525 |
} |
|
526 |
||
527 |
void JfrJavaSupport::throw_internal_error(const char* message, TRAPS) { |
|
528 |
create_and_throw(vmSymbols::java_lang_InternalError(), message, THREAD); |
|
529 |
} |
|
530 |
||
531 |
void JfrJavaSupport::throw_illegal_argument_exception(const char* message, TRAPS) { |
|
532 |
create_and_throw(vmSymbols::java_lang_IllegalArgumentException(), message, THREAD); |
|
533 |
} |
|
534 |
||
535 |
void JfrJavaSupport::throw_out_of_memory_error(const char* message, TRAPS) { |
|
536 |
create_and_throw(vmSymbols::java_lang_OutOfMemoryError(), message, THREAD); |
|
537 |
} |
|
538 |
||
539 |
void JfrJavaSupport::throw_class_format_error(const char* message, TRAPS) { |
|
540 |
create_and_throw(vmSymbols::java_lang_ClassFormatError(), message, THREAD); |
|
541 |
} |
|
542 |
||
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
58863
diff
changeset
|
543 |
void JfrJavaSupport::throw_runtime_exception(const char* message, TRAPS) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
58863
diff
changeset
|
544 |
create_and_throw(vmSymbols::java_lang_RuntimeException(), message, THREAD); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
58863
diff
changeset
|
545 |
} |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
58863
diff
changeset
|
546 |
|
50113 | 547 |
void JfrJavaSupport::abort(jstring errorMsg, Thread* t) { |
548 |
DEBUG_ONLY(check_java_thread_in_vm(t)); |
|
549 |
||
550 |
ResourceMark rm(t); |
|
551 |
const char* const error_msg = c_str(errorMsg, t); |
|
552 |
if (error_msg != NULL) { |
|
553 |
log_error(jfr, system)("%s",error_msg); |
|
554 |
} |
|
555 |
log_error(jfr, system)("%s", "An irrecoverable error in Jfr. Shutting down VM..."); |
|
556 |
vm_abort(); |
|
557 |
} |
|
558 |
||
559 |
JfrJavaSupport::CAUSE JfrJavaSupport::_cause = JfrJavaSupport::VM_ERROR; |
|
560 |
void JfrJavaSupport::set_cause(jthrowable throwable, Thread* t) { |
|
561 |
DEBUG_ONLY(check_java_thread_in_vm(t)); |
|
562 |
||
563 |
HandleMark hm(t); |
|
564 |
Handle ex(t, JNIHandles::resolve_external_guard(throwable)); |
|
565 |
||
566 |
if (ex.is_null()) { |
|
567 |
return; |
|
568 |
} |
|
569 |
||
570 |
if (ex->is_a(SystemDictionary::OutOfMemoryError_klass())) { |
|
571 |
_cause = OUT_OF_MEMORY; |
|
572 |
return; |
|
573 |
} |
|
574 |
if (ex->is_a(SystemDictionary::StackOverflowError_klass())) { |
|
575 |
_cause = STACK_OVERFLOW; |
|
576 |
return; |
|
577 |
} |
|
578 |
if (ex->is_a(SystemDictionary::Error_klass())) { |
|
579 |
_cause = VM_ERROR; |
|
580 |
return; |
|
581 |
} |
|
582 |
if (ex->is_a(SystemDictionary::RuntimeException_klass())) { |
|
583 |
_cause = RUNTIME_EXCEPTION; |
|
584 |
return; |
|
585 |
} |
|
586 |
if (ex->is_a(SystemDictionary::Exception_klass())) { |
|
587 |
_cause = UNKNOWN; |
|
588 |
return; |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
void JfrJavaSupport::uncaught_exception(jthrowable throwable, Thread* t) { |
|
593 |
DEBUG_ONLY(check_java_thread_in_vm(t)); |
|
594 |
assert(throwable != NULL, "invariant"); |
|
595 |
set_cause(throwable, t); |
|
596 |
} |
|
597 |
||
598 |
JfrJavaSupport::CAUSE JfrJavaSupport::cause() { |
|
599 |
return _cause; |
|
600 |
} |
|
601 |
||
602 |
const char* const JDK_JFR_MODULE_NAME = "jdk.jfr"; |
|
603 |
const char* const JDK_JFR_PACKAGE_NAME = "jdk/jfr"; |
|
604 |
||
605 |
static bool is_jdk_jfr_module_in_readability_graph() { |
|
606 |
Thread* const t = Thread::current(); |
|
607 |
// take one of the packages in the module to be located and query for its definition. |
|
54847
59ea39bb2809
8223657: Remove unused THREAD argument from SymbolTable functions
coleenp
parents:
53418
diff
changeset
|
608 |
TempNewSymbol pkg_sym = SymbolTable::new_symbol(JDK_JFR_PACKAGE_NAME); |
50113 | 609 |
return Modules::is_package_defined(pkg_sym, Handle(), t); |
610 |
} |
|
611 |
||
612 |
static void print_module_resolution_error(outputStream* stream) { |
|
613 |
assert(stream != NULL, "invariant"); |
|
50388
55fac6146d31
8203919: Remove "-add-modules" hint from error message
egahlin
parents:
50113
diff
changeset
|
614 |
stream->print_cr("Module %s not found.", JDK_JFR_MODULE_NAME); |
50113 | 615 |
stream->print_cr("Flight Recorder can not be enabled."); |
616 |
} |
|
617 |
||
618 |
bool JfrJavaSupport::is_jdk_jfr_module_available() { |
|
619 |
return is_jdk_jfr_module_in_readability_graph(); |
|
620 |
} |
|
621 |
||
622 |
bool JfrJavaSupport::is_jdk_jfr_module_available(outputStream* stream, TRAPS) { |
|
623 |
if (!JfrJavaSupport::is_jdk_jfr_module_available()) { |
|
624 |
if (stream != NULL) { |
|
625 |
print_module_resolution_error(stream); |
|
626 |
} |
|
627 |
return false; |
|
628 |
} |
|
629 |
return true; |
|
630 |
} |
|
631 |
||
58863 | 632 |
class ThreadExclusionListAccess : public StackObj { |
633 |
private: |
|
634 |
static Semaphore _mutex_semaphore; |
|
635 |
public: |
|
636 |
ThreadExclusionListAccess() { _mutex_semaphore.wait(); } |
|
637 |
~ThreadExclusionListAccess() { _mutex_semaphore.signal(); } |
|
638 |
}; |
|
639 |
||
640 |
Semaphore ThreadExclusionListAccess::_mutex_semaphore(1); |
|
641 |
static GrowableArray<jweak>* exclusion_list = NULL; |
|
642 |
||
643 |
static bool equals(const jweak excluded_thread, Handle target_thread) { |
|
644 |
return JfrJavaSupport::resolve_non_null(excluded_thread) == target_thread(); |
|
645 |
} |
|
646 |
||
647 |
static int find_exclusion_thread_idx(Handle thread) { |
|
648 |
if (exclusion_list != NULL) { |
|
649 |
for (int i = 0; i < exclusion_list->length(); ++i) { |
|
650 |
if (equals(exclusion_list->at(i), thread)) { |
|
651 |
return i; |
|
652 |
} |
|
653 |
} |
|
654 |
} |
|
655 |
return -1; |
|
656 |
} |
|
657 |
||
658 |
static Handle as_handle(jobject thread) { |
|
659 |
return Handle(Thread::current(), JfrJavaSupport::resolve_non_null(thread)); |
|
660 |
} |
|
661 |
||
662 |
static bool thread_is_not_excluded(Handle thread) { |
|
663 |
return -1 == find_exclusion_thread_idx(thread); |
|
664 |
} |
|
665 |
||
666 |
static bool thread_is_not_excluded(jobject thread) { |
|
667 |
return thread_is_not_excluded(as_handle(thread)); |
|
668 |
} |
|
669 |
||
670 |
static bool is_thread_excluded(jobject thread) { |
|
671 |
return !thread_is_not_excluded(thread); |
|
672 |
} |
|
673 |
||
674 |
#ifdef ASSERT |
|
675 |
static bool is_thread_excluded(Handle thread) { |
|
676 |
return !thread_is_not_excluded(thread); |
|
677 |
} |
|
678 |
#endif // ASSERT |
|
679 |
||
680 |
static int add_thread_to_exclusion_list(jobject thread) { |
|
681 |
ThreadExclusionListAccess lock; |
|
682 |
if (exclusion_list == NULL) { |
|
683 |
exclusion_list = new (ResourceObj::C_HEAP, mtTracing) GrowableArray<jweak>(10, true, mtTracing); |
|
684 |
} |
|
685 |
assert(exclusion_list != NULL, "invariant"); |
|
686 |
assert(thread_is_not_excluded(thread), "invariant"); |
|
687 |
jweak ref = JfrJavaSupport::global_weak_jni_handle(thread, Thread::current()); |
|
688 |
const int idx = exclusion_list->append(ref); |
|
689 |
assert(is_thread_excluded(thread), "invariant"); |
|
690 |
return idx; |
|
691 |
} |
|
692 |
||
693 |
static void remove_thread_from_exclusion_list(Handle thread) { |
|
694 |
assert(exclusion_list != NULL, "invariant"); |
|
695 |
assert(is_thread_excluded(thread), "invariant"); |
|
696 |
assert(exclusion_list != NULL, "invariant"); |
|
697 |
const int idx = find_exclusion_thread_idx(thread); |
|
698 |
assert(idx >= 0, "invariant"); |
|
699 |
assert(idx < exclusion_list->length(), "invariant"); |
|
700 |
JfrJavaSupport::destroy_global_weak_jni_handle(exclusion_list->at(idx)); |
|
701 |
exclusion_list->delete_at(idx); |
|
702 |
assert(thread_is_not_excluded(thread), "invariant"); |
|
703 |
if (0 == exclusion_list->length()) { |
|
704 |
delete exclusion_list; |
|
705 |
exclusion_list = NULL; |
|
706 |
} |
|
707 |
} |
|
708 |
||
709 |
static void remove_thread_from_exclusion_list(jobject thread) { |
|
710 |
ThreadExclusionListAccess lock; |
|
711 |
remove_thread_from_exclusion_list(as_handle(thread)); |
|
712 |
} |
|
713 |
||
714 |
// includes removal |
|
715 |
static bool check_exclusion_state_on_thread_start(JavaThread* jt) { |
|
716 |
Handle h_obj(jt, jt->threadObj()); |
|
717 |
ThreadExclusionListAccess lock; |
|
718 |
if (thread_is_not_excluded(h_obj)) { |
|
719 |
return false; |
|
720 |
} |
|
721 |
remove_thread_from_exclusion_list(h_obj); |
|
722 |
return true; |
|
723 |
} |
|
724 |
||
725 |
jlong JfrJavaSupport::jfr_thread_id(jobject thread) { |
|
50113 | 726 |
ThreadsListHandle tlh; |
727 |
JavaThread* native_thread = NULL; |
|
58863 | 728 |
(void)tlh.cv_internal_thread_to_JavaThread(thread, &native_thread, NULL); |
50113 | 729 |
return native_thread != NULL ? JFR_THREAD_ID(native_thread) : 0; |
730 |
} |
|
58863 | 731 |
|
732 |
void JfrJavaSupport::exclude(jobject thread) { |
|
733 |
HandleMark hm; |
|
734 |
ThreadsListHandle tlh; |
|
735 |
JavaThread* native_thread = NULL; |
|
736 |
(void)tlh.cv_internal_thread_to_JavaThread(thread, &native_thread, NULL); |
|
737 |
if (native_thread != NULL) { |
|
738 |
JfrThreadLocal::exclude(native_thread); |
|
739 |
} else { |
|
740 |
// not started yet, track the thread oop |
|
741 |
add_thread_to_exclusion_list(thread); |
|
742 |
} |
|
743 |
} |
|
744 |
||
745 |
void JfrJavaSupport::include(jobject thread) { |
|
746 |
HandleMark hm; |
|
747 |
ThreadsListHandle tlh; |
|
748 |
JavaThread* native_thread = NULL; |
|
749 |
(void)tlh.cv_internal_thread_to_JavaThread(thread, &native_thread, NULL); |
|
750 |
if (native_thread != NULL) { |
|
751 |
JfrThreadLocal::include(native_thread); |
|
752 |
} else { |
|
753 |
// not started yet, untrack the thread oop |
|
754 |
remove_thread_from_exclusion_list(thread); |
|
755 |
} |
|
756 |
} |
|
757 |
||
758 |
bool JfrJavaSupport::is_excluded(jobject thread) { |
|
759 |
HandleMark hm; |
|
760 |
ThreadsListHandle tlh; |
|
761 |
JavaThread* native_thread = NULL; |
|
762 |
(void)tlh.cv_internal_thread_to_JavaThread(thread, &native_thread, NULL); |
|
763 |
return native_thread != NULL ? native_thread->jfr_thread_local()->is_excluded() : is_thread_excluded(thread); |
|
764 |
} |
|
765 |
||
766 |
void JfrJavaSupport::on_thread_start(Thread* t) { |
|
767 |
assert(t != NULL, "invariant"); |
|
768 |
assert(Thread::current() == t, "invariant"); |
|
769 |
if (!t->is_Java_thread()) { |
|
770 |
return; |
|
771 |
} |
|
772 |
DEBUG_ONLY(check_new_unstarted_java_thread(t);) |
|
773 |
HandleMark hm; |
|
774 |
if (check_exclusion_state_on_thread_start((JavaThread*)t)) { |
|
775 |
JfrThreadLocal::exclude(t); |
|
776 |
} |
|
777 |
} |