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

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

Issue 2444143002: Add process lifetime annotations to stack samples. (Closed)
Patch Set: switch from #define generator array to builder class Created 4 years 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 <utility> 10 #include <utility>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/metrics/field_trial.h" 13 #include "base/metrics/field_trial.h"
14 #include "base/profiler/stack_sampling_profiler.h" 14 #include "base/profiler/stack_sampling_profiler.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "components/metrics/call_stack_profile_params.h" 18 #include "components/metrics/call_stack_profile_params.h"
19 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" 19 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
20 #include "components/variations/entropy_provider.h" 20 #include "components/variations/entropy_provider.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 using base::StackSamplingProfiler; 23 using base::StackSamplingProfiler;
24 using Frame = StackSamplingProfiler::Frame; 24 using Frame = StackSamplingProfiler::Frame;
25 using Module = StackSamplingProfiler::Module; 25 using Module = StackSamplingProfiler::Module;
26 using Profile = StackSamplingProfiler::CallStackProfile; 26 using Profile = StackSamplingProfiler::CallStackProfile;
27 using Profiles = StackSamplingProfiler::CallStackProfiles; 27 using Profiles = StackSamplingProfiler::CallStackProfiles;
28 using Sample = StackSamplingProfiler::Sample; 28 using Sample = StackSamplingProfiler::Sample;
29 29
30 namespace {
31
32 struct ExpectedProtoModule {
33 const char* build_id;
34 uint64_t name_md5;
35 uint64_t base_address;
36 };
37
38 struct ExpectedProtoEntry {
39 int32_t module_index;
40 uint64_t address;
41 };
42
43 struct ExpectedProtoSample {
44 uint32_t process_phases; // Bit-field of expected phases.
45 const ExpectedProtoEntry* entries;
46 int entry_count;
47 int64_t entry_repeats;
48 };
49
50 struct ExpectedProtoProfile {
51 int32_t duration_ms;
52 int32_t period_ms;
53 const ExpectedProtoModule* modules;
54 int module_count;
55 const ExpectedProtoSample* samples;
56 int sample_count;
57 };
58
59 class ProfilesGenerator {
60 public:
61 using Generator = uintptr_t;
Mike Wittman 2016/11/23 00:28:40 This and the following enum can be made private.
bcwhite 2016/11/23 14:45:53 Done.
62
63 enum : Generator {
64 kEndOfGenerator,
65 kAddPhase,
66 kNewProfile,
67 kNewSample,
68 kAddFrame,
69 kDefineModule,
70 };
71
72 ProfilesGenerator(){};
73 ~ProfilesGenerator(){};
74
75 ProfilesGenerator& AddPhase(int phase) {
76 generator_.push_back(kAddPhase);
77 generator_.push_back(static_cast<Generator>(phase));
78 return *this;
79 }
80 ProfilesGenerator& NewProfile(int duration_ms, int interval_ms) {
81 generator_.push_back(kNewProfile);
82 generator_.push_back(static_cast<Generator>(duration_ms));
83 generator_.push_back(static_cast<Generator>(interval_ms));
84 return *this;
85 }
86 ProfilesGenerator& NewSample() {
87 generator_.push_back(kNewSample);
88 return *this;
89 }
90 ProfilesGenerator& AddFrame(size_t module, uintptr_t offset) {
91 generator_.push_back(kAddFrame);
92 generator_.push_back(static_cast<Generator>(module));
93 generator_.push_back(static_cast<Generator>(offset));
94 return *this;
95 }
96 ProfilesGenerator& DefineModule(const char* name,
97 const base::FilePath& path,
98 uintptr_t base) {
99 generator_.push_back(kDefineModule);
100 generator_.push_back(reinterpret_cast<Generator>(name));
101 generator_.push_back(reinterpret_cast<Generator>(&path));
102 generator_.push_back(static_cast<Generator>(base));
103 return *this;
104 }
105 ProfilesGenerator& End() {
Mike Wittman 2016/11/23 00:28:40 I think this function can be removed (based on com
bcwhite 2016/11/23 14:45:53 Done.
106 generator_.push_back(kEndOfGenerator);
107 return *this;
108 }
109
110 void GenerateProfiles(Profiles* profiles);
Mike Wittman 2016/11/23 00:28:40 If we change this to Profiles Build() const; then
bcwhite 2016/11/23 14:45:53 I like it! Except... oh. <sigh> I guess I shou
Mike Wittman 2016/11/23 16:50:54 Nice, this is much cleaner.
111
112 private:
113 std::vector<uintptr_t> generator_;
114
115 DISALLOW_COPY_AND_ASSIGN(ProfilesGenerator);
116 };
117
118 void ProfilesGenerator::GenerateProfiles(Profiles* profiles) {
119 uint32_t process_phases = 0;
120 Profile* profile = nullptr;
121
122 const ProfilesGenerator::Generator* generator = &generator_[0];
123 while (true) {
Mike Wittman 2016/11/23 00:28:40 We should be able to eliminate kEndOfGenerator now
bcwhite 2016/11/23 14:45:53 Done.
124 switch (*generator++) {
125 case kEndOfGenerator:
126 return;
127 case kAddPhase:
128 process_phases |= 1 << *generator++;
129 break;
130 case kNewProfile:
131 profiles->push_back(Profile());
132 profile = &profiles->back();
133 profile->profile_duration =
134 base::TimeDelta::FromMilliseconds(*generator++);
135 profile->sampling_period =
136 base::TimeDelta::FromMilliseconds(*generator++);
137 break;
138 case kNewSample:
139 profile->samples.push_back(Sample());
140 profile->samples.back().process_phases = process_phases;
141 break;
142 case kAddFrame: {
143 size_t index = static_cast<size_t>(*generator++);
144 uintptr_t address = static_cast<uintptr_t>(*generator++);
145 profile->samples.back().frames.push_back(Frame(address, index));
146 } break;
147 case kDefineModule: {
148 const char* name =
149 reinterpret_cast<const char*>(static_cast<uintptr_t>(*generator++));
150 const base::FilePath* path = reinterpret_cast<const base::FilePath*>(
151 static_cast<uintptr_t>(*generator++));
152 uintptr_t base = static_cast<uintptr_t>(*generator++);
153 profile->modules.push_back(Module(base, name, *path));
154 } break;
155 default:
156 NOTREACHED();
157 };
158 }
159 }
160
161 } // namespace
162
30 namespace metrics { 163 namespace metrics {
31 164
32 // This test fixture enables the field trial that 165 // This test fixture enables the field trial that
33 // CallStackProfileMetricsProvider depends on to report profiles. 166 // CallStackProfileMetricsProvider depends on to report profiles.
34 class CallStackProfileMetricsProviderTest : public testing::Test { 167 class CallStackProfileMetricsProviderTest : public testing::Test {
35 public: 168 public:
36 CallStackProfileMetricsProviderTest() 169 CallStackProfileMetricsProviderTest()
37 : field_trial_list_(nullptr) { 170 : field_trial_list_(nullptr) {
38 base::FieldTrialList::CreateFieldTrial( 171 base::FieldTrialList::CreateFieldTrial(
39 TestState::kFieldTrialName, 172 TestState::kFieldTrialName,
40 TestState::kReportProfilesGroupName); 173 TestState::kReportProfilesGroupName);
41 TestState::ResetStaticStateForTesting(); 174 TestState::ResetStaticStateForTesting();
42 } 175 }
43 176
44 ~CallStackProfileMetricsProviderTest() override {} 177 ~CallStackProfileMetricsProviderTest() override {}
45 178
46 // Utility function to append profiles to the metrics provider. 179 // Utility function to append profiles to the metrics provider.
47 void AppendProfiles(const CallStackProfileParams& params, Profiles profiles) { 180 void AppendProfiles(const CallStackProfileParams& params, Profiles profiles) {
48 CallStackProfileMetricsProvider::GetProfilerCallback(params).Run( 181 CallStackProfileMetricsProvider::GetProfilerCallback(params).Run(
49 std::move(profiles)); 182 std::move(profiles));
50 } 183 }
51 184
185 void VerifyProfileProto(const ExpectedProtoProfile& expected,
186 const SampledProfile& proto);
187
52 private: 188 private:
53 // Exposes field trial/group names from the CallStackProfileMetricsProvider. 189 // Exposes field trial/group names from the CallStackProfileMetricsProvider.
54 class TestState : public CallStackProfileMetricsProvider { 190 class TestState : public CallStackProfileMetricsProvider {
55 public: 191 public:
56 using CallStackProfileMetricsProvider::kFieldTrialName; 192 using CallStackProfileMetricsProvider::kFieldTrialName;
57 using CallStackProfileMetricsProvider::kReportProfilesGroupName; 193 using CallStackProfileMetricsProvider::kReportProfilesGroupName;
58 using CallStackProfileMetricsProvider::ResetStaticStateForTesting; 194 using CallStackProfileMetricsProvider::ResetStaticStateForTesting;
59 }; 195 };
60 196
61 base::FieldTrialList field_trial_list_; 197 base::FieldTrialList field_trial_list_;
62 198
63 DISALLOW_COPY_AND_ASSIGN(CallStackProfileMetricsProviderTest); 199 DISALLOW_COPY_AND_ASSIGN(CallStackProfileMetricsProviderTest);
64 }; 200 };
65 201
202 void CallStackProfileMetricsProviderTest::VerifyProfileProto(
203 const ExpectedProtoProfile& expected,
204 const SampledProfile& proto) {
205 ASSERT_TRUE(proto.has_call_stack_profile());
206 const CallStackProfile& stack = proto.call_stack_profile();
207
208 ASSERT_TRUE(stack.has_profile_duration_ms());
209 EXPECT_EQ(expected.duration_ms, stack.profile_duration_ms());
210 ASSERT_TRUE(stack.has_sampling_period_ms());
211 EXPECT_EQ(expected.period_ms, stack.sampling_period_ms());
212
213 ASSERT_EQ(expected.module_count, stack.module_id().size());
214 for (int m = 0; m < expected.module_count; ++m) {
215 SCOPED_TRACE("module " + base::IntToString(m));
216 const CallStackProfile::ModuleIdentifier& module_id =
217 stack.module_id().Get(m);
218 ASSERT_TRUE(module_id.has_build_id());
219 EXPECT_EQ(expected.modules[m].build_id, module_id.build_id());
220 ASSERT_TRUE(module_id.has_name_md5_prefix());
221 EXPECT_EQ(expected.modules[m].name_md5, module_id.name_md5_prefix());
222 }
223
224 ASSERT_EQ(expected.sample_count, stack.sample().size());
225 for (int s = 0; s < expected.sample_count; ++s) {
226 SCOPED_TRACE("sample " + base::IntToString(s));
227 const CallStackProfile::Sample& proto_sample = stack.sample().Get(s);
228
229 uint32_t process_phases = 0;
230 for (int i = 0; i < proto_sample.process_phase().size(); ++i)
231 process_phases |= 1U << proto_sample.process_phase().Get(i);
232 EXPECT_EQ(expected.samples[s].process_phases, process_phases);
233
234 ASSERT_EQ(expected.samples[s].entry_count, proto_sample.entry().size());
235 ASSERT_TRUE(proto_sample.has_count());
236 EXPECT_EQ(expected.samples[s].entry_repeats, proto_sample.count());
237 for (int e = 0; e < expected.samples[s].entry_count; ++e) {
238 SCOPED_TRACE("entry " + base::SizeTToString(e));
239 const CallStackProfile::Entry& entry = proto_sample.entry().Get(e);
240 if (expected.samples[s].entries[e].module_index >= 0) {
241 ASSERT_TRUE(entry.has_module_id_index());
242 EXPECT_EQ(expected.samples[s].entries[e].module_index,
243 entry.module_id_index());
244 ASSERT_TRUE(entry.has_address());
245 EXPECT_EQ(expected.samples[s].entries[e].address, entry.address());
246 } else {
247 EXPECT_FALSE(entry.has_module_id_index());
248 EXPECT_FALSE(entry.has_address());
249 }
250 }
251 }
252 }
253
66 // Checks that all properties from multiple profiles are filled as expected. 254 // Checks that all properties from multiple profiles are filled as expected.
67 TEST_F(CallStackProfileMetricsProviderTest, MultipleProfiles) { 255 TEST_F(CallStackProfileMetricsProviderTest, MultipleProfiles) {
68 const uintptr_t module1_base_address = 0x1000; 256 const uintptr_t moduleA_base_address = 0x1000;
69 const uintptr_t module2_base_address = 0x2000; 257 const uintptr_t moduleB_base_address = 0x2000;
70 const uintptr_t module3_base_address = 0x3000; 258 const uintptr_t moduleC_base_address = 0x3000;
71 259 const char* moduleA_name = "ABCD";
72 const Module profile_modules[][2] = { 260 const char* moduleB_name = "EFGH";
73 { 261 const char* moduleC_name = "MNOP";
74 Module(
75 module1_base_address,
76 "ABCD",
77 #if defined(OS_WIN)
78 base::FilePath(L"c:\\some\\path\\to\\chrome.exe")
79 #else
80 base::FilePath("/some/path/to/chrome")
81 #endif
82 ),
83 Module(
84 module2_base_address,
85 "EFGH",
86 #if defined(OS_WIN)
87 base::FilePath(L"c:\\some\\path\\to\\third_party.dll")
88 #else
89 base::FilePath("/some/path/to/third_party.so")
90 #endif
91 ),
92 },
93 {
94 Module(
95 module3_base_address,
96 "MNOP",
97 #if defined(OS_WIN)
98 base::FilePath(L"c:\\some\\path\\to\\third_party2.dll")
99 #else
100 base::FilePath("/some/path/to/third_party2.so")
101 #endif
102 ),
103 Module( // Repeated from the first profile.
104 module1_base_address,
105 "ABCD",
106 #if defined(OS_WIN)
107 base::FilePath(L"c:\\some\\path\\to\\chrome.exe")
108 #else
109 base::FilePath("/some/path/to/chrome")
110 #endif
111 )
112 }
113 };
114 262
115 // Values for Windows generated with: 263 // Values for Windows generated with:
116 // perl -MDigest::MD5=md5 -MEncode=encode 264 // perl -MDigest::MD5=md5 -MEncode=encode
117 // -e 'for(@ARGV){printf "%x\n", unpack "Q>", md5 encode "UTF-16LE", $_}' 265 // -e 'for(@ARGV){printf "%x\n", unpack "Q>", md5 encode "UTF-16LE", $_}'
118 // chrome.exe third_party.dll third_party2.dll 266 // chrome.exe third_party.dll third_party2.dll
119 // 267 //
120 // Values for Linux generated with: 268 // Values for Linux generated with:
121 // perl -MDigest::MD5=md5 269 // perl -MDigest::MD5=md5
122 // -e 'for(@ARGV){printf "%x\n", unpack "Q>", md5 $_}' 270 // -e 'for(@ARGV){printf "%x\n", unpack "Q>", md5 $_}'
123 // chrome third_party.so third_party2.so 271 // chrome third_party.so third_party2.so
124 const uint64_t profile_expected_name_md5_prefixes[][2] = {
125 {
126 #if defined(OS_WIN) 272 #if defined(OS_WIN)
127 0x46c3e4166659ac02ULL, 0x7e2b8bfddeae1abaULL 273 uint64_t moduleA_md5 = 0x46C3E4166659AC02ULL;
274 uint64_t moduleB_md5 = 0x7E2B8BFDDEAE1ABAULL;
275 uint64_t moduleC_md5 = 0x87B66F4573A4D5CAULL;
276 base::FilePath moduleA_path(L"c:\\some\\path\\to\\chrome.exe");
277 base::FilePath moduleB_path(L"c:\\some\\path\\to\\third_party.dll");
278 base::FilePath moduleC_path(L"c:\\some\\path\\to\\third_party2.dll");
128 #else 279 #else
129 0x554838a8451ac36cUL, 0x843661148659c9f8UL 280 uint64_t moduleA_md5 = 0x554838A8451AC36CULL;
281 uint64_t moduleB_md5 = 0x843661148659C9F8ULL;
282 uint64_t moduleC_md5 = 0xB4647E539FA6EC9EULL;
283 base::FilePath moduleA_path("/some/path/to/chrome");
284 base::FilePath moduleB_path("/some/path/to/third_party.so");
285 base::FilePath moduleC_path("/some/path/to/third_party2.so");
130 #endif 286 #endif
131 },
132 {
133 #if defined(OS_WIN)
134 0x87b66f4573a4d5caULL, 0x46c3e4166659ac02ULL
135 #else
136 0xb4647e539fa6ec9eUL, 0x554838a8451ac36cUL
137 #endif
138 }};
139 287
140 // Represents two stack samples for each of two profiles, where each stack 288 // Represents two stack samples for each of two profiles, where each stack
141 // contains three frames. Each frame contains an instruction pointer and a 289 // contains three frames. Each frame contains an instruction pointer and a
142 // module index corresponding to the module for the profile in 290 // module index corresponding to the module for the profile in
143 // profile_modules. 291 // profile_modules.
144 // 292 //
145 // So, the first stack sample below has its top frame in module 0 at an offset 293 // So, the first stack sample below has its top frame in module 0 at an offset
146 // of 0x10 from the module's base address, the next-to-top frame in module 1 294 // of 0x10 from the module's base address, the next-to-top frame in module 1
147 // at an offset of 0x20 from the module's base address, and the bottom frame 295 // at an offset of 0x20 from the module's base address, and the bottom frame
148 // in module 0 at an offset of 0x30 from the module's base address 296 // in module 0 at an offset of 0x30 from the module's base address
149 const Frame profile_sample_frames[][2][3] = { 297 ProfilesGenerator test_profiles_generator;
150 { 298 test_profiles_generator
299 .NewProfile(100, 10)
300 .DefineModule(moduleA_name, moduleA_path, moduleA_base_address)
301 .DefineModule(moduleB_name, moduleB_path, moduleB_base_address)
302
303 .NewSample()
304 .AddFrame(0, moduleA_base_address + 0x10)
305 .AddFrame(1, moduleB_base_address + 0x20)
306 .AddFrame(0, moduleA_base_address + 0x30)
307 .NewSample()
308 .AddFrame(1, moduleB_base_address + 0x10)
309 .AddFrame(0, moduleA_base_address + 0x20)
310 .AddFrame(1, moduleB_base_address + 0x30)
311
312 .NewProfile(200, 20)
313 .DefineModule(moduleC_name, moduleC_path, moduleC_base_address)
314 .DefineModule(moduleA_name, moduleA_path, moduleA_base_address)
315
316 .NewSample()
317 .AddFrame(0, moduleC_base_address + 0x10)
318 .AddFrame(1, moduleA_base_address + 0x20)
319 .AddFrame(0, moduleC_base_address + 0x30)
320 .NewSample()
321 .AddFrame(1, moduleA_base_address + 0x10)
322 .AddFrame(0, moduleC_base_address + 0x20)
323 .AddFrame(1, moduleA_base_address + 0x30)
324
325 .End();
326
327 const ExpectedProtoModule expected_proto_modules1[] = {
328 { moduleA_name, moduleA_md5, moduleA_base_address },
329 { moduleB_name, moduleB_md5, moduleB_base_address }
330 };
331
332 const ExpectedProtoEntry expected_proto_entries11[] = {
333 { 0, 0x10 },
334 { 1, 0x20 },
335 { 0, 0x30 },
336 };
337 const ExpectedProtoEntry expected_proto_entries12[] = {
338 { 1, 0x10 },
339 { 0, 0x20 },
340 { 1, 0x30 },
341 };
342 const ExpectedProtoSample expected_proto_samples1[] = {
151 { 343 {
152 Frame(module1_base_address + 0x10, 0), 344 0, expected_proto_entries11, arraysize(expected_proto_entries11), 1,
153 Frame(module2_base_address + 0x20, 1),
154 Frame(module1_base_address + 0x30, 0)
155 }, 345 },
156 { 346 {
157 Frame(module2_base_address + 0x10, 1), 347 0, expected_proto_entries12, arraysize(expected_proto_entries12), 1,
158 Frame(module1_base_address + 0x20, 0), 348 },
159 Frame(module2_base_address + 0x30, 1) 349 };
160 } 350
161 }, 351 const ExpectedProtoModule expected_proto_modules2[] = {
162 { 352 { moduleC_name, moduleC_md5, moduleC_base_address },
353 { moduleA_name, moduleA_md5, moduleA_base_address }
354 };
355
356 const ExpectedProtoEntry expected_proto_entries21[] = {
357 { 0, 0x10 },
358 { 1, 0x20 },
359 { 0, 0x30 },
360 };
361 const ExpectedProtoEntry expected_proto_entries22[] = {
362 { 1, 0x10 },
363 { 0, 0x20 },
364 { 1, 0x30 },
365 };
366 const ExpectedProtoSample expected_proto_samples2[] = {
163 { 367 {
164 Frame(module3_base_address + 0x10, 0), 368 0, expected_proto_entries11, arraysize(expected_proto_entries21), 1,
165 Frame(module1_base_address + 0x20, 1),
166 Frame(module3_base_address + 0x30, 0)
167 }, 369 },
168 { 370 {
169 Frame(module1_base_address + 0x10, 1), 371 0, expected_proto_entries12, arraysize(expected_proto_entries22), 1,
170 Frame(module3_base_address + 0x20, 0), 372 },
171 Frame(module1_base_address + 0x30, 1)
172 }
173 }
174 }; 373 };
175 374
176 base::TimeDelta profile_durations[2] = { 375 const ExpectedProtoProfile expected_proto_profiles[] = {
177 base::TimeDelta::FromMilliseconds(100), 376 {
178 base::TimeDelta::FromMilliseconds(200) 377 100, 10,
378 expected_proto_modules1, arraysize(expected_proto_modules1),
379 expected_proto_samples1, arraysize(expected_proto_samples1),
380 },
381 {
382 200, 20,
383 expected_proto_modules2, arraysize(expected_proto_modules2),
384 expected_proto_samples2, arraysize(expected_proto_samples2),
385 },
179 }; 386 };
180 387
181 base::TimeDelta profile_sampling_periods[2] = { 388 Profiles profiles;
182 base::TimeDelta::FromMilliseconds(10), 389 test_profiles_generator.GenerateProfiles(&profiles);
183 base::TimeDelta::FromMilliseconds(20) 390 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());
184 };
185
186 std::vector<Profile> profiles;
187 for (size_t i = 0; i < arraysize(profile_sample_frames); ++i) {
188 Profile profile;
189 profile.modules.insert(
190 profile.modules.end(), &profile_modules[i][0],
191 &profile_modules[i][0] + arraysize(profile_modules[i]));
192
193 for (size_t j = 0; j < arraysize(profile_sample_frames[i]); ++j) {
194 profile.samples.push_back(Sample());
195 Sample& sample = profile.samples.back();
196 sample.insert(sample.end(), &profile_sample_frames[i][j][0],
197 &profile_sample_frames[i][j][0] +
198 arraysize(profile_sample_frames[i][j]));
199 }
200
201 profile.profile_duration = profile_durations[i];
202 profile.sampling_period = profile_sampling_periods[i];
203
204 profiles.push_back(std::move(profile));
205 }
206 391
207 CallStackProfileMetricsProvider provider; 392 CallStackProfileMetricsProvider provider;
208 provider.OnRecordingEnabled(); 393 provider.OnRecordingEnabled();
209 AppendProfiles( 394 AppendProfiles(
210 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 395 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
211 CallStackProfileParams::UI_THREAD, 396 CallStackProfileParams::UI_THREAD,
212 CallStackProfileParams::PROCESS_STARTUP, 397 CallStackProfileParams::PROCESS_STARTUP,
213 CallStackProfileParams::MAY_SHUFFLE), 398 CallStackProfileParams::MAY_SHUFFLE),
214 std::move(profiles)); 399 std::move(profiles));
215 ChromeUserMetricsExtension uma_proto; 400 ChromeUserMetricsExtension uma_proto;
216 provider.ProvideGeneralMetrics(&uma_proto); 401 provider.ProvideGeneralMetrics(&uma_proto);
217 402
218 ASSERT_EQ(static_cast<int>(arraysize(profile_sample_frames)), 403 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
219 uma_proto.sampled_profile().size()); 404 uma_proto.sampled_profile().size());
220 for (size_t i = 0; i < arraysize(profile_sample_frames); ++i) { 405 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
221 SCOPED_TRACE("profile " + base::SizeTToString(i)); 406 SCOPED_TRACE("profile " + base::SizeTToString(p));
222 const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(i); 407 VerifyProfileProto(expected_proto_profiles[p],
223 ASSERT_TRUE(sampled_profile.has_call_stack_profile()); 408 uma_proto.sampled_profile().Get(p));
224 const CallStackProfile& call_stack_profile =
225 sampled_profile.call_stack_profile();
226
227 ASSERT_EQ(static_cast<int>(arraysize(profile_sample_frames[i])),
228 call_stack_profile.sample().size());
229 for (size_t j = 0; j < arraysize(profile_sample_frames[i]); ++j) {
230 SCOPED_TRACE("sample " + base::SizeTToString(j));
231 const CallStackProfile::Sample& proto_sample =
232 call_stack_profile.sample().Get(j);
233 ASSERT_EQ(static_cast<int>(arraysize(profile_sample_frames[i][j])),
234 proto_sample.entry().size());
235 ASSERT_TRUE(proto_sample.has_count());
236 EXPECT_EQ(1u, proto_sample.count());
237 for (size_t k = 0; k < arraysize(profile_sample_frames[i][j]); ++k) {
238 SCOPED_TRACE("frame " + base::SizeTToString(k));
239 const CallStackProfile::Entry& entry = proto_sample.entry().Get(k);
240 ASSERT_TRUE(entry.has_address());
241 const char* instruction_pointer = reinterpret_cast<const char*>(
242 profile_sample_frames[i][j][k].instruction_pointer);
243 const char* module_base_address = reinterpret_cast<const char*>(
244 profile_modules[i][profile_sample_frames[i][j][k].module_index]
245 .base_address);
246 EXPECT_EQ(
247 static_cast<uint64_t>(instruction_pointer - module_base_address),
248 entry.address());
249 ASSERT_TRUE(entry.has_module_id_index());
250 EXPECT_EQ(profile_sample_frames[i][j][k].module_index,
251 static_cast<size_t>(entry.module_id_index()));
252 }
253 }
254
255 ASSERT_EQ(static_cast<int>(arraysize(profile_modules[i])),
256 call_stack_profile.module_id().size());
257 for (size_t j = 0; j < arraysize(profile_modules[i]); ++j) {
258 SCOPED_TRACE("module " + base::SizeTToString(j));
259 const CallStackProfile::ModuleIdentifier& module_identifier =
260 call_stack_profile.module_id().Get(j);
261 ASSERT_TRUE(module_identifier.has_build_id());
262 EXPECT_EQ(profile_modules[i][j].id, module_identifier.build_id());
263 ASSERT_TRUE(module_identifier.has_name_md5_prefix());
264 EXPECT_EQ(profile_expected_name_md5_prefixes[i][j],
265 module_identifier.name_md5_prefix());
266 }
267
268 ASSERT_TRUE(call_stack_profile.has_profile_duration_ms());
269 EXPECT_EQ(profile_durations[i].InMilliseconds(),
270 call_stack_profile.profile_duration_ms());
271 ASSERT_TRUE(call_stack_profile.has_sampling_period_ms());
272 EXPECT_EQ(profile_sampling_periods[i].InMilliseconds(),
273 call_stack_profile.sampling_period_ms());
274 ASSERT_TRUE(sampled_profile.has_process());
275 EXPECT_EQ(BROWSER_PROCESS, sampled_profile.process());
276 ASSERT_TRUE(sampled_profile.has_thread());
277 EXPECT_EQ(UI_THREAD, sampled_profile.thread());
278 ASSERT_TRUE(sampled_profile.has_trigger_event());
279 EXPECT_EQ(SampledProfile::PROCESS_STARTUP, sampled_profile.trigger_event());
280 } 409 }
281 } 410 }
282 411
283 // Checks that all duplicate samples are collapsed with 412 // Checks that all duplicate samples are collapsed with
284 // preserve_sample_ordering = false. 413 // preserve_sample_ordering = false.
285 TEST_F(CallStackProfileMetricsProviderTest, RepeatedStacksUnordered) { 414 TEST_F(CallStackProfileMetricsProviderTest, RepeatedStacksUnordered) {
286 const uintptr_t module_base_address = 0x1000; 415 const uintptr_t module_base_address = 0x1000;
416 const char* module_name = "ABCD";
287 417
288 const Module modules[] = {
289 Module(
290 module_base_address,
291 "ABCD",
292 #if defined(OS_WIN) 418 #if defined(OS_WIN)
293 base::FilePath(L"c:\\some\\path\\to\\chrome.exe") 419 uint64_t module_md5 = 0x46C3E4166659AC02ULL;
420 base::FilePath module_path(L"c:\\some\\path\\to\\chrome.exe");
294 #else 421 #else
295 base::FilePath("/some/path/to/chrome") 422 uint64_t module_md5 = 0x554838A8451AC36CULL;
423 base::FilePath module_path("/some/path/to/chrome");
296 #endif 424 #endif
297 ) 425
426 ProfilesGenerator test_profiles_generator;
427 test_profiles_generator
428 .NewProfile(100, 10)
429 .DefineModule(module_name, module_path, module_base_address)
430
431 .AddPhase(0)
432 .NewSample()
433 .AddFrame(0, module_base_address + 0x10)
434 .NewSample()
435 .AddFrame(0, module_base_address + 0x20)
436 .NewSample()
437 .AddFrame(0, module_base_address + 0x10)
438 .NewSample()
439 .AddFrame(0, module_base_address + 0x10)
440
441 .AddPhase(1)
442 .NewSample()
443 .AddFrame(0, module_base_address + 0x10)
444 .NewSample()
445 .AddFrame(0, module_base_address + 0x20)
446 .NewSample()
447 .AddFrame(0, module_base_address + 0x10)
448 .NewSample()
449 .AddFrame(0, module_base_address + 0x10)
450
451 .End();
452
453 const ExpectedProtoModule expected_proto_modules[] = {
454 { module_name, module_md5, module_base_address },
298 }; 455 };
299 456
300 // Duplicate samples in slots 0, 2, and 3. 457 const ExpectedProtoEntry expected_proto_entries[] = {
301 const Frame sample_frames[][1] = { 458 { 0, 0x10 },
302 { Frame(module_base_address + 0x10, 0), }, 459 { 0, 0x20 },
303 { Frame(module_base_address + 0x20, 0), }, 460 };
304 { Frame(module_base_address + 0x10, 0), }, 461 const ExpectedProtoSample expected_proto_samples[] = {
305 { Frame(module_base_address + 0x10, 0) } 462 { 1, &expected_proto_entries[0], 1, 3 },
463 { 0, &expected_proto_entries[1], 1, 1 },
464 { 2, &expected_proto_entries[0], 1, 3 },
465 { 0, &expected_proto_entries[1], 1, 1 },
306 }; 466 };
307 467
308 Profile profile; 468 const ExpectedProtoProfile expected_proto_profiles[] = {
309 profile.modules.insert(profile.modules.end(), &modules[0], 469 {
310 &modules[0] + arraysize(modules)); 470 100, 10,
471 expected_proto_modules, arraysize(expected_proto_modules),
472 expected_proto_samples, arraysize(expected_proto_samples),
473 },
474 };
311 475
312 for (size_t i = 0; i < arraysize(sample_frames); ++i) {
313 profile.samples.push_back(Sample());
314 Sample& sample = profile.samples.back();
315 sample.insert(sample.end(), &sample_frames[i][0],
316 &sample_frames[i][0] + arraysize(sample_frames[i]));
317 }
318
319 profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
320 profile.sampling_period = base::TimeDelta::FromMilliseconds(10);
321 Profiles profiles; 476 Profiles profiles;
322 profiles.push_back(std::move(profile)); 477 test_profiles_generator.GenerateProfiles(&profiles);
478 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());
323 479
324 CallStackProfileMetricsProvider provider; 480 CallStackProfileMetricsProvider provider;
325 provider.OnRecordingEnabled(); 481 provider.OnRecordingEnabled();
326 AppendProfiles( 482 AppendProfiles(
327 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 483 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
328 CallStackProfileParams::UI_THREAD, 484 CallStackProfileParams::UI_THREAD,
329 CallStackProfileParams::PROCESS_STARTUP, 485 CallStackProfileParams::PROCESS_STARTUP,
330 CallStackProfileParams::MAY_SHUFFLE), 486 CallStackProfileParams::MAY_SHUFFLE),
331 std::move(profiles)); 487 std::move(profiles));
332 ChromeUserMetricsExtension uma_proto; 488 ChromeUserMetricsExtension uma_proto;
333 provider.ProvideGeneralMetrics(&uma_proto); 489 provider.ProvideGeneralMetrics(&uma_proto);
334 490
335 ASSERT_EQ(1, uma_proto.sampled_profile().size()); 491 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
336 const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0); 492 uma_proto.sampled_profile().size());
337 ASSERT_TRUE(sampled_profile.has_call_stack_profile()); 493 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
338 const CallStackProfile& call_stack_profile = 494 SCOPED_TRACE("profile " + base::SizeTToString(p));
339 sampled_profile.call_stack_profile(); 495 VerifyProfileProto(expected_proto_profiles[p],
340 496 uma_proto.sampled_profile().Get(p));
341 ASSERT_EQ(2, call_stack_profile.sample().size());
342 for (int i = 0; i < 2; ++i) {
343 SCOPED_TRACE("sample " + base::IntToString(i));
344 const CallStackProfile::Sample& proto_sample =
345 call_stack_profile.sample().Get(i);
346 ASSERT_EQ(static_cast<int>(arraysize(sample_frames[i])),
347 proto_sample.entry().size());
348 ASSERT_TRUE(proto_sample.has_count());
349 EXPECT_EQ(i == 0 ? 3u : 1u, proto_sample.count());
350 for (size_t j = 0; j < arraysize(sample_frames[i]); ++j) {
351 SCOPED_TRACE("frame " + base::SizeTToString(j));
352 const CallStackProfile::Entry& entry = proto_sample.entry().Get(j);
353 ASSERT_TRUE(entry.has_address());
354 const char* instruction_pointer = reinterpret_cast<const char*>(
355 sample_frames[i][j].instruction_pointer);
356 const char* module_base_address = reinterpret_cast<const char*>(
357 modules[sample_frames[i][j].module_index].base_address);
358 EXPECT_EQ(
359 static_cast<uint64_t>(instruction_pointer - module_base_address),
360 entry.address());
361 ASSERT_TRUE(entry.has_module_id_index());
362 EXPECT_EQ(sample_frames[i][j].module_index,
363 static_cast<size_t>(entry.module_id_index()));
364 }
365 } 497 }
366 } 498 }
367 499
368 // Checks that only contiguous duplicate samples are collapsed with 500 // Checks that only contiguous duplicate samples are collapsed with
369 // preserve_sample_ordering = true. 501 // preserve_sample_ordering = true.
370 TEST_F(CallStackProfileMetricsProviderTest, RepeatedStacksOrdered) { 502 TEST_F(CallStackProfileMetricsProviderTest, RepeatedStacksOrdered) {
371 const uintptr_t module_base_address = 0x1000; 503 const uintptr_t module_base_address = 0x1000;
504 const char* module_name = "ABCD";
372 505
373 const Module modules[] = {
374 Module(
375 module_base_address,
376 "ABCD",
377 #if defined(OS_WIN) 506 #if defined(OS_WIN)
378 base::FilePath(L"c:\\some\\path\\to\\chrome.exe") 507 uint64_t module_md5 = 0x46C3E4166659AC02ULL;
508 base::FilePath module_path(L"c:\\some\\path\\to\\chrome.exe");
379 #else 509 #else
380 base::FilePath("/some/path/to/chrome") 510 uint64_t module_md5 = 0x554838A8451AC36CULL;
511 base::FilePath module_path("/some/path/to/chrome");
381 #endif 512 #endif
382 ) 513
514 ProfilesGenerator test_profiles_generator;
515 test_profiles_generator
516 .NewProfile(100, 10)
517 .DefineModule(module_name, module_path, module_base_address)
518
519 .AddPhase(0)
520 .NewSample()
521 .AddFrame(0, module_base_address + 0x10)
522 .NewSample()
523 .AddFrame(0, module_base_address + 0x20)
524 .NewSample()
525 .AddFrame(0, module_base_address + 0x10)
526 .NewSample()
527 .AddFrame(0, module_base_address + 0x10)
528
529 .AddPhase(1)
530 .NewSample()
531 .AddFrame(0, module_base_address + 0x10)
532 .NewSample()
533 .AddFrame(0, module_base_address + 0x20)
534 .NewSample()
535 .AddFrame(0, module_base_address + 0x10)
536 .NewSample()
537 .AddFrame(0, module_base_address + 0x10)
538
539 .End();
540
541 const ExpectedProtoModule expected_proto_modules[] = {
542 { module_name, module_md5, module_base_address },
383 }; 543 };
384 544
385 // Duplicate samples in slots 0, 2, and 3. 545 const ExpectedProtoEntry expected_proto_entries[] = {
386 const Frame sample_frames[][1] = { 546 { 0, 0x10 },
387 { Frame(module_base_address + 0x10, 0), }, 547 { 0, 0x20 },
388 { Frame(module_base_address + 0x20, 0), }, 548 };
389 { Frame(module_base_address + 0x10, 0), }, 549 const ExpectedProtoSample expected_proto_samples[] = {
390 { Frame(module_base_address + 0x10, 0) } 550 { 1, &expected_proto_entries[0], 1, 1 },
551 { 0, &expected_proto_entries[1], 1, 1 },
552 { 0, &expected_proto_entries[0], 1, 2 },
553 { 2, &expected_proto_entries[0], 1, 1 },
554 { 0, &expected_proto_entries[1], 1, 1 },
555 { 0, &expected_proto_entries[0], 1, 2 },
391 }; 556 };
392 557
393 Profile profile; 558 const ExpectedProtoProfile expected_proto_profiles[] = {
394 profile.modules.insert(profile.modules.end(), &modules[0], 559 {
395 &modules[0] + arraysize(modules)); 560 100, 10,
561 expected_proto_modules, arraysize(expected_proto_modules),
562 expected_proto_samples, arraysize(expected_proto_samples),
563 },
564 };
396 565
397 for (size_t i = 0; i < arraysize(sample_frames); ++i) {
398 profile.samples.push_back(Sample());
399 Sample& sample = profile.samples.back();
400 sample.insert(sample.end(), &sample_frames[i][0],
401 &sample_frames[i][0] + arraysize(sample_frames[i]));
402 }
403
404 profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
405 profile.sampling_period = base::TimeDelta::FromMilliseconds(10);
406 Profiles profiles; 566 Profiles profiles;
407 profiles.push_back(std::move(profile)); 567 test_profiles_generator.GenerateProfiles(&profiles);
568 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());
408 569
409 CallStackProfileMetricsProvider provider; 570 CallStackProfileMetricsProvider provider;
410 provider.OnRecordingEnabled(); 571 provider.OnRecordingEnabled();
411 AppendProfiles(CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 572 AppendProfiles(CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
412 CallStackProfileParams::UI_THREAD, 573 CallStackProfileParams::UI_THREAD,
413 CallStackProfileParams::PROCESS_STARTUP, 574 CallStackProfileParams::PROCESS_STARTUP,
414 CallStackProfileParams::PRESERVE_ORDER), 575 CallStackProfileParams::PRESERVE_ORDER),
415 std::move(profiles)); 576 std::move(profiles));
416 ChromeUserMetricsExtension uma_proto; 577 ChromeUserMetricsExtension uma_proto;
417 provider.ProvideGeneralMetrics(&uma_proto); 578 provider.ProvideGeneralMetrics(&uma_proto);
418 579
419 ASSERT_EQ(1, uma_proto.sampled_profile().size()); 580 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
420 const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0); 581 uma_proto.sampled_profile().size());
421 ASSERT_TRUE(sampled_profile.has_call_stack_profile()); 582 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
422 const CallStackProfile& call_stack_profile = 583 SCOPED_TRACE("profile " + base::SizeTToString(p));
423 sampled_profile.call_stack_profile(); 584 VerifyProfileProto(expected_proto_profiles[p],
424 585 uma_proto.sampled_profile().Get(p));
425 ASSERT_EQ(3, call_stack_profile.sample().size());
426 for (int i = 0; i < 3; ++i) {
427 SCOPED_TRACE("sample " + base::IntToString(i));
428 const CallStackProfile::Sample& proto_sample =
429 call_stack_profile.sample().Get(i);
430 ASSERT_EQ(static_cast<int>(arraysize(sample_frames[i])),
431 proto_sample.entry().size());
432 ASSERT_TRUE(proto_sample.has_count());
433 EXPECT_EQ(i == 2 ? 2u : 1u, proto_sample.count());
434 for (size_t j = 0; j < arraysize(sample_frames[i]); ++j) {
435 SCOPED_TRACE("frame " + base::SizeTToString(j));
436 const CallStackProfile::Entry& entry = proto_sample.entry().Get(j);
437 ASSERT_TRUE(entry.has_address());
438 const char* instruction_pointer = reinterpret_cast<const char*>(
439 sample_frames[i][j].instruction_pointer);
440 const char* module_base_address = reinterpret_cast<const char*>(
441 modules[sample_frames[i][j].module_index].base_address);
442 EXPECT_EQ(
443 static_cast<uint64_t>(instruction_pointer - module_base_address),
444 entry.address());
445 ASSERT_TRUE(entry.has_module_id_index());
446 EXPECT_EQ(sample_frames[i][j].module_index,
447 static_cast<size_t>(entry.module_id_index()));
448 }
449 } 586 }
450 } 587 }
451 588
452 // Checks that unknown modules produce an empty Entry. 589 // Checks that unknown modules produce an empty Entry.
453 TEST_F(CallStackProfileMetricsProviderTest, UnknownModule) { 590 TEST_F(CallStackProfileMetricsProviderTest, UnknownModule) {
454 const Frame frame(0x1000, Frame::kUnknownModuleIndex); 591 ProfilesGenerator test_profiles_generator;
592 test_profiles_generator
593 .NewProfile(100, 10)
594 .NewSample()
595 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
596 .End();
455 597
456 Profile profile; 598 const ExpectedProtoEntry expected_proto_entries[] = {
599 { -1, 0 },
600 };
601 const ExpectedProtoSample expected_proto_samples[] = {
602 { 0, &expected_proto_entries[0], 1, 1 },
603 };
457 604
458 profile.samples.push_back(Sample(1, frame)); 605 const ExpectedProtoProfile expected_proto_profiles[] = {
606 {
607 100, 10,
608 nullptr, 0,
609 expected_proto_samples, arraysize(expected_proto_samples),
610 },
611 };
459 612
460 profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
461 profile.sampling_period = base::TimeDelta::FromMilliseconds(10);
462 Profiles profiles; 613 Profiles profiles;
463 profiles.push_back(std::move(profile)); 614 test_profiles_generator.GenerateProfiles(&profiles);
615 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());
464 616
465 CallStackProfileMetricsProvider provider; 617 CallStackProfileMetricsProvider provider;
466 provider.OnRecordingEnabled(); 618 provider.OnRecordingEnabled();
467 AppendProfiles( 619 AppendProfiles(
468 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 620 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
469 CallStackProfileParams::UI_THREAD, 621 CallStackProfileParams::UI_THREAD,
470 CallStackProfileParams::PROCESS_STARTUP, 622 CallStackProfileParams::PROCESS_STARTUP,
471 CallStackProfileParams::MAY_SHUFFLE), 623 CallStackProfileParams::MAY_SHUFFLE),
472 std::move(profiles)); 624 std::move(profiles));
473 ChromeUserMetricsExtension uma_proto; 625 ChromeUserMetricsExtension uma_proto;
474 provider.ProvideGeneralMetrics(&uma_proto); 626 provider.ProvideGeneralMetrics(&uma_proto);
475 627
476 ASSERT_EQ(1, uma_proto.sampled_profile().size()); 628 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
477 const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0); 629 uma_proto.sampled_profile().size());
478 ASSERT_TRUE(sampled_profile.has_call_stack_profile()); 630 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
479 const CallStackProfile& call_stack_profile = 631 SCOPED_TRACE("profile " + base::SizeTToString(p));
480 sampled_profile.call_stack_profile(); 632 VerifyProfileProto(expected_proto_profiles[p],
481 633 uma_proto.sampled_profile().Get(p));
482 ASSERT_EQ(1, call_stack_profile.sample().size()); 634 }
483 const CallStackProfile::Sample& proto_sample =
484 call_stack_profile.sample().Get(0);
485 ASSERT_EQ(1, proto_sample.entry().size());
486 ASSERT_TRUE(proto_sample.has_count());
487 EXPECT_EQ(1u, proto_sample.count());
488 const CallStackProfile::Entry& entry = proto_sample.entry().Get(0);
489 EXPECT_FALSE(entry.has_address());
490 EXPECT_FALSE(entry.has_module_id_index());
491 } 635 }
492 636
493 // Checks that pending profiles are only passed back to ProvideGeneralMetrics 637 // Checks that pending profiles are only passed back to ProvideGeneralMetrics
494 // once. 638 // once.
495 TEST_F(CallStackProfileMetricsProviderTest, ProfilesProvidedOnlyOnce) { 639 TEST_F(CallStackProfileMetricsProviderTest, ProfilesProvidedOnlyOnce) {
496 CallStackProfileMetricsProvider provider; 640 CallStackProfileMetricsProvider provider;
497 for (int i = 0; i < 2; ++i) { 641 for (int r = 0; r < 2; ++r) {
498 Profile profile; 642 ProfilesGenerator test_profiles_generator;
499 profile.samples.push_back( 643 test_profiles_generator
500 Sample(1, Frame(0x1000, Frame::kUnknownModuleIndex))); 644 // Use the sampling period to distinguish the two profiles.
645 .NewProfile(100, r)
646 .NewSample()
647 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
648 .End();
501 649
502 profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
503 // Use the sampling period to distinguish the two profiles.
504 profile.sampling_period = base::TimeDelta::FromMilliseconds(i);
505 Profiles profiles; 650 Profiles profiles;
506 profiles.push_back(std::move(profile)); 651 test_profiles_generator.GenerateProfiles(&profiles);
652 ASSERT_EQ(1U, profiles.size());
507 653
654 CallStackProfileMetricsProvider provider;
508 provider.OnRecordingEnabled(); 655 provider.OnRecordingEnabled();
509 AppendProfiles( 656 AppendProfiles(
510 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 657 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
511 CallStackProfileParams::UI_THREAD, 658 CallStackProfileParams::UI_THREAD,
512 CallStackProfileParams::PROCESS_STARTUP, 659 CallStackProfileParams::PROCESS_STARTUP,
513 CallStackProfileParams::MAY_SHUFFLE), 660 CallStackProfileParams::MAY_SHUFFLE),
514 std::move(profiles)); 661 std::move(profiles));
515 ChromeUserMetricsExtension uma_proto; 662 ChromeUserMetricsExtension uma_proto;
516 provider.ProvideGeneralMetrics(&uma_proto); 663 provider.ProvideGeneralMetrics(&uma_proto);
517 664
518 ASSERT_EQ(1, uma_proto.sampled_profile().size()); 665 ASSERT_EQ(1, uma_proto.sampled_profile().size());
519 const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0); 666 const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0);
520 ASSERT_TRUE(sampled_profile.has_call_stack_profile()); 667 ASSERT_TRUE(sampled_profile.has_call_stack_profile());
521 const CallStackProfile& call_stack_profile = 668 const CallStackProfile& call_stack_profile =
522 sampled_profile.call_stack_profile(); 669 sampled_profile.call_stack_profile();
523 ASSERT_TRUE(call_stack_profile.has_sampling_period_ms()); 670 ASSERT_TRUE(call_stack_profile.has_sampling_period_ms());
524 EXPECT_EQ(i, call_stack_profile.sampling_period_ms()); 671 EXPECT_EQ(r, call_stack_profile.sampling_period_ms());
525 } 672 }
526 } 673 }
527 674
528 // Checks that pending profiles are provided to ProvideGeneralMetrics 675 // Checks that pending profiles are provided to ProvideGeneralMetrics
529 // when collected before CallStackProfileMetricsProvider is instantiated. 676 // when collected before CallStackProfileMetricsProvider is instantiated.
530 TEST_F(CallStackProfileMetricsProviderTest, 677 TEST_F(CallStackProfileMetricsProviderTest,
531 ProfilesProvidedWhenCollectedBeforeInstantiation) { 678 ProfilesProvidedWhenCollectedBeforeInstantiation) {
532 Profile profile; 679 ProfilesGenerator test_profiles_generator;
533 profile.samples.push_back( 680 test_profiles_generator
534 Sample(1, Frame(0x1000, Frame::kUnknownModuleIndex))); 681 .NewProfile(100, 10)
682 .NewSample()
683 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
684 .End();
535 685
536 profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
537 profile.sampling_period = base::TimeDelta::FromMilliseconds(10);
538 Profiles profiles; 686 Profiles profiles;
539 profiles.push_back(std::move(profile)); 687 test_profiles_generator.GenerateProfiles(&profiles);
688 ASSERT_EQ(1U, profiles.size());
540 689
541 AppendProfiles( 690 AppendProfiles(
542 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 691 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
543 CallStackProfileParams::UI_THREAD, 692 CallStackProfileParams::UI_THREAD,
544 CallStackProfileParams::PROCESS_STARTUP, 693 CallStackProfileParams::PROCESS_STARTUP,
545 CallStackProfileParams::MAY_SHUFFLE), 694 CallStackProfileParams::MAY_SHUFFLE),
546 std::move(profiles)); 695 std::move(profiles));
547 696
548 CallStackProfileMetricsProvider provider; 697 CallStackProfileMetricsProvider provider;
549 provider.OnRecordingEnabled(); 698 provider.OnRecordingEnabled();
550 ChromeUserMetricsExtension uma_proto; 699 ChromeUserMetricsExtension uma_proto;
551 provider.ProvideGeneralMetrics(&uma_proto); 700 provider.ProvideGeneralMetrics(&uma_proto);
552 701
553 EXPECT_EQ(1, uma_proto.sampled_profile_size()); 702 EXPECT_EQ(1, uma_proto.sampled_profile_size());
554 } 703 }
555 704
556 // Checks that pending profiles are not provided to ProvideGeneralMetrics 705 // Checks that pending profiles are not provided to ProvideGeneralMetrics
557 // while recording is disabled. 706 // while recording is disabled.
558 TEST_F(CallStackProfileMetricsProviderTest, ProfilesNotProvidedWhileDisabled) { 707 TEST_F(CallStackProfileMetricsProviderTest, ProfilesNotProvidedWhileDisabled) {
559 Profile profile; 708 ProfilesGenerator test_profiles_generator;
560 profile.samples.push_back( 709 test_profiles_generator
561 Sample(1, Frame(0x1000, Frame::kUnknownModuleIndex))); 710 .NewProfile(100, 10)
711 .NewSample()
712 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
713 .End();
562 714
563 profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
564 profile.sampling_period = base::TimeDelta::FromMilliseconds(10);
565 Profiles profiles; 715 Profiles profiles;
566 profiles.push_back(std::move(profile)); 716 test_profiles_generator.GenerateProfiles(&profiles);
717 ASSERT_EQ(1U, profiles.size());
567 718
568 CallStackProfileMetricsProvider provider; 719 CallStackProfileMetricsProvider provider;
569 provider.OnRecordingDisabled(); 720 provider.OnRecordingDisabled();
570 AppendProfiles( 721 AppendProfiles(
571 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 722 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
572 CallStackProfileParams::UI_THREAD, 723 CallStackProfileParams::UI_THREAD,
573 CallStackProfileParams::PROCESS_STARTUP, 724 CallStackProfileParams::PROCESS_STARTUP,
574 CallStackProfileParams::MAY_SHUFFLE), 725 CallStackProfileParams::MAY_SHUFFLE),
575 std::move(profiles)); 726 std::move(profiles));
576 ChromeUserMetricsExtension uma_proto; 727 ChromeUserMetricsExtension uma_proto;
577 provider.ProvideGeneralMetrics(&uma_proto); 728 provider.ProvideGeneralMetrics(&uma_proto);
578 729
579 EXPECT_EQ(0, uma_proto.sampled_profile_size()); 730 EXPECT_EQ(0, uma_proto.sampled_profile_size());
580 } 731 }
581 732
582 // Checks that pending profiles are not provided to ProvideGeneralMetrics 733 // Checks that pending profiles are not provided to ProvideGeneralMetrics
583 // if recording is disabled while profiling. 734 // if recording is disabled while profiling.
584 TEST_F(CallStackProfileMetricsProviderTest, 735 TEST_F(CallStackProfileMetricsProviderTest,
585 ProfilesNotProvidedAfterChangeToDisabled) { 736 ProfilesNotProvidedAfterChangeToDisabled) {
586 Profile profile; 737 ProfilesGenerator test_profiles_generator;
587 profile.samples.push_back( 738 test_profiles_generator
588 Sample(1, Frame(0x1000, Frame::kUnknownModuleIndex))); 739 .NewProfile(100, 10)
589 740 .NewSample()
590 profile.profile_duration = base::TimeDelta::FromMilliseconds(100); 741 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
591 profile.sampling_period = base::TimeDelta::FromMilliseconds(10); 742 .End();
592 743
593 CallStackProfileMetricsProvider provider; 744 CallStackProfileMetricsProvider provider;
594 provider.OnRecordingEnabled(); 745 provider.OnRecordingEnabled();
595 base::StackSamplingProfiler::CompletedCallback callback = 746 base::StackSamplingProfiler::CompletedCallback callback =
596 CallStackProfileMetricsProvider::GetProfilerCallback( 747 CallStackProfileMetricsProvider::GetProfilerCallback(
597 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 748 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
598 CallStackProfileParams::UI_THREAD, 749 CallStackProfileParams::UI_THREAD,
599 CallStackProfileParams::PROCESS_STARTUP, 750 CallStackProfileParams::PROCESS_STARTUP,
600 CallStackProfileParams::MAY_SHUFFLE)); 751 CallStackProfileParams::MAY_SHUFFLE));
601 752
602 provider.OnRecordingDisabled(); 753 provider.OnRecordingDisabled();
603 Profiles profiles; 754 Profiles profiles;
604 profiles.push_back(std::move(profile)); 755 test_profiles_generator.GenerateProfiles(&profiles);
605 callback.Run(std::move(profiles)); 756 callback.Run(std::move(profiles));
606 ChromeUserMetricsExtension uma_proto; 757 ChromeUserMetricsExtension uma_proto;
607 provider.ProvideGeneralMetrics(&uma_proto); 758 provider.ProvideGeneralMetrics(&uma_proto);
608 759
609 EXPECT_EQ(0, uma_proto.sampled_profile_size()); 760 EXPECT_EQ(0, uma_proto.sampled_profile_size());
610 } 761 }
611 762
612 // Checks that pending profiles are not provided to ProvideGeneralMetrics if 763 // Checks that pending profiles are not provided to ProvideGeneralMetrics if
613 // recording is enabled, but then disabled and reenabled while profiling. 764 // recording is enabled, but then disabled and reenabled while profiling.
614 TEST_F(CallStackProfileMetricsProviderTest, 765 TEST_F(CallStackProfileMetricsProviderTest,
615 ProfilesNotProvidedAfterChangeToDisabledThenEnabled) { 766 ProfilesNotProvidedAfterChangeToDisabledThenEnabled) {
616 Profile profile; 767 ProfilesGenerator test_profiles_generator;
617 profile.samples.push_back( 768 test_profiles_generator
618 Sample(1, Frame(0x1000, Frame::kUnknownModuleIndex))); 769 .NewProfile(100, 10)
619 770 .NewSample()
620 profile.profile_duration = base::TimeDelta::FromMilliseconds(100); 771 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
621 profile.sampling_period = base::TimeDelta::FromMilliseconds(10); 772 .End();
622 773
623 CallStackProfileMetricsProvider provider; 774 CallStackProfileMetricsProvider provider;
624 provider.OnRecordingEnabled(); 775 provider.OnRecordingEnabled();
625 base::StackSamplingProfiler::CompletedCallback callback = 776 base::StackSamplingProfiler::CompletedCallback callback =
626 CallStackProfileMetricsProvider::GetProfilerCallback( 777 CallStackProfileMetricsProvider::GetProfilerCallback(
627 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 778 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
628 CallStackProfileParams::UI_THREAD, 779 CallStackProfileParams::UI_THREAD,
629 CallStackProfileParams::PROCESS_STARTUP, 780 CallStackProfileParams::PROCESS_STARTUP,
630 CallStackProfileParams::MAY_SHUFFLE)); 781 CallStackProfileParams::MAY_SHUFFLE));
631 782
632 provider.OnRecordingDisabled(); 783 provider.OnRecordingDisabled();
633 provider.OnRecordingEnabled(); 784 provider.OnRecordingEnabled();
634 Profiles profiles; 785 Profiles profiles;
635 profiles.push_back(std::move(profile)); 786 test_profiles_generator.GenerateProfiles(&profiles);
636 callback.Run(std::move(profiles)); 787 callback.Run(std::move(profiles));
637 ChromeUserMetricsExtension uma_proto; 788 ChromeUserMetricsExtension uma_proto;
638 provider.ProvideGeneralMetrics(&uma_proto); 789 provider.ProvideGeneralMetrics(&uma_proto);
639 790
640 EXPECT_EQ(0, uma_proto.sampled_profile_size()); 791 EXPECT_EQ(0, uma_proto.sampled_profile_size());
641 } 792 }
642 793
643 // Checks that pending profiles are not provided to ProvideGeneralMetrics 794 // Checks that pending profiles are not provided to ProvideGeneralMetrics
644 // if recording is disabled, but then enabled while profiling. 795 // if recording is disabled, but then enabled while profiling.
645 TEST_F(CallStackProfileMetricsProviderTest, 796 TEST_F(CallStackProfileMetricsProviderTest,
646 ProfilesNotProvidedAfterChangeFromDisabled) { 797 ProfilesNotProvidedAfterChangeFromDisabled) {
647 Profile profile; 798 ProfilesGenerator test_profiles_generator;
648 profile.samples.push_back( 799 test_profiles_generator
649 Sample(1, Frame(0x1000, Frame::kUnknownModuleIndex))); 800 .NewProfile(100, 10)
650 801 .NewSample()
651 profile.profile_duration = base::TimeDelta::FromMilliseconds(100); 802 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
652 profile.sampling_period = base::TimeDelta::FromMilliseconds(10); 803 .End();
653 804
654 CallStackProfileMetricsProvider provider; 805 CallStackProfileMetricsProvider provider;
655 provider.OnRecordingDisabled(); 806 provider.OnRecordingDisabled();
656 base::StackSamplingProfiler::CompletedCallback callback = 807 base::StackSamplingProfiler::CompletedCallback callback =
657 CallStackProfileMetricsProvider::GetProfilerCallback( 808 CallStackProfileMetricsProvider::GetProfilerCallback(
658 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 809 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
659 CallStackProfileParams::UI_THREAD, 810 CallStackProfileParams::UI_THREAD,
660 CallStackProfileParams::PROCESS_STARTUP, 811 CallStackProfileParams::PROCESS_STARTUP,
661 CallStackProfileParams::MAY_SHUFFLE)); 812 CallStackProfileParams::MAY_SHUFFLE));
662 813
663 provider.OnRecordingEnabled(); 814 provider.OnRecordingEnabled();
664 Profiles profiles; 815 Profiles profiles;
665 profiles.push_back(std::move(profile)); 816 test_profiles_generator.GenerateProfiles(&profiles);
666 callback.Run(std::move(profiles)); 817 callback.Run(std::move(profiles));
667 ChromeUserMetricsExtension uma_proto; 818 ChromeUserMetricsExtension uma_proto;
668 provider.ProvideGeneralMetrics(&uma_proto); 819 provider.ProvideGeneralMetrics(&uma_proto);
669 820
670 EXPECT_EQ(0, uma_proto.sampled_profile_size()); 821 EXPECT_EQ(0, uma_proto.sampled_profile_size());
671 } 822 }
672 823
673 } // namespace metrics 824 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/call_stack_profile_metrics_provider.cc ('k') | components/metrics/proto/call_stack_profile.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698