Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/metrics/call_stack_profile_metrics_provider.h" | 5 #include "components/metrics/call_stack_profile_metrics_provider.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/message_loop/message_loop_proxy.h" | |
| 15 #include "base/metrics/field_trial.h" | |
| 13 #include "base/profiler/stack_sampling_profiler.h" | 16 #include "base/profiler/stack_sampling_profiler.h" |
| 14 #include "components/metrics/metrics_hashes.h" | 17 #include "components/metrics/metrics_hashes.h" |
| 15 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" | 18 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
| 16 | 19 |
| 17 using base::StackSamplingProfiler; | 20 using base::StackSamplingProfiler; |
| 18 | 21 |
| 19 namespace metrics { | 22 namespace metrics { |
| 20 | 23 |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 26 void IgnoreCompletedProfiles( | |
|
Ilya Sherman
2015/03/24 21:07:55
nit: Doc string, please.
| |
| 27 const std::vector<base::StackSamplingProfiler::Profile>& profiles) { | |
| 28 } | |
| 29 | |
| 23 // The protobuf expects the MD5 checksum prefix of the module name. | 30 // The protobuf expects the MD5 checksum prefix of the module name. |
| 24 uint64 HashModuleFilename(const base::FilePath& filename) { | 31 uint64 HashModuleFilename(const base::FilePath& filename) { |
| 25 const base::FilePath::StringType basename = filename.BaseName().value(); | 32 const base::FilePath::StringType basename = filename.BaseName().value(); |
| 26 // Copy the bytes in basename into a string buffer. | 33 // Copy the bytes in basename into a string buffer. |
| 27 size_t basename_length_in_bytes = | 34 size_t basename_length_in_bytes = |
| 28 basename.size() * sizeof(base::FilePath::CharType); | 35 basename.size() * sizeof(base::FilePath::CharType); |
| 29 std::string name_bytes(basename_length_in_bytes, '\0'); | 36 std::string name_bytes(basename_length_in_bytes, '\0'); |
| 30 memcpy(&name_bytes[0], &basename[0], basename_length_in_bytes); | 37 memcpy(&name_bytes[0], &basename[0], basename_length_in_bytes); |
| 31 return HashMetricName(name_bytes); | 38 return HashMetricName(name_bytes); |
| 32 } | 39 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 proto_profile->add_module_id(); | 104 proto_profile->add_module_id(); |
| 98 module_id->set_build_id(module.id); | 105 module_id->set_build_id(module.id); |
| 99 module_id->set_name_md5_prefix(HashModuleFilename(module.filename)); | 106 module_id->set_name_md5_prefix(HashModuleFilename(module.filename)); |
| 100 } | 107 } |
| 101 | 108 |
| 102 proto_profile->set_profile_duration_ms( | 109 proto_profile->set_profile_duration_ms( |
| 103 profile.profile_duration.InMilliseconds()); | 110 profile.profile_duration.InMilliseconds()); |
| 104 proto_profile->set_sampling_period_ms( | 111 proto_profile->set_sampling_period_ms( |
| 105 profile.sampling_period.InMilliseconds()); | 112 profile.sampling_period.InMilliseconds()); |
| 106 } | 113 } |
| 114 | |
| 107 } // namespace | 115 } // namespace |
| 108 | 116 |
| 109 CallStackProfileMetricsProvider::CallStackProfileMetricsProvider() {} | 117 const char CallStackProfileMetricsProvider::kFieldTrialName[] = |
| 118 "StackProfiling"; | |
| 119 const char CallStackProfileMetricsProvider::kReportProfilesGroupName[] = | |
| 120 "Report profiles"; | |
| 110 | 121 |
| 111 CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() {} | 122 CallStackProfileMetricsProvider::CallStackProfileMetricsProvider() |
| 123 : weak_factory_(this) { | |
| 124 } | |
| 125 | |
| 126 CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() { | |
| 127 StackSamplingProfiler::SetDefaultCompletedCallback( | |
| 128 StackSamplingProfiler::CompletedCallback()); | |
| 129 } | |
| 130 | |
| 131 void CallStackProfileMetricsProvider::OnRecordingEnabled() { | |
| 132 StackSamplingProfiler::SetDefaultCompletedCallback( | |
| 133 base::Bind(&CallStackProfileMetricsProvider::ReceiveCompletedProfiles, | |
| 134 base::MessageLoopProxy::current(), | |
| 135 weak_factory_.GetWeakPtr())); | |
| 136 } | |
| 137 | |
| 138 void CallStackProfileMetricsProvider::OnRecordingDisabled() { | |
| 139 StackSamplingProfiler::SetDefaultCompletedCallback( | |
| 140 base::Bind(&IgnoreCompletedProfiles)); | |
| 141 pending_profiles_.clear(); | |
| 142 } | |
| 112 | 143 |
| 113 void CallStackProfileMetricsProvider::ProvideGeneralMetrics( | 144 void CallStackProfileMetricsProvider::ProvideGeneralMetrics( |
| 114 ChromeUserMetricsExtension* uma_proto) { | 145 ChromeUserMetricsExtension* uma_proto) { |
| 115 std::vector<StackSamplingProfiler::Profile> profiles; | 146 if (!IsSamplingProfilingReportingEnabled()) |
| 116 if (!source_profiles_for_test_.empty()) | 147 return; |
| 117 profiles.swap(source_profiles_for_test_); | |
| 118 else | |
| 119 StackSamplingProfiler::GetPendingProfiles(&profiles); | |
| 120 | 148 |
| 121 for (const StackSamplingProfiler::Profile& profile : profiles) { | 149 for (const StackSamplingProfiler::Profile& profile : pending_profiles_) { |
| 122 CallStackProfile* call_stack_profile = | 150 CallStackProfile* call_stack_profile = |
| 123 uma_proto->add_sampled_profile()->mutable_call_stack_profile(); | 151 uma_proto->add_sampled_profile()->mutable_call_stack_profile(); |
| 124 CopyProfileToProto(profile, call_stack_profile); | 152 CopyProfileToProto(profile, call_stack_profile); |
| 125 } | 153 } |
| 126 } | 154 } |
| 127 | 155 |
| 128 void CallStackProfileMetricsProvider::SetSourceProfilesForTesting( | 156 void CallStackProfileMetricsProvider::SetSourceProfilesForTesting( |
| 129 const std::vector<StackSamplingProfiler::Profile>& profiles) { | 157 const std::vector<StackSamplingProfiler::Profile>& profiles) { |
| 130 source_profiles_for_test_ = profiles; | 158 pending_profiles_ = profiles; |
| 159 } | |
| 160 | |
| 161 // static | |
| 162 bool CallStackProfileMetricsProvider::IsSamplingProfilingReportingEnabled() { | |
| 163 const std::string group_name = | |
| 164 base::FieldTrialList::FindFullName( | |
| 165 CallStackProfileMetricsProvider::kFieldTrialName); | |
| 166 return group_name == | |
| 167 CallStackProfileMetricsProvider::kReportProfilesGroupName; | |
| 168 } | |
| 169 | |
| 170 // static | |
| 171 // Posts a message back to our own thread to collect the profiles. | |
| 172 void CallStackProfileMetricsProvider::ReceiveCompletedProfiles( | |
| 173 scoped_refptr<base::MessageLoopProxy> message_loop, | |
| 174 base::WeakPtr<CallStackProfileMetricsProvider> provider, | |
| 175 const std::vector<base::StackSamplingProfiler::Profile>& profiles) { | |
| 176 message_loop->PostTask( | |
| 177 FROM_HERE, | |
| 178 base::Bind(&CallStackProfileMetricsProvider::AppendCompletedProfiles, | |
| 179 provider, | |
| 180 profiles)); | |
| 181 } | |
| 182 | |
| 183 void CallStackProfileMetricsProvider::AppendCompletedProfiles( | |
| 184 const std::vector<base::StackSamplingProfiler::Profile>& profiles) { | |
| 185 pending_profiles_.insert(pending_profiles_.end(), profiles.begin(), | |
| 186 profiles.end()); | |
| 131 } | 187 } |
| 132 | 188 |
| 133 } // namespace metrics | 189 } // namespace metrics |
| OLD | NEW |