File: call_stack_profile_metrics_provider_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (746 lines) | stat: -rw-r--r-- 26,842 bytes parent folder | download
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// 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 <stddef.h>
#include <stdint.h>

#include <utility>

#include "base/macros.h"
#include "base/metrics/field_trial.h"
#include "base/profiler/stack_sampling_profiler.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "components/metrics/call_stack_profile_params.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 Profiles = StackSamplingProfiler::CallStackProfiles;
using Sample = StackSamplingProfiler::Sample;

namespace {

struct ExpectedProtoModule {
  const char* build_id;
  uint64_t name_md5;
  uint64_t base_address;
};

struct ExpectedProtoEntry {
  int32_t module_index;
  uint64_t address;
};

struct ExpectedProtoSample {
  uint32_t process_milestones;  // Bit-field of expected milestones.
  const ExpectedProtoEntry* entries;
  int entry_count;
  int64_t entry_repeats;
};

struct ExpectedProtoProfile {
  int32_t duration_ms;
  int32_t period_ms;
  const ExpectedProtoModule* modules;
  int module_count;
  const ExpectedProtoSample* samples;
  int sample_count;
};

class ProfilesFactory {
 public:
  ProfilesFactory(){};
  ~ProfilesFactory(){};

  ProfilesFactory& AddMilestone(int milestone);
  ProfilesFactory& NewProfile(int duration_ms, int interval_ms);
  ProfilesFactory& NewSample();
  ProfilesFactory& AddFrame(size_t module, uintptr_t offset);
  ProfilesFactory& DefineModule(const char* name,
                                const base::FilePath& path,
                                uintptr_t base);

  Profiles Build();

 private:
  Profiles profiles_;
  uint32_t process_milestones_ = 0;

  DISALLOW_COPY_AND_ASSIGN(ProfilesFactory);
};

ProfilesFactory& ProfilesFactory::AddMilestone(int milestone) {
  process_milestones_ |= 1 << milestone;
  return *this;
}

ProfilesFactory& ProfilesFactory::NewProfile(int duration_ms, int interval_ms) {
  profiles_.push_back(Profile());
  Profile* profile = &profiles_.back();
  profile->profile_duration = base::TimeDelta::FromMilliseconds(duration_ms);
  profile->sampling_period = base::TimeDelta::FromMilliseconds(interval_ms);
  return *this;
}

ProfilesFactory& ProfilesFactory::NewSample() {
  profiles_.back().samples.push_back(Sample());
  profiles_.back().samples.back().process_milestones = process_milestones_;
  return *this;
}

ProfilesFactory& ProfilesFactory::AddFrame(size_t module, uintptr_t offset) {
  profiles_.back().samples.back().frames.push_back(Frame(offset, module));
  return *this;
}

ProfilesFactory& ProfilesFactory::DefineModule(const char* name,
                                               const base::FilePath& path,
                                               uintptr_t base) {
  profiles_.back().modules.push_back(Module(base, name, path));
  return *this;
}

Profiles ProfilesFactory::Build() {
  return std::move(profiles_);
}

}  // namespace

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_(nullptr) {
    base::FieldTrialList::CreateFieldTrial(
        TestState::kFieldTrialName,
        TestState::kReportProfilesGroupName);
    TestState::ResetStaticStateForTesting();
  }

  ~CallStackProfileMetricsProviderTest() override {}

  // Utility function to append profiles to the metrics provider.
  void AppendProfiles(const CallStackProfileParams& params, Profiles profiles) {
    CallStackProfileMetricsProvider::GetProfilerCallback(params).Run(
        std::move(profiles));
  }

  void VerifyProfileProto(const ExpectedProtoProfile& expected,
                          const SampledProfile& proto);

 private:
  // Exposes field trial/group names from the CallStackProfileMetricsProvider.
  class TestState : public CallStackProfileMetricsProvider {
   public:
    using CallStackProfileMetricsProvider::kFieldTrialName;
    using CallStackProfileMetricsProvider::kReportProfilesGroupName;
    using CallStackProfileMetricsProvider::ResetStaticStateForTesting;
  };

  base::FieldTrialList field_trial_list_;

  DISALLOW_COPY_AND_ASSIGN(CallStackProfileMetricsProviderTest);
};

