зеркало из https://github.com/mozilla/pjs.git
Bug 698326 - Add memory reporter for the url-classifier. r=njn,jlebar
This commit is contained in:
Родитель
549563e7fe
Коммит
04e05dc4b9
|
@ -160,9 +160,9 @@
|
|||
#define NS_TYPEAHEADFIND_CID \
|
||||
{ 0xe7f70966, 0x9a37, 0x48d7, { 0x8a, 0xeb, 0x35, 0x99, 0x8f, 0x31, 0x09, 0x0e} }
|
||||
|
||||
// {42ef1d52-3351-4973-98f8-d18f089bccfa}
|
||||
// {5edc87c2-6960-44e5-8431-bdfbb56f6aff}
|
||||
#define NS_URLCLASSIFIERPREFIXSET_CID \
|
||||
{ 0x42ef1d52, 0x3351, 0x4973, { 0x98, 0xf8, 0xd1, 0x8f, 0x08, 0x9b, 0xcc, 0xfa} }
|
||||
{ 0x5edc87c2, 0x6960, 0x44e5, { 0x84, 0x31, 0xbd, 0xfb, 0xb5, 0x6f, 0x6a, 0xff} }
|
||||
|
||||
// {5eb7c3c1-ec1f-4007-87cc-eefb37d68ce6}
|
||||
#define NS_URLCLASSIFIERDBSERVICE_CID \
|
||||
|
|
|
@ -41,7 +41,7 @@
|
|||
|
||||
interface nsIArray;
|
||||
|
||||
[scriptable, uuid(42ef1d52-3351-4973-98f8-d18f089bccfa)]
|
||||
[scriptable, uuid(5edc87c2-6960-44e5-8431-bdfbb56f6aff)]
|
||||
interface nsIUrlClassifierPrefixSet : nsISupports
|
||||
{
|
||||
void setPrefixes([const, array, size_is(aLength)] in unsigned long aPrefixes,
|
||||
|
@ -51,7 +51,7 @@ interface nsIUrlClassifierPrefixSet : nsISupports
|
|||
boolean contains(in unsigned long aPrefix);
|
||||
boolean probe(in unsigned long aPrefix, in unsigned long aKey,
|
||||
inout boolean aReady);
|
||||
PRUint32 estimateSize();
|
||||
PRUint32 sizeOfIncludingThis(in boolean aCountMe);
|
||||
PRUint32 getKey();
|
||||
boolean isEmpty();
|
||||
void loadFromFile(in nsIFile aFile);
|
||||
|
|
|
@ -3653,7 +3653,7 @@ nsUrlClassifierDBServiceWorker::LoadPrefixSet(nsCOMPtr<nsIFile> & aFile)
|
|||
|
||||
#ifdef DEBUG
|
||||
PRUint32 size = 0;
|
||||
rv = mPrefixSet->EstimateSize(&size);
|
||||
rv = mPrefixSet->SizeOfIncludingThis(true, &size);
|
||||
LOG(("SB tree done, size = %d bytes\n", size));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
#endif
|
||||
|
|
|
@ -42,6 +42,7 @@
|
|||
#include "nsCOMPtr.h"
|
||||
#include "nsDebug.h"
|
||||
#include "nsTArray.h"
|
||||
#include "nsString.h"
|
||||
#include "nsUrlClassifierPrefixSet.h"
|
||||
#include "nsIUrlClassifierPrefixSet.h"
|
||||
#include "nsIRandomGenerator.h"
|
||||
|
@ -67,6 +68,78 @@ static const PRLogModuleInfo *gUrlClassifierPrefixSetLog = nsnull;
|
|||
#define LOG_ENABLED() (false)
|
||||
#endif
|
||||
|
||||
class nsPrefixSetReporter : public nsIMemoryReporter
|
||||
{
|
||||
public:
|
||||
nsPrefixSetReporter(nsUrlClassifierPrefixSet * aParent, const nsACString & aName);
|
||||
virtual ~nsPrefixSetReporter() {};
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIMEMORYREPORTER
|
||||
|
||||
private:
|
||||
nsCString mPath;
|
||||
nsUrlClassifierPrefixSet * mParent;
|
||||
};
|
||||
|
||||
NS_IMPL_THREADSAFE_ISUPPORTS1(nsPrefixSetReporter, nsIMemoryReporter)
|
||||
|
||||
nsPrefixSetReporter::nsPrefixSetReporter(nsUrlClassifierPrefixSet * aParent,
|
||||
const nsACString & aName)
|
||||
: mParent(aParent)
|
||||
{
|
||||
mPath.Assign(NS_LITERAL_CSTRING("explicit/storage/prefixset"));
|
||||
if (!aName.IsEmpty()) {
|
||||
mPath.Append("/");
|
||||
mPath.Append(aName);
|
||||
}
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPrefixSetReporter::GetProcess(nsACString & aProcess)
|
||||
{
|
||||
aProcess.Truncate();
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPrefixSetReporter::GetPath(nsACString & aPath)
|
||||
{
|
||||
aPath.Assign(mPath);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPrefixSetReporter::GetKind(PRInt32 * aKind)
|
||||
{
|
||||
*aKind = nsIMemoryReporter::KIND_HEAP;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPrefixSetReporter::GetUnits(PRInt32 * aUnits)
|
||||
{
|
||||
*aUnits = nsIMemoryReporter::UNITS_BYTES;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPrefixSetReporter::GetAmount(PRInt64 * aAmount)
|
||||
{
|
||||
PRUint32 size;
|
||||
nsresult rv = mParent->SizeOfIncludingThis(true, &size);
|
||||
*aAmount = size;
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPrefixSetReporter::GetDescription(nsACString & aDescription)
|
||||
{
|
||||
aDescription.Assign(NS_LITERAL_CSTRING("Memory used by a PrefixSet for "
|
||||
"UrlClassifier, in bytes."));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMPL_THREADSAFE_ISUPPORTS1(nsUrlClassifierPrefixSet, nsIUrlClassifierPrefixSet)
|
||||
|
||||
nsUrlClassifierPrefixSet::nsUrlClassifierPrefixSet()
|
||||
|
@ -84,6 +157,14 @@ nsUrlClassifierPrefixSet::nsUrlClassifierPrefixSet()
|
|||
if (NS_FAILED(rv)) {
|
||||
LOG(("Failed to initialize PrefixSet"));
|
||||
}
|
||||
|
||||
mReporter = new nsPrefixSetReporter(this, NS_LITERAL_CSTRING("all"));
|
||||
NS_RegisterMemoryReporter(mReporter);
|
||||
}
|
||||
|
||||
nsUrlClassifierPrefixSet::~nsUrlClassifierPrefixSet()
|
||||
{
|
||||
NS_UnregisterMemoryReporter(mReporter);
|
||||
}
|
||||
|
||||
nsresult
|
||||
|
@ -199,6 +280,8 @@ PRUint32 nsUrlClassifierPrefixSet::BinSearch(PRUint32 start,
|
|||
NS_IMETHODIMP
|
||||
nsUrlClassifierPrefixSet::Contains(PRUint32 aPrefix, bool * aFound)
|
||||
{
|
||||
mPrefixSetLock.AssertCurrentThreadOwns();
|
||||
|
||||
*aFound = false;
|
||||
|
||||
if (!mHasPrefixes) {
|
||||
|
@ -244,15 +327,18 @@ nsUrlClassifierPrefixSet::Contains(PRUint32 aPrefix, bool * aFound)
|
|||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsUrlClassifierPrefixSet::EstimateSize(PRUint32 * aSize)
|
||||
nsUrlClassifierPrefixSet::SizeOfIncludingThis(bool aCountMe, PRUint32 * aSize)
|
||||
{
|
||||
MutexAutoLock lock(mPrefixSetLock);
|
||||
*aSize = sizeof(bool);
|
||||
if (mHasPrefixes) {
|
||||
*aSize += sizeof(PRUint16) * mDeltas.Length();
|
||||
*aSize += sizeof(PRUint32) * mIndexPrefixes.Length();
|
||||
*aSize += sizeof(PRUint32) * mIndexStarts.Length();
|
||||
if (aCountMe) {
|
||||
size_t usable = moz_malloc_usable_size(this);
|
||||
*aSize = (PRUint32)(usable ? usable : sizeof(*this));
|
||||
} else {
|
||||
*aSize = 0;
|
||||
}
|
||||
*aSize += mDeltas.SizeOf();
|
||||
*aSize += mIndexPrefixes.SizeOf();
|
||||
*aSize += mIndexStarts.SizeOf();
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -45,16 +45,19 @@
|
|||
#include "nsID.h"
|
||||
#include "nsIFile.h"
|
||||
#include "nsIUrlClassifierPrefixSet.h"
|
||||
#include "nsIMemoryReporter.h"
|
||||
#include "nsToolkitCompsCID.h"
|
||||
#include "mozilla/Mutex.h"
|
||||
#include "mozilla/CondVar.h"
|
||||
#include "mozilla/FileUtils.h"
|
||||
|
||||
class nsPrefixSetReporter;
|
||||
|
||||
class nsUrlClassifierPrefixSet : public nsIUrlClassifierPrefixSet
|
||||
{
|
||||
public:
|
||||
nsUrlClassifierPrefixSet();
|
||||
virtual ~nsUrlClassifierPrefixSet() {};
|
||||
virtual ~nsUrlClassifierPrefixSet();
|
||||
|
||||
// Can send an empty Array to clean the tree
|
||||
NS_IMETHOD SetPrefixes(const PRUint32* aArray, PRUint32 aLength);
|
||||
|
@ -69,7 +72,7 @@ public:
|
|||
NS_IMETHOD Probe(PRUint32 aPrefix, PRUint32 aKey, bool* aReady, bool* aFound);
|
||||
// Return the estimated size of the set on disk and in memory,
|
||||
// in bytes
|
||||
NS_IMETHOD EstimateSize(PRUint32* aSize);
|
||||
NS_IMETHOD SizeOfIncludingThis(bool aCountMe, PRUint32* aSize);
|
||||
NS_IMETHOD IsEmpty(bool * aEmpty);
|
||||
NS_IMETHOD LoadFromFile(nsIFile* aFile);
|
||||
NS_IMETHOD StoreToFile(nsIFile* aFile);
|
||||
|
@ -85,6 +88,7 @@ protected:
|
|||
|
||||
mozilla::Mutex mPrefixSetLock;
|
||||
mozilla::CondVar mSetIsReady;
|
||||
nsRefPtr<nsPrefixSetReporter> mReporter;
|
||||
|
||||
PRUint32 BinSearch(PRUint32 start, PRUint32 end, PRUint32 target);
|
||||
nsresult LoadFromFd(mozilla::AutoFDClose & fileFd);
|
||||
|
@ -103,6 +107,7 @@ protected:
|
|||
nsTArray<PRUint32> mIndexStarts;
|
||||
// array containing deltas from indices.
|
||||
nsTArray<PRUint16> mDeltas;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -28,6 +28,12 @@ function arrContains(arr, target) {
|
|||
return (!(i < 0 || i >= arr.length) && arr[i] == target);
|
||||
}
|
||||
|
||||
function wrappedProbe(pset, prefix) {
|
||||
let key = pset.getKey();
|
||||
let dummy = {};
|
||||
return pset.probe(prefix, key, dummy);
|
||||
};
|
||||
|
||||
// doRandomLookups: we use this to test for false membership with random input
|
||||
// over the range of prefixes (unsigned 32-bits integers).
|
||||
// pset: a nsIUrlClassifierPrefixSet to test.
|
||||
|
@ -39,7 +45,7 @@ function doRandomLookups(pset, prefixes, N) {
|
|||
while (arrContains(prefixes, randInt))
|
||||
randInt = Math.floor(Math.random() * Math.pow(2, 32));
|
||||
|
||||
do_check_false(pset.contains(randInt));
|
||||
do_check_false(wrappedProbe(pset, randInt));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,7 +56,7 @@ function doExpectedLookups(pset, prefixes, N) {
|
|||
for (let i = 0; i < N; i++) {
|
||||
prefixes.forEach(function (x) {
|
||||
dump("Checking " + x + "\n");
|
||||
do_check_true(pset.contains(x));
|
||||
do_check_true(wrappedProbe(pset, x));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -63,11 +69,11 @@ function testBasicPset() {
|
|||
let prefixes = [2,100,50,2000,78000,1593203];
|
||||
pset.setPrefixes(prefixes, prefixes.length);
|
||||
|
||||
do_check_true(pset.contains(100));
|
||||
do_check_false(pset.contains(100000));
|
||||
do_check_true(pset.contains(1593203));
|
||||
do_check_false(pset.contains(999));
|
||||
do_check_false(pset.contains(0));
|
||||
do_check_true(wrappedProbe(pset, 100));
|
||||
do_check_false(wrappedProbe(pset, 100000));
|
||||
do_check_true(wrappedProbe(pset, 1593203));
|
||||
do_check_false(wrappedProbe(pset, 999));
|
||||
do_check_false(wrappedProbe(pset, 0));
|
||||
}
|
||||
|
||||
function testDuplicates() {
|
||||
|
@ -76,12 +82,12 @@ function testDuplicates() {
|
|||
let prefixes = [1,1,2,2,2,3,3,3,3,3,3,5,6,6,7,7,9,9,9];
|
||||
pset.setPrefixes(prefixes, prefixes.length);
|
||||
|
||||
do_check_true(pset.contains(1));
|
||||
do_check_true(pset.contains(2));
|
||||
do_check_true(pset.contains(5));
|
||||
do_check_true(pset.contains(9));
|
||||
do_check_false(pset.contains(4));
|
||||
do_check_false(pset.contains(8));
|
||||
do_check_true(wrappedProbe(pset, 1));
|
||||
do_check_true(wrappedProbe(pset, 2));
|
||||
do_check_true(wrappedProbe(pset, 5));
|
||||
do_check_true(wrappedProbe(pset, 9));
|
||||
do_check_false(wrappedProbe(pset, 4));
|
||||
do_check_false(wrappedProbe(pset, 8));
|
||||
}
|
||||
|
||||
function testSimplePset() {
|
||||
|
@ -114,7 +120,7 @@ function testReSetPrefixes() {
|
|||
|
||||
doExpectedLookups(pset, secondPrefixes, 1);
|
||||
for (let i = 0; i < prefixes.length; i++) {
|
||||
do_check_false(pset.contains(prefixes[i]));
|
||||
do_check_false(wrappedProbe(pset, prefixes[i]));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,12 +148,12 @@ function testTinySet() {
|
|||
let prefixes = [1];
|
||||
pset.setPrefixes(prefixes, prefixes.length);
|
||||
|
||||
do_check_true(pset.contains(1));
|
||||
do_check_false(pset.contains(100000));
|
||||
do_check_true(wrappedProbe(pset, 1));
|
||||
do_check_false(wrappedProbe(pset, 100000));
|
||||
|
||||
prefixes = [];
|
||||
pset.setPrefixes(prefixes, prefixes.length);
|
||||
do_check_false(pset.contains(1));
|
||||
do_check_false(wrappedProbe(pset, 1));
|
||||
}
|
||||
|
||||
let tests = [testBasicPset,
|
||||
|
|
Загрузка…
Ссылка в новой задаче