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