summaryrefslogtreecommitdiffstats
path: root/chrome/browser/metrics/thread_watcher.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/metrics/thread_watcher.cc')
-rw-r--r--chrome/browser/metrics/thread_watcher.cc206
1 files changed, 60 insertions, 146 deletions
diff --git a/chrome/browser/metrics/thread_watcher.cc b/chrome/browser/metrics/thread_watcher.cc
index b1597f7..ececcac 100644
--- a/chrome/browser/metrics/thread_watcher.cc
+++ b/chrome/browser/metrics/thread_watcher.cc
@@ -2,16 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#if 0
+
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/metrics/metrics_service.h"
#include "chrome/browser/metrics/thread_watcher.h"
#include "chrome/common/notification_service.h"
-#if defined(OS_WIN)
-#include <Objbase.h>
-#endif
-
// static
const int ThreadWatcher::kPingCount = 3;
@@ -44,10 +42,11 @@ void ThreadWatcher::StartWatching(const BrowserThread::ID thread_id,
DCHECK_GE(sleep_time.InMilliseconds(), 0);
DCHECK_GE(unresponsive_time.InMilliseconds(), sleep_time.InMilliseconds());
- // If we are not on WatchDogThread, then post a task to call StartWatching on
- // WatchDogThread.
- if (!WatchDogThread::CurrentlyOnWatchDogThread()) {
- WatchDogThread::PostTask(
+ // If we are not on WATCHDOG thread, then post a task to call StartWatching on
+ // WATCHDOG thread.
+ if (!BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG)) {
+ BrowserThread::PostTask(
+ BrowserThread::WATCHDOG,
FROM_HERE,
NewRunnableFunction(
&ThreadWatcher::StartWatching,
@@ -55,7 +54,7 @@ void ThreadWatcher::StartWatching(const BrowserThread::ID thread_id,
return;
}
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG));
// Create a new thread watcher object for the given thread and activate it.
ThreadWatcher* watcher =
@@ -65,7 +64,7 @@ void ThreadWatcher::StartWatching(const BrowserThread::ID thread_id,
}
void ThreadWatcher::ActivateThreadWatching() {
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG));
if (active_) return;
active_ = true;
ping_count_ = kPingCount;
@@ -75,14 +74,14 @@ void ThreadWatcher::ActivateThreadWatching() {
}
void ThreadWatcher::DeActivateThreadWatching() {
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG));
active_ = false;
ping_count_ = 0;
method_factory_.RevokeAll();
}
void ThreadWatcher::WakeUp() {
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG));
// There is some user activity, PostPingMessage task of thread watcher if
// needed.
if (!active_) return;
@@ -96,7 +95,7 @@ void ThreadWatcher::WakeUp() {
}
void ThreadWatcher::PostPingMessage() {
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG));
// If we have stopped watching or if the user is idle, then stop sending
// ping messages.
if (!active_ || ping_count_ <= 0)
@@ -127,7 +126,7 @@ void ThreadWatcher::PostPingMessage() {
}
void ThreadWatcher::OnPongMessage(uint64 ping_sequence_number) {
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG));
// Record watched thread's response time.
base::TimeDelta response_time = base::TimeTicks::Now() - ping_time_;
histogram_->AddTime(response_time);
@@ -153,7 +152,7 @@ void ThreadWatcher::OnPongMessage(uint64 ping_sequence_number) {
}
bool ThreadWatcher::OnCheckResponsiveness(uint64 ping_sequence_number) {
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG));
// If we have stopped watching then consider thread as responding.
if (!active_)
return true;
@@ -179,7 +178,7 @@ void ThreadWatcher::OnPingMessage(const BrowserThread::ID thread_id,
Task* callback_task) {
// This method is called on watched thread.
DCHECK(BrowserThread::CurrentlyOn(thread_id));
- WatchDogThread::PostTask(FROM_HERE, callback_task);
+ BrowserThread::PostTask(BrowserThread::WATCHDOG, FROM_HERE, callback_task);
}
//------------------------------------------------------------------------------
@@ -192,15 +191,22 @@ ThreadWatcherList::ThreadWatcherList()
: last_wakeup_time_(base::TimeTicks::Now()) {
// Assert we are not running on WATCHDOG thread. Would be ideal to assert we
// are on UI thread, but Unit tests are not running on UI thread.
- DCHECK(!WatchDogThread::CurrentlyOnWatchDogThread());
- CHECK(!global_);
+ DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG));
+ DCHECK(!global_);
global_ = this;
// Register Notifications observer.
+#if defined(OS_WIN)
MetricsService::SetupNotifications(&registrar_, this);
+#endif
}
ThreadWatcherList::~ThreadWatcherList() {
base::AutoLock auto_lock(lock_);
+ while (!registered_.empty()) {
+ RegistrationList::iterator it = registered_.begin();
+ delete it->second;
+ registered_.erase(it->first);
+ }
DCHECK(this == global_);
global_ = NULL;
}
@@ -214,76 +220,30 @@ void ThreadWatcherList::Register(ThreadWatcher* watcher) {
}
// static
-void ThreadWatcherList::StartWatchingAll() {
- if (!WatchDogThread::CurrentlyOnWatchDogThread()) {
- WatchDogThread::PostDelayedTask(
- FROM_HERE,
- NewRunnableFunction(&ThreadWatcherList::StartWatchingAll),
- base::TimeDelta::FromSeconds(10).InMilliseconds());
- return;
- }
-
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
- const base::TimeDelta kSleepTime = base::TimeDelta::FromSeconds(5);
- const base::TimeDelta kUnresponsiveTime = base::TimeDelta::FromSeconds(10);
- if (BrowserThread::IsMessageLoopValid(BrowserThread::UI)) {
- ThreadWatcher::StartWatching(BrowserThread::UI, "UI", kSleepTime,
- kUnresponsiveTime);
- }
- if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
- ThreadWatcher::StartWatching(BrowserThread::IO, "IO", kSleepTime,
- kUnresponsiveTime);
- }
- if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) {
- ThreadWatcher::StartWatching(BrowserThread::DB, "DB", kSleepTime,
- kUnresponsiveTime);
- }
- if (BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
- ThreadWatcher::StartWatching(BrowserThread::FILE, "FILE", kSleepTime,
- kUnresponsiveTime);
- }
- if (BrowserThread::IsMessageLoopValid(BrowserThread::CACHE)) {
- ThreadWatcher::StartWatching(BrowserThread::CACHE, "CACHE", kSleepTime,
- kUnresponsiveTime);
- }
-}
-
-// static
void ThreadWatcherList::StopWatchingAll() {
- // Assert we are not running on WATCHDOG thread. Would be ideal to assert we
- // are on UI thread, but Unit tests are not running on UI thread.
- DCHECK(!WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!global_)
return;
-
- // Remove all notifications for all watched threads.
- RemoveNotifications();
-
- // Delete all thread watcher objects on WatchDogThread.
- WatchDogThread::PostTask(
- FROM_HERE,
- NewRunnableMethod(global_, &ThreadWatcherList::DeleteAll));
+ base::AutoLock auto_lock(global_->lock_);
+ for (RegistrationList::iterator it = global_->registered_.begin();
+ global_->registered_.end() != it;
+ ++it)
+ BrowserThread::PostTask(
+ BrowserThread::WATCHDOG,
+ FROM_HERE,
+ NewRunnableMethod(
+ it->second, &ThreadWatcher::DeActivateThreadWatching));
}
// static
void ThreadWatcherList::RemoveNotifications() {
- // Assert we are not running on WATCHDOG thread. Would be ideal to assert we
- // are on UI thread, but Unit tests are not running on UI thread.
- DCHECK(!WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!global_)
return;
+#if defined(OS_WIN)
base::AutoLock auto_lock(global_->lock_);
global_->registrar_.RemoveAll();
-}
-
-void ThreadWatcherList::DeleteAll() {
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
- base::AutoLock auto_lock(lock_);
- while (!registered_.empty()) {
- RegistrationList::iterator it = registered_.begin();
- delete it->second;
- registered_.erase(it->first);
- }
+#endif
}
void ThreadWatcherList::Observe(NotificationType type,
@@ -300,15 +260,15 @@ void ThreadWatcherList::Observe(NotificationType type,
last_wakeup_time_ = now;
}
}
- if (need_to_awaken) {
- WatchDogThread::PostTask(
+ if (need_to_awaken)
+ BrowserThread::PostTask(
+ BrowserThread::WATCHDOG,
FROM_HERE,
NewRunnableMethod(this, &ThreadWatcherList::WakeUpAll));
- }
}
void ThreadWatcherList::WakeUpAll() {
- DCHECK(WatchDogThread::CurrentlyOnWatchDogThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WATCHDOG));
base::AutoLock auto_lock(lock_);
for (RegistrationList::iterator it = global_->registered_.begin();
global_->registered_.end() != it;
@@ -334,88 +294,42 @@ ThreadWatcher* ThreadWatcherList::PreLockedFind(
//------------------------------------------------------------------------------
// WatchDogThread methods and members.
-// static
-base::Lock WatchDogThread::lock_;
-// static
-WatchDogThread* WatchDogThread::watchdog_thread_ = NULL;
-
// The WatchDogThread object must outlive any tasks posted to the IO thread
// before the Quit task.
DISABLE_RUNNABLE_METHOD_REFCOUNT(WatchDogThread);
-WatchDogThread::WatchDogThread() : Thread("WATCHDOG") {
+WatchDogThread::WatchDogThread()
+ : BrowserProcessSubThread(BrowserThread::WATCHDOG) {
}
WatchDogThread::~WatchDogThread() {
+ // Remove all notifications for all watched threads.
+ ThreadWatcherList::RemoveNotifications();
// We cannot rely on our base class to stop the thread since we want our
// CleanUp function to run.
Stop();
}
-// static
-bool WatchDogThread::CurrentlyOnWatchDogThread() {
- base::AutoLock lock(lock_);
- return watchdog_thread_ &&
- watchdog_thread_->message_loop() == MessageLoop::current();
-}
-
-// static
-bool WatchDogThread::PostTask(const tracked_objects::Location& from_here,
- Task* task) {
- return PostTaskHelper(from_here, task, 0);
-}
-
-// static
-bool WatchDogThread::PostDelayedTask(const tracked_objects::Location& from_here,
- Task* task,
- int64 delay_ms) {
- return PostTaskHelper(from_here, task, delay_ms);
-}
-
-// static
-bool WatchDogThread::PostTaskHelper(
- const tracked_objects::Location& from_here,
- Task* task,
- int64 delay_ms) {
- {
- base::AutoLock lock(lock_);
-
- MessageLoop* message_loop = watchdog_thread_ ?
- watchdog_thread_->message_loop() : NULL;
- if (message_loop) {
- message_loop->PostDelayedTask(from_here, task, delay_ms);
- return true;
- }
- }
- delete task;
-
- return false;
-}
-
void WatchDogThread::Init() {
// This thread shouldn't be allowed to perform any blocking disk I/O.
base::ThreadRestrictions::SetIOAllowed(false);
-#if defined(OS_WIN)
- // Initializes the COM library on the current thread.
- HRESULT result = CoInitialize(NULL);
- CHECK(result == S_OK);
-#endif
-
- base::AutoLock lock(lock_);
- CHECK(!watchdog_thread_);
- watchdog_thread_ = this;
-}
-
-void WatchDogThread::CleanUp() {
- base::AutoLock lock(lock_);
- watchdog_thread_ = NULL;
-}
+ BrowserProcessSubThread::Init();
-void WatchDogThread::CleanUpAfterMessageLoopDestruction() {
#if defined(OS_WIN)
- // Closes the COM library on the current thread. CoInitialize must
- // be balanced by a corresponding call to CoUninitialize.
- CoUninitialize();
+ const base::TimeDelta kSleepTime = base::TimeDelta::FromSeconds(5);
+ const base::TimeDelta kUnresponsiveTime = base::TimeDelta::FromSeconds(10);
+ ThreadWatcher::StartWatching(BrowserThread::UI, "UI", kSleepTime,
+ kUnresponsiveTime);
+ ThreadWatcher::StartWatching(BrowserThread::IO, "IO", kSleepTime,
+ kUnresponsiveTime);
+ ThreadWatcher::StartWatching(BrowserThread::DB, "DB", kSleepTime,
+ kUnresponsiveTime);
+ ThreadWatcher::StartWatching(BrowserThread::FILE, "FILE", kSleepTime,
+ kUnresponsiveTime);
+ ThreadWatcher::StartWatching(BrowserThread::CACHE, "CACHE", kSleepTime,
+ kUnresponsiveTime);
#endif
}
+
+#endif // 0