Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(862)

Unified Diff: base/profiler/stack_sampling_profiler.cc

Issue 981143006: Metrics provider for statistical stack profiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: fix clang compilation Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/profiler/stack_sampling_profiler.cc
diff --git a/base/profiler/stack_sampling_profiler.cc b/base/profiler/stack_sampling_profiler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..743db66f8fcb6367da1f18961416268f2fd2ec7a
--- /dev/null
+++ b/base/profiler/stack_sampling_profiler.cc
@@ -0,0 +1,235 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/profiler/stack_sampling_profiler.h"
+
+#include <algorithm>
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/memory/singleton.h"
+#include "base/synchronization/lock.h"
+#include "base/synchronization/waitable_event.h"
+#include "base/timer/elapsed_timer.h"
+
+template <typename T> struct DefaultSingletonTraits;
+
+namespace base {
+
+namespace {
+// Time to delay before first samples are taken.
+const int64 kInitialDelayInMicroseconds = 0;
+// Number of sampling bursts to perform.
+const int kNumberOfBursts = 1;
+// Interval between sampling bursts. This is the desired duration from the start
+// of one burst to the start of the next burst.
+const int64 kBurstIntervalInMicroseconds = 10000000; // 10 sec
+// Number of samples to record per burst.
+const int kNumberOfSamples = 300;
+// Interval between samples during a sampling burst. This is the desired
+// duration from the start of one burst to the start of the next burst.
+const int64 kSamplingIntervalInMicroseconds = 100000; // 100 ms
+
+// Thread-safe singleton class that stores collected profiles waiting to be
+// processed.
+class PendingProfiles {
+public:
+ PendingProfiles();
+ ~PendingProfiles();
+
+ static PendingProfiles* GetInstance();
+
+ // Appends |profiles|. This function is thread safe.
+ void PutProfiles(const std::vector<StackSamplingProfiler::Profile>& profiles);
+ // Gets the pending profiles into *|profiles|. This function is thread safe.
+ void GetProfiles(std::vector<StackSamplingProfiler::Profile>* profiles);
+
+private:
+ base::Lock profiles_lock_;
+ std::vector<StackSamplingProfiler::Profile> profiles_;
+
+ DISALLOW_COPY_AND_ASSIGN(PendingProfiles);
+};
+
+PendingProfiles::PendingProfiles() {}
+
+PendingProfiles::~PendingProfiles() {}
+
+// static
+PendingProfiles* PendingProfiles::GetInstance() {
+ return Singleton<PendingProfiles>::get();
+}
+
+void PendingProfiles::PutProfiles(
+ const std::vector<StackSamplingProfiler::Profile>& profiles) {
+ base::AutoLock scoped_lock(profiles_lock_);
+ profiles_.insert(profiles_.end(), profiles.begin(), profiles.end());
+}
+
+void PendingProfiles::GetProfiles(
+ std::vector<StackSamplingProfiler::Profile>* profiles) {
+ profiles->clear();
+
+ base::AutoLock scoped_lock(profiles_lock_);
+ profiles_.swap(*profiles);
+}
+} // namespace
+
+StackSamplingProfiler::Profile::Profile() {}
+
+StackSamplingProfiler::Profile::~Profile() {}
+
+class StackSamplingProfiler::SamplingThread : public PlatformThread::Delegate {
+ public:
+ // Samples stacks using |native_sampler|. When complete, invokes
+ // |profiles_callback| with the collected profiles. |profiles_callback| must
+ // be thread-safe and may consume the contents of the vector.
+ SamplingThread(
+ scoped_ptr<NativeStackSampler> native_sampler,
+ base::Callback<void(const std::vector<Profile>&)> profiles_callback);
+ ~SamplingThread() override;
+
+ // Implementation of PlatformThread::Delegate:
+ void ThreadMain() override;
+
+ void Stop();
+
+ private:
+ // Collects a profile from a single burst. Returns true if the profile was
+ // collected, or false if collection was stopped before it completed.
+ bool CollectProfile(Profile* profile, TimeDelta* elapsed_time);
+ // Collects profiles from all bursts, or until the sampling is stopped. If
+ // stopped before complete, |profiles| will contains only full bursts.
+ void CollectProfiles(std::vector<Profile>* profiles);
+
+ scoped_ptr<NativeStackSampler> native_sampler_;
+
+ base::WaitableEvent stop_event_;
+
+ base::Callback<void(const std::vector<Profile>&)> profiles_callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(SamplingThread);
+};
+
+StackSamplingProfiler::SamplingThread::SamplingThread(
+ scoped_ptr<NativeStackSampler> native_sampler,
+ base::Callback<void(const std::vector<Profile>&)> profiles_callback)
+ : native_sampler_(native_sampler.Pass()),
+ stop_event_(false, false),
+ profiles_callback_(profiles_callback) {
+}
+
+StackSamplingProfiler::SamplingThread::~SamplingThread() {}
+
+void StackSamplingProfiler::SamplingThread::ThreadMain() {
+ PlatformThread::SetName("Chrome_SamplingProfilerThread");
+
+ std::vector<Profile> profiles;
+ CollectProfiles(&profiles);
+ profiles_callback_.Run(profiles);
+}
+
+bool StackSamplingProfiler::SamplingThread::CollectProfile(
+ Profile* profile, TimeDelta* elapsed_time) {
+ base::ElapsedTimer profile_timer;
+ Profile current_profile;
+ native_sampler_->ProfileRecordingStarting(&current_profile);
+ const TimeDelta sampling_interval =
+ TimeDelta::FromMicroseconds(kSamplingIntervalInMicroseconds);
+ current_profile.sampling_period = sampling_interval;
+ bool stopped_early = false;
+ for (int j = 0; j < kNumberOfSamples; ++j) {
+ base::ElapsedTimer sample_timer;
+ current_profile.samples.push_back(Sample());
+ native_sampler_->RecordStackSample(&current_profile.samples.back());
+ base::TimeDelta elapsed_sample_time = sample_timer.Elapsed();
+ if (j != kNumberOfSamples - 1) {
+ if (stop_event_.TimedWait(
+ std::max(sampling_interval - elapsed_sample_time, TimeDelta()))) {
+ stopped_early = true;
+ break;
+ }
+ }
+ }
+
+ *elapsed_time = profile_timer.Elapsed();
+ current_profile.profile_duration = *elapsed_time;
+ native_sampler_->ProfileRecordingStopped();
+
+ if (!stopped_early)
+ *profile = current_profile;
+
+ return !stopped_early;
+}
+
+void StackSamplingProfiler::SamplingThread::CollectProfiles(
+ std::vector<Profile>* profiles) {
+ if (stop_event_.TimedWait(TimeDelta::FromMicroseconds(
+ kInitialDelayInMicroseconds)))
+ return;
+
+ const TimeDelta burst_interval =
+ TimeDelta::FromMicroseconds(kBurstIntervalInMicroseconds);
+ for (int i = 0; i < kNumberOfBursts; ++i) {
+ Profile profile;
+ base::TimeDelta elapsed_profile_time;
+ if (CollectProfile(&profile, &elapsed_profile_time))
+ profiles->push_back(profile);
+ else
+ return;
+
+ if (stop_event_.TimedWait(
+ std::max(burst_interval - elapsed_profile_time, TimeDelta())))
+ return;
+ }
+}
+
+void StackSamplingProfiler::SamplingThread::Stop() {
+ stop_event_.Signal();
+}
+
+void StackSamplingProfiler::SamplingThreadDeleter::operator()(
+ SamplingThread* thread) const {
+ delete thread;
+}
+
+StackSamplingProfiler::NativeStackSampler::NativeStackSampler() {}
+
+StackSamplingProfiler::NativeStackSampler::~NativeStackSampler() {}
+
+StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id)
+ : thread_id_(thread_id) {}
+
+StackSamplingProfiler::~StackSamplingProfiler() {}
+
+void StackSamplingProfiler::Start() {
+ native_sampler_ = NativeStackSampler::Create(thread_id_);
+ if (!native_sampler_)
+ return;
+
+ sampling_thread_.reset(
+ new SamplingThread(
+ native_sampler_.Pass(),
+ base::Bind(&PendingProfiles::PutProfiles,
+ base::Unretained(PendingProfiles::GetInstance()))));
+ if (!PlatformThread::CreateNonJoinable(0, sampling_thread_.get()))
+ LOG(ERROR) << "failed to create thread";
+}
+
+void StackSamplingProfiler::Stop() {
+ if (sampling_thread_)
+ sampling_thread_->Stop();
+}
+
+// static
+void StackSamplingProfiler::GetPendingProfiles(std::vector<Profile>* profiles) {
+ PendingProfiles::GetInstance()->GetProfiles(profiles);
+}
+
+bool operator==(const StackSamplingProfiler::Frame &a,
+ const StackSamplingProfiler::Frame &b) {
+ return a.ip_offset == b.ip_offset && a.module_index == b.module_index;
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698