mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
deps: V8: backport b49206d from upstream
This is the v8.x backport of https://github.com/nodejs/node/pull/20727. Original commit message: ThreadDataTable: Change global linked list to per-Isolate hash map. For use cases with a large number of threads or a large number of isolates (or both), ThreadDataTable can be a major performance bottleneck due to O(n) lookup time of the linked list. Switching to a hash map reduces this to O(1). Example 1: Sandstorm.io, a Node.js app that utilizes "fibers", was observed spending the majority of CPU time iterating over the ThreadDataTable. See: https://sandstorm.io/news/2016-09-30-fiber-bomb-debugging-story Example 2: Cloudflare's Workers engine, a high-multi-tenancy web server framework built on V8 (but not Node), creates large numbers of threads and isolates per-process. It saw a 34x improvement in throughput when we applied this patch. Cloudflare has been using a patch in production since the Worker launch which replaces the linked list with a hash map -- but still global. This commit builds on that but goes further and creates a separate hash map and mutex for each isolate, with the table being a member of the Isolate class. This avoids any globals and should reduce lock contention. Bug: v8:5338 Change-Id: If0d11509afb2e043b888c376e36d3463db931b47 Reviewed-on: https://chromium-review.googlesource.com/1014407 Reviewed-by: Yang Guo <yangguo@chromium.org> Commit-Queue: Yang Guo <yangguo@chromium.org> Cr-Commit-Position: refs/heads/master@{#52753} Backport-PR-URL: https://github.com/nodejs/node/pull/21529 PR-URL: https://github.com/nodejs/node/pull/20727 Refs: https://github.com/nodejs/node/issues/20083 Refs: https://github.com/nodejs/node/issues/20083 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
185aca054e
commit
f987a512d4
4 files changed
+45
-71
No files matched your search
Vendored
+1
-1
@@ -11,7 +11,7 @@
|
||||
#define V8_MAJOR_VERSION 6
|
||||
#define V8_MINOR_VERSION 2
|
||||
#define V8_BUILD_NUMBER 414
|
||||
#define V8_PATCH_LEVEL 57
|
||||
#define V8_PATCH_LEVEL 58
|
||||
|
||||
// Use 1 for candidates and 0 otherwise.
|
||||
// (Boolean macro values are not supported by all preprocessors.)
|
||||
|
||||
Vendored
+26
-59
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <fstream> // NOLINT(readability/streams)
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "src/assembler-inl.h"
|
||||
#include "src/ast/ast-value-factory.h"
|
||||
@@ -128,8 +129,6 @@ void ThreadLocalTop::Free() {
|
||||
base::Thread::LocalStorageKey Isolate::isolate_key_;
|
||||
base::Thread::LocalStorageKey Isolate::thread_id_key_;
|
||||
base::Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_;
|
||||
base::LazyMutex Isolate::thread_data_table_mutex_ = LAZY_MUTEX_INITIALIZER;
|
||||
Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL;
|
||||
base::Atomic32 Isolate::isolate_counter_ = 0;
|
||||
#if DEBUG
|
||||
base::Atomic32 Isolate::isolate_key_created_ = 0;
|
||||
@@ -140,13 +139,13 @@ Isolate::PerIsolateThreadData*
|
||||
ThreadId thread_id = ThreadId::Current();
|
||||
PerIsolateThreadData* per_thread = NULL;
|
||||
{
|
||||
base::LockGuard<base::Mutex> lock_guard(thread_data_table_mutex_.Pointer());
|
||||
per_thread = thread_data_table_->Lookup(this, thread_id);
|
||||
base::LockGuard<base::Mutex> lock_guard(&thread_data_table_mutex_);
|
||||
per_thread = thread_data_table_.Lookup(thread_id);
|
||||
if (per_thread == NULL) {
|
||||
per_thread = new PerIsolateThreadData(this, thread_id);
|
||||
thread_data_table_->Insert(per_thread);
|
||||
thread_data_table_.Insert(per_thread);
|
||||
}
|
||||
DCHECK(thread_data_table_->Lookup(this, thread_id) == per_thread);
|
||||
DCHECK(thread_data_table_.Lookup(thread_id) == per_thread);
|
||||
}
|
||||
return per_thread;
|
||||
}
|
||||
@@ -157,12 +156,11 @@ void Isolate::DiscardPerThreadDataForThisThread() {
|
||||
if (thread_id_int) {
|
||||
ThreadId thread_id = ThreadId(thread_id_int);
|
||||
DCHECK(!thread_manager_->mutex_owner_.Equals(thread_id));
|
||||
base::LockGuard<base::Mutex> lock_guard(thread_data_table_mutex_.Pointer());
|
||||
PerIsolateThreadData* per_thread =
|
||||
thread_data_table_->Lookup(this, thread_id);
|
||||
base::LockGuard<base::Mutex> lock_guard(&thread_data_table_mutex_);
|
||||
PerIsolateThreadData* per_thread = thread_data_table_.Lookup(thread_id);
|
||||
if (per_thread) {
|
||||
DCHECK(!per_thread->thread_state_);
|
||||
thread_data_table_->Remove(per_thread);
|
||||
thread_data_table_.Remove(per_thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -178,23 +176,20 @@ Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThread(
|
||||
ThreadId thread_id) {
|
||||
PerIsolateThreadData* per_thread = NULL;
|
||||
{
|
||||
base::LockGuard<base::Mutex> lock_guard(thread_data_table_mutex_.Pointer());
|
||||
per_thread = thread_data_table_->Lookup(this, thread_id);
|
||||
base::LockGuard<base::Mutex> lock_guard(&thread_data_table_mutex_);
|
||||
per_thread = thread_data_table_.Lookup(thread_id);
|
||||
}
|
||||
return per_thread;
|
||||
}
|
||||
|
||||
|
||||
void Isolate::InitializeOncePerProcess() {
|
||||
base::LockGuard<base::Mutex> lock_guard(thread_data_table_mutex_.Pointer());
|
||||
CHECK(thread_data_table_ == NULL);
|
||||
isolate_key_ = base::Thread::CreateThreadLocalKey();
|
||||
#if DEBUG
|
||||
base::Relaxed_Store(&isolate_key_created_, 1);
|
||||
#endif
|
||||
thread_id_key_ = base::Thread::CreateThreadLocalKey();
|
||||
per_isolate_thread_data_key_ = base::Thread::CreateThreadLocalKey();
|
||||
thread_data_table_ = new Isolate::ThreadDataTable();
|
||||
}
|
||||
|
||||
Address Isolate::get_address_from_id(IsolateAddressId id) {
|
||||
@@ -2093,18 +2088,9 @@ char* Isolate::RestoreThread(char* from) {
|
||||
return from + sizeof(ThreadLocalTop);
|
||||
}
|
||||
|
||||
Isolate::ThreadDataTable::ThreadDataTable() : table_() {}
|
||||
|
||||
Isolate::ThreadDataTable::ThreadDataTable()
|
||||
: list_(NULL) {
|
||||
}
|
||||
|
||||
|
||||
Isolate::ThreadDataTable::~ThreadDataTable() {
|
||||
// TODO(svenpanne) The assertion below would fire if an embedder does not
|
||||
// cleanly dispose all Isolates before disposing v8, so we are conservative
|
||||
// and leave it out for now.
|
||||
// DCHECK_NULL(list_);
|
||||
}
|
||||
Isolate::ThreadDataTable::~ThreadDataTable() {}
|
||||
|
||||
void Isolate::ReleaseManagedObjects() {
|
||||
Isolate::ManagedObjectFinalizer* current =
|
||||
@@ -2151,39 +2137,30 @@ Isolate::PerIsolateThreadData::~PerIsolateThreadData() {
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Isolate::PerIsolateThreadData*
|
||||
Isolate::ThreadDataTable::Lookup(Isolate* isolate,
|
||||
ThreadId thread_id) {
|
||||
for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) {
|
||||
if (data->Matches(isolate, thread_id)) return data;
|
||||
}
|
||||
return NULL;
|
||||
Isolate::PerIsolateThreadData* Isolate::ThreadDataTable::Lookup(
|
||||
ThreadId thread_id) {
|
||||
auto t = table_.find(thread_id);
|
||||
if (t == table_.end()) return nullptr;
|
||||
return t->second;
|
||||
}
|
||||
|
||||
|
||||
void Isolate::ThreadDataTable::Insert(Isolate::PerIsolateThreadData* data) {
|
||||
if (list_ != NULL) list_->prev_ = data;
|
||||
data->next_ = list_;
|
||||
list_ = data;
|
||||
bool inserted = table_.insert(std::make_pair(data->thread_id_, data)).second;
|
||||
CHECK(inserted);
|
||||
}
|
||||
|
||||
|
||||
void Isolate::ThreadDataTable::Remove(PerIsolateThreadData* data) {
|
||||
if (list_ == data) list_ = data->next_;
|
||||
if (data->next_ != NULL) data->next_->prev_ = data->prev_;
|
||||
if (data->prev_ != NULL) data->prev_->next_ = data->next_;
|
||||
table_.erase(data->thread_id_);
|
||||
delete data;
|
||||
}
|
||||
|
||||
|
||||
void Isolate::ThreadDataTable::RemoveAllThreads(Isolate* isolate) {
|
||||
PerIsolateThreadData* data = list_;
|
||||
while (data != NULL) {
|
||||
PerIsolateThreadData* next = data->next_;
|
||||
if (data->isolate() == isolate) Remove(data);
|
||||
data = next;
|
||||
void Isolate::ThreadDataTable::RemoveAllThreads() {
|
||||
for (auto& x : table_) {
|
||||
delete x.second;
|
||||
}
|
||||
table_.clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -2360,10 +2337,6 @@ Isolate::Isolate(bool enable_serializer)
|
||||
wasm_compilation_manager_(new wasm::CompilationManager()),
|
||||
abort_on_uncaught_exception_callback_(NULL),
|
||||
total_regexp_code_generated_(0) {
|
||||
{
|
||||
base::LockGuard<base::Mutex> lock_guard(thread_data_table_mutex_.Pointer());
|
||||
CHECK(thread_data_table_);
|
||||
}
|
||||
id_ = base::Relaxed_AtomicIncrement(&isolate_counter_, 1);
|
||||
TRACE_ISOLATE(constructor);
|
||||
|
||||
@@ -2418,8 +2391,8 @@ void Isolate::TearDown() {
|
||||
Deinit();
|
||||
|
||||
{
|
||||
base::LockGuard<base::Mutex> lock_guard(thread_data_table_mutex_.Pointer());
|
||||
thread_data_table_->RemoveAllThreads(this);
|
||||
base::LockGuard<base::Mutex> lock_guard(&thread_data_table_mutex_);
|
||||
thread_data_table_.RemoveAllThreads();
|
||||
}
|
||||
|
||||
delete this;
|
||||
@@ -2429,12 +2402,6 @@ void Isolate::TearDown() {
|
||||
}
|
||||
|
||||
|
||||
void Isolate::GlobalTearDown() {
|
||||
delete thread_data_table_;
|
||||
thread_data_table_ = NULL;
|
||||
}
|
||||
|
||||
|
||||
void Isolate::ClearSerializerData() {
|
||||
delete external_reference_table_;
|
||||
external_reference_table_ = NULL;
|
||||
|
||||
Vendored
+18
-10
@@ -8,6 +8,8 @@
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "include/v8-debug.h"
|
||||
#include "src/allocation.h"
|
||||
@@ -243,6 +245,8 @@ class ThreadId {
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const ThreadId& other) const { return Equals(other); }
|
||||
|
||||
// Returns ThreadId for current thread.
|
||||
static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
|
||||
|
||||
@@ -283,7 +287,6 @@ class ThreadId {
|
||||
friend class Isolate;
|
||||
};
|
||||
|
||||
|
||||
#define FIELD_ACCESSOR(type, name) \
|
||||
inline void set_##name(type v) { name##_ = v; } \
|
||||
inline type name() const { return name##_; }
|
||||
@@ -557,8 +560,6 @@ class Isolate {
|
||||
|
||||
void ReleaseManagedObjects();
|
||||
|
||||
static void GlobalTearDown();
|
||||
|
||||
void ClearSerializerData();
|
||||
|
||||
// Find the PerThread for this particular (isolate, thread) combination
|
||||
@@ -1334,20 +1335,24 @@ class Isolate {
|
||||
void* embedder_data_[Internals::kNumIsolateDataSlots];
|
||||
Heap heap_;
|
||||
|
||||
// The per-process lock should be acquired before the ThreadDataTable is
|
||||
// modified.
|
||||
class ThreadDataTable {
|
||||
public:
|
||||
ThreadDataTable();
|
||||
~ThreadDataTable();
|
||||
|
||||
PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id);
|
||||
PerIsolateThreadData* Lookup(ThreadId thread_id);
|
||||
void Insert(PerIsolateThreadData* data);
|
||||
void Remove(PerIsolateThreadData* data);
|
||||
void RemoveAllThreads(Isolate* isolate);
|
||||
void RemoveAllThreads();
|
||||
|
||||
private:
|
||||
PerIsolateThreadData* list_;
|
||||
struct Hasher {
|
||||
std::size_t operator()(const ThreadId& t) const {
|
||||
return std::hash<int>()(t.ToInteger());
|
||||
}
|
||||
};
|
||||
|
||||
std::unordered_map<ThreadId, PerIsolateThreadData*, Hasher> table_;
|
||||
};
|
||||
|
||||
// These items form a stack synchronously with threads Enter'ing and Exit'ing
|
||||
@@ -1375,12 +1380,15 @@ class Isolate {
|
||||
DISALLOW_COPY_AND_ASSIGN(EntryStackItem);
|
||||
};
|
||||
|
||||
static base::LazyMutex thread_data_table_mutex_;
|
||||
// TODO(kenton@cloudflare.com): This mutex can be removed if
|
||||
// thread_data_table_ is always accessed under the isolate lock. I do not
|
||||
// know if this is the case, so I'm preserving it for now.
|
||||
base::Mutex thread_data_table_mutex_;
|
||||
|
||||
static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
|
||||
static base::Thread::LocalStorageKey isolate_key_;
|
||||
static base::Thread::LocalStorageKey thread_id_key_;
|
||||
static ThreadDataTable* thread_data_table_;
|
||||
ThreadDataTable thread_data_table_;
|
||||
|
||||
// A global counter for all generated Isolates, might overflow.
|
||||
static base::Atomic32 isolate_counter_;
|
||||
|
||||
Vendored
-1
@@ -46,7 +46,6 @@ void V8::TearDown() {
|
||||
Bootstrapper::TearDownExtensions();
|
||||
ElementsAccessor::TearDown();
|
||||
RegisteredExtension::UnregisterAll();
|
||||
Isolate::GlobalTearDown();
|
||||
sampler::Sampler::TearDown();
|
||||
FlagList::ResetAllFlags(); // Frees memory held by string arguments.
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user