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 "memory/universe.hpp"
  31 #include "runtime/handles.inline.hpp"
  32 #include "runtime/interfaceSupport.inline.hpp"
  33 #include "runtime/javaCalls.hpp"
  34 #include "runtime/jniHandles.hpp"
  35 #include "runtime/serviceThread.hpp"
  36 #include "runtime/mutexLocker.hpp"
  37 #include "runtime/os.hpp"
  38 #include "prims/jvmtiImpl.hpp"
  39 #include "prims/resolvedMethodTable.hpp"
  40 
  41 ServiceThread* ServiceThread::_instance = NULL;
  42 
  43 void ServiceThread::initialize() {
  44   EXCEPTION_MARK;
  45 
  46   const char* name = "Service Thread";
  47   Handle string = java_lang_String::create_from_str(name, CHECK);
  48 
  49   // Initialize thread_oop to put it into the system threadGroup
  50   Handle thread_group (THREAD, Universe::system_thread_group());
  51   Handle thread_oop = JavaCalls::construct_new_instance(
  52                           SystemDictionary::Thread_klass(),
  53                           vmSymbols::threadgroup_string_void_signature(),
  54                           thread_group,
  55                           string,
  56                           CHECK);
  57 
  58   {
  59     MutexLocker mu(Threads_lock);
  60     ServiceThread* thread =  new ServiceThread(&service_thread_entry);
  61 
  62     // At this point it may be possible that no osthread was created for the
  63     // JavaThread due to lack of memory. We would have to throw an exception
  64     // in that case. However, since this must work and we do not allow
  65     // exceptions anyway, check and abort if this fails.
  66     if (thread == NULL || thread->osthread() == NULL) {
  67       vm_exit_during_initialization("java.lang.OutOfMemoryError",
  68                                     os::native_thread_creation_failed_msg());
  69     }
  70 
  71     java_lang_Thread::set_thread(thread_oop(), thread);
  72     java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
  73     java_lang_Thread::set_daemon(thread_oop());
  74     thread->set_threadObj(thread_oop());
  75     _instance = thread;
  76 
  77     Threads::add(thread);
  78     Thread::start(thread);
  79   }
  80 }
  81 
  82 static bool needs_oopstorage_cleanup(OopStorage* const* storages,
  83                                      bool* needs_cleanup,
  84                                      size_t size) {
  85   bool any_needs_cleanup = false;
  86   for (size_t i = 0; i < size; ++i) {
  87     assert(!needs_cleanup[i], "precondition");
  88     if (storages[i]->needs_delete_empty_blocks()) {
  89       needs_cleanup[i] = true;
  90       any_needs_cleanup = true;
  91     }
  92   }
  93   return any_needs_cleanup;
  94 }
  95 
  96 static void cleanup_oopstorages(OopStorage* const* storages,
  97                                 const bool* needs_cleanup,
  98                                 size_t size) {
  99   for (size_t i = 0; i < size; ++i) {
 100     if (needs_cleanup[i]) {
 101       storages[i]->delete_empty_blocks();
 102     }
 103   }
 104 }
 105 
 106 void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) {
 107   OopStorage* const oopstorages[] = {
 108     JNIHandles::global_handles(),
 109     JNIHandles::weak_global_handles(),
 110     StringTable::weak_storage(),
 111     SystemDictionary::vm_weak_oop_storage()
 112   };
 113   const size_t oopstorage_count = ARRAY_SIZE(oopstorages);
 114 
 115   while (true) {
 116     bool has_jvmti_events = false;
 117     bool stringtable_work = false;
 118     bool symboltable_work = false;
 119     bool resolved_method_table_work = false;
 120     bool protection_domain_table_work = false;
 121     bool oopstorage_work = false;
 122     bool oopstorages_cleanup[oopstorage_count] = {}; // Zero (false) initialize.
 123     JvmtiDeferredEvent jvmti_event;
 124     {
 125       // Need state transition ThreadBlockInVM so that this thread
 126       // will be handled by safepoint correctly when this thread is
 127       // notified at a safepoint.
 128 
 129       // This ThreadBlockInVM object is not also considered to be
 130       // suspend-equivalent because ServiceThread is not visible to
 131       // external suspension.
 132 
 133       ThreadBlockInVM tbivm(jt);
 134 
 135       MonitorLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
 136       // Process all available work on each (outer) iteration, rather than
 137       // only the first recognized bit of work, to avoid frequently true early
 138       // tests from potentially starving later work.  Hence the use of
 139       // arithmetic-or to combine results; we don't want short-circuiting.
 140       while (((has_jvmti_events = JvmtiDeferredEventQueue::has_events()) |              
 141               (stringtable_work = StringTable::has_work()) |
 142               (symboltable_work = SymbolTable::has_work()) |
 143               (resolved_method_table_work = ResolvedMethodTable::has_work()) |
 144               (protection_domain_table_work = SystemDictionary::pd_cache_table()->has_work()) |
 145               (oopstorage_work = needs_oopstorage_cleanup(oopstorages,
 146                                                           oopstorages_cleanup,
 147                                                           oopstorage_count)))
 148 
 149              == 0) {
 150         // Wait until notified that there is some work to do.
 151         ml.wait();
 152       }
 153 
 154       if (has_jvmti_events) {
 155         jvmti_event = JvmtiDeferredEventQueue::dequeue();
 156       }
 157     }
 158 
 159     if (stringtable_work) {
 160       StringTable::do_concurrent_work(jt);
 161     }
 162 
 163     if (symboltable_work) {
 164       SymbolTable::do_concurrent_work(jt);
 165     }
 166 
 167     if (has_jvmti_events) {
 168       jvmti_event.post();
 169     }
 170 
 171     if (resolved_method_table_work) {
 172       ResolvedMethodTable::do_concurrent_work(jt);
 173     }
 174 
 175     if (protection_domain_table_work) {
 176       SystemDictionary::pd_cache_table()->unlink();
 177     }
 178 
 179     if (oopstorage_work) {
 180       cleanup_oopstorages(oopstorages, oopstorages_cleanup, oopstorage_count);
 181     }
 182   }
 183 }
 184 
 185 bool ServiceThread::is_service_thread(Thread* thread) {
 186   return thread == _instance;
 187 }