| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 //------------------------------------------------------------------------------ | 5 //------------------------------------------------------------------------------ |
| 6 | 6 |
| 7 #ifndef CHROME_BROWSER_METRICS_THREAD_WATCHER_H_ | 7 #ifndef CHROME_BROWSER_METRICS_THREAD_WATCHER_H_ |
| 8 #define CHROME_BROWSER_METRICS_THREAD_WATCHER_H_ | 8 #define CHROME_BROWSER_METRICS_THREAD_WATCHER_H_ |
| 9 #pragma once | 9 #pragma once |
| 10 | 10 |
| 11 #if 0 | |
| 12 | |
| 13 #include <map> | 11 #include <map> |
| 14 #include <string> | 12 #include <string> |
| 15 #include <vector> | 13 #include <vector> |
| 16 | 14 |
| 17 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 18 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 17 #include "base/message_loop.h" |
| 19 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
| 20 #include "base/ref_counted.h" | 19 #include "base/ref_counted.h" |
| 21 #include "base/scoped_ptr.h" | 20 #include "base/scoped_ptr.h" |
| 22 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 23 #include "base/task.h" | 22 #include "base/task.h" |
| 24 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
| 25 #include "base/time.h" | 24 #include "base/time.h" |
| 26 #include "chrome/browser/browser_process_sub_thread.h" | |
| 27 #include "chrome/browser/browser_thread.h" | 25 #include "chrome/browser/browser_thread.h" |
| 28 #include "chrome/common/notification_observer.h" | 26 #include "chrome/common/notification_observer.h" |
| 29 #include "chrome/common/notification_registrar.h" | 27 #include "chrome/common/notification_registrar.h" |
| 30 | 28 |
| 31 class CustomThreadWatcher; | 29 class CustomThreadWatcher; |
| 32 class ThreadWatcherList; | 30 class ThreadWatcherList; |
| 33 | 31 |
| 34 //------------------------------------------------------------------------------ | 32 //------------------------------------------------------------------------------ |
| 35 // This class performs health check on threads that would like to be watched. It | 33 // This class performs health check on threads that would like to be watched. It |
| 36 // sends ping message to the watched thread and the watched thread responds back | 34 // sends ping message to the watched thread and the watched thread responds back |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 // This will ensure that the watching is actively taking place, and awaken | 98 // This will ensure that the watching is actively taking place, and awaken |
| 101 // (i.e., post a PostPingMessage) if the watcher has stopped pinging due to | 99 // (i.e., post a PostPingMessage) if the watcher has stopped pinging due to |
| 102 // lack of user activity. It will also reset ping_count_ to kPingCount. | 100 // lack of user activity. It will also reset ping_count_ to kPingCount. |
| 103 virtual void WakeUp(); | 101 virtual void WakeUp(); |
| 104 | 102 |
| 105 // This method records when ping message was sent and it will Post a task | 103 // This method records when ping message was sent and it will Post a task |
| 106 // (OnPingMessage) to the watched thread that does nothing but respond with | 104 // (OnPingMessage) to the watched thread that does nothing but respond with |
| 107 // OnPongMessage. It also posts a task (OnCheckResponsiveness) to check | 105 // OnPongMessage. It also posts a task (OnCheckResponsiveness) to check |
| 108 // responsiveness of monitored thread that would be called after waiting | 106 // responsiveness of monitored thread that would be called after waiting |
| 109 // unresponsive_time_. | 107 // unresponsive_time_. |
| 110 // This method is accessible on WATCHDOG thread. | 108 // This method is accessible on WatchDogThread. |
| 111 virtual void PostPingMessage(); | 109 virtual void PostPingMessage(); |
| 112 | 110 |
| 113 // This method handles a Pong Message from watched thread. It will track the | 111 // This method handles a Pong Message from watched thread. It will track the |
| 114 // response time (pong time minus ping time) via histograms. It posts a | 112 // response time (pong time minus ping time) via histograms. It posts a |
| 115 // PostPingMessage task that would be called after waiting sleep_time_. It | 113 // PostPingMessage task that would be called after waiting sleep_time_. It |
| 116 // increments ping_sequence_number_ by 1. | 114 // increments ping_sequence_number_ by 1. |
| 117 // This method is accessible on WATCHDOG thread. | 115 // This method is accessible on WatchDogThread. |
| 118 virtual void OnPongMessage(uint64 ping_sequence_number); | 116 virtual void OnPongMessage(uint64 ping_sequence_number); |
| 119 | 117 |
| 120 // This method will determine if the watched thread is responsive or not. If | 118 // This method will determine if the watched thread is responsive or not. If |
| 121 // the latest ping_sequence_number_ is not same as the ping_sequence_number | 119 // the latest ping_sequence_number_ is not same as the ping_sequence_number |
| 122 // that is passed in, then we can assume that watched thread has responded | 120 // that is passed in, then we can assume that watched thread has responded |
| 123 // with a pong message. | 121 // with a pong message. |
| 124 // This method is accessible on WATCHDOG thread. | 122 // This method is accessible on WatchDogThread. |
| 125 virtual bool OnCheckResponsiveness(uint64 ping_sequence_number); | 123 virtual bool OnCheckResponsiveness(uint64 ping_sequence_number); |
| 126 | 124 |
| 127 private: | 125 private: |
| 128 friend class ThreadWatcherList; | 126 friend class ThreadWatcherList; |
| 129 | 127 |
| 130 // Allow tests to access our innards for testing purposes. | 128 // Allow tests to access our innards for testing purposes. |
| 131 FRIEND_TEST(ThreadWatcherTest, Registration); | 129 FRIEND_TEST(ThreadWatcherTest, Registration); |
| 132 FRIEND_TEST(ThreadWatcherTest, ThreadResponding); | 130 FRIEND_TEST(ThreadWatcherTest, ThreadResponding); |
| 133 FRIEND_TEST(ThreadWatcherTest, ThreadNotResponding); | 131 FRIEND_TEST(ThreadWatcherTest, ThreadNotResponding); |
| 134 FRIEND_TEST(ThreadWatcherTest, MultipleThreadsResponding); | 132 FRIEND_TEST(ThreadWatcherTest, MultipleThreadsResponding); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 typedef std::map<BrowserThread::ID, ThreadWatcher*> RegistrationList; | 194 typedef std::map<BrowserThread::ID, ThreadWatcher*> RegistrationList; |
| 197 | 195 |
| 198 // This singleton holds the global list of registered ThreadWatchers. | 196 // This singleton holds the global list of registered ThreadWatchers. |
| 199 ThreadWatcherList(); | 197 ThreadWatcherList(); |
| 200 // Destructor deletes all registered ThreadWatcher instances. | 198 // Destructor deletes all registered ThreadWatcher instances. |
| 201 ~ThreadWatcherList(); | 199 ~ThreadWatcherList(); |
| 202 | 200 |
| 203 // Register() stores a pointer to the given ThreadWatcher in a global map. | 201 // Register() stores a pointer to the given ThreadWatcher in a global map. |
| 204 static void Register(ThreadWatcher* watcher); | 202 static void Register(ThreadWatcher* watcher); |
| 205 | 203 |
| 206 // This method posts a task on WATCHDOG thread to RevokeAll tasks and to | 204 // This method posts a task on WatchDogThread to RevokeAll tasks and to |
| 207 // deactive thread watching of other threads and tell NotificationService to | 205 // deactive thread watching of other threads and tell NotificationService to |
| 208 // stop calling Observe. | 206 // stop calling Observe. |
| 207 // This method is accessible on UI thread. |
| 209 static void StopWatchingAll(); | 208 static void StopWatchingAll(); |
| 210 | 209 |
| 211 // RemoveAll NotificationTypes that are being observed. | 210 // RemoveAll NotificationTypes that are being observed. |
| 211 // This method is accessible on UI thread. |
| 212 static void RemoveNotifications(); | 212 static void RemoveNotifications(); |
| 213 | 213 |
| 214 private: | 214 private: |
| 215 // Allow tests to access our innards for testing purposes. | 215 // Allow tests to access our innards for testing purposes. |
| 216 FRIEND_TEST(ThreadWatcherTest, Registration); | 216 FRIEND_TEST(ThreadWatcherTest, Registration); |
| 217 | 217 |
| 218 // Delete all thread watcher objects and remove them from global map. |
| 219 // This method is accessible on WatchDogThread. |
| 220 void DeleteAll(); |
| 221 |
| 218 // This will ensure that the watching is actively taking place. It will wakeup | 222 // This will ensure that the watching is actively taking place. It will wakeup |
| 219 // all thread watchers every 2 seconds. This is the implementation of | 223 // all thread watchers every 2 seconds. This is the implementation of |
| 220 // NotificationObserver. When a matching notification is posted to the | 224 // NotificationObserver. When a matching notification is posted to the |
| 221 // notification service, this method is called. | 225 // notification service, this method is called. |
| 226 // This method is accessible on UI thread. |
| 222 virtual void Observe(NotificationType type, | 227 virtual void Observe(NotificationType type, |
| 223 const NotificationSource& source, | 228 const NotificationSource& source, |
| 224 const NotificationDetails& details); | 229 const NotificationDetails& details); |
| 225 | 230 |
| 226 // This will ensure that the watching is actively taking place, and awaken | 231 // This will ensure that the watching is actively taking place, and awaken |
| 227 // all thread watchers that are registered. | 232 // all thread watchers that are registered. |
| 233 // This method is accessible on WatchDogThread. |
| 228 virtual void WakeUpAll(); | 234 virtual void WakeUpAll(); |
| 229 | 235 |
| 230 // The Find() method can be used to test to see if a given ThreadWatcher was | 236 // The Find() method can be used to test to see if a given ThreadWatcher was |
| 231 // already registered, or to retrieve a pointer to it from the global map. | 237 // already registered, or to retrieve a pointer to it from the global map. |
| 232 static ThreadWatcher* Find(const BrowserThread::ID thread_id); | 238 static ThreadWatcher* Find(const BrowserThread::ID thread_id); |
| 233 | 239 |
| 234 // Helper function should be called only while holding lock_. | 240 // Helper function should be called only while holding lock_. |
| 235 ThreadWatcher* PreLockedFind(const BrowserThread::ID thread_id); | 241 ThreadWatcher* PreLockedFind(const BrowserThread::ID thread_id); |
| 236 | 242 |
| 237 static ThreadWatcherList* global_; // The singleton of this class. | 243 static ThreadWatcherList* global_; // The singleton of this class. |
| 238 | 244 |
| 239 // Lock for access to registered_. | 245 // Lock for access to registered_. |
| 240 base::Lock lock_; | 246 base::Lock lock_; |
| 241 RegistrationList registered_; | 247 RegistrationList registered_; |
| 242 | 248 |
| 243 // The registrar that holds NotificationTypes to be observed. | 249 // The registrar that holds NotificationTypes to be observed. |
| 244 NotificationRegistrar registrar_; | 250 NotificationRegistrar registrar_; |
| 245 | 251 |
| 246 // This is the last time when woke all thread watchers up. | 252 // This is the last time when woke all thread watchers up. |
| 247 base::TimeTicks last_wakeup_time_; | 253 base::TimeTicks last_wakeup_time_; |
| 248 | 254 |
| 249 DISALLOW_COPY_AND_ASSIGN(ThreadWatcherList); | 255 DISALLOW_COPY_AND_ASSIGN(ThreadWatcherList); |
| 250 }; | 256 }; |
| 251 | 257 |
| 252 //------------------------------------------------------------------------------ | 258 //------------------------------------------------------------------------------ |
| 253 // Class for WATCHDOG thread and in its Init method, we start watching UI, IO, | 259 // Class for WatchDogThread and in its Init method, we start watching UI, IO, |
| 254 // DB, FILE, CACHED threads. | 260 // DB, FILE, CACHED threads. |
| 255 class WatchDogThread : public BrowserProcessSubThread { | 261 class WatchDogThread : public base::Thread { |
| 256 public: | 262 public: |
| 263 // Constructor. |
| 257 WatchDogThread(); | 264 WatchDogThread(); |
| 265 |
| 266 // Destroys the thread and stops the thread. |
| 258 virtual ~WatchDogThread(); | 267 virtual ~WatchDogThread(); |
| 259 | 268 |
| 269 // Start watching all browser threads. |
| 270 static void StartWatchingAll(); |
| 271 |
| 272 // Returns current message loop for the watchdog thread. This only returns |
| 273 // non-null after a successful call to Start. After Stop has been called, |
| 274 // this will return NULL. |
| 275 static MessageLoop* CurrentMessageLoop(); |
| 276 |
| 277 // Callable on any thread. Returns whether you're currently on a |
| 278 // watchdog_thread_. |
| 279 static bool CurrentlyOnWatchDogThread(); |
| 280 |
| 260 protected: | 281 protected: |
| 261 virtual void Init(); | 282 virtual void Init(); |
| 283 virtual void CleanUp(); |
| 284 virtual void CleanUpAfterMessageLoopDestruction(); |
| 285 |
| 286 // This lock protects watchdog_thread_. |
| 287 static base::Lock lock_; |
| 288 static WatchDogThread* watchdog_thread_; |
| 262 | 289 |
| 263 DISALLOW_COPY_AND_ASSIGN(WatchDogThread); | 290 DISALLOW_COPY_AND_ASSIGN(WatchDogThread); |
| 264 }; | 291 }; |
| 265 | 292 |
| 266 | |
| 267 DISABLE_RUNNABLE_METHOD_REFCOUNT(ThreadWatcher); | 293 DISABLE_RUNNABLE_METHOD_REFCOUNT(ThreadWatcher); |
| 268 DISABLE_RUNNABLE_METHOD_REFCOUNT(ThreadWatcherList); | 294 DISABLE_RUNNABLE_METHOD_REFCOUNT(ThreadWatcherList); |
| 269 | 295 |
| 270 #endif // 0 | |
| 271 | |
| 272 #endif // CHROME_BROWSER_METRICS_THREAD_WATCHER_H_ | 296 #endif // CHROME_BROWSER_METRICS_THREAD_WATCHER_H_ |
| OLD | NEW |