Bug 1831852 - Avoid extra call to jemalloc_stats in telemetry r=mccr8

Differential Revision: https://phabricator.services.mozilla.com/D177596
This commit is contained in:
Paul Bone 2023-05-11 04:40:44 +00:00
parent e390ec299c
commit dc0b5eff1e
3 changed files with 35 additions and 10 deletions

View File

@ -282,9 +282,17 @@ nsresult MemoryTelemetry::GatherReports(
#ifndef XP_MACOSX
RECORD(MEMORY_UNIQUE, ResidentUnique, UNITS_BYTES);
#endif
RECORD(MEMORY_HEAP_ALLOCATED, HeapAllocated, UNITS_BYTES);
RECORD(MEMORY_HEAP_OVERHEAD_FRACTION, HeapOverheadFraction,
UNITS_PERCENTAGE);
#ifdef HAVE_JEMALLOC_STATS
jemalloc_stats_t stats;
jemalloc_stats(&stats);
HandleMemoryReport(Telemetry::MEMORY_HEAP_ALLOCATED,
nsIMemoryReporter::UNITS_BYTES,
mgr->HeapAllocated(stats));
HandleMemoryReport(Telemetry::MEMORY_HEAP_OVERHEAD_FRACTION,
nsIMemoryReporter::UNITS_PERCENTAGE,
mgr->HeapOverheadFraction(stats));
#endif
if (completionRunnable) {
NS_DispatchToMainThread(completionRunnable.forget(),

View File

@ -66,11 +66,6 @@ using namespace mozilla;
using namespace mozilla::ipc;
using namespace dom;
#if defined(MOZ_MEMORY)
# define HAVE_JEMALLOC_STATS 1
# include "mozmemory.h"
#endif // MOZ_MEMORY
#if defined(XP_LINUX)
# include "mozilla/MemoryMapping.h"
@ -1257,7 +1252,10 @@ static size_t HeapOverhead(const jemalloc_stats_t& aStats) {
// This has UNITS_PERCENTAGE, so it is multiplied by 100x *again* on top of the
// 100x for the percentage.
static int64_t HeapOverheadFraction(const jemalloc_stats_t& aStats) {
// static
int64_t nsMemoryReporterManager::HeapOverheadFraction(
const jemalloc_stats_t& aStats) {
size_t heapOverhead = HeapOverhead(aStats);
size_t heapCommitted = aStats.allocated + heapOverhead;
return int64_t(10000 * (heapOverhead / (double)heapCommitted));
@ -2563,12 +2561,19 @@ int64_t nsMemoryReporterManager::ResidentUnique(ResidentUniqueArg) {
#endif // XP_{WIN, MACOSX, LINUX, *}
#ifdef HAVE_JEMALLOC_STATS
// static
size_t nsMemoryReporterManager::HeapAllocated(const jemalloc_stats_t& aStats) {
return aStats.allocated;
}
#endif
NS_IMETHODIMP
nsMemoryReporterManager::GetHeapAllocated(int64_t* aAmount) {
#ifdef HAVE_JEMALLOC_STATS
jemalloc_stats_t stats;
jemalloc_stats(&stats);
*aAmount = stats.allocated;
*aAmount = HeapAllocated(stats);
return NS_OK;
#else
*aAmount = 0;

View File

@ -18,6 +18,11 @@
# include <windows.h>
#endif // XP_WIN
#if defined(MOZ_MEMORY)
# define HAVE_JEMALLOC_STATS 1
# include "mozmemory.h"
#endif // MOZ_MEMORY
namespace mozilla {
class MemoryReportingProcess;
namespace dom {
@ -198,6 +203,13 @@ class nsMemoryReporterManager final : public nsIMemoryReporterManager,
};
SizeOfTabFns mSizeOfTabFns;
#ifdef HAVE_JEMALLOC_STATS
// These C++ only versions of HeapAllocated and HeapOverheadFraction avoid
// extra calls to jemalloc_stats;
static size_t HeapAllocated(const jemalloc_stats_t& stats);
static int64_t HeapOverheadFraction(const jemalloc_stats_t& stats);
#endif
private:
bool IsRegistrationBlocked() MOZ_EXCLUDES(mMutex) {
mozilla::MutexAutoLock lock(mMutex);