File: parkable_string_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (231 lines) | stat: -rw-r--r-- 9,042 bytes parent folder | download | duplicates (6)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_PARKABLE_STRING_MANAGER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_PARKABLE_STRING_MANAGER_H_

#include <memory>
#include <utility>

#include "base/feature_list.h"
#include "base/gtest_prod_util.h"
#include "base/memory/post_delayed_memory_reduction_task.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/memory_dump_provider.h"
#include "third_party/blink/renderer/platform/bindings/parkable_string.h"
#include "third_party/blink/renderer/platform/disk_data_allocator.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/scheduler/public/rail_mode_observer.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_functions.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class DiskDataAllocator;
class ParkableString;

class PLATFORM_EXPORT ParkableStringManagerDumpProvider
    : public base::trace_event::MemoryDumpProvider {
  USING_FAST_MALLOC(ParkableStringManagerDumpProvider);

 public:
  static ParkableStringManagerDumpProvider* Instance();
  ParkableStringManagerDumpProvider(const ParkableStringManagerDumpProvider&) =
      delete;
  ParkableStringManagerDumpProvider& operator=(
      const ParkableStringManagerDumpProvider&) = delete;
  ~ParkableStringManagerDumpProvider() override;

  bool OnMemoryDump(const base::trace_event::MemoryDumpArgs&,
                    base::trace_event::ProcessMemoryDump*) override;

 private:
  ParkableStringManagerDumpProvider();
};

// Manages all the ParkableStrings, and parks eligible strings after the
// renderer has been backgrounded.
// Main Thread only.
// When a `ParkableString` is unparked on a background thread, a task is posted
// to the main thread to update the entries in the manager. Hence, it is
// possible to temporarily have an unparked `ParkableString` inaccessible
// through `unparked_strings_`. This can cause aging of the string to be
// delayed or a variation on the sizes recorded in 'ComputeStatistics()`.
class PLATFORM_EXPORT ParkableStringManager : public RAILModeObserver {
  USING_FAST_MALLOC(ParkableStringManager);

 public:
  struct Statistics;

  static ParkableStringManager& Instance();
  ParkableStringManager(const ParkableStringManager&) = delete;
  ParkableStringManager& operator=(const ParkableStringManager&) = delete;
  ~ParkableStringManager() override;

  void SetRendererBackgrounded(bool backgrounded);
  void OnRAILModeChanged(RAILMode rail_mode) override;

  void PurgeMemory();
  // Number of parked and unparked strings. Public for testing.
  size_t Size() const;

