Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: net/disk_cache/backend_unittest.cc

Issue 2779733002: [HttpCache] Store some memcache info to memory dumps for debugging (Closed)
Patch Set: Rebase xunjieli's change Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/files/file.h" 7 #include "base/files/file.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/metrics/field_trial.h" 10 #include "base/metrics/field_trial.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/test/mock_entropy_provider.h" 16 #include "base/test/mock_entropy_provider.h"
17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
18 #include "base/threading/platform_thread.h" 18 #include "base/threading/platform_thread.h"
19 #include "base/threading/thread_restrictions.h" 19 #include "base/threading/thread_restrictions.h"
20 #include "base/threading/thread_task_runner_handle.h" 20 #include "base/threading/thread_task_runner_handle.h"
21 #include "base/trace_event/memory_usage_estimator.h" 21 #include "base/trace_event/memory_allocator_dump.h"
22 #include "base/trace_event/process_memory_dump.h"
23 #include "base/trace_event/trace_event_argument.h"
22 #include "net/base/cache_type.h" 24 #include "net/base/cache_type.h"
23 #include "net/base/io_buffer.h" 25 #include "net/base/io_buffer.h"
24 #include "net/base/net_errors.h" 26 #include "net/base/net_errors.h"
25 #include "net/base/test_completion_callback.h" 27 #include "net/base/test_completion_callback.h"
26 #include "net/disk_cache/blockfile/backend_impl.h" 28 #include "net/disk_cache/blockfile/backend_impl.h"
27 #include "net/disk_cache/blockfile/entry_impl.h" 29 #include "net/disk_cache/blockfile/entry_impl.h"
28 #include "net/disk_cache/blockfile/experiments.h" 30 #include "net/disk_cache/blockfile/experiments.h"
29 #include "net/disk_cache/blockfile/histogram_macros.h" 31 #include "net/disk_cache/blockfile/histogram_macros.h"
30 #include "net/disk_cache/blockfile/mapped_file.h" 32 #include "net/disk_cache/blockfile/mapped_file.h"
31 #include "net/disk_cache/cache_util.h" 33 #include "net/disk_cache/cache_util.h"
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 } 319 }
318 320
319 void DiskCacheBackendTest::BackendBasics() { 321 void DiskCacheBackendTest::BackendBasics() {
320 InitCache(); 322 InitCache();
321 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; 323 disk_cache::Entry *entry1 = NULL, *entry2 = NULL;
322 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); 324 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1));
323 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk()); 325 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk());
324 ASSERT_TRUE(NULL != entry1); 326 ASSERT_TRUE(NULL != entry1);
325 entry1->Close(); 327 entry1->Close();
326 entry1 = NULL; 328 entry1 = NULL;
327 // base::trace_event::EstimateMemoryUsage(cache_) is added to make sure
328 // tracking memory doesn't introduce crashes.
329 EXPECT_LT(0u, base::trace_event::EstimateMemoryUsage(cache_));
330 329
331 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk()); 330 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk());
332 ASSERT_TRUE(NULL != entry1); 331 ASSERT_TRUE(NULL != entry1);
333 entry1->Close(); 332 entry1->Close();
334 entry1 = NULL; 333 entry1 = NULL;
335 334
336 EXPECT_NE(net::OK, CreateEntry("the first key", &entry1)); 335 EXPECT_NE(net::OK, CreateEntry("the first key", &entry1));
337 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk()); 336 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk());
338 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2)); 337 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2));
339 ASSERT_THAT(CreateEntry("some other key", &entry2), IsOk()); 338 ASSERT_THAT(CreateEntry("some other key", &entry2), IsOk());
340 ASSERT_TRUE(NULL != entry1); 339 ASSERT_TRUE(NULL != entry1);
341 ASSERT_TRUE(NULL != entry2); 340 ASSERT_TRUE(NULL != entry2);
342 EXPECT_EQ(2, cache_->GetEntryCount()); 341 EXPECT_EQ(2, cache_->GetEntryCount());
343 EXPECT_LT(0u, base::trace_event::EstimateMemoryUsage(cache_));
344 342
345 disk_cache::Entry* entry3 = NULL; 343 disk_cache::Entry* entry3 = NULL;
346 ASSERT_THAT(OpenEntry("some other key", &entry3), IsOk()); 344 ASSERT_THAT(OpenEntry("some other key", &entry3), IsOk());
347 ASSERT_TRUE(NULL != entry3); 345 ASSERT_TRUE(NULL != entry3);
348 EXPECT_TRUE(entry2 == entry3); 346 EXPECT_TRUE(entry2 == entry3);
349 EXPECT_LT(0u, base::trace_event::EstimateMemoryUsage(cache_));
350 347
351 EXPECT_THAT(DoomEntry("some other key"), IsOk()); 348 EXPECT_THAT(DoomEntry("some other key"), IsOk());
352 EXPECT_EQ(1, cache_->GetEntryCount()); 349 EXPECT_EQ(1, cache_->GetEntryCount());
353 entry1->Close(); 350 entry1->Close();
354 entry2->Close(); 351 entry2->Close();
355 entry3->Close(); 352 entry3->Close();
356 EXPECT_LT(0u, base::trace_event::EstimateMemoryUsage(cache_));
357 353
358 EXPECT_THAT(DoomEntry("the first key"), IsOk()); 354 EXPECT_THAT(DoomEntry("the first key"), IsOk());
359 EXPECT_EQ(0, cache_->GetEntryCount()); 355 EXPECT_EQ(0, cache_->GetEntryCount());
360 356
361 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk()); 357 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk());
362 ASSERT_THAT(CreateEntry("some other key", &entry2), IsOk()); 358 ASSERT_THAT(CreateEntry("some other key", &entry2), IsOk());
363 entry1->Doom(); 359 entry1->Doom();
364 entry1->Close(); 360 entry1->Close();
365 EXPECT_THAT(DoomEntry("some other key"), IsOk()); 361 EXPECT_THAT(DoomEntry("some other key"), IsOk());
366 EXPECT_EQ(0, cache_->GetEntryCount()); 362 EXPECT_EQ(0, cache_->GetEntryCount());
367 entry2->Close(); 363 entry2->Close();
368 EXPECT_LT(0u, base::trace_event::EstimateMemoryUsage(cache_));
369 } 364 }
370 365
371 TEST_F(DiskCacheBackendTest, Basics) { 366 TEST_F(DiskCacheBackendTest, Basics) {
372 BackendBasics(); 367 BackendBasics();
373 } 368 }
374 369
375 TEST_F(DiskCacheBackendTest, NewEvictionBasics) { 370 TEST_F(DiskCacheBackendTest, NewEvictionBasics) {
376 SetNewEviction(); 371 SetNewEviction();
377 BackendBasics(); 372 BackendBasics();
378 } 373 }
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 std::unique_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( 508 std::unique_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl(
514 cache_path_, cache_thread.task_runner(), NULL)); 509 cache_path_, cache_thread.task_runner(), NULL));
515 int rv = cache->Init(cb.callback()); 510 int rv = cache->Init(cb.callback());
516 EXPECT_THAT(cb.GetResult(rv), IsError(net::ERR_FAILED)); 511 EXPECT_THAT(cb.GetResult(rv), IsError(net::ERR_FAILED));
517 base::ThreadRestrictions::SetIOAllowed(prev); 512 base::ThreadRestrictions::SetIOAllowed(prev);
518 513
519 cache.reset(); 514 cache.reset();
520 DisableIntegrityCheck(); 515 DisableIntegrityCheck();
521 } 516 }
522 517
518 TEST_F(DiskCacheBackendTest, MemCacheMemoryDump) {
519 memory_only_ = true;
520 BackendBasics();
521 base::trace_event::MemoryDumpArgs args = {
522 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND};
523 base::trace_event::ProcessMemoryDump pmd(nullptr, args);
524 base::trace_event::MemoryAllocatorDump* parent =
525 pmd.CreateAllocatorDump("net/url_request_context/main/0x123/http_cache");
526
527 ASSERT_LT(0u, cache_->DumpMemoryStats(&pmd, parent->absolute_name()));
528 EXPECT_EQ(2u, pmd.allocator_dumps().size());
529 const base::trace_event::MemoryAllocatorDump* sub_dump =
530 pmd.GetAllocatorDump(parent->absolute_name() + "/memory_backend");
531 ASSERT_NE(nullptr, sub_dump);
532
533 // Verify that the appropriate attributes were set.
534 std::unique_ptr<base::Value> raw_attrs =
535 sub_dump->attributes_for_testing()->ToBaseValue();
536 base::DictionaryValue* attrs;
537 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs));
538 EXPECT_EQ(3u, attrs->size());
539 base::DictionaryValue* size_attrs;
540 ASSERT_TRUE(attrs->GetDictionary(
541 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs));
542 ASSERT_TRUE(attrs->GetDictionary("mem_backend_size", &size_attrs));
543 ASSERT_TRUE(attrs->GetDictionary("mem_backend_max_size", &size_attrs));
544 }
545
546 TEST_F(DiskCacheBackendTest, SimpleCacheMemoryDump) {
547 simple_cache_mode_ = true;
548 BackendBasics();
549 base::trace_event::MemoryDumpArgs args = {
550 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND};
551 base::trace_event::ProcessMemoryDump pmd(nullptr, args);
552 base::trace_event::MemoryAllocatorDump* parent =
553 pmd.CreateAllocatorDump("net/url_request_context/main/0x123/http_cache");
554
555 ASSERT_LT(0u, cache_->DumpMemoryStats(&pmd, parent->absolute_name()));
556 EXPECT_EQ(2u, pmd.allocator_dumps().size());
557 const base::trace_event::MemoryAllocatorDump* sub_dump =
558 pmd.GetAllocatorDump(parent->absolute_name() + "/simple_backend");
559 ASSERT_NE(nullptr, sub_dump);
560
561 // Verify that the appropriate attributes were set.
562 std::unique_ptr<base::Value> raw_attrs =
563 sub_dump->attributes_for_testing()->ToBaseValue();
564 base::DictionaryValue* attrs;
565 ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs));
566 EXPECT_EQ(1u, attrs->size());
567 base::DictionaryValue* size_attrs;
568 ASSERT_TRUE(attrs->GetDictionary(
569 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs));
570 }
571
572 TEST_F(DiskCacheBackendTest, BlockFileCacheMemoryDump) {
573 // TODO(jkarlin): If the blockfile cache gets memory dump support, update
574 // this test.
575 BackendBasics();
576 base::trace_event::MemoryDumpArgs args = {
577 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND};
578 base::trace_event::ProcessMemoryDump pmd(nullptr, args);
579 base::trace_event::MemoryAllocatorDump* parent =
580 pmd.CreateAllocatorDump("net/url_request_context/main/0x123/http_cache");
581
582 ASSERT_EQ(0u, cache_->DumpMemoryStats(&pmd, parent->absolute_name()));
583 EXPECT_EQ(1u, pmd.allocator_dumps().size());
584 }
585
523 TEST_F(DiskCacheBackendTest, ExternalFiles) { 586 TEST_F(DiskCacheBackendTest, ExternalFiles) {
524 InitCache(); 587 InitCache();
525 // First, let's create a file on the folder. 588 // First, let's create a file on the folder.
526 base::FilePath filename = cache_path_.AppendASCII("f_000001"); 589 base::FilePath filename = cache_path_.AppendASCII("f_000001");
527 590
528 const int kSize = 50; 591 const int kSize = 50;
529 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 592 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
530 CacheTestFillBuffer(buffer1->data(), kSize, false); 593 CacheTestFillBuffer(buffer1->data(), kSize, false);
531 ASSERT_EQ(kSize, base::WriteFile(filename, buffer1->data(), kSize)); 594 ASSERT_EQ(kSize, base::WriteFile(filename, buffer1->data(), kSize));
532 595
(...skipping 3395 matching lines...) Expand 10 before | Expand all | Expand 10 after
3928 // because that would advance the cache directory mtime and invalidate the 3991 // because that would advance the cache directory mtime and invalidate the
3929 // index. 3992 // index.
3930 entry2->Doom(); 3993 entry2->Doom();
3931 entry2->Close(); 3994 entry2->Close();
3932 3995
3933 DisableFirstCleanup(); 3996 DisableFirstCleanup();
3934 InitCache(); 3997 InitCache();
3935 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, 3998 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED,
3936 simple_cache_impl_->index()->init_method()); 3999 simple_cache_impl_->index()->init_method());
3937 } 4000 }
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage_cache_unittest.cc ('k') | net/disk_cache/blockfile/backend_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698