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/metrics/field_trial.h" | |
13 #include "base/profiler/stack_sampling_profiler.h" | 15 #include "base/profiler/stack_sampling_profiler.h" |
14 #include "components/metrics/metrics_hashes.h" | 16 #include "components/metrics/metrics_hashes.h" |
15 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" | 17 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
16 | 18 |
17 using base::StackSamplingProfiler; | 19 using base::StackSamplingProfiler; |
18 | 20 |
19 namespace metrics { | 21 namespace metrics { |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
25 bool IsSamplingProfilingReportingEnabled() { | |
Ilya Sherman
2015/03/24 04:02:03
nit: Doc string, please.
Mike Wittman
2015/03/24 18:37:39
Done.
| |
26 const std::string group_name = | |
27 base::FieldTrialList::FindFullName( | |
28 CallStackProfileMetricsProvider::kFieldTrialName); | |
29 return group_name == | |
30 CallStackProfileMetricsProvider::kReportProfilesGroupName; | |
31 } | |
32 | |
23 // The protobuf expects the MD5 checksum prefix of the module name. | 33 // The protobuf expects the MD5 checksum prefix of the module name. |
24 uint64 HashModuleFilename(const base::FilePath& filename) { | 34 uint64 HashModuleFilename(const base::FilePath& filename) { |
25 const base::FilePath::StringType basename = filename.BaseName().value(); | 35 const base::FilePath::StringType basename = filename.BaseName().value(); |
26 // Copy the bytes in basename into a string buffer. | 36 // Copy the bytes in basename into a string buffer. |
27 size_t basename_length_in_bytes = | 37 size_t basename_length_in_bytes = |
28 basename.size() * sizeof(base::FilePath::CharType); | 38 basename.size() * sizeof(base::FilePath::CharType); |
29 std::string name_bytes(basename_length_in_bytes, '\0'); | 39 std::string name_bytes(basename_length_in_bytes, '\0'); |
30 memcpy(&name_bytes[0], &basename[0], basename_length_in_bytes); | 40 memcpy(&name_bytes[0], &basename[0], basename_length_in_bytes); |
31 return HashMetricName(name_bytes); | 41 return HashMetricName(name_bytes); |
32 } | 42 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 CallStackProfile::ModuleIdentifier* module_id = | 106 CallStackProfile::ModuleIdentifier* module_id = |
97 proto_profile->add_module_id(); | 107 proto_profile->add_module_id(); |
98 module_id->set_build_id(module.id); | 108 module_id->set_build_id(module.id); |
99 module_id->set_name_md5_prefix(HashModuleFilename(module.filename)); | 109 module_id->set_name_md5_prefix(HashModuleFilename(module.filename)); |
100 } | 110 } |
101 | 111 |
102 proto_profile->set_profile_duration_ms( | 112 proto_profile->set_profile_duration_ms( |
103 profile.profile_duration.InMilliseconds()); | 113 profile.profile_duration.InMilliseconds()); |
104 proto_profile->set_sampling_period_ms( | 114 proto_profile->set_sampling_period_ms( |
105 profile.sampling_period.InMilliseconds()); | 115 profile.sampling_period.InMilliseconds()); |
106 } | 116 } |
Ilya Sherman
2015/03/24 04:02:03
nit: Please leave a blank line here.
Mike Wittman
2015/03/24 18:37:39
Done.
| |
107 } // namespace | 117 } // namespace |
108 | 118 |
109 CallStackProfileMetricsProvider::CallStackProfileMetricsProvider() {} | 119 const char CallStackProfileMetricsProvider::kFieldTrialName[] = |
120 "StackProfiling"; | |
121 const char CallStackProfileMetricsProvider::kReportProfilesGroupName[] = | |
122 "Report profiles"; | |
110 | 123 |
111 CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() {} | 124 CallStackProfileMetricsProvider::CallStackProfileMetricsProvider() |
125 : weak_factory_(this) { | |
126 } | |
127 | |
128 CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() { | |
129 StackSamplingProfiler::SetDefaultCompletedCallback( | |
130 StackSamplingProfiler::CompletedCallback()); | |
131 } | |
132 | |
133 void CallStackProfileMetricsProvider::OnRecordingEnabled() { | |
134 StackSamplingProfiler::SetDefaultCompletedCallback( | |
135 base::Bind(&CallStackProfileMetricsProvider::ReceiveCompletedProfiles, | |
136 weak_factory_.GetWeakPtr())); | |
Ilya Sherman
2015/03/24 04:02:03
Note that weak pointers are only safe to read on t
Mike Wittman
2015/03/24 18:37:39
Ah, didn't realize that. Updated to post a task ba
| |
137 } | |
138 | |
139 void CallStackProfileMetricsProvider::OnRecordingDisabled() { | |
140 StackSamplingProfiler::SetDefaultCompletedCallback( | |
141 base::Bind(&CallStackProfileMetricsProvider::IgnoreCompletedProfiles)); | |
142 { | |
143 base::AutoLock scoped_lock(pending_profiles_lock_); | |
144 pending_profiles_.clear(); | |
145 } | |
146 } | |
112 | 147 |
113 void CallStackProfileMetricsProvider::ProvideGeneralMetrics( | 148 void CallStackProfileMetricsProvider::ProvideGeneralMetrics( |
114 ChromeUserMetricsExtension* uma_proto) { | 149 ChromeUserMetricsExtension* uma_proto) { |
115 std::vector<StackSamplingProfiler::Profile> profiles; | 150 std::vector<StackSamplingProfiler::Profile> profiles; |
116 if (!source_profiles_for_test_.empty()) | 151 { |
117 profiles.swap(source_profiles_for_test_); | 152 base::AutoLock scoped_lock(pending_profiles_lock_); |
118 else | 153 profiles.swap(pending_profiles_); |
119 StackSamplingProfiler::GetPendingProfiles(&profiles); | 154 } |
155 | |
156 if (!IsSamplingProfilingReportingEnabled()) | |
157 return; | |
120 | 158 |
121 for (const StackSamplingProfiler::Profile& profile : profiles) { | 159 for (const StackSamplingProfiler::Profile& profile : profiles) { |
122 CallStackProfile* call_stack_profile = | 160 CallStackProfile* call_stack_profile = |
123 uma_proto->add_sampled_profile()->mutable_call_stack_profile(); | 161 uma_proto->add_sampled_profile()->mutable_call_stack_profile(); |
124 CopyProfileToProto(profile, call_stack_profile); | 162 CopyProfileToProto(profile, call_stack_profile); |
125 } | 163 } |
126 } | 164 } |
127 | 165 |
128 void CallStackProfileMetricsProvider::SetSourceProfilesForTesting( | 166 void CallStackProfileMetricsProvider::SetSourceProfilesForTesting( |
129 const std::vector<StackSamplingProfiler::Profile>& profiles) { | 167 const std::vector<StackSamplingProfiler::Profile>& profiles) { |
130 source_profiles_for_test_ = profiles; | 168 base::AutoLock scoped_lock(pending_profiles_lock_); |
169 pending_profiles_ = profiles; | |
170 } | |
171 | |
172 void CallStackProfileMetricsProvider::ReceiveCompletedProfiles( | |
173 const std::vector<base::StackSamplingProfiler::Profile>& profiles) { | |
174 base::AutoLock scoped_lock(pending_profiles_lock_); | |
175 pending_profiles_.insert(pending_profiles_.end(), profiles.begin(), | |
176 profiles.end()); | |
177 } | |
178 | |
179 // static | |
180 void CallStackProfileMetricsProvider::IgnoreCompletedProfiles( | |
181 const std::vector<base::StackSamplingProfiler::Profile>& profiles) { | |
131 } | 182 } |
132 | 183 |
133 } // namespace metrics | 184 } // namespace metrics |
OLD | NEW |