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 277 278 279 280 281 282 283 284 285 286 287 288
|
// Copyright 2023 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/public/resource_attribution/queries.h"
#include <utility>
#include "base/check.h"
#include "base/containers/enum_set.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/timer/timer.h"
#include "components/performance_manager/resource_attribution/query_params.h"
#include "components/performance_manager/resource_attribution/query_scheduler.h"
namespace resource_attribution {
namespace {
using QueryParams = internal::QueryParams;
using QueryScheduler = internal::QueryScheduler;
// The minimum delay between QueryOnce() calls for kMemorySummary resources.
// This can only be updated in unit tests so doesn't need to be thread-safe.
// Copied from ProcessMetricsDecorator::kMinImmediateRefreshDelay.
// TODO(crbug.com/40926264): Manage timing centrally in QueryScheduler.
base::TimeDelta g_min_memory_query_delay = base::Seconds(2);
} // namespace
class ScopedResourceUsageQuery::ThrottledTimer {
public:
ThrottledTimer() = default;
~ThrottledTimer() = default;
ThrottledTimer(const ThrottledTimer&) = delete;
ThrottledTimer& operator=(const ThrottledTimer&) = delete;
// Starts the timer to repeatedly query `params` after `delay`.
// `observer_list` will be notified with the results.
void StartTimer(base::TimeDelta delay,
internal::QueryParams* params,
scoped_refptr<ObserverList> observer_list);
// Sends the scheduler a request for query results for `params`.
// `observer_list` will be notified with the results. If `timer_fired` is
// true, this is invoked from the timer, otherwise it's invoked from
// QueryOnce().
void SendRequestToScheduler(internal::QueryParams* params,
scoped_refptr<ObserverList> observer_list,
bool timer_fired);
private:
// Returns true if SendRequestToScheduler should be called for `params`, false
// otherwise. This must be called on every request to update state.
bool ShouldSendRequest(internal::QueryParams* params, bool timer_fired);
base::RepeatingTimer timer_;
base::TimeTicks last_fire_time_;
base::TimeTicks next_fire_time_;
base::TimeTicks last_query_once_time_;
};
void ScopedResourceUsageQuery::ThrottledTimer::StartTimer(
base::TimeDelta delay,
internal::QueryParams* params,
scoped_refptr<ObserverList> observer_list) {
CHECK(!timer_.IsRunning());
CHECK(delay.is_positive());
// Unretained is safe because ScopedResourceUsageQuery owns both `this` and
// `params`.
timer_.Start(FROM_HERE, delay,
base::BindRepeating(&ThrottledTimer::SendRequestToScheduler,
base::Unretained(this),
base::Unretained(params), observer_list,
/*timer_fired=*/true));
next_fire_time_ = base::TimeTicks::Now() + delay;
}
void ScopedResourceUsageQuery::ThrottledTimer::SendRequestToScheduler(
internal::QueryParams* params,
scoped_refptr<ObserverList> observer_list,
bool timer_fired) {
if (ShouldSendRequest(params, timer_fired)) {
if (auto* scheduler = QueryScheduler::Get()) {
scheduler->RequestResults(
*params, base::BindOnce(&ScopedResourceUsageQuery::NotifyObservers,
observer_list));
}
}
}
bool ScopedResourceUsageQuery::ThrottledTimer::ShouldSendRequest(
internal::QueryParams* params,
bool timer_fired) {
if (!params->resource_types.Has(ResourceType::kMemorySummary)) {
// Only memory queries are throttled.
return true;
}
const auto now = base::TimeTicks::Now();
if (timer_fired) {
// Repeating queries aren't throttled, but need to save the current time to
// throttle QueryOnce().
CHECK(timer_.IsRunning());
last_fire_time_ = now;
next_fire_time_ = now + timer_.GetCurrentDelay();
return true;
}
// Check if this QueryOnce() should be throttled.
if (!last_query_once_time_.is_null() &&
now < last_query_once_time_ + g_min_memory_query_delay) {
// QueryOnce() called recently.
return false;
}
if (!last_fire_time_.is_null() &&
now < last_fire_time_ + g_min_memory_query_delay) {
// Timer fired recently.
return false;
}
if (!next_fire_time_.is_null() &&
now > next_fire_time_ - g_min_memory_query_delay) {
// Timer is going to fire soon.
return false;
}
last_query_once_time_ = now;
return true;
}
ScopedResourceUsageQuery::ScopedDisableMemoryQueryDelayForTesting::
ScopedDisableMemoryQueryDelayForTesting()
: previous_delay_(g_min_memory_query_delay) {
g_min_memory_query_delay = base::TimeDelta();
}
ScopedResourceUsageQuery::ScopedDisableMemoryQueryDelayForTesting::
~ScopedDisableMemoryQueryDelayForTesting() {
CHECK(g_min_memory_query_delay.is_zero());
g_min_memory_query_delay = previous_delay_;
}
ScopedResourceUsageQuery::~ScopedResourceUsageQuery() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!params_) {
// `params_` was moved to another ScopedResourceUsageQuery.
return;
}
// Notify the scheduler this query no longer exists. Sends the QueryParams to
// the scheduler to delete to be sure they're valid until the scheduler reads
// them.
// TODO(crbug.com/40755583): No need to pass ownership of params since this is
// no longer asynchronous.
if (auto* scheduler = QueryScheduler::Get()) {
scheduler->RemoveScopedQuery(std::move(params_));
}
}
ScopedResourceUsageQuery::ScopedResourceUsageQuery(ScopedResourceUsageQuery&&) =
default;
ScopedResourceUsageQuery& ScopedResourceUsageQuery::operator=(
ScopedResourceUsageQuery&&) = default;
void ScopedResourceUsageQuery::AddObserver(QueryResultObserver* observer) {
// ObserverListThreadSafe can be called on any sequence.
observer_list_->AddObserver(observer);
}
void ScopedResourceUsageQuery::RemoveObserver(QueryResultObserver* observer) {
// Must be called on the same sequence as AddObserver. ObserverListThreadSafe
// will validate this.
observer_list_->RemoveObserver(observer);
}
void ScopedResourceUsageQuery::Start(base::TimeDelta delay) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (auto* scheduler = QueryScheduler::Get()) {
scheduler->StartRepeatingQuery(params_.get());
}
throttled_timer_->StartTimer(delay, params_.get(), observer_list_);
}
void ScopedResourceUsageQuery::QueryOnce() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
throttled_timer_->SendRequestToScheduler(params_.get(), observer_list_,
/*timer_fired=*/false);
}
QueryParams* ScopedResourceUsageQuery::GetParamsForTesting() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return params_.get();
}
// static
base::TimeDelta ScopedResourceUsageQuery::GetMinMemoryQueryDelayForTesting() {
return g_min_memory_query_delay;
}
ScopedResourceUsageQuery::ScopedResourceUsageQuery(
base::PassKey<QueryBuilder>,
std::unique_ptr<QueryParams> params)
: params_(std::move(params)),
throttled_timer_(std::make_unique<ThrottledTimer>()) {
if (auto* scheduler = QueryScheduler::Get()) {
scheduler->AddScopedQuery(params_.get());
}
}
// static
void ScopedResourceUsageQuery::NotifyObservers(
scoped_refptr<ObserverList> observer_list,
const QueryResultMap& results) {
observer_list->Notify(FROM_HERE, &QueryResultObserver::OnResourceUsageUpdated,
results);
}
QueryBuilder::QueryBuilder() : params_(std::make_unique<QueryParams>()) {}
QueryBuilder::~QueryBuilder() = default;
QueryBuilder::QueryBuilder(QueryBuilder&&) = default;
QueryBuilder& QueryBuilder::operator=(QueryBuilder&&) = default;
QueryBuilder& QueryBuilder::AddResourceContext(const ResourceContext& context) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(params_);
params_->contexts.AddResourceContext(context);
return *this;
}
QueryBuilder& QueryBuilder::AddResourceType(ResourceType resource_type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(params_);
params_->resource_types.Put(resource_type);
return *this;
}
ScopedResourceUsageQuery QueryBuilder::CreateScopedQuery() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ValidateQuery();
// Pass ownership of `params_` to the scoped query, to avoid copying the
// parameter contents.
return ScopedResourceUsageQuery(base::PassKey<QueryBuilder>(),
std::move(params_));
}
void QueryBuilder::QueryOnce(
base::OnceCallback<void(const QueryResultMap&)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ValidateQuery();
if (auto* scheduler = QueryScheduler::Get()) {
scheduler->RequestResults(*params_, std::move(callback));
}
params_.reset();
}
QueryBuilder QueryBuilder::Clone() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return QueryBuilder(params_->Clone());
}
QueryParams* QueryBuilder::GetParamsForTesting() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return params_.get();
}
QueryBuilder::QueryBuilder(std::unique_ptr<QueryParams> params)
: params_(std::move(params)) {}
QueryBuilder& QueryBuilder::AddAllContextsWithTypeId(
internal::ResourceContextTypeId type_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(params_);
params_->contexts.AddAllContextsOfType(type_id);
return *this;
}
void QueryBuilder::ValidateQuery() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(params_);
CHECK(!params_->contexts.IsEmpty());
CHECK(!params_->resource_types.empty());
}
} // namespace resource_attribution
|