Bug 1425613 - Part 1 - Record and expose pref access statistics in nsIPrefService in debug mode. r=njn

These statistics will be used by browser tests to analyze frequently accessed
preferences so that we can recommend using preference observers instead.

MozReview-Commit-ID: 9uZnwmjZL4U
This commit is contained in:
Johann Hofmann
2018-03-01 13:59:43 +01:00
parent c525c302e5
commit 061941f827
2 changed files with 71 additions and 1 deletions

View File

@@ -725,6 +725,10 @@ private:
class PrefEntry : public PLDHashEntryHdr
{
public:
#ifdef DEBUG
// This field is before mPref to minimize sizeof(PrefEntry) on 64-bit.
uint32_t mAccessCount;
#endif
Pref* mPref; // Note: this is never null in a live entry.
static bool MatchEntry(const PLDHashEntryHdr* aEntry, const void* aKey)
@@ -740,6 +744,9 @@ public:
auto entry = static_cast<PrefEntry*>(aEntry);
auto prefName = static_cast<const char*>(aKey);
#ifdef DEBUG
entry->mAccessCount = 0;
#endif
entry->mPref = new Pref(prefName);
}
@@ -941,7 +948,15 @@ static Pref*
pref_HashTableLookup(const char* aPrefName)
{
PrefEntry* entry = pref_HashTableLookupInner(aPrefName);
return entry ? entry->mPref : nullptr;
if (!entry) {
return nullptr;
}
#ifdef DEBUG
entry->mAccessCount += 1;
#endif
return entry->mPref;
}
static nsresult
@@ -3615,6 +3630,34 @@ Preferences::GetDefaultBranch(const char* aPrefRoot, nsIPrefBranch** aRetVal)
return NS_OK;
}
NS_IMETHODIMP
Preferences::ReadStats(nsIPrefStatsCallback* aCallback)
{
#ifdef DEBUG
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
PrefEntry* entry = static_cast<PrefEntry*>(iter.Get());
aCallback->Visit(entry->mPref->Name(), entry->mAccessCount);
}
return NS_OK;
#else
return NS_ERROR_NOT_IMPLEMENTED;
#endif
}
NS_IMETHODIMP
Preferences::ResetStats()
{
#ifdef DEBUG
for (auto iter = gHashTable->Iter(); !iter.Done(); iter.Next()) {
static_cast<PrefEntry*>(iter.Get())->mAccessCount = 0;
}
return NS_OK;
#else
return NS_ERROR_NOT_IMPLEMENTED;
#endif
}
NS_IMETHODIMP
Preferences::GetDirty(bool* aRetVal)
{