void CallStackProfileMetricsProviderTest::VerifyProfileProto(
    const ExpectedProtoProfile& expected,
    const SampledProfile& proto) {
  ASSERT_TRUE(proto.has_call_stack_profile());
  const CallStackProfile& stack = proto.call_stack_profile();

  ASSERT_TRUE(stack.has_profile_duration_ms());
  EXPECT_EQ(expected.duration_ms, stack.profile_duration_ms());
  ASSERT_TRUE(stack.has_sampling_period_ms());
  EXPECT_EQ(expected.period_ms, stack.sampling_period_ms());

  ASSERT_EQ(expected.module_count, stack.module_id().size());
  for (int m = 0; m < expected.module_count; ++m) {
    SCOPED_TRACE("module " + base::IntToString(m));
    const CallStackProfile::ModuleIdentifier& module_id =
        stack.module_id().Get(m);
    ASSERT_TRUE(module_id.has_build_id());
    EXPECT_EQ(expected.modules[m].build_id, module_id.build_id());
    ASSERT_TRUE(module_id.has_name_md5_prefix());
    EXPECT_EQ(expected.modules[m].name_md5, module_id.name_md5_prefix());
  }

  ASSERT_EQ(expected.sample_count, stack.sample().size());
  for (int s = 0; s < expected.sample_count; ++s) {
    SCOPED_TRACE("sample " + base::IntToString(s));
    const CallStackProfile::Sample& proto_sample = stack.sample().Get(s);

    uint32_t process_milestones = 0;
    for (int i = 0; i < proto_sample.process_phase().size(); ++i)
      process_milestones |= 1U << proto_sample.process_phase().Get(i);
    EXPECT_EQ(expected.samples[s].process_milestones, process_milestones);

    ASSERT_EQ(expected.samples[s].entry_count, proto_sample.entry().size());
    ASSERT_TRUE(proto_sample.has_count());
    EXPECT_EQ(expected.samples[s].entry_repeats, proto_sample.count());
    for (int e = 0; e < expected.samples[s].entry_count; ++e) {
      SCOPED_TRACE("entry " + base::SizeTToString(e));
      const CallStackProfile::Entry& entry = proto_sample.entry().Get(e);
      if (expected.samples[s].entries[e].module_index >= 0) {
        ASSERT_TRUE(entry.has_module_id_index());
        EXPECT_EQ(expected.samples[s].entries[e].module_index,
                  entry.module_id_index());
        ASSERT_TRUE(entry.has_address());
        EXPECT_EQ(expected.samples[s].entries[e].address, entry.address());
      } else {
        EXPECT_FALSE(entry.has_module_id_index());
        EXPECT_FALSE(entry.has_address());
      }
    }
  }
}

