author | mgronlun |
Fri, 11 Oct 2019 19:46:05 +0200 | |
branch | JEP-349-branch |
changeset 58567 | e77a97d0edbb |
parent 57987 | 23e3cd901cb6 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
57360 | 2 |
* Copyright (c) 2012, 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 "jni.h" |
|
27 |
#include "classfile/javaClasses.hpp" |
|
28 |
#include "classfile/symbolTable.hpp" |
|
29 |
#include "classfile/systemDictionary.hpp" |
|
57360 | 30 |
#include "jfr/jfr.hpp" |
50113 | 31 |
#include "jfr/jni/jfrJavaSupport.hpp" |
32 |
#include "jfr/recorder/jfrRecorder.hpp" |
|
33 |
#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" |
|
34 |
#include "jfr/recorder/service/jfrRecorderThread.hpp" |
|
35 |
#include "memory/resourceArea.hpp" |
|
54786 | 36 |
#include "memory/universe.hpp" |
50113 | 37 |
#include "runtime/handles.inline.hpp" |
38 |
#include "runtime/mutexLocker.hpp" |
|
39 |
#include "runtime/thread.inline.hpp" |
|
40 |
#include "utilities/preserveException.hpp" |
|
41 |
#include "utilities/macros.hpp" |
|
42 |
||
43 |
static Thread* start_thread(instanceHandle thread_oop, ThreadFunction proc, TRAPS) { |
|
44 |
assert(thread_oop.not_null(), "invariant"); |
|
45 |
assert(proc != NULL, "invariant"); |
|
46 |
||
47 |
bool allocation_failed = false; |
|
48 |
JavaThread* new_thread = NULL; |
|
49 |
{ |
|
50 |
MutexLocker mu(Threads_lock); |
|
51 |
new_thread = new JavaThread(proc); |
|
52 |
// At this point it may be possible that no |
|
53 |
// osthread was created for the JavaThread due to lack of memory. |
|
54 |
if (new_thread == NULL || new_thread->osthread() == NULL) { |
|
55 |
delete new_thread; |
|
56 |
allocation_failed = true; |
|
57 |
} else { |
|
58 |
java_lang_Thread::set_thread(thread_oop(), new_thread); |
|
59 |
java_lang_Thread::set_priority(thread_oop(), NormPriority); |
|
60 |
java_lang_Thread::set_daemon(thread_oop()); |
|
61 |
new_thread->set_threadObj(thread_oop()); |
|
62 |
Threads::add(new_thread); |
|
63 |
} |
|
64 |
} |
|
65 |
if (allocation_failed) { |
|
66 |
JfrJavaSupport::throw_out_of_memory_error("Unable to create native recording thread for JFR", CHECK_NULL); |
|
67 |
} |
|
68 |
Thread::start(new_thread); |
|
69 |
return new_thread; |
|
70 |
} |
|
71 |
||
72 |
JfrPostBox* JfrRecorderThread::_post_box = NULL; |
|
73 |
||
74 |
JfrPostBox& JfrRecorderThread::post_box() { |
|
75 |
return *_post_box; |
|
76 |
} |
|
77 |
||
78 |
// defined in JfrRecorderThreadLoop.cpp |
|
79 |
void recorderthread_entry(JavaThread*, Thread*); |
|
80 |
||
81 |
bool JfrRecorderThread::start(JfrCheckpointManager* cp_manager, JfrPostBox* post_box, TRAPS) { |
|
82 |
assert(cp_manager != NULL, "invariant"); |
|
83 |
assert(post_box != NULL, "invariant"); |
|
84 |
_post_box = post_box; |
|
85 |
||
86 |
static const char klass[] = "jdk/jfr/internal/JVMUpcalls"; |
|
87 |
static const char method[] = "createRecorderThread"; |
|
88 |
static const char signature[] = "(Ljava/lang/ThreadGroup;Ljava/lang/ClassLoader;)Ljava/lang/Thread;"; |
|
89 |
||
90 |
JavaValue result(T_OBJECT); |
|
91 |
JfrJavaArguments create_thread_args(&result, klass, method, signature, CHECK_false); |
|
92 |
||
93 |
// arguments |
|
94 |
create_thread_args.push_oop(Universe::system_thread_group()); |
|
95 |
create_thread_args.push_oop(SystemDictionary::java_system_loader()); |
|
96 |
||
97 |
JfrJavaSupport::call_static(&create_thread_args, CHECK_false); |
|
98 |
instanceHandle h_thread_oop(THREAD, (instanceOop)result.get_jobject()); |
|
99 |
assert(h_thread_oop.not_null(), "invariant"); |
|
100 |
// attempt thread start |
|
58567
e77a97d0edbb
Metadata races (TestStart) and thread exclusion inclusion (TestRecursive)
mgronlun
parents:
57987
diff
changeset
|
101 |
Thread* const t = start_thread(h_thread_oop, recorderthread_entry,THREAD); |
50113 | 102 |
if (!HAS_PENDING_EXCEPTION) { |
57360 | 103 |
Jfr::exclude_thread(t); |
50113 | 104 |
cp_manager->register_service_thread(t); |
105 |
return true; |
|
106 |
} |
|
107 |
assert(HAS_PENDING_EXCEPTION, "invariant"); |
|
108 |
// Start failed, remove the thread from the system thread group |
|
109 |
JavaValue void_result(T_VOID); |
|
110 |
JfrJavaArguments remove_thread_args(&void_result); |
|
111 |
remove_thread_args.set_klass(SystemDictionary::ThreadGroup_klass()); |
|
112 |
remove_thread_args.set_name(vmSymbols::remove_method_name()); |
|
113 |
remove_thread_args.set_signature(vmSymbols::thread_void_signature()); |
|
114 |
remove_thread_args.set_receiver(Universe::system_thread_group()); |
|
115 |
remove_thread_args.push_oop(h_thread_oop()); |
|
116 |
CautiouslyPreserveExceptionMark cpe(THREAD); |
|
117 |
JfrJavaSupport::call_special(&remove_thread_args, THREAD); |
|
118 |
return false; |
|
119 |
} |