Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / base / PHCManager.cpp
blobf8124312f5f5d9a7130b7e221043cb198aa1389b
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "PHCManager.h"
9 #include "PHC.h"
10 #include "mozilla/Literals.h"
11 #include "mozilla/Preferences.h"
12 #include "mozilla/StaticPrefs_memory.h"
13 #include "mozilla/Telemetry.h"
14 #include "prsystem.h"
16 namespace mozilla {
18 using namespace phc;
20 static const char kPHCEnabledPref[] = "memory.phc.enabled";
21 static const char kPHCMinRamMBPref[] = "memory.phc.min_ram_mb";
22 static const char kPHCAvgDelayFirst[] = "memory.phc.avg_delay.first";
23 static const char kPHCAvgDelayNormal[] = "memory.phc.avg_delay.normal";
24 static const char kPHCAvgDelayPageRuse[] = "memory.phc.avg_delay.page_reuse";
26 // True if PHC has ever been enabled for this process.
27 static bool sWasPHCEnabled = false;
29 static void UpdatePHCState() {
30 size_t mem_size = PR_GetPhysicalMemorySize() / (1_MiB);
31 size_t min_mem_size = StaticPrefs::memory_phc_min_ram_mb();
33 // Only enable PHC if there are at least 8GB of ram. Note that we use
34 // 1000 bytes per kilobyte rather than 1024. Some 8GB machines will have
35 // slightly lower actual RAM available after some hardware devices
36 // reserve some.
37 if (StaticPrefs::memory_phc_enabled() && mem_size >= min_mem_size) {
38 // Set PHC probablities before enabling PHC so that the first allocation
39 // delay gets used.
40 SetPHCProbabilities(StaticPrefs::memory_phc_avg_delay_first(),
41 StaticPrefs::memory_phc_avg_delay_normal(),
42 StaticPrefs::memory_phc_avg_delay_page_reuse());
44 SetPHCState(Enabled);
45 sWasPHCEnabled = true;
46 } else {
47 SetPHCState(OnlyFree);
51 static void PrefChangeCallback(const char* aPrefName, void* aNull) {
52 MOZ_ASSERT((0 == strcmp(aPrefName, kPHCEnabledPref)) ||
53 (0 == strcmp(aPrefName, kPHCMinRamMBPref)) ||
54 (0 == strcmp(aPrefName, kPHCAvgDelayFirst)) ||
55 (0 == strcmp(aPrefName, kPHCAvgDelayNormal)) ||
56 (0 == strcmp(aPrefName, kPHCAvgDelayPageRuse)));
58 UpdatePHCState();
61 void InitPHCState() {
62 Preferences::RegisterCallback(PrefChangeCallback, kPHCEnabledPref);
63 Preferences::RegisterCallback(PrefChangeCallback, kPHCMinRamMBPref);
64 Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayFirst);
65 Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayNormal);
66 Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayPageRuse);
67 UpdatePHCState();
70 void ReportPHCTelemetry() {
71 if (!sWasPHCEnabled) {
72 return;
75 MemoryUsage usage;
76 PHCMemoryUsage(usage);
78 Accumulate(Telemetry::MEMORY_PHC_SLOP, usage.mFragmentationBytes);
80 PHCStats stats;
81 GetPHCStats(stats);
83 Accumulate(Telemetry::MEMORY_PHC_SLOTS_ALLOCATED, stats.mSlotsAllocated);
84 Accumulate(Telemetry::MEMORY_PHC_SLOTS_FREED, stats.mSlotsFreed);
85 // There are also slots that are unused (neither free nor allocated) they
86 // can be calculated by knowing the total number of slots.
89 }; // namespace mozilla