2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1998-06-06 00:46:23 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1998-06-06 00:46:23 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-06-06 00:46:23 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-08-14 08:18:27 +04:00
|
|
|
#include "nscore.h"
|
1998-06-06 00:46:23 +04:00
|
|
|
#include "nsISizeOfHandler.h"
|
1999-08-31 07:04:37 +04:00
|
|
|
#include "nsIAtom.h"
|
1998-06-06 00:46:23 +04:00
|
|
|
#include "plhash.h"
|
|
|
|
|
|
|
|
class nsSizeOfHandler : public nsISizeOfHandler {
|
|
|
|
public:
|
|
|
|
nsSizeOfHandler();
|
1999-04-25 08:57:38 +04:00
|
|
|
virtual ~nsSizeOfHandler();
|
1998-06-06 00:46:23 +04:00
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsISizeOfHandler
|
1999-08-31 07:04:37 +04:00
|
|
|
NS_IMETHOD Init();
|
2000-05-13 02:11:45 +04:00
|
|
|
NS_IMETHOD RecordObject(void* aObject, PRBool* aResult);
|
|
|
|
NS_IMETHOD AddSize(nsIAtom* aKey, PRUint32 aSize);
|
|
|
|
NS_IMETHOD Report(nsISizeofReportFunc aFunc, void* aArg);
|
|
|
|
NS_IMETHOD GetTotals(PRUint32* aCountResult, PRUint32* aTotalSizeResult);
|
1998-06-06 00:46:23 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PRUint32 mTotalSize;
|
1999-08-31 07:04:37 +04:00
|
|
|
PRUint32 mTotalCount;
|
|
|
|
PLHashTable* mSizeTable;
|
|
|
|
PLHashTable* mObjectTable;
|
|
|
|
|
2000-05-13 02:11:45 +04:00
|
|
|
static PRIntn PR_CALLBACK RemoveObjectEntry(PLHashEntry* he, PRIntn i, void* arg);
|
|
|
|
static PRIntn PR_CALLBACK RemoveSizeEntry(PLHashEntry* he, PRIntn i, void* arg);
|
|
|
|
static PRIntn PR_CALLBACK ReportEntry(PLHashEntry* he, PRIntn i, void* arg);
|
1999-08-31 07:04:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class SizeOfDataStats {
|
|
|
|
public:
|
|
|
|
SizeOfDataStats(nsIAtom* aType, PRUint32 aSize);
|
|
|
|
~SizeOfDataStats();
|
|
|
|
|
|
|
|
void Update(PRUint32 aSize);
|
|
|
|
|
|
|
|
nsIAtom* mType; // type
|
|
|
|
PRUint32 mCount; // # of objects of this type
|
|
|
|
PRUint32 mTotalSize; // total size of all objects of this type
|
|
|
|
PRUint32 mMinSize; // smallest size for this type
|
|
|
|
PRUint32 mMaxSize; // largest size for this type
|
1998-06-06 00:46:23 +04:00
|
|
|
};
|
|
|
|
|
1999-08-31 07:04:37 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2001-01-28 02:06:33 +03:00
|
|
|
MOZ_DECL_CTOR_COUNTER(SizeOfDataStats)
|
1999-10-07 04:49:40 +04:00
|
|
|
|
1999-08-31 07:04:37 +04:00
|
|
|
SizeOfDataStats::SizeOfDataStats(nsIAtom* aType, PRUint32 aSize)
|
|
|
|
: mType(aType),
|
|
|
|
mCount(1),
|
|
|
|
mTotalSize(aSize),
|
|
|
|
mMinSize(aSize),
|
|
|
|
mMaxSize(aSize)
|
|
|
|
{
|
1999-10-07 04:49:40 +04:00
|
|
|
MOZ_COUNT_CTOR(SizeOfDataStats);
|
1999-08-31 07:04:37 +04:00
|
|
|
NS_IF_ADDREF(mType);
|
|
|
|
}
|
|
|
|
|
|
|
|
SizeOfDataStats::~SizeOfDataStats()
|
|
|
|
{
|
1999-10-07 04:49:40 +04:00
|
|
|
MOZ_COUNT_DTOR(SizeOfDataStats);
|
1999-08-31 07:04:37 +04:00
|
|
|
NS_IF_RELEASE(mType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SizeOfDataStats::Update(PRUint32 aSize)
|
|
|
|
{
|
|
|
|
mCount++;
|
|
|
|
if (aSize < mMinSize) mMinSize = aSize;
|
|
|
|
if (aSize > mMaxSize) mMaxSize = aSize;
|
|
|
|
mTotalSize += aSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2001-08-14 08:18:27 +04:00
|
|
|
#define POINTER_HASH_KEY(_atom) (PLHashNumber) NS_PTR_TO_INT32(_atom)
|
1999-08-31 07:04:37 +04:00
|
|
|
|
1998-06-06 00:46:23 +04:00
|
|
|
static PLHashNumber
|
1999-08-31 07:04:37 +04:00
|
|
|
PointerHashKey(nsIAtom* key)
|
1998-06-06 00:46:23 +04:00
|
|
|
{
|
1999-08-31 07:04:37 +04:00
|
|
|
return POINTER_HASH_KEY(key);
|
1998-06-06 00:46:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PRIntn
|
1999-08-31 07:04:37 +04:00
|
|
|
PointerCompareKeys(void* key1, void* key2)
|
1998-06-06 00:46:23 +04:00
|
|
|
{
|
|
|
|
return key1 == key2;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSizeOfHandler::nsSizeOfHandler()
|
1999-08-31 07:04:37 +04:00
|
|
|
: mTotalSize(0),
|
|
|
|
mTotalCount(0)
|
1998-06-06 00:46:23 +04:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mTotalSize = 0;
|
1999-08-31 07:04:37 +04:00
|
|
|
mSizeTable = PL_NewHashTable(32, (PLHashFunction) PointerHashKey,
|
|
|
|
(PLHashComparator) PointerCompareKeys,
|
|
|
|
(PLHashComparator) nsnull,
|
|
|
|
nsnull, nsnull);
|
|
|
|
mObjectTable = PL_NewHashTable(32, (PLHashFunction) PointerHashKey,
|
|
|
|
(PLHashComparator) PointerCompareKeys,
|
|
|
|
(PLHashComparator) nsnull,
|
|
|
|
nsnull, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIntn
|
|
|
|
nsSizeOfHandler::RemoveObjectEntry(PLHashEntry* he, PRIntn i, void* arg)
|
|
|
|
{
|
|
|
|
// Remove and free this entry and continue enumerating
|
|
|
|
return HT_ENUMERATE_REMOVE | HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIntn
|
|
|
|
nsSizeOfHandler::RemoveSizeEntry(PLHashEntry* he, PRIntn i, void* arg)
|
|
|
|
{
|
|
|
|
if (he->value) {
|
|
|
|
SizeOfDataStats* stats = (SizeOfDataStats*) he->value;
|
|
|
|
he->value = nsnull;
|
|
|
|
delete stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove and free this entry and continue enumerating
|
|
|
|
return HT_ENUMERATE_REMOVE | HT_ENUMERATE_NEXT;
|
1998-06-06 00:46:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSizeOfHandler::~nsSizeOfHandler()
|
|
|
|
{
|
1999-08-31 07:04:37 +04:00
|
|
|
if (nsnull != mObjectTable) {
|
|
|
|
PL_HashTableEnumerateEntries(mObjectTable, RemoveObjectEntry, 0);
|
|
|
|
PL_HashTableDestroy(mObjectTable);
|
|
|
|
}
|
|
|
|
if (nsnull != mSizeTable) {
|
|
|
|
PL_HashTableEnumerateEntries(mSizeTable, RemoveSizeEntry, 0);
|
|
|
|
PL_HashTableDestroy(mSizeTable);
|
1998-06-06 00:46:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-23 14:14:16 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsSizeOfHandler, nsISizeOfHandler)
|
1998-06-06 00:46:23 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-08-31 07:04:37 +04:00
|
|
|
nsSizeOfHandler::Init()
|
1998-06-06 00:46:23 +04:00
|
|
|
{
|
1999-08-31 07:04:37 +04:00
|
|
|
if (mObjectTable) {
|
|
|
|
PL_HashTableEnumerateEntries(mObjectTable, RemoveObjectEntry, 0);
|
|
|
|
}
|
|
|
|
if (mSizeTable) {
|
|
|
|
PL_HashTableEnumerateEntries(mSizeTable, RemoveSizeEntry, 0);
|
|
|
|
}
|
|
|
|
mTotalCount = 0;
|
|
|
|
mTotalSize = 0;
|
1998-06-06 00:46:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-31 07:04:37 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSizeOfHandler::RecordObject(void* aAddr, PRBool* aResult)
|
1998-06-06 00:46:23 +04:00
|
|
|
{
|
1999-08-31 07:04:37 +04:00
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1998-06-06 00:46:23 +04:00
|
|
|
}
|
1999-08-31 07:04:37 +04:00
|
|
|
|
|
|
|
PRBool result = PR_TRUE;
|
|
|
|
if (mObjectTable && aAddr) {
|
|
|
|
PLHashNumber hashCode = POINTER_HASH_KEY(aAddr);
|
|
|
|
PLHashEntry** hep = PL_HashTableRawLookup(mObjectTable, hashCode, aAddr);
|
1998-06-06 00:46:23 +04:00
|
|
|
PLHashEntry* he = *hep;
|
1999-08-31 07:04:37 +04:00
|
|
|
if (!he) {
|
|
|
|
// We've never seen it before. Add it to the table
|
|
|
|
(void) PL_HashTableRawAdd(mObjectTable, hep, hashCode, aAddr, aAddr);
|
|
|
|
result = PR_FALSE;
|
1998-06-06 00:46:23 +04:00
|
|
|
}
|
1999-08-31 07:04:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = result;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSizeOfHandler::AddSize(nsIAtom* aType, PRUint32 aSize)
|
|
|
|
{
|
|
|
|
PLHashNumber hashCode = POINTER_HASH_KEY(aType);
|
|
|
|
PLHashEntry** hep = PL_HashTableRawLookup(mSizeTable, hashCode, aType);
|
|
|
|
PLHashEntry* he = *hep;
|
|
|
|
if (he) {
|
|
|
|
// Stats already exist
|
|
|
|
SizeOfDataStats* stats = (SizeOfDataStats*) he->value;
|
|
|
|
stats->Update(aSize);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Make new stats for the new frame type
|
|
|
|
SizeOfDataStats* newStats = new SizeOfDataStats(aType, aSize);
|
|
|
|
PL_HashTableRawAdd(mSizeTable, hep, hashCode, aType, newStats);
|
|
|
|
}
|
|
|
|
mTotalCount++;
|
|
|
|
mTotalSize += aSize;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ReportArgs {
|
|
|
|
nsISizeOfHandler* mHandler;
|
|
|
|
nsISizeofReportFunc mFunc;
|
|
|
|
void* mArg;
|
|
|
|
};
|
|
|
|
|
|
|
|
PRIntn
|
|
|
|
nsSizeOfHandler::ReportEntry(PLHashEntry* he, PRIntn i, void* arg)
|
|
|
|
{
|
|
|
|
ReportArgs* ra = (ReportArgs*) arg;
|
|
|
|
if (he && ra && ra->mFunc) {
|
|
|
|
SizeOfDataStats* stats = (SizeOfDataStats*) he->value;
|
|
|
|
if (stats) {
|
|
|
|
(*ra->mFunc)(ra->mHandler, stats->mType, stats->mCount,
|
|
|
|
stats->mTotalSize, stats->mMinSize, stats->mMaxSize,
|
|
|
|
ra->mArg);
|
1998-06-06 00:46:23 +04:00
|
|
|
}
|
|
|
|
}
|
1999-10-07 04:49:40 +04:00
|
|
|
return HT_ENUMERATE_NEXT;
|
1998-06-06 00:46:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-08-31 07:04:37 +04:00
|
|
|
nsSizeOfHandler::Report(nsISizeofReportFunc aFunc, void* aArg)
|
1998-06-06 00:46:23 +04:00
|
|
|
{
|
1999-08-31 07:04:37 +04:00
|
|
|
ReportArgs ra;
|
|
|
|
ra.mHandler = this;
|
|
|
|
ra.mFunc = aFunc;
|
|
|
|
ra.mArg = aArg;
|
|
|
|
PL_HashTableEnumerateEntries(mSizeTable, ReportEntry, (void*) &ra);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSizeOfHandler::GetTotals(PRUint32* aTotalCountResult,
|
|
|
|
PRUint32* aTotalSizeResult)
|
|
|
|
{
|
|
|
|
if (!aTotalCountResult || !aTotalSizeResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
*aTotalCountResult = mTotalCount;
|
|
|
|
*aTotalSizeResult = mTotalSize;
|
1998-06-06 00:46:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_COM nsresult
|
1998-06-06 00:46:23 +04:00
|
|
|
NS_NewSizeOfHandler(nsISizeOfHandler** aInstancePtrResult)
|
|
|
|
{
|
1999-08-31 07:04:37 +04:00
|
|
|
if (!aInstancePtrResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
1998-06-06 00:46:23 +04:00
|
|
|
nsISizeOfHandler *it = new nsSizeOfHandler();
|
|
|
|
if (it == nsnull) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
1999-08-23 14:14:16 +04:00
|
|
|
return it->QueryInterface(NS_GET_IID(nsISizeOfHandler), (void **) aInstancePtrResult);
|
1998-06-06 00:46:23 +04:00
|
|
|
}
|