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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
content / browser / scheduler / responsiveness / calculator.cc [blame]
// 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.
#include "content/browser/scheduler/responsiveness/calculator.h"
#include <algorithm>
#include <set>
#include <utility>
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "content/public/browser/browser_thread.h"
namespace content {
namespace responsiveness {
namespace {
// We divide the measurement interval into discretized time slices. Each slice
// is marked as congested if it contained a congested task. A congested task is
// one whose execution latency is greater than kCongestionThreshold.
constexpr auto kMeasurementPeriod = base::Seconds(30);
// A task or event longer than kCongestionThreshold is considered congested.
constexpr auto kCongestionThreshold = base::Milliseconds(100);
// If there have been no events/tasks on the UI thread for a significant period
// of time, it's likely because Chrome was suspended.
// This value is copied from queueing_time_estimator.cc:kInvalidPeriodThreshold.
constexpr auto kSuspendInterval = base::Seconds(30);
constexpr char kLatencyEventCategory[] = "latency";
// The names emitted for CongestedIntervals measurement events.
constexpr char kCongestedIntervalEvent[] = "CongestedInterval";
constexpr char kCongestedIntervalsMeasurementEvent[] =
"CongestedIntervals measurement period";
// Given a |congestion|, finds each congested slice between |start_time| and
// |end_time|, and adds it to |congested_slices|.
void AddCongestedSlices(std::set<int>* congested_slices,
const Calculator::Congestion& congestion,
base::TimeTicks start_time,
base::TimeTicks end_time) {
// Ignore the first congestion threshold, since that's the part of the
// task/event that wasn't congested.
base::TimeTicks congestion_start =
congestion.start_time + kCongestionThreshold;
// Bound by |start_time| and |end_time|.
congestion_start = std::max(congestion_start, start_time);
base::TimeTicks congestion_end = std::min(congestion.end_time, end_time);
// Find each congested slice, and add it to |congested_slices|.
while (congestion_start < congestion_end) {
// Convert |congestion_start| to a slice label.
int64_t label =
(congestion_start - start_time).IntDiv(kCongestionThreshold);
congested_slices->insert(label);
congestion_start += kCongestionThreshold;
}
}
} // namespace
Calculator::Congestion::Congestion(base::TimeTicks start_time,
base::TimeTicks end_time)
: start_time(start_time), end_time(end_time) {
DCHECK_LE(start_time, end_time);
}
Calculator::Calculator(
std::unique_ptr<ResponsivenessCalculatorDelegate> delegate)
: last_calculation_time_(base::TimeTicks::Now()),
most_recent_activity_time_(last_calculation_time_),
delegate_(std::move(delegate))
#if BUILDFLAG(IS_ANDROID)
,
application_status_listener_(
base::android::ApplicationStatusListener::New(
base::BindRepeating(&Calculator::OnApplicationStateChanged,
// Listener is destroyed at destructor, and
// object will be alive for any callback.
base::Unretained(this)))) {
// This class assumes construction and access from the UI thread from all
// methods that aren't explicitly flagged otherwise (i.e. *OnIOThread()).
DCHECK_CURRENTLY_ON(BrowserThread::UI);
OnApplicationStateChanged(
base::android::ApplicationStatusListener::GetState());
}
#else
{
}
#endif
Calculator::~Calculator() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
void Calculator::TaskOrEventFinishedOnUIThread(
base::TimeTicks queue_time,
base::TimeTicks execution_start_time,
base::TimeTicks execution_finish_time) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK_GE(execution_start_time, queue_time);
if (execution_finish_time - queue_time >= kCongestionThreshold) {
GetCongestionOnUIThread().emplace_back(queue_time, execution_finish_time);
if (execution_finish_time - execution_start_time >= kCongestionThreshold) {
GetExecutionCongestionOnUIThread().emplace_back(execution_start_time,
execution_finish_time);
}
}
// We rely on the assumption that |finish_time| is the current time.
CalculateResponsivenessIfNecessary(/*current_time=*/execution_finish_time);
}
void Calculator::TaskOrEventFinishedOnIOThread(
base::TimeTicks queue_time,
base::TimeTicks execution_start_time,
base::TimeTicks execution_finish_time) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_GE(execution_start_time, queue_time);
if (execution_finish_time - queue_time >= kCongestionThreshold) {
base::AutoLock lock(io_thread_lock_);
congestion_on_io_thread_.emplace_back(queue_time, execution_finish_time);
if (execution_finish_time - execution_start_time >= kCongestionThreshold) {
execution_congestion_on_io_thread_.emplace_back(execution_start_time,
execution_finish_time);
}
}
}
void Calculator::OnFirstIdle() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!past_first_idle_);
past_first_idle_ = true;
}
void Calculator::EmitResponsiveness(CongestionType congestion_type,
size_t num_congested_slices,
StartupStage startup_stage) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
static constexpr size_t kMaxCongestedSlices =
kMeasurementPeriod / kCongestionThreshold;
static constexpr size_t kBucketCount = 50;
DCHECK_LE(num_congested_slices, kMaxCongestedSlices);
switch (congestion_type) {
case CongestionType::kExecutionOnly: {
UMA_HISTOGRAM_COUNTS_1000("Browser.MainThreadsCongestion.RunningOnly",
num_congested_slices);
// Only kFirstInterval and kPeriodic are reported with a suffix, stages
// in between are only part of the unsuffixed histogram.
if (startup_stage_ == StartupStage::kFirstInterval) {
UMA_HISTOGRAM_COUNTS_1000(
"Browser.MainThreadsCongestion.RunningOnly.Initial",
num_congested_slices);
} else if (startup_stage_ == StartupStage::kPeriodic) {
UMA_HISTOGRAM_COUNTS_1000(
"Browser.MainThreadsCongestion.RunningOnly.Periodic",
num_congested_slices);
}
break;
}
case CongestionType::kQueueAndExecution: {
// Queuing congestion doesn't count before OnFirstIdle().
if (startup_stage_ == StartupStage::kFirstInterval ||
startup_stage_ == StartupStage::kFirstIntervalDoneWithoutFirstIdle) {
break;
}
UMA_HISTOGRAM_CUSTOM_COUNTS("Browser.MainThreadsCongestion",
num_congested_slices, 1, kMaxCongestedSlices,
kBucketCount);
if (delegate_) {
delegate_->OnResponsivenessEmitted(num_congested_slices, 1,
kMaxCongestedSlices, kBucketCount);
}
if (startup_stage_ == StartupStage::kFirstIntervalAfterFirstIdle) {
UMA_HISTOGRAM_CUSTOM_COUNTS("Browser.MainThreadsCongestion.Initial",
num_congested_slices, 1,
kMaxCongestedSlices, kBucketCount);
} else if (startup_stage_ == StartupStage::kPeriodic) {
UMA_HISTOGRAM_CUSTOM_COUNTS("Browser.MainThreadsCongestion.Periodic",
num_congested_slices, 1,
kMaxCongestedSlices, kBucketCount);
}
break;
}
}
}
void Calculator::EmitResponsivenessTraceEvents(
CongestionType congestion_type,
base::TimeTicks start_time,
base::TimeTicks end_time,
const std::set<int>& congested_slices) {
// Only output kCongestedIntervalsMeasurementEvent event when there are
// congested slices during the measurement.
if (congested_slices.empty() ||
congestion_type != CongestionType::kQueueAndExecution)
return;
// Emit a trace event to highlight the duration of congested intervals
// measurement.
EmitCongestedIntervalsMeasurementTraceEvent(start_time, end_time,
congested_slices.size());
// |congested_slices| contains the id of congested slices, e.g.
// {3,6,7,8,41,42}. As such if the slice following slice x is x+1, we coalesce
// it.
std::set<int>::const_iterator congested_slice_it = congested_slices.begin();
while (congested_slice_it != congested_slices.end()) {
const int start_slice = *congested_slice_it;
// Find the first slice that is not in the current sequence. After the loop,
// |congested_slice_it| will point to the first congested slice in the next
// sequence(or end() if at the end of the slices) while |current_slice|
// will point to the first non-congested slice number which correspond to
// the end of the current sequence.
int current_slice = start_slice;
do {
++congested_slice_it;
++current_slice;
} while (congested_slice_it != congested_slices.end() &&
*congested_slice_it == current_slice);
// Output a trace event for the range [start_slice, current_slice[.
EmitCongestedIntervalTraceEvent(
start_time + start_slice * kCongestionThreshold,
start_time + current_slice * kCongestionThreshold);
}
}
void Calculator::EmitCongestedIntervalsMeasurementTraceEvent(
base::TimeTicks start_time,
base::TimeTicks end_time,
size_t amount_of_slices) {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP1(
kLatencyEventCategory, kCongestedIntervalsMeasurementEvent,
TRACE_ID_LOCAL(this), start_time, "amount_of_slices", amount_of_slices);
TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(
kLatencyEventCategory, kCongestedIntervalsMeasurementEvent,
TRACE_ID_LOCAL(this), end_time);
}
void Calculator::EmitCongestedIntervalTraceEvent(base::TimeTicks start_time,
base::TimeTicks end_time) {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
kLatencyEventCategory, kCongestedIntervalEvent, TRACE_ID_LOCAL(this),
start_time);
TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(
kLatencyEventCategory, kCongestedIntervalEvent, TRACE_ID_LOCAL(this),
end_time);
}
base::TimeTicks Calculator::GetLastCalculationTime() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return last_calculation_time_;
}
void Calculator::CalculateResponsivenessIfNecessary(
base::TimeTicks current_time) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::TimeTicks last_activity_time = most_recent_activity_time_;
most_recent_activity_time_ = current_time;
// We intentionally dump all data if it appears that Chrome was suspended.
// [e.g. machine is asleep, process is backgrounded on Android]. We don't have
// an explicit signal for this. Instead, we rely on the assumption that when
// Chrome is not suspended, there is a steady stream of tasks and events on
// the UI thread. If there's been a significant amount of time since the last
// calculation, then it's likely because Chrome was suspended.
bool is_suspended = current_time - last_activity_time > kSuspendInterval;
#if BUILDFLAG(IS_ANDROID)
is_suspended |= !is_application_visible_;
#endif
if (is_suspended) {
// Notify the delegate that the interval ended so that it can reset its
// accumulated data for the current interval.
if (delegate_) {
delegate_->OnMeasurementIntervalEnded();
}
last_calculation_time_ = current_time;
GetExecutionCongestionOnUIThread().clear();
GetCongestionOnUIThread().clear();
{
base::AutoLock lock(io_thread_lock_);
execution_congestion_on_io_thread_.clear();
congestion_on_io_thread_.clear();
}
return;
}
base::TimeDelta time_since_last_calculation =
current_time - last_calculation_time_;
if (time_since_last_calculation <= kMeasurementPeriod)
return;
// At least |kMeasurementPeriod| time has passed, so we want to move forward
// |last_calculation_time_| and make measurements based on congestions in that
// interval.
const base::TimeTicks new_calculation_time =
current_time - (time_since_last_calculation % kMeasurementPeriod);
// Acquire the congestions in the measurement interval from the UI and IO
// threads.
std::vector<CongestionList> execution_congestion_from_multiple_threads;
std::vector<CongestionList> congestion_from_multiple_threads;
execution_congestion_from_multiple_threads.push_back(
TakeCongestionsOlderThanTime(&GetExecutionCongestionOnUIThread(),
new_calculation_time));
congestion_from_multiple_threads.push_back(TakeCongestionsOlderThanTime(
&GetCongestionOnUIThread(), new_calculation_time));
{
base::AutoLock lock(io_thread_lock_);
execution_congestion_from_multiple_threads.push_back(
TakeCongestionsOlderThanTime(&execution_congestion_on_io_thread_,
new_calculation_time));
congestion_from_multiple_threads.push_back(TakeCongestionsOlderThanTime(
&congestion_on_io_thread_, new_calculation_time));
}
if (delegate_) {
delegate_->OnMeasurementIntervalEnded();
}
CalculateResponsiveness(CongestionType::kExecutionOnly,
std::move(execution_congestion_from_multiple_threads),
last_calculation_time_, new_calculation_time);
CalculateResponsiveness(CongestionType::kQueueAndExecution,
std::move(congestion_from_multiple_threads),
last_calculation_time_, new_calculation_time);
if (startup_stage_ == StartupStage::kFirstInterval)
startup_stage_ = StartupStage::kFirstIntervalDoneWithoutFirstIdle;
if (startup_stage_ == StartupStage::kFirstIntervalDoneWithoutFirstIdle &&
past_first_idle_) {
startup_stage_ = StartupStage::kFirstIntervalAfterFirstIdle;
} else if (startup_stage_ == StartupStage::kFirstIntervalAfterFirstIdle) {
startup_stage_ = StartupStage::kPeriodic;
}
last_calculation_time_ = new_calculation_time;
}
void Calculator::CalculateResponsiveness(
CongestionType congestion_type,
std::vector<CongestionList> congestions_from_multiple_threads,
base::TimeTicks start_time,
base::TimeTicks end_time) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
while (start_time < end_time) {
const base::TimeTicks current_interval_end_time =
start_time + kMeasurementPeriod;
// We divide the current measurement interval into slices. Each slice is
// given a monotonically increasing label, from 0 to |kNumberOfSlices - 1|.
// Example [all times in milliseconds since UNIX epoch]:
// The measurement interval is [50135, 80135].
// The slice [50135, 50235] is labeled 0.
// The slice [50235, 50335] is labeled 1.
// ...
// The slice [80035, 80135] is labeled 299.
std::set<int> congested_slices;
for (const CongestionList& congestions :
congestions_from_multiple_threads) {
for (const Congestion& congestion : congestions) {
AddCongestedSlices(&congested_slices, congestion, start_time,
current_interval_end_time);
}
}
EmitResponsiveness(congestion_type, congested_slices.size(),
startup_stage_);
// If the 'latency' tracing category is enabled and we are ready to observe
// queuing times (past first idle), emit trace events for the measurement
// duration and the congested slices.
bool latency_category_enabled;
TRACE_EVENT_CATEGORY_GROUP_ENABLED(kLatencyEventCategory,
&latency_category_enabled);
if (latency_category_enabled &&
(startup_stage_ == StartupStage::kFirstIntervalAfterFirstIdle ||
startup_stage_ == StartupStage::kPeriodic)) {
EmitResponsivenessTraceEvents(congestion_type, start_time,
current_interval_end_time,
congested_slices);
}
start_time = current_interval_end_time;
}
}
Calculator::CongestionList& Calculator::GetExecutionCongestionOnUIThread() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return execution_congestion_on_ui_thread_;
}
Calculator::CongestionList& Calculator::GetCongestionOnUIThread() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return congestion_on_ui_thread_;
}
#if BUILDFLAG(IS_ANDROID)
void Calculator::OnApplicationStateChanged(
base::android::ApplicationState state) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
switch (state) {
case base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES:
case base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES:
// The application is still visible and partially hidden in paused state.
is_application_visible_ = true;
break;
case base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES:
case base::android::APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES:
is_application_visible_ = false;
break;
case base::android::APPLICATION_STATE_UNKNOWN:
break; // Keep in previous state.
}
}
#endif
// static
Calculator::CongestionList Calculator::TakeCongestionsOlderThanTime(
CongestionList* congestions,
base::TimeTicks end_time) {
// Find all congestions with Congestion.start_time < |end_time|.
auto it = std::partition(congestions->begin(), congestions->end(),
[&end_time](const Congestion& congestion) {
return congestion.start_time < end_time;
});
// Early exit. We don't need to remove any Congestions either, since
// Congestion.end_time
// >= Congestion.start_time.
if (it == congestions->begin())
return CongestionList();
CongestionList congestions_to_return(congestions->begin(), it);
// Remove all congestions with Congestion.end_time < |end_time|.
auto first_congestion_to_keep =
std::partition(congestions->begin(), congestions->end(),
[&end_time](const Congestion& congestion) {
return congestion.end_time < end_time;
});
congestions->erase(congestions->begin(), first_congestion_to_keep);
return congestions_to_return;
}
} // namespace responsiveness
} // namespace content