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

Side by Side Diff: chrome/browser/metrics/call_stack_profile_metrics_provider.cc

Issue 981143006: Metrics provider for statistical stack profiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: address comments Created 5 years, 9 months 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "call_stack_profile_metrics_provider.h"
6
7 #include <map>
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "base/md5.h"
12 #include "base/profiler/stack_sampling_profiler.h"
13 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
14
15 using base::StackSamplingProfiler;
16 using metrics::CallStackEntry;
Alexei Svitkine (slow) 2015/03/17 23:10:10 If this file is moved to metrics component, all th
Mike Wittman 2015/03/18 01:48:35 Done.
17 using metrics::CallStackProfile;
18 using metrics::ChromeUserMetricsExtension;
19 using metrics::ModuleIdentifier;
20 using metrics::SampledProfile;
21
22 namespace {
23
24 // The protobuf expects the MD5 checksum prefix of the module name.
25 uint64 HashModuleFilename(const base::FilePath& filename) {
Alexei Svitkine (slow) 2015/03/17 23:10:10 Can you just use HashMetricName()? https://code.g
Mike Wittman 2015/03/18 01:48:35 Done.
26 const base::FilePath::StringType basename = filename.BaseName().value();
27 base::MD5Digest md5;
28 base::MD5Sum(&basename[0], basename.size() * sizeof(base::FilePath::CharType),
29 &md5);
30 return *reinterpret_cast<uint64*>(&md5.a[0]);
Ilya Sherman 2015/03/17 04:54:35 Are you sure this is endianness-safe? Also, IIRC,
Mike Wittman 2015/03/18 00:54:27 Looks like I can reuse the existing code as-is. Al
31 }
32
33 // Transcode |sample| into |proto_sample|, using base addresses in |modules| to
34 // compute module IP offsets.
Ilya Sherman 2015/03/17 04:54:34 Optional nit: I assume that "IP" is "instruction p
Mike Wittman 2015/03/18 00:54:27 Done.
35 void CopySampleToProto(
36 const StackSamplingProfiler::Sample& sample,
37 const std::vector<StackSamplingProfiler::Module>& modules,
38 CallStackProfile::Sample* proto_sample) {
39 for (const StackSamplingProfiler::Frame& frame : sample) {
40 CallStackEntry* call_stack_entry = proto_sample->add_entry();
41 int64 module_offset =
42 reinterpret_cast<const char*>(frame.instruction_pointer) -
43 reinterpret_cast<const char*>(modules[frame.module_index].base_address);
44 DCHECK_GE(module_offset, 0);
45 call_stack_entry->set_address(static_cast<uint64>(module_offset));
46 call_stack_entry->set_module_id_index(frame.module_index);
47 }
48 }
49
50 // Transcode |profile| into |proto_profile|.
51 void CopyProfileToProto(
52 const StackSamplingProfiler::Profile& profile,
53 metrics::CallStackProfile* proto_profile) {
54 if (profile.samples.empty())
55 return;
56
57 if (profile.preserve_sample_ordering) {
Ilya Sherman 2015/03/17 04:54:35 It's still not clear to me, given that all of the
Mike Wittman 2015/03/18 00:54:27 It could be computed in base, but it's not clear y
58 // Collapse only consecutive repeated samples together.
59 CallStackProfile::Sample* current_sample_proto = nullptr;
60 for (auto it = profile.samples.begin(); it != profile.samples.end(); ++it) {
61 if (!current_sample_proto || *it != *(it - 1)) {
62 current_sample_proto = proto_profile->add_sample();
63 CopySampleToProto(*it, profile.modules, current_sample_proto);
64 current_sample_proto->set_count(1);
65 } else {
66 current_sample_proto->set_count(current_sample_proto->count() + 1);
67 }
68 }
69 } else {
70 // Collapse all repeated samples together.
71 std::map<StackSamplingProfiler::Sample, int> sample_index;
72 for (auto it = profile.samples.begin(); it != profile.samples.end(); ++it) {
73 auto loc = sample_index.find(*it);
Ilya Sherman 2015/03/17 04:54:34 nit: Please spell out "location"
Mike Wittman 2015/03/18 00:54:27 Done.
74 if (loc == sample_index.end()) {
75 CallStackProfile::Sample* sample_proto = proto_profile->add_sample();
76 CopySampleToProto(*it, profile.modules, sample_proto);
77 sample_proto->set_count(1);
78 sample_index.insert(
79 std::make_pair(
80 *it, static_cast<int>(proto_profile->sample().size()) - 1));
81 } else {
82 CallStackProfile::Sample* sample_proto =
83 proto_profile->mutable_sample()->Mutable(loc->second);
84 sample_proto->set_count(sample_proto->count() + 1);
85 }
86 }
87 }
88
89 for (const StackSamplingProfiler::Module& module : profile.modules) {
90 ModuleIdentifier* module_id = proto_profile->add_module_id();
91 module_id->set_build_id(module.id);
92 module_id->set_name_md5_prefix(HashModuleFilename(module.filename));
93 }
94
95 proto_profile->set_profile_duration_ms(
96 profile.profile_duration.InMilliseconds());
97 proto_profile->set_sampling_period_ms(
98 profile.sampling_period.InMilliseconds());
99 }
100 } // namespace
101
102 CallStackProfileMetricsProvider::CallStackProfileMetricsProvider() {}
103
104 CallStackProfileMetricsProvider::~CallStackProfileMetricsProvider() {}
105
106 void CallStackProfileMetricsProvider::ProvideGeneralMetrics(
107 metrics::ChromeUserMetricsExtension* uma_proto) {
108 std::vector<StackSamplingProfiler::Profile> profiles;
109 if (!source_profiles_for_test_.empty())
110 profiles.swap(source_profiles_for_test_);
111 else
112 StackSamplingProfiler::GetPendingProfiles(&profiles);
113
114 for (StackSamplingProfiler::Profile profile : profiles) {
Alexei Svitkine (slow) 2015/03/17 23:10:09 Nit: const ref
Mike Wittman 2015/03/18 01:48:35 Done.
115 CallStackProfile* call_stack_profile =
116 uma_proto->add_sampled_profile()->mutable_call_stack_profile();
117 CopyProfileToProto(profile, call_stack_profile);
118 }
119 }
120
121 void CallStackProfileMetricsProvider::SetSourceProfilesForTest(
122 const std::vector<StackSamplingProfiler::Profile>& profiles) {
123 source_profiles_for_test_ = profiles;
124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698