src/share/vm/runtime/serviceThread.cpp

Print this page


   1 /*
   2  * Copyright (c) 2011, 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 "runtime/interfaceSupport.hpp"
  27 #include "runtime/javaCalls.hpp"
  28 #include "runtime/serviceThread.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 #include "prims/jvmtiImpl.hpp"
  31 #include "services/gcNotifier.hpp"


  32 
  33 ServiceThread* ServiceThread::_instance = NULL;
  34 
  35 void ServiceThread::initialize() {
  36   EXCEPTION_MARK;
  37 
  38   instanceKlassHandle klass (THREAD,  SystemDictionary::Thread_klass());
  39   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);
  40 
  41   const char* name = JDK_Version::is_gte_jdk17x_version() ?
  42       "Service Thread" : "Low Memory Detector";
  43 
  44   Handle string = java_lang_String::create_from_str(name, CHECK);
  45 
  46   // Initialize thread_oop to put it into the system threadGroup
  47   Handle thread_group (THREAD, Universe::system_thread_group());
  48   JavaValue result(T_VOID);
  49   JavaCalls::call_special(&result, thread_oop,
  50                           klass,
  51                           vmSymbols::object_initializer_name(),


  66       vm_exit_during_initialization("java.lang.OutOfMemoryError",
  67                                     "unable to create new native thread");
  68     }
  69 
  70     java_lang_Thread::set_thread(thread_oop(), thread);
  71     java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
  72     java_lang_Thread::set_daemon(thread_oop());
  73     thread->set_threadObj(thread_oop());
  74     _instance = thread;
  75 
  76     Threads::add(thread);
  77     Thread::start(thread);
  78   }
  79 }
  80 
  81 void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) {
  82   while (true) {
  83     bool sensors_changed = false;
  84     bool has_jvmti_events = false;
  85     bool has_gc_notification_event = false;

  86     JvmtiDeferredEvent jvmti_event;
  87     {
  88       // Need state transition ThreadBlockInVM so that this thread
  89       // will be handled by safepoint correctly when this thread is
  90       // notified at a safepoint.
  91 
  92       // This ThreadBlockInVM object is not also considered to be
  93       // suspend-equivalent because ServiceThread is not visible to
  94       // external suspension.
  95 
  96       ThreadBlockInVM tbivm(jt);
  97 
  98       MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
  99       while (!(sensors_changed = LowMemoryDetector::has_pending_requests()) &&
 100              !(has_jvmti_events = JvmtiDeferredEventQueue::has_events()) &&
 101               !(has_gc_notification_event = GCNotifier::has_event())) {

 102         // wait until one of the sensors has pending requests, or there is a
 103         // pending JVMTI event or JMX GC notification to post
 104         Service_lock->wait(Mutex::_no_safepoint_check_flag);
 105       }
 106 
 107       if (has_jvmti_events) {
 108         jvmti_event = JvmtiDeferredEventQueue::dequeue();
 109       }
 110     }
 111 
 112     if (has_jvmti_events) {
 113       jvmti_event.post();
 114     }
 115 
 116     if (sensors_changed) {
 117       LowMemoryDetector::process_sensor_changes(jt);
 118     }
 119 
 120     if(has_gc_notification_event) {
 121         GCNotifier::sendNotification(CHECK);
 122     }



 123   }

 124 }
 125 
 126 bool ServiceThread::is_service_thread(Thread* thread) {
 127   return thread == _instance;
 128 }
   1 /*
   2  * Copyright (c) 2012, 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 "runtime/interfaceSupport.hpp"
  27 #include "runtime/javaCalls.hpp"
  28 #include "runtime/serviceThread.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 #include "prims/jvmtiImpl.hpp"
  31 #include "services/gcNotifier.hpp"
  32 #include "services/diagnosticArgument.hpp"
  33 #include "services/diagnosticFramework.hpp"
  34 
  35 ServiceThread* ServiceThread::_instance = NULL;
  36 
  37 void ServiceThread::initialize() {
  38   EXCEPTION_MARK;
  39 
  40   instanceKlassHandle klass (THREAD,  SystemDictionary::Thread_klass());
  41   instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);
  42 
  43   const char* name = JDK_Version::is_gte_jdk17x_version() ?
  44       "Service Thread" : "Low Memory Detector";
  45 
  46   Handle string = java_lang_String::create_from_str(name, CHECK);
  47 
  48   // Initialize thread_oop to put it into the system threadGroup
  49   Handle thread_group (THREAD, Universe::system_thread_group());
  50   JavaValue result(T_VOID);
  51   JavaCalls::call_special(&result, thread_oop,
  52                           klass,
  53                           vmSymbols::object_initializer_name(),


  68       vm_exit_during_initialization("java.lang.OutOfMemoryError",
  69                                     "unable to create new native thread");
  70     }
  71 
  72     java_lang_Thread::set_thread(thread_oop(), thread);
  73     java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
  74     java_lang_Thread::set_daemon(thread_oop());
  75     thread->set_threadObj(thread_oop());
  76     _instance = thread;
  77 
  78     Threads::add(thread);
  79     Thread::start(thread);
  80   }
  81 }
  82 
  83 void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) {
  84   while (true) {
  85     bool sensors_changed = false;
  86     bool has_jvmti_events = false;
  87     bool has_gc_notification_event = false;
  88     bool has_dcmd_notification_event = false;
  89     JvmtiDeferredEvent jvmti_event;
  90     {
  91       // Need state transition ThreadBlockInVM so that this thread
  92       // will be handled by safepoint correctly when this thread is
  93       // notified at a safepoint.
  94 
  95       // This ThreadBlockInVM object is not also considered to be
  96       // suspend-equivalent because ServiceThread is not visible to
  97       // external suspension.
  98 
  99       ThreadBlockInVM tbivm(jt);
 100 
 101       MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
 102       while (!(sensors_changed = LowMemoryDetector::has_pending_requests()) &&
 103              !(has_jvmti_events = JvmtiDeferredEventQueue::has_events()) &&
 104               !(has_gc_notification_event = GCNotifier::has_event()) &&
 105               !(has_dcmd_notification_event = DCmdFactory::has_pending_jmx_notification())) {
 106         // wait until one of the sensors has pending requests, or there is a
 107         // pending JVMTI event or JMX GC notification to post
 108         Service_lock->wait(Mutex::_no_safepoint_check_flag);
 109       }
 110 
 111       if (has_jvmti_events) {
 112         jvmti_event = JvmtiDeferredEventQueue::dequeue();
 113       }
 114     }
 115 
 116     if (has_jvmti_events) {
 117       jvmti_event.post();
 118     }
 119 
 120     if (sensors_changed) {
 121       LowMemoryDetector::process_sensor_changes(jt);
 122     }
 123 
 124     if(has_gc_notification_event) {
 125         GCNotifier::sendNotification(CHECK);
 126     }
 127 
 128     if(has_dcmd_notification_event) {
 129       DCmdFactory::send_notification(CHECK);
 130     }
 131   }
 132 }
 133 
 134 bool ServiceThread::is_service_thread(Thread* thread) {
 135   return thread == _instance;
 136 }