Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / metrics / thread_watcher.h
blobf62b04d1be4550771df2db0dbd0b630ca65223a6
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file defines a WatchDog thread that monitors the responsiveness of other
6 // browser threads like UI, IO, DB, FILE and CACHED threads. It also defines
7 // ThreadWatcher class which performs health check on threads that would like to
8 // be watched. This file also defines ThreadWatcherList class that has list of
9 // all active ThreadWatcher objects.
11 // ThreadWatcher class sends ping message to the watched thread and the watched
12 // thread responds back with a pong message. It uploads response time
13 // (difference between ping and pong times) as a histogram.
15 // TODO(raman): ThreadWatcher can detect hung threads. If a hung thread is
16 // detected, we should probably just crash, and allow the crash system to gather
17 // then stack trace.
19 // Example Usage:
21 // The following is an example for watching responsiveness of watched (IO)
22 // thread. |sleep_time| specifies how often ping messages have to be sent to
23 // watched (IO) thread. |unresponsive_time| is the wait time after ping
24 // message is sent, to check if we have received pong message or not.
25 // |unresponsive_threshold| specifies the number of unanswered ping messages
26 // after which watched (IO) thread is considered as not responsive.
27 // |crash_on_hang| specifies if we want to crash the browser when the watched
28 // (IO) thread has become sufficiently unresponsive, while other threads are
29 // sufficiently responsive. |live_threads_threshold| specifies the number of
30 // browser threads that are to be responsive when we want to crash the browser
31 // because of hung watched (IO) thread.
33 // base::TimeDelta sleep_time = base::TimeDelta::FromSeconds(5);
34 // base::TimeDelta unresponsive_time = base::TimeDelta::FromSeconds(10);
35 // uint32 unresponsive_threshold = ThreadWatcherList::kUnresponsiveCount;
36 // bool crash_on_hang = false;
37 // uint32 live_threads_threshold = ThreadWatcherList::kLiveThreadsThreshold;
38 // ThreadWatcher::StartWatching(
39 // BrowserThread::IO, "IO", sleep_time, unresponsive_time,
40 // unresponsive_threshold, crash_on_hang, live_threads_threshold);
42 #ifndef CHROME_BROWSER_METRICS_THREAD_WATCHER_H_
43 #define CHROME_BROWSER_METRICS_THREAD_WATCHER_H_
45 #include <map>
46 #include <string>
47 #include <vector>
49 #include "base/basictypes.h"
50 #include "base/command_line.h"
51 #include "base/gtest_prod_util.h"
52 #include "base/memory/ref_counted.h"
53 #include "base/memory/weak_ptr.h"
54 #include "base/message_loop/message_loop.h"
55 #include "base/metrics/histogram.h"
56 #include "base/single_thread_task_runner.h"
57 #include "base/synchronization/lock.h"
58 #include "base/threading/platform_thread.h"
59 #include "base/threading/thread.h"
60 #include "base/threading/watchdog.h"
61 #include "base/time/time.h"
62 #include "content/public/browser/browser_thread.h"
63 #include "content/public/browser/notification_observer.h"
64 #include "content/public/browser/notification_registrar.h"
66 class CustomThreadWatcher;
67 class StartupTimeBomb;
68 class ThreadWatcherList;
69 class ThreadWatcherObserver;
71 // This class performs health check on threads that would like to be watched.
72 class ThreadWatcher {
73 public:
74 // base::Bind supports methods with up to 6 parameters. WatchingParams is used
75 // as a workaround that limitation for invoking ThreadWatcher::StartWatching.
76 struct WatchingParams {
77 const content::BrowserThread::ID& thread_id;
78 const std::string& thread_name;
79 const base::TimeDelta& sleep_time;
80 const base::TimeDelta& unresponsive_time;
81 uint32 unresponsive_threshold;
82 bool crash_on_hang;
83 uint32 live_threads_threshold;
85 WatchingParams(const content::BrowserThread::ID& thread_id_in,
86 const std::string& thread_name_in,
87 const base::TimeDelta& sleep_time_in,
88 const base::TimeDelta& unresponsive_time_in,
89 uint32 unresponsive_threshold_in,
90 bool crash_on_hang_in,
91 uint32 live_threads_threshold_in)
92 : thread_id(thread_id_in),
93 thread_name(thread_name_in),
94 sleep_time(sleep_time_in),
95 unresponsive_time(unresponsive_time_in),
96 unresponsive_threshold(unresponsive_threshold_in),
97 crash_on_hang(crash_on_hang_in),
98 live_threads_threshold(live_threads_threshold_in) {
102 // This method starts performing health check on the given |thread_id|. It
103 // will create ThreadWatcher object for the given |thread_id|, |thread_name|.
104 // |sleep_time| is the wait time between ping messages. |unresponsive_time| is
105 // the wait time after ping message is sent, to check if we have received pong
106 // message or not. |unresponsive_threshold| is used to determine if the thread
107 // is responsive or not. The watched thread is considered unresponsive if it
108 // hasn't responded with a pong message for |unresponsive_threshold| number of
109 // ping messages. |crash_on_hang| specifies if browser should be crashed when
110 // the watched thread is unresponsive. |live_threads_threshold| specifies the
111 // number of browser threads that are to be responsive when we want to crash
112 // the browser and watched thread has become sufficiently unresponsive. It
113 // will register that ThreadWatcher object and activate the thread watching of
114 // the given thread_id.
115 static void StartWatching(const WatchingParams& params);
117 // Return the |thread_id_| of the thread being watched.
118 content::BrowserThread::ID thread_id() const { return thread_id_; }
120 // Return the name of the thread being watched.
121 std::string thread_name() const { return thread_name_; }
123 // Return the sleep time between ping messages to be sent to the thread.
124 base::TimeDelta sleep_time() const { return sleep_time_; }
126 // Return the the wait time to check the responsiveness of the thread.
127 base::TimeDelta unresponsive_time() const { return unresponsive_time_; }
129 // Returns true if we are montioring the thread.
130 bool active() const { return active_; }
132 // Returns |ping_time_| (used by unit tests).
133 base::TimeTicks ping_time() const { return ping_time_; }
135 // Returns |ping_sequence_number_| (used by unit tests).
136 uint64 ping_sequence_number() const { return ping_sequence_number_; }
138 protected:
139 // Construct a ThreadWatcher for the given |thread_id|. |sleep_time| is the
140 // wait time between ping messages. |unresponsive_time| is the wait time after
141 // ping message is sent, to check if we have received pong message or not.
142 explicit ThreadWatcher(const WatchingParams& params);
144 virtual ~ThreadWatcher();
146 // This method activates the thread watching which starts ping/pong messaging.
147 virtual void ActivateThreadWatching();
149 // This method de-activates the thread watching and revokes all tasks.
150 virtual void DeActivateThreadWatching();
152 // This will ensure that the watching is actively taking place, and awaken
153 // (i.e., post a PostPingMessage()) if the watcher has stopped pinging due to
154 // lack of user activity. It will also reset |ping_count_| to
155 // |unresponsive_threshold_|.
156 virtual void WakeUp();
158 // This method records when ping message was sent and it will Post a task
159 // (OnPingMessage()) to the watched thread that does nothing but respond with
160 // OnPongMessage(). It also posts a task (OnCheckResponsiveness()) to check
161 // responsiveness of monitored thread that would be called after waiting
162 // |unresponsive_time_|.
163 // This method is accessible on WatchDogThread.
164 virtual void PostPingMessage();
166 // This method handles a Pong Message from watched thread. It will track the
167 // response time (pong time minus ping time) via histograms. It posts a
168 // PostPingMessage() task that would be called after waiting |sleep_time_|. It
169 // increments |ping_sequence_number_| by 1.
170 // This method is accessible on WatchDogThread.
171 virtual void OnPongMessage(uint64 ping_sequence_number);
173 // This method will determine if the watched thread is responsive or not. If
174 // the latest |ping_sequence_number_| is not same as the
175 // |ping_sequence_number| that is passed in, then we can assume that watched
176 // thread has responded with a pong message.
177 // This method is accessible on WatchDogThread.
178 virtual void OnCheckResponsiveness(uint64 ping_sequence_number);
180 // Set by OnCheckResponsiveness when it determines if the watched thread is
181 // responsive or not.
182 bool responsive_;
184 private:
185 friend class ThreadWatcherList;
186 friend class CustomThreadWatcher;
188 // Allow tests to access our innards for testing purposes.
189 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, Registration);
190 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, ThreadResponding);
191 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, ThreadNotResponding);
192 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, MultipleThreadsResponding);
193 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, MultipleThreadsNotResponding);
195 // Post constructor initialization.
196 void Initialize();
198 // Watched thread does nothing except post callback_task to the WATCHDOG
199 // Thread. This method is called on watched thread.
200 static void OnPingMessage(const content::BrowserThread::ID& thread_id,
201 const base::Closure& callback_task);
203 // This method resets |unresponsive_count_| to zero because watched thread is
204 // responding to the ping message with a pong message.
205 void ResetHangCounters();
207 // This method records watched thread is not responding to the ping message.
208 // It increments |unresponsive_count_| by 1.
209 void GotNoResponse();
211 // This method returns true if the watched thread has not responded with a
212 // pong message for |unresponsive_threshold_| number of ping messages.
213 bool IsVeryUnresponsive();
215 // The |thread_id_| of the thread being watched. Only one instance can exist
216 // for the given |thread_id_| of the thread being watched.
217 const content::BrowserThread::ID thread_id_;
219 // The name of the thread being watched.
220 const std::string thread_name_;
222 // Used to post messages to watched thread.
223 scoped_refptr<base::SingleThreadTaskRunner> watched_runner_;
225 // It is the sleep time between the receipt of a pong message back, and the
226 // sending of another ping message.
227 const base::TimeDelta sleep_time_;
229 // It is the duration from sending a ping message, until we check status to be
230 // sure a pong message has been returned.
231 const base::TimeDelta unresponsive_time_;
233 // This is the last time when ping message was sent.
234 base::TimeTicks ping_time_;
236 // This is the last time when we got pong message.
237 base::TimeTicks pong_time_;
239 // This is the sequence number of the next ping for which there is no pong. If
240 // the instance is sleeping, then it will be the sequence number for the next
241 // ping.
242 uint64 ping_sequence_number_;
244 // This is set to true if thread watcher is watching.
245 bool active_;
247 // The counter tracks least number of ping messages that will be sent to
248 // watched thread before the ping-pong mechanism will go into an extended
249 // sleep. If this value is zero, then the mechanism is in an extended sleep,
250 // and awaiting some observed user action before continuing.
251 int ping_count_;
253 // Histogram that keeps track of response times for the watched thread.
254 base::HistogramBase* response_time_histogram_;
256 // Histogram that keeps track of unresponsive time since the last pong message
257 // when we got no response (GotNoResponse()) from the watched thread.
258 base::HistogramBase* unresponsive_time_histogram_;
260 // Histogram that keeps track of how many threads are responding when we got
261 // no response (GotNoResponse()) from the watched thread.
262 base::HistogramBase* responsive_count_histogram_;
264 // Histogram that keeps track of how many threads are not responding when we
265 // got no response (GotNoResponse()) from the watched thread. Count includes
266 // the thread that got no response.
267 base::HistogramBase* unresponsive_count_histogram_;
269 // This counter tracks the unresponsiveness of watched thread. If this value
270 // is zero then watched thread has responded with a pong message. This is
271 // incremented by 1 when we got no response (GotNoResponse()) from the watched
272 // thread.
273 uint32 unresponsive_count_;
275 // This is set to true when we would have crashed the browser because the
276 // watched thread hasn't responded at least |unresponsive_threshold_| times.
277 // It is reset to false when watched thread responds with a pong message.
278 bool hung_processing_complete_;
280 // This is used to determine if the watched thread is responsive or not. If
281 // watched thread's |unresponsive_count_| is greater than or equal to
282 // |unresponsive_threshold_| then we would consider it as unresponsive.
283 uint32 unresponsive_threshold_;
285 // This is set to true if we want to crash the browser when the watched thread
286 // has become sufficiently unresponsive, while other threads are sufficiently
287 // responsive.
288 bool crash_on_hang_;
290 // This specifies the number of browser threads that are to be responsive when
291 // we want to crash the browser because watched thread has become sufficiently
292 // unresponsive.
293 uint32 live_threads_threshold_;
295 // We use this factory to create callback tasks for ThreadWatcher object. We
296 // use this during ping-pong messaging between WatchDog thread and watched
297 // thread.
298 base::WeakPtrFactory<ThreadWatcher> weak_ptr_factory_;
300 DISALLOW_COPY_AND_ASSIGN(ThreadWatcher);
303 // Class with a list of all active thread watchers. A thread watcher is active
304 // if it has been registered, which includes determing the histogram name. This
305 // class provides utility functions to start and stop watching all browser
306 // threads. Only one instance of this class exists.
307 class ThreadWatcherList {
308 public:
309 // A map from BrowserThread to the actual instances.
310 typedef std::map<content::BrowserThread::ID, ThreadWatcher*> RegistrationList;
312 // A map from thread names (UI, IO, etc) to |CrashDataThresholds|.
313 // |live_threads_threshold| specifies the maximum number of browser threads
314 // that have to be responsive when we want to crash the browser because of
315 // hung watched thread. This threshold allows us to either look for a system
316 // deadlock, or look for a solo hung thread. A small live_threads_threshold
317 // looks for a broad deadlock (few browser threads left running), and a large
318 // threshold looks for a single hung thread (this in only appropriate for a
319 // thread that *should* never have much jank, such as the IO).
321 // |unresponsive_threshold| specifies the number of unanswered ping messages
322 // after which watched (UI, IO, etc) thread is considered as not responsive.
323 // We translate "time" (given in seconds) into a number of pings. As a result,
324 // we only declare a thread unresponsive when a lot of "time" has passed (many
325 // pings), and yet our pinging thread has continued to process messages (so we
326 // know the entire PC is not hung). Set this number higher to crash less
327 // often, and lower to crash more often.
329 // The map lists all threads (by name) that can induce a crash by hanging. It
330 // is populated from the command line, or given a default list. See
331 // InitializeAndStartWatching() for the separate list of all threads that are
332 // watched, as they provide the system context of how hung *other* threads
333 // are.
335 // ThreadWatcher monitors five browser threads (i.e., UI, IO, DB, FILE,
336 // and CACHE). Out of the 5 threads, any subset may be watched, to potentially
337 // cause a crash. The following example's command line causes exactly 3
338 // threads to be watched.
340 // The example command line argument consists of "UI:3:18,IO:3:18,FILE:5:90".
341 // In that string, the first parameter specifies the thread_id: UI, IO or
342 // FILE. The second parameter specifies |live_threads_threshold|. For UI and
343 // IO threads, we would crash if the number of threads responding is less than
344 // or equal to 3. The third parameter specifies the unresponsive threshold
345 // seconds. This number is used to calculate |unresponsive_threshold|. In this
346 // example for UI and IO threads, we would crash if those threads don't
347 // respond for 18 seconds (or 9 unanswered ping messages) and for FILE thread,
348 // crash_seconds is set to 90 seconds (or 45 unanswered ping messages).
350 // The following examples explain how the data in |CrashDataThresholds|
351 // controls the crashes.
353 // Example 1: If the |live_threads_threshold| value for "IO" was 3 and
354 // unresponsive threshold seconds is 18 (or |unresponsive_threshold| is 9),
355 // then we would crash if the IO thread was hung (9 unanswered ping messages)
356 // and if at least one thread is responding and total responding threads is
357 // less than or equal to 3 (this thread, plus at least one other thread is
358 // unresponsive). We would not crash if none of the threads are responding, as
359 // we'd assume such large hang counts mean that the system is generally
360 // unresponsive.
361 // Example 2: If the |live_threads_threshold| value for "UI" was any number
362 // higher than 6 and unresponsive threshold seconds is 18 (or
363 // |unresponsive_threshold| is 9), then we would always crash if the UI thread
364 // was hung (9 unanswered ping messages), no matter what the other threads are
365 // doing.
366 // Example 3: If the |live_threads_threshold| value of "FILE" was 5 and
367 // unresponsive threshold seconds is 90 (or |unresponsive_threshold| is 45),
368 // then we would only crash if the FILE thread was the ONLY hung thread
369 // (because we watch 6 threads). If there was another unresponsive thread, we
370 // would not consider this a problem worth crashing for. FILE thread would be
371 // considered as hung if it didn't respond for 45 ping messages.
372 struct CrashDataThresholds {
373 CrashDataThresholds(uint32 live_threads_threshold,
374 uint32 unresponsive_threshold);
375 CrashDataThresholds();
377 uint32 live_threads_threshold;
378 uint32 unresponsive_threshold;
380 typedef std::map<std::string, CrashDataThresholds> CrashOnHangThreadMap;
382 // This method posts a task on WatchDogThread to start watching all browser
383 // threads.
384 // This method is accessible on UI thread.
385 static void StartWatchingAll(const base::CommandLine& command_line);
387 // This method posts a task on WatchDogThread to RevokeAll tasks and to
388 // deactive thread watching of other threads and tell NotificationService to
389 // stop calling Observe.
390 // This method is accessible on UI thread.
391 static void StopWatchingAll();
393 // Register() stores a pointer to the given ThreadWatcher in a global map.
394 static void Register(ThreadWatcher* watcher);
396 // This method returns true if the ThreadWatcher object is registerd.
397 static bool IsRegistered(const content::BrowserThread::ID thread_id);
399 // This method returns number of responsive and unresponsive watched threads.
400 static void GetStatusOfThreads(uint32* responding_thread_count,
401 uint32* unresponding_thread_count);
403 // This will ensure that the watching is actively taking place, and awaken
404 // all thread watchers that are registered.
405 static void WakeUpAll();
407 private:
408 // Allow tests to access our innards for testing purposes.
409 friend class CustomThreadWatcher;
410 friend class ThreadWatcherListTest;
411 friend class ThreadWatcherTest;
412 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherAndroidTest,
413 ApplicationStatusNotification);
414 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherListTest, Restart);
415 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, ThreadNamesOnlyArgs);
416 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, ThreadNamesAndLiveThresholdArgs);
417 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, CrashOnHangThreadsAllArgs);
419 // This singleton holds the global list of registered ThreadWatchers.
420 ThreadWatcherList();
422 // Destructor deletes all registered ThreadWatcher instances.
423 virtual ~ThreadWatcherList();
425 // Parses the command line to get |crash_on_hang_threads| map from
426 // switches::kCrashOnHangThreads. |crash_on_hang_threads| is a map of
427 // |crash_on_hang| thread's names to |CrashDataThresholds|.
428 static void ParseCommandLine(
429 const base::CommandLine& command_line,
430 uint32* unresponsive_threshold,
431 CrashOnHangThreadMap* crash_on_hang_threads);
433 // Parses the argument |crash_on_hang_thread_names| and creates
434 // |crash_on_hang_threads| map of |crash_on_hang| thread's names to
435 // |CrashDataThresholds|. If |crash_on_hang_thread_names| doesn't specify
436 // |live_threads_threshold|, then it uses |default_live_threads_threshold| as
437 // the value. If |crash_on_hang_thread_names| doesn't specify |crash_seconds|,
438 // then it uses |default_crash_seconds| as the value.
439 static void ParseCommandLineCrashOnHangThreads(
440 const std::string& crash_on_hang_thread_names,
441 uint32 default_live_threads_threshold,
442 uint32 default_crash_seconds,
443 CrashOnHangThreadMap* crash_on_hang_threads);
445 // This constructs the |ThreadWatcherList| singleton and starts watching
446 // browser threads by calling StartWatching() on each browser thread that is
447 // watched. It disarms StartupTimeBomb.
448 static void InitializeAndStartWatching(
449 uint32 unresponsive_threshold,
450 const CrashOnHangThreadMap& crash_on_hang_threads);
452 // This method calls ThreadWatcher::StartWatching() to perform health check on
453 // the given |thread_id|.
454 static void StartWatching(
455 const content::BrowserThread::ID& thread_id,
456 const std::string& thread_name,
457 const base::TimeDelta& sleep_time,
458 const base::TimeDelta& unresponsive_time,
459 uint32 unresponsive_threshold,
460 const CrashOnHangThreadMap& crash_on_hang_threads);
462 // Delete all thread watcher objects and remove them from global map. It also
463 // deletes |g_thread_watcher_list_|.
464 static void DeleteAll();
466 // The Find() method can be used to test to see if a given ThreadWatcher was
467 // already registered, or to retrieve a pointer to it from the global map.
468 static ThreadWatcher* Find(const content::BrowserThread::ID& thread_id);
470 // Sets |g_stopped_| on the WatchDogThread. This is necessary to reflect the
471 // state between the delayed |StartWatchingAll| and the immediate
472 // |StopWatchingAll|.
473 static void SetStopped(bool stopped);
475 // The singleton of this class and is used to keep track of information about
476 // threads that are being watched.
477 static ThreadWatcherList* g_thread_watcher_list_;
479 // StartWatchingAll() is delayed in relation to StopWatchingAll(), so if
480 // a Stop comes first, prevent further initialization.
481 static bool g_stopped_;
483 // This is the wait time between ping messages.
484 static const int kSleepSeconds;
486 // This is the wait time after ping message is sent, to check if we have
487 // received pong message or not.
488 static const int kUnresponsiveSeconds;
490 // Default values for |unresponsive_threshold|.
491 static const int kUnresponsiveCount;
493 // Default values for |live_threads_threshold|.
494 static const int kLiveThreadsThreshold;
496 // Default value for the delay until |InitializeAndStartWatching| is called.
497 // Non-const for tests.
498 static int g_initialize_delay_seconds;
500 // Map of all registered watched threads, from thread_id to ThreadWatcher.
501 RegistrationList registered_;
503 DISALLOW_COPY_AND_ASSIGN(ThreadWatcherList);
506 // This class ensures that the thread watching is actively taking place. Only
507 // one instance of this class exists.
508 class ThreadWatcherObserver : public content::NotificationObserver {
509 public:
510 // Registers |g_thread_watcher_observer_| as the Notifications observer.
511 // |wakeup_interval| specifies how often to wake up thread watchers. This
512 // method is accessible on UI thread.
513 static void SetupNotifications(const base::TimeDelta& wakeup_interval);
515 // Removes all ints from |registrar_| and deletes
516 // |g_thread_watcher_observer_|. This method is accessible on UI thread.
517 static void RemoveNotifications();
519 private:
520 // Constructor of |g_thread_watcher_observer_| singleton.
521 explicit ThreadWatcherObserver(const base::TimeDelta& wakeup_interval);
523 // Destructor of |g_thread_watcher_observer_| singleton.
524 ~ThreadWatcherObserver() override;
526 // This ensures all thread watchers are active because there is some user
527 // activity. It will wake up all thread watchers every |wakeup_interval_|
528 // seconds. This is the implementation of content::NotificationObserver. When
529 // a matching notification is posted to the notification service, this method
530 // is called.
531 void Observe(int type,
532 const content::NotificationSource& source,
533 const content::NotificationDetails& details) override;
535 // The singleton of this class.
536 static ThreadWatcherObserver* g_thread_watcher_observer_;
538 // The registrar that holds ints to be observed.
539 content::NotificationRegistrar registrar_;
541 // This is the last time when woke all thread watchers up.
542 base::TimeTicks last_wakeup_time_;
544 // It is the time interval between wake up calls to thread watchers.
545 const base::TimeDelta wakeup_interval_;
547 DISALLOW_COPY_AND_ASSIGN(ThreadWatcherObserver);
550 // Class for WatchDogThread and in its Init method, we start watching UI, IO,
551 // DB, FILE, CACHED threads.
552 class WatchDogThread : public base::Thread {
553 public:
554 // Constructor.
555 WatchDogThread();
557 // Destroys the thread and stops the thread.
558 ~WatchDogThread() override;
560 // Callable on any thread. Returns whether you're currently on a
561 // WatchDogThread.
562 static bool CurrentlyOnWatchDogThread();
564 // These are the same methods in message_loop.h, but are guaranteed to either
565 // get posted to the MessageLoop if it's still alive, or be deleted otherwise.
566 // They return true iff the watchdog thread existed and the task was posted.
567 // Note that even if the task is posted, there's no guarantee that it will
568 // run, since the target thread may already have a Quit message in its queue.
569 static bool PostTask(const tracked_objects::Location& from_here,
570 const base::Closure& task);
571 static bool PostDelayedTask(const tracked_objects::Location& from_here,
572 const base::Closure& task,
573 base::TimeDelta delay);
575 protected:
576 void Init() override;
577 void CleanUp() override;
579 private:
580 static bool PostTaskHelper(
581 const tracked_objects::Location& from_here,
582 const base::Closure& task,
583 base::TimeDelta delay);
585 DISALLOW_COPY_AND_ASSIGN(WatchDogThread);
588 // This is a wrapper class for getting the crash dumps of the hangs during
589 // startup.
590 class StartupTimeBomb {
591 public:
592 // This singleton is instantiated when the browser process is launched.
593 StartupTimeBomb();
595 // Destructor disarm's startup_watchdog_ (if it is arm'ed) so that alarm
596 // doesn't go off.
597 ~StartupTimeBomb();
599 // Constructs |startup_watchdog_| which spawns a thread and starts timer.
600 // |duration| specifies how long |startup_watchdog_| will wait before it
601 // calls alarm.
602 void Arm(const base::TimeDelta& duration);
604 // Disarms |startup_watchdog_| thread and then deletes it which stops the
605 // Watchdog thread.
606 void Disarm();
608 // Disarms |g_startup_timebomb_|.
609 static void DisarmStartupTimeBomb();
611 private:
612 // Deletes |startup_watchdog_| if it is joinable. If |startup_watchdog_| is
613 // not joinable, then it will post a delayed task to try again.
614 void DeleteStartupWatchdog();
616 // The singleton of this class.
617 static StartupTimeBomb* g_startup_timebomb_;
619 // Watches for hangs during startup until it is disarm'ed.
620 base::Watchdog* startup_watchdog_;
622 // The |thread_id_| on which this object is constructed.
623 const base::PlatformThreadId thread_id_;
625 DISALLOW_COPY_AND_ASSIGN(StartupTimeBomb);
628 // This is a wrapper class for detecting hangs during shutdown.
629 class ShutdownWatcherHelper {
630 public:
631 // Create an empty holder for |shutdown_watchdog_|.
632 ShutdownWatcherHelper();
634 // Destructor disarm's shutdown_watchdog_ so that alarm doesn't go off.
635 ~ShutdownWatcherHelper();
637 // Constructs ShutdownWatchDogThread which spawns a thread and starts timer.
638 // |duration| specifies how long it will wait before it calls alarm.
639 void Arm(const base::TimeDelta& duration);
641 private:
642 // shutdown_watchdog_ watches for hangs during shutdown.
643 base::Watchdog* shutdown_watchdog_;
645 // The |thread_id_| on which this object is constructed.
646 const base::PlatformThreadId thread_id_;
648 DISALLOW_COPY_AND_ASSIGN(ShutdownWatcherHelper);
651 #endif // CHROME_BROWSER_METRICS_THREAD_WATCHER_H_