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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_SCHEDULER_H_
#define CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_SCHEDULER_H_
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "content/common/content_export.h"
namespace content {
class PrefetchContainer;
class PrefetchService;
// Priority of `PrefetchContainer`
//
// - Larger values take priority.
// - If `PrefetchContainer` has a priority larger than `kBurstThreshold`, it
// will be executed with additional capacity. For more details, see the
// comment #algorithm in `PrefetchScheduler::Progress()`.
// - Note that priority should be explicitly re-calculated if related attributes
// of `PrefetchContainer` are changed. See
// `PrefetchScheduler::CalculatePriority()`, the call sites, and
// `PrefetchScheduler::NotifyAttributeMightChangedAndProgressAsync()`.
//
// Maintenance policy:
//
// - Put a priority per reason. Think twice before mixing multiple reasons into
// a priority. (For ease of understanding the behavior.)
// - It's safe to renumber with preserving the order. Do it if needed.
//
// TODO(crbug.com/406403063): Rethink about granularity. We don't stick on the
// above policy. More rough priorities e.g. kBase/kHigh/kBurst might work well.
//
// See also `PrefetchScheduler::NotifyAttributeMightChangedAndProgressAsync()`
// when you add a new one.
enum class PrefetchPriority {
// Default.
kBase = 0,
// For tests. Do not use outside tests.
kHighTest = 1,
// Priority for prefetch ahead of prerender.
kHighAheadOfPrerender = 2,
// It's a threshold for burst. Do not use it in
// `PrefetchQueue::CalculatePriority()`. For burst, see a comment of
// `PrefetchScheduler`.
kBurstThreshold = 10,
// For tests. Do not use outside tests.
kBurstTest = 11,
// Burst priority for prefetch ahead of prerender.
kBurstAheadOfPrerender = 12,
};
// Priority queue for prefetches
//
// It's a pure data structure and intended to be used in only
// `PrefetchScheduler`.
class PrefetchQueue {
public:
struct Item {
Item(base::WeakPtr<PrefetchContainer> prefetch_container,
PrefetchPriority priority);
~Item();
// Movable but not copyable.
Item(const Item&&);
Item& operator=(const Item&&);
Item(const Item&) = delete;
Item& operator=(const Item&) = delete;
base::WeakPtr<PrefetchContainer> prefetch_container;
PrefetchPriority priority;
};
PrefetchQueue();
~PrefetchQueue();
// Not movable nor copyable.
PrefetchQueue(const PrefetchQueue&&) = delete;
PrefetchQueue& operator=(const PrefetchQueue&&) = delete;
PrefetchQueue(const PrefetchQueue&) = delete;
PrefetchQueue& operator=(const PrefetchQueue&) = delete;
size_t size() const { return queue_.size(); }
void Push(base::WeakPtr<PrefetchContainer> prefetch_container,
PrefetchPriority priority);
// Pops `PrefetchContainer`
//
// Returns the most high priority and first-in one iff found satisfying:
//
// - `pred`
// - Priority is larger or equal to `threshold_priority`.
template <class Predicate>
std::optional<PrefetchQueue::Item> Pop(Predicate pred,
PrefetchPriority threshold_priority) {
for (auto it = queue_.cbegin(); it != queue_.cend(); ++it) {
if (it->priority < threshold_priority) {
break;
}
if (pred(*it)) {
PrefetchQueue::Item ret = std::move(*it);
queue_.erase(it);
return std::move(ret);
}
}
return std::nullopt;
}
// Removes `PrefechContainer`.
//
// Returns true iff `PrefetchContainer` exists and is removed.
bool Remove(base::WeakPtr<PrefetchContainer> prefetch_container);
// Updates priority.
//
// Returns true iff priority is updated.
bool MaybeUpdatePriority(PrefetchContainer& prefetch_container,
PrefetchPriority priority);
private:
std::vector<PrefetchQueue::Item> queue_;
};
// Schedules prefetches with limited concurrency.
//
// - Limits concurrently running prefetches.
// - Bursts and use a different limit if a `PrefetchContainer` has priority
// higher than `PrefetchPriority::kBurstThreshold`.
// - Manages waiting prefetches with a priority queue.
// - Prevents stuck (`queue_.size() > 0 && active_set_.size() == 0`).
//
// For more details, see
// https://docs.google.com/document/d/1W0Nk3Nq6NaUXkBppOUC5zyNmhVqMjYShm1bydGYd9qc
class CONTENT_EXPORT PrefetchScheduler {
public:
explicit PrefetchScheduler(PrefetchService* prefetch_service);
~PrefetchScheduler();
// Not movable nor copyable.
PrefetchScheduler(const PrefetchScheduler&&) = delete;
PrefetchScheduler& operator=(const PrefetchScheduler&&) = delete;
PrefetchScheduler(const PrefetchScheduler&) = delete;
PrefetchScheduler& operator=(const PrefetchScheduler&) = delete;
// See `active_set_`.
bool IsInActiveSet(const PrefetchContainer& prefetch_container);
// Modify-and-progress-async methods
//
// To prevent stuck, this class (and `PrefetchService`) must ensure that
// `Progress()` will be called eventually after modification. To ensure that,
// these modification methods automatically emit `Progress()` in async
// fashion.
//
// Contract:
//
// - `PrefetchService` must call `RemoveAndProgressAsync()` before weak
// pointer invalidation of the `PrefetchContainer`.
// - `PrefetchService` must call
// `NotifyAttributeMightChangedAndProgressAsync()` after modification of
// `PrefetchContainer` that can change priority.
//
// [#cant-detect-prefetch-document-manager] Note that `PrefetchScheduler`
// can't detect changes of `PrefetchDocumentManager` and
// `PrefetchDocumentManager::CanPrefetchNow()`. So, `PrefetchService` must
// explicitly call `Progress()`.
//
// If `should_progress` is false, doesn't call `ProgressAsync()`.
void PushAndProgress(PrefetchContainer& prefetch_container);
void PushAndProgressAsync(PrefetchContainer& prefetch_container);
// Note that this doesn't call `PrefetchService::ResetPrefetchContainer()`.
void RemoveAndProgressAsync(PrefetchContainer& prefetch_container,
bool should_progress = true);
void NotifyAttributeMightChangedAndProgressAsync(
PrefetchContainer& prefetch_container,
bool should_proggress = true);
// Progress scheduling
//
// This can call methods of `PrefetchService` with
// `PassKey<PrefetchScheduler>`, e.g. starting the load and eviction.
//
// Contract: See the comment [#cant-detect-prefetch-document-manager].
void Progress();
void SetCalculatePriorityForTesting(
base::RepeatingCallback<PrefetchPriority(const PrefetchContainer&)>
callback);
private:
PrefetchPriority CalculatePriority(
const PrefetchContainer& prefetch_container);
void ProgressAsync();
void ProgressOne(base::WeakPtr<PrefetchContainer> prefetch_container);
// Safety: This class is owned by `PrefetchService` and has the same lifetime.
const raw_ptr<PrefetchService> prefetch_service_;
// Max heap by priority
PrefetchQueue queue_;
// Active set of prefetches, i.e. prefetches that are in loading state
// (roughly).
//
// We use `std::vector` as set-like containers can't use `base::WeakPtr`.
std::vector<base::WeakPtr<PrefetchContainer>> active_set_;
// Used to reduce unnecessary multiple `Progress()` calls.
bool is_progress_scheduled_ = false;
// Used to prevent `PrefetchScheduler::ProgressOne()` ->
// `PrefetchService::EvictPrefetch()` ->
// `PrefetchScheduler::RemoveAndProgressAsync()` trigger
// `PrefetchScheduler::ProgressAsync()`.
bool in_eviction_ = false;
base::RepeatingCallback<PrefetchPriority(const PrefetchContainer&)>
calculate_priority_for_test_;
#if DCHECK_IS_ON()
// Protects against `Progress()` being called recursively.
bool progress_reentrancy_guard_ = false;
#endif
base::WeakPtrFactory<PrefetchScheduler> weak_method_factory_{this};
};
} // namespace content
#endif // CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_SCHEDULER_H_
|