// Checks that all properties from multiple profiles are filled as expected.
TEST_F(CallStackProfileMetricsProviderTest, MultipleProfiles) {
  const uintptr_t moduleA_base_address = 0x1000;
  const uintptr_t moduleB_base_address = 0x2000;
  const uintptr_t moduleC_base_address = 0x3000;
  const char* moduleA_name = "ABCD";
  const char* moduleB_name = "EFGH";
  const char* moduleC_name = "MNOP";

  // 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
#if defined(OS_WIN)
  uint64_t moduleA_md5 = 0x46C3E4166659AC02ULL;
  uint64_t moduleB_md5 = 0x7E2B8BFDDEAE1ABAULL;
  uint64_t moduleC_md5 = 0x87B66F4573A4D5CAULL;
  base::FilePath moduleA_path(L"c:\\some\\path\\to\\chrome.exe");
  base::FilePath moduleB_path(L"c:\\some\\path\\to\\third_party.dll");
  base::FilePath moduleC_path(L"c:\\some\\path\\to\\third_party2.dll");
#else
  uint64_t moduleA_md5 = 0x554838A8451AC36CULL;
  uint64_t moduleB_md5 = 0x843661148659C9F8ULL;
  uint64_t moduleC_md5 = 0xB4647E539FA6EC9EULL;
  base::FilePath moduleA_path("/some/path/to/chrome");
  base::FilePath moduleB_path("/some/path/to/third_party.so");
  base::FilePath moduleC_path("/some/path/to/third_party2.so");
#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
  Profiles profiles = ProfilesFactory()
      .NewProfile(100, 10)
      .DefineModule(moduleA_name, moduleA_path, moduleA_base_address)
      .DefineModule(moduleB_name, moduleB_path, moduleB_base_address)

      .NewSample()
      .AddFrame(0, moduleA_base_address + 0x10)
      .AddFrame(1, moduleB_base_address + 0x20)
      .AddFrame(0, moduleA_base_address + 0x30)
      .NewSample()
      .AddFrame(1, moduleB_base_address + 0x10)
      .AddFrame(0, moduleA_base_address + 0x20)
      .AddFrame(1, moduleB_base_address + 0x30)

      .NewProfile(200, 20)
      .DefineModule(moduleC_name, moduleC_path, moduleC_base_address)
      .DefineModule(moduleA_name, moduleA_path, moduleA_base_address)

      .NewSample()
      .AddFrame(0, moduleC_base_address + 0x10)
      .AddFrame(1, moduleA_base_address + 0x20)
      .AddFrame(0, moduleC_base_address + 0x30)
      .NewSample()
      .AddFrame(1, moduleA_base_address + 0x10)
      .AddFrame(0, moduleC_base_address + 0x20)
      .AddFrame(1, moduleA_base_address + 0x30)

      .Build();

  const ExpectedProtoModule expected_proto_modules1[] = {
      { moduleA_name, moduleA_md5, moduleA_base_address },
      { moduleB_name, moduleB_md5, moduleB_base_address }
  };

  const ExpectedProtoEntry expected_proto_entries11[] = {
      { 0, 0x10 },
      { 1, 0x20 },
      { 0, 0x30 },
  };
  const ExpectedProtoEntry expected_proto_entries12[] = {
      { 1, 0x10 },
      { 0, 0x20 },
      { 1, 0x30 },
  };
  const ExpectedProtoSample expected_proto_samples1[] = {
      {
          0, expected_proto_entries11, arraysize(expected_proto_entries11), 1,
      },
      {
          0, expected_proto_entries12, arraysize(expected_proto_entries12), 1,
      },
  };

  const ExpectedProtoModule expected_proto_modules2[] = {
      { moduleC_name, moduleC_md5, moduleC_base_address },
      { moduleA_name, moduleA_md5, moduleA_base_address }
  };

  const ExpectedProtoEntry expected_proto_entries21[] = {
      { 0, 0x10 },
      { 1, 0x20 },
      { 0, 0x30 },
  };
  const ExpectedProtoEntry expected_proto_entries22[] = {
      { 1, 0x10 },
      { 0, 0x20 },
      { 1, 0x30 },
  };
  const ExpectedProtoSample expected_proto_samples2[] = {
      {
          0, expected_proto_entries11, arraysize(expected_proto_entries21), 1,
      },
      {
          0, expected_proto_entries12, arraysize(expected_proto_entries22), 1,
      },
  };

  const ExpectedProtoProfile expected_proto_profiles[] = {
      {
          100, 10,
          expected_proto_modules1, arraysize(expected_proto_modules1),
          expected_proto_samples1, arraysize(expected_proto_samples1),
      },
      {
          200, 20,
          expected_proto_modules2, arraysize(expected_proto_modules2),
          expected_proto_samples2, arraysize(expected_proto_samples2),
      },
  };

  ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());

  CallStackProfileMetricsProvider provider;
  provider.OnRecordingEnabled();
  AppendProfiles(
      CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                             CallStackProfileParams::UI_THREAD,
                             CallStackProfileParams::PROCESS_STARTUP,
                             CallStackProfileParams::MAY_SHUFFLE),
      std::move(profiles));
  ChromeUserMetricsExtension uma_proto;
  provider.ProvideGeneralMetrics(&uma_proto);

  ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
            uma_proto.sampled_profile().size());
  for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
    SCOPED_TRACE("profile " + base::SizeTToString(p));
    VerifyProfileProto(expected_proto_profiles[p],
                       uma_proto.sampled_profile().Get(p));
  }
}

