OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/file_metrics_provider.h" | 5 #include "components/metrics/file_metrics_provider.h" |
6 | 6 |
| 7 #include <functional> |
| 8 |
7 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
8 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
9 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
10 #include "base/macros.h" | 12 #include "base/macros.h" |
11 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
12 #include "base/metrics/histogram_flattener.h" | 14 #include "base/metrics/histogram_flattener.h" |
13 #include "base/metrics/histogram_snapshot_manager.h" | 15 #include "base/metrics/histogram_snapshot_manager.h" |
14 #include "base/metrics/persistent_histogram_allocator.h" | 16 #include "base/metrics/persistent_histogram_allocator.h" |
15 #include "base/metrics/persistent_memory_allocator.h" | 17 #include "base/metrics/persistent_memory_allocator.h" |
16 #include "base/metrics/sparse_histogram.h" | 18 #include "base/metrics/sparse_histogram.h" |
17 #include "base/metrics/statistics_recorder.h" | 19 #include "base/metrics/statistics_recorder.h" |
18 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
19 #include "base/test/test_simple_task_runner.h" | 21 #include "base/test/test_simple_task_runner.h" |
20 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
21 #include "base/time/time.h" | 23 #include "base/time/time.h" |
22 #include "components/metrics/metrics_pref_names.h" | 24 #include "components/metrics/metrics_pref_names.h" |
| 25 #include "components/metrics/persistent_system_profile.h" |
| 26 #include "components/metrics/proto/system_profile.pb.h" |
23 #include "components/prefs/pref_registry_simple.h" | 27 #include "components/prefs/pref_registry_simple.h" |
24 #include "components/prefs/testing_pref_service.h" | 28 #include "components/prefs/testing_pref_service.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
26 | 30 |
27 namespace { | 31 namespace { |
28 const char kMetricsName[] = "TestMetrics"; | 32 const char kMetricsName[] = "TestMetrics"; |
29 const char kMetricsFilename[] = "file.metrics"; | 33 const char kMetricsFilename[] = "file.metrics"; |
30 } // namespace | 34 } // namespace |
31 | 35 |
32 namespace metrics { | 36 namespace metrics { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 115 } |
112 | 116 |
113 bool HasInitialStabilityMetrics() { | 117 bool HasInitialStabilityMetrics() { |
114 return provider()->HasInitialStabilityMetrics(); | 118 return provider()->HasInitialStabilityMetrics(); |
115 } | 119 } |
116 | 120 |
117 void MergeHistogramDeltas() { | 121 void MergeHistogramDeltas() { |
118 provider()->MergeHistogramDeltas(); | 122 provider()->MergeHistogramDeltas(); |
119 } | 123 } |
120 | 124 |
| 125 bool ProvideIndependentMetrics( |
| 126 SystemProfileProto* profile_proto, |
| 127 base::HistogramSnapshotManager* snapshot_manager) { |
| 128 return provider()->ProvideIndependentMetrics(profile_proto, |
| 129 snapshot_manager); |
| 130 } |
| 131 |
121 void RecordInitialHistogramSnapshots( | 132 void RecordInitialHistogramSnapshots( |
122 base::HistogramSnapshotManager* snapshot_manager) { | 133 base::HistogramSnapshotManager* snapshot_manager) { |
123 provider()->RecordInitialHistogramSnapshots(snapshot_manager); | 134 provider()->RecordInitialHistogramSnapshots(snapshot_manager); |
124 } | 135 } |
125 | 136 |
126 size_t GetSnapshotHistogramCount() { | 137 size_t GetSnapshotHistogramCount() { |
127 // Merge the data from the allocator into the StatisticsRecorder. | 138 // Merge the data from the allocator into the StatisticsRecorder. |
128 provider()->MergeHistogramDeltas(); | 139 provider()->MergeHistogramDeltas(); |
129 | 140 |
130 // Flatten what is known to see what has changed since the last time. | 141 // Flatten what is known to see what has changed since the last time. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 } | 180 } |
170 | 181 |
171 void WriteMetricsFileAtTime(const base::FilePath& path, | 182 void WriteMetricsFileAtTime(const base::FilePath& path, |
172 base::PersistentHistogramAllocator* metrics, | 183 base::PersistentHistogramAllocator* metrics, |
173 base::Time write_time) { | 184 base::Time write_time) { |
174 WriteMetricsFile(path, metrics); | 185 WriteMetricsFile(path, metrics); |
175 base::TouchFile(path, write_time, write_time); | 186 base::TouchFile(path, write_time, write_time); |
176 } | 187 } |
177 | 188 |
178 std::unique_ptr<base::PersistentHistogramAllocator> | 189 std::unique_ptr<base::PersistentHistogramAllocator> |
179 CreateMetricsFileWithHistograms(int histogram_count) { | 190 CreateMetricsFileWithHistograms( |
| 191 int histogram_count, |
| 192 const std::function<void(base::PersistentHistogramAllocator*)>& |
| 193 callback) { |
180 // Get this first so it isn't created inside the persistent allocator. | 194 // Get this first so it isn't created inside the persistent allocator. |
181 base::GlobalHistogramAllocator::GetCreateHistogramResultHistogram(); | 195 base::GlobalHistogramAllocator::GetCreateHistogramResultHistogram(); |
182 | 196 |
183 base::GlobalHistogramAllocator::CreateWithLocalMemory( | 197 base::GlobalHistogramAllocator::CreateWithLocalMemory( |
184 create_large_files_ ? kLargeFileSize : kSmallFileSize, | 198 create_large_files_ ? kLargeFileSize : kSmallFileSize, |
185 0, kMetricsName); | 199 0, kMetricsName); |
186 | 200 |
187 CreateGlobalHistograms(histogram_count); | 201 CreateGlobalHistograms(histogram_count); |
188 | 202 |
189 std::unique_ptr<base::PersistentHistogramAllocator> histogram_allocator = | 203 std::unique_ptr<base::PersistentHistogramAllocator> histogram_allocator = |
190 base::GlobalHistogramAllocator::ReleaseForTesting(); | 204 base::GlobalHistogramAllocator::ReleaseForTesting(); |
| 205 callback(histogram_allocator.get()); |
| 206 |
191 WriteMetricsFile(metrics_file(), histogram_allocator.get()); | 207 WriteMetricsFile(metrics_file(), histogram_allocator.get()); |
192 return histogram_allocator; | 208 return histogram_allocator; |
193 } | 209 } |
194 | 210 |
| 211 std::unique_ptr<base::PersistentHistogramAllocator> |
| 212 CreateMetricsFileWithHistograms(int histogram_count) { |
| 213 return CreateMetricsFileWithHistograms( |
| 214 histogram_count, [](base::PersistentHistogramAllocator* allocator) {}); |
| 215 } |
| 216 |
195 base::HistogramBase* GetCreatedHistogram(int index) { | 217 base::HistogramBase* GetCreatedHistogram(int index) { |
196 DCHECK_GT(kMaxCreateHistograms, index); | 218 DCHECK_GT(kMaxCreateHistograms, index); |
197 return created_histograms_[index]; | 219 return created_histograms_[index]; |
198 } | 220 } |
199 | 221 |
200 const bool create_large_files_; | 222 const bool create_large_files_; |
201 | 223 |
202 private: | 224 private: |
203 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 225 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
204 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 226 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 CreateMetricsFileWithHistograms(2); | 426 CreateMetricsFileWithHistograms(2); |
405 | 427 |
406 // Register the file and allow the "checker" task to run. | 428 // Register the file and allow the "checker" task to run. |
407 ASSERT_TRUE(PathExists(metrics_file())); | 429 ASSERT_TRUE(PathExists(metrics_file())); |
408 provider()->RegisterSource(metrics_file(), | 430 provider()->RegisterSource(metrics_file(), |
409 FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE, | 431 FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE, |
410 FileMetricsProvider::ASSOCIATE_PREVIOUS_RUN, | 432 FileMetricsProvider::ASSOCIATE_PREVIOUS_RUN, |
411 kMetricsName); | 433 kMetricsName); |
412 | 434 |
413 // Record embedded snapshots via snapshot-manager. | 435 // Record embedded snapshots via snapshot-manager. |
414 HasInitialStabilityMetrics(); | 436 ASSERT_TRUE(HasInitialStabilityMetrics()); |
415 RunTasks(); | 437 RunTasks(); |
416 { | 438 { |
417 HistogramFlattenerDeltaRecorder flattener; | 439 HistogramFlattenerDeltaRecorder flattener; |
418 base::HistogramSnapshotManager snapshot_manager(&flattener); | 440 base::HistogramSnapshotManager snapshot_manager(&flattener); |
419 RecordInitialHistogramSnapshots(&snapshot_manager); | 441 RecordInitialHistogramSnapshots(&snapshot_manager); |
420 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); | 442 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); |
421 } | 443 } |
422 EXPECT_TRUE(base::PathExists(metrics_file())); | 444 EXPECT_TRUE(base::PathExists(metrics_file())); |
423 OnDidCreateMetricsLog(); | 445 OnDidCreateMetricsLog(); |
424 RunTasks(); | 446 RunTasks(); |
425 EXPECT_FALSE(base::PathExists(metrics_file())); | 447 EXPECT_FALSE(base::PathExists(metrics_file())); |
426 | 448 |
427 // A run for normal histograms should produce nothing. | 449 // A run for normal histograms should produce nothing. |
428 CreateMetricsFileWithHistograms(2); | 450 CreateMetricsFileWithHistograms(2); |
429 OnDidCreateMetricsLog(); | 451 OnDidCreateMetricsLog(); |
430 RunTasks(); | 452 RunTasks(); |
431 EXPECT_EQ(0U, GetSnapshotHistogramCount()); | 453 EXPECT_EQ(0U, GetSnapshotHistogramCount()); |
432 EXPECT_TRUE(base::PathExists(metrics_file())); | 454 EXPECT_TRUE(base::PathExists(metrics_file())); |
433 OnDidCreateMetricsLog(); | 455 OnDidCreateMetricsLog(); |
434 RunTasks(); | 456 RunTasks(); |
435 EXPECT_TRUE(base::PathExists(metrics_file())); | 457 EXPECT_TRUE(base::PathExists(metrics_file())); |
436 } | 458 } |
437 | 459 |
| 460 TEST_P(FileMetricsProviderTest, AccessEmbeddedProfileMetricsWithoutProfile) { |
| 461 ASSERT_FALSE(PathExists(metrics_file())); |
| 462 CreateMetricsFileWithHistograms(2); |
| 463 |
| 464 // Register the file and allow the "checker" task to run. |
| 465 ASSERT_TRUE(PathExists(metrics_file())); |
| 466 provider()->RegisterSource( |
| 467 metrics_file(), FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE, |
| 468 FileMetricsProvider::ASSOCIATE_INTERNAL_PROFILE, kMetricsName); |
| 469 |
| 470 // Record embedded snapshots via snapshot-manager. |
| 471 OnDidCreateMetricsLog(); |
| 472 RunTasks(); |
| 473 { |
| 474 HistogramFlattenerDeltaRecorder flattener; |
| 475 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 476 SystemProfileProto profile; |
| 477 |
| 478 // A read of metrics with internal profiles should return nothing. |
| 479 EXPECT_FALSE(ProvideIndependentMetrics(&profile, &snapshot_manager)); |
| 480 } |
| 481 EXPECT_TRUE(base::PathExists(metrics_file())); |
| 482 OnDidCreateMetricsLog(); |
| 483 RunTasks(); |
| 484 EXPECT_FALSE(base::PathExists(metrics_file())); |
| 485 } |
| 486 |
| 487 TEST_P(FileMetricsProviderTest, AccessEmbeddedProfileMetricsWithProfile) { |
| 488 ASSERT_FALSE(PathExists(metrics_file())); |
| 489 CreateMetricsFileWithHistograms( |
| 490 2, [](base::PersistentHistogramAllocator* allocator) { |
| 491 SystemProfileProto profile_proto; |
| 492 SystemProfileProto::FieldTrial* trial = profile_proto.add_field_trial(); |
| 493 trial->set_name_id(123); |
| 494 trial->set_group_id(456); |
| 495 |
| 496 PersistentSystemProfile persistent_profile; |
| 497 persistent_profile.RegisterPersistentAllocator( |
| 498 allocator->memory_allocator()); |
| 499 persistent_profile.SetSystemProfile(profile_proto); |
| 500 }); |
| 501 |
| 502 // Register the file and allow the "checker" task to run. |
| 503 ASSERT_TRUE(PathExists(metrics_file())); |
| 504 provider()->RegisterSource( |
| 505 metrics_file(), FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE, |
| 506 FileMetricsProvider::ASSOCIATE_INTERNAL_PROFILE, kMetricsName); |
| 507 |
| 508 // Record embedded snapshots via snapshot-manager. |
| 509 OnDidCreateMetricsLog(); |
| 510 RunTasks(); |
| 511 { |
| 512 HistogramFlattenerDeltaRecorder flattener; |
| 513 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 514 RecordInitialHistogramSnapshots(&snapshot_manager); |
| 515 EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 516 |
| 517 // A read of metrics with internal profiles should return one result. |
| 518 SystemProfileProto profile; |
| 519 EXPECT_TRUE(ProvideIndependentMetrics(&profile, &snapshot_manager)); |
| 520 EXPECT_FALSE(ProvideIndependentMetrics(&profile, &snapshot_manager)); |
| 521 } |
| 522 EXPECT_TRUE(base::PathExists(metrics_file())); |
| 523 OnDidCreateMetricsLog(); |
| 524 RunTasks(); |
| 525 EXPECT_FALSE(base::PathExists(metrics_file())); |
| 526 } |
| 527 |
| 528 TEST_P(FileMetricsProviderTest, AccessEmbeddedFallbackMetricsWithoutProfile) { |
| 529 ASSERT_FALSE(PathExists(metrics_file())); |
| 530 CreateMetricsFileWithHistograms(2); |
| 531 |
| 532 // Register the file and allow the "checker" task to run. |
| 533 ASSERT_TRUE(PathExists(metrics_file())); |
| 534 provider()->RegisterSource( |
| 535 metrics_file(), FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE, |
| 536 FileMetricsProvider::ASSOCIATE_INTERNAL_PROFILE_OR_PREVIOUS_RUN, |
| 537 kMetricsName); |
| 538 |
| 539 // Record embedded snapshots via snapshot-manager. |
| 540 ASSERT_TRUE(HasInitialStabilityMetrics()); |
| 541 RunTasks(); |
| 542 { |
| 543 HistogramFlattenerDeltaRecorder flattener; |
| 544 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 545 RecordInitialHistogramSnapshots(&snapshot_manager); |
| 546 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 547 |
| 548 // A read of metrics with internal profiles should return nothing. |
| 549 SystemProfileProto profile; |
| 550 EXPECT_FALSE(ProvideIndependentMetrics(&profile, &snapshot_manager)); |
| 551 } |
| 552 EXPECT_TRUE(base::PathExists(metrics_file())); |
| 553 OnDidCreateMetricsLog(); |
| 554 RunTasks(); |
| 555 EXPECT_FALSE(base::PathExists(metrics_file())); |
| 556 } |
| 557 |
| 558 TEST_P(FileMetricsProviderTest, AccessEmbeddedFallbackMetricsWithProfile) { |
| 559 ASSERT_FALSE(PathExists(metrics_file())); |
| 560 CreateMetricsFileWithHistograms( |
| 561 2, [](base::PersistentHistogramAllocator* allocator) { |
| 562 SystemProfileProto profile_proto; |
| 563 SystemProfileProto::FieldTrial* trial = profile_proto.add_field_trial(); |
| 564 trial->set_name_id(123); |
| 565 trial->set_group_id(456); |
| 566 |
| 567 PersistentSystemProfile persistent_profile; |
| 568 persistent_profile.RegisterPersistentAllocator( |
| 569 allocator->memory_allocator()); |
| 570 persistent_profile.SetSystemProfile(profile_proto); |
| 571 }); |
| 572 |
| 573 // Register the file and allow the "checker" task to run. |
| 574 ASSERT_TRUE(PathExists(metrics_file())); |
| 575 provider()->RegisterSource( |
| 576 metrics_file(), FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE, |
| 577 FileMetricsProvider::ASSOCIATE_INTERNAL_PROFILE_OR_PREVIOUS_RUN, |
| 578 kMetricsName); |
| 579 |
| 580 // Record embedded snapshots via snapshot-manager. |
| 581 EXPECT_FALSE(HasInitialStabilityMetrics()); |
| 582 RunTasks(); |
| 583 { |
| 584 HistogramFlattenerDeltaRecorder flattener; |
| 585 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 586 RecordInitialHistogramSnapshots(&snapshot_manager); |
| 587 EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 588 |
| 589 // A read of metrics with internal profiles should return one result. |
| 590 SystemProfileProto profile; |
| 591 EXPECT_TRUE(ProvideIndependentMetrics(&profile, &snapshot_manager)); |
| 592 EXPECT_FALSE(ProvideIndependentMetrics(&profile, &snapshot_manager)); |
| 593 } |
| 594 EXPECT_TRUE(base::PathExists(metrics_file())); |
| 595 OnDidCreateMetricsLog(); |
| 596 RunTasks(); |
| 597 EXPECT_FALSE(base::PathExists(metrics_file())); |
| 598 } |
| 599 |
438 } // namespace metrics | 600 } // namespace metrics |
OLD | NEW |