Commit 8f96f66f authored by Franziska Hinkelmann's avatar Franziska Hinkelmann Committed by Commit Bot

[cpu-profiler] Use unique pointers for clearer ownership

Use unique pointers in vectors of current and finished profiles.

Change-Id: Ifb78f7d3804e9883062741fd4e4e31109965d501
Reviewed-on: https://chromium-review.googlesource.com/898984
Commit-Queue: Franziska Hinkelmann <franzih@chromium.org>
Reviewed-by: 's avatarPeter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51113}
parent 18a07e11
......@@ -205,7 +205,7 @@ int CpuProfiler::GetProfilesCount() {
CpuProfile* CpuProfiler::GetProfile(int index) {
return profiles_->profiles()->at(index);
return profiles_->profiles()->at(index).get();
}
......@@ -217,7 +217,6 @@ void CpuProfiler::DeleteAllProfiles() {
void CpuProfiler::DeleteProfile(CpuProfile* profile) {
profiles_->RemoveProfile(profile);
delete profile;
if (profiles_->profiles()->empty() && !is_profiling_) {
// If this was the last profile, clean up all accessory data as well.
ResetProfiles();
......
......@@ -530,12 +530,6 @@ CpuProfilesCollection::CpuProfilesCollection(Isolate* isolate)
profiler_(nullptr),
current_profiles_semaphore_(1) {}
CpuProfilesCollection::~CpuProfilesCollection() {
for (CpuProfile* profile : finished_profiles_) delete profile;
for (CpuProfile* profile : current_profiles_) delete profile;
}
bool CpuProfilesCollection::StartProfiling(const char* title,
bool record_samples) {
current_profiles_semaphore_.Wait();
......@@ -543,7 +537,7 @@ bool CpuProfilesCollection::StartProfiling(const char* title,
current_profiles_semaphore_.Signal();
return false;
}
for (CpuProfile* profile : current_profiles_) {
for (const std::unique_ptr<CpuProfile>& profile : current_profiles_) {
if (strcmp(profile->title(), title) == 0) {
// Ignore attempts to start profile with the same title...
current_profiles_semaphore_.Signal();
......@@ -551,7 +545,8 @@ bool CpuProfilesCollection::StartProfiling(const char* title,
return true;
}
}
current_profiles_.push_back(new CpuProfile(profiler_, title, record_samples));
current_profiles_.emplace_back(
new CpuProfile(profiler_, title, record_samples));
current_profiles_semaphore_.Signal();
return true;
}
......@@ -561,19 +556,22 @@ CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) {
const int title_len = StrLength(title);
CpuProfile* profile = nullptr;
current_profiles_semaphore_.Wait();
for (size_t i = current_profiles_.size(); i != 0; --i) {
CpuProfile* current_profile = current_profiles_[i - 1];
if (title_len == 0 || strcmp(current_profile->title(), title) == 0) {
profile = current_profile;
current_profiles_.erase(current_profiles_.begin() + i - 1);
break;
}
auto it =
std::find_if(current_profiles_.rbegin(), current_profiles_.rend(),
[&](const std::unique_ptr<CpuProfile>& p) {
return title_len == 0 || strcmp(p->title(), title) == 0;
});
if (it != current_profiles_.rend()) {
(*it)->FinishProfile();
profile = it->get();
finished_profiles_.push_back(std::move(*it));
// Convert reverse iterator to matching forward iterator.
current_profiles_.erase(--(it.base()));
}
current_profiles_semaphore_.Signal();
if (!profile) return nullptr;
profile->FinishProfile();
finished_profiles_.push_back(profile);
current_profiles_semaphore_.Signal();
return profile;
}
......@@ -590,7 +588,10 @@ bool CpuProfilesCollection::IsLastProfile(const char* title) {
void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
// Called from VM thread for a completed profile.
auto pos =
std::find(finished_profiles_.begin(), finished_profiles_.end(), profile);
std::find_if(finished_profiles_.begin(), finished_profiles_.end(),
[&](const std::unique_ptr<CpuProfile>& finished_profile) {
return finished_profile.get() == profile;
});
DCHECK(pos != finished_profiles_.end());
finished_profiles_.erase(pos);
}
......@@ -602,7 +603,7 @@ void CpuProfilesCollection::AddPathToCurrentProfiles(
// method, we don't bother minimizing the duration of lock holding,
// e.g. copying contents of the list to a local vector.
current_profiles_semaphore_.Wait();
for (CpuProfile* profile : current_profiles_) {
for (const std::unique_ptr<CpuProfile>& profile : current_profiles_) {
profile->AddPath(timestamp, path, src_line, update_stats);
}
current_profiles_semaphore_.Signal();
......
......@@ -338,12 +338,13 @@ class CodeMap {
class CpuProfilesCollection {
public:
explicit CpuProfilesCollection(Isolate* isolate);
~CpuProfilesCollection();
void set_cpu_profiler(CpuProfiler* profiler) { profiler_ = profiler; }
bool StartProfiling(const char* title, bool record_samples);
CpuProfile* StopProfiling(const char* title);
std::vector<CpuProfile*>* profiles() { return &finished_profiles_; }
std::vector<std::unique_ptr<CpuProfile>>* profiles() {
return &finished_profiles_;
}
const char* GetName(Name* name) { return resource_names_.GetName(name); }
bool IsLastProfile(const char* title);
void RemoveProfile(CpuProfile* profile);
......@@ -358,11 +359,11 @@ class CpuProfilesCollection {
private:
StringsStorage resource_names_;
std::vector<CpuProfile*> finished_profiles_;
std::vector<std::unique_ptr<CpuProfile>> finished_profiles_;
CpuProfiler* profiler_;
// Accessed by VM thread and profile generator thread.
std::vector<CpuProfile*> current_profiles_;
std::vector<std::unique_ptr<CpuProfile>> current_profiles_;
base::Semaphore current_profiles_semaphore_;
DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
......
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