File: browser_performance_scenarios.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (276 lines) | stat: -rw-r--r-- 11,241 bytes parent folder | download | duplicates (3)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/scenarios/browser_performance_scenarios.h"

#include <atomic>
#include <memory>
#include <utility>

#include "base/check_op.h"
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/structured_shared_memory.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/trace_event/typed_macros.h"
#include "base/types/pass_key.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/process_node.h"
#include "components/performance_manager/public/performance_manager.h"
#include "components/performance_manager/scenario_api/performance_scenario_memory.h"
#include "components/performance_manager/scenario_api/performance_scenario_observer.h"
#include "components/performance_manager/scenario_api/performance_scenarios.h"
#include "components/performance_manager/scenarios/performance_scenario_data.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "third_party/perfetto/include/perfetto/tracing/string_helpers.h"
#include "third_party/perfetto/include/perfetto/tracing/track.h"

namespace performance_manager {

using performance_scenarios::MatchingScenarioObserver;
using performance_scenarios::PerformanceScenarioObserver;
using performance_scenarios::PerformanceScenarioObserverList;
using performance_scenarios::ScenarioScope;

namespace {

void MaybeEmitNestingChangeEvent(
    const perfetto::NamedTrack* track,
    size_t old_nesting_level,
    size_t new_nesting_level,
    base::span<const perfetto::StaticString> event_names) {
  // Close trace events for each removed nesting level.
  for (size_t i = old_nesting_level; track && i > new_nesting_level; --i) {
    TRACE_EVENT_END("performance_scenarios", *track);
  }
  // Open trace events for each added nesting level.
  for (size_t i = old_nesting_level; track && i < new_nesting_level; ++i) {
    TRACE_EVENT_BEGIN("performance_scenarios", event_names.at(i), *track);
  }
}

// Generic methods that change according to the Scenario type.
template <typename Scenario>
struct ScenarioTraits {
  explicit ScenarioTraits(PerformanceScenarioData* state_ptr);

  // Returns a reference to the Scenario slot in shared memory.
  std::atomic<Scenario>& ScenarioRef();

  // Records trace events for a switch from `old_scenario` to `new_scenario` if
  // a tracing track is registered.
  void MaybeRecordTraceEvent(Scenario old_scenario,
                             Scenario new_scenario) const;

  // Notifies a ProcessNode's PerformanceScenarioObserver list of a switch from
  // `old_scenario` to `new_scenario`.
  void NotifyProcessObservers(Scenario old_scenario,
                              Scenario new_scenario) const;
};

template <>
struct ScenarioTraits<LoadingScenario> {
  explicit ScenarioTraits(PerformanceScenarioData* state_ptr)
      : state_ptr(state_ptr) {}

  std::atomic<LoadingScenario>& ScenarioRef() {
    return state_ptr->shared_state().WritableRef().loading;
  }

  void MaybeRecordTraceEvent(LoadingScenario old_scenario,
                             LoadingScenario new_scenario) const {
    MaybeEmitNestingChangeEvent(
        state_ptr->loading_tracing_track(), static_cast<size_t>(old_scenario),
        static_cast<size_t>(new_scenario),
        {"AnyPageLoading", "VisiblePageLoading", "FocusedPageLoading"});
  }

  void NotifyProcessObservers(LoadingScenario old_scenario,
                              LoadingScenario new_scenario) const {
    state_ptr->observers().Notify(
        &PerformanceScenarioObserver::OnLoadingScenarioChanged,
        ScenarioScope::kCurrentProcess, old_scenario, new_scenario);
    InputScenario input_scenario =
        state_ptr->shared_state().ReadOnlyRef().input.load(
            std::memory_order_relaxed);
    state_ptr->matching_observers().Notify(
        &MatchingScenarioObserver::NotifyIfScenarioMatchChanged,
        ScenarioScope::kCurrentProcess, new_scenario, input_scenario);
  }

  raw_ptr<PerformanceScenarioData> state_ptr;
};

template <>
struct ScenarioTraits<InputScenario> {
  explicit ScenarioTraits(PerformanceScenarioData* state_ptr)
      : state_ptr(state_ptr) {}

  std::atomic<InputScenario>& ScenarioRef() {
    return state_ptr->shared_state().WritableRef().input;
  }

  void MaybeRecordTraceEvent(InputScenario old_scenario,
                             InputScenario new_scenario) const {
    MaybeEmitNestingChangeEvent(state_ptr->input_tracing_track(),
                                static_cast<size_t>(old_scenario),
                                static_cast<size_t>(new_scenario),
                                {"TypingTapOrScroll", "TapOrScroll", "Scroll"});
  }

  void NotifyProcessObservers(InputScenario old_scenario,
                              InputScenario new_scenario) const {
    state_ptr->observers().Notify(
        &PerformanceScenarioObserver::OnInputScenarioChanged,
        ScenarioScope::kCurrentProcess, old_scenario, new_scenario);
    LoadingScenario loading_scenario =
        state_ptr->shared_state().ReadOnlyRef().loading.load(
            std::memory_order_relaxed);
    state_ptr->matching_observers().Notify(
        &MatchingScenarioObserver::NotifyIfScenarioMatchChanged,
        ScenarioScope::kCurrentProcess, loading_scenario, new_scenario);
  }

