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   // Used by find_JavaThread_from_java_tid
 184   static bool is_valid_java_thread(jlong java_tid, JavaThread* jt);
 185 public:
 186   ThreadsList(int entries);
 187   ~ThreadsList();
 188 
 189   template <class T>
 190   void threads_do(T *cl) const;
 191 
 192   uint length() const                       { return _length; }
 193 
 194   JavaThread *const thread_at(uint i) const { return _threads[i]; }
 195 
 196   JavaThread *const *threads() const        { return _threads; }
 197 
 198   // Returns -1 if target is not found.
 199   int find_index_of_JavaThread(JavaThread* target);
 200   JavaThread* find_JavaThread_from_java_tid(jlong java_tid) const;
 201   bool includes(const JavaThread * const p) const;
 202 };
 203 
 204 // An abstract safe ptr to a ThreadsList comprising either a stable hazard ptr
 205 // for leaves, or a retained reference count for nested uses. The user of this
 206 // API does not need to know which mechanism is providing the safety.
 207 class SafeThreadsListPtr {
 208   friend class ThreadsListSetter;
 209 
 210   SafeThreadsListPtr* _previous;
 211   Thread*                 _thread;
 212   ThreadsList*            _list;
 213   bool                    _has_ref_count;
 214   bool                    _needs_release;
 215 
 216   void acquire_stable_list();
 217   void acquire_stable_list_fast_path();
 218   void acquire_stable_list_nested_path();
 219 
 220   void release_stable_list();
 221 
 222   void verify_hazard_ptr_scanned();
 223 
 224 public:
 225   // Constructor that attaches the list onto a thread.
 226   SafeThreadsListPtr(Thread *thread, bool acquire) :
 227     _previous(NULL),
 228     _thread(thread),
 229     _list(NULL),
 230     _has_ref_count(false),
 231     _needs_release(false)
 232   {
 233     if (acquire) {
 234       acquire_stable_list();
 235     }
 236   }
 237 
 238   // Constructor that transfers ownership of the pointer.
 239   SafeThreadsListPtr(SafeThreadsListPtr& other) :
 240     _previous(other._previous),
 241     _thread(other._thread),
 242     _list(other._list),
 243     _has_ref_count(other._has_ref_count),
 244     _needs_release(other._needs_release)
 245   {
 246     other._needs_release = false;
 247   }
 248 
 249   ~SafeThreadsListPtr() {
 250     if (_needs_release) {
 251       release_stable_list();
 252     }
 253   }
 254 
 255   ThreadsList* list() const { return _list; }
 256   SafeThreadsListPtr* previous() const { return _previous; }
 257   void print_on(outputStream* st);
 258 };
 259 
 260 // A helper to optionally set the hazard ptr in ourself. This helper can
 261 // be used by ourself or by another thread. If the hazard ptr is set(),
 262 // then the destructor will release it.
 263 //
 264 class ThreadsListSetter : public StackObj {
 265 private:
 266   SafeThreadsListPtr _list_ptr;
 267 
 268 public:
 269   ThreadsListSetter() : _list_ptr(Thread::current(), /* acquire */ false) {}
 270   ThreadsList* list() { return _list_ptr.list(); }
 271   void set() { _list_ptr.acquire_stable_list(); }
 272   bool is_set() { return _list_ptr._needs_release; }
 273 };
 274 
 275 // This stack allocated ThreadsListHandle keeps all JavaThreads in the
 276 // ThreadsList from being deleted until it is safe.
 277 //
 278 class ThreadsListHandle : public StackObj {
 279   SafeThreadsListPtr _list_ptr;
 280   elapsedTimer _timer;  // Enabled via -XX:+EnableThreadSMRStatistics.
 281 
 282 public:
 283   ThreadsListHandle(Thread *self = Thread::current());
 284   ~ThreadsListHandle();
 285 
 286   ThreadsList *list() const {
 287     return _list_ptr.list();
 288   }
 289 
 290   template <class T>
 291   void threads_do(T *cl) const {
 292     return list()->threads_do(cl);
 293   }
 294 
 295   bool cv_internal_thread_to_JavaThread(jobject jthread, JavaThread ** jt_pp, oop * thread_oop_p);
 296 
 297   bool includes(JavaThread* p) {
 298     return list()->includes(p);
 299   }
 300 
 301   uint length() const {
 302     return list()->length();
 303   }
 304 };
 305 
 306 // This stack allocated JavaThreadIterator is used to walk the
 307 // specified ThreadsList using the following style:
 308 //
 309 //   JavaThreadIterator jti(t_list);
 310 //   for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) {
 311 //     ...
 312 //   }
 313 //
 314 class JavaThreadIterator : public StackObj {
 315   ThreadsList * _list;
 316   uint _index;
 317 
 318 public:
 319   JavaThreadIterator(ThreadsList *list) : _list(list), _index(0) {
 320     assert(list != NULL, "ThreadsList must not be NULL.");
 321   }
 322 
 323   JavaThread *first() {
 324     _index = 0;
 325     return _list->thread_at(_index);
 326   }
 327 
 328   uint length() const {
 329     return _list->length();
 330   }
 331 
 332   ThreadsList *list() const {
 333     return _list;
 334   }
 335 
 336   JavaThread *next() {
 337     if (++_index >= length()) {
 338       return NULL;
 339     }
 340     return _list->thread_at(_index);
 341   }
 342 };
 343 
 344 // This stack allocated ThreadsListHandle and JavaThreadIterator combo
 345 // is used to walk the ThreadsList in the included ThreadsListHandle
 346 // using the following style:
 347 //
 348 //   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
 349 //     ...
 350 //   }
 351 //
 352 class JavaThreadIteratorWithHandle : public StackObj {
 353   ThreadsListHandle _tlh;
 354   uint _index;
 355 
 356 public:
 357   JavaThreadIteratorWithHandle() : _index(0) {}
 358 
 359   uint length() const {
 360     return _tlh.length();
 361   }
 362 
 363   ThreadsList *list() const {
 364     return _tlh.list();
 365   }
 366 
 367   JavaThread *next() {
 368     if (_index >= length()) {
 369       return NULL;
 370     }
 371     return _tlh.list()->thread_at(_index++);
 372   }
 373 
 374   void rewind() {
 375     _index = 0;
 376   }
 377 };
 378 
 379 #endif // SHARE_RUNTIME_THREADSMR_HPP