aoqi@0: /* aoqi@0: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "runtime/interfaceSupport.hpp" aoqi@0: #include "runtime/javaCalls.hpp" aoqi@0: #include "runtime/serviceThread.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: #include "prims/jvmtiImpl.hpp" jcoomes@7164: #include "services/allocationContextService.hpp" aoqi@0: #include "services/gcNotifier.hpp" aoqi@0: #include "services/diagnosticArgument.hpp" aoqi@0: #include "services/diagnosticFramework.hpp" aoqi@0: aoqi@0: ServiceThread* ServiceThread::_instance = NULL; aoqi@0: aoqi@0: void ServiceThread::initialize() { aoqi@0: EXCEPTION_MARK; aoqi@0: aoqi@0: instanceKlassHandle klass (THREAD, SystemDictionary::Thread_klass()); aoqi@0: instanceHandle thread_oop = klass->allocate_instance_handle(CHECK); aoqi@0: aoqi@0: const char* name = JDK_Version::is_gte_jdk17x_version() ? aoqi@0: "Service Thread" : "Low Memory Detector"; aoqi@0: aoqi@0: Handle string = java_lang_String::create_from_str(name, CHECK); aoqi@0: aoqi@0: // Initialize thread_oop to put it into the system threadGroup aoqi@0: Handle thread_group (THREAD, Universe::system_thread_group()); aoqi@0: JavaValue result(T_VOID); aoqi@0: JavaCalls::call_special(&result, thread_oop, aoqi@0: klass, aoqi@0: vmSymbols::object_initializer_name(), aoqi@0: vmSymbols::threadgroup_string_void_signature(), aoqi@0: thread_group, aoqi@0: string, aoqi@0: CHECK); aoqi@0: aoqi@0: { aoqi@0: MutexLocker mu(Threads_lock); aoqi@0: ServiceThread* thread = new ServiceThread(&service_thread_entry); aoqi@0: aoqi@0: // At this point it may be possible that no osthread was created for the aoqi@0: // JavaThread due to lack of memory. We would have to throw an exception aoqi@0: // in that case. However, since this must work and we do not allow aoqi@0: // exceptions anyway, check and abort if this fails. aoqi@0: if (thread == NULL || thread->osthread() == NULL) { aoqi@0: vm_exit_during_initialization("java.lang.OutOfMemoryError", aoqi@0: "unable to create new native thread"); aoqi@0: } aoqi@0: aoqi@0: java_lang_Thread::set_thread(thread_oop(), thread); aoqi@0: java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); aoqi@0: java_lang_Thread::set_daemon(thread_oop()); aoqi@0: thread->set_threadObj(thread_oop()); aoqi@0: _instance = thread; aoqi@0: aoqi@0: Threads::add(thread); aoqi@0: Thread::start(thread); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) { aoqi@0: while (true) { aoqi@0: bool sensors_changed = false; aoqi@0: bool has_jvmti_events = false; aoqi@0: bool has_gc_notification_event = false; aoqi@0: bool has_dcmd_notification_event = false; jcoomes@7164: bool acs_notify = false; aoqi@0: JvmtiDeferredEvent jvmti_event; aoqi@0: { aoqi@0: // Need state transition ThreadBlockInVM so that this thread aoqi@0: // will be handled by safepoint correctly when this thread is aoqi@0: // notified at a safepoint. aoqi@0: aoqi@0: // This ThreadBlockInVM object is not also considered to be aoqi@0: // suspend-equivalent because ServiceThread is not visible to aoqi@0: // external suspension. aoqi@0: aoqi@0: ThreadBlockInVM tbivm(jt); aoqi@0: aoqi@0: MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); aoqi@0: while (!(sensors_changed = LowMemoryDetector::has_pending_requests()) && aoqi@0: !(has_jvmti_events = JvmtiDeferredEventQueue::has_events()) && aoqi@0: !(has_gc_notification_event = GCNotifier::has_event()) && jcoomes@7164: !(has_dcmd_notification_event = DCmdFactory::has_pending_jmx_notification()) && jcoomes@7164: !(acs_notify = AllocationContextService::should_notify())) { aoqi@0: // wait until one of the sensors has pending requests, or there is a aoqi@0: // pending JVMTI event or JMX GC notification to post aoqi@0: Service_lock->wait(Mutex::_no_safepoint_check_flag); aoqi@0: } aoqi@0: aoqi@0: if (has_jvmti_events) { aoqi@0: jvmti_event = JvmtiDeferredEventQueue::dequeue(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (has_jvmti_events) { aoqi@0: jvmti_event.post(); aoqi@0: } aoqi@0: aoqi@0: if (sensors_changed) { aoqi@0: LowMemoryDetector::process_sensor_changes(jt); aoqi@0: } aoqi@0: aoqi@0: if(has_gc_notification_event) { aoqi@0: GCNotifier::sendNotification(CHECK); aoqi@0: } aoqi@0: aoqi@0: if(has_dcmd_notification_event) { aoqi@0: DCmdFactory::send_notification(CHECK); aoqi@0: } jcoomes@7164: jcoomes@7164: if (acs_notify) { jcoomes@7164: AllocationContextService::notify(CHECK); jcoomes@7164: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool ServiceThread::is_service_thread(Thread* thread) { aoqi@0: return thread == _instance; aoqi@0: }