// Checks that all duplicate samples are collapsed with
// preserve_sample_ordering = false.
TEST_F(CallStackProfileMetricsProviderTest, RepeatedStacksUnordered) {
  const uintptr_t module_base_address = 0x1000;
  const char* module_name = "ABCD";

#if defined(OS_WIN)
  uint64_t module_md5 = 0x46C3E4166659AC02ULL;
  base::FilePath module_path(L"c:\\some\\path\\to\\chrome.exe");
#else
  uint64_t module_md5 = 0x554838A8451AC36CULL;
  base::FilePath module_path("/some/path/to/chrome");
#endif

  Profiles profiles = ProfilesFactory()
      .NewProfile(100, 10)
      .DefineModule(module_name, module_path, module_base_address)

      .AddMilestone(0)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)
      .NewSample()
      .AddFrame(0, module_base_address + 0x20)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)

      .AddMilestone(1)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)
      .NewSample()
      .AddFrame(0, module_base_address + 0x20)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)

      .Build();

  const ExpectedProtoModule expected_proto_modules[] = {
      { module_name, module_md5, module_base_address },
  };

  const ExpectedProtoEntry expected_proto_entries[] = {
      { 0, 0x10 },
      { 0, 0x20 },
  };
  const ExpectedProtoSample expected_proto_samples[] = {
      { 1, &expected_proto_entries[0], 1, 3 },
      { 0, &expected_proto_entries[1], 1, 1 },
      { 2, &expected_proto_entries[0], 1, 3 },
      { 0, &expected_proto_entries[1], 1, 1 },
  };

  const ExpectedProtoProfile expected_proto_profiles[] = {
      {
          100, 10,
          expected_proto_modules, arraysize(expected_proto_modules),
          expected_proto_samples, arraysize(expected_proto_samples),
      },
  };

  ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());

  CallStackProfileMetricsProvider provider;
  provider.OnRecordingEnabled();
  AppendProfiles(
      CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                             CallStackProfileParams::UI_THREAD,
                             CallStackProfileParams::PROCESS_STARTUP,
                             CallStackProfileParams::MAY_SHUFFLE),
      std::move(profiles));
  ChromeUserMetricsExtension uma_proto;
  provider.ProvideGeneralMetrics(&uma_proto);

  ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
            uma_proto.sampled_profile().size());
  for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
    SCOPED_TRACE("profile " + base::SizeTToString(p));
    VerifyProfileProto(expected_proto_profiles[p],
                       uma_proto.sampled_profile().Get(p));
  }
}

// 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 char* module_name = "ABCD";

#if defined(OS_WIN)
  uint64_t module_md5 = 0x46C3E4166659AC02ULL;
  base::FilePath module_path(L"c:\\some\\path\\to\\chrome.exe");
#else
  uint64_t module_md5 = 0x554838A8451AC36CULL;
  base::FilePath module_path("/some/path/to/chrome");
#endif

  Profiles profiles = ProfilesFactory()
      .NewProfile(100, 10)
      .DefineModule(module_name, module_path, module_base_address)

      .AddMilestone(0)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)
      .NewSample()
      .AddFrame(0, module_base_address + 0x20)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)

      .AddMilestone(1)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)
      .NewSample()
      .AddFrame(0, module_base_address + 0x20)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)
      .NewSample()
      .AddFrame(0, module_base_address + 0x10)

      .Build();

  const ExpectedProtoModule expected_proto_modules[] = {
      { module_name, module_md5, module_base_address },
  };

  const ExpectedProtoEntry expected_proto_entries[] = {
      { 0, 0x10 },
      { 0, 0x20 },
  };
  const ExpectedProtoSample expected_proto_samples[] = {
      { 1, &expected_proto_entries[0], 1, 1 },
      { 0, &expected_proto_entries[1], 1, 1 },
      { 0, &expected_proto_entries[0], 1, 2 },
      { 2, &expected_proto_entries[0], 1, 1 },
      { 0, &expected_proto_entries[1], 1, 1 },
      { 0, &expected_proto_entries[0], 1, 2 },
  };

  const ExpectedProtoProfile expected_proto_profiles[] = {
      {
          100, 10,
          expected_proto_modules, arraysize(expected_proto_modules),
          expected_proto_samples, arraysize(expected_proto_samples),
      },
  };

  ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());

  CallStackProfileMetricsProvider provider;
  provider.OnRecordingEnabled();
  AppendProfiles(CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                                        CallStackProfileParams::UI_THREAD,
                                        CallStackProfileParams::PROCESS_STARTUP,
                                        CallStackProfileParams::PRESERVE_ORDER),
                 std::move(profiles));
  ChromeUserMetricsExtension uma_proto;
  provider.ProvideGeneralMetrics(&uma_proto);

  ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
            uma_proto.sampled_profile().size());
  for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
    SCOPED_TRACE("profile " + base::SizeTToString(p));
    VerifyProfileProto(expected_proto_profiles[p],
                       uma_proto.sampled_profile().Get(p));
  }
}

