1 | /* |
2 | * Copyright (c) 2017, 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 | #ifndef SHARE_RUNTIME_THREADSMR_HPP |
26 | #define SHARE_RUNTIME_THREADSMR_HPP |
27 | |
28 | #include "memory/allocation.hpp" |
29 | #include "runtime/timer.hpp" |
30 | |
31 | class JavaThread; |
32 | class Monitor; |
33 | class outputStream; |
34 | class Thread; |
35 | class ThreadClosure; |
36 | |
37 | // Thread Safe Memory Reclamation (Thread-SMR) support. |
38 | // |
39 | // ThreadsListHandles are used to safely perform operations on one or more |
40 | // threads without the risk of the thread or threads exiting during the |
41 | // operation. It is no longer necessary to hold the Threads_lock to safely |
42 | // perform an operation on a target thread. |
43 | // |
44 | // There are several different ways to refer to java.lang.Thread objects |
45 | // so we have a few ways to get a protected JavaThread *: |
46 | // |
47 | // JNI jobject example: |
48 | // jobject jthread = ...; |
49 | // : |
50 | // ThreadsListHandle tlh; |
51 | // JavaThread* jt = NULL; |
52 | // bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &jt, NULL); |
53 | // if (is_alive) { |
54 | // : // do stuff with 'jt'... |
55 | // } |
56 | // |
57 | // JVM/TI jthread example: |
58 | // jthread thread = ...; |
59 | // : |
60 | // JavaThread* jt = NULL; |
61 | // ThreadsListHandle tlh; |
62 | // jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &jt, NULL); |
63 | // if (err != JVMTI_ERROR_NONE) { |
64 | // return err; |
65 | // } |
66 | // : // do stuff with 'jt'... |
67 | // |
68 | // JVM/TI oop example (this one should be very rare): |
69 | // oop thread_obj = ...; |
70 | // : |
71 | // JavaThread *jt = NULL; |
72 | // ThreadsListHandle tlh; |
73 | // jvmtiError err = JvmtiExport::cv_oop_to_JavaThread(tlh.list(), thread_obj, &jt); |
74 | // if (err != JVMTI_ERROR_NONE) { |
75 | // return err; |
76 | // } |
77 | // : // do stuff with 'jt'... |
78 | // |
79 | // A JavaThread * that is included in the ThreadsList that is held by |
80 | // a ThreadsListHandle is protected as long as the ThreadsListHandle |
81 | // remains in scope. The target JavaThread * may have logically exited, |
82 | // but that target JavaThread * will not be deleted until it is no |
83 | // longer protected by a ThreadsListHandle. |
84 | |
85 | |
86 | // SMR Support for the Threads class. |
87 | // |
88 | class ThreadsSMRSupport : AllStatic { |
89 | friend class VMStructs; |
90 | friend class SafeThreadsListPtr; // for _nested_thread_list_max, delete_notify(), release_stable_list_wake_up() access |
91 | |
92 | // The coordination between ThreadsSMRSupport::release_stable_list() and |
93 | // ThreadsSMRSupport::smr_delete() uses the delete_lock in order to |
94 | // reduce the traffic on the Threads_lock. |
95 | static Monitor* delete_lock() { return ThreadsSMRDelete_lock; } |
96 | |
97 | // The '_cnt', '_max' and '_times" fields are enabled via |
98 | // -XX:+EnableThreadSMRStatistics (see thread.cpp for a |
99 | // description about each field): |
100 | static uint _delete_lock_wait_cnt; |
101 | static uint _delete_lock_wait_max; |
102 | // The delete_notify flag is used for proper double-check |
103 | // locking in order to reduce the traffic on the system wide |
104 | // Thread-SMR delete_lock. |
105 | static volatile uint _delete_notify; |
106 | static volatile uint _deleted_thread_cnt; |
107 | static volatile uint _deleted_thread_time_max; |
108 | static volatile uint _deleted_thread_times; |
109 | static ThreadsList _bootstrap_list; |
110 | static ThreadsList* volatile _java_thread_list; |
111 | static uint64_t _java_thread_list_alloc_cnt; |
112 | static uint64_t _java_thread_list_free_cnt; |
113 | static uint _java_thread_list_max; |
114 | static uint _nested_thread_list_max; |
115 | static volatile uint _tlh_cnt; |
116 | static volatile uint _tlh_time_max; |
117 | static volatile uint _tlh_times; |
118 | static ThreadsList* _to_delete_list; |
119 | static uint _to_delete_list_cnt; |
120 | static uint _to_delete_list_max; |
121 | |
122 | static ThreadsList *acquire_stable_list_fast_path(Thread *self); |
123 | static ThreadsList *acquire_stable_list_nested_path(Thread *self); |
124 | static void add_deleted_thread_times(uint add_value); |
125 | static void add_tlh_times(uint add_value); |
126 | static void clear_delete_notify(); |
127 | static bool delete_notify(); |
128 | static void free_list(ThreadsList* threads); |
129 | static void inc_deleted_thread_cnt(); |
130 | static void inc_java_thread_list_alloc_cnt(); |
131 | static void inc_tlh_cnt(); |
132 | static bool is_a_protected_JavaThread(JavaThread *thread); |
133 | static void release_stable_list_wake_up(bool is_nested); |
134 | static void set_delete_notify(); |
135 | static void threads_do(ThreadClosure *tc); |
136 | static void threads_do(ThreadClosure *tc, ThreadsList *list); |
137 | static void update_deleted_thread_time_max(uint new_value); |
138 | static void update_java_thread_list_max(uint new_value); |
139 | static void update_tlh_time_max(uint new_value); |
140 | static void verify_hazard_ptr_scanned(Thread *self, ThreadsList *threads); |
141 | static ThreadsList* xchg_java_thread_list(ThreadsList* new_list); |
142 | |
143 | public: |
144 | static void add_thread(JavaThread *thread); |
145 | static ThreadsList* get_java_thread_list(); |
146 | static bool is_a_protected_JavaThread_with_lock(JavaThread *thread); |
147 | static bool is_bootstrap_list(ThreadsList* list); |
148 | static void remove_thread(JavaThread *thread); |
149 | static void smr_delete(JavaThread *thread); |
150 | static void update_tlh_stats(uint millis); |
151 | |
152 | // Logging and printing support: |
153 | static void log_statistics(); |
154 | static void print_info_elements_on(outputStream* st, ThreadsList* t_list); |
155 | static void print_info_on(outputStream* st); |
156 | static void print_info_on(const Thread* thread, outputStream* st); |
157 | }; |
158 | |
159 | // A fast list of JavaThreads. |
160 | // |
161 | class ThreadsList : public CHeapObj<mtThread> { |
162 | friend class VMStructs; |
163 | friend class SafeThreadsListPtr; // for {dec,inc}_nested_handle_cnt() access |
164 | friend class ThreadsSMRSupport; // for _nested_handle_cnt, {add,remove}_thread(), {,set_}next_list() access |
165 | |
166 | const uint _length; |
167 | ThreadsList* _next_list; |
168 | JavaThread *const *const _threads; |
169 | volatile intx _nested_handle_cnt; |
170 | |
171 | template <class T> |
172 | void threads_do_dispatch(T *cl, JavaThread *const thread) const; |
173 | |
174 | ThreadsList *next_list() const { return _next_list; } |
175 | void set_next_list(ThreadsList *list) { _next_list = list; } |
176 | |
177 | void inc_nested_handle_cnt(); |
178 | void dec_nested_handle_cnt(); |
179 | |
180 | static ThreadsList* add_thread(ThreadsList* list, JavaThread* java_thread); |
181 | static ThreadsList* remove_thread(ThreadsList* list, JavaThread* java_thread); |
182 | |
183 | public: |
184 | ThreadsList(int entries); |
185 | ~ThreadsList(); |
186 | |
187 | template <class T> |
188 | void threads_do(T *cl) const; |
189 | |
190 | uint length() const { return _length; } |
191 | |
192 | JavaThread *const thread_at(uint i) const { return _threads[i]; } |
193 | |
194 | JavaThread *const *threads() const { return _threads; } |
195 | |
196 | // Returns -1 if target is not found. |
197 | int find_index_of_JavaThread(JavaThread* target); |
198 | JavaThread* find_JavaThread_from_java_tid(jlong java_tid) const; |
199 | bool includes(const JavaThread * const p) const; |
200 | }; |
201 | |
202 | // An abstract safe ptr to a ThreadsList comprising either a stable hazard ptr |
203 | // for leaves, or a retained reference count for nested uses. The user of this |
204 | // API does not need to know which mechanism is providing the safety. |
205 | class SafeThreadsListPtr { |
206 | friend class ThreadsListSetter; |
207 | |
208 | SafeThreadsListPtr* _previous; |
209 | Thread* _thread; |
210 | ThreadsList* _list; |
211 | bool _has_ref_count; |
212 | bool _needs_release; |
213 | |
214 | void acquire_stable_list(); |
215 | void acquire_stable_list_fast_path(); |
216 | void acquire_stable_list_nested_path(); |
217 | |
218 | void release_stable_list(); |
219 | |
220 | void verify_hazard_ptr_scanned(); |
221 | |
222 | public: |
223 | // Constructor that attaches the list onto a thread. |
224 | SafeThreadsListPtr(Thread *thread, bool acquire) : |
225 | _previous(NULL), |
226 | _thread(thread), |
227 | _list(NULL), |
228 | _has_ref_count(false), |
229 | _needs_release(false) |
230 | { |
231 | if (acquire) { |
232 | acquire_stable_list(); |
233 | } |
234 | } |
235 | |
236 | // Constructor that transfers ownership of the pointer. |
237 | SafeThreadsListPtr(SafeThreadsListPtr& other) : |
238 | _previous(other._previous), |
239 | _thread(other._thread), |
240 | _list(other._list), |
241 | _has_ref_count(other._has_ref_count), |
242 | _needs_release(other._needs_release) |
243 | { |
244 | other._needs_release = false; |
245 | } |
246 | |
247 | ~SafeThreadsListPtr() { |
248 | if (_needs_release) { |
249 | release_stable_list(); |
250 | } |
251 | } |
252 | |
253 | ThreadsList* list() const { return _list; } |
254 | SafeThreadsListPtr* previous() const { return _previous; } |
255 | void print_on(outputStream* st); |
256 | }; |
257 | |
258 | // A helper to optionally set the hazard ptr in ourself. This helper can |
259 | // be used by ourself or by another thread. If the hazard ptr is set(), |
260 | // then the destructor will release it. |
261 | // |
262 | class ThreadsListSetter : public StackObj { |
263 | private: |
264 | SafeThreadsListPtr _list_ptr; |
265 | |
266 | public: |
267 | ThreadsListSetter() : _list_ptr(Thread::current(), /* acquire */ false) {} |
268 | ThreadsList* list() { return _list_ptr.list(); } |
269 | void set() { _list_ptr.acquire_stable_list(); } |
270 | bool is_set() { return _list_ptr._needs_release; } |
271 | }; |
272 | |
273 | // This stack allocated ThreadsListHandle keeps all JavaThreads in the |
274 | // ThreadsList from being deleted until it is safe. |
275 | // |
276 | class ThreadsListHandle : public StackObj { |
277 | SafeThreadsListPtr _list_ptr; |
278 | elapsedTimer _timer; // Enabled via -XX:+EnableThreadSMRStatistics. |
279 | |
280 | public: |
281 | ThreadsListHandle(Thread *self = Thread::current()); |
282 | ~ThreadsListHandle(); |
283 | |
284 | ThreadsList *list() const { |
285 | return _list_ptr.list(); |
286 | } |
287 | |
288 | template <class T> |
289 | void threads_do(T *cl) const { |
290 | return list()->threads_do(cl); |
291 | } |
292 | |
293 | bool cv_internal_thread_to_JavaThread(jobject jthread, JavaThread ** jt_pp, oop * thread_oop_p); |
294 | |
295 | bool includes(JavaThread* p) { |
296 | return list()->includes(p); |
297 | } |
298 | |
299 | uint length() const { |
300 | return list()->length(); |
301 | } |
302 | }; |
303 | |
304 | // This stack allocated JavaThreadIterator is used to walk the |
305 | // specified ThreadsList using the following style: |
306 | // |
307 | // JavaThreadIterator jti(t_list); |
308 | // for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) { |
309 | // ... |
310 | // } |
311 | // |
312 | class JavaThreadIterator : public StackObj { |
313 | ThreadsList * _list; |
314 | uint _index; |
315 | |
316 | public: |
317 | JavaThreadIterator(ThreadsList *list) : _list(list), _index(0) { |
318 | assert(list != NULL, "ThreadsList must not be NULL." ); |
319 | } |
320 | |
321 | JavaThread *first() { |
322 | _index = 0; |
323 | return _list->thread_at(_index); |
324 | } |
325 | |
326 | uint length() const { |
327 | return _list->length(); |
328 | } |
329 | |
330 | ThreadsList *list() const { |
331 | return _list; |
332 | } |
333 | |
334 | JavaThread *next() { |
335 | if (++_index >= length()) { |
336 | return NULL; |
337 | } |
338 | return _list->thread_at(_index); |
339 | } |
340 | }; |
341 | |
342 | // This stack allocated ThreadsListHandle and JavaThreadIterator combo |
343 | // is used to walk the ThreadsList in the included ThreadsListHandle |
344 | // using the following style: |
345 | // |
346 | // for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { |
347 | // ... |
348 | // } |
349 | // |
350 | class JavaThreadIteratorWithHandle : public StackObj { |
351 | ThreadsListHandle _tlh; |
352 | uint _index; |
353 | |
354 | public: |
355 | JavaThreadIteratorWithHandle() : _index(0) {} |
356 | |
357 | uint length() const { |
358 | return _tlh.length(); |
359 | } |
360 | |
361 | ThreadsList *list() const { |
362 | return _tlh.list(); |
363 | } |
364 | |
365 | JavaThread *next() { |
366 | if (_index >= length()) { |
367 | return NULL; |
368 | } |
369 | return _tlh.list()->thread_at(_index++); |
370 | } |
371 | |
372 | void rewind() { |
373 | _index = 0; |
374 | } |
375 | }; |
376 | |
377 | #endif // SHARE_RUNTIME_THREADSMR_HPP |
378 | |