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
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <limits>
#include "chrome/browser/jankometer.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/metrics/stats_counters.h"
#include "base/string_util.h"
#include "base/threading/thread.h"
#include "base/threading/watchdog.h"
#include "base/time.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/browser_thread.h"
#if defined(TOOLKIT_USES_GTK)
#include "chrome/browser/ui/gtk/gtk_util.h"
#endif
using base::TimeDelta;
using base::TimeTicks;
using content::BrowserThread;
namespace {
// The maximum threshold of delay of the message before considering it a delay.
// For a debug build, you may want to set IO delay around 500ms.
// For a release build, setting it around 350ms is sensible.
// Visit about:histograms to see what the distribution is on your system, with
// your build. Be sure to do some work to get interesting stats.
// The numbers below came from a warm start (you'll get about 5-10 alarms with
// a cold start), and running the page-cycler for 5 rounds.
#ifdef NDEBUG
const int kMaxUIMessageDelayMs = 350;
const int kMaxIOMessageDelayMs = 200;
#else
const int kMaxUIMessageDelayMs = 500;
const int kMaxIOMessageDelayMs = 400;
#endif
// Maximum processing time (excluding queueing delay) for a message before
// considering it delayed.
const int kMaxMessageProcessingMs = 100;
// TODO(brettw) Consider making this a pref.
const bool kPlaySounds = false;
//------------------------------------------------------------------------------
// Provide a special watchdog to make it easy to set the breakpoint on this
// class only.
class JankWatchdog : public base::Watchdog {
public:
JankWatchdog(const TimeDelta& duration,
const std::string& thread_watched_name,
bool enabled)
: Watchdog(duration, thread_watched_name, enabled),
thread_name_watched_(thread_watched_name),
alarm_count_(0) {
}
virtual ~JankWatchdog() {}
virtual void Alarm() {
// Put break point here if you want to stop threads and look at what caused
// the jankiness.
alarm_count_++;
Watchdog::Alarm();
}
private:
std::string thread_name_watched_;
int alarm_count_;
DISALLOW_COPY_AND_ASSIGN(JankWatchdog);
};
class JankObserverHelper {
public:
JankObserverHelper(const std::string& thread_name,
const TimeDelta& excessive_duration,
bool watchdog_enable);
~JankObserverHelper();
void StartProcessingTimers(const TimeDelta& queueing_time);
void EndProcessingTimers();
// Indicate if we will bother to measuer this message.
bool MessageWillBeMeasured();
static void SetDefaultMessagesToSkip(int count) { discard_count_ = count; }
private:
const TimeDelta max_message_delay_;
// Indicate if we'll bother measuring this message.
bool measure_current_message_;
// Down counter which will periodically hit 0, and only then bother to measure
// the corresponding message.
int events_till_measurement_;
// The value to re-initialize events_till_measurement_ after it reaches 0.
static int discard_count_;
// Time at which the current message processing began.
TimeTicks begin_process_message_;
// Time the current message spent in the queue -- delta between message
// construction time and message processing time.
TimeDelta queueing_time_;
// Counters for the two types of jank we measure.
base::StatsCounter slow_processing_counter_; // Msgs w/ long proc time.
base::StatsCounter queueing_delay_counter_; // Msgs w/ long queueing delay.
base::Histogram* const process_times_; // Time spent proc. task.
base::Histogram* const total_times_; // Total queueing plus proc.
JankWatchdog total_time_watchdog_; // Watching for excessive total_time.
DISALLOW_COPY_AND_ASSIGN(JankObserverHelper);
};
JankObserverHelper::JankObserverHelper(
const std::string& thread_name,
const TimeDelta& excessive_duration,
bool watchdog_enable)
: max_message_delay_(excessive_duration),
measure_current_message_(true),
events_till_measurement_(0),
slow_processing_counter_(std::string("Chrome.SlowMsg") + thread_name),
queueing_delay_counter_(std::string("Chrome.DelayMsg") + thread_name),
process_times_(base::Histogram::FactoryGet(
std::string("Chrome.ProcMsgL ") + thread_name,
1, 3600000, 50, base::Histogram::kUmaTargetedHistogramFlag)),
total_times_(base::Histogram::FactoryGet(
std::string("Chrome.TotalMsgL ") + thread_name,
1, 3600000, 50, base::Histogram::kUmaTargetedHistogramFlag)),
total_time_watchdog_(excessive_duration, thread_name, watchdog_enable) {
if (discard_count_ > 0) {
// Select a vaguely random sample-start-point.
events_till_measurement_ = static_cast<int>(
(TimeTicks::Now() - TimeTicks()).InSeconds() % (discard_count_ + 1));
}
}
JankObserverHelper::~JankObserverHelper() {}
// Called when a message has just begun processing, initializes
// per-message variables and timers.
void JankObserverHelper::StartProcessingTimers(const TimeDelta& queueing_time) {
DCHECK(measure_current_message_);
begin_process_message_ = TimeTicks::Now();
queueing_time_ = queueing_time;
// Simulate arming when the message entered the queue.
total_time_watchdog_.ArmSomeTimeDeltaAgo(queueing_time_);
if (queueing_time_ > max_message_delay_) {
// Message is too delayed.
queueing_delay_counter_.Increment();
#if defined(OS_WIN)
if (kPlaySounds)
MessageBeep(MB_ICONASTERISK);
#endif
}
}
// Called when a message has just finished processing, finalizes
// per-message variables and timers.
void JankObserverHelper::EndProcessingTimers() {
if (!measure_current_message_)
return;
total_time_watchdog_.Disarm();
TimeTicks now = TimeTicks::Now();
if (begin_process_message_ != TimeTicks()) {
TimeDelta processing_time = now - begin_process_message_;
process_times_->AddTime(processing_time);
total_times_->AddTime(queueing_time_ + processing_time);
}
if (now - begin_process_message_ >
TimeDelta::FromMilliseconds(kMaxMessageProcessingMs)) {
// Message took too long to process.
slow_processing_counter_.Increment();
#if defined(OS_WIN)
if (kPlaySounds)
MessageBeep(MB_ICONHAND);
#endif
}
// Reset message specific times.
begin_process_message_ = base::TimeTicks();
queueing_time_ = base::TimeDelta();
}
bool JankObserverHelper::MessageWillBeMeasured() {
measure_current_message_ = events_till_measurement_ <= 0;
if (!measure_current_message_)
--events_till_measurement_;
else
events_till_measurement_ = discard_count_;
return measure_current_message_;
}
// static
int JankObserverHelper::discard_count_ = 99; // Measure only 1 in 100.
//------------------------------------------------------------------------------
class IOJankObserver : public base::RefCountedThreadSafe<IOJankObserver>,
public MessageLoopForIO::IOObserver,
public MessageLoop::TaskObserver {
public:
IOJankObserver(const char* thread_name,
TimeDelta excessive_duration,
bool watchdog_enable)
: helper_(thread_name, excessive_duration, watchdog_enable) {}
~IOJankObserver() {}
// Attaches the observer to the current thread's message loop. You can only
// attach to the current thread, so this function can be invoked on another
// thread to attach it.
void AttachToCurrentThread() {
MessageLoop::current()->AddTaskObserver(this);
MessageLoopForIO::current()->AddIOObserver(this);
}
// Detaches the observer to the current thread's message loop.
void DetachFromCurrentThread() {
MessageLoopForIO::current()->RemoveIOObserver(this);
MessageLoop::current()->RemoveTaskObserver(this);
}
virtual void WillProcessIOEvent() OVERRIDE {
if (!helper_.MessageWillBeMeasured())
return;
helper_.StartProcessingTimers(base::TimeDelta());
}
virtual void DidProcessIOEvent() OVERRIDE {
helper_.EndProcessingTimers();
}
virtual void WillProcessTask(base::TimeTicks time_posted) OVERRIDE {
if (!helper_.MessageWillBeMeasured())
return;
base::TimeTicks now = base::TimeTicks::Now();
const base::TimeDelta queueing_time = now - time_posted;
helper_.StartProcessingTimers(queueing_time);
}
virtual void DidProcessTask(base::TimeTicks time_posted) OVERRIDE {
helper_.EndProcessingTimers();
}
private:
friend class base::RefCountedThreadSafe<IOJankObserver>;
JankObserverHelper helper_;
DISALLOW_COPY_AND_ASSIGN(IOJankObserver);
};
//------------------------------------------------------------------------------
class UIJankObserver : public base::RefCountedThreadSafe<UIJankObserver>,
public MessageLoop::TaskObserver,
public MessageLoopForUI::Observer {
public:
UIJankObserver(const char* thread_name,
TimeDelta excessive_duration,
bool watchdog_enable)
: helper_(thread_name, excessive_duration, watchdog_enable) {}
// Attaches the observer to the current thread's message loop. You can only
// attach to the current thread, so this function can be invoked on another
// thread to attach it.
void AttachToCurrentThread() {
DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
MessageLoopForUI::current()->AddObserver(this);
MessageLoop::current()->AddTaskObserver(this);
}
// Detaches the observer to the current thread's message loop.
void DetachFromCurrentThread() {
DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI);
MessageLoop::current()->RemoveTaskObserver(this);
MessageLoopForUI::current()->RemoveObserver(this);
}
virtual void WillProcessTask(base::TimeTicks time_posted) OVERRIDE {
if (!helper_.MessageWillBeMeasured())
return;
base::TimeTicks now = base::TimeTicks::Now();
const base::TimeDelta queueing_time = now - time_posted;
helper_.StartProcessingTimers(queueing_time);
}
virtual void DidProcessTask(base::TimeTicks time_posted) OVERRIDE {
helper_.EndProcessingTimers();
}
#if defined(OS_WIN)
virtual base::EventStatus WillProcessEvent(
const base::NativeEvent& event) OVERRIDE {
if (!helper_.MessageWillBeMeasured())
return base::EVENT_CONTINUE;
// GetMessageTime returns a LONG (signed 32-bit) and GetTickCount returns
// a DWORD (unsigned 32-bit). They both wrap around when the time is longer
// than they can hold. I'm not sure if GetMessageTime wraps around to 0,
// or if the original time comes from GetTickCount, it might wrap around
// to -1.
//
// Therefore, I cast to DWORD so if it wraps to -1 we will correct it. If
// it doesn't, then our time delta will be negative if a message happens
// to straddle the wraparound point, it will still be OK.
DWORD cur_message_issue_time = static_cast<DWORD>(event.time);
DWORD cur_time = GetTickCount();
base::TimeDelta queueing_time =
base::TimeDelta::FromMilliseconds(cur_time - cur_message_issue_time);
helper_.StartProcessingTimers(queueing_time);
return base::EVENT_CONTINUE;
}
virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE {
helper_.EndProcessingTimers();
}
#elif defined(USE_AURA)
virtual base::EventStatus WillProcessEvent(
const base::NativeEvent& event) OVERRIDE {
return base::EVENT_CONTINUE;
}
virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE {
}
#elif defined(TOOLKIT_USES_GTK)
virtual void WillProcessEvent(GdkEvent* event) {
if (!helper_.MessageWillBeMeasured())
return;
// TODO(evanm): we want to set queueing_time_ using
// gdk_event_get_time, but how do you convert that info
// into a delta?
// guint event_time = gdk_event_get_time(event);
base::TimeDelta queueing_time = base::TimeDelta::FromMilliseconds(0);
helper_.StartProcessingTimers(queueing_time);
}
virtual void DidProcessEvent(GdkEvent* event) {
helper_.EndProcessingTimers();
}
#endif
private:
friend class base::RefCountedThreadSafe<UIJankObserver>;
~UIJankObserver() {}
JankObserverHelper helper_;
DISALLOW_COPY_AND_ASSIGN(UIJankObserver);
};
// These objects are created by InstallJankometer and leaked.
const scoped_refptr<UIJankObserver>* ui_observer = NULL;
const scoped_refptr<IOJankObserver>* io_observer = NULL;
} // namespace
void InstallJankometer(const CommandLine& parsed_command_line) {
if (ui_observer || io_observer) {
NOTREACHED() << "Initializing jank-o-meter twice";
return;
}
bool ui_watchdog_enabled = false;
bool io_watchdog_enabled = false;
if (parsed_command_line.HasSwitch(switches::kEnableWatchdog)) {
std::string list =
parsed_command_line.GetSwitchValueASCII(switches::kEnableWatchdog);
if (list.npos != list.find("ui"))
ui_watchdog_enabled = true;
if (list.npos != list.find("io"))
io_watchdog_enabled = true;
}
if (ui_watchdog_enabled || io_watchdog_enabled)
JankObserverHelper::SetDefaultMessagesToSkip(0); // Watch everything.
// Install on the UI thread.
ui_observer = new scoped_refptr<UIJankObserver>(
new UIJankObserver(
"UI",
TimeDelta::FromMilliseconds(kMaxUIMessageDelayMs),
ui_watchdog_enabled));
(*ui_observer)->AttachToCurrentThread();
// Now install on the I/O thread. Hiccups on that thread will block
// interaction with web pages. We must proxy to that thread before we can
// add our observer.
io_observer = new scoped_refptr<IOJankObserver>(
new IOJankObserver(
"IO",
TimeDelta::FromMilliseconds(kMaxIOMessageDelayMs),
io_watchdog_enabled));
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&IOJankObserver::AttachToCurrentThread, io_observer->get()));
}
void UninstallJankometer() {
if (ui_observer) {
(*ui_observer)->DetachFromCurrentThread();
delete ui_observer;
ui_observer = NULL;
}
if (io_observer) {
// IO thread can't be running when we remove observers.
DCHECK((!g_browser_process) || !(g_browser_process->io_thread()));
delete io_observer;
io_observer = NULL;
}
}
|