libsampler: Cleanup SamplerManager

- Move the samplers related part out of SignalHandler class (remove friendship).
  - Make the SamplerManager class a singleton.
  - Minor tweaks.

BUG=v8:4789

Review-Url: https://codereview.chromium.org/2018773002
Cr-Commit-Position: refs/heads/master@{#36596}
This commit is contained in:
alph 2016-05-30 09:19:07 -07:00 committed by Commit bot
parent 63efe9e416
commit 8b47179b14

View File

@ -163,40 +163,36 @@ namespace {
#if defined(USE_SIGNALS) #if defined(USE_SIGNALS)
typedef std::vector<Sampler*> SamplerList; typedef std::vector<Sampler*> SamplerList;
typedef SamplerList::iterator SamplerListIterator; typedef SamplerList::iterator SamplerListIterator;
typedef base::AtomicValue<bool> AtomicMutex;
class AtomicGuard { class AtomicGuard {
public: public:
explicit AtomicGuard(base::AtomicValue<int>* atomic, bool is_block = true) explicit AtomicGuard(AtomicMutex* atomic, bool is_blocking = true)
: atomic_(atomic), : atomic_(atomic), is_success_(false) {
is_success_(false) {
do { do {
// Use Acquire_Load to gain mutual exclusion. // Use Acquire_Load to gain mutual exclusion.
USE(atomic_->Value()); USE(atomic_->Value());
is_success_ = atomic_->TrySetValue(0, 1); is_success_ = atomic_->TrySetValue(false, true);
} while (is_block && !is_success_); } while (is_blocking && !is_success_);
} }
bool is_success() { return is_success_; } bool is_success() const { return is_success_; }
~AtomicGuard() { ~AtomicGuard() {
if (is_success_) { if (!is_success_) return;
atomic_->SetValue(0); atomic_->SetValue(false);
}
atomic_ = NULL;
} }
private: private:
base::AtomicValue<int>* atomic_; AtomicMutex* const atomic_;
bool is_success_; bool is_success_;
}; };
// Returns key for hash map. // Returns key for hash map.
void* ThreadKey(pthread_t thread_id) { void* ThreadKey(pthread_t thread_id) {
return reinterpret_cast<void*>(thread_id); return reinterpret_cast<void*>(thread_id);
} }
// Returns hash value for hash map. // Returns hash value for hash map.
uint32_t ThreadHash(pthread_t thread_id) { uint32_t ThreadHash(pthread_t thread_id) {
#if V8_OS_MACOSX #if V8_OS_MACOSX
@ -221,19 +217,19 @@ class Sampler::PlatformData {
pthread_t vm_tid_; pthread_t vm_tid_;
}; };
class SamplerManager { class SamplerManager {
public: public:
static void AddSampler(Sampler* sampler) { SamplerManager() : sampler_map_(HashMap::PointersMatch) {}
void AddSampler(Sampler* sampler) {
AtomicGuard atomic_guard(&samplers_access_counter_); AtomicGuard atomic_guard(&samplers_access_counter_);
DCHECK(sampler->IsActive() || !sampler->IsRegistered()); DCHECK(sampler->IsActive() || !sampler->IsRegistered());
// Add sampler into map if needed. // Add sampler into map if needed.
pthread_t thread_id = sampler->platform_data()->vm_tid(); pthread_t thread_id = sampler->platform_data()->vm_tid();
HashMap::Entry* entry = HashMap::Entry* entry = sampler_map_.LookupOrInsert(ThreadKey(thread_id),
sampler_map_.Pointer()->LookupOrInsert(ThreadKey(thread_id), ThreadHash(thread_id));
ThreadHash(thread_id)); DCHECK(entry != nullptr);
DCHECK(entry != NULL); if (entry->value == nullptr) {
if (entry->value == NULL) {
SamplerList* samplers = new SamplerList(); SamplerList* samplers = new SamplerList();
samplers->push_back(sampler); samplers->push_back(sampler);
entry->value = samplers; entry->value = samplers;
@ -253,15 +249,15 @@ class SamplerManager {
} }
} }
static void RemoveSampler(Sampler* sampler) { void RemoveSampler(Sampler* sampler) {
AtomicGuard atomic_guard(&samplers_access_counter_); AtomicGuard atomic_guard(&samplers_access_counter_);
DCHECK(sampler->IsActive() || sampler->IsRegistered()); DCHECK(sampler->IsActive() || sampler->IsRegistered());
// Remove sampler from map. // Remove sampler from map.
pthread_t thread_id = sampler->platform_data()->vm_tid(); pthread_t thread_id = sampler->platform_data()->vm_tid();
void* thread_key = ThreadKey(thread_id); void* thread_key = ThreadKey(thread_id);
uint32_t thread_hash = ThreadHash(thread_id); uint32_t thread_hash = ThreadHash(thread_id);
HashMap::Entry* entry = sampler_map_.Get().Lookup(thread_key, thread_hash); HashMap::Entry* entry = sampler_map_.Lookup(thread_key, thread_hash);
DCHECK(entry != NULL); DCHECK(entry != nullptr);
SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value);
for (SamplerListIterator iter = samplers->begin(); iter != samplers->end(); for (SamplerListIterator iter = samplers->begin(); iter != samplers->end();
++iter) { ++iter) {
@ -271,27 +267,43 @@ class SamplerManager {
} }
} }
if (samplers->empty()) { if (samplers->empty()) {
sampler_map_.Pointer()->Remove(thread_key, thread_hash); sampler_map_.Remove(thread_key, thread_hash);
delete samplers; delete samplers;
} }
} }
private: #if defined(USE_SIGNALS)
struct HashMapCreateTrait { void DoSample(const v8::RegisterState& state) {
static void Construct(HashMap* allocated_ptr) { AtomicGuard atomic_guard(&SamplerManager::samplers_access_counter_, false);
new (allocated_ptr) HashMap(HashMap::PointersMatch); if (!atomic_guard.is_success()) return;
pthread_t thread_id = pthread_self();
HashMap::Entry* entry =
sampler_map_.Lookup(ThreadKey(thread_id), ThreadHash(thread_id));
if (!entry) return;
SamplerList& samplers = *static_cast<SamplerList*>(entry->value);
for (int i = 0; i < samplers.size(); ++i) {
Sampler* sampler = samplers[i];
Isolate* isolate = sampler->isolate();
// We require a fully initialized and entered isolate.
if (isolate == nullptr || !isolate->IsInUse()) continue;
if (v8::Locker::IsActive() && !Locker::IsLocked(isolate)) continue;
sampler->SampleStack(state);
} }
}; }
friend class SignalHandler; #endif
static base::LazyInstance<HashMap, HashMapCreateTrait>::type
sampler_map_; static SamplerManager* instance() { return instance_.Pointer(); }
static base::AtomicValue<int> samplers_access_counter_;
private:
HashMap sampler_map_;
static AtomicMutex samplers_access_counter_;
static base::LazyInstance<SamplerManager>::type instance_;
}; };
base::LazyInstance<HashMap, SamplerManager::HashMapCreateTrait>::type AtomicMutex SamplerManager::samplers_access_counter_;
SamplerManager::sampler_map_ = LAZY_INSTANCE_INITIALIZER; base::LazyInstance<SamplerManager>::type SamplerManager::instance_ =
base::AtomicValue<int> SamplerManager::samplers_access_counter_(0); LAZY_INSTANCE_INITIALIZER;
#elif V8_OS_WIN || V8_OS_CYGWIN #elif V8_OS_WIN || V8_OS_CYGWIN
@ -314,9 +326,9 @@ class Sampler::PlatformData {
GetCurrentThreadId())) {} GetCurrentThreadId())) {}
~PlatformData() { ~PlatformData() {
if (profiled_thread_ != NULL) { if (profiled_thread_ != nullptr) {
CloseHandle(profiled_thread_); CloseHandle(profiled_thread_);
profiled_thread_ = NULL; profiled_thread_ = nullptr;
} }
} }
@ -332,7 +344,10 @@ class Sampler::PlatformData {
class SignalHandler { class SignalHandler {
public: public:
static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); }
static void TearDown() { delete mutex_; mutex_ = NULL; } static void TearDown() {
delete mutex_;
mutex_ = nullptr;
}
static void IncreaseSamplerCount() { static void IncreaseSamplerCount() {
base::LockGuard<base::Mutex> lock_guard(mutex_); base::LockGuard<base::Mutex> lock_guard(mutex_);
@ -385,8 +400,7 @@ class SignalHandler {
static struct sigaction old_signal_handler_; static struct sigaction old_signal_handler_;
}; };
base::Mutex* SignalHandler::mutex_ = nullptr;
base::Mutex* SignalHandler::mutex_ = NULL;
int SignalHandler::client_count_ = 0; int SignalHandler::client_count_ = 0;
struct sigaction SignalHandler::old_signal_handler_; struct sigaction SignalHandler::old_signal_handler_;
bool SignalHandler::signal_handler_installed_ = false; bool SignalHandler::signal_handler_installed_ = false;
@ -398,29 +412,9 @@ void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info,
void* context) { void* context) {
USE(info); USE(info);
if (signal != SIGPROF) return; if (signal != SIGPROF) return;
AtomicGuard atomic_guard(&SamplerManager::samplers_access_counter_, false);
if (!atomic_guard.is_success()) return;
pthread_t thread_id = pthread_self();
HashMap::Entry* entry =
SamplerManager::sampler_map_.Pointer()->Lookup(ThreadKey(thread_id),
ThreadHash(thread_id));
if (entry == NULL) return;
SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value);
v8::RegisterState state; v8::RegisterState state;
FillRegisterState(context, &state); FillRegisterState(context, &state);
SamplerManager::instance()->DoSample(state);
for (int i = 0; i < samplers->size(); ++i) {
Sampler* sampler = (*samplers)[i];
Isolate* isolate = sampler->isolate();
// We require a fully initialized and entered isolate.
if (isolate == NULL || !isolate->IsInUse()) return;
if (v8::Locker::IsActive() && !Locker::IsLocked(isolate)) return;
sampler->SampleStack(state);
}
} }
void SignalHandler::FillRegisterState(void* context, RegisterState* state) { void SignalHandler::FillRegisterState(void* context, RegisterState* state) {
@ -592,7 +586,7 @@ Sampler::~Sampler() {
DCHECK(!IsActive()); DCHECK(!IsActive());
#if defined(USE_SIGNALS) #if defined(USE_SIGNALS)
if (IsRegistered()) { if (IsRegistered()) {
SamplerManager::RemoveSampler(this); SamplerManager::instance()->RemoveSampler(this);
} }
#endif #endif
delete data_; delete data_;
@ -602,14 +596,14 @@ void Sampler::Start() {
DCHECK(!IsActive()); DCHECK(!IsActive());
SetActive(true); SetActive(true);
#if defined(USE_SIGNALS) #if defined(USE_SIGNALS)
SamplerManager::AddSampler(this); SamplerManager::instance()->AddSampler(this);
#endif #endif
} }
void Sampler::Stop() { void Sampler::Stop() {
#if defined(USE_SIGNALS) #if defined(USE_SIGNALS)
SamplerManager::RemoveSampler(this); SamplerManager::instance()->RemoveSampler(this);
#endif #endif
DCHECK(IsActive()); DCHECK(IsActive());
SetActive(false); SetActive(false);
@ -638,7 +632,7 @@ void Sampler::DecreaseProfilingDepth() {
void Sampler::DoSample() { void Sampler::DoSample() {
if (!SignalHandler::Installed()) return; if (!SignalHandler::Installed()) return;
if (!IsActive() && !IsRegistered()) { if (!IsActive() && !IsRegistered()) {
SamplerManager::AddSampler(this); SamplerManager::instance()->AddSampler(this);
SetRegistered(true); SetRegistered(true);
} }
pthread_kill(platform_data()->vm_tid(), SIGPROF); pthread_kill(platform_data()->vm_tid(), SIGPROF);
@ -648,7 +642,7 @@ void Sampler::DoSample() {
void Sampler::DoSample() { void Sampler::DoSample() {
HANDLE profiled_thread = platform_data()->profiled_thread(); HANDLE profiled_thread = platform_data()->profiled_thread();
if (profiled_thread == NULL) return; if (profiled_thread == nullptr) return;
const DWORD kSuspendFailed = static_cast<DWORD>(-1); const DWORD kSuspendFailed = static_cast<DWORD>(-1);
if (SuspendThread(profiled_thread) == kSuspendFailed) return; if (SuspendThread(profiled_thread) == kSuspendFailed) return;