OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 // This file defines a WatchDog thread that monitors the responsiveness of other | 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 | 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 | 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 | 8 // be watched. This file also defines ThreadWatcherList class that has list of |
9 // all active ThreadWatcher objects. | 9 // all active ThreadWatcher objects. |
10 // | 10 // |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 | 301 |
302 // Class with a list of all active thread watchers. A thread watcher is active | 302 // Class with a list of all active thread watchers. A thread watcher is active |
303 // if it has been registered, which includes determing the histogram name. This | 303 // if it has been registered, which includes determing the histogram name. This |
304 // class provides utility functions to start and stop watching all browser | 304 // class provides utility functions to start and stop watching all browser |
305 // threads. Only one instance of this class exists. | 305 // threads. Only one instance of this class exists. |
306 class ThreadWatcherList { | 306 class ThreadWatcherList { |
307 public: | 307 public: |
308 // A map from BrowserThread to the actual instances. | 308 // A map from BrowserThread to the actual instances. |
309 typedef std::map<content::BrowserThread::ID, ThreadWatcher*> RegistrationList; | 309 typedef std::map<content::BrowserThread::ID, ThreadWatcher*> RegistrationList; |
310 | 310 |
311 // A map from thread names (UI, IO, etc) to |live_threads_threshold|. | 311 // A map from thread names (UI, IO, etc) to |CrashDataThresholds|. |
312 // |live_threads_threshold| specifies the maximum number of browser threads | 312 // |live_threads_threshold| specifies the maximum number of browser threads |
313 // that have to be responsive when we want to crash the browser because of | 313 // that have to be responsive when we want to crash the browser because of |
314 // hung watched thread. | 314 // hung watched thread. This threshold allows us to either look for a system |
| 315 // deadlock, or look for a solo hung thread. A small live_threads_threshold |
| 316 // looks for a broad deadlock (few browser threads left running), and a large |
| 317 // threshold looks for a single hung thread (this in only appropriate for a |
| 318 // thread that *should* never have much jank, such as the IO). |
| 319 // |
| 320 // |unresponsive_threshold| specifies the number of unanswered ping messages |
| 321 // after which watched (UI, IO, etc) thread is considered as not responsive. |
| 322 // We translate "time" (given in seconds) into a number of pings. As a result, |
| 323 // we only declare a thread unresponsive when a lot of "time" has passed (many |
| 324 // pings), and yet our pinging thread has continued to process messages (so we |
| 325 // know the entire PC is not hung). Set this number higher to crash less |
| 326 // often, and lower to crash more often. |
315 // | 327 // |
316 // The map lists all threads (by name) that can induce a crash by hanging. It | 328 // The map lists all threads (by name) that can induce a crash by hanging. It |
317 // is populated from the command line, or given a default list. See | 329 // is populated from the command line, or given a default list. See |
318 // InitializeAndStartWatching() for the separate list of all threads that are | 330 // InitializeAndStartWatching() for the separate list of all threads that are |
319 // watched, as they provide the system context of how hung *other* threads | 331 // watched, as they provide the system context of how hung *other* threads |
320 // are. | 332 // are. |
321 // | 333 // |
322 // Example 1: If the value for "IO" was 3, then we would crash if at least one | 334 // ThreadWatcher monitors six browser threads (i.e., UI, IO, DB, FILE, |
323 // thread is responding and total responding threads is less than or equal to | 335 // FILE_USER_BLOCKING and CACHE). Out of the 6 threads, any subset may be |
324 // 3 (this thread, plus at least one other thread is unresponsive). We would | 336 // watched, to potentially cause a crash. The following example's command line |
325 // not crash if none of the threads are not responding, as we'd assume such | 337 // causes exactly 3 threads to be watched. |
326 // large hang counts mean that the system is generally unresponsive. | 338 // |
327 // Example 2: If the value for "UI" was INT_MAX, then we would always crash if | 339 // The example command line argument consists of "UI:3:18,IO:3:18,FILE:5:90". |
328 // the UI thread was hung, no matter what the other threads are doing. | 340 // In that string, the first parameter specifies the thread_id: UI, IO or |
329 // Example 3: If the value of "FILE" was 5, then we would only crash if the | 341 // FILE. The second parameter specifies |live_threads_threshold|. For UI and |
330 // FILE thread was the ONLY hung thread (because we watch 6 threads). IF there | 342 // IO threads, we would crash if the number of threads responding is less than |
331 // was another unresponsive thread, we would not consider this a problem worth | 343 // or equal to 3. The third parameter specifies the unresponsive threshold |
332 // crashing for. | 344 // seconds. This number is used to calculate |unresponsive_threshold|. In this |
333 typedef std::map<std::string, uint32> CrashOnHangThreadMap; | 345 // example for UI and IO threads, we would crash if those threads don't |
| 346 // respond for 18 seconds (or 9 unanswered ping messages) and for FILE thread, |
| 347 // crash_seconds is set to 90 seconds (or 45 unanswered ping messages). |
| 348 // |
| 349 // The following examples explain how the data in |CrashDataThresholds| |
| 350 // controls the crashes. |
| 351 // |
| 352 // Example 1: If the |live_threads_threshold| value for "IO" was 3 and |
| 353 // unresponsive threshold seconds is 18 (or |unresponsive_threshold| is 9), |
| 354 // then we would crash if the IO thread was hung (9 unanswered ping messages) |
| 355 // and if at least one thread is responding and total responding threads is |
| 356 // less than or equal to 3 (this thread, plus at least one other thread is |
| 357 // unresponsive). We would not crash if none of the threads are responding, as |
| 358 // we'd assume such large hang counts mean that the system is generally |
| 359 // unresponsive. |
| 360 // Example 2: If the |live_threads_threshold| value for "UI" was any number |
| 361 // higher than 6 and unresponsive threshold seconds is 18 (or |
| 362 // |unresponsive_threshold| is 9), then we would always crash if the UI thread |
| 363 // was hung (9 unanswered ping messages), no matter what the other threads are |
| 364 // doing. |
| 365 // Example 3: If the |live_threads_threshold| value of "FILE" was 5 and |
| 366 // unresponsive threshold seconds is 90 (or |unresponsive_threshold| is 45), |
| 367 // then we would only crash if the FILE thread was the ONLY hung thread |
| 368 // (because we watch 6 threads). If there was another unresponsive thread, we |
| 369 // would not consider this a problem worth crashing for. FILE thread would be |
| 370 // considered as hung if it didn't respond for 45 ping messages. |
| 371 struct CrashDataThresholds { |
| 372 CrashDataThresholds(uint32 live_threads_threshold, |
| 373 uint32 unresponsive_threshold); |
| 374 CrashDataThresholds(); |
| 375 |
| 376 uint32 live_threads_threshold; |
| 377 uint32 unresponsive_threshold; |
| 378 }; |
| 379 typedef std::map<std::string, CrashDataThresholds> CrashOnHangThreadMap; |
334 | 380 |
335 // This method posts a task on WatchDogThread to start watching all browser | 381 // This method posts a task on WatchDogThread to start watching all browser |
336 // threads. | 382 // threads. |
337 // This method is accessible on UI thread. | 383 // This method is accessible on UI thread. |
338 static void StartWatchingAll(const CommandLine& command_line); | 384 static void StartWatchingAll(const CommandLine& command_line); |
339 | 385 |
340 // This method posts a task on WatchDogThread to RevokeAll tasks and to | 386 // This method posts a task on WatchDogThread to RevokeAll tasks and to |
341 // deactive thread watching of other threads and tell NotificationService to | 387 // deactive thread watching of other threads and tell NotificationService to |
342 // stop calling Observe. | 388 // stop calling Observe. |
343 // This method is accessible on UI thread. | 389 // This method is accessible on UI thread. |
(...skipping 10 matching lines...) Expand all Loading... |
354 uint32* unresponding_thread_count); | 400 uint32* unresponding_thread_count); |
355 | 401 |
356 // This will ensure that the watching is actively taking place, and awaken | 402 // This will ensure that the watching is actively taking place, and awaken |
357 // all thread watchers that are registered. | 403 // all thread watchers that are registered. |
358 static void WakeUpAll(); | 404 static void WakeUpAll(); |
359 | 405 |
360 private: | 406 private: |
361 // Allow tests to access our innards for testing purposes. | 407 // Allow tests to access our innards for testing purposes. |
362 friend class CustomThreadWatcher; | 408 friend class CustomThreadWatcher; |
363 friend class ThreadWatcherTest; | 409 friend class ThreadWatcherTest; |
364 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, CommandLineArgs); | 410 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, ThreadNamesOnlyArgs); |
| 411 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, ThreadNamesAndLiveThresholdArgs); |
| 412 FRIEND_TEST_ALL_PREFIXES(ThreadWatcherTest, CrashOnHangThreadsAllArgs); |
365 | 413 |
366 // This singleton holds the global list of registered ThreadWatchers. | 414 // This singleton holds the global list of registered ThreadWatchers. |
367 ThreadWatcherList(); | 415 ThreadWatcherList(); |
368 | 416 |
369 // Destructor deletes all registered ThreadWatcher instances. | 417 // Destructor deletes all registered ThreadWatcher instances. |
370 virtual ~ThreadWatcherList(); | 418 virtual ~ThreadWatcherList(); |
371 | 419 |
372 // Parses the command line to get |unresponsive_threshold| from | 420 // Parses the command line to get |crash_on_hang_threads| map from |
373 // switches::kCrashOnHangSeconds, |crash_on_hang| thread names from | 421 // switches::kCrashOnHangThreads. |crash_on_hang_threads| is a map of |
374 // switches::kCrashOnHangThreads and |live_threads_threshold| from | 422 // |crash_on_hang| thread's names to |CrashDataThresholds|. |
375 // switches::kCrashOnLive. |crash_on_hang_threads| is a map of | |
376 // |crash_on_hang| thread's names to |live_threads_threshold|. | |
377 static void ParseCommandLine( | 423 static void ParseCommandLine( |
378 const CommandLine& command_line, | 424 const CommandLine& command_line, |
379 uint32* unresponsive_threshold, | 425 uint32* unresponsive_threshold, |
380 CrashOnHangThreadMap* crash_on_hang_threads); | 426 CrashOnHangThreadMap* crash_on_hang_threads); |
381 | 427 |
| 428 // Parses the argument |crash_on_hang_thread_names| and creates |
| 429 // |crash_on_hang_threads| map of |crash_on_hang| thread's names to |
| 430 // |CrashDataThresholds|. If |crash_on_hang_thread_names| doesn't specify |
| 431 // |live_threads_threshold|, then it uses |default_live_threads_threshold| as |
| 432 // the value. If |crash_on_hang_thread_names| doesn't specify |crash_seconds|, |
| 433 // then it uses |default_crash_seconds| as the value. |
| 434 static void ParseCommandLineCrashOnHangThreads( |
| 435 const std::string& crash_on_hang_thread_names, |
| 436 uint32 default_live_threads_threshold, |
| 437 uint32 default_crash_seconds, |
| 438 CrashOnHangThreadMap* crash_on_hang_threads); |
| 439 |
382 // This constructs the |ThreadWatcherList| singleton and starts watching | 440 // This constructs the |ThreadWatcherList| singleton and starts watching |
383 // browser threads by calling StartWatching() on each browser thread that is | 441 // browser threads by calling StartWatching() on each browser thread that is |
384 // watched. It disarms StartupTimeBomb. | 442 // watched. It disarms StartupTimeBomb. |
385 static void InitializeAndStartWatching( | 443 static void InitializeAndStartWatching( |
386 uint32 unresponsive_threshold, | 444 uint32 unresponsive_threshold, |
387 const CrashOnHangThreadMap& crash_on_hang_threads); | 445 const CrashOnHangThreadMap& crash_on_hang_threads); |
388 | 446 |
389 // This method calls ThreadWatcher::StartWatching() to perform health check on | 447 // This method calls ThreadWatcher::StartWatching() to perform health check on |
390 // the given |thread_id|. | 448 // the given |thread_id|. |
391 static void StartWatching( | 449 static void StartWatching( |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 // shutdown_watchdog_ watches for hangs during shutdown. | 624 // shutdown_watchdog_ watches for hangs during shutdown. |
567 base::Watchdog* shutdown_watchdog_; | 625 base::Watchdog* shutdown_watchdog_; |
568 | 626 |
569 // The |thread_id_| on which this object is constructed. | 627 // The |thread_id_| on which this object is constructed. |
570 const base::PlatformThreadId thread_id_; | 628 const base::PlatformThreadId thread_id_; |
571 | 629 |
572 DISALLOW_COPY_AND_ASSIGN(ShutdownWatcherHelper); | 630 DISALLOW_COPY_AND_ASSIGN(ShutdownWatcherHelper); |
573 }; | 631 }; |
574 | 632 |
575 #endif // CHROME_BROWSER_METRICS_THREAD_WATCHER_H_ | 633 #endif // CHROME_BROWSER_METRICS_THREAD_WATCHER_H_ |
OLD | NEW |