| 1 | /* |
| 2 | * Copyright (c) 2012, 2018, 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 "jni.h" |
| 27 | #include "classfile/javaClasses.hpp" |
| 28 | #include "classfile/symbolTable.hpp" |
| 29 | #include "classfile/systemDictionary.hpp" |
| 30 | #include "jfr/jni/jfrJavaSupport.hpp" |
| 31 | #include "jfr/recorder/jfrRecorder.hpp" |
| 32 | #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" |
| 33 | #include "jfr/recorder/service/jfrRecorderThread.hpp" |
| 34 | #include "memory/resourceArea.hpp" |
| 35 | #include "memory/universe.hpp" |
| 36 | #include "runtime/handles.inline.hpp" |
| 37 | #include "runtime/mutexLocker.hpp" |
| 38 | #include "runtime/thread.inline.hpp" |
| 39 | #include "utilities/preserveException.hpp" |
| 40 | #include "utilities/macros.hpp" |
| 41 | |
| 42 | static Thread* start_thread(instanceHandle thread_oop, ThreadFunction proc, TRAPS) { |
| 43 | assert(thread_oop.not_null(), "invariant" ); |
| 44 | assert(proc != NULL, "invariant" ); |
| 45 | |
| 46 | bool allocation_failed = false; |
| 47 | JavaThread* new_thread = NULL; |
| 48 | { |
| 49 | MutexLocker mu(Threads_lock); |
| 50 | new_thread = new JavaThread(proc); |
| 51 | // At this point it may be possible that no |
| 52 | // osthread was created for the JavaThread due to lack of memory. |
| 53 | if (new_thread == NULL || new_thread->osthread() == NULL) { |
| 54 | delete new_thread; |
| 55 | allocation_failed = true; |
| 56 | } else { |
| 57 | java_lang_Thread::set_thread(thread_oop(), new_thread); |
| 58 | java_lang_Thread::set_priority(thread_oop(), NormPriority); |
| 59 | java_lang_Thread::set_daemon(thread_oop()); |
| 60 | new_thread->set_threadObj(thread_oop()); |
| 61 | Threads::add(new_thread); |
| 62 | } |
| 63 | } |
| 64 | if (allocation_failed) { |
| 65 | JfrJavaSupport::throw_out_of_memory_error("Unable to create native recording thread for JFR" , CHECK_NULL); |
| 66 | } |
| 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 |
| 101 | const Thread* const t = start_thread(h_thread_oop, recorderthread_entry,THREAD); |
| 102 | if (!HAS_PENDING_EXCEPTION) { |
| 103 | cp_manager->register_service_thread(t); |
| 104 | return true; |
| 105 | } |
| 106 | assert(HAS_PENDING_EXCEPTION, "invariant" ); |
| 107 | // Start failed, remove the thread from the system thread group |
| 108 | JavaValue void_result(T_VOID); |
| 109 | JfrJavaArguments remove_thread_args(&void_result); |
| 110 | remove_thread_args.set_klass(SystemDictionary::ThreadGroup_klass()); |
| 111 | remove_thread_args.set_name(vmSymbols::remove_method_name()); |
| 112 | remove_thread_args.set_signature(vmSymbols::thread_void_signature()); |
| 113 | remove_thread_args.set_receiver(Universe::system_thread_group()); |
| 114 | remove_thread_args.push_oop(h_thread_oop()); |
| 115 | CautiouslyPreserveExceptionMark cpe(THREAD); |
| 116 | JfrJavaSupport::call_special(&remove_thread_args, THREAD); |
| 117 | return false; |
| 118 | } |
| 119 | |