1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
// 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 "components/metrics/call_stack_profile_metrics_provider.h"
#include "base/metrics/field_trial.h"
#include "base/profiler/stack_sampling_profiler.h"
#include "base/strings/string_number_conversions.h"
#include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
#include "components/variations/entropy_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::StackSamplingProfiler;
using Frame = StackSamplingProfiler::Frame;
using Module = StackSamplingProfiler::Module;
using Profile = StackSamplingProfiler::CallStackProfile;
using Sample = StackSamplingProfiler::Sample;
namespace metrics {
// This test fixture enables the field trial that
// CallStackProfileMetricsProvider depends on to report profiles.
class CallStackProfileMetricsProviderTest : public testing::Test {
public:
CallStackProfileMetricsProviderTest()
: field_trial_list_(new base::FieldTrialList(
new metrics::SHA1EntropyProvider("foo"))) {
base::FieldTrialList::CreateFieldTrial(
FieldTrialState::kFieldTrialName,
FieldTrialState::kReportProfilesGroupName);
}
~CallStackProfileMetricsProviderTest() override {}
private:
// Exposes field trial/group names from the CallStackProfileMetricsProvider.
class FieldTrialState : public CallStackProfileMetricsProvider {
public:
using CallStackProfileMetricsProvider::kFieldTrialName;
using CallStackProfileMetricsProvider::kReportProfilesGroupName;
};
const scoped_ptr<base::FieldTrialList> field_trial_list_;
};
// Checks that all properties from multiple profiles are filled as expected.
TEST_F(CallStackProfileMetricsProviderTest, MultipleProfiles) {
const uintptr_t module1_base_address = 0x1000;
const uintptr_t module2_base_address = 0x2000;
const uintptr_t module3_base_address = 0x3000;
const Module profile_modules[][2] = {
{
Module(
reinterpret_cast<const void*>(module1_base_address),
"ABCD",
#if defined(OS_WIN)
base::FilePath(L"c:\\some\\path\\to\\chrome.exe")
#else
base::FilePath("/some/path/to/chrome")
#endif
),
Module(
reinterpret_cast<const void*>(module2_base_address),
"EFGH",
#if defined(OS_WIN)
base::FilePath(L"c:\\some\\path\\to\\third_party.dll")
#else
base::FilePath("/some/path/to/third_party.so")
#endif
),
},
{
Module(
reinterpret_cast<const void*>(module3_base_address),
"MNOP",
#if defined(OS_WIN)
base::FilePath(L"c:\\some\\path\\to\\third_party2.dll")
#else
base::FilePath("/some/path/to/third_party2.so")
#endif
),
Module( // Repeated from the first profile.
reinterpret_cast<const void*>(module1_base_address),
"ABCD",
#if defined(OS_WIN)
base::FilePath(L"c:\\some\\path\\to\\chrome.exe")
#else
base::FilePath("/some/path/to/chrome")
#endif
)
}
};
// Values for Windows generated with:
// perl -MDigest::MD5=md5 -MEncode=encode
// -e 'for(@ARGV){printf "%x\n", unpack "Q>", md5 encode "UTF-16LE", $_}'
// chrome.exe third_party.dll third_party2.dll
//
// Values for Linux generated with:
// perl -MDigest::MD5=md5
// -e 'for(@ARGV){printf "%x\n", unpack "Q>", md5 $_}'
// chrome third_party.so third_party2.so
const uint64 profile_expected_name_md5_prefixes[][2] = {
{
#if defined(OS_WIN)
0x46c3e4166659ac02ULL,
0x7e2b8bfddeae1abaULL
#else
0x554838a8451ac36cUL,
0x843661148659c9f8UL
#endif
},
{
#if defined(OS_WIN)
0x87b66f4573a4d5caULL,
0x46c3e4166659ac02ULL
#else
0xb4647e539fa6ec9eUL,
0x554838a8451ac36cUL
#endif
}
};
// Represents two stack samples for each of two profiles, where each stack
// contains three frames. Each frame contains an instruction pointer and a
// module index corresponding to the module for the profile in
// profile_modules.
//
// So, the first stack sample below has its top frame in module 0 at an offset
// of 0x10 from the module's base address, the next-to-top frame in module 1
// at an offset of 0x20 from the module's base address, and the bottom frame
// in module 0 at an offset of 0x30 from the module's base address
const Frame profile_sample_frames[][2][3] = {
{
{
Frame(reinterpret_cast<const void*>(module1_base_address + 0x10), 0),
Frame(reinterpret_cast<const void*>(module2_base_address + 0x20), 1),
Frame(reinterpret_cast<const void*>(module1_base_address + 0x30), 0)
},
{
Frame(reinterpret_cast<const void*>(module2_base_address + 0x10), 1),
Frame(reinterpret_cast<const void*>(module1_base_address + 0x20), 0),
Frame(reinterpret_cast<const void*>(module2_base_address + 0x30), 1)
}
},
{
{
Frame(reinterpret_cast<const void*>(module3_base_address + 0x10), 0),
Frame(reinterpret_cast<const void*>(module1_base_address + 0x20), 1),
Frame(reinterpret_cast<const void*>(module3_base_address + 0x30), 0)
},
{
Frame(reinterpret_cast<const void*>(module1_base_address + 0x10), 1),
Frame(reinterpret_cast<const void*>(module3_base_address + 0x20), 0),
Frame(reinterpret_cast<const void*>(module1_base_address + 0x30), 1)
}
}
};
base::TimeDelta profile_durations[2] = {
base::TimeDelta::FromMilliseconds(100),
base::TimeDelta::FromMilliseconds(200)
};
base::TimeDelta profile_sampling_periods[2] = {
base::TimeDelta::FromMilliseconds(10),
base::TimeDelta::FromMilliseconds(20)
};
std::vector<Profile> profiles;
for (size_t i = 0; i < arraysize(profile_sample_frames); ++i) {
Profile profile;
profile.modules.insert(
profile.modules.end(), &profile_modules[i][0],
&profile_modules[i][0] + arraysize(profile_modules[i]));
for (size_t j = 0; j < arraysize(profile_sample_frames[i]); ++j) {
profile.samples.push_back(Sample());
Sample& sample = profile.samples.back();
sample.insert(sample.end(), &profile_sample_frames[i][j][0],
&profile_sample_frames[i][j][0] +
arraysize(profile_sample_frames[i][j]));
}
profile.profile_duration = profile_durations[i];
profile.sampling_period = profile_sampling_periods[i];
profile.preserve_sample_ordering = false;
profile.user_data = CallStackProfileMetricsProvider::PROCESS_STARTUP;
profiles.push_back(profile);
}
CallStackProfileMetricsProvider provider;
provider.AppendSourceProfilesForTesting(profiles);
ChromeUserMetricsExtension uma_proto;
provider.ProvideGeneralMetrics(&uma_proto);
ASSERT_EQ(static_cast<int>(arraysize(profile_sample_frames)),
uma_proto.sampled_profile().size());
for (size_t i = 0; i < arraysize(profile_sample_frames); ++i) {
SCOPED_TRACE("profile " + base::IntToString(i));
const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(i);
ASSERT_TRUE(sampled_profile.has_call_stack_profile());
const CallStackProfile& call_stack_profile =
sampled_profile.call_stack_profile();
ASSERT_EQ(static_cast<int>(arraysize(profile_sample_frames[i])),
call_stack_profile.sample().size());
for (size_t j = 0; j < arraysize(profile_sample_frames[i]); ++j) {
SCOPED_TRACE("sample " + base::IntToString(j));
const CallStackProfile::Sample& proto_sample =
call_stack_profile.sample().Get(j);
ASSERT_EQ(static_cast<int>(arraysize(profile_sample_frames[i][j])),
proto_sample.entry().size());
ASSERT_TRUE(proto_sample.has_count());
EXPECT_EQ(1u, proto_sample.count());
for (size_t k = 0; k < arraysize(profile_sample_frames[i][j]); ++k) {
SCOPED_TRACE("frame " + base::IntToString(k));
const CallStackProfile::Entry& entry = proto_sample.entry().Get(k);
ASSERT_TRUE(entry.has_address());
const char* instruction_pointer = reinterpret_cast<const char*>(
profile_sample_frames[i][j][k].instruction_pointer);
const char* module_base_address = reinterpret_cast<const char*>(
profile_modules[i][profile_sample_frames[i][j][k].module_index]
.base_address);
EXPECT_EQ(static_cast<uint64>(instruction_pointer -
module_base_address), entry.address());
ASSERT_TRUE(entry.has_module_id_index());
EXPECT_EQ(profile_sample_frames[i][j][k].module_index,
static_cast<size_t>(entry.module_id_index()));
}
}
ASSERT_EQ(static_cast<int>(arraysize(profile_modules[i])),
call_stack_profile.module_id().size());
for (size_t j = 0; j < arraysize(profile_modules[i]); ++j) {
SCOPED_TRACE("module " + base::IntToString(j));
const CallStackProfile::ModuleIdentifier& module_identifier =
call_stack_profile.module_id().Get(j);
ASSERT_TRUE(module_identifier.has_build_id());
EXPECT_EQ(profile_modules[i][j].id, module_identifier.build_id());
ASSERT_TRUE(module_identifier.has_name_md5_prefix());
EXPECT_EQ(profile_expected_name_md5_prefixes[i][j],
module_identifier.name_md5_prefix());
}
ASSERT_TRUE(call_stack_profile.has_profile_duration_ms());
EXPECT_EQ(profile_durations[i].InMilliseconds(),
call_stack_profile.profile_duration_ms());
ASSERT_TRUE(call_stack_profile.has_sampling_period_ms());
EXPECT_EQ(profile_sampling_periods[i].InMilliseconds(),
call_stack_profile.sampling_period_ms());
ASSERT_TRUE(sampled_profile.has_trigger_event());
EXPECT_EQ(SampledProfile::PROCESS_STARTUP, sampled_profile.trigger_event());
}
}
// Checks that all duplicate samples are collapsed with
// preserve_sample_ordering = false.
TEST_F(CallStackProfileMetricsProviderTest, RepeatedStacksUnordered) {
const uintptr_t module_base_address = 0x1000;
const Module modules[] = {
Module(
reinterpret_cast<const void*>(module_base_address),
"ABCD",
#if defined(OS_WIN)
base::FilePath(L"c:\\some\\path\\to\\chrome.exe")
#else
base::FilePath("/some/path/to/chrome")
#endif
)
};
// Duplicate samples in slots 0, 2, and 3.
const Frame sample_frames[][1] = {
{ Frame(reinterpret_cast<const void*>(module_base_address + 0x10), 0), },
{ Frame(reinterpret_cast<const void*>(module_base_address + 0x20), 0), },
{ Frame(reinterpret_cast<const void*>(module_base_address + 0x10), 0), },
{ Frame(reinterpret_cast<const void*>(module_base_address + 0x10), 0) }
};
Profile profile;
profile.modules.insert(profile.modules.end(), &modules[0],
&modules[0] + arraysize(modules));
for (size_t i = 0; i < arraysize(sample_frames); ++i) {
profile.samples.push_back(Sample());
Sample& sample = profile.samples.back();
sample.insert(sample.end(), &sample_frames[i][0],
&sample_frames[i][0] + arraysize(sample_frames[i]));
}
profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
profile.sampling_period = base::TimeDelta::FromMilliseconds(10);
profile.preserve_sample_ordering = false;
CallStackProfileMetricsProvider provider;
provider.AppendSourceProfilesForTesting(std::vector<Profile>(1, profile));
ChromeUserMetricsExtension uma_proto;
provider.ProvideGeneralMetrics(&uma_proto);
ASSERT_EQ(1, uma_proto.sampled_profile().size());
const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0);
ASSERT_TRUE(sampled_profile.has_call_stack_profile());
const CallStackProfile& call_stack_profile =
sampled_profile.call_stack_profile();
ASSERT_EQ(2, call_stack_profile.sample().size());
for (int i = 0; i < 2; ++i) {
SCOPED_TRACE("sample " + base::IntToString(i));
const CallStackProfile::Sample& proto_sample =
call_stack_profile.sample().Get(i);
ASSERT_EQ(static_cast<int>(arraysize(sample_frames[i])),
proto_sample.entry().size());
ASSERT_TRUE(proto_sample.has_count());
EXPECT_EQ(i == 0 ? 3u : 1u, proto_sample.count());
for (size_t j = 0; j < arraysize(sample_frames[i]); ++j) {
SCOPED_TRACE("frame " + base::IntToString(j));
const CallStackProfile::Entry& entry = proto_sample.entry().Get(j);
ASSERT_TRUE(entry.has_address());
const char* instruction_pointer = reinterpret_cast<const char*>(
sample_frames[i][j].instruction_pointer);
const char* module_base_address = reinterpret_cast<const char*>(
modules[sample_frames[i][j].module_index].base_address);
EXPECT_EQ(static_cast<uint64>(instruction_pointer - module_base_address),
entry.address());
ASSERT_TRUE(entry.has_module_id_index());
EXPECT_EQ(sample_frames[i][j].module_index,
static_cast<size_t>(entry.module_id_index()));
}
}
}
// Checks that only contiguous duplicate samples are collapsed with
// preserve_sample_ordering = true.
TEST_F(CallStackProfileMetricsProviderTest, RepeatedStacksOrdered) {
const uintptr_t module_base_address = 0x1000;
const Module modules[] = {
Module(
reinterpret_cast<const void*>(module_base_address),
"ABCD",
#if defined(OS_WIN)
base::FilePath(L"c:\\some\\path\\to\\chrome.exe")
#else
base::FilePath("/some/path/to/chrome")
#endif
)
};
// Duplicate samples in slots 0, 2, and 3.
const Frame sample_frames[][1] = {
{ Frame(reinterpret_cast<const void*>(module_base_address + 0x10), 0), },
{ Frame(reinterpret_cast<const void*>(module_base_address + 0x20), 0), },
{ Frame(reinterpret_cast<const void*>(module_base_address + 0x10), 0), },
{ Frame(reinterpret_cast<const void*>(module_base_address + 0x10), 0) }
};
Profile profile;
profile.modules.insert(profile.modules.end(), &modules[0],
&modules[0] + arraysize(modules));
for (size_t i = 0; i < arraysize(sample_frames); ++i) {
profile.samples.push_back(Sample());
Sample& sample = profile.samples.back();
sample.insert(sample.end(), &sample_frames[i][0],
&sample_frames[i][0] + arraysize(sample_frames[i]));
}
profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
profile.sampling_period = base::TimeDelta::FromMilliseconds(10);
profile.preserve_sample_ordering = true;
CallStackProfileMetricsProvider provider;
provider.AppendSourceProfilesForTesting(std::vector<Profile>(1, profile));
ChromeUserMetricsExtension uma_proto;
provider.ProvideGeneralMetrics(&uma_proto);
ASSERT_EQ(1, uma_proto.sampled_profile().size());
const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0);
ASSERT_TRUE(sampled_profile.has_call_stack_profile());
const CallStackProfile& call_stack_profile =
sampled_profile.call_stack_profile();
ASSERT_EQ(3, call_stack_profile.sample().size());
for (int i = 0; i < 3; ++i) {
SCOPED_TRACE("sample " + base::IntToString(i));
const CallStackProfile::Sample& proto_sample =
call_stack_profile.sample().Get(i);
ASSERT_EQ(static_cast<int>(arraysize(sample_frames[i])),
proto_sample.entry().size());
ASSERT_TRUE(proto_sample.has_count());
EXPECT_EQ(i == 2 ? 2u : 1u, proto_sample.count());
for (size_t j = 0; j < arraysize(sample_frames[i]); ++j) {
SCOPED_TRACE("frame " + base::IntToString(j));
const CallStackProfile::Entry& entry = proto_sample.entry().Get(j);
ASSERT_TRUE(entry.has_address());
const char* instruction_pointer = reinterpret_cast<const char*>(
sample_frames[i][j].instruction_pointer);
const char* module_base_address = reinterpret_cast<const char*>(
modules[sample_frames[i][j].module_index].base_address);
EXPECT_EQ(static_cast<uint64>(instruction_pointer - module_base_address),
entry.address());
ASSERT_TRUE(entry.has_module_id_index());
EXPECT_EQ(sample_frames[i][j].module_index,
static_cast<size_t>(entry.module_id_index()));
}
}
}
// Checks that unknown modules produce an empty Entry.
TEST_F(CallStackProfileMetricsProviderTest, UnknownModule) {
const Frame frame(reinterpret_cast<const void*>(0x1000),
Frame::kUnknownModuleIndex);
Profile profile;
profile.samples.push_back(Sample(1, frame));
profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
profile.sampling_period = base::TimeDelta::FromMilliseconds(10);
profile.preserve_sample_ordering = false;
CallStackProfileMetricsProvider provider;
provider.AppendSourceProfilesForTesting(std::vector<Profile>(1, profile));
ChromeUserMetricsExtension uma_proto;
provider.ProvideGeneralMetrics(&uma_proto);
ASSERT_EQ(1, uma_proto.sampled_profile().size());
const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0);
ASSERT_TRUE(sampled_profile.has_call_stack_profile());
const CallStackProfile& call_stack_profile =
sampled_profile.call_stack_profile();
ASSERT_EQ(1, call_stack_profile.sample().size());
const CallStackProfile::Sample& proto_sample =
call_stack_profile.sample().Get(0);
ASSERT_EQ(1, proto_sample.entry().size());
ASSERT_TRUE(proto_sample.has_count());
EXPECT_EQ(1u, proto_sample.count());
const CallStackProfile::Entry& entry = proto_sample.entry().Get(0);
EXPECT_FALSE(entry.has_address());
EXPECT_FALSE(entry.has_module_id_index());
}
// Checks that pending profiles are only passed back to ProvideGeneralMetrics
// once.
TEST_F(CallStackProfileMetricsProviderTest, ProfilesProvidedOnlyOnce) {
CallStackProfileMetricsProvider provider;
for (int i = 0; i < 2; ++i) {
Profile profile;
profile.samples.push_back(Sample(1, Frame(
reinterpret_cast<const void*>(0x1000), Frame::kUnknownModuleIndex)));
profile.profile_duration = base::TimeDelta::FromMilliseconds(100);
// Use the sampling period to distinguish the two profiles.
profile.sampling_period = base::TimeDelta::FromMilliseconds(i);
profile.preserve_sample_ordering = false;
provider.AppendSourceProfilesForTesting(std::vector<Profile>(1, profile));
ChromeUserMetricsExtension uma_proto;
provider.ProvideGeneralMetrics(&uma_proto);
ASSERT_EQ(1, uma_proto.sampled_profile().size());
const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0);
ASSERT_TRUE(sampled_profile.has_call_stack_profile());
const CallStackProfile& call_stack_profile =
sampled_profile.call_stack_profile();
ASSERT_TRUE(call_stack_profile.has_sampling_period_ms());
EXPECT_EQ(i, call_stack_profile.sampling_period_ms());
}
}
} // namespace metrics
|