Chromium Code Reviews| Index: chrome/browser/metrics/call_stack_profile_metrics_provider.cc |
| diff --git a/chrome/browser/metrics/call_stack_profile_metrics_provider.cc b/chrome/browser/metrics/call_stack_profile_metrics_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3071476344f79c907f18829e0f486fcd49556c47 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/call_stack_profile_metrics_provider.cc |
| @@ -0,0 +1,81 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "call_stack_profile_metrics_provider.h" |
| + |
| +#include "base/md5.h" |
| +#include "base/profiler/stack_sampling_profiler.h" |
| +#include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
| + |
| +using base::StackSamplingProfiler; |
| +using metrics::CallStackProfile; |
| +using metrics::CallStackEntry; |
|
Ilya Sherman
2015/03/10 01:44:46
nit: Please alphabetize
Mike Wittman
2015/03/16 23:55:14
Done.
|
| +using metrics::ChromeUserMetricsExtension; |
| +using metrics::ModuleIdentifier; |
| +using metrics::SampledProfile; |
| + |
| +namespace { |
|
Ilya Sherman
2015/03/10 01:44:46
nit: Please include a blank line after this one.
Mike Wittman
2015/03/16 23:55:15
Done.
|
| +// 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(); |
| + base::MD5Digest md5; |
| + base::MD5Sum(&basename[0], basename.size() * sizeof(base::FilePath::CharType), |
| + &md5); |
| + return *reinterpret_cast<uint64*>(&md5.a[0]); |
| +} |
| + |
| +void CopySampleToProto(const StackSamplingProfiler::Sample& sample, |
| + CallStackProfile::Sample* proto_sample) { |
|
Ilya Sherman
2015/03/10 01:44:46
nit: Please document all functions, including loca
Mike Wittman
2015/03/16 23:55:15
Done.
|
| + for (const StackSamplingProfiler::Frame& frame : sample) { |
| + CallStackEntry* call_stack_entry = proto_sample->add_entries(); |
| + call_stack_entry->set_address(frame.ip_offset); |
| + call_stack_entry->set_module_id_index(frame.module_index); |
| + } |
| +} |
| + |
| +void CopyProfileToProto( |
| + const StackSamplingProfiler::Profile& profile, |
| + metrics::CallStackProfile* proto_profile) { |
| + if (profile.samples.empty()) |
| + return; |
| + |
| + CallStackProfile::Sample* current_sample_proto = nullptr; |
| + for (auto it = profile.samples.begin(); it != profile.samples.end(); ++it) { |
| + if (!current_sample_proto || *it != *(it - 1)) { |
|
Ilya Sherman
2015/03/10 01:44:46
Hmm, this seems like a surprising design. Why doe
Mike Wittman
2015/03/16 23:55:15
After further discussion, we want to support two d
|
| + current_sample_proto = proto_profile->add_samples(); |
| + CopySampleToProto(profile.samples.front(), current_sample_proto); |
| + current_sample_proto->set_count(1); |
| + } else { |
| + current_sample_proto->set_count(current_sample_proto->count() + 1); |
| + } |
| + } |
| + |
| + for (const StackSamplingProfiler::Module& module : profile.modules) { |
| + ModuleIdentifier* module_id = proto_profile->add_module_ids(); |
| + module_id->set_build_id(module.id); |
| + module_id->set_name_md5_prefix(HashModuleFilename(module.filename)); |
| + } |
| + |
| + proto_profile->set_profile_duration_ms( |
| + profile.profile_duration.InMilliseconds()); |
| + proto_profile->set_sampling_period_ms( |
| + profile.sampling_period.InMilliseconds()); |
| +} |
| +} // namespace |
| + |
| +CallStackProfileMetricsProvider::CallStackProfileMetricsProvider() {} |
| + |
| +CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() {} |
| + |
| +void CallStackProfileMetricsProvider::ProvideGeneralMetrics( |
| + metrics::ChromeUserMetricsExtension* uma_proto) { |
| + std::vector<StackSamplingProfiler::Profile> profiles; |
| + StackSamplingProfiler::GetPendingProfiles(&profiles); |
| + |
| + for (StackSamplingProfiler::Profile profile : profiles) { |
| + CallStackProfile* call_stack_profile = |
| + uma_proto->add_sampled_profile()->mutable_call_stack_profile(); |
| + CopyProfileToProto(profile, call_stack_profile); |
| + } |
| +} |