Bug 1567437 - Add memory reporter for hyphenation data. r=njn

Differential Revision: https://phabricator.services.mozilla.com/D38721

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jonathan Kew 2019-07-23 09:11:00 +00:00
parent 92b529251c
commit 23aba052e2
3 changed files with 78 additions and 13 deletions

View File

@ -3,19 +3,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Simple replacement for hnjalloc.h from libhyphen-2.x, to use moz_x* memory
* allocation functions. Note that the hyphen.c code does *NOT* check for
* NULL from memory (re)allocation, so it is essential that we use the
* "infallible" moz_x* variants here.
*/
#include "mozilla/mozalloc.h"
#define hnj_malloc(size) moz_xmalloc(size)
#define hnj_realloc(p, size) moz_xrealloc(p, size)
#define hnj_free(p) free(p)
/*
* To enable us to load hyphenation dictionaries from arbitrary resource URIs,
* not just through file paths using stdio, we override the (few) stdio APIs
@ -40,6 +27,10 @@ typedef struct hnjFile_ hnjFile;
extern "C" {
#endif
void* hnj_malloc(size_t size);
void* hnj_realloc(void* ptr, size_t size);
void hnj_free(void* ptr);
hnjFile* hnjFopen(const char* aURISpec, const char* aMode);
int hnjFclose(hnjFile* f);

View File

@ -14,6 +14,7 @@
#include "nsDirectoryServiceDefs.h"
#include "nsNetUtil.h"
#include "nsUnicharUtils.h"
#include "mozilla/CountingAllocatorBase.h"
#include "mozilla/Preferences.h"
#include "nsZipArchive.h"
#include "mozilla/Services.h"
@ -28,6 +29,58 @@ using namespace mozilla;
static const char kIntlHyphenationAliasPrefix[] = "intl.hyphenation-alias.";
static const char kMemoryPressureNotification[] = "memory-pressure";
class HyphenReporter final : public nsIMemoryReporter,
public CountingAllocatorBase<HyphenReporter> {
private:
~HyphenReporter() = default;
public:
NS_DECL_ISUPPORTS
static void* Malloc(long aSize) { return CountingMalloc(aSize); }
static void Free(void* aPtr) { return CountingFree(aPtr); }
static void* Realloc(void* aPtr, long aNewSize) {
return CountingRealloc(aPtr, aNewSize);
}
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize) override {
size_t total = MemoryAllocated();
if (nsHyphenationManager::Instance()) {
total += nsHyphenationManager::Instance()->SizeOfIncludingThis(
moz_malloc_size_of);
}
MOZ_COLLECT_REPORT("explicit/hyphenation", KIND_HEAP, UNITS_BYTES, total,
"Memory used by hyphenation data.");
return NS_OK;
}
};
NS_IMPL_ISUPPORTS(HyphenReporter, nsIMemoryReporter)
template <>
CountingAllocatorBase<HyphenReporter>::AmountType
CountingAllocatorBase<HyphenReporter>::sAmount(0);
/**
* Allocation wrappers to track the amount of memory allocated by libhyphen.
*/
extern "C" {
void* hnj_malloc(size_t aSize);
void* hnj_realloc(void* aPtr, size_t aSize);
void hnj_free(void* aPtr);
};
void* hnj_malloc(size_t aSize) { return HyphenReporter::Malloc(aSize); }
void* hnj_realloc(void* aPtr, size_t aSize) {
return HyphenReporter::Realloc(aPtr, aSize);
}
void hnj_free(void* aPtr) { HyphenReporter::Free(aPtr); }
nsHyphenationManager* nsHyphenationManager::sInstance = nullptr;
NS_IMPL_ISUPPORTS(nsHyphenationManager::MemoryPressureObserver, nsIObserver)
@ -57,6 +110,8 @@ nsHyphenationManager* nsHyphenationManager::Instance() {
obs->AddObserver(new MemoryPressureObserver, kMemoryPressureNotification,
false);
}
RegisterStrongMemoryReporter(new HyphenReporter());
}
return sInstance;
}
@ -296,3 +351,20 @@ void nsHyphenationManager::LoadAliases() {
}
}
}
size_t nsHyphenationManager::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) {
size_t result = aMallocSizeOf(this);
result += mHyphAliases.ShallowSizeOfExcludingThis(aMallocSizeOf);
result += mPatternFiles.ShallowSizeOfExcludingThis(aMallocSizeOf);
// Measurement of the URIs stored in mPatternFiles may be added later if DMD
// finds it is worthwhile.
result += mHyphenators.ShallowSizeOfExcludingThis(aMallocSizeOf);
for (auto i = mHyphenators.ConstIter(); !i.Done(); i.Next()) {
result += aMallocSizeOf(i.Data().get());
}
return result;
}

View File

@ -26,6 +26,8 @@ class nsHyphenationManager {
static void Shutdown();
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
private:
~nsHyphenationManager();