Skip to content

Commit

Permalink
Bug 1867190 - Add prefs for PHC probablities r=glandium
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulBone committed Jan 30, 2024
1 parent 9f2470a commit 3a813ca
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
29 changes: 28 additions & 1 deletion memory/build/PHC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,15 @@ static const Time kMaxTime = ~(Time(0));
constexpr Delay Rnd64ToDelay(Delay aAvgDelay, uint64_t aRnd) {
MOZ_ASSERT(IsPowerOfTwo(aAvgDelay), "must be a power of two");

return (aRnd & (aAvgDelay * 2 - 1)) + 1;
return (aRnd & (uint64_t(aAvgDelay) * 2 - 1)) + 1;
}

static Delay CheckProbability(int64_t aProb) {
// Limit delays calculated from prefs to 0x80000000, this is the largest
// power-of-two that fits in a Delay since it is a uint32_t.
// The minimum is 2 that way not every allocation goes straight to PHC.
return RoundUpPow2(
std::min(std::max(aProb, int64_t(2)), int64_t(0x80000000)));
}

// Maps a pointer to a PHC-specific structure:
Expand Down Expand Up @@ -863,6 +871,15 @@ class GMut {
mPhcState = aState;
}

void SetProbabilities(int64_t aAvgDelayFirst, int64_t aAvgDelayNormal,
int64_t aAvgDelayPageReuse) {
MutexAutoLock lock(GMut::sMutex);

mAvgFirstAllocDelay = CheckProbability(aAvgDelayFirst);
mAvgAllocDelay = CheckProbability(aAvgDelayNormal);
mAvgPageReuseDelay = CheckProbability(aAvgDelayPageReuse);
}

private:
template <int N>
uint64_t RandomSeed() {
Expand Down Expand Up @@ -1807,4 +1824,14 @@ void SetPHCState(PHCState aState) {

gMut->SetState(aState);
}

void SetPHCProbabilities(int64_t aAvgDelayFirst, int64_t aAvgDelayNormal,
int64_t aAvgDelayPageReuse) {
if (!maybe_init()) {
return;
}

gMut->SetProbabilities(aAvgDelayFirst, aAvgDelayNormal, aAvgDelayPageReuse);
}

} // namespace mozilla::phc
4 changes: 4 additions & 0 deletions memory/build/PHC.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ enum PHCState {

MOZ_JEMALLOC_API void SetPHCState(PHCState aState);

MOZ_JEMALLOC_API void SetPHCProbabilities(int64_t aAvgDelayFirst,
int64_t aAvgDelayNormal,
int64_t aAvgDelayPageReuse);

struct MemoryUsage {
// The amount of memory used for PHC metadata, eg information about each
// allocation including stacks.
Expand Down
15 changes: 15 additions & 0 deletions modules/libpref/init/StaticPrefList.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11051,6 +11051,21 @@
value: 8000
mirror: always

- name: memory.phc.avg_delay.first
type: uint32_t
value: 65536
mirror: always

- name: memory.phc.avg_delay.normal
type: uint32_t
value: 16384
mirror: always

- name: memory.phc.avg_delay.page_reuse
type: uint32_t
value: 262144
mirror: always

#---------------------------------------------------------------------------
# Prefs starting with "midi."
#---------------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions toolkit/components/nimbus/FeatureManifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,24 @@ phc:
setPref:
branch: user
pref: memory.phc.min_ram_mb
phcAvgDelayFirst:
description: The delay before the first PHC allocation
type: int
setPref:
branch: user
pref: memory.phc.avg_delay.first
phcAvgDelayNormal:
description: The delay between PHC allocations
type: int
setPref:
branch: user
pref: memory.phc.avg_delay.normal
phcAvgDelayPageReuse:
description: The delay before reusing a PHC page
type: int
setPref:
branch: user
pref: memory.phc.avg_delay.page_reuse

mailto:
description: Prefs to control aspects of the mailto handler
Expand Down
17 changes: 16 additions & 1 deletion xpcom/base/PHCManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ using namespace phc;

static const char kPHCEnabledPref[] = "memory.phc.enabled";
static const char kPHCMinRamMBPref[] = "memory.phc.min_ram_mb";
static const char kPHCAvgDelayFirst[] = "memory.phc.avg_delay.first";
static const char kPHCAvgDelayNormal[] = "memory.phc.avg_delay.normal";
static const char kPHCAvgDelayPageRuse[] = "memory.phc.avg_delay.page_reuse";

// True if PHC has ever been enabled for this process.
static bool sWasPHCEnabled = false;
Expand All @@ -32,6 +35,12 @@ static void UpdatePHCState() {
// slightly lower actual RAM available after some hardware devices
// reserve some.
if (StaticPrefs::memory_phc_enabled() && mem_size >= min_mem_size) {
// Set PHC probablities before enabling PHC so that the first allocation
// delay gets used.
SetPHCProbabilities(StaticPrefs::memory_phc_avg_delay_first(),
StaticPrefs::memory_phc_avg_delay_normal(),
StaticPrefs::memory_phc_avg_delay_page_reuse());

SetPHCState(Enabled);
sWasPHCEnabled = true;
} else {
Expand All @@ -41,14 +50,20 @@ static void UpdatePHCState() {

static void PrefChangeCallback(const char* aPrefName, void* aNull) {
MOZ_ASSERT((0 == strcmp(aPrefName, kPHCEnabledPref)) ||
(0 == strcmp(aPrefName, kPHCMinRamMBPref)));
(0 == strcmp(aPrefName, kPHCMinRamMBPref)) ||
(0 == strcmp(aPrefName, kPHCAvgDelayFirst)) ||
(0 == strcmp(aPrefName, kPHCAvgDelayNormal)) ||
(0 == strcmp(aPrefName, kPHCAvgDelayPageRuse)));

UpdatePHCState();
}

void InitPHCState() {
Preferences::RegisterCallback(PrefChangeCallback, kPHCEnabledPref);
Preferences::RegisterCallback(PrefChangeCallback, kPHCMinRamMBPref);
Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayFirst);
Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayNormal);
Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayPageRuse);
UpdatePHCState();
}

Expand Down

0 comments on commit 3a813ca

Please sign in to comment.