Bug 1814798 - pt 2. Add a PHCManager component to control PHC r=glandium,emilio

This change adds the PHCManager class which observes a pref to control PHC.
The pref can be changed at runtime and will affect the parent and content
processes.

Differential Revision: https://phabricator.services.mozilla.com/D178754
This commit is contained in:
Paul Bone 2023-09-12 04:03:16 +00:00
Родитель 51e69845d9
Коммит 8f391e64c7
8 изменённых файлов: 95 добавлений и 1 удалений

Просмотреть файл

@ -324,7 +324,7 @@ static const size_t kAllPagesSize = kNumAllPages * kPageSize;
static const size_t kAllPagesJemallocSize = kAllPagesSize - kPageSize;
// The default state for PHC. Either Enabled or OnlyFree.
#define DEFAULT_STATE mozilla::phc::Enabled
#define DEFAULT_STATE mozilla::phc::OnlyFree
// The junk value used to fill new allocation in debug builds. It's same value
// as the one used by mozjemalloc. PHC applies it unconditionally in debug

Просмотреть файл

@ -11010,6 +11010,19 @@
mirror: always
#endif
#---------------------------------------------------------------------------
# Prefs starting with "memory."
#---------------------------------------------------------------------------
- name: memory.phc.enabled
type: bool
#if defined(MOZ_PHC)
value: true
#else
value: false
#endif
mirror: always
#---------------------------------------------------------------------------
# Prefs starting with "midi."
#---------------------------------------------------------------------------

Просмотреть файл

@ -62,6 +62,7 @@ pref_groups = [
"layout",
"mathml",
"media",
"memory",
"midi",
"mousewheel",
"mozilla",
@ -172,3 +173,6 @@ else:
FINAL_TARGET_PP_FILES += [
"greprefs.js",
]
if CONFIG["MOZ_PHC"]:
DEFINES["MOZ_PHC"] = True

35
xpcom/base/PHCManager.cpp Normal file
Просмотреть файл

@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
#include "PHCManager.h"
#include "replace_malloc_bridge.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_memory.h"
namespace mozilla {
using namespace phc;
static const char kPHCPref[] = "memory.phc.enabled";
static PHCState GetPHCStateFromPref() {
return StaticPrefs::memory_phc_enabled() ? Enabled : OnlyFree;
}
static void PrefChangeCallback(const char* aPrefName, void* aNull) {
MOZ_ASSERT(0 == strcmp(aPrefName, kPHCPref));
ReplaceMalloc::SetPHCState(GetPHCStateFromPref());
}
void InitPHCState() {
ReplaceMalloc::SetPHCState(GetPHCStateFromPref());
Preferences::RegisterCallback(PrefChangeCallback, kPHCPref);
}
}; // namespace mozilla

18
xpcom/base/PHCManager.h Normal file
Просмотреть файл

@ -0,0 +1,18 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
#ifndef mozilla_PHCManager_h
#define mozilla_PHCManager_h
namespace mozilla {
// Read the PHC pref and potentially initialise PHC. Also register a
// callback for the pref to update PHC as the pref changes.
void InitPHCState();
}; // namespace mozilla
#endif // mozilla_PHCManager_h

Просмотреть файл

@ -257,3 +257,15 @@ LOCAL_INCLUDES += [
if CONFIG["MOZ_WIDGET_TOOLKIT"] == "gtk":
CXXFLAGS += CONFIG["MOZ_GTK3_CFLAGS"]
if CONFIG["MOZ_PHC"]:
EXPORTS.mozilla += [
"PHCManager.h",
]
DEFINES["MOZ_PHC"] = 1
UNIFIED_SOURCES += ["PHCManager.cpp"]
with Files("PHCManager.*"):
BUG_COMPONENT = ("Core", "Memory Allocator")

Просмотреть файл

@ -89,6 +89,9 @@
#include "mozilla/AvailableMemoryTracker.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/CountingAllocatorBase.h"
#ifdef MOZ_PHC
# include "mozilla/PHCManager.h"
#endif
#include "mozilla/UniquePtr.h"
#include "mozilla/ServoStyleConsts.h"
@ -448,6 +451,12 @@ NS_InitXPCOM(nsIServiceManager** aResult, nsIFile* aBinDirectory,
NS_ADDREF(*aResult = nsComponentManagerImpl::gComponentManager);
}
#ifdef MOZ_PHC
// This is the earliest possible moment we can start PHC while still being
// able to read prefs.
mozilla::InitPHCState();
#endif
// After autoreg, but before we actually instantiate any components,
// add any services listed in the "xpcom-directory-providers" category
// to the directory service.

Просмотреть файл

@ -103,3 +103,6 @@ if CONFIG["MOZ_VPX"]:
LOCAL_INCLUDES += [
"/media/libvpx",
]
if CONFIG["MOZ_PHC"]:
DEFINES["MOZ_PHC"] = 1