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