This source file includes following definitions.
- alarm_count_
- Alarm
- SetDefaultMessagesToSkip
- total_time_watchdog_
- StartProcessingTimers
- EndProcessingTimers
- MessageWillBeMeasured
- AttachToCurrentThread
- DetachFromCurrentThread
- WillProcessIOEvent
- DidProcessIOEvent
- WillProcessTask
- DidProcessTask
- AttachToCurrentThread
- DetachFromCurrentThread
- WillProcessTask
- DidProcessTask
- WillProcessEvent
- DidProcessEvent
- InstallJankometer
- UninstallJankometer
#include "chrome/browser/jankometer.h"
#include <limits>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/metrics/stats_counters.h"
#include "base/pending_task.h"
#include "base/strings/string_util.h"
#include "base/threading/thread.h"
#include "base/threading/watchdog.h"
#include "base/time/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"
using base::TimeDelta;
using base::TimeTicks;
using content::BrowserThread;
namespace {
#ifdef NDEBUG
const int kMaxUIMessageDelayMs = 350;
const int kMaxIOMessageDelayMs = 200;
#else
const int kMaxUIMessageDelayMs = 500;
const int kMaxIOMessageDelayMs = 400;
#endif
const int kMaxMessageProcessingMs = 100;
const bool kPlaySounds = false;
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() OVERRIDE {
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();
bool MessageWillBeMeasured();
static void SetDefaultMessagesToSkip(int count) { discard_count_ = count; }
private:
const TimeDelta max_message_delay_;
bool measure_current_message_;
int events_till_measurement_;
static int discard_count_;
TimeTicks begin_process_message_;
TimeDelta queueing_time_;
base::StatsCounter slow_processing_counter_;
base::StatsCounter queueing_delay_counter_;
base::HistogramBase* const process_times_;
base::HistogramBase* const total_times_;
JankWatchdog total_time_watchdog_;
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) {
events_till_measurement_ = static_cast<int>(
(TimeTicks::Now() - TimeTicks()).InSeconds() % (discard_count_ + 1));
}
}
JankObserverHelper::~JankObserverHelper() {}
void JankObserverHelper::StartProcessingTimers(const TimeDelta& queueing_time) {
DCHECK(measure_current_message_);
begin_process_message_ = TimeTicks::Now();
queueing_time_ = queueing_time;
total_time_watchdog_.ArmSomeTimeDeltaAgo(queueing_time_);
if (queueing_time_ > max_message_delay_) {
queueing_delay_counter_.Increment();
if (kPlaySounds)
MessageBeep(MB_ICONASTERISK);
}
}
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)) {
slow_processing_counter_.Increment();
if (kPlaySounds)
MessageBeep(MB_ICONHAND);
}
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_;
}
int JankObserverHelper::discard_count_ = 99;
class IOJankObserver : public base::RefCountedThreadSafe<IOJankObserver>,
public base::MessageLoopForIO::IOObserver,
public base::MessageLoop::TaskObserver {
public:
IOJankObserver(const char* thread_name,
TimeDelta excessive_duration,
bool watchdog_enable)
: helper_(thread_name, excessive_duration, watchdog_enable) {}
void AttachToCurrentThread() {
base::MessageLoop::current()->AddTaskObserver(this);
base::MessageLoopForIO::current()->AddIOObserver(this);
}
void DetachFromCurrentThread() {
base::MessageLoopForIO::current()->RemoveIOObserver(this);
base::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(const base::PendingTask& pending_task) OVERRIDE {
if (!helper_.MessageWillBeMeasured())
return;
base::TimeTicks now = base::TimeTicks::Now();
const base::TimeDelta queueing_time = now - pending_task.time_posted;
helper_.StartProcessingTimers(queueing_time);
}
virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
helper_.EndProcessingTimers();
}
private:
friend class base::RefCountedThreadSafe<IOJankObserver>;
virtual ~IOJankObserver() {}
JankObserverHelper helper_;
DISALLOW_COPY_AND_ASSIGN(IOJankObserver);
};
class UIJankObserver : public base::RefCountedThreadSafe<UIJankObserver>,
public base::MessageLoop::TaskObserver,
public base::MessageLoopForUI::Observer {
public:
UIJankObserver(const char* thread_name,
TimeDelta excessive_duration,
bool watchdog_enable)
: helper_(thread_name, excessive_duration, watchdog_enable) {}
void AttachToCurrentThread() {
DCHECK(base::MessageLoopForUI::IsCurrent());
base::MessageLoopForUI::current()->AddObserver(this);
base::MessageLoop::current()->AddTaskObserver(this);
}
void DetachFromCurrentThread() {
DCHECK(base::MessageLoopForUI::IsCurrent());
base::MessageLoop::current()->RemoveTaskObserver(this);
base::MessageLoopForUI::current()->RemoveObserver(this);
}
virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
if (!helper_.MessageWillBeMeasured())
return;
base::TimeTicks now = base::TimeTicks::Now();
const base::TimeDelta queueing_time = now - pending_task.time_posted;
helper_.StartProcessingTimers(queueing_time);
}
virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
helper_.EndProcessingTimers();
}
virtual base::EventStatus WillProcessEvent(
const base::NativeEvent& event) OVERRIDE {
if (!helper_.MessageWillBeMeasured())
return base::EVENT_CONTINUE;
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();
}
private:
friend class base::RefCountedThreadSafe<UIJankObserver>;
virtual ~UIJankObserver() {}
JankObserverHelper helper_;
DISALLOW_COPY_AND_ASSIGN(UIJankObserver);
};
const scoped_refptr<UIJankObserver>* ui_observer = NULL;
const scoped_refptr<IOJankObserver>* io_observer = NULL;
}
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);
ui_observer = new scoped_refptr<UIJankObserver>(
new UIJankObserver(
"UI",
TimeDelta::FromMilliseconds(kMaxUIMessageDelayMs),
ui_watchdog_enabled));
(*ui_observer)->AttachToCurrentThread();
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) {
DCHECK((!g_browser_process) || !(g_browser_process->io_thread()));
delete io_observer;
io_observer = NULL;
}
}