File: PHCManager.cpp

package info (click to toggle)
firefox 146.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,260 kB
  • sloc: cpp: 7,587,892; javascript: 6,509,455; ansic: 3,755,295; python: 1,410,813; xml: 629,201; asm: 438,677; java: 186,096; sh: 62,697; makefile: 18,086; objc: 13,087; perl: 12,811; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (121 lines) | stat: -rw-r--r-- 4,715 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* -*- 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 "PHC.h"
#include "mozilla/Literals.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_memory.h"
#include "mozilla/glean/XpcomMetrics.h"
#include "prsystem.h"

namespace mozilla {

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 kPHCAvgDelayPageReuse[] = "memory.phc.avg_delay.page_reuse";

static const char kPHCAvgDelayContentFirst[] =
    "memory.phc.avg_delay.content.first";
static const char kPHCAvgDelayContentNormal[] =
    "memory.phc.avg_delay.content.normal";
static const char kPHCAvgDelayContentPageReuse[] =
    "memory.phc.avg_delay.content.page_reuse";

static const char kPHCSizePref[] = "memory.phc.size_kb";

// True if PHC has ever been enabled for this process.
static bool sWasPHCEnabled = false;

static void UpdatePHCState() {
  size_t mem_size = PR_GetPhysicalMemorySize() / (1_MiB);
  size_t min_mem_size = StaticPrefs::memory_phc_min_ram_mb();

  // Only enable PHC if there are at least 8GB of ram.  Note that we use
  // 1000 bytes per kilobyte rather than 1024.  Some 8GB machines will have
  // 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.
    if (XRE_IsContentProcess()) {
      // Content processes can come and go and have their own delays
      // configured.
      SetPHCProbabilities(
          StaticPrefs::memory_phc_avg_delay_content_first(),
          StaticPrefs::memory_phc_avg_delay_content_normal(),
          StaticPrefs::memory_phc_avg_delay_content_page_reuse());
    } else {
      // All other process types tend to exist for the length of the
      // session, they're grouped here.
      SetPHCProbabilities(StaticPrefs::memory_phc_avg_delay_first(),
                          StaticPrefs::memory_phc_avg_delay_normal(),
                          StaticPrefs::memory_phc_avg_delay_page_reuse());
    }

    SetPHCSize(StaticPrefs::memory_phc_size_kb() * 1024);

    SetPHCState(Enabled);
    sWasPHCEnabled = true;
  } else {
    SetPHCState(OnlyFree);
  }
}

static void PrefChangeCallback(const char* aPrefName, void* aNull) {
  MOZ_ASSERT((0 == strcmp(aPrefName, kPHCEnabledPref)) ||
             (0 == strcmp(aPrefName, kPHCMinRamMBPref)) ||
             (0 == strcmp(aPrefName, kPHCAvgDelayFirst)) ||
             (0 == strcmp(aPrefName, kPHCAvgDelayNormal)) ||
             (0 == strcmp(aPrefName, kPHCAvgDelayPageReuse)) ||
             (0 == strcmp(aPrefName, kPHCAvgDelayContentFirst)) ||
             (0 == strcmp(aPrefName, kPHCAvgDelayContentNormal)) ||
             (0 == strcmp(aPrefName, kPHCAvgDelayContentPageReuse)) ||
             (0 == strcmp(aPrefName, kPHCSizePref)));

  UpdatePHCState();
}

void InitPHCState() {
  Preferences::RegisterCallback(PrefChangeCallback, kPHCEnabledPref);
  Preferences::RegisterCallback(PrefChangeCallback, kPHCMinRamMBPref);
  Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayFirst);
  Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayNormal);
  Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayPageReuse);
  Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayContentFirst);
  Preferences::RegisterCallback(PrefChangeCallback, kPHCAvgDelayContentNormal);
  Preferences::RegisterCallback(PrefChangeCallback,
                                kPHCAvgDelayContentPageReuse);
  Preferences::RegisterCallback(PrefChangeCallback, kPHCSizePref);
  UpdatePHCState();
}

void ReportPHCTelemetry() {
  if (!sWasPHCEnabled) {
    return;
  }

  MemoryUsage usage;
  PHCMemoryUsage(usage);

  glean::memory_phc::slop.Accumulate(usage.mFragmentationBytes);

  PHCStats stats;
  GetPHCStats(stats);

  glean::memory_phc::slots_allocated.AccumulateSingleSample(
      stats.mSlotsAllocated);
  glean::memory_phc::slots_freed.AccumulateSingleSample(stats.mSlotsFreed);
  // There are also slots that are unused (neither free nor allocated) they
  // can be calculated by knowing the total number of slots.
}

};  // namespace mozilla