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