Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / xpcom / base / PHCManager.cpp
blobed41b0c5e70397982f81ce26e7a2af0d6ff29080
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/glean/XpcomMetrics.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 static const char kPHCAvgDelayContentFirst[] =
27 "memory.phc.avg_delay.content.first";
28 static const char kPHCAvgDelayContentNormal[] =
29 "memory.phc.avg_delay.content.normal";
30 static const char kPHCAvgDelayContentPageRuse[] =
31 "memory.phc.avg_delay.content.page_reuse";
33 // True if PHC has ever been enabled for this process.
34 static bool sWasPHCEnabled = false;
36 static void UpdatePHCState() {
37 size_t mem_size = PR_GetPhysicalMemorySize() / (1_MiB);
38 size_t min_mem_size = StaticPrefs::memory_phc_min_ram_mb();
40 // Only enable PHC if there are at least 8GB of ram. Note that we use
41 // 1000 bytes per kilobyte rather than 1024. Some 8GB machines will have
42 // slightly lower actual RAM available after some hardware devices
43 // reserve some.
44 if (StaticPrefs::memory_phc_enabled() && mem_size >= min_mem_size) {
45 // Set PHC probablities before enabling PHC so that the first allocation
46 // delay gets used.
47 if (XRE_IsContentProcess()) {
48 // Content processes can come and go and have their own delays
49 // configured.
50 SetPHCProbabilities(
51 StaticPrefs::memory_phc_avg_delay_content_first(),
52 StaticPrefs::memory_phc_avg_delay_content_normal(),
53 StaticPrefs::memory_phc_avg_delay_content_page_reuse());
54 } else {
55 // All other process types tend to exist for the length of the
56 // session, they're grouped here.
57 SetPHCProbabilities(StaticPrefs::memory_phc_avg_delay_first(),
58 StaticPrefs::memory_phc_avg_delay_normal(),
59 StaticPrefs::memory_phc_avg_delay_page_reuse());
62 SetPHCState(Enabled);
63 sWasPHCEnabled = true;
64 } else {
65 SetPHCState(OnlyFree);
69 static void PrefChangeCallback(const char* aPrefName, void* aNull) {
70 MOZ_ASSERT((0 == strcmp(aPrefName, kPHCEnabledPref)) ||
71 (0 == strcmp(aPrefName, kPHCMinRamMBPref)) ||
72 (0 == strcmp(aPrefName, kPHCAvgDelayFirst)) ||
73 (0 == strcmp(aPrefName, kPHCAvgDelayNormal)) ||
74 (0 == strcmp(aPrefName, kPHCAvgDelayPageRuse)) ||
75 (0 == strcmp(aPrefName, kPHCAvgDelayContentFirst)) ||
76 (0 == strcmp(aPrefName, kPHCAvgDelayContentNormal)) ||
77 (0 == strcmp(aPrefName, kPHCAvgDelayContentPageRuse)));
79 UpdatePHCState();
82 void InitPHCState() {
83 Preferences::RegisterCallback(PrefChangeCallback, kPHCEnabledPref);
84 Preferences::RegisterCallback(PrefChangeCallback, kPHCMinRamMBPref);
85 Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayFirst);
86 Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayNormal);
87 Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayPageRuse);
88 Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayContentFirst);
89 Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayContentNormal);
90 Preferences::RegisterCallback(PrefChangeCallback,
91 kPHCAvgDelayContentPageRuse);
92 UpdatePHCState();
95 void ReportPHCTelemetry() {
96 if (!sWasPHCEnabled) {
97 return;
100 MemoryUsage usage;
101 PHCMemoryUsage(usage);
103 glean::memory_phc::slop.Accumulate(usage.mFragmentationBytes);
105 PHCStats stats;
106 GetPHCStats(stats);
108 glean::memory_phc::slots_allocated.AccumulateSingleSample(
109 stats.mSlotsAllocated);
110 glean::memory_phc::slots_freed.AccumulateSingleSample(stats.mSlotsFreed);
111 // There are also slots that are unused (neither free nor allocated) they
112 // can be calculated by knowing the total number of slots.
115 }; // namespace mozilla