src/share/vm/runtime/serviceThread.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "runtime/interfaceSupport.hpp"
aoqi@0 27 #include "runtime/javaCalls.hpp"
aoqi@0 28 #include "runtime/serviceThread.hpp"
aoqi@0 29 #include "runtime/mutexLocker.hpp"
aoqi@0 30 #include "prims/jvmtiImpl.hpp"
jcoomes@7164 31 #include "services/allocationContextService.hpp"
aoqi@0 32 #include "services/gcNotifier.hpp"
aoqi@0 33 #include "services/diagnosticArgument.hpp"
aoqi@0 34 #include "services/diagnosticFramework.hpp"
aoqi@0 35
aoqi@0 36 ServiceThread* ServiceThread::_instance = NULL;
aoqi@0 37
aoqi@0 38 void ServiceThread::initialize() {
aoqi@0 39 EXCEPTION_MARK;
aoqi@0 40
aoqi@0 41 instanceKlassHandle klass (THREAD, SystemDictionary::Thread_klass());
aoqi@0 42 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);
aoqi@0 43
aoqi@0 44 const char* name = JDK_Version::is_gte_jdk17x_version() ?
aoqi@0 45 "Service Thread" : "Low Memory Detector";
aoqi@0 46
aoqi@0 47 Handle string = java_lang_String::create_from_str(name, CHECK);
aoqi@0 48
aoqi@0 49 // Initialize thread_oop to put it into the system threadGroup
aoqi@0 50 Handle thread_group (THREAD, Universe::system_thread_group());
aoqi@0 51 JavaValue result(T_VOID);
aoqi@0 52 JavaCalls::call_special(&result, thread_oop,
aoqi@0 53 klass,
aoqi@0 54 vmSymbols::object_initializer_name(),
aoqi@0 55 vmSymbols::threadgroup_string_void_signature(),
aoqi@0 56 thread_group,
aoqi@0 57 string,
aoqi@0 58 CHECK);
aoqi@0 59
aoqi@0 60 {
aoqi@0 61 MutexLocker mu(Threads_lock);
aoqi@0 62 ServiceThread* thread = new ServiceThread(&service_thread_entry);
aoqi@0 63
aoqi@0 64 // At this point it may be possible that no osthread was created for the
aoqi@0 65 // JavaThread due to lack of memory. We would have to throw an exception
aoqi@0 66 // in that case. However, since this must work and we do not allow
aoqi@0 67 // exceptions anyway, check and abort if this fails.
aoqi@0 68 if (thread == NULL || thread->osthread() == NULL) {
aoqi@0 69 vm_exit_during_initialization("java.lang.OutOfMemoryError",
aoqi@0 70 "unable to create new native thread");
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 java_lang_Thread::set_thread(thread_oop(), thread);
aoqi@0 74 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
aoqi@0 75 java_lang_Thread::set_daemon(thread_oop());
aoqi@0 76 thread->set_threadObj(thread_oop());
aoqi@0 77 _instance = thread;
aoqi@0 78
aoqi@0 79 Threads::add(thread);
aoqi@0 80 Thread::start(thread);
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) {
aoqi@0 85 while (true) {
aoqi@0 86 bool sensors_changed = false;
aoqi@0 87 bool has_jvmti_events = false;
aoqi@0 88 bool has_gc_notification_event = false;
aoqi@0 89 bool has_dcmd_notification_event = false;
jcoomes@7164 90 bool acs_notify = false;
aoqi@0 91 JvmtiDeferredEvent jvmti_event;
aoqi@0 92 {
aoqi@0 93 // Need state transition ThreadBlockInVM so that this thread
aoqi@0 94 // will be handled by safepoint correctly when this thread is
aoqi@0 95 // notified at a safepoint.
aoqi@0 96
aoqi@0 97 // This ThreadBlockInVM object is not also considered to be
aoqi@0 98 // suspend-equivalent because ServiceThread is not visible to
aoqi@0 99 // external suspension.
aoqi@0 100
aoqi@0 101 ThreadBlockInVM tbivm(jt);
aoqi@0 102
aoqi@0 103 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 104 while (!(sensors_changed = LowMemoryDetector::has_pending_requests()) &&
aoqi@0 105 !(has_jvmti_events = JvmtiDeferredEventQueue::has_events()) &&
aoqi@0 106 !(has_gc_notification_event = GCNotifier::has_event()) &&
jcoomes@7164 107 !(has_dcmd_notification_event = DCmdFactory::has_pending_jmx_notification()) &&
jcoomes@7164 108 !(acs_notify = AllocationContextService::should_notify())) {
aoqi@0 109 // wait until one of the sensors has pending requests, or there is a
aoqi@0 110 // pending JVMTI event or JMX GC notification to post
aoqi@0 111 Service_lock->wait(Mutex::_no_safepoint_check_flag);
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 if (has_jvmti_events) {
aoqi@0 115 jvmti_event = JvmtiDeferredEventQueue::dequeue();
aoqi@0 116 }
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 if (has_jvmti_events) {
aoqi@0 120 jvmti_event.post();
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 if (sensors_changed) {
aoqi@0 124 LowMemoryDetector::process_sensor_changes(jt);
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 if(has_gc_notification_event) {
aoqi@0 128 GCNotifier::sendNotification(CHECK);
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 if(has_dcmd_notification_event) {
aoqi@0 132 DCmdFactory::send_notification(CHECK);
aoqi@0 133 }
jcoomes@7164 134
jcoomes@7164 135 if (acs_notify) {
jcoomes@7164 136 AllocationContextService::notify(CHECK);
jcoomes@7164 137 }
aoqi@0 138 }
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 bool ServiceThread::is_service_thread(Thread* thread) {
aoqi@0 142 return thread == _instance;
aoqi@0 143 }

mercurial