1 /*
2 * Copyright (c) 2017, 2018, 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 "logging/logStream.hpp"
27 #include "memory/allocation.inline.hpp"
28 #include "runtime/jniHandles.inline.hpp"
29 #include "runtime/sharedRuntime.hpp"
30 #include "runtime/thread.inline.hpp"
31 #include "runtime/threadSMR.inline.hpp"
32 #include "runtime/vmOperations.hpp"
33 #include "services/threadIdTable.hpp"
34 #include "services/threadService.hpp"
35 #include "utilities/copy.hpp"
36 #include "utilities/globalDefinitions.hpp"
37 #include "utilities/ostream.hpp"
38 #include "utilities/resourceHash.hpp"
39 #include "utilities/vmError.hpp"
40
41 // The '_cnt', '_max' and '_times" fields are enabled via
42 // -XX:+EnableThreadSMRStatistics:
43
44 // # of parallel threads in _delete_lock->wait().
45 // Impl note: Hard to imagine > 64K waiting threads so this could be 16-bit,
46 // but there is no nice 16-bit _FORMAT support.
47 uint ThreadsSMRSupport::_delete_lock_wait_cnt = 0;
48
49 // Max # of parallel threads in _delete_lock->wait().
50 // Impl note: See _delete_lock_wait_cnt note.
51 uint ThreadsSMRSupport::_delete_lock_wait_max = 0;
52
53 // Flag to indicate when an _delete_lock->notify() is needed.
54 // Impl note: See _delete_lock_wait_cnt note.
55 volatile uint ThreadsSMRSupport::_delete_notify = 0;
56
57 // # of threads deleted over VM lifetime.
58 // Impl note: Atomically incremented over VM lifetime so use unsigned for more
59 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
60 // isn't available everywhere (or is it?).
61 volatile uint ThreadsSMRSupport::_deleted_thread_cnt = 0;
62
63 // Max time in millis to delete a thread.
64 // Impl note: 16-bit might be too small on an overloaded machine. Use
65 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
66 // loop for correctness.
67 volatile uint ThreadsSMRSupport::_deleted_thread_time_max = 0;
68
69 // Cumulative time in millis to delete threads.
70 // Impl note: Atomically added to over VM lifetime so use unsigned for more
71 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
72 // isn't available everywhere (or is it?).
73 volatile uint ThreadsSMRSupport::_deleted_thread_times = 0;
74
75 ThreadsList* volatile ThreadsSMRSupport::_java_thread_list = new ThreadsList(0);
76
77 // # of ThreadsLists allocated over VM lifetime.
78 // Impl note: We allocate a new ThreadsList for every thread create and
79 // every thread delete so we need a bigger type than the
80 // _deleted_thread_cnt field.
81 uint64_t ThreadsSMRSupport::_java_thread_list_alloc_cnt = 1;
82
83 // # of ThreadsLists freed over VM lifetime.
84 // Impl note: See _java_thread_list_alloc_cnt note.
85 uint64_t ThreadsSMRSupport::_java_thread_list_free_cnt = 0;
86
87 // Max size ThreadsList allocated.
88 // Impl note: Max # of threads alive at one time should fit in unsigned 32-bit.
89 uint ThreadsSMRSupport::_java_thread_list_max = 0;
90
91 // Max # of nested ThreadsLists for a thread.
92 // Impl note: Hard to imagine > 64K nested ThreadsLists so this could be
93 // 16-bit, but there is no nice 16-bit _FORMAT support.
94 uint ThreadsSMRSupport::_nested_thread_list_max = 0;
95
96 // # of ThreadsListHandles deleted over VM lifetime.
97 // Impl note: Atomically incremented over VM lifetime so use unsigned for
98 // more range. There will be fewer ThreadsListHandles than threads so
99 // unsigned 32-bit should be fine.
100 volatile uint ThreadsSMRSupport::_tlh_cnt = 0;
101
102 // Max time in millis to delete a ThreadsListHandle.
103 // Impl note: 16-bit might be too small on an overloaded machine. Use
104 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
105 // loop for correctness.
106 volatile uint ThreadsSMRSupport::_tlh_time_max = 0;
107
108 // Cumulative time in millis to delete ThreadsListHandles.
109 // Impl note: Atomically added to over VM lifetime so use unsigned for more
110 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
111 // isn't available everywhere (or is it?).
112 volatile uint ThreadsSMRSupport::_tlh_times = 0;
113
114 ThreadsList* ThreadsSMRSupport::_to_delete_list = NULL;
115
116 // # of parallel ThreadsLists on the to-delete list.
117 // Impl note: Hard to imagine > 64K ThreadsLists needing to be deleted so
118 // this could be 16-bit, but there is no nice 16-bit _FORMAT support.
119 uint ThreadsSMRSupport::_to_delete_list_cnt = 0;
120
121 // Max # of parallel ThreadsLists on the to-delete list.
122 // Impl note: See _to_delete_list_cnt note.
123 uint ThreadsSMRSupport::_to_delete_list_max = 0;
124
125 // 'inline' functions first so the definitions are before first use:
126
add_deleted_thread_times(uint add_value)127 inline void ThreadsSMRSupport::add_deleted_thread_times(uint add_value) {
128 Atomic::add(add_value, &_deleted_thread_times);
129 }
130
inc_deleted_thread_cnt()131 inline void ThreadsSMRSupport::inc_deleted_thread_cnt() {
132 Atomic::inc(&_deleted_thread_cnt);
133 }
134
inc_java_thread_list_alloc_cnt()135 inline void ThreadsSMRSupport::inc_java_thread_list_alloc_cnt() {
136 _java_thread_list_alloc_cnt++;
137 }
138
update_deleted_thread_time_max(uint new_value)139 inline void ThreadsSMRSupport::update_deleted_thread_time_max(uint new_value) {
140 while (true) {
141 uint cur_value = _deleted_thread_time_max;
142 if (new_value <= cur_value) {
143 // No need to update max value so we're done.
144 break;
145 }
146 if (Atomic::cmpxchg(new_value, &_deleted_thread_time_max, cur_value) == cur_value) {
147 // Updated max value so we're done. Otherwise try it all again.
148 break;
149 }
150 }
151 }
152
update_java_thread_list_max(uint new_value)153 inline void ThreadsSMRSupport::update_java_thread_list_max(uint new_value) {
154 if (new_value > _java_thread_list_max) {
155 _java_thread_list_max = new_value;
156 }
157 }
158
xchg_java_thread_list(ThreadsList * new_list)159 inline ThreadsList* ThreadsSMRSupport::xchg_java_thread_list(ThreadsList* new_list) {
160 return (ThreadsList*)Atomic::xchg(new_list, &_java_thread_list);
161 }
162
163
164 // Hash table of pointers found by a scan. Used for collecting hazard
165 // pointers (ThreadsList references). Also used for collecting JavaThreads
166 // that are indirectly referenced by hazard ptrs. An instance of this
167 // class only contains one type of pointer.
168 //
169 class ThreadScanHashtable : public CHeapObj<mtThread> {
170 private:
ptr_equals(void * const & s1,void * const & s2)171 static bool ptr_equals(void * const& s1, void * const& s2) {
172 return s1 == s2;
173 }
174
ptr_hash(void * const & s1)175 static unsigned int ptr_hash(void * const& s1) {
176 // 2654435761 = 2^32 * Phi (golden ratio)
177 return (unsigned int)(((uint32_t)(uintptr_t)s1) * 2654435761u);
178 }
179
180 int _table_size;
181 // ResourceHashtable SIZE is specified at compile time so our
182 // dynamic _table_size is unused for now; 1031 is the first prime
183 // after 1024.
184 typedef ResourceHashtable<void *, int, &ThreadScanHashtable::ptr_hash,
185 &ThreadScanHashtable::ptr_equals, 1031,
186 ResourceObj::C_HEAP, mtThread> PtrTable;
187 PtrTable * _ptrs;
188
189 public:
190 // ResourceHashtable is passed to various functions and populated in
191 // different places so we allocate it using C_HEAP to make it immune
192 // from any ResourceMarks that happen to be in the code paths.
ThreadScanHashtable(int table_size)193 ThreadScanHashtable(int table_size) : _table_size(table_size), _ptrs(new (ResourceObj::C_HEAP, mtThread) PtrTable()) {}
194
~ThreadScanHashtable()195 ~ThreadScanHashtable() { delete _ptrs; }
196
has_entry(void * pointer)197 bool has_entry(void *pointer) {
198 int *val_ptr = _ptrs->get(pointer);
199 return val_ptr != NULL && *val_ptr == 1;
200 }
201
add_entry(void * pointer)202 void add_entry(void *pointer) {
203 _ptrs->put(pointer, 1);
204 }
205 };
206
207 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
208 // (ThreadsList references) into a hash table. This closure handles part 2
209 // of the dance - adding all the JavaThreads referenced by the hazard
210 // pointer (ThreadsList reference) to the hash table.
211 //
212 class AddThreadHazardPointerThreadClosure : public ThreadClosure {
213 private:
214 ThreadScanHashtable *_table;
215
216 public:
AddThreadHazardPointerThreadClosure(ThreadScanHashtable * table)217 AddThreadHazardPointerThreadClosure(ThreadScanHashtable *table) : _table(table) {}
218
do_thread(Thread * thread)219 virtual void do_thread(Thread *thread) {
220 if (!_table->has_entry((void*)thread)) {
221 // The same JavaThread might be on more than one ThreadsList or
222 // more than one thread might be using the same ThreadsList. In
223 // either case, we only need a single entry for a JavaThread.
224 _table->add_entry((void*)thread);
225 }
226 }
227 };
228
229 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
230 // (ThreadsList references) into a hash table. This closure handles part 1
231 // of the dance - hazard ptr chain walking and dispatch to another
232 // closure.
233 //
234 class ScanHazardPtrGatherProtectedThreadsClosure : public ThreadClosure {
235 private:
236 ThreadScanHashtable *_table;
237 public:
ScanHazardPtrGatherProtectedThreadsClosure(ThreadScanHashtable * table)238 ScanHazardPtrGatherProtectedThreadsClosure(ThreadScanHashtable *table) : _table(table) {}
239
do_thread(Thread * thread)240 virtual void do_thread(Thread *thread) {
241 assert_locked_or_safepoint(Threads_lock);
242
243 if (thread == NULL) return;
244
245 // This code races with ThreadsSMRSupport::acquire_stable_list() which
246 // is lock-free so we have to handle some special situations.
247 //
248 ThreadsList *current_list = NULL;
249 while (true) {
250 current_list = thread->get_threads_hazard_ptr();
251 // No hazard ptr so nothing more to do.
252 if (current_list == NULL) {
253 return;
254 }
255
256 // If the hazard ptr is verified as stable (since it is not tagged),
257 // then it is safe to use.
258 if (!Thread::is_hazard_ptr_tagged(current_list)) break;
259
260 // The hazard ptr is tagged as not yet verified as being stable
261 // so we are racing with acquire_stable_list(). This exchange
262 // attempts to invalidate the hazard ptr. If we win the race,
263 // then we can ignore this unstable hazard ptr and the other
264 // thread will retry the attempt to publish a stable hazard ptr.
265 // If we lose the race, then we retry our attempt to look at the
266 // hazard ptr.
267 if (thread->cmpxchg_threads_hazard_ptr(NULL, current_list) == current_list) return;
268 }
269
270 // The current JavaThread has a hazard ptr (ThreadsList reference)
271 // which might be _java_thread_list or it might be an older
272 // ThreadsList that has been removed but not freed. In either case,
273 // the hazard ptr is protecting all the JavaThreads on that
274 // ThreadsList.
275 AddThreadHazardPointerThreadClosure add_cl(_table);
276 current_list->threads_do(&add_cl);
277 }
278 };
279
280 // Closure to gather hazard ptrs (ThreadsList references) into a hash table.
281 //
282 class ScanHazardPtrGatherThreadsListClosure : public ThreadClosure {
283 private:
284 ThreadScanHashtable *_table;
285 public:
ScanHazardPtrGatherThreadsListClosure(ThreadScanHashtable * table)286 ScanHazardPtrGatherThreadsListClosure(ThreadScanHashtable *table) : _table(table) {}
287
do_thread(Thread * thread)288 virtual void do_thread(Thread* thread) {
289 assert_locked_or_safepoint(Threads_lock);
290
291 if (thread == NULL) return;
292 ThreadsList *threads = thread->get_threads_hazard_ptr();
293 if (threads == NULL) {
294 return;
295 }
296 // In this closure we always ignore the tag that might mark this
297 // hazard ptr as not yet verified. If we happen to catch an
298 // unverified hazard ptr that is subsequently discarded (not
299 // published), then the only side effect is that we might keep a
300 // to-be-deleted ThreadsList alive a little longer.
301 threads = Thread::untag_hazard_ptr(threads);
302 if (!_table->has_entry((void*)threads)) {
303 _table->add_entry((void*)threads);
304 }
305 }
306 };
307
308 // Closure to print JavaThreads that have a hazard ptr (ThreadsList
309 // reference) that contains an indirect reference to a specific JavaThread.
310 //
311 class ScanHazardPtrPrintMatchingThreadsClosure : public ThreadClosure {
312 private:
313 JavaThread *_thread;
314 public:
ScanHazardPtrPrintMatchingThreadsClosure(JavaThread * thread)315 ScanHazardPtrPrintMatchingThreadsClosure(JavaThread *thread) : _thread(thread) {}
316
do_thread(Thread * thread)317 virtual void do_thread(Thread *thread) {
318 assert_locked_or_safepoint(Threads_lock);
319
320 if (thread == NULL) return;
321 ThreadsList *current_list = thread->get_threads_hazard_ptr();
322 if (current_list == NULL) {
323 return;
324 }
325 // If the hazard ptr is unverified, then ignore it.
326 if (Thread::is_hazard_ptr_tagged(current_list)) return;
327
328 // The current JavaThread has a hazard ptr (ThreadsList reference)
329 // which might be _java_thread_list or it might be an older
330 // ThreadsList that has been removed but not freed. In either case,
331 // the hazard ptr is protecting all the JavaThreads on that
332 // ThreadsList, but we only care about matching a specific JavaThread.
333 JavaThreadIterator jti(current_list);
334 for (JavaThread *p = jti.first(); p != NULL; p = jti.next()) {
335 if (p == _thread) {
336 log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread1=" INTPTR_FORMAT " has a hazard pointer for thread2=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread), p2i(_thread));
337 break;
338 }
339 }
340 }
341 };
342
343 // Closure to determine if the specified JavaThread is found by
344 // threads_do().
345 //
346 class VerifyHazardPtrThreadClosure : public ThreadClosure {
347 private:
348 bool _found;
349 Thread *_self;
350
351 public:
VerifyHazardPtrThreadClosure(Thread * self)352 VerifyHazardPtrThreadClosure(Thread *self) : _found(false), _self(self) {}
353
found() const354 bool found() const { return _found; }
355
do_thread(Thread * thread)356 virtual void do_thread(Thread *thread) {
357 if (thread == _self) {
358 _found = true;
359 }
360 }
361 };
362
363
364 // Acquire a stable ThreadsList.
365 //
acquire_stable_list()366 void SafeThreadsListPtr::acquire_stable_list() {
367 assert(_thread != NULL, "sanity check");
368 _needs_release = true;
369 _previous = _thread->_threads_list_ptr;
370 _thread->_threads_list_ptr = this;
371
372 if (_thread->get_threads_hazard_ptr() == NULL) {
373 // The typical case is first.
374 acquire_stable_list_fast_path();
375 return;
376 }
377
378 // The nested case is rare.
379 acquire_stable_list_nested_path();
380 }
381
382 // Fast path way to acquire a stable ThreadsList.
383 //
acquire_stable_list_fast_path()384 void SafeThreadsListPtr::acquire_stable_list_fast_path() {
385 assert(_thread != NULL, "sanity check");
386 assert(_thread->get_threads_hazard_ptr() == NULL, "sanity check");
387
388 ThreadsList* threads;
389
390 // Stable recording of a hazard ptr for SMR. This code does not use
391 // locks so its use of the _smr_java_thread_list & _threads_hazard_ptr
392 // fields is racy relative to code that uses those fields with locks.
393 // OrderAccess and Atomic functions are used to deal with those races.
394 //
395 while (true) {
396 threads = ThreadsSMRSupport::get_java_thread_list();
397
398 // Publish a tagged hazard ptr to denote that the hazard ptr is not
399 // yet verified as being stable. Due to the fence after the hazard
400 // ptr write, it will be sequentially consistent w.r.t. the
401 // sequentially consistent writes of the ThreadsList, even on
402 // non-multiple copy atomic machines where stores can be observed
403 // in different order from different observer threads.
404 ThreadsList* unverified_threads = Thread::tag_hazard_ptr(threads);
405 _thread->set_threads_hazard_ptr(unverified_threads);
406
407 // If _smr_java_thread_list has changed, we have lost a race with
408 // Threads::add() or Threads::remove() and have to try again.
409 if (ThreadsSMRSupport::get_java_thread_list() != threads) {
410 continue;
411 }
412
413 // We try to remove the tag which will verify the hazard ptr as
414 // being stable. This exchange can race with a scanning thread
415 // which might invalidate the tagged hazard ptr to keep it from
416 // being followed to access JavaThread ptrs. If we lose the race,
417 // we simply retry. If we win the race, then the stable hazard
418 // ptr is officially published.
419 if (_thread->cmpxchg_threads_hazard_ptr(threads, unverified_threads) == unverified_threads) {
420 break;
421 }
422 }
423
424 // A stable hazard ptr has been published letting other threads know
425 // that the ThreadsList and the JavaThreads reachable from this list
426 // are protected and hence they should not be deleted until everyone
427 // agrees it is safe to do so.
428
429 _list = threads;
430
431 verify_hazard_ptr_scanned();
432 }
433
434 // Acquire a nested stable ThreadsList; this is rare so it uses
435 // reference counting.
436 //
acquire_stable_list_nested_path()437 void SafeThreadsListPtr::acquire_stable_list_nested_path() {
438 assert(_thread != NULL, "sanity check");
439 assert(_thread->get_threads_hazard_ptr() != NULL,
440 "cannot have a NULL regular hazard ptr when acquiring a nested hazard ptr");
441
442 // The thread already has a hazard ptr (ThreadsList ref) so we need
443 // to create a nested ThreadsListHandle with the current ThreadsList
444 // since it might be different than our current hazard ptr. To remedy
445 // the situation, the ThreadsList pointed to by the pre-existing
446 // stable hazard ptr is reference counted before the hazard ptr may
447 // be released and moved to a new ThreadsList. The old ThreadsList
448 // is remembered in the ThreadsListHandle.
449
450 ThreadsList* current_list = _previous->_list;
451 if (EnableThreadSMRStatistics) {
452 _thread->inc_nested_threads_hazard_ptr_cnt();
453 }
454 current_list->inc_nested_handle_cnt();
455 _previous->_has_ref_count = true; // promote SafeThreadsListPtr to be reference counted
456 _thread->_threads_hazard_ptr = NULL; // clear the hazard ptr so we can go through the fast path below
457
458 if (EnableThreadSMRStatistics && _thread->nested_threads_hazard_ptr_cnt() > ThreadsSMRSupport::_nested_thread_list_max) {
459 ThreadsSMRSupport::_nested_thread_list_max = _thread->nested_threads_hazard_ptr_cnt();
460 }
461
462 acquire_stable_list_fast_path();
463
464 verify_hazard_ptr_scanned();
465
466 log_debug(thread, smr)("tid=" UINTX_FORMAT ": SafeThreadsListPtr::acquire_stable_list: add nested list pointer to ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(_list));
467 }
468
469 // Release a stable ThreadsList.
470 //
release_stable_list()471 void SafeThreadsListPtr::release_stable_list() {
472 assert(_thread != NULL, "sanity check");
473 assert(_thread->_threads_list_ptr == this, "sanity check");
474 _thread->_threads_list_ptr = _previous;
475
476 if (_has_ref_count) {
477 // If a SafeThreadsListPtr has been promoted to use reference counting
478 // due to nesting of ThreadsListHandles, then the reference count must be
479 // decremented, at which point it may be freed. The forgotten value of
480 // the list no longer matters at this point and should already be NULL.
481 assert(_thread->get_threads_hazard_ptr() == NULL, "sanity check");
482 if (EnableThreadSMRStatistics) {
483 _thread->dec_nested_threads_hazard_ptr_cnt();
484 }
485 _list->dec_nested_handle_cnt();
486
487 log_debug(thread, smr)("tid=" UINTX_FORMAT ": SafeThreadsListPtr::release_stable_list: delete nested list pointer to ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(_list));
488 } else {
489 // The normal case: a leaf ThreadsListHandle. This merely requires setting
490 // the thread hazard ptr back to NULL.
491 assert(_thread->get_threads_hazard_ptr() != NULL, "sanity check");
492 _thread->set_threads_hazard_ptr(NULL);
493 }
494
495 // After releasing the hazard ptr, other threads may go ahead and
496 // free up some memory temporarily used by a ThreadsList snapshot.
497
498 // We use double-check locking to reduce traffic on the system
499 // wide Thread-SMR delete_lock.
500 if (ThreadsSMRSupport::delete_notify()) {
501 // An exiting thread might be waiting in smr_delete(); we need to
502 // check with delete_lock to be sure.
503 ThreadsSMRSupport::release_stable_list_wake_up(_has_ref_count);
504 }
505 }
506
507 // Verify that the stable hazard ptr used to safely keep threads
508 // alive is scanned by threads_do() which is a key piece of honoring
509 // the Thread-SMR protocol.
verify_hazard_ptr_scanned()510 void SafeThreadsListPtr::verify_hazard_ptr_scanned() {
511 #ifdef ASSERT
512 assert(_list != NULL, "_list must not be NULL");
513
514 // The closure will attempt to verify that the calling thread can
515 // be found by threads_do() on the specified ThreadsList. If it
516 // is successful, then the specified ThreadsList was acquired as
517 // a stable hazard ptr by the calling thread in a way that honored
518 // the Thread-SMR protocol.
519 //
520 // If the calling thread cannot be found by threads_do() and if
521 // it is not the shutdown thread, then the calling thread is not
522 // honoring the Thread-SMR ptotocol. This means that the specified
523 // ThreadsList is not a stable hazard ptr and can be freed by
524 // another thread from the to-be-deleted list at any time.
525 //
526 // Note: The shutdown thread has removed itself from the Threads
527 // list and is safe to have a waiver from this check because
528 // VM_Exit::_shutdown_thread is not set until after the VMThread
529 // has started the final safepoint which holds the Threads_lock
530 // for the remainder of the VM's life.
531 //
532 VerifyHazardPtrThreadClosure cl(_thread);
533 ThreadsSMRSupport::threads_do(&cl, _list);
534
535 // If the calling thread is not honoring the Thread-SMR protocol,
536 // then we will either crash in threads_do() above because 'threads'
537 // was freed by another thread or we will fail the assert() below.
538 // In either case, we won't get past this point with a badly placed
539 // ThreadsListHandle.
540
541 assert(cl.found() || _thread == VM_Exit::shutdown_thread(), "Acquired a ThreadsList snapshot from a thread not recognized by the Thread-SMR protocol.");
542 #endif
543 }
544
545 // 'entries + 1' so we always have at least one entry.
ThreadsList(int entries)546 ThreadsList::ThreadsList(int entries) :
547 _length(entries),
548 _next_list(NULL),
549 _threads(NEW_C_HEAP_ARRAY(JavaThread*, entries + 1, mtThread)),
550 _nested_handle_cnt(0)
551 {
552 *(JavaThread**)(_threads + entries) = NULL; // Make sure the extra entry is NULL.
553 }
554
~ThreadsList()555 ThreadsList::~ThreadsList() {
556 FREE_C_HEAP_ARRAY(JavaThread*, _threads);
557 }
558
559 // Add a JavaThread to a ThreadsList. The returned ThreadsList is a
560 // new copy of the specified ThreadsList with the specified JavaThread
561 // appended to the end.
add_thread(ThreadsList * list,JavaThread * java_thread)562 ThreadsList *ThreadsList::add_thread(ThreadsList *list, JavaThread *java_thread) {
563 const uint index = list->_length;
564 const uint new_length = index + 1;
565 const uint head_length = index;
566 ThreadsList *const new_list = new ThreadsList(new_length);
567
568 if (head_length > 0) {
569 Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
570 }
571 *(JavaThread**)(new_list->_threads + index) = java_thread;
572
573 return new_list;
574 }
575
dec_nested_handle_cnt()576 void ThreadsList::dec_nested_handle_cnt() {
577 // The decrement needs to be MO_ACQ_REL. At the moment, the Atomic::dec
578 // backend on PPC does not yet conform to these requirements. Therefore
579 // the decrement is simulated with an Atomic::sub(1, &addr).
580 // Without this MO_ACQ_REL Atomic::dec simulation, the nested SMR mechanism
581 // is not generally safe to use.
582 Atomic::sub(1, &_nested_handle_cnt);
583 }
584
find_index_of_JavaThread(JavaThread * target)585 int ThreadsList::find_index_of_JavaThread(JavaThread *target) {
586 if (target == NULL) {
587 return -1;
588 }
589 for (uint i = 0; i < length(); i++) {
590 if (target == thread_at(i)) {
591 return (int)i;
592 }
593 }
594 return -1;
595 }
596
find_JavaThread_from_java_tid(jlong java_tid) const597 JavaThread* ThreadsList::find_JavaThread_from_java_tid(jlong java_tid) const {
598 ThreadIdTable::lazy_initialize(this);
599 JavaThread* thread = ThreadIdTable::find_thread_by_tid(java_tid);
600 if (thread == NULL) {
601 // If the thread is not found in the table find it
602 // with a linear search and add to the table.
603 for (uint i = 0; i < length(); i++) {
604 thread = thread_at(i);
605 oop tobj = thread->threadObj();
606 // Ignore the thread if it hasn't run yet, has exited
607 // or is starting to exit.
608 if (tobj != NULL && java_tid == java_lang_Thread::thread_id(tobj)) {
609 MutexLocker ml(Threads_lock);
610 // Must be inside the lock to ensure that we don't add a thread to the table
611 // that has just passed the removal point in ThreadsSMRSupport::remove_thread()
612 if (!thread->is_exiting()) {
613 ThreadIdTable::add_thread(java_tid, thread);
614 return thread;
615 }
616 }
617 }
618 } else if (!thread->is_exiting()) {
619 return thread;
620 }
621 return NULL;
622 }
623
inc_nested_handle_cnt()624 void ThreadsList::inc_nested_handle_cnt() {
625 // The increment needs to be MO_SEQ_CST. At the moment, the Atomic::inc
626 // backend on PPC does not yet conform to these requirements. Therefore
627 // the increment is simulated with a load phi; cas phi + 1; loop.
628 // Without this MO_SEQ_CST Atomic::inc simulation, the nested SMR mechanism
629 // is not generally safe to use.
630 intx sample = OrderAccess::load_acquire(&_nested_handle_cnt);
631 for (;;) {
632 if (Atomic::cmpxchg(sample + 1, &_nested_handle_cnt, sample) == sample) {
633 return;
634 } else {
635 sample = OrderAccess::load_acquire(&_nested_handle_cnt);
636 }
637 }
638 }
639
includes(const JavaThread * const p) const640 bool ThreadsList::includes(const JavaThread * const p) const {
641 if (p == NULL) {
642 return false;
643 }
644 for (uint i = 0; i < length(); i++) {
645 if (thread_at(i) == p) {
646 return true;
647 }
648 }
649 return false;
650 }
651
652 // Remove a JavaThread from a ThreadsList. The returned ThreadsList is a
653 // new copy of the specified ThreadsList with the specified JavaThread
654 // removed.
remove_thread(ThreadsList * list,JavaThread * java_thread)655 ThreadsList *ThreadsList::remove_thread(ThreadsList* list, JavaThread* java_thread) {
656 assert(list->_length > 0, "sanity");
657
658 uint i = (uint)list->find_index_of_JavaThread(java_thread);
659 assert(i < list->_length, "did not find JavaThread on the list");
660 const uint index = i;
661 const uint new_length = list->_length - 1;
662 const uint head_length = index;
663 const uint tail_length = (new_length >= index) ? (new_length - index) : 0;
664 ThreadsList *const new_list = new ThreadsList(new_length);
665
666 if (head_length > 0) {
667 Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
668 }
669 if (tail_length > 0) {
670 Copy::disjoint_words((HeapWord*)list->_threads + index + 1, (HeapWord*)new_list->_threads + index, tail_length);
671 }
672
673 return new_list;
674 }
675
ThreadsListHandle(Thread * self)676 ThreadsListHandle::ThreadsListHandle(Thread *self) : _list_ptr(self, /* acquire */ true) {
677 assert(self == Thread::current(), "sanity check");
678 if (EnableThreadSMRStatistics) {
679 _timer.start();
680 }
681 }
682
~ThreadsListHandle()683 ThreadsListHandle::~ThreadsListHandle() {
684 if (EnableThreadSMRStatistics) {
685 _timer.stop();
686 uint millis = (uint)_timer.milliseconds();
687 ThreadsSMRSupport::update_tlh_stats(millis);
688 }
689 }
690
691 // Convert an internal thread reference to a JavaThread found on the
692 // associated ThreadsList. This ThreadsListHandle "protects" the
693 // returned JavaThread *.
694 //
695 // If thread_oop_p is not NULL, then the caller wants to use the oop
696 // after this call so the oop is returned. On success, *jt_pp is set
697 // to the converted JavaThread * and true is returned. On error,
698 // returns false.
699 //
cv_internal_thread_to_JavaThread(jobject jthread,JavaThread ** jt_pp,oop * thread_oop_p)700 bool ThreadsListHandle::cv_internal_thread_to_JavaThread(jobject jthread,
701 JavaThread ** jt_pp,
702 oop * thread_oop_p) {
703 assert(this->list() != NULL, "must have a ThreadsList");
704 assert(jt_pp != NULL, "must have a return JavaThread pointer");
705 // thread_oop_p is optional so no assert()
706
707 // The JVM_* interfaces don't allow a NULL thread parameter; JVM/TI
708 // allows a NULL thread parameter to signify "current thread" which
709 // allows us to avoid calling cv_external_thread_to_JavaThread().
710 // The JVM_* interfaces have no such leeway.
711
712 oop thread_oop = JNIHandles::resolve_non_null(jthread);
713 // Looks like an oop at this point.
714 if (thread_oop_p != NULL) {
715 // Return the oop to the caller; the caller may still want
716 // the oop even if this function returns false.
717 *thread_oop_p = thread_oop;
718 }
719
720 JavaThread *java_thread = java_lang_Thread::thread(thread_oop);
721 if (java_thread == NULL) {
722 // The java.lang.Thread does not contain a JavaThread * so it has
723 // not yet run or it has died.
724 return false;
725 }
726 // Looks like a live JavaThread at this point.
727
728 if (java_thread != JavaThread::current()) {
729 // jthread is not for the current JavaThread so have to verify
730 // the JavaThread * against the ThreadsList.
731 if (EnableThreadSMRExtraValidityChecks && !includes(java_thread)) {
732 // Not on the JavaThreads list so it is not alive.
733 return false;
734 }
735 }
736
737 // Return a live JavaThread that is "protected" by the
738 // ThreadsListHandle in the caller.
739 *jt_pp = java_thread;
740 return true;
741 }
742
add_thread(JavaThread * thread)743 void ThreadsSMRSupport::add_thread(JavaThread *thread){
744 ThreadsList *new_list = ThreadsList::add_thread(get_java_thread_list(), thread);
745 if (EnableThreadSMRStatistics) {
746 inc_java_thread_list_alloc_cnt();
747 update_java_thread_list_max(new_list->length());
748 }
749 // Initial _java_thread_list will not generate a "Threads::add" mesg.
750 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
751
752 ThreadsList *old_list = xchg_java_thread_list(new_list);
753 free_list(old_list);
754 if (ThreadIdTable::is_initialized()) {
755 jlong tid = SharedRuntime::get_java_tid(thread);
756 ThreadIdTable::add_thread(tid, thread);
757 }
758 }
759
760 // set_delete_notify() and clear_delete_notify() are called
761 // under the protection of the delete_lock, but we also use an
762 // Atomic operation to ensure the memory update is seen earlier than
763 // when the delete_lock is dropped.
764 //
clear_delete_notify()765 void ThreadsSMRSupport::clear_delete_notify() {
766 Atomic::dec(&_delete_notify);
767 }
768
delete_notify()769 bool ThreadsSMRSupport::delete_notify() {
770 // Use load_acquire() in order to see any updates to _delete_notify
771 // earlier than when delete_lock is grabbed.
772 return (OrderAccess::load_acquire(&_delete_notify) != 0);
773 }
774
775 // Safely free a ThreadsList after a Threads::add() or Threads::remove().
776 // The specified ThreadsList may not get deleted during this call if it
777 // is still in-use (referenced by a hazard ptr). Other ThreadsLists
778 // in the chain may get deleted by this call if they are no longer in-use.
free_list(ThreadsList * threads)779 void ThreadsSMRSupport::free_list(ThreadsList* threads) {
780 assert_locked_or_safepoint(Threads_lock);
781
782 threads->set_next_list(_to_delete_list);
783 _to_delete_list = threads;
784 if (EnableThreadSMRStatistics) {
785 _to_delete_list_cnt++;
786 if (_to_delete_list_cnt > _to_delete_list_max) {
787 _to_delete_list_max = _to_delete_list_cnt;
788 }
789 }
790
791 // Hash table size should be first power of two higher than twice the length of the ThreadsList
792 int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
793 hash_table_size--;
794 hash_table_size |= hash_table_size >> 1;
795 hash_table_size |= hash_table_size >> 2;
796 hash_table_size |= hash_table_size >> 4;
797 hash_table_size |= hash_table_size >> 8;
798 hash_table_size |= hash_table_size >> 16;
799 hash_table_size++;
800
801 // Gather a hash table of the current hazard ptrs:
802 ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
803 ScanHazardPtrGatherThreadsListClosure scan_cl(scan_table);
804 threads_do(&scan_cl);
805 OrderAccess::acquire(); // Must order reads of hazard ptr before reads of
806 // nested reference counters
807
808 // Walk through the linked list of pending freeable ThreadsLists
809 // and free the ones that are not referenced from hazard ptrs.
810 ThreadsList* current = _to_delete_list;
811 ThreadsList* prev = NULL;
812 ThreadsList* next = NULL;
813 bool threads_is_freed = false;
814 while (current != NULL) {
815 next = current->next_list();
816 if (!scan_table->has_entry((void*)current) && current->_nested_handle_cnt == 0) {
817 // This ThreadsList is not referenced by a hazard ptr.
818 if (prev != NULL) {
819 prev->set_next_list(next);
820 }
821 if (_to_delete_list == current) {
822 _to_delete_list = next;
823 }
824
825 log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::free_list: threads=" INTPTR_FORMAT " is freed.", os::current_thread_id(), p2i(current));
826 if (current == threads) threads_is_freed = true;
827 delete current;
828 if (EnableThreadSMRStatistics) {
829 _java_thread_list_free_cnt++;
830 _to_delete_list_cnt--;
831 }
832 } else {
833 prev = current;
834 }
835 current = next;
836 }
837
838 if (!threads_is_freed) {
839 // Only report "is not freed" on the original call to
840 // free_list() for this ThreadsList.
841 log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::free_list: threads=" INTPTR_FORMAT " is not freed.", os::current_thread_id(), p2i(threads));
842 }
843
844 delete scan_table;
845 }
846
847 // Return true if the specified JavaThread is protected by a hazard
848 // pointer (ThreadsList reference). Otherwise, returns false.
849 //
is_a_protected_JavaThread(JavaThread * thread)850 bool ThreadsSMRSupport::is_a_protected_JavaThread(JavaThread *thread) {
851 assert_locked_or_safepoint(Threads_lock);
852
853 // Hash table size should be first power of two higher than twice
854 // the length of the Threads list.
855 int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
856 hash_table_size--;
857 hash_table_size |= hash_table_size >> 1;
858 hash_table_size |= hash_table_size >> 2;
859 hash_table_size |= hash_table_size >> 4;
860 hash_table_size |= hash_table_size >> 8;
861 hash_table_size |= hash_table_size >> 16;
862 hash_table_size++;
863
864 // Gather a hash table of the JavaThreads indirectly referenced by
865 // hazard ptrs.
866 ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
867 ScanHazardPtrGatherProtectedThreadsClosure scan_cl(scan_table);
868 threads_do(&scan_cl);
869 OrderAccess::acquire(); // Must order reads of hazard ptr before reads of
870 // nested reference counters
871
872 // Walk through the linked list of pending freeable ThreadsLists
873 // and include the ones that are currently in use by a nested
874 // ThreadsListHandle in the search set.
875 ThreadsList* current = _to_delete_list;
876 while (current != NULL) {
877 if (current->_nested_handle_cnt != 0) {
878 // 'current' is in use by a nested ThreadsListHandle so the hazard
879 // ptr is protecting all the JavaThreads on that ThreadsList.
880 AddThreadHazardPointerThreadClosure add_cl(scan_table);
881 current->threads_do(&add_cl);
882 }
883 current = current->next_list();
884 }
885
886 bool thread_is_protected = false;
887 if (scan_table->has_entry((void*)thread)) {
888 thread_is_protected = true;
889 }
890 delete scan_table;
891 return thread_is_protected;
892 }
893
894 // Wake up portion of the release stable ThreadsList protocol;
895 // uses the delete_lock().
896 //
release_stable_list_wake_up(bool is_nested)897 void ThreadsSMRSupport::release_stable_list_wake_up(bool is_nested) {
898 const char* log_str = is_nested ? "nested hazard ptr" : "regular hazard ptr";
899
900 // Note: delete_lock is held in smr_delete() for the entire
901 // hazard ptr search so that we do not lose this notify() if
902 // the exiting thread has to wait. That code path also holds
903 // Threads_lock (which was grabbed before delete_lock) so that
904 // threads_do() can be called. This means the system can't start a
905 // safepoint which means this thread can't take too long to get to
906 // a safepoint because of being blocked on delete_lock.
907 //
908 MonitorLockerEx ml(ThreadsSMRSupport::delete_lock(), Monitor::_no_safepoint_check_flag);
909 if (ThreadsSMRSupport::delete_notify()) {
910 // Notify any exiting JavaThreads that are waiting in smr_delete()
911 // that we've released a ThreadsList.
912 ml.notify_all();
913 log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::release_stable_list notified %s", os::current_thread_id(), log_str);
914 }
915 }
916
remove_thread(JavaThread * thread)917 void ThreadsSMRSupport::remove_thread(JavaThread *thread) {
918 if (ThreadIdTable::is_initialized()) {
919 jlong tid = SharedRuntime::get_java_tid(thread);
920 ThreadIdTable::remove_thread(tid);
921 }
922 ThreadsList *new_list = ThreadsList::remove_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
923 if (EnableThreadSMRStatistics) {
924 ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
925 // This list is smaller so no need to check for a "longest" update.
926 }
927
928 // Final _java_thread_list will not generate a "Threads::remove" mesg.
929 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::remove: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
930
931 ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
932 ThreadsSMRSupport::free_list(old_list);
933 }
934
935 // See note for clear_delete_notify().
936 //
set_delete_notify()937 void ThreadsSMRSupport::set_delete_notify() {
938 Atomic::inc(&_delete_notify);
939 }
940
941 // Safely delete a JavaThread when it is no longer in use by a
942 // ThreadsListHandle.
943 //
smr_delete(JavaThread * thread)944 void ThreadsSMRSupport::smr_delete(JavaThread *thread) {
945 assert(!Threads_lock->owned_by_self(), "sanity");
946
947 bool has_logged_once = false;
948 elapsedTimer timer;
949 if (EnableThreadSMRStatistics) {
950 timer.start();
951 }
952
953 while (true) {
954 {
955 // No safepoint check because this JavaThread is not on the
956 // Threads list.
957 MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
958 // Cannot use a MonitorLockerEx helper here because we have
959 // to drop the Threads_lock first if we wait.
960 ThreadsSMRSupport::delete_lock()->lock_without_safepoint_check();
961 // Set the delete_notify flag after we grab delete_lock
962 // and before we scan hazard ptrs because we're doing
963 // double-check locking in release_stable_list().
964 ThreadsSMRSupport::set_delete_notify();
965
966 if (!is_a_protected_JavaThread(thread)) {
967 // This is the common case.
968 ThreadsSMRSupport::clear_delete_notify();
969 ThreadsSMRSupport::delete_lock()->unlock();
970 break;
971 }
972 if (!has_logged_once) {
973 has_logged_once = true;
974 log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread=" INTPTR_FORMAT " is not deleted.", os::current_thread_id(), p2i(thread));
975 if (log_is_enabled(Debug, os, thread)) {
976 ScanHazardPtrPrintMatchingThreadsClosure scan_cl(thread);
977 threads_do(&scan_cl);
978 ThreadsList* current = _to_delete_list;
979 while (current != NULL) {
980 if (current->_nested_handle_cnt != 0 && current->includes(thread)) {
981 log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: found nested hazard pointer to thread=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread));
982 }
983 current = current->next_list();
984 }
985 }
986 }
987 } // We have to drop the Threads_lock to wait or delete the thread
988
989 if (EnableThreadSMRStatistics) {
990 _delete_lock_wait_cnt++;
991 if (_delete_lock_wait_cnt > _delete_lock_wait_max) {
992 _delete_lock_wait_max = _delete_lock_wait_cnt;
993 }
994 }
995 // Wait for a release_stable_list() call before we check again. No
996 // safepoint check, no timeout, and not as suspend equivalent flag
997 // because this JavaThread is not on the Threads list.
998 ThreadsSMRSupport::delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
999 !Mutex::_as_suspend_equivalent_flag);
1000 if (EnableThreadSMRStatistics) {
1001 _delete_lock_wait_cnt--;
1002 }
1003
1004 ThreadsSMRSupport::clear_delete_notify();
1005 ThreadsSMRSupport::delete_lock()->unlock();
1006 // Retry the whole scenario.
1007 }
1008
1009 delete thread;
1010 if (EnableThreadSMRStatistics) {
1011 timer.stop();
1012 uint millis = (uint)timer.milliseconds();
1013 ThreadsSMRSupport::inc_deleted_thread_cnt();
1014 ThreadsSMRSupport::add_deleted_thread_times(millis);
1015 ThreadsSMRSupport::update_deleted_thread_time_max(millis);
1016 }
1017
1018 log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread=" INTPTR_FORMAT " is deleted.", os::current_thread_id(), p2i(thread));
1019 }
1020
1021 // Apply the closure to all threads in the system, with a snapshot of
1022 // all JavaThreads provided by the list parameter.
threads_do(ThreadClosure * tc,ThreadsList * list)1023 void ThreadsSMRSupport::threads_do(ThreadClosure *tc, ThreadsList *list) {
1024 list->threads_do(tc);
1025 Threads::non_java_threads_do(tc);
1026 }
1027
1028 // Apply the closure to all threads in the system.
threads_do(ThreadClosure * tc)1029 void ThreadsSMRSupport::threads_do(ThreadClosure *tc) {
1030 threads_do(tc, _java_thread_list);
1031 }
1032
1033
1034 // Debug, logging, and printing stuff at the end:
1035
1036 // Print SMR info for a SafeThreadsListPtr to a given output stream.
print_on(outputStream * st)1037 void SafeThreadsListPtr::print_on(outputStream* st) {
1038 if (this == _thread->_threads_list_ptr) {
1039 // The top level hazard ptr.
1040 st->print(" _threads_hazard_ptr=" INTPTR_FORMAT, p2i(_list));
1041 } else {
1042 // Nested hazard ptrs.
1043 st->print(", _nested_threads_hazard_ptr=" INTPTR_FORMAT, p2i(_list));
1044 }
1045 }
1046
1047 // Log Threads class SMR info.
log_statistics()1048 void ThreadsSMRSupport::log_statistics() {
1049 LogTarget(Info, thread, smr) log;
1050 if (log.is_enabled()) {
1051 LogStream out(log);
1052 print_info_on(&out);
1053 }
1054 }
1055
1056 // Print SMR info for a thread to a given output stream.
print_info_on(const Thread * thread,outputStream * st)1057 void ThreadsSMRSupport::print_info_on(const Thread* thread, outputStream* st) {
1058 if (thread->_threads_hazard_ptr != NULL) {
1059 st->print(" _threads_hazard_ptr=" INTPTR_FORMAT, p2i(thread->_threads_hazard_ptr));
1060 }
1061 if (EnableThreadSMRStatistics && thread->_threads_list_ptr != NULL) {
1062 // The count is only interesting if we have a _threads_list_ptr.
1063 st->print(", _nested_threads_hazard_ptr_cnt=%u", thread->_nested_threads_hazard_ptr_cnt);
1064 }
1065 if (SafepointSynchronize::is_at_safepoint() || Thread::current() == thread) {
1066 // It is only safe to walk the list if we're at a safepoint or the
1067 // calling thread is walking its own list.
1068 SafeThreadsListPtr* current = thread->_threads_list_ptr;
1069 if (current != NULL) {
1070 // Skip the top nesting level as it is always printed above.
1071 current = current->previous();
1072 }
1073 while (current != NULL) {
1074 current->print_on(st);
1075 current = current->previous();
1076 }
1077 }
1078 }
1079
1080 // Print Threads class SMR info.
print_info_on(outputStream * st)1081 void ThreadsSMRSupport::print_info_on(outputStream* st) {
1082 // Only grab the Threads_lock if we don't already own it and if we
1083 // are not reporting an error.
1084 // Note: Not grabbing the Threads_lock during error reporting is
1085 // dangerous because the data structures we want to print can be
1086 // freed concurrently. However, grabbing the Threads_lock during
1087 // error reporting can be equally dangerous since this thread might
1088 // block during error reporting or a nested error could leave the
1089 // Threads_lock held. The classic no win scenario.
1090 //
1091 MutexLockerEx ml((Threads_lock->owned_by_self() || VMError::is_error_reported()) ? NULL : Threads_lock);
1092
1093 st->print_cr("Threads class SMR info:");
1094 st->print_cr("_java_thread_list=" INTPTR_FORMAT ", length=%u, "
1095 "elements={", p2i(_java_thread_list),
1096 _java_thread_list->length());
1097 print_info_elements_on(st, _java_thread_list);
1098 st->print_cr("}");
1099 if (_to_delete_list != NULL) {
1100 st->print_cr("_to_delete_list=" INTPTR_FORMAT ", length=%u, "
1101 "elements={", p2i(_to_delete_list),
1102 _to_delete_list->length());
1103 print_info_elements_on(st, _to_delete_list);
1104 st->print_cr("}");
1105 for (ThreadsList *t_list = _to_delete_list->next_list();
1106 t_list != NULL; t_list = t_list->next_list()) {
1107 st->print("next-> " INTPTR_FORMAT ", length=%u, "
1108 "elements={", p2i(t_list), t_list->length());
1109 print_info_elements_on(st, t_list);
1110 st->print_cr("}");
1111 }
1112 }
1113 if (!EnableThreadSMRStatistics) {
1114 return;
1115 }
1116 st->print_cr("_java_thread_list_alloc_cnt=" UINT64_FORMAT ", "
1117 "_java_thread_list_free_cnt=" UINT64_FORMAT ", "
1118 "_java_thread_list_max=%u, "
1119 "_nested_thread_list_max=%u",
1120 _java_thread_list_alloc_cnt,
1121 _java_thread_list_free_cnt,
1122 _java_thread_list_max,
1123 _nested_thread_list_max);
1124 if (_tlh_cnt > 0) {
1125 st->print_cr("_tlh_cnt=%u"
1126 ", _tlh_times=%u"
1127 ", avg_tlh_time=%0.2f"
1128 ", _tlh_time_max=%u",
1129 _tlh_cnt, _tlh_times,
1130 ((double) _tlh_times / _tlh_cnt),
1131 _tlh_time_max);
1132 }
1133 if (_deleted_thread_cnt > 0) {
1134 st->print_cr("_deleted_thread_cnt=%u"
1135 ", _deleted_thread_times=%u"
1136 ", avg_deleted_thread_time=%0.2f"
1137 ", _deleted_thread_time_max=%u",
1138 _deleted_thread_cnt, _deleted_thread_times,
1139 ((double) _deleted_thread_times / _deleted_thread_cnt),
1140 _deleted_thread_time_max);
1141 }
1142 st->print_cr("_delete_lock_wait_cnt=%u, _delete_lock_wait_max=%u",
1143 _delete_lock_wait_cnt, _delete_lock_wait_max);
1144 st->print_cr("_to_delete_list_cnt=%u, _to_delete_list_max=%u",
1145 _to_delete_list_cnt, _to_delete_list_max);
1146 }
1147
1148 // Print ThreadsList elements (4 per line).
print_info_elements_on(outputStream * st,ThreadsList * t_list)1149 void ThreadsSMRSupport::print_info_elements_on(outputStream* st, ThreadsList* t_list) {
1150 uint cnt = 0;
1151 JavaThreadIterator jti(t_list);
1152 for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) {
1153 st->print(INTPTR_FORMAT, p2i(jt));
1154 if (cnt < t_list->length() - 1) {
1155 // Separate with comma or comma-space except for the last one.
1156 if (((cnt + 1) % 4) == 0) {
1157 // Four INTPTR_FORMAT fit on an 80 column line so end the
1158 // current line with just a comma.
1159 st->print_cr(",");
1160 } else {
1161 // Not the last one on the current line so use comma-space:
1162 st->print(", ");
1163 }
1164 } else {
1165 // Last one so just end the current line.
1166 st->cr();
1167 }
1168 cnt++;
1169 }
1170 }
1171