src/share/vm/runtime/serviceThread.cpp

Thu, 21 Nov 2013 12:30:35 -0800

author
kvn
date
Thu, 21 Nov 2013 12:30:35 -0800
changeset 6485
da862781b584
parent 5047
31a4e55f8c9d
child 6876
710a3c8b516e
child 7164
fa6c442c59ee
permissions
-rw-r--r--

Merge

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

mercurial