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

Unified Diff: components/metrics/call_stack_profile_metrics_provider.cc

Issue 1029653002: Enable startup profiling by Win x64 stack sampling profiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@statprof-metrics-provider
Patch Set: early destruction fix 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: components/metrics/call_stack_profile_metrics_provider.cc
diff --git a/components/metrics/call_stack_profile_metrics_provider.cc b/components/metrics/call_stack_profile_metrics_provider.cc
index 7ed60f5ba6b301b215582a6212a8e4162faeddf2..43c2092d2bd2378b28237f124875fd480e54618a 100644
--- a/components/metrics/call_stack_profile_metrics_provider.cc
+++ b/components/metrics/call_stack_profile_metrics_provider.cc
@@ -8,8 +8,11 @@
#include <map>
#include <utility>
+#include "base/bind.h"
#include "base/logging.h"
#include "base/macros.h"
+#include "base/message_loop/message_loop_proxy.h"
+#include "base/metrics/field_trial.h"
#include "base/profiler/stack_sampling_profiler.h"
#include "components/metrics/metrics_hashes.h"
#include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
@@ -20,6 +23,12 @@ namespace metrics {
namespace {
+// Accepts and ignores the completed profiles. Used when metrics reporting is
+// disabled.
+void IgnoreCompletedProfiles(
+ const std::vector<base::StackSamplingProfiler::Profile>& profiles) {
+}
+
// The protobuf expects the MD5 checksum prefix of the module name.
uint64 HashModuleFilename(const base::FilePath& filename) {
const base::FilePath::StringType basename = filename.BaseName().value();
@@ -104,21 +113,42 @@ void CopyProfileToProto(
proto_profile->set_sampling_period_ms(
profile.sampling_period.InMilliseconds());
}
+
} // namespace
-CallStackProfileMetricsProvider::CallStackProfileMetricsProvider() {}
+const char CallStackProfileMetricsProvider::kFieldTrialName[] =
+ "StackProfiling";
+const char CallStackProfileMetricsProvider::kReportProfilesGroupName[] =
+ "Report profiles";
-CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() {}
+CallStackProfileMetricsProvider::CallStackProfileMetricsProvider()
+ : weak_factory_(this) {
+}
+
+CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() {
+ StackSamplingProfiler::SetDefaultCompletedCallback(
+ StackSamplingProfiler::CompletedCallback());
+}
+
+void CallStackProfileMetricsProvider::OnRecordingEnabled() {
+ StackSamplingProfiler::SetDefaultCompletedCallback(
+ base::Bind(&CallStackProfileMetricsProvider::ReceiveCompletedProfiles,
+ base::MessageLoopProxy::current(),
+ weak_factory_.GetWeakPtr()));
+}
+
+void CallStackProfileMetricsProvider::OnRecordingDisabled() {
+ StackSamplingProfiler::SetDefaultCompletedCallback(
+ base::Bind(&IgnoreCompletedProfiles));
+ pending_profiles_.clear();
+}
void CallStackProfileMetricsProvider::ProvideGeneralMetrics(
ChromeUserMetricsExtension* uma_proto) {
- std::vector<StackSamplingProfiler::Profile> profiles;
- if (!source_profiles_for_test_.empty())
- profiles.swap(source_profiles_for_test_);
- else
- StackSamplingProfiler::GetPendingProfiles(&profiles);
+ if (!IsSamplingProfilingReportingEnabled())
+ return;
- for (const StackSamplingProfiler::Profile& profile : profiles) {
+ for (const StackSamplingProfiler::Profile& profile : pending_profiles_) {
CallStackProfile* call_stack_profile =
uma_proto->add_sampled_profile()->mutable_call_stack_profile();
CopyProfileToProto(profile, call_stack_profile);
@@ -127,7 +157,35 @@ void CallStackProfileMetricsProvider::ProvideGeneralMetrics(
void CallStackProfileMetricsProvider::SetSourceProfilesForTesting(
const std::vector<StackSamplingProfiler::Profile>& profiles) {
- source_profiles_for_test_ = profiles;
+ pending_profiles_ = profiles;
+}
+
+// static
+bool CallStackProfileMetricsProvider::IsSamplingProfilingReportingEnabled() {
+ const std::string group_name =
+ base::FieldTrialList::FindFullName(
+ CallStackProfileMetricsProvider::kFieldTrialName);
+ return group_name ==
+ CallStackProfileMetricsProvider::kReportProfilesGroupName;
+}
+
+// static
+// Posts a message back to our own thread to collect the profiles.
+void CallStackProfileMetricsProvider::ReceiveCompletedProfiles(
+ scoped_refptr<base::MessageLoopProxy> message_loop,
+ base::WeakPtr<CallStackProfileMetricsProvider> provider,
+ const std::vector<base::StackSamplingProfiler::Profile>& profiles) {
+ message_loop->PostTask(
+ FROM_HERE,
+ base::Bind(&CallStackProfileMetricsProvider::AppendCompletedProfiles,
+ provider,
+ profiles));
+}
+
+void CallStackProfileMetricsProvider::AppendCompletedProfiles(
+ const std::vector<base::StackSamplingProfiler::Profile>& profiles) {
+ pending_profiles_.insert(pending_profiles_.end(), profiles.begin(),
+ profiles.end());
}
} // namespace metrics

Powered by Google App Engine
This is Rietveld 408576698