1 /*
  2  * Copyright (c) 2012, 2024, 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/classLoaderDataGraph.inline.hpp"
 27 #include "classfile/javaClasses.hpp"
 28 #include "classfile/protectionDomainCache.hpp"
 29 #include "classfile/stringTable.hpp"
 30 #include "classfile/symbolTable.hpp"
 31 #include "classfile/systemDictionary.hpp"
 32 #include "classfile/vmClasses.hpp"
 33 #include "gc/shared/oopStorage.hpp"
 34 #include "gc/shared/oopStorageSet.hpp"
 35 #include "memory/universe.hpp"
 36 #include "interpreter/oopMapCache.hpp"
 37 #include "oops/oopHandle.inline.hpp"
 38 #include "runtime/handles.inline.hpp"
 39 #include "runtime/interfaceSupport.inline.hpp"
 40 #include "runtime/java.hpp"
 41 #include "runtime/javaCalls.hpp"
 42 #include "runtime/jniHandles.hpp"
 43 #include "runtime/serviceThread.hpp"
 44 #include "runtime/lightweightSynchronizer.hpp"
 45 #include "runtime/mutexLocker.hpp"
 46 #include "runtime/os.hpp"
 47 #include "prims/jvmtiImpl.hpp"
 48 #include "prims/jvmtiTagMap.hpp"
 49 #include "prims/resolvedMethodTable.hpp"
 50 #include "services/diagnosticArgument.hpp"
 51 #include "services/diagnosticFramework.hpp"
 52 #include "services/finalizerService.hpp"
 53 #include "services/gcNotifier.hpp"
 54 #include "services/lowMemoryDetector.hpp"
 55 #include "services/threadIdTable.hpp"
 56 
 57 DEBUG_ONLY(JavaThread* ServiceThread::_instance = nullptr;)
 58 JvmtiDeferredEvent* ServiceThread::_jvmti_event = nullptr;
 59 // The service thread has it's own static deferred event queue.
 60 // Events can be posted before JVMTI vm_start, so it's too early to call JvmtiThreadState::state_for
 61 // to add this field to the per-JavaThread event queue.  TODO: fix this sometime later
 62 JvmtiDeferredEventQueue ServiceThread::_jvmti_service_queue;
 63 
 64 void ServiceThread::initialize() {
 65   EXCEPTION_MARK;
 66 
 67   const char* name = "Service Thread";
 68   Handle thread_oop = JavaThread::create_system_thread_object(name, CHECK);
 69 
 70   ServiceThread* thread = new ServiceThread(&service_thread_entry);
 71   JavaThread::vm_exit_on_osthread_failure(thread);
 72 
 73   JavaThread::start_internal_daemon(THREAD, thread, thread_oop, NearMaxPriority);
 74   DEBUG_ONLY(_instance = thread;)
 75 }
 76 
 77 static void cleanup_oopstorages() {
 78   for (OopStorage* storage : OopStorageSet::Range<OopStorageSet::Id>()) {
 79     storage->delete_empty_blocks();
 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     bool stringtable_work = false;
 90     bool symboltable_work = false;
 91     bool finalizerservice_work = false;
 92     bool resolved_method_table_work = false;
 93     bool thread_id_table_work = false;
 94     bool protection_domain_table_work = false;
 95     bool oopstorage_work = false;
 96     JvmtiDeferredEvent jvmti_event;
 97     bool oop_handles_to_release = false;
 98     bool cldg_cleanup_work = false;
 99     bool jvmti_tagmap_work = false;
100     bool oopmap_cache_work = false;
101     bool omworldtable_work = false;
102     {
103       // Need state transition ThreadBlockInVM so that this thread
104       // will be handled by safepoint correctly when this thread is
105       // notified at a safepoint.
106 
107       // This ThreadBlockInVM object is not also considered to be
108       // suspend-equivalent because ServiceThread is not visible to
109       // external suspension.
110 
111       ThreadBlockInVM tbivm(jt);
112 
113       MonitorLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
114       // Process all available work on each (outer) iteration, rather than
115       // only the first recognized bit of work, to avoid frequently true early
116       // tests from potentially starving later work.  Hence the use of
117       // arithmetic-or to combine results; we don't want short-circuiting.
118       while (((sensors_changed = (!UseNotificationThread && LowMemoryDetector::has_pending_requests())) |
119               (has_jvmti_events = _jvmti_service_queue.has_events()) |
120               (has_gc_notification_event = (!UseNotificationThread && GCNotifier::has_event())) |
121               (has_dcmd_notification_event = (!UseNotificationThread && DCmdFactory::has_pending_jmx_notification())) |
122               (stringtable_work = StringTable::has_work()) |
123               (symboltable_work = SymbolTable::has_work()) |
124               (finalizerservice_work = FinalizerService::has_work()) |
125               (resolved_method_table_work = ResolvedMethodTable::has_work()) |
126               (thread_id_table_work = ThreadIdTable::has_work()) |
127               (protection_domain_table_work = ProtectionDomainCacheTable::has_work()) |
128               (oopstorage_work = OopStorage::has_cleanup_work_and_reset()) |
129               (oop_handles_to_release = JavaThread::has_oop_handles_to_release()) |
130               (cldg_cleanup_work = ClassLoaderDataGraph::should_clean_metaspaces_and_reset()) |
131               (jvmti_tagmap_work = JvmtiTagMap::has_object_free_events_and_reset()) |
132               (oopmap_cache_work = OopMapCache::has_cleanup_work()) |
133               (omworldtable_work = LightweightSynchronizer::needs_resize())
134              ) == 0) {
135         // Wait until notified that there is some work to do or timer expires.
136         // Some cleanup requests don't notify the ServiceThread so work needs to be done at periodic intervals.
137         ml.wait(ServiceThreadCleanupInterval);
138       }
139 
140       if (has_jvmti_events) {
141         // Get the event under the Service_lock
142         jvmti_event = _jvmti_service_queue.dequeue();
143         _jvmti_event = &jvmti_event;
144       }
145     }
146 
147     if (stringtable_work) {
148       StringTable::do_concurrent_work(jt);
149     }
150 
151     if (symboltable_work) {
152       SymbolTable::do_concurrent_work(jt);
153     }
154 
155     if (finalizerservice_work) {
156       FinalizerService::do_concurrent_work(jt);
157     }
158 
159     if (has_jvmti_events) {
160       _jvmti_event->post();
161       _jvmti_event = nullptr;  // reset
162     }
163 
164     if (!UseNotificationThread) {
165       if (sensors_changed) {
166         LowMemoryDetector::process_sensor_changes(jt);
167       }
168 
169       if(has_gc_notification_event) {
170         GCNotifier::sendNotification(CHECK);
171       }
172 
173       if(has_dcmd_notification_event) {
174         DCmdFactory::send_notification(CHECK);
175       }
176     }
177 
178     if (resolved_method_table_work) {
179       ResolvedMethodTable::do_concurrent_work(jt);
180     }
181 
182     if (thread_id_table_work) {
183       ThreadIdTable::do_concurrent_work(jt);
184     }
185 
186     if (protection_domain_table_work) {
187       ProtectionDomainCacheTable::unlink();
188     }
189 
190     if (oopstorage_work) {
191       cleanup_oopstorages();
192     }
193 
194     if (oop_handles_to_release) {
195       JavaThread::release_oop_handles();
196     }
197 
198     if (cldg_cleanup_work) {
199       ClassLoaderDataGraph::safepoint_and_clean_metaspaces();
200     }
201 
202     if (jvmti_tagmap_work) {
203       JvmtiTagMap::flush_all_object_free_events();
204     }
205 
206     if (oopmap_cache_work) {
207       OopMapCache::cleanup();
208     }
209 
210     if (omworldtable_work) {
211       LightweightSynchronizer::resize_table(jt);
212     }
213   }
214 }
215 
216 void ServiceThread::enqueue_deferred_event(JvmtiDeferredEvent* event) {
217   MutexLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
218   // If you enqueue events before the service thread runs, gc
219   // cannot keep the nmethod alive.  This could be restricted to compiled method
220   // load and unload events, if we wanted to be picky.
221   assert(_instance != nullptr, "cannot enqueue events before the service thread runs");
222   _jvmti_service_queue.enqueue(*event);
223   Service_lock->notify_all();
224  }
225 
226 void ServiceThread::oops_do_no_frames(OopClosure* f, NMethodClosure* cf) {
227   JavaThread::oops_do_no_frames(f, cf);
228   // The ServiceThread "owns" the JVMTI Deferred events, scan them here
229   // to keep them alive until they are processed.
230   if (_jvmti_event != nullptr) {
231     _jvmti_event->oops_do(f, cf);
232   }
233   // Requires a lock, because threads can be adding to this queue.
234   MutexLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
235   _jvmti_service_queue.oops_do(f, cf);
236 }
237 
238 void ServiceThread::nmethods_do(NMethodClosure* cf) {
239   JavaThread::nmethods_do(cf);
240   if (cf != nullptr) {
241     if (_jvmti_event != nullptr) {
242       _jvmti_event->nmethods_do(cf);
243     }
244     // Requires a lock, because threads can be adding to this queue.
245     MutexLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
246     _jvmti_service_queue.nmethods_do(cf);
247   }
248 }