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"
10 #include "mozilla/Literals.h"
11 #include "mozilla/Preferences.h"
12 #include "mozilla/StaticPrefs_memory.h"
13 #include "mozilla/Telemetry.h"
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
37 if (StaticPrefs::memory_phc_enabled() && mem_size
>= min_mem_size
) {
38 // Set PHC probablities before enabling PHC so that the first allocation
40 SetPHCProbabilities(StaticPrefs::memory_phc_avg_delay_first(),
41 StaticPrefs::memory_phc_avg_delay_normal(),
42 StaticPrefs::memory_phc_avg_delay_page_reuse());
45 sWasPHCEnabled
= true;
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
)));
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
);
70 void ReportPHCTelemetry() {
71 if (!sWasPHCEnabled
) {
76 PHCMemoryUsage(usage
);
78 Accumulate(Telemetry::MEMORY_PHC_SLOP
, usage
.mFragmentationBytes
);
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