Commit d701b6e5 authored by xunjieli's avatar xunjieli Committed by Commit bot

Instrument SdchManager using MemoryDumpProvider

This CL instruments SdchManager using MemoryDumpProvider.
Even though SDCH might be unshipped (see blink-dev),
it is useful to get an idea of how much memory dictionaries
cost.

BUG=669108

Review-Url: https://codereview.chromium.org/2541073003
Cr-Commit-Position: refs/heads/master@{#441485}
parent 04cac696
......@@ -13,7 +13,10 @@
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/default_clock.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/values.h"
#include "crypto/sha2.h"
#include "net/base/parse_number.h"
......@@ -324,6 +327,36 @@ void SdchManager::RemoveObserver(SdchObserver* observer) {
observers_.RemoveObserver(observer);
}
void SdchManager::DumpMemoryStats(
base::trace_event::ProcessMemoryDump* pmd,
const std::string& parent_dump_absolute_name) const {
// If there are no dictionaries stored, return early without creating a new
// MemoryAllocatorDump.
size_t total_count = dictionaries_.size();
if (total_count == 0)
return;
std::string name = base::StringPrintf("net/sdch_manager_%p", this);
base::trace_event::MemoryAllocatorDump* dump = pmd->GetAllocatorDump(name);
if (dump == nullptr) {
dump = pmd->CreateAllocatorDump(name);
size_t total_size = 0;
for (const auto& dictionary : dictionaries_) {
total_size += dictionary.second->data.text().size();
}
dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
base::trace_event::MemoryAllocatorDump::kUnitsBytes,
total_size);
dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount,
base::trace_event::MemoryAllocatorDump::kUnitsObjects,
total_count);
}
// Create an empty row under parent's dump so size can be attributed correctly
// if |this| is shared between URLRequestContexts.
base::trace_event::MemoryAllocatorDump* empty_row_dump =
pmd->CreateAllocatorDump(parent_dump_absolute_name + "/sdch_manager");
pmd->AddOwnershipEdge(empty_row_dump->guid(), dump->guid());
}
SdchProblemCode SdchManager::AddSdchDictionary(
const std::string& dictionary_text,
const GURL& dictionary_url,
......
......@@ -32,6 +32,10 @@ class GURL;
namespace base {
class Value;
namespace trace_event {
class ProcessMemoryDump;
}
}
namespace net {
......@@ -195,6 +199,11 @@ class NET_EXPORT SdchManager {
void AddObserver(SdchObserver* observer);
void RemoveObserver(SdchObserver* observer);
// Dumps memory allocation stats. |parent_dump_absolute_name| is the name
// used by the parent MemoryAllocatorDump in the memory dump hierarchy.
void DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd,
const std::string& parent_dump_absolute_name) const;
// Logs an SDCH failure to UMA and |netlog|.
static void LogSdchProblem(NetLogWithSource netlog, SdchProblemCode problem);
......
......@@ -11,8 +11,12 @@
#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/test/simple_test_clock.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event_argument.h"
#include "net/base/sdch_observer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
......@@ -636,4 +640,58 @@ TEST_F(SdchManagerTest, AddRemoveNotifications) {
sdch_manager()->RemoveObserver(&observer);
}
TEST_F(SdchManagerTest, DumpMemoryStats) {
MockSdchObserver observer;
sdch_manager()->AddObserver(&observer);
std::string dictionary_domain("x.y.z.google.com");
GURL target_gurl("http://" + dictionary_domain);
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
std::string client_hash;
std::string server_hash;
SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash);
EXPECT_TRUE(AddSdchDictionary(dictionary_text, target_gurl));
EXPECT_EQ(1, observer.dictionary_added_notifications());
EXPECT_EQ(target_gurl, observer.last_dictionary_url());
EXPECT_EQ(server_hash, observer.last_server_hash());
base::trace_event::MemoryDumpArgs dump_args = {
base::trace_event::MemoryDumpLevelOfDetail::DETAILED};
std::unique_ptr<base::trace_event::ProcessMemoryDump> pmd(
new base::trace_event::ProcessMemoryDump(nullptr, dump_args));
base::trace_event::MemoryAllocatorDump* parent =
pmd->CreateAllocatorDump("parent");
sdch_manager()->DumpMemoryStats(pmd.get(), parent->absolute_name());
const base::trace_event::MemoryAllocatorDump* sub_dump =
pmd->GetAllocatorDump("parent/sdch_manager");
ASSERT_NE(nullptr, sub_dump);
const base::trace_event::MemoryAllocatorDump* dump = pmd->GetAllocatorDump(
base::StringPrintf("net/sdch_manager_%p", sdch_manager()));
std::unique_ptr<base::Value> raw_attrs =
dump->attributes_for_testing()->ToBaseValue();
base::DictionaryValue* attrs;
ASSERT_TRUE(raw_attrs->GetAsDictionary(&attrs));
base::DictionaryValue* size_attrs;
ASSERT_TRUE(attrs->GetDictionary(
base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs));
size_t offset = dictionary_text.find("\n\n") + 2;
std::string size;
ASSERT_TRUE(size_attrs->GetString("value", &size));
int actual_size;
ASSERT_TRUE(base::HexStringToInt(size, &actual_size));
EXPECT_EQ(dictionary_text.size() - offset, static_cast<size_t>(actual_size));
base::DictionaryValue* count_attrs;
ASSERT_TRUE(attrs->GetDictionary(
base::trace_event::MemoryAllocatorDump::kNameObjectCount, &count_attrs));
std::string count;
ASSERT_TRUE(count_attrs->GetString("value", &count));
// One dictionary.
EXPECT_EQ("1", count);
sdch_manager()->RemoveObserver(&observer);
}
} // namespace net
......@@ -13,6 +13,7 @@
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/process_memory_dump.h"
#include "net/base/sdch_manager.h"
#include "net/cookies/cookie_store.h"
#include "net/dns/host_resolver.h"
#include "net/http/http_transaction_factory.h"
......@@ -138,6 +139,8 @@ bool URLRequestContext::OnMemoryDump(
network_session->DumpMemoryStats(pmd, dump->absolute_name());
}
SSLClientSocketImpl::DumpSSLClientSessionMemoryStats(pmd);
if (sdch_manager_)
sdch_manager_->DumpMemoryStats(pmd, dump->absolute_name());
return true;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment