Path: blob/master/src/hotspot/share/services/threadIdTable.cpp
41145 views
1/*2* Copyright (c) 2019, 2020, 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "classfile/javaClasses.hpp"27#include "runtime/atomic.hpp"28#include "runtime/interfaceSupport.inline.hpp"29#include "runtime/thread.hpp"30#include "runtime/threadSMR.hpp"31#include "runtime/timerTrace.hpp"32#include "services/threadIdTable.hpp"33#include "utilities/concurrentHashTable.inline.hpp"34#include "utilities/concurrentHashTableTasks.inline.hpp"3536typedef ConcurrentHashTable<ThreadIdTableConfig, mtInternal> ThreadIdTableHash;3738// 2^24 is max size39static const size_t END_SIZE = 24;40// Default initial size 25641static const size_t DEFAULT_TABLE_SIZE_LOG = 8;42// Prefer short chains of avg 243static const double PREF_AVG_LIST_LEN = 2.0;44static ThreadIdTableHash* volatile _local_table = NULL;45static volatile size_t _current_size = 0;46static volatile size_t _items_count = 0;4748volatile bool ThreadIdTable::_is_initialized = false;49volatile bool ThreadIdTable::_has_work = false;5051class ThreadIdTableEntry : public CHeapObj<mtInternal> {52private:53jlong _tid;54JavaThread* _java_thread;55public:56ThreadIdTableEntry(jlong tid, JavaThread* java_thread) :57_tid(tid), _java_thread(java_thread) {}5859jlong tid() const { return _tid; }60JavaThread* thread() const { return _java_thread; }61};6263class ThreadIdTableConfig : public AllStatic {64public:65typedef ThreadIdTableEntry* Value;6667static uintx get_hash(Value const& value, bool* is_dead) {68jlong tid = value->tid();69return primitive_hash(tid);70}71static void* allocate_node(void* context, size_t size, Value const& value) {72ThreadIdTable::item_added();73return AllocateHeap(size, mtInternal);74}75static void free_node(void* context, void* memory, Value const& value) {76delete value;77FreeHeap(memory);78ThreadIdTable::item_removed();79}80};8182static size_t ceil_log2(size_t val) {83size_t ret;84for (ret = 1; ((size_t)1 << ret) < val; ++ret);85return ret;86}8788// Lazily creates the table and populates it with the given89// thread list90void ThreadIdTable::lazy_initialize(const ThreadsList *threads) {91if (!_is_initialized) {92{93// There is no obvious benefits in allowing the thread table94// to be concurently populated during the initalization.95MutexLocker ml(ThreadIdTableCreate_lock);96if (_is_initialized) {97return;98}99create_table(threads->length());100_is_initialized = true;101}102for (uint i = 0; i < threads->length(); i++) {103JavaThread* thread = threads->thread_at(i);104oop tobj = thread->threadObj();105if (tobj != NULL) {106jlong java_tid = java_lang_Thread::thread_id(tobj);107MutexLocker ml(Threads_lock);108if (!thread->is_exiting()) {109// Must be inside the lock to ensure that we don't add a thread to the table110// that has just passed the removal point in ThreadsSMRSupport::remove_thread()111add_thread(java_tid, thread);112}113}114}115}116}117118void ThreadIdTable::create_table(size_t size) {119assert(_local_table == NULL, "Thread table is already created");120size_t size_log = ceil_log2(size);121size_t start_size_log =122size_log > DEFAULT_TABLE_SIZE_LOG ? size_log : DEFAULT_TABLE_SIZE_LOG;123_current_size = (size_t)1 << start_size_log;124_local_table = new ThreadIdTableHash(start_size_log, END_SIZE);125}126127void ThreadIdTable::item_added() {128Atomic::inc(&_items_count);129log_trace(thread, table) ("Thread entry added");130}131132void ThreadIdTable::item_removed() {133Atomic::dec(&_items_count);134log_trace(thread, table) ("Thread entry removed");135}136137double ThreadIdTable::get_load_factor() {138return ((double)_items_count) / _current_size;139}140141size_t ThreadIdTable::table_size() {142return (size_t)1 << _local_table->get_size_log2(Thread::current());143}144145void ThreadIdTable::check_concurrent_work() {146if (_has_work) {147return;148}149150double load_factor = get_load_factor();151// Resize if we have more items than preferred load factor152if ( load_factor > PREF_AVG_LIST_LEN && !_local_table->is_max_size_reached()) {153log_debug(thread, table)("Concurrent work triggered, load factor: %g",154load_factor);155trigger_concurrent_work();156}157}158159void ThreadIdTable::trigger_concurrent_work() {160MutexLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);161_has_work = true;162Service_lock->notify_all();163}164165void ThreadIdTable::grow(JavaThread* jt) {166ThreadIdTableHash::GrowTask gt(_local_table);167if (!gt.prepare(jt)) {168return;169}170log_trace(thread, table)("Started to grow");171TraceTime timer("Grow", TRACETIME_LOG(Debug, membername, table, perf));172while (gt.do_task(jt)) {173gt.pause(jt);174{175ThreadBlockInVM tbivm(jt);176}177gt.cont(jt);178}179gt.done(jt);180_current_size = table_size();181log_info(thread, table)("Grown to size:" SIZE_FORMAT, _current_size);182}183184class ThreadIdTableLookup : public StackObj {185private:186jlong _tid;187uintx _hash;188public:189ThreadIdTableLookup(jlong tid)190: _tid(tid), _hash(primitive_hash(tid)) {}191uintx get_hash() const {192return _hash;193}194bool equals(ThreadIdTableEntry** value, bool* is_dead) {195bool equals = primitive_equals(_tid, (*value)->tid());196if (!equals) {197return false;198}199return true;200}201};202203class ThreadGet : public StackObj {204private:205JavaThread* _return;206public:207ThreadGet(): _return(NULL) {}208void operator()(ThreadIdTableEntry** val) {209_return = (*val)->thread();210}211JavaThread* get_res_thread() {212return _return;213}214};215216void ThreadIdTable::do_concurrent_work(JavaThread* jt) {217assert(_is_initialized, "Thread table is not initialized");218_has_work = false;219double load_factor = get_load_factor();220log_debug(thread, table)("Concurrent work, load factor: %g", load_factor);221if (load_factor > PREF_AVG_LIST_LEN && !_local_table->is_max_size_reached()) {222grow(jt);223}224}225226JavaThread* ThreadIdTable::add_thread(jlong tid, JavaThread* java_thread) {227assert(_is_initialized, "Thread table is not initialized");228Thread* thread = Thread::current();229ThreadIdTableLookup lookup(tid);230ThreadGet tg;231while (true) {232if (_local_table->get(thread, lookup, tg)) {233return tg.get_res_thread();234}235ThreadIdTableEntry* entry = new ThreadIdTableEntry(tid, java_thread);236// The hash table takes ownership of the ThreadTableEntry,237// even if it's not inserted.238if (_local_table->insert(thread, lookup, entry)) {239check_concurrent_work();240return java_thread;241}242}243}244245JavaThread* ThreadIdTable::find_thread_by_tid(jlong tid) {246assert(_is_initialized, "Thread table is not initialized");247Thread* thread = Thread::current();248ThreadIdTableLookup lookup(tid);249ThreadGet tg;250_local_table->get(thread, lookup, tg);251return tg.get_res_thread();252}253254bool ThreadIdTable::remove_thread(jlong tid) {255assert(_is_initialized, "Thread table is not initialized");256Thread* thread = Thread::current();257ThreadIdTableLookup lookup(tid);258return _local_table->remove(thread, lookup);259}260261262