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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <cstring> | 11 #include <cstring> |
| 12 #include <map> | 12 #include <map> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/location.h" | 18 #include "base/location.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/macros.h" | 20 #include "base/macros.h" |
| 21 #include "base/memory/singleton.h" | 21 #include "base/memory/singleton.h" |
| 22 #include "base/metrics/field_trial.h" | 22 #include "base/metrics/field_trial_params.h" |
| 23 #include "base/metrics/metrics_hashes.h" | 23 #include "base/metrics/metrics_hashes.h" |
| 24 #include "base/profiler/stack_sampling_profiler.h" | 24 #include "base/profiler/stack_sampling_profiler.h" |
| 25 #include "base/single_thread_task_runner.h" | 25 #include "base/single_thread_task_runner.h" |
| 26 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 27 #include "base/threading/thread_task_runner_handle.h" | 27 #include "base/threading/thread_task_runner_handle.h" |
| 28 #include "base/time/time.h" | 28 #include "base/time/time.h" |
| 29 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" | 29 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
| 30 | 30 |
| 31 using base::StackSamplingProfiler; | 31 using base::StackSamplingProfiler; |
| 32 | 32 |
| 33 namespace metrics { | 33 namespace metrics { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // Interval for periodic (post-startup) sampling, when enabled. | |
| 38 constexpr base::TimeDelta kPeriodicSamplingInterval = | |
| 39 base::TimeDelta::FromSeconds(1); | |
| 40 | |
| 37 // Provide a mapping from the C++ "enum" definition of various process mile- | 41 // Provide a mapping from the C++ "enum" definition of various process mile- |
| 38 // stones to the equivalent protobuf "enum" definition. This table-lookup | 42 // stones to the equivalent protobuf "enum" definition. This table-lookup |
| 39 // conversion allows for the implementation to evolve and still be compatible | 43 // conversion allows for the implementation to evolve and still be compatible |
| 40 // with the protobuf -- even if there are ever more than 32 defined proto | 44 // with the protobuf -- even if there are ever more than 32 defined proto |
| 41 // values, though never more than 32 could be in-use in a given C++ version | 45 // values, though never more than 32 could be in-use in a given C++ version |
| 42 // of the code. | 46 // of the code. |
| 43 const ProcessPhase | 47 const ProcessPhase |
| 44 kProtoPhases[CallStackProfileMetricsProvider::MILESTONES_MAX_VALUE] = { | 48 kProtoPhases[CallStackProfileMetricsProvider::MILESTONES_MAX_VALUE] = { |
| 45 ProcessPhase::MAIN_LOOP_START, | 49 ProcessPhase::MAIN_LOOP_START, |
| 46 ProcessPhase::MAIN_NAVIGATION_START, | 50 ProcessPhase::MAIN_NAVIGATION_START, |
| 47 ProcessPhase::MAIN_NAVIGATION_FINISHED, | 51 ProcessPhase::MAIN_NAVIGATION_FINISHED, |
| 48 ProcessPhase::FIRST_NONEMPTY_PAINT, | 52 ProcessPhase::FIRST_NONEMPTY_PAINT, |
| 49 | 53 |
| 50 ProcessPhase::SHUTDOWN_START, | 54 ProcessPhase::SHUTDOWN_START, |
| 51 }; | 55 }; |
| 52 | 56 |
| 53 // ProfilesState -------------------------------------------------------------- | 57 // ProfilesState -------------------------------------------------------------- |
| 54 | 58 |
| 55 // A set of profiles and the CallStackProfileMetricsProvider state associated | 59 // A set of profiles and the CallStackProfileMetricsProvider state associated |
| 56 // with them. | 60 // with them. |
| 57 struct ProfilesState { | 61 struct ProfilesState { |
| 58 ProfilesState(const CallStackProfileParams& params, | 62 ProfilesState(const CallStackProfileParams& params, |
| 59 StackSamplingProfiler::CallStackProfiles profiles, | 63 StackSamplingProfiler::CallStackProfiles profiles); |
| 60 base::TimeTicks start_timestamp); | |
| 61 ProfilesState(ProfilesState&&); | 64 ProfilesState(ProfilesState&&); |
| 62 ProfilesState& operator=(ProfilesState&&); | 65 ProfilesState& operator=(ProfilesState&&); |
| 63 | 66 |
| 64 // The metrics-related parameters provided to | 67 // The metrics-related parameters provided to |
| 65 // CallStackProfileMetricsProvider::GetProfilerCallback(). | 68 // CallStackProfileMetricsProvider::GetProfilerCallback(). |
| 66 CallStackProfileParams params; | 69 CallStackProfileParams params; |
| 67 | 70 |
| 68 // The call stack profiles collected by the profiler. | 71 // The call stack profiles collected by the profiler. |
| 69 StackSamplingProfiler::CallStackProfiles profiles; | 72 StackSamplingProfiler::CallStackProfiles profiles; |
| 70 | 73 |
| 71 // The time at which the CallStackProfileMetricsProvider became aware of the | |
| 72 // request for profiling. In particular, this is when callback was requested | |
| 73 // via CallStackProfileMetricsProvider::GetProfilerCallback(). Used to | |
| 74 // determine if collection was disabled during the collection of the profile. | |
| 75 base::TimeTicks start_timestamp; | |
| 76 | |
| 77 private: | 74 private: |
| 78 DISALLOW_COPY_AND_ASSIGN(ProfilesState); | 75 DISALLOW_COPY_AND_ASSIGN(ProfilesState); |
| 79 }; | 76 }; |
| 80 | 77 |
| 81 ProfilesState::ProfilesState(const CallStackProfileParams& params, | 78 ProfilesState::ProfilesState(const CallStackProfileParams& params, |
| 82 StackSamplingProfiler::CallStackProfiles profiles, | 79 StackSamplingProfiler::CallStackProfiles profiles) |
| 83 base::TimeTicks start_timestamp) | 80 : params(params), profiles(std::move(profiles)) {} |
| 84 : params(params), | |
| 85 profiles(std::move(profiles)), | |
| 86 start_timestamp(start_timestamp) {} | |
| 87 | 81 |
| 88 ProfilesState::ProfilesState(ProfilesState&&) = default; | 82 ProfilesState::ProfilesState(ProfilesState&&) = default; |
| 89 | 83 |
| 90 // Some versions of GCC need this for push_back to work with std::move. | 84 // Some versions of GCC need this for push_back to work with std::move. |
| 91 ProfilesState& ProfilesState::operator=(ProfilesState&&) = default; | 85 ProfilesState& ProfilesState::operator=(ProfilesState&&) = default; |
| 92 | 86 |
| 93 // PendingProfiles ------------------------------------------------------------ | 87 // PendingProfiles ------------------------------------------------------------ |
| 94 | 88 |
| 95 // Singleton class responsible for retaining profiles received via the callback | 89 // Singleton class responsible for retaining profiles received via the callback |
| 96 // created by CallStackProfileMetricsProvider::GetProfilerCallback(). These are | 90 // created by CallStackProfileMetricsProvider::GetProfilerCallback(). These are |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 } | 167 } |
| 174 | 168 |
| 175 void PendingProfiles::CollectProfilesIfCollectionEnabled( | 169 void PendingProfiles::CollectProfilesIfCollectionEnabled( |
| 176 ProfilesState profiles) { | 170 ProfilesState profiles) { |
| 177 base::AutoLock scoped_lock(lock_); | 171 base::AutoLock scoped_lock(lock_); |
| 178 | 172 |
| 179 // Only collect if collection is not disabled and hasn't been disabled | 173 // Only collect if collection is not disabled and hasn't been disabled |
| 180 // since the start of collection for this profile. | 174 // since the start of collection for this profile. |
| 181 if (!collection_enabled_ || | 175 if (!collection_enabled_ || |
| 182 (!last_collection_disable_time_.is_null() && | 176 (!last_collection_disable_time_.is_null() && |
| 183 last_collection_disable_time_ >= profiles.start_timestamp)) { | 177 last_collection_disable_time_ >= profiles.params.start_timestamp)) { |
| 184 return; | 178 return; |
| 185 } | 179 } |
| 186 | 180 |
| 187 profiles_.push_back(std::move(profiles)); | 181 profiles_.push_back(std::move(profiles)); |
| 188 } | 182 } |
| 189 | 183 |
| 190 void PendingProfiles::ResetToDefaultStateForTesting() { | 184 void PendingProfiles::ResetToDefaultStateForTesting() { |
| 191 base::AutoLock scoped_lock(lock_); | 185 base::AutoLock scoped_lock(lock_); |
| 192 | 186 |
| 193 collection_enabled_ = true; | 187 collection_enabled_ = true; |
| 194 last_collection_disable_time_ = base::TimeTicks(); | 188 last_collection_disable_time_ = base::TimeTicks(); |
| 195 profiles_.clear(); | 189 profiles_.clear(); |
| 196 } | 190 } |
| 197 | 191 |
| 198 // |collection_enabled_| is initialized to true to collect any profiles that are | 192 // |collection_enabled_| is initialized to true to collect any profiles that are |
| 199 // generated prior to creation of the CallStackProfileMetricsProvider. The | 193 // generated prior to creation of the CallStackProfileMetricsProvider. The |
| 200 // ultimate disposition of these pre-creation collected profiles will be | 194 // ultimate disposition of these pre-creation collected profiles will be |
| 201 // determined by the initial recording state provided to | 195 // determined by the initial recording state provided to |
| 202 // CallStackProfileMetricsProvider. | 196 // CallStackProfileMetricsProvider. |
| 203 PendingProfiles::PendingProfiles() : collection_enabled_(true) {} | 197 PendingProfiles::PendingProfiles() : collection_enabled_(true) {} |
| 204 | 198 |
| 205 PendingProfiles::~PendingProfiles() {} | 199 PendingProfiles::~PendingProfiles() {} |
| 206 | 200 |
| 207 // Functions to process completed profiles ------------------------------------ | 201 // Functions to process completed profiles ------------------------------------ |
| 208 | 202 |
| 209 // Will be invoked on either the main thread or the profiler's thread. Provides | 203 // Will be invoked on either the main thread or the profiler's thread. Provides |
| 210 // the profiles to PendingProfiles to append, if the collecting state allows. | 204 // the profiles to PendingProfiles to append, if the collecting state allows. |
| 211 void ReceiveCompletedProfilesImpl( | 205 base::Optional<StackSamplingProfiler::SamplingParams> |
| 212 const CallStackProfileParams& params, | 206 ReceiveCompletedProfilesImpl( |
| 213 base::TimeTicks start_timestamp, | 207 CallStackProfileParams* params, |
| 214 StackSamplingProfiler::CallStackProfiles profiles) { | 208 StackSamplingProfiler::CallStackProfiles profiles) { |
| 215 PendingProfiles::GetInstance()->CollectProfilesIfCollectionEnabled( | 209 PendingProfiles::GetInstance()->CollectProfilesIfCollectionEnabled( |
| 216 ProfilesState(params, std::move(profiles), start_timestamp)); | 210 ProfilesState(*params, std::move(profiles))); |
| 211 | |
| 212 // Now, schedule periodic sampling every 1s, if enabled by trial. | |
| 213 // TODO(asvitkine): Support periodic sampling for non-browser processes. | |
| 214 if (CallStackProfileMetricsProvider::IsPeriodicSamplingEnabled() && | |
| 215 params->process == CallStackProfileParams::BROWSER_PROCESS && | |
| 216 params->thread == CallStackProfileParams::UI_THREAD) { | |
| 217 params->trigger = metrics::CallStackProfileParams::PERIODIC_COLLECTION; | |
| 218 params->start_timestamp = base::TimeTicks::Now(); | |
| 219 | |
| 220 StackSamplingProfiler::SamplingParams sampling_params; | |
| 221 sampling_params.initial_delay = kPeriodicSamplingInterval; | |
| 222 sampling_params.bursts = 1; | |
| 223 sampling_params.samples_per_burst = 1; | |
| 224 // Below are unused: | |
| 225 sampling_params.burst_interval = base::TimeDelta::FromMilliseconds(0); | |
| 226 sampling_params.sampling_interval = base::TimeDelta::FromMilliseconds(0); | |
| 227 return sampling_params; | |
| 228 } | |
| 229 return base::Optional<StackSamplingProfiler::SamplingParams>(); | |
| 217 } | 230 } |
| 218 | 231 |
| 219 // Invoked on an arbitrary thread. Ignores the provided profiles. | 232 // Invoked on an arbitrary thread. Ignores the provided profiles. |
| 220 void IgnoreCompletedProfiles( | 233 base::Optional<StackSamplingProfiler::SamplingParams> IgnoreCompletedProfiles( |
| 221 StackSamplingProfiler::CallStackProfiles profiles) {} | 234 StackSamplingProfiler::CallStackProfiles profiles) { |
| 235 return base::Optional<StackSamplingProfiler::SamplingParams>(); | |
| 236 } | |
| 222 | 237 |
| 223 // Functions to encode protobufs ---------------------------------------------- | 238 // Functions to encode protobufs ---------------------------------------------- |
| 224 | 239 |
| 225 // The protobuf expects the MD5 checksum prefix of the module name. | 240 // The protobuf expects the MD5 checksum prefix of the module name. |
| 226 uint64_t HashModuleFilename(const base::FilePath& filename) { | 241 uint64_t HashModuleFilename(const base::FilePath& filename) { |
| 227 const base::FilePath::StringType basename = filename.BaseName().value(); | 242 const base::FilePath::StringType basename = filename.BaseName().value(); |
| 228 // Copy the bytes in basename into a string buffer. | 243 // Copy the bytes in basename into a string buffer. |
| 229 size_t basename_length_in_bytes = | 244 size_t basename_length_in_bytes = |
| 230 basename.size() * sizeof(base::FilePath::CharType); | 245 basename.size() * sizeof(base::FilePath::CharType); |
| 231 std::string name_bytes(basename_length_in_bytes, '\0'); | 246 std::string name_bytes(basename_length_in_bytes, '\0'); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 sample_proto->add_process_phase(kProtoPhases[bit]); | 288 sample_proto->add_process_phase(kProtoPhases[bit]); |
| 274 new_milestones ^= flag; // Bit is set so XOR will clear it. | 289 new_milestones ^= flag; // Bit is set so XOR will clear it. |
| 275 } | 290 } |
| 276 } | 291 } |
| 277 } | 292 } |
| 278 | 293 |
| 279 // Transcode |profile| into |proto_profile|. | 294 // Transcode |profile| into |proto_profile|. |
| 280 void CopyProfileToProto( | 295 void CopyProfileToProto( |
| 281 const StackSamplingProfiler::CallStackProfile& profile, | 296 const StackSamplingProfiler::CallStackProfile& profile, |
| 282 CallStackProfileParams::SampleOrderingSpec ordering_spec, | 297 CallStackProfileParams::SampleOrderingSpec ordering_spec, |
| 298 int64_t sampling_period_ms, | |
| 283 CallStackProfile* proto_profile) { | 299 CallStackProfile* proto_profile) { |
| 284 if (profile.samples.empty()) | 300 if (profile.samples.empty()) |
| 285 return; | 301 return; |
| 286 | 302 |
| 287 const bool preserve_order = | 303 const bool preserve_order = |
| 288 (ordering_spec == CallStackProfileParams::PRESERVE_ORDER); | 304 (ordering_spec == CallStackProfileParams::PRESERVE_ORDER); |
| 289 | 305 |
| 290 std::map<StackSamplingProfiler::Sample, int> sample_index; | 306 std::map<StackSamplingProfiler::Sample, int> sample_index; |
| 291 uint32_t milestones = 0; | 307 uint32_t milestones = 0; |
| 292 for (auto it = profile.samples.begin(); it != profile.samples.end(); ++it) { | 308 for (auto it = profile.samples.begin(); it != profile.samples.end(); ++it) { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 323 | 339 |
| 324 for (const StackSamplingProfiler::Module& module : profile.modules) { | 340 for (const StackSamplingProfiler::Module& module : profile.modules) { |
| 325 CallStackProfile::ModuleIdentifier* module_id = | 341 CallStackProfile::ModuleIdentifier* module_id = |
| 326 proto_profile->add_module_id(); | 342 proto_profile->add_module_id(); |
| 327 module_id->set_build_id(module.id); | 343 module_id->set_build_id(module.id); |
| 328 module_id->set_name_md5_prefix(HashModuleFilename(module.filename)); | 344 module_id->set_name_md5_prefix(HashModuleFilename(module.filename)); |
| 329 } | 345 } |
| 330 | 346 |
| 331 proto_profile->set_profile_duration_ms( | 347 proto_profile->set_profile_duration_ms( |
| 332 profile.profile_duration.InMilliseconds()); | 348 profile.profile_duration.InMilliseconds()); |
| 333 proto_profile->set_sampling_period_ms( | 349 proto_profile->set_sampling_period_ms(sampling_period_ms); |
| 334 profile.sampling_period.InMilliseconds()); | |
| 335 } | 350 } |
| 336 | 351 |
| 337 // Translates CallStackProfileParams's process to the corresponding | 352 // Translates CallStackProfileParams's process to the corresponding |
| 338 // execution context Process. | 353 // execution context Process. |
| 339 Process ToExecutionContextProcess(CallStackProfileParams::Process process) { | 354 Process ToExecutionContextProcess(CallStackProfileParams::Process process) { |
| 340 switch (process) { | 355 switch (process) { |
| 341 case CallStackProfileParams::UNKNOWN_PROCESS: | 356 case CallStackProfileParams::UNKNOWN_PROCESS: |
| 342 return UNKNOWN_PROCESS; | 357 return UNKNOWN_PROCESS; |
| 343 case CallStackProfileParams::BROWSER_PROCESS: | 358 case CallStackProfileParams::BROWSER_PROCESS: |
| 344 return BROWSER_PROCESS; | 359 return BROWSER_PROCESS; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 CallStackProfileParams::Trigger trigger) { | 413 CallStackProfileParams::Trigger trigger) { |
| 399 switch (trigger) { | 414 switch (trigger) { |
| 400 case CallStackProfileParams::UNKNOWN: | 415 case CallStackProfileParams::UNKNOWN: |
| 401 return SampledProfile::UNKNOWN_TRIGGER_EVENT; | 416 return SampledProfile::UNKNOWN_TRIGGER_EVENT; |
| 402 case CallStackProfileParams::PROCESS_STARTUP: | 417 case CallStackProfileParams::PROCESS_STARTUP: |
| 403 return SampledProfile::PROCESS_STARTUP; | 418 return SampledProfile::PROCESS_STARTUP; |
| 404 case CallStackProfileParams::JANKY_TASK: | 419 case CallStackProfileParams::JANKY_TASK: |
| 405 return SampledProfile::JANKY_TASK; | 420 return SampledProfile::JANKY_TASK; |
| 406 case CallStackProfileParams::THREAD_HUNG: | 421 case CallStackProfileParams::THREAD_HUNG: |
| 407 return SampledProfile::THREAD_HUNG; | 422 return SampledProfile::THREAD_HUNG; |
| 423 case CallStackProfileParams::PERIODIC_COLLECTION: | |
| 424 return SampledProfile::PERIODIC_COLLECTION; | |
| 408 } | 425 } |
| 409 NOTREACHED(); | 426 NOTREACHED(); |
| 410 return SampledProfile::UNKNOWN_TRIGGER_EVENT; | 427 return SampledProfile::UNKNOWN_TRIGGER_EVENT; |
| 411 } | 428 } |
| 412 | 429 |
| 413 } // namespace | 430 } // namespace |
| 414 | 431 |
| 415 // CallStackProfileMetricsProvider -------------------------------------------- | 432 // CallStackProfileMetricsProvider -------------------------------------------- |
| 416 | 433 |
| 417 const char CallStackProfileMetricsProvider::kFieldTrialName[] = | 434 const base::Feature CallStackProfileMetricsProvider::kEnableReporting = { |
| 418 "StackProfiling"; | 435 "SamplingProfilerReporting", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 419 const char CallStackProfileMetricsProvider::kReportProfilesGroupName[] = | |
| 420 "Report profiles"; | |
| 421 | 436 |
| 422 CallStackProfileMetricsProvider::CallStackProfileMetricsProvider() { | 437 CallStackProfileMetricsProvider::CallStackProfileMetricsProvider() { |
| 423 } | 438 } |
| 424 | 439 |
| 425 CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() { | 440 CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() { |
| 426 } | 441 } |
| 427 | 442 |
| 428 // This function can be invoked on an abitrary thread. | 443 // This function can be invoked on an abitrary thread. |
| 429 StackSamplingProfiler::CompletedCallback | 444 StackSamplingProfiler::CompletedCallback |
| 430 CallStackProfileMetricsProvider::GetProfilerCallback( | 445 CallStackProfileMetricsProvider::GetProfilerCallback( |
| 431 const CallStackProfileParams& params) { | 446 CallStackProfileParams* params) { |
| 432 // Ignore the profiles if the collection is disabled. If the collection state | 447 // Ignore the profiles if the collection is disabled. If the collection state |
| 433 // changes while collecting, this will be detected by the callback and | 448 // changes while collecting, this will be detected by the callback and |
| 434 // profiles will be ignored at that point. | 449 // profiles will be ignored at that point. |
| 435 if (!PendingProfiles::GetInstance()->IsCollectionEnabled()) | 450 if (!PendingProfiles::GetInstance()->IsCollectionEnabled()) |
| 436 return base::Bind(&IgnoreCompletedProfiles); | 451 return base::Bind(&IgnoreCompletedProfiles); |
| 437 | 452 |
| 438 return base::Bind(&ReceiveCompletedProfilesImpl, params, | 453 params->start_timestamp = base::TimeTicks::Now(); |
| 439 base::TimeTicks::Now()); | 454 return base::Bind(&ReceiveCompletedProfilesImpl, params); |
| 440 } | 455 } |
| 441 | 456 |
| 442 // static | 457 // static |
| 443 void CallStackProfileMetricsProvider::ReceiveCompletedProfiles( | 458 void CallStackProfileMetricsProvider::ReceiveCompletedProfiles( |
| 444 const CallStackProfileParams& params, | 459 CallStackProfileParams* params, |
| 445 base::TimeTicks start_timestamp, | 460 base::StackSamplingProfiler::CallStackProfiles profiles) { |
| 446 StackSamplingProfiler::CallStackProfiles profiles) { | 461 ReceiveCompletedProfilesImpl(params, std::move(profiles)); |
| 447 ReceiveCompletedProfilesImpl(params, start_timestamp, std::move(profiles)); | 462 } |
| 463 | |
| 464 // static | |
| 465 bool CallStackProfileMetricsProvider::IsPeriodicSamplingEnabled() { | |
| 466 // Ensure FeatureList has been initialized before calling into an API that | |
| 467 // calls base::FeatureList::IsEnabled() internally. While extremely unlikely, | |
| 468 // it is possible that the profiler callback and therefore this function get | |
| 469 // called before FeatureList initialization (e.g. if machine was suspended). | |
| 470 return base::FeatureList::GetInstance() != nullptr && | |
| 471 base::GetFieldTrialParamByFeatureAsBool(kEnableReporting, "periodic", | |
| 472 false); | |
| 448 } | 473 } |
| 449 | 474 |
| 450 void CallStackProfileMetricsProvider::OnRecordingEnabled() { | 475 void CallStackProfileMetricsProvider::OnRecordingEnabled() { |
| 451 PendingProfiles::GetInstance()->SetCollectionEnabled(true); | 476 PendingProfiles::GetInstance()->SetCollectionEnabled(true); |
| 452 } | 477 } |
| 453 | 478 |
| 454 void CallStackProfileMetricsProvider::OnRecordingDisabled() { | 479 void CallStackProfileMetricsProvider::OnRecordingDisabled() { |
| 455 PendingProfiles::GetInstance()->SetCollectionEnabled(false); | 480 PendingProfiles::GetInstance()->SetCollectionEnabled(false); |
| 456 } | 481 } |
| 457 | 482 |
| 458 void CallStackProfileMetricsProvider::ProvideGeneralMetrics( | 483 void CallStackProfileMetricsProvider::ProvideGeneralMetrics( |
| 459 ChromeUserMetricsExtension* uma_proto) { | 484 ChromeUserMetricsExtension* uma_proto) { |
| 460 std::vector<ProfilesState> pending_profiles; | 485 std::vector<ProfilesState> pending_profiles; |
| 461 PendingProfiles::GetInstance()->Swap(&pending_profiles); | 486 PendingProfiles::GetInstance()->Swap(&pending_profiles); |
| 462 | 487 |
| 463 DCHECK(IsReportingEnabledByFieldTrial() || pending_profiles.empty()); | 488 DCHECK(IsReportingEnabledByFieldTrial() || pending_profiles.empty()); |
| 464 | 489 |
| 490 // TODO(asvitkine): For post-startup periodic samples, this is currently | |
| 491 // wasteful as each sample is reported in its own profile. We should attempt | |
| 492 // to merge profiles to save bandwidth. | |
| 465 for (const ProfilesState& profiles_state : pending_profiles) { | 493 for (const ProfilesState& profiles_state : pending_profiles) { |
| 466 for (const StackSamplingProfiler::CallStackProfile& profile : | 494 for (const StackSamplingProfiler::CallStackProfile& profile : |
| 467 profiles_state.profiles) { | 495 profiles_state.profiles) { |
| 468 SampledProfile* sampled_profile = uma_proto->add_sampled_profile(); | 496 SampledProfile* sampled_profile = uma_proto->add_sampled_profile(); |
| 469 sampled_profile->set_process(ToExecutionContextProcess( | 497 sampled_profile->set_process( |
| 470 profiles_state.params.process)); | 498 ToExecutionContextProcess(profiles_state.params.process)); |
| 471 sampled_profile->set_thread(ToExecutionContextThread( | 499 sampled_profile->set_thread( |
| 472 profiles_state.params.thread)); | 500 ToExecutionContextThread(profiles_state.params.thread)); |
| 473 sampled_profile->set_trigger_event(ToSampledProfileTriggerEvent( | 501 sampled_profile->set_trigger_event( |
| 474 profiles_state.params.trigger)); | 502 ToSampledProfileTriggerEvent(profiles_state.params.trigger)); |
| 503 | |
| 504 int64_t sampling_period_ms = profile.sampling_period.InMilliseconds(); | |
| 505 if (sampled_profile->trigger_event() == | |
| 506 SampledProfile::PERIODIC_COLLECTION) { | |
| 507 sampling_period_ms += kPeriodicSamplingInterval.InMilliseconds(); | |
|
Mike Wittman
2017/07/07 21:01:11
Sorry, overlooked this change earlier. We actually
Alexei Svitkine (slow)
2017/07/10 22:05:37
Done. Moved the logic to CollectProfilesIfCollecti
| |
| 508 } | |
| 475 CopyProfileToProto(profile, profiles_state.params.ordering_spec, | 509 CopyProfileToProto(profile, profiles_state.params.ordering_spec, |
| 510 sampling_period_ms, | |
| 476 sampled_profile->mutable_call_stack_profile()); | 511 sampled_profile->mutable_call_stack_profile()); |
| 477 } | 512 } |
| 478 } | 513 } |
| 479 } | 514 } |
| 480 | 515 |
| 481 // static | 516 // static |
| 482 void CallStackProfileMetricsProvider::ResetStaticStateForTesting() { | 517 void CallStackProfileMetricsProvider::ResetStaticStateForTesting() { |
| 483 PendingProfiles::GetInstance()->ResetToDefaultStateForTesting(); | 518 PendingProfiles::GetInstance()->ResetToDefaultStateForTesting(); |
| 484 } | 519 } |
| 485 | 520 |
| 486 // static | 521 // static |
| 487 bool CallStackProfileMetricsProvider::IsReportingEnabledByFieldTrial() { | 522 bool CallStackProfileMetricsProvider::IsReportingEnabledByFieldTrial() { |
| 488 const std::string group_name = base::FieldTrialList::FindFullName( | 523 return base::FeatureList::IsEnabled(kEnableReporting); |
| 489 CallStackProfileMetricsProvider::kFieldTrialName); | |
| 490 return group_name == | |
| 491 CallStackProfileMetricsProvider::kReportProfilesGroupName; | |
| 492 } | 524 } |
| 493 | 525 |
| 494 } // namespace metrics | 526 } // namespace metrics |
| OLD | NEW |