| 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 "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/files/memory_mapped_file.h" | 8 #include "base/files/memory_mapped_file.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 ~FileMetricsProviderTest() override { | 88 ~FileMetricsProviderTest() override { |
| 89 // Clear out any final remaining tasks. | 89 // Clear out any final remaining tasks. |
| 90 task_runner_->RunUntilIdle(); | 90 task_runner_->RunUntilIdle(); |
| 91 // If a global histogram allocator exists at this point then it likely | 91 // If a global histogram allocator exists at this point then it likely |
| 92 // acquired histograms that will continue to point to the released | 92 // acquired histograms that will continue to point to the released |
| 93 // memory and potentially cause use-after-free memory corruption. | 93 // memory and potentially cause use-after-free memory corruption. |
| 94 DCHECK(!base::GlobalHistogramAllocator::Get()); | 94 DCHECK(!base::GlobalHistogramAllocator::Get()); |
| 95 } | 95 } |
| 96 | 96 |
| 97 TestingPrefServiceSimple* prefs() { return prefs_.get(); } | 97 TestingPrefServiceSimple* prefs() { return prefs_.get(); } |
| 98 base::FilePath temp_dir() { return temp_dir_.path(); } | 98 base::FilePath temp_dir() { return temp_dir_.GetPath(); } |
| 99 base::FilePath metrics_file() { | 99 base::FilePath metrics_file() { |
| 100 return temp_dir_.path().AppendASCII(kMetricsFilename); | 100 return temp_dir_.GetPath().AppendASCII(kMetricsFilename); |
| 101 } | 101 } |
| 102 | 102 |
| 103 FileMetricsProvider* provider() { | 103 FileMetricsProvider* provider() { |
| 104 if (!provider_) | 104 if (!provider_) |
| 105 provider_.reset(new FileMetricsProvider(task_runner_, prefs())); | 105 provider_.reset(new FileMetricsProvider(task_runner_, prefs())); |
| 106 return provider_.get(); | 106 return provider_.get(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void OnDidCreateMetricsLog() { | 109 void OnDidCreateMetricsLog() { |
| 110 provider()->OnDidCreateMetricsLog(); | 110 provider()->OnDidCreateMetricsLog(); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 base::GlobalHistogramAllocator::Get(); | 279 base::GlobalHistogramAllocator::Get(); |
| 280 base::HistogramBase* histogram; | 280 base::HistogramBase* histogram; |
| 281 | 281 |
| 282 // Create files starting with a timestamp a few minutes back. | 282 // Create files starting with a timestamp a few minutes back. |
| 283 base::Time base_time = base::Time::Now() - base::TimeDelta::FromMinutes(10); | 283 base::Time base_time = base::Time::Now() - base::TimeDelta::FromMinutes(10); |
| 284 | 284 |
| 285 // Create some files in an odd order. The files are "touched" back in time to | 285 // Create some files in an odd order. The files are "touched" back in time to |
| 286 // ensure that each file has a later timestamp on disk than the previous one. | 286 // ensure that each file has a later timestamp on disk than the previous one. |
| 287 base::ScopedTempDir metrics_files; | 287 base::ScopedTempDir metrics_files; |
| 288 EXPECT_TRUE(metrics_files.CreateUniqueTempDir()); | 288 EXPECT_TRUE(metrics_files.CreateUniqueTempDir()); |
| 289 WriteMetricsFileAtTime(metrics_files.path().AppendASCII(".foo.pma"), | 289 WriteMetricsFileAtTime(metrics_files.GetPath().AppendASCII(".foo.pma"), |
| 290 allocator, base_time); | 290 allocator, base_time); |
| 291 WriteMetricsFileAtTime(metrics_files.path().AppendASCII("_bar.pma"), | 291 WriteMetricsFileAtTime(metrics_files.GetPath().AppendASCII("_bar.pma"), |
| 292 allocator, base_time); | 292 allocator, base_time); |
| 293 | 293 |
| 294 histogram = base::Histogram::FactoryGet("h1", 1, 100, 10, 0); | 294 histogram = base::Histogram::FactoryGet("h1", 1, 100, 10, 0); |
| 295 histogram->Add(1); | 295 histogram->Add(1); |
| 296 WriteMetricsFileAtTime(metrics_files.path().AppendASCII("a1.pma"), allocator, | 296 WriteMetricsFileAtTime(metrics_files.GetPath().AppendASCII("a1.pma"), |
| 297 allocator, |
| 297 base_time + base::TimeDelta::FromMinutes(1)); | 298 base_time + base::TimeDelta::FromMinutes(1)); |
| 298 | 299 |
| 299 histogram = base::Histogram::FactoryGet("h2", 1, 100, 10, 0); | 300 histogram = base::Histogram::FactoryGet("h2", 1, 100, 10, 0); |
| 300 histogram->Add(2); | 301 histogram->Add(2); |
| 301 WriteMetricsFileAtTime(metrics_files.path().AppendASCII("c2.pma"), allocator, | 302 WriteMetricsFileAtTime(metrics_files.GetPath().AppendASCII("c2.pma"), |
| 303 allocator, |
| 302 base_time + base::TimeDelta::FromMinutes(2)); | 304 base_time + base::TimeDelta::FromMinutes(2)); |
| 303 | 305 |
| 304 histogram = base::Histogram::FactoryGet("h3", 1, 100, 10, 0); | 306 histogram = base::Histogram::FactoryGet("h3", 1, 100, 10, 0); |
| 305 histogram->Add(3); | 307 histogram->Add(3); |
| 306 WriteMetricsFileAtTime(metrics_files.path().AppendASCII("b3.pma"), allocator, | 308 WriteMetricsFileAtTime(metrics_files.GetPath().AppendASCII("b3.pma"), |
| 309 allocator, |
| 307 base_time + base::TimeDelta::FromMinutes(3)); | 310 base_time + base::TimeDelta::FromMinutes(3)); |
| 308 | 311 |
| 309 histogram = base::Histogram::FactoryGet("h4", 1, 100, 10, 0); | 312 histogram = base::Histogram::FactoryGet("h4", 1, 100, 10, 0); |
| 310 histogram->Add(3); | 313 histogram->Add(3); |
| 311 WriteMetricsFileAtTime(metrics_files.path().AppendASCII("d4.pma"), allocator, | 314 WriteMetricsFileAtTime(metrics_files.GetPath().AppendASCII("d4.pma"), |
| 315 allocator, |
| 312 base_time + base::TimeDelta::FromMinutes(4)); | 316 base_time + base::TimeDelta::FromMinutes(4)); |
| 313 | 317 |
| 314 base::TouchFile(metrics_files.path().AppendASCII("b3.pma"), | 318 base::TouchFile(metrics_files.GetPath().AppendASCII("b3.pma"), |
| 315 base_time + base::TimeDelta::FromMinutes(5), | 319 base_time + base::TimeDelta::FromMinutes(5), |
| 316 base_time + base::TimeDelta::FromMinutes(5)); | 320 base_time + base::TimeDelta::FromMinutes(5)); |
| 317 | 321 |
| 318 WriteMetricsFileAtTime(metrics_files.path().AppendASCII("baz"), allocator, | 322 WriteMetricsFileAtTime(metrics_files.GetPath().AppendASCII("baz"), allocator, |
| 319 base_time + base::TimeDelta::FromMinutes(6)); | 323 base_time + base::TimeDelta::FromMinutes(6)); |
| 320 | 324 |
| 321 // The global allocator has to be detached here so that no metrics created | 325 // The global allocator has to be detached here so that no metrics created |
| 322 // by code called below get stored in it as that would make for potential | 326 // by code called below get stored in it as that would make for potential |
| 323 // use-after-free operations if that code is called again. | 327 // use-after-free operations if that code is called again. |
| 324 base::GlobalHistogramAllocator::ReleaseForTesting(); | 328 base::GlobalHistogramAllocator::ReleaseForTesting(); |
| 325 | 329 |
| 326 // Register the file and allow the "checker" task to run. | 330 // Register the file and allow the "checker" task to run. |
| 327 provider()->RegisterSource(metrics_files.path(), | 331 provider()->RegisterSource(metrics_files.GetPath(), |
| 328 FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_DIR, | 332 FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_DIR, |
| 329 FileMetricsProvider::ASSOCIATE_CURRENT_RUN, | 333 FileMetricsProvider::ASSOCIATE_CURRENT_RUN, |
| 330 kMetricsName); | 334 kMetricsName); |
| 331 | 335 |
| 332 // Files could come out in the order: a1, c2, d4, b3. They are recognizeable | 336 // Files could come out in the order: a1, c2, d4, b3. They are recognizeable |
| 333 // by the number of histograms contained within each. | 337 // by the number of histograms contained within each. |
| 334 const uint32_t expect_order[] = {1, 2, 4, 3, 0}; | 338 const uint32_t expect_order[] = {1, 2, 4, 3, 0}; |
| 335 for (size_t i = 0; i < arraysize(expect_order); ++i) { | 339 for (size_t i = 0; i < arraysize(expect_order); ++i) { |
| 336 // Record embedded snapshots via snapshot-manager. | 340 // Record embedded snapshots via snapshot-manager. |
| 337 OnDidCreateMetricsLog(); | 341 OnDidCreateMetricsLog(); |
| 338 RunTasks(); | 342 RunTasks(); |
| 339 EXPECT_EQ(expect_order[i], GetSnapshotHistogramCount()) << i; | 343 EXPECT_EQ(expect_order[i], GetSnapshotHistogramCount()) << i; |
| 340 } | 344 } |
| 341 | 345 |
| 342 EXPECT_FALSE(base::PathExists(metrics_files.path().AppendASCII("a1.pma"))); | 346 EXPECT_FALSE(base::PathExists(metrics_files.GetPath().AppendASCII("a1.pma"))); |
| 343 EXPECT_FALSE(base::PathExists(metrics_files.path().AppendASCII("c2.pma"))); | 347 EXPECT_FALSE(base::PathExists(metrics_files.GetPath().AppendASCII("c2.pma"))); |
| 344 EXPECT_FALSE(base::PathExists(metrics_files.path().AppendASCII("b3.pma"))); | 348 EXPECT_FALSE(base::PathExists(metrics_files.GetPath().AppendASCII("b3.pma"))); |
| 345 EXPECT_FALSE(base::PathExists(metrics_files.path().AppendASCII("d4.pma"))); | 349 EXPECT_FALSE(base::PathExists(metrics_files.GetPath().AppendASCII("d4.pma"))); |
| 346 EXPECT_TRUE(base::PathExists(metrics_files.path().AppendASCII(".foo.pma"))); | 350 EXPECT_TRUE( |
| 347 EXPECT_TRUE(base::PathExists(metrics_files.path().AppendASCII("_bar.pma"))); | 351 base::PathExists(metrics_files.GetPath().AppendASCII(".foo.pma"))); |
| 348 EXPECT_TRUE(base::PathExists(metrics_files.path().AppendASCII("baz"))); | 352 EXPECT_TRUE( |
| 353 base::PathExists(metrics_files.GetPath().AppendASCII("_bar.pma"))); |
| 354 EXPECT_TRUE(base::PathExists(metrics_files.GetPath().AppendASCII("baz"))); |
| 349 } | 355 } |
| 350 | 356 |
| 351 TEST_P(FileMetricsProviderTest, AccessReadWriteMetrics) { | 357 TEST_P(FileMetricsProviderTest, AccessReadWriteMetrics) { |
| 352 // Create a global histogram allocator that maps to a file. | 358 // Create a global histogram allocator that maps to a file. |
| 353 ASSERT_FALSE(PathExists(metrics_file())); | 359 ASSERT_FALSE(PathExists(metrics_file())); |
| 354 base::GlobalHistogramAllocator::GetCreateHistogramResultHistogram(); | 360 base::GlobalHistogramAllocator::GetCreateHistogramResultHistogram(); |
| 355 base::GlobalHistogramAllocator::CreateWithFile( | 361 base::GlobalHistogramAllocator::CreateWithFile( |
| 356 metrics_file(), | 362 metrics_file(), |
| 357 create_large_files_ ? kLargeFileSize : kSmallFileSize, | 363 create_large_files_ ? kLargeFileSize : kSmallFileSize, |
| 358 0, kMetricsName); | 364 0, kMetricsName); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 OnDidCreateMetricsLog(); | 430 OnDidCreateMetricsLog(); |
| 425 RunTasks(); | 431 RunTasks(); |
| 426 EXPECT_EQ(0U, GetSnapshotHistogramCount()); | 432 EXPECT_EQ(0U, GetSnapshotHistogramCount()); |
| 427 EXPECT_TRUE(base::PathExists(metrics_file())); | 433 EXPECT_TRUE(base::PathExists(metrics_file())); |
| 428 OnDidCreateMetricsLog(); | 434 OnDidCreateMetricsLog(); |
| 429 RunTasks(); | 435 RunTasks(); |
| 430 EXPECT_TRUE(base::PathExists(metrics_file())); | 436 EXPECT_TRUE(base::PathExists(metrics_file())); |
| 431 } | 437 } |
| 432 | 438 |
| 433 } // namespace metrics | 439 } // namespace metrics |
| OLD | NEW |