src/hotspot/share/services/threadIdTable.cpp
changeset 58340 f4abe950c3b0
child 58504 94dd00d2da29
equal deleted inserted replaced
58339:c7d9df2e470c 58340:f4abe950c3b0
       
     1 
       
     2 /*
       
     3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     5 *
       
     6 * This code is free software; you can redistribute it and/or modify it
       
     7 * under the terms of the GNU General Public License version 2 only, as
       
     8 * published by the Free Software Foundation.
       
     9 *
       
    10 * This code is distributed in the hope that it will be useful, but WITHOUT
       
    11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    13 * version 2 for more details (a copy is included in the LICENSE file that
       
    14 * accompanied this code).
       
    15 *
       
    16 * You should have received a copy of the GNU General Public License version
       
    17 * 2 along with this work; if not, write to the Free Software Foundation,
       
    18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19 *
       
    20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    21 * or visit www.oracle.com if you need additional information or have any
       
    22 * questions.
       
    23 *
       
    24 */
       
    25 
       
    26 #include "precompiled.hpp"
       
    27 #include "runtime/interfaceSupport.inline.hpp"
       
    28 #include "runtime/thread.hpp"
       
    29 #include "runtime/threadSMR.hpp"
       
    30 #include "runtime/timerTrace.hpp"
       
    31 #include "services/threadIdTable.hpp"
       
    32 #include "utilities/concurrentHashTable.inline.hpp"
       
    33 #include "utilities/concurrentHashTableTasks.inline.hpp"
       
    34 
       
    35 
       
    36 typedef ConcurrentHashTable<ThreadIdTableConfig, mtInternal> ThreadIdTableHash;
       
    37 
       
    38 // 2^24 is max size
       
    39 static const size_t END_SIZE = 24;
       
    40 // Default initial size 256
       
    41 static const size_t DEFAULT_TABLE_SIZE_LOG = 8;
       
    42 // Prefer short chains of avg 2
       
    43 static const double PREF_AVG_LIST_LEN = 2.0;
       
    44 static ThreadIdTableHash* volatile _local_table = NULL;
       
    45 static volatile size_t _current_size = 0;
       
    46 static volatile size_t _items_count = 0;
       
    47 
       
    48 volatile bool ThreadIdTable::_is_initialized = false;
       
    49 
       
    50 class ThreadIdTableEntry : public CHeapObj<mtInternal> {
       
    51 private:
       
    52   jlong _tid;
       
    53   JavaThread* _java_thread;
       
    54 public:
       
    55   ThreadIdTableEntry(jlong tid, JavaThread* java_thread) :
       
    56     _tid(tid), _java_thread(java_thread) {}
       
    57 
       
    58   jlong tid() const { return _tid; }
       
    59   JavaThread* thread() const { return _java_thread; }
       
    60 };
       
    61 
       
    62 class ThreadIdTableConfig : public AllStatic {
       
    63   public:
       
    64     typedef ThreadIdTableEntry* Value;
       
    65 
       
    66     static uintx get_hash(Value const& value, bool* is_dead) {
       
    67       jlong tid = value->tid();
       
    68       return primitive_hash(tid);
       
    69     }
       
    70     static void* allocate_node(size_t size, Value const& value) {
       
    71       ThreadIdTable::item_added();
       
    72       return AllocateHeap(size, mtInternal);
       
    73     }
       
    74     static void free_node(void* memory, Value const& value) {
       
    75       delete value;
       
    76       FreeHeap(memory);
       
    77       ThreadIdTable::item_removed();
       
    78     }
       
    79 };
       
    80 
       
    81 static size_t ceil_log2(size_t val) {
       
    82   size_t ret;
       
    83   for (ret = 1; ((size_t)1 << ret) < val; ++ret);
       
    84   return ret;
       
    85 }
       
    86 
       
    87 // Lazily creates the table and populates it with the given
       
    88 // thread list
       
    89 void ThreadIdTable::lazy_initialize(const ThreadsList *threads) {
       
    90   if (!_is_initialized) {
       
    91     {
       
    92       // There is no obvious benefits in allowing the thread table
       
    93       // to be concurently populated during the initalization.
       
    94       MutexLocker ml(ThreadIdTableCreate_lock);
       
    95       if (_is_initialized) {
       
    96         return;
       
    97       }
       
    98       create_table(threads->length());
       
    99       _is_initialized = true;
       
   100     }
       
   101     for (uint i = 0; i < threads->length(); i++) {
       
   102       JavaThread* thread = threads->thread_at(i);
       
   103       oop tobj = thread->threadObj();
       
   104       if (tobj != NULL) {
       
   105         jlong java_tid = java_lang_Thread::thread_id(tobj);
       
   106         MutexLocker ml(Threads_lock);
       
   107         if (!thread->is_exiting()) {
       
   108           // Must be inside the lock to ensure that we don't add a thread to the table
       
   109           // that has just passed the removal point in ThreadsSMRSupport::remove_thread()
       
   110           add_thread(java_tid, thread);
       
   111         }
       
   112       }
       
   113     }
       
   114   }
       
   115 }
       
   116 
       
   117 void ThreadIdTable::create_table(size_t size) {
       
   118   assert(_local_table == NULL, "Thread table is already created");
       
   119   size_t size_log = ceil_log2(size);
       
   120   size_t start_size_log =
       
   121       size_log > DEFAULT_TABLE_SIZE_LOG ? size_log : DEFAULT_TABLE_SIZE_LOG;
       
   122   _current_size = (size_t)1 << start_size_log;
       
   123   _local_table = new ThreadIdTableHash(start_size_log, END_SIZE);
       
   124 }
       
   125 
       
   126 void ThreadIdTable::item_added() {
       
   127   Atomic::inc(&_items_count);
       
   128   log_trace(thread, table) ("Thread entry added");
       
   129 }
       
   130 
       
   131 void ThreadIdTable::item_removed() {
       
   132   Atomic::dec(&_items_count);
       
   133   log_trace(thread, table) ("Thread entry removed");
       
   134 }
       
   135 
       
   136 double ThreadIdTable::get_load_factor() {
       
   137   return ((double)_items_count) / _current_size;
       
   138 }
       
   139 
       
   140 size_t ThreadIdTable::table_size() {
       
   141   return (size_t)1 << _local_table->get_size_log2(Thread::current());
       
   142 }
       
   143 
       
   144 void ThreadIdTable::grow(JavaThread* jt) {
       
   145   ThreadIdTableHash::GrowTask gt(_local_table);
       
   146   if (!gt.prepare(jt)) {
       
   147     return;
       
   148   }
       
   149   log_trace(thread, table)("Started to grow");
       
   150   TraceTime timer("Grow", TRACETIME_LOG(Debug, membername, table, perf));
       
   151   while (gt.do_task(jt)) {
       
   152     gt.pause(jt);
       
   153     {
       
   154       ThreadBlockInVM tbivm(jt);
       
   155     }
       
   156     gt.cont(jt);
       
   157   }
       
   158   gt.done(jt);
       
   159   _current_size = table_size();
       
   160   log_info(thread, table)("Grown to size:" SIZE_FORMAT, _current_size);
       
   161 }
       
   162 
       
   163 class ThreadIdTableLookup : public StackObj {
       
   164 private:
       
   165   jlong _tid;
       
   166   uintx _hash;
       
   167 public:
       
   168   ThreadIdTableLookup(jlong tid)
       
   169     : _tid(tid), _hash(primitive_hash(tid)) {}
       
   170   uintx get_hash() const {
       
   171     return _hash;
       
   172   }
       
   173   bool equals(ThreadIdTableEntry** value, bool* is_dead) {
       
   174     bool equals = primitive_equals(_tid, (*value)->tid());
       
   175     if (!equals) {
       
   176       return false;
       
   177     }
       
   178     return true;
       
   179   }
       
   180 };
       
   181 
       
   182 class ThreadGet : public StackObj {
       
   183 private:
       
   184   JavaThread* _return;
       
   185 public:
       
   186   ThreadGet(): _return(NULL) {}
       
   187   void operator()(ThreadIdTableEntry** val) {
       
   188     _return = (*val)->thread();
       
   189   }
       
   190   JavaThread* get_res_thread() {
       
   191     return _return;
       
   192   }
       
   193 };
       
   194 
       
   195 void ThreadIdTable::grow_if_required() {
       
   196   assert(Thread::current()->is_Java_thread(),"Must be Java thread");
       
   197   assert(_is_initialized, "Thread table is not initialized");
       
   198   double load_factor = get_load_factor();
       
   199   log_debug(thread, table)("Concurrent work, load factor: %g", load_factor);
       
   200   if (load_factor > PREF_AVG_LIST_LEN && !_local_table->is_max_size_reached()) {
       
   201     grow(JavaThread::current());
       
   202   }
       
   203 }
       
   204 
       
   205 JavaThread* ThreadIdTable::add_thread(jlong tid, JavaThread* java_thread) {
       
   206   assert(_is_initialized, "Thread table is not initialized");
       
   207   Thread* thread = Thread::current();
       
   208   ThreadIdTableLookup lookup(tid);
       
   209   ThreadGet tg;
       
   210   while (true) {
       
   211     if (_local_table->get(thread, lookup, tg)) {
       
   212       return tg.get_res_thread();
       
   213     }
       
   214     ThreadIdTableEntry* entry = new ThreadIdTableEntry(tid, java_thread);
       
   215     // The hash table takes ownership of the ThreadTableEntry,
       
   216     // even if it's not inserted.
       
   217     if (_local_table->insert(thread, lookup, entry)) {
       
   218       grow_if_required();
       
   219       return java_thread;
       
   220     }
       
   221   }
       
   222 }
       
   223 
       
   224 JavaThread* ThreadIdTable::find_thread_by_tid(jlong tid) {
       
   225   assert(_is_initialized, "Thread table is not initialized");
       
   226   Thread* thread = Thread::current();
       
   227   ThreadIdTableLookup lookup(tid);
       
   228   ThreadGet tg;
       
   229   _local_table->get(thread, lookup, tg);
       
   230   return tg.get_res_thread();
       
   231 }
       
   232 
       
   233 bool ThreadIdTable::remove_thread(jlong tid) {
       
   234   assert(_is_initialized, "Thread table is not initialized");
       
   235   Thread* thread = Thread::current();
       
   236   ThreadIdTableLookup lookup(tid);
       
   237   return _local_table->remove(thread, lookup);
       
   238 }