  raw_ptr<PerformanceScenarioData> state_ptr;
};

// Holds the browser's global scenario state handle.
std::unique_ptr<PerformanceScenarioData>& GlobalSharedStatePtr() {
  static base::NoDestructor<std::unique_ptr<PerformanceScenarioData>> state_ptr;
  return *state_ptr;
}

// Returns a pointer to the shared memory region for communicating private state
// for `process_node`. Creates a region if none exists yet, returning nullptr on
// failure. The region's lifetime is tied to `process_node`.
PerformanceScenarioData* GetSharedStateForProcessNode(
    const ProcessNode* process_node) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  auto& data = PerformanceScenarioData::GetOrCreate(process_node);
  // Only return the process data if it holds a shared memory region.
  return data.HasSharedState() ? &data : nullptr;
}

// Returns a pointer to the global shared memory region that can be read by all
// processes, or nullptr if none exists. GlobalPerformanceScenarioMemory
// manages the lifetime of the region.
PerformanceScenarioData* GetGlobalSharedState() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return GlobalSharedStatePtr().get();
}

// Sets the value for Scenario in the memory region held in `state_ptr` to
// `new_scenario`.
template <typename Scenario>
void SetScenarioValue(ScenarioScope scope,
                      Scenario new_scenario,
                      PerformanceScenarioData* state_ptr) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (state_ptr) {
    ScenarioTraits<Scenario> traits(state_ptr);
    // std::memory_order_relaxed is sufficient since no other memory depends on
    // the scenario value.
    Scenario old_scenario =
        traits.ScenarioRef().exchange(new_scenario, std::memory_order_relaxed);
    if (old_scenario != new_scenario) {
      traits.MaybeRecordTraceEvent(old_scenario, new_scenario);
      switch (scope) {
        case ScenarioScope::kCurrentProcess:
          // Notify observers for the ProcessNode holding `state_ptr`.
          traits.NotifyProcessObservers(old_scenario, new_scenario);
          break;
        case ScenarioScope::kGlobal:
          // Notify all global observers registered in the browser process.
          if (auto observers = PerformanceScenarioObserverList::GetForScope(
                  ScenarioScope::kGlobal)) {
            observers->NotifyIfScenarioChanged();
          }
          break;
      }
    }
  }
}

template <typename Scenario>
void SetScenarioValueForRenderProcessHost(Scenario scenario,
                                          content::RenderProcessHost* host) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  base::WeakPtr<ProcessNode> process_node =
      PerformanceManager::GetProcessNodeForRenderProcessHost(host);
  CHECK(process_node);
  SetScenarioValue(ScenarioScope::kCurrentProcess, scenario,
                   GetSharedStateForProcessNode(process_node.get()));
}

}  // namespace

void SetGlobalSharedScenarioState(
    base::PassKey<ScopedGlobalScenarioMemory>,
    std::unique_ptr<PerformanceScenarioData> state) {
  // No BrowserThread::UI here because this might be called on the main thread
  // before browser threads are set up.
  CHECK_NE(state == nullptr, GlobalSharedStatePtr() == nullptr);
  GlobalSharedStatePtr() = std::move(state);
}

base::ReadOnlySharedMemoryRegion GetSharedScenarioRegionForProcessNode(
    const ProcessNode* process_node) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  PerformanceScenarioData* state_ptr =
      GetSharedStateForProcessNode(process_node);
  // When this is called, the ProcessTrack should be available.
  if (state_ptr) {
    state_ptr->EnsureTracingTracks(process_node);
  }
  return state_ptr ? state_ptr->shared_state().DuplicateReadOnlyRegion()
                   : base::ReadOnlySharedMemoryRegion();
}

base::ReadOnlySharedMemoryRegion GetGlobalSharedScenarioRegion() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  PerformanceScenarioData* state_ptr = GetGlobalSharedState();
  return state_ptr ? state_ptr->shared_state().DuplicateReadOnlyRegion()
                   : base::ReadOnlySharedMemoryRegion();
}

void SetLoadingScenarioForProcess(LoadingScenario scenario,
                                  content::RenderProcessHost* host) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  SetScenarioValueForRenderProcessHost(scenario, host);
}

void SetLoadingScenarioForProcessNode(LoadingScenario scenario,
                                      const ProcessNode* process_node) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  SetScenarioValue(ScenarioScope::kCurrentProcess, scenario,
                   GetSharedStateForProcessNode(process_node));
}

void SetGlobalLoadingScenario(LoadingScenario scenario) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  SetScenarioValue(ScenarioScope::kGlobal, scenario, GetGlobalSharedState());
}

void SetInputScenarioForProcess(InputScenario scenario,
                                content::RenderProcessHost* host) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  SetScenarioValueForRenderProcessHost(scenario, host);
}

void SetInputScenarioForProcessNode(InputScenario scenario,
                                    const ProcessNode* process_node) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  SetScenarioValue(ScenarioScope::kCurrentProcess, scenario,
                   GetSharedStateForProcessNode(process_node));
}

void SetGlobalInputScenario(InputScenario scenario) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  SetScenarioValue(ScenarioScope::kGlobal, scenario, GetGlobalSharedState());
}

}  // namespace performance_manager