// Checks that unknown modules produce an empty Entry.
TEST_F(CallStackProfileMetricsProviderTest, UnknownModule) {
  Profiles profiles = ProfilesFactory()
      .NewProfile(100, 10)
      .NewSample()
      .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
      .Build();

  const ExpectedProtoEntry expected_proto_entries[] = {
      { -1, 0 },
  };
  const ExpectedProtoSample expected_proto_samples[] = {
      { 0, &expected_proto_entries[0], 1, 1 },
  };

  const ExpectedProtoProfile expected_proto_profiles[] = {
      {
          100, 10,
          nullptr, 0,
          expected_proto_samples, arraysize(expected_proto_samples),
      },
  };

  ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());

  CallStackProfileMetricsProvider provider;
  provider.OnRecordingEnabled();
  AppendProfiles(
      CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                             CallStackProfileParams::UI_THREAD,
                             CallStackProfileParams::PROCESS_STARTUP,
                             CallStackProfileParams::MAY_SHUFFLE),
      std::move(profiles));
  ChromeUserMetricsExtension uma_proto;
  provider.ProvideGeneralMetrics(&uma_proto);

  ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
            uma_proto.sampled_profile().size());
  for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
    SCOPED_TRACE("profile " + base::SizeTToString(p));
    VerifyProfileProto(expected_proto_profiles[p],
                       uma_proto.sampled_profile().Get(p));
  }
}

// Checks that pending profiles are only passed back to ProvideGeneralMetrics
// once.
TEST_F(CallStackProfileMetricsProviderTest, ProfilesProvidedOnlyOnce) {
  CallStackProfileMetricsProvider provider;
  for (int r = 0; r < 2; ++r) {
    Profiles profiles = ProfilesFactory()
        // Use the sampling period to distinguish the two profiles.
        .NewProfile(100, r)
        .NewSample()
        .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
        .Build();

    ASSERT_EQ(1U, profiles.size());

    CallStackProfileMetricsProvider provider;
    provider.OnRecordingEnabled();
    AppendProfiles(
        CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                               CallStackProfileParams::UI_THREAD,
                               CallStackProfileParams::PROCESS_STARTUP,
                               CallStackProfileParams::MAY_SHUFFLE),
        std::move(profiles));
    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(r, call_stack_profile.sampling_period_ms());
  }
}

// Checks that pending profiles are provided to ProvideGeneralMetrics
// when collected before CallStackProfileMetricsProvider is instantiated.
TEST_F(CallStackProfileMetricsProviderTest,
       ProfilesProvidedWhenCollectedBeforeInstantiation) {
  Profiles profiles = ProfilesFactory()
      .NewProfile(100, 10)
      .NewSample()
      .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
      .Build();

  ASSERT_EQ(1U, profiles.size());

  AppendProfiles(
      CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                             CallStackProfileParams::UI_THREAD,
                             CallStackProfileParams::PROCESS_STARTUP,
                             CallStackProfileParams::MAY_SHUFFLE),
      std::move(profiles));

  CallStackProfileMetricsProvider provider;
  provider.OnRecordingEnabled();
  ChromeUserMetricsExtension uma_proto;
  provider.ProvideGeneralMetrics(&uma_proto);

  EXPECT_EQ(1, uma_proto.sampled_profile_size());
}

