Commit 4dbdeea2 authored by Peter Marshall's avatar Peter Marshall Committed by Commit Bot

[cpu-profiler] Use unordered_map instead of custom hashmap in sampler

There is no reason to use the custom map here anymore. This lets us
get rid of the custom hash and a lot of casts.

We can also store the SamplerList by value in the map rather than a
pointer, then we don't have to manage the lifetime explicitly.

Also move the SamplerList typedef inside of SamplerManager because it's
an internal detail. Remove the include for <map> because we aren't using
this anywhere anyway.

Change-Id: I787a1b6c3ffc331ec3f36e66d5e07bd115c4cbb4
Reviewed-on: https://chromium-review.googlesource.com/c/1419317Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58945}
parent a8b8a440
...@@ -57,11 +57,10 @@ typedef zx_arm64_general_regs_t zx_thread_state_general_regs_t; ...@@ -57,11 +57,10 @@ typedef zx_arm64_general_regs_t zx_thread_state_general_regs_t;
#endif #endif
#include <algorithm> #include <algorithm>
#include <unordered_map>
#include <vector> #include <vector>
#include <map>
#include "src/base/atomic-utils.h" #include "src/base/atomic-utils.h"
#include "src/base/hashmap.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T)
...@@ -174,7 +173,6 @@ namespace sampler { ...@@ -174,7 +173,6 @@ namespace sampler {
namespace { namespace {
#if defined(USE_SIGNALS) #if defined(USE_SIGNALS)
typedef std::vector<Sampler*> SamplerList;
typedef std::atomic_bool AtomicMutex; typedef std::atomic_bool AtomicMutex;
class AtomicGuard { class AtomicGuard {
...@@ -201,20 +199,6 @@ class AtomicGuard { ...@@ -201,20 +199,6 @@ class AtomicGuard {
bool is_success_; bool is_success_;
}; };
// Returns key for hash map.
void* ThreadKey(pthread_t thread_id) {
return reinterpret_cast<void*>(thread_id);
}
// Returns hash value for hash map.
uint32_t ThreadHash(pthread_t thread_id) {
#if V8_OS_BSD
return static_cast<uint32_t>(reinterpret_cast<intptr_t>(thread_id));
#else
return static_cast<uint32_t>(thread_id);
#endif
}
#endif // USE_SIGNALS #endif // USE_SIGNALS
} // namespace } // namespace
...@@ -236,23 +220,22 @@ class SamplerManager { ...@@ -236,23 +220,22 @@ class SamplerManager {
public: public:
SamplerManager() = default; SamplerManager() = default;
typedef std::vector<Sampler*> SamplerList;
// Add |sampler| to the map if it is not already present. // Add |sampler| to the map if it is not already present.
void AddSampler(Sampler* sampler) { 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());
pthread_t thread_id = sampler->platform_data()->vm_tid(); pthread_t thread_id = sampler->platform_data()->vm_tid();
base::HashMap::Entry* entry = auto it = sampler_map_.find(thread_id);
sampler_map_.LookupOrInsert(ThreadKey(thread_id), if (it == sampler_map_.end()) {
ThreadHash(thread_id)); SamplerList samplers;
DCHECK_NOT_NULL(entry); samplers.push_back(sampler);
if (entry->value == nullptr) { sampler_map_.emplace(thread_id, std::move(samplers));
SamplerList* samplers = new SamplerList();
samplers->push_back(sampler);
entry->value = samplers;
} else { } else {
SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); SamplerList& samplers = it->second;
auto it = std::find(samplers->begin(), samplers->end(), sampler); auto it = std::find(samplers.begin(), samplers.end(), sampler);
if (it == samplers->end()) samplers->push_back(sampler); if (it == samplers.end()) samplers.push_back(sampler);
} }
} }
...@@ -262,16 +245,13 @@ class SamplerManager { ...@@ -262,16 +245,13 @@ class SamplerManager {
AtomicGuard atomic_guard(&samplers_access_counter_); AtomicGuard atomic_guard(&samplers_access_counter_);
DCHECK(sampler->IsActive() || sampler->IsRegistered()); DCHECK(sampler->IsActive() || sampler->IsRegistered());
pthread_t thread_id = sampler->platform_data()->vm_tid(); pthread_t thread_id = sampler->platform_data()->vm_tid();
void* thread_key = ThreadKey(thread_id); auto it = sampler_map_.find(thread_id);
uint32_t thread_hash = ThreadHash(thread_id); DCHECK_NE(it, sampler_map_.end());
base::HashMap::Entry* entry = sampler_map_.Lookup(thread_key, thread_hash); SamplerList& samplers = it->second;
DCHECK_NOT_NULL(entry); samplers.erase(std::remove(samplers.begin(), samplers.end(), sampler),
SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); samplers.end());
samplers->erase(std::remove(samplers->begin(), samplers->end(), sampler), if (samplers.empty()) {
samplers->end()); sampler_map_.erase(it);
if (samplers->empty()) {
sampler_map_.Remove(thread_key, thread_hash);
delete samplers;
} }
} }
...@@ -283,10 +263,9 @@ class SamplerManager { ...@@ -283,10 +263,9 @@ class SamplerManager {
AtomicGuard atomic_guard(&samplers_access_counter_, false); AtomicGuard atomic_guard(&samplers_access_counter_, false);
if (!atomic_guard.is_success()) return; if (!atomic_guard.is_success()) return;
pthread_t thread_id = pthread_self(); pthread_t thread_id = pthread_self();
base::HashMap::Entry* entry = auto it = sampler_map_.find(thread_id);
sampler_map_.Lookup(ThreadKey(thread_id), ThreadHash(thread_id)); if (it == sampler_map_.end()) return;
if (!entry) return; SamplerList& samplers = it->second;
SamplerList& samplers = *static_cast<SamplerList*>(entry->value);
for (Sampler* sampler : samplers) { for (Sampler* sampler : samplers) {
Isolate* isolate = sampler->isolate(); Isolate* isolate = sampler->isolate();
...@@ -304,7 +283,7 @@ class SamplerManager { ...@@ -304,7 +283,7 @@ class SamplerManager {
} }
private: private:
base::HashMap sampler_map_; std::unordered_map<pthread_t, SamplerList> sampler_map_;
AtomicMutex samplers_access_counter_; AtomicMutex samplers_access_counter_;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment