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() {
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 }
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 }
|
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() {
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 }
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 }
|