// Checks that pending profiles are not provided to ProvideGeneralMetrics
// while recording is disabled.
TEST_F(CallStackProfileMetricsProviderTest, ProfilesNotProvidedWhileDisabled) {
  Profiles profiles = ProfilesFactory()
      .NewProfile(100, 10)
      .NewSample()
      .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
      .Build();

  ASSERT_EQ(1U, profiles.size());

  CallStackProfileMetricsProvider provider;
  provider.OnRecordingDisabled();
  AppendProfiles(
      CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                             CallStackProfileParams::UI_THREAD,
                             CallStackProfileParams::PROCESS_STARTUP,
                             CallStackProfileParams::MAY_SHUFFLE),
      std::move(profiles));
  ChromeUserMetricsExtension uma_proto;
  provider.ProvideGeneralMetrics(&uma_proto);

  EXPECT_EQ(0, uma_proto.sampled_profile_size());
}

// Checks that pending profiles are not provided to ProvideGeneralMetrics
// if recording is disabled while profiling.
TEST_F(CallStackProfileMetricsProviderTest,
       ProfilesNotProvidedAfterChangeToDisabled) {
  CallStackProfileMetricsProvider provider;
  provider.OnRecordingEnabled();
  base::StackSamplingProfiler::CompletedCallback callback =
      CallStackProfileMetricsProvider::GetProfilerCallback(
          CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                                 CallStackProfileParams::UI_THREAD,
                                 CallStackProfileParams::PROCESS_STARTUP,
                                 CallStackProfileParams::MAY_SHUFFLE));
  provider.OnRecordingDisabled();

  Profiles profiles = ProfilesFactory()
      .NewProfile(100, 10)
      .NewSample()
      .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
      .Build();
  callback.Run(std::move(profiles));
  ChromeUserMetricsExtension uma_proto;
  provider.ProvideGeneralMetrics(&uma_proto);

  EXPECT_EQ(0, uma_proto.sampled_profile_size());
}

// Checks that pending profiles are not provided to ProvideGeneralMetrics if
// recording is enabled, but then disabled and reenabled while profiling.
TEST_F(CallStackProfileMetricsProviderTest,
       ProfilesNotProvidedAfterChangeToDisabledThenEnabled) {
  CallStackProfileMetricsProvider provider;
  provider.OnRecordingEnabled();
  base::StackSamplingProfiler::CompletedCallback callback =
      CallStackProfileMetricsProvider::GetProfilerCallback(
          CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                                 CallStackProfileParams::UI_THREAD,
                                 CallStackProfileParams::PROCESS_STARTUP,
                                 CallStackProfileParams::MAY_SHUFFLE));
  provider.OnRecordingDisabled();
  provider.OnRecordingEnabled();

  Profiles profiles = ProfilesFactory()
      .NewProfile(100, 10)
      .NewSample()
      .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
      .Build();
  callback.Run(std::move(profiles));
  ChromeUserMetricsExtension uma_proto;
  provider.ProvideGeneralMetrics(&uma_proto);

  EXPECT_EQ(0, uma_proto.sampled_profile_size());
}

// Checks that pending profiles are not provided to ProvideGeneralMetrics
// if recording is disabled, but then enabled while profiling.
TEST_F(CallStackProfileMetricsProviderTest,
       ProfilesNotProvidedAfterChangeFromDisabled) {
  CallStackProfileMetricsProvider provider;
  provider.OnRecordingDisabled();
  base::StackSamplingProfiler::CompletedCallback callback =
      CallStackProfileMetricsProvider::GetProfilerCallback(
          CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
                                 CallStackProfileParams::UI_THREAD,
                                 CallStackProfileParams::PROCESS_STARTUP,
                                 CallStackProfileParams::MAY_SHUFFLE));
  provider.OnRecordingEnabled();

  Profiles profiles = ProfilesFactory()
      .NewProfile(100, 10)
      .NewSample()
      .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
      .Build();
  callback.Run(std::move(profiles));
  ChromeUserMetricsExtension uma_proto;
  provider.ProvideGeneralMetrics(&uma_proto);

  EXPECT_EQ(0, uma_proto.sampled_profile_size());
}

}  // namespace metrics