Bug 986302 - Add memory reporting for HPACK tables. r=hurley r=njn

This commit is contained in:
Nate Hughes 2015-07-17 14:38:10 -07:00
parent 7e340e3a68
commit 17ce4e8545
4 changed files with 113 additions and 1 deletions

View File

@ -16,6 +16,7 @@
#include "Http2Compression.h"
#include "Http2HuffmanIncoming.h"
#include "Http2HuffmanOutgoing.h"
#include "mozilla/StaticPtr.h"
extern PRThread *gSocketThread;
@ -24,12 +25,70 @@ namespace net {
static nsDeque *gStaticHeaders = nullptr;
class HpackStaticTableReporter final : public nsIMemoryReporter
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
HpackStaticTableReporter() {}
NS_IMETHODIMP
CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
bool aAnonymize)
{
return MOZ_COLLECT_REPORT(
"explicit/network/hpack/static-table", KIND_HEAP, UNITS_BYTES,
gStaticHeaders->SizeOfIncludingThis(MallocSizeOf),
"Memory usage of HPACK static table.");
}
private:
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
~HpackStaticTableReporter() {}
};
NS_IMPL_ISUPPORTS(HpackStaticTableReporter, nsIMemoryReporter)
class HpackDynamicTableReporter final : public nsIMemoryReporter
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
explicit HpackDynamicTableReporter(Http2BaseCompressor* aCompressor)
: mCompressor(aCompressor)
{}
NS_IMETHODIMP
CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
bool aAnonymize)
{
return MOZ_COLLECT_REPORT(
"explicit/network/hpack/dynamic-tables", KIND_HEAP, UNITS_BYTES,
mCompressor->SizeOfExcludingThis(MallocSizeOf),
"Aggregate memory usage of HPACK dynamic tables.");
}
private:
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
~HpackDynamicTableReporter() {}
Http2BaseCompressor* mCompressor;
};
NS_IMPL_ISUPPORTS(HpackDynamicTableReporter, nsIMemoryReporter)
StaticRefPtr<HpackStaticTableReporter> gStaticReporter;
void
Http2CompressionCleanup()
{
// this happens after the socket thread has been destroyed
delete gStaticHeaders;
gStaticHeaders = nullptr;
UnregisterStrongMemoryReporter(gStaticReporter);
gStaticReporter = nullptr;
}
static void
@ -51,6 +110,8 @@ InitializeStaticHeaders()
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread);
if (!gStaticHeaders) {
gStaticHeaders = new nsDeque();
gStaticReporter = new HpackStaticTableReporter();
RegisterStrongMemoryReporter(gStaticReporter);
AddStaticElement(NS_LITERAL_CSTRING(":authority"));
AddStaticElement(NS_LITERAL_CSTRING(":method"), NS_LITERAL_CSTRING("GET"));
AddStaticElement(NS_LITERAL_CSTRING(":method"), NS_LITERAL_CSTRING("POST"));
@ -115,6 +176,17 @@ InitializeStaticHeaders()
}
}
size_t nvPair::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
{
return mName.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
mValue.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
}
size_t nvPair::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
{
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
nvFIFO::nvFIFO()
: mByteCount(0)
, mTable()
@ -203,6 +275,13 @@ Http2BaseCompressor::Http2BaseCompressor()
: mOutput(nullptr)
, mMaxBuffer(kDefaultMaxBuffer)
{
mDynamicReporter = new HpackDynamicTableReporter(this);
RegisterStrongMemoryReporter(mDynamicReporter);
}
Http2BaseCompressor::~Http2BaseCompressor()
{
UnregisterStrongMemoryReporter(mDynamicReporter);
}
void
@ -211,6 +290,16 @@ Http2BaseCompressor::ClearHeaderTable()
mHeaderTable.Clear();
}
size_t
Http2BaseCompressor::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
size_t size = 0;
for (uint32_t i = mHeaderTable.StaticLength(); i < mHeaderTable.Length(); ++i) {
size += mHeaderTable[i]->SizeOfIncludingThis(aMallocSizeOf);
}
return size;
}
void
Http2BaseCompressor::MakeRoom(uint32_t amount, const char *direction)
{

View File

@ -12,6 +12,7 @@
#include "mozilla/Attributes.h"
#include "nsDeque.h"
#include "nsString.h"
#include "nsIMemoryReporter.h"
namespace mozilla {
namespace net {
@ -29,6 +30,8 @@ nvPair(const nsACString &name, const nsACString &value)
{ }
uint32_t Size() const { return mName.Length() + mValue.Length() + 32; }
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
nsCString mName;
nsCString mValue;
@ -54,11 +57,14 @@ private:
nsDeque mTable;
};
class HpackDynamicTableReporter;
class Http2BaseCompressor
{
public:
Http2BaseCompressor();
virtual ~Http2BaseCompressor() { };
virtual ~Http2BaseCompressor();
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
protected:
const static uint32_t kDefaultMaxBuffer = 4096;
@ -71,6 +77,9 @@ protected:
nvFIFO mHeaderTable;
uint32_t mMaxBuffer;
private:
nsRefPtr<HpackDynamicTableReporter> mDynamicReporter;
};
class Http2Compressor;

View File

@ -464,6 +464,9 @@ XPCOM_API(nsresult) RegisterStrongMemoryReporter(nsIMemoryReporter* aReporter);
// to this reporter.
XPCOM_API(nsresult) RegisterWeakMemoryReporter(nsIMemoryReporter* aReporter);
// Unregister a strong memory reporter.
XPCOM_API(nsresult) UnregisterStrongMemoryReporter(nsIMemoryReporter* aReporter);
// Unregister a weak memory reporter.
XPCOM_API(nsresult) UnregisterWeakMemoryReporter(nsIMemoryReporter* aReporter);

View File

@ -2293,6 +2293,17 @@ RegisterWeakMemoryReporter(nsIMemoryReporter* aReporter)
return mgr->RegisterWeakReporter(aReporter);
}
nsresult
UnregisterStrongMemoryReporter(nsIMemoryReporter* aReporter)
{
nsCOMPtr<nsIMemoryReporterManager> mgr =
do_GetService("@mozilla.org/memory-reporter-manager;1");
if (!mgr) {
return NS_ERROR_FAILURE;
}
return mgr->UnregisterStrongReporter(aReporter);
}
nsresult
UnregisterWeakMemoryReporter(nsIMemoryReporter* aReporter)
{