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

Side by Side Diff: components/metrics/call_stack_profile_metrics_provider.cc

Issue 2438073002: Use movable types for CallStackProfile(s) to remove copying of data. (Closed)
Patch Set: addressed review comments by wittman Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 21 matching lines...) Expand all
32 namespace metrics { 32 namespace metrics {
33 33
34 namespace { 34 namespace {
35 35
36 // ProfilesState -------------------------------------------------------------- 36 // ProfilesState --------------------------------------------------------------
37 37
38 // A set of profiles and the CallStackProfileMetricsProvider state associated 38 // A set of profiles and the CallStackProfileMetricsProvider state associated
39 // with them. 39 // with them.
40 struct ProfilesState { 40 struct ProfilesState {
41 ProfilesState(const CallStackProfileParams& params, 41 ProfilesState(const CallStackProfileParams& params,
42 const base::StackSamplingProfiler::CallStackProfiles& profiles, 42 base::StackSamplingProfiler::CallStackProfiles profiles,
43 base::TimeTicks start_timestamp); 43 base::TimeTicks start_timestamp);
44 ProfilesState(ProfilesState&&);
45 ProfilesState& operator=(ProfilesState&&);
44 46
45 // The metrics-related parameters provided to 47 // The metrics-related parameters provided to
46 // CallStackProfileMetricsProvider::GetProfilerCallback(). 48 // CallStackProfileMetricsProvider::GetProfilerCallback().
47 CallStackProfileParams params; 49 CallStackProfileParams params;
48 50
49 // The call stack profiles collected by the profiler. 51 // The call stack profiles collected by the profiler.
50 base::StackSamplingProfiler::CallStackProfiles profiles; 52 base::StackSamplingProfiler::CallStackProfiles profiles;
51 53
52 // The time at which the CallStackProfileMetricsProvider became aware of the 54 // The time at which the CallStackProfileMetricsProvider became aware of the
53 // request for profiling. In particular, this is when callback was requested 55 // request for profiling. In particular, this is when callback was requested
54 // via CallStackProfileMetricsProvider::GetProfilerCallback(). Used to 56 // via CallStackProfileMetricsProvider::GetProfilerCallback(). Used to
55 // determine if collection was disabled during the collection of the profile. 57 // determine if collection was disabled during the collection of the profile.
56 base::TimeTicks start_timestamp; 58 base::TimeTicks start_timestamp;
59
60 private:
61 DISALLOW_COPY_AND_ASSIGN(ProfilesState);
Alexei Svitkine (slow) 2016/10/24 15:26:22 Indent seems wrong. Run git cl format?
bcwhite 2016/10/24 21:30:30 Done.
57 }; 62 };
58 63
59 ProfilesState::ProfilesState( 64 ProfilesState::ProfilesState(
60 const CallStackProfileParams& params, 65 const CallStackProfileParams& params,
61 const base::StackSamplingProfiler::CallStackProfiles& profiles, 66 base::StackSamplingProfiler::CallStackProfiles profiles,
62 base::TimeTicks start_timestamp) 67 base::TimeTicks start_timestamp)
63 : params(params), 68 : params(params),
64 profiles(profiles), 69 profiles(std::move(profiles)),
65 start_timestamp(start_timestamp) { 70 start_timestamp(start_timestamp) {}
66 } 71
72 ProfilesState::ProfilesState(ProfilesState&&) = default;
73
74 // Some versions of GCC need this for push_back to work with std::move.
75 ProfilesState& ProfilesState::operator=(ProfilesState&&) = default;
67 76
68 // PendingProfiles ------------------------------------------------------------ 77 // PendingProfiles ------------------------------------------------------------
69 78
70 // Singleton class responsible for retaining profiles received via the callback 79 // Singleton class responsible for retaining profiles received via the callback
71 // created by CallStackProfileMetricsProvider::GetProfilerCallback(). These are 80 // created by CallStackProfileMetricsProvider::GetProfilerCallback(). These are
72 // then sent to UMA on the invocation of 81 // then sent to UMA on the invocation of
73 // CallStackProfileMetricsProvider::ProvideGeneralMetrics(). We need to store 82 // CallStackProfileMetricsProvider::ProvideGeneralMetrics(). We need to store
74 // the profiles outside of a CallStackProfileMetricsProvider instance since 83 // the profiles outside of a CallStackProfileMetricsProvider instance since
75 // callers may start profiling before the CallStackProfileMetricsProvider is 84 // callers may start profiling before the CallStackProfileMetricsProvider is
76 // created. 85 // created.
77 // 86 //
78 // Member functions on this class may be called on any thread. 87 // Member functions on this class may be called on any thread.
79 class PendingProfiles { 88 class PendingProfiles {
80 public: 89 public:
81 static PendingProfiles* GetInstance(); 90 static PendingProfiles* GetInstance();
82 91
83 void Clear(); 92 void Clear();
84 void Swap(std::vector<ProfilesState>* profiles); 93 void Swap(std::vector<ProfilesState>* profiles);
85 94
86 // Enables the collection of profiles by CollectProfilesIfCollectionEnabled if 95 // Enables the collection of profiles by CollectProfilesIfCollectionEnabled if
87 // |enabled| is true. Otherwise, clears current profiles and ignores profiles 96 // |enabled| is true. Otherwise, clears current profiles and ignores profiles
88 // provided to future invocations of CollectProfilesIfCollectionEnabled. 97 // provided to future invocations of CollectProfilesIfCollectionEnabled.
89 void SetCollectionEnabled(bool enabled); 98 void SetCollectionEnabled(bool enabled);
90 99
91 // True if profiles are being collected. 100 // True if profiles are being collected.
92 bool IsCollectionEnabled() const; 101 bool IsCollectionEnabled() const;
93 102
94 // Adds |profile| to the list of profiles if collection is enabled. 103 // Adds |profiles| to the list of profiles if collection is enabled.
95 void CollectProfilesIfCollectionEnabled(const ProfilesState& profiles); 104 void CollectProfilesIfCollectionEnabled(ProfilesState profiles);
96 105
97 // Allows testing against the initial state multiple times. 106 // Allows testing against the initial state multiple times.
98 void ResetToDefaultStateForTesting(); 107 void ResetToDefaultStateForTesting();
99 108
100 private: 109 private:
101 friend struct base::DefaultSingletonTraits<PendingProfiles>; 110 friend struct base::DefaultSingletonTraits<PendingProfiles>;
102 111
103 PendingProfiles(); 112 PendingProfiles();
104 ~PendingProfiles(); 113 ~PendingProfiles();
105 114
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 last_collection_disable_time_ = base::TimeTicks::Now(); 155 last_collection_disable_time_ = base::TimeTicks::Now();
147 } 156 }
148 } 157 }
149 158
150 bool PendingProfiles::IsCollectionEnabled() const { 159 bool PendingProfiles::IsCollectionEnabled() const {
151 base::AutoLock scoped_lock(lock_); 160 base::AutoLock scoped_lock(lock_);
152 return collection_enabled_; 161 return collection_enabled_;
153 } 162 }
154 163
155 void PendingProfiles::CollectProfilesIfCollectionEnabled( 164 void PendingProfiles::CollectProfilesIfCollectionEnabled(
156 const ProfilesState& profiles) { 165 ProfilesState profiles) {
157 base::AutoLock scoped_lock(lock_); 166 base::AutoLock scoped_lock(lock_);
158 167
159 // Only collect if collection is not disabled and hasn't been disabled 168 // Only collect if collection is not disabled and hasn't been disabled
160 // since the start of collection for this profile. 169 // since the start of collection for this profile.
161 if (!collection_enabled_ || 170 if (!collection_enabled_ ||
162 (!last_collection_disable_time_.is_null() && 171 (!last_collection_disable_time_.is_null() &&
163 last_collection_disable_time_ >= profiles.start_timestamp)) { 172 last_collection_disable_time_ >= profiles.start_timestamp)) {
164 return; 173 return;
165 } 174 }
166 175
167 profiles_.push_back(profiles); 176 profiles_.push_back(std::move(profiles));
168 } 177 }
169 178
170 void PendingProfiles::ResetToDefaultStateForTesting() { 179 void PendingProfiles::ResetToDefaultStateForTesting() {
171 base::AutoLock scoped_lock(lock_); 180 base::AutoLock scoped_lock(lock_);
172 181
173 collection_enabled_ = true; 182 collection_enabled_ = true;
174 last_collection_disable_time_ = base::TimeTicks(); 183 last_collection_disable_time_ = base::TimeTicks();
175 profiles_.clear(); 184 profiles_.clear();
176 } 185 }
177 186
178 // |collection_enabled_| is initialized to true to collect any profiles that are 187 // |collection_enabled_| is initialized to true to collect any profiles that are
179 // generated prior to creation of the CallStackProfileMetricsProvider. The 188 // generated prior to creation of the CallStackProfileMetricsProvider. The
180 // ultimate disposition of these pre-creation collected profiles will be 189 // ultimate disposition of these pre-creation collected profiles will be
181 // determined by the initial recording state provided to 190 // determined by the initial recording state provided to
182 // CallStackProfileMetricsProvider. 191 // CallStackProfileMetricsProvider.
183 PendingProfiles::PendingProfiles() : collection_enabled_(true) {} 192 PendingProfiles::PendingProfiles() : collection_enabled_(true) {}
184 193
185 PendingProfiles::~PendingProfiles() {} 194 PendingProfiles::~PendingProfiles() {}
186 195
187 // Functions to process completed profiles ------------------------------------ 196 // Functions to process completed profiles ------------------------------------
188 197
189 // Will be invoked on either the main thread or the profiler's thread. Provides 198 // Will be invoked on either the main thread or the profiler's thread. Provides
190 // the profiles to PendingProfiles to append, if the collecting state allows. 199 // the profiles to PendingProfiles to append, if the collecting state allows.
191 void ReceiveCompletedProfilesImpl( 200 void ReceiveCompletedProfilesImpl(
192 const CallStackProfileParams& params, 201 const CallStackProfileParams& params,
193 base::TimeTicks start_timestamp, 202 base::TimeTicks start_timestamp,
194 const StackSamplingProfiler::CallStackProfiles& profiles) { 203 StackSamplingProfiler::CallStackProfiles profiles) {
195 PendingProfiles::GetInstance()->CollectProfilesIfCollectionEnabled( 204 PendingProfiles::GetInstance()->CollectProfilesIfCollectionEnabled(
196 ProfilesState(params, profiles, start_timestamp)); 205 ProfilesState(params, std::move(profiles), start_timestamp));
197 } 206 }
198 207
199 // Invoked on an arbitrary thread. Ignores the provided profiles. 208 // Invoked on an arbitrary thread. Ignores the provided profiles.
200 void IgnoreCompletedProfiles( 209 void IgnoreCompletedProfiles(
201 const StackSamplingProfiler::CallStackProfiles& profiles) { 210 StackSamplingProfiler::CallStackProfiles profiles) {}
202 }
203 211
204 // Functions to encode protobufs ---------------------------------------------- 212 // Functions to encode protobufs ----------------------------------------------
205 213
206 // The protobuf expects the MD5 checksum prefix of the module name. 214 // The protobuf expects the MD5 checksum prefix of the module name.
207 uint64_t HashModuleFilename(const base::FilePath& filename) { 215 uint64_t HashModuleFilename(const base::FilePath& filename) {
208 const base::FilePath::StringType basename = filename.BaseName().value(); 216 const base::FilePath::StringType basename = filename.BaseName().value();
209 // Copy the bytes in basename into a string buffer. 217 // Copy the bytes in basename into a string buffer.
210 size_t basename_length_in_bytes = 218 size_t basename_length_in_bytes =
211 basename.size() * sizeof(base::FilePath::CharType); 219 basename.size() * sizeof(base::FilePath::CharType);
212 std::string name_bytes(basename_length_in_bytes, '\0'); 220 std::string name_bytes(basename_length_in_bytes, '\0');
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 return base::Bind(&IgnoreCompletedProfiles); 399 return base::Bind(&IgnoreCompletedProfiles);
392 400
393 return base::Bind(&ReceiveCompletedProfilesImpl, params, 401 return base::Bind(&ReceiveCompletedProfilesImpl, params,
394 base::TimeTicks::Now()); 402 base::TimeTicks::Now());
395 } 403 }
396 404
397 // static 405 // static
398 void CallStackProfileMetricsProvider::ReceiveCompletedProfiles( 406 void CallStackProfileMetricsProvider::ReceiveCompletedProfiles(
399 const CallStackProfileParams& params, 407 const CallStackProfileParams& params,
400 base::TimeTicks start_timestamp, 408 base::TimeTicks start_timestamp,
401 const base::StackSamplingProfiler::CallStackProfiles& profiles) { 409 base::StackSamplingProfiler::CallStackProfiles profiles) {
402 ReceiveCompletedProfilesImpl(params, start_timestamp, profiles); 410 ReceiveCompletedProfilesImpl(params, start_timestamp, std::move(profiles));
403 } 411 }
404 412
405 void CallStackProfileMetricsProvider::OnRecordingEnabled() { 413 void CallStackProfileMetricsProvider::OnRecordingEnabled() {
406 PendingProfiles::GetInstance()->SetCollectionEnabled(true); 414 PendingProfiles::GetInstance()->SetCollectionEnabled(true);
407 } 415 }
408 416
409 void CallStackProfileMetricsProvider::OnRecordingDisabled() { 417 void CallStackProfileMetricsProvider::OnRecordingDisabled() {
410 PendingProfiles::GetInstance()->SetCollectionEnabled(false); 418 PendingProfiles::GetInstance()->SetCollectionEnabled(false);
411 } 419 }
412 420
(...skipping 27 matching lines...) Expand all
440 448
441 // static 449 // static
442 bool CallStackProfileMetricsProvider::IsReportingEnabledByFieldTrial() { 450 bool CallStackProfileMetricsProvider::IsReportingEnabledByFieldTrial() {
443 const std::string group_name = base::FieldTrialList::FindFullName( 451 const std::string group_name = base::FieldTrialList::FindFullName(
444 CallStackProfileMetricsProvider::kFieldTrialName); 452 CallStackProfileMetricsProvider::kFieldTrialName);
445 return group_name == 453 return group_name ==
446 CallStackProfileMetricsProvider::kReportProfilesGroupName; 454 CallStackProfileMetricsProvider::kReportProfilesGroupName;
447 } 455 }
448 456
449 } // namespace metrics 457 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698