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 "classfile/protectionDomainCache.hpp"
  27 #include "classfile/stringTable.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "gc/shared/oopStorage.hpp"
  31 #include "gc/shared/oopStorageSet.hpp"
  32 #include "memory/universe.hpp"
  33 #include "runtime/handles.inline.hpp"
  34 #include "runtime/interfaceSupport.inline.hpp"
  35 #include "runtime/javaCalls.hpp"
  36 #include "runtime/jniHandles.hpp"
  37 #include "runtime/serviceThread.hpp"
  38 #include "runtime/mutexLocker.hpp"
  39 #include "runtime/os.hpp"
  40 #include "prims/jvmtiImpl.hpp"
  41 #include "prims/resolvedMethodTable.hpp"
  42 #include "services/diagnosticArgument.hpp"
  43 #include "services/diagnosticFramework.hpp"
  44 #include "services/gcNotifier.hpp"
  45 #include "services/lowMemoryDetector.hpp"
  46 #include "services/threadIdTable.hpp"
  47 
  48 ServiceThread* ServiceThread::_instance = NULL;
  49 JvmtiDeferredEvent* ServiceThread::_jvmti_event = NULL;
  50 
  51 void ServiceThread::initialize() {
  52   EXCEPTION_MARK;
  53 
  54   const char* name = "Service Thread";
  55   Handle string = java_lang_String::create_from_str(name, CHECK);
  56 
  57   // Initialize thread_oop to put it into the system threadGroup
  58   Handle thread_group (THREAD, Universe::system_thread_group());
  59   Handle thread_oop = JavaCalls::construct_new_instance(
  60                           SystemDictionary::Thread_klass(),
  61                           vmSymbols::threadgroup_string_void_signature(),
  62                           thread_group,
  63                           string,
  64                           CHECK);
  65 
  66   {
  67     MutexLocker mu(Threads_lock);
  68     ServiceThread* thread =  new ServiceThread(&service_thread_entry);
  69 
  70     // At this point it may be possible that no osthread was created for the
  71     // JavaThread due to lack of memory. We would have to throw an exception
  72     // in that case. However, since this must work and we do not allow
  73     // exceptions anyway, check and abort if this fails.
  74     if (thread == NULL || thread->osthread() == NULL) {
  75       vm_exit_during_initialization("java.lang.OutOfMemoryError",
  76                                     os::native_thread_creation_failed_msg());
  77     }
  78 
  79     java_lang_Thread::set_thread(thread_oop(), thread);
  80     java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
  81     java_lang_Thread::set_daemon(thread_oop());
  82     thread->set_threadObj(thread_oop());
  83     _instance = thread;
  84 
  85     Threads::add(thread);
  86     Thread::start(thread);
  87   }
  88 }
  89 
  90 static void cleanup_oopstorages() {
  91   OopStorageSet::Iterator it = OopStorageSet::all_iterator();
  92   for ( ; !it.is_end(); ++it) {
  93     it->delete_empty_blocks();
  94   }
  95 }
  96 
  97 void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) {
  98   while (true) {
  99     bool sensors_changed = false;
 100     bool has_jvmti_events = false;
 101     bool has_gc_notification_event = false;
 102     bool has_dcmd_notification_event = false;
 103     bool stringtable_work = false;
 104     bool symboltable_work = false;
 105     bool resolved_method_table_work = false;
 106     bool thread_id_table_work = false;
 107     bool protection_domain_table_work = false;
 108     bool oopstorage_work = false;
 109     bool deflate_idle_monitors = false;
 110     JvmtiDeferredEvent jvmti_event;
 111     {
 112       // Need state transition ThreadBlockInVM so that this thread
 113       // will be handled by safepoint correctly when this thread is
 114       // notified at a safepoint.
 115 
 116       // This ThreadBlockInVM object is not also considered to be
 117       // suspend-equivalent because ServiceThread is not visible to
 118       // external suspension.
 119 
 120       ThreadBlockInVM tbivm(jt);
 121 
 122       MonitorLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
 123       // Process all available work on each (outer) iteration, rather than
 124       // only the first recognized bit of work, to avoid frequently true early
 125       // tests from potentially starving later work.  Hence the use of
 126       // arithmetic-or to combine results; we don't want short-circuiting.
 127       while (((sensors_changed = (!UseNotificationThread && LowMemoryDetector::has_pending_requests())) |
 128               (has_jvmti_events = JvmtiDeferredEventQueue::has_events()) |
 129               (has_gc_notification_event = (!UseNotificationThread && GCNotifier::has_event())) |
 130               (has_dcmd_notification_event = (!UseNotificationThread && DCmdFactory::has_pending_jmx_notification())) |
 131               (stringtable_work = StringTable::has_work()) |
 132               (symboltable_work = SymbolTable::has_work()) |
 133               (resolved_method_table_work = ResolvedMethodTable::has_work()) |
 134               (thread_id_table_work = ThreadIdTable::has_work()) |
 135               (protection_domain_table_work = SystemDictionary::pd_cache_table()->has_work()) |
 136               (oopstorage_work = OopStorage::has_cleanup_work_and_reset()) |
 137               (deflate_idle_monitors = ObjectSynchronizer::is_async_deflation_needed())
 138              ) == 0) {
 139         // Wait until notified that there is some work to do.
 140         // If AsyncDeflateIdleMonitors, then we wait for
 141         // GuaranteedSafepointInterval so that is_async_deflation_needed()
 142         // is checked at the same interval.
 143         ml.wait(AsyncDeflateIdleMonitors ? GuaranteedSafepointInterval : 0);
 144       }
 145 
 146       if (has_jvmti_events) {
 147         // Get the event under the Service_lock
 148         jvmti_event = JvmtiDeferredEventQueue::dequeue();
 149         _jvmti_event = &jvmti_event;
 150       }
 151     }
 152 
 153     if (stringtable_work) {
 154       StringTable::do_concurrent_work(jt);
 155     }
 156 
 157     if (symboltable_work) {
 158       SymbolTable::do_concurrent_work(jt);
 159     }
 160 
 161     if (has_jvmti_events) {
 162       _jvmti_event->post();
 163       _jvmti_event = NULL;  // reset
 164     }
 165 
 166     if (!UseNotificationThread) {
 167       if (sensors_changed) {
 168         LowMemoryDetector::process_sensor_changes(jt);
 169       }
 170 
 171       if(has_gc_notification_event) {
 172         GCNotifier::sendNotification(CHECK);
 173       }
 174 
 175       if(has_dcmd_notification_event) {
 176         DCmdFactory::send_notification(CHECK);
 177       }
 178     }
 179 
 180     if (resolved_method_table_work) {
 181       ResolvedMethodTable::do_concurrent_work(jt);
 182     }
 183 
 184     if (thread_id_table_work) {
 185       ThreadIdTable::do_concurrent_work(jt);
 186     }
 187 
 188     if (protection_domain_table_work) {
 189       SystemDictionary::pd_cache_table()->unlink();
 190     }
 191 
 192     if (oopstorage_work) {
 193       cleanup_oopstorages();
 194     }
 195 
 196     if (deflate_idle_monitors) {
 197       ObjectSynchronizer::deflate_idle_monitors_using_JT();
 198     }
 199   }
 200 }
 201 
 202 void ServiceThread::oops_do(OopClosure* f, CodeBlobClosure* cf) {
 203   JavaThread::oops_do(f, cf);
 204   // The ServiceThread "owns" the JVMTI Deferred events, scan them here
 205   // to keep them alive until they are processed.
 206   if (cf != NULL) {
 207     if (_jvmti_event != NULL) {
 208       _jvmti_event->oops_do(f, cf);
 209     }
 210     MutexLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
 211     JvmtiDeferredEventQueue::oops_do(f, cf);
 212   }
 213 }
 214 
 215 void ServiceThread::nmethods_do(CodeBlobClosure* cf) {
 216   JavaThread::nmethods_do(cf);
 217   if (cf != NULL) {
 218     if (_jvmti_event != NULL) {
 219       _jvmti_event->nmethods_do(cf);
 220     }
 221     MutexLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
 222     JvmtiDeferredEventQueue::nmethods_do(cf);
 223   }
 224 }