  bool OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd);

  // Whether a string is parkable or not. Can be called from any thread.
  static bool ShouldPark(const StringImpl& string);

  static base::TimeDelta AgingInterval();

  // According to UMA data (as of 2021-11-09) ~70% of renderers exist for less
  // than 60 seconds. Using this as a delay of the first parking attempts
  // to lower the number of parking operations on strings that are discarded
  // soon after when the renderer dies. Subsequent additions will schedule
  // according to the default delay. This applies to all renderers, young or
  // old. This is considered a necessary trade-off to cover the spare renderer
  // use-case. The spare renderer might have a long lifetime on first addition
  // of a parkable string but it does not indicate a higher chance of the
  // renderer staying alive for a long time.
  constexpr static base::TimeDelta kFirstParkingDelay{base::Seconds(60)};

  static const char* kAllocatorDumpName;

  // Compares not the pointers, but the arrays. Uses pointers to save space.
  struct SecureDigestHashTraits
      : GenericHashTraits<const ParkableStringImpl::SecureDigest*> {
    static unsigned GetHash(const ParkableStringImpl::SecureDigest* digest) {
      // The first bytes of the hash are as good as anything else.
      return *reinterpret_cast<const unsigned*>(digest->data());
    }

    static bool Equal(const ParkableStringImpl::SecureDigest* const a,
                      const ParkableStringImpl::SecureDigest* const b) {
      return a == b || *a == *b;
    }

    static constexpr bool kSafeToCompareToEmptyOrDeleted = false;
  };

  // Relies on secure hash equality for deduplication. If one day SHA256 becomes
  // insecure, then this would need to be updated to a more robust hash.
  using StringMap = WTF::HashMap<const ParkableStringImpl::SecureDigest*,
                                 ParkableStringImpl*,
                                 SecureDigestHashTraits>;

  bool IsOnParkedMapForTesting(ParkableStringImpl* string);
  bool IsOnDiskMapForTesting(ParkableStringImpl* string);

 private:
  friend class ParkableString;
  friend class ParkableStringImpl;

  scoped_refptr<ParkableStringImpl> Add(
      scoped_refptr<StringImpl>&&,
      std::unique_ptr<ParkableStringImpl::SecureDigest> digest);

  void RemoveOnMainThread(ParkableStringImpl* string);
  // If on a background thread, posts a `RemoveOnMainThread` task to the Main
  // thread. Calls `RemoveOnMainThread` otherwise.
  void Remove(ParkableStringImpl* string);

  void OnParked(ParkableStringImpl*);
  void OnWrittenToDisk(ParkableStringImpl*);
  void OnUnparked(ParkableStringImpl*, bool);

  // If on a background thread, posts a `CompleteUnparkOnMainThread` task to
  // the Main thread. Calls `CompleteUnparkOnMainThread` otherwise.
  void CompleteUnpark(ParkableStringImpl* string,
                      base::TimeDelta elapsed,
                      base::TimeDelta disk_elapsed);

  void CompleteUnparkOnMainThread(ParkableStringImpl* string,
                                  base::TimeDelta elapsed,
                                  base::TimeDelta disk_elapsed);

  void ParkAll(ParkableStringImpl::ParkingMode mode);
  void RecordStatisticsAfter5Minutes() const;
  void AgeStringsAndPark(base::MemoryReductionTaskContext context);
  void ScheduleAgingTaskIfNeeded();

  void RecordUnparkingTime(base::TimeDelta unparking_time) {
    total_unparking_time_ += unparking_time;
  }
  void RecordParkingThreadTime(base::TimeDelta parking_thread_time) {
    total_parking_thread_time_ += parking_thread_time;
  }
  void RecordDiskWriteTime(base::TimeDelta write_time) {
    total_disk_write_time_ += write_time;
  }
  void RecordDiskReadTime(base::TimeDelta read_time) {
    total_disk_read_time_ += read_time;
  }

  Statistics ComputeStatistics() const;

  DiskDataAllocator& data_allocator() const {
    if (allocator_for_testing_)
      return *allocator_for_testing_;

    return DiskDataAllocator::Instance();
  }

  void SetDataAllocatorForTesting(
      std::unique_ptr<DiskDataAllocator> allocator) {
    allocator_for_testing_ = std::move(allocator);
  }

  void SetTaskRunnerForTesting(
      scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
    task_runner_ = std::move(task_runner);
  }

  scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
    return task_runner_;
  }

  void AssertRemoved(ParkableStringImpl* string);
  void ResetForTesting();
  bool IsPaused() const;
  bool HasPendingWork() const;
  ParkableStringManager();

  // Arbitrarily chosen, was shown to not regress metrics in a field experiment
  // in 2019 on desktop and Android. From local testing, strings are either
  // requested in a very rapid succession (during compilation), or almost
  // never. We want to allow strings to be dropped quickly, to reduce peak
  // memory usage, particularly as reading and decompressing strings is
  // typically very cheap.
  constexpr static base::TimeDelta kAgingInterval = base::Seconds(2);
  constexpr static base::TimeDelta kLessAggressiveAgingInterval =
      base::Seconds(10);

  bool backgrounded_ = false;
  RAILMode rail_mode_ = RAILMode::kDefault;
  bool has_pending_aging_task_ = false;
  bool has_posted_unparking_time_accounting_task_ = false;
  bool did_register_memory_pressure_listener_ = false;
  base::TimeDelta total_unparking_time_;
  base::TimeDelta total_parking_thread_time_;
  base::TimeDelta total_disk_read_time_;
  base::TimeDelta total_disk_write_time_;

  StringMap unparked_strings_;
  StringMap parked_strings_;
  StringMap on_disk_strings_;

  bool first_string_aging_was_delayed_ = false;

  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  std::unique_ptr<DiskDataAllocator> allocator_for_testing_;

  friend class ParkableStringTest;
  FRIEND_TEST_ALL_PREFIXES(ParkableStringTest, SynchronousCompression);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_PARKABLE_STRING_MANAGER_H_