Commit 90a7222e authored by Olivier Li's avatar Olivier Li Committed by Chromium LUCI CQ

Make HangWatcher::RegisterThread() static

This supports cases where the HangWatcher is not started and adding code
to start it would be unnecessary. For example : android_browsertests

Bug: 1157943
Change-Id: I9fdc2905a25f67acd6c0e5608eb2c3d5fac19fa2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2593551Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Commit-Queue: Oliver Li <olivierli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#837292}
parent 09f8cf62
...@@ -310,9 +310,8 @@ void WorkerThread::RunWorker() { ...@@ -310,9 +310,8 @@ void WorkerThread::RunWorker() {
// If this process has a HangWatcher register this thread for watching. // If this process has a HangWatcher register this thread for watching.
base::ScopedClosureRunner unregister_for_hang_watching; base::ScopedClosureRunner unregister_for_hang_watching;
if (watch_for_hangs) { if (watch_for_hangs) {
unregister_for_hang_watching = unregister_for_hang_watching = base::HangWatcher::RegisterThread(
base::HangWatcher::GetInstance()->RegisterThread( base::HangWatcher::ThreadType::kThreadPoolThread);
base::HangWatcher::ThreadType::kThreadPoolThread);
} }
// A WorkerThread starts out waiting for work. // A WorkerThread starts out waiting for work.
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/callback_forward.h"
#include "base/callback_helpers.h" #include "base/callback_helpers.h"
#include "base/containers/flat_map.h" #include "base/containers/flat_map.h"
#include "base/debug/alias.h" #include "base/debug/alias.h"
...@@ -471,7 +472,8 @@ void HangWatcher::RecordHang() { ...@@ -471,7 +472,8 @@ void HangWatcher::RecordHang() {
base::debug::Alias(&line_number); base::debug::Alias(&line_number);
} }
ScopedClosureRunner HangWatcher::RegisterThread(ThreadType thread_type) { ScopedClosureRunner HangWatcher::RegisterThreadInternal(
ThreadType thread_type) {
AutoLock auto_lock(watch_state_lock_); AutoLock auto_lock(watch_state_lock_);
watch_states_.push_back( watch_states_.push_back(
...@@ -482,6 +484,15 @@ ScopedClosureRunner HangWatcher::RegisterThread(ThreadType thread_type) { ...@@ -482,6 +484,15 @@ ScopedClosureRunner HangWatcher::RegisterThread(ThreadType thread_type) {
Unretained(HangWatcher::GetInstance()))); Unretained(HangWatcher::GetInstance())));
} }
// static
ScopedClosureRunner HangWatcher::RegisterThread(ThreadType thread_type) {
if (!GetInstance()) {
return ScopedClosureRunner();
}
return GetInstance()->RegisterThreadInternal(thread_type);
}
base::TimeTicks HangWatcher::WatchStateSnapShot::GetHighestDeadline() const { base::TimeTicks HangWatcher::WatchStateSnapShot::GetHighestDeadline() const {
DCHECK(!hung_watch_state_copies_.empty()); DCHECK(!hung_watch_state_copies_.empty());
// Since entries are sorted in increasing order the last entry is the largest // Since entries are sorted in increasing order the last entry is the largest
......
...@@ -181,9 +181,11 @@ class BASE_EXPORT HangWatcher : public DelegateSimpleThread::Delegate { ...@@ -181,9 +181,11 @@ class BASE_EXPORT HangWatcher : public DelegateSimpleThread::Delegate {
// Sets up the calling thread to be monitored for threads. Returns a // Sets up the calling thread to be monitored for threads. Returns a
// ScopedClosureRunner that unregisters the thread. This closure has to be // ScopedClosureRunner that unregisters the thread. This closure has to be
// called from the registered thread before it's joined. // called from the registered thread before it's joined. Returns a null
ScopedClosureRunner RegisterThread(ThreadType thread_type) // closure in the case where there is no HangWatcher instance to register the
LOCKS_EXCLUDED(watch_state_lock_) WARN_UNUSED_RESULT; // thread with.
static ScopedClosureRunner RegisterThread(ThreadType thread_type)
WARN_UNUSED_RESULT;
// Choose a closure to be run at the end of each call to Monitor(). Use only // Choose a closure to be run at the end of each call to Monitor(). Use only
// for testing. Reentering the HangWatcher in the closure must be done with // for testing. Reentering the HangWatcher in the closure must be done with
...@@ -230,6 +232,10 @@ class BASE_EXPORT HangWatcher : public DelegateSimpleThread::Delegate { ...@@ -230,6 +232,10 @@ class BASE_EXPORT HangWatcher : public DelegateSimpleThread::Delegate {
void Start(); void Start();
private: private:
// See comment of ::RegisterThread() for details.
ScopedClosureRunner RegisterThreadInternal(ThreadType thread_type)
LOCKS_EXCLUDED(watch_state_lock_) WARN_UNUSED_RESULT;
// Use to assert that functions are called on the monitoring thread. // Use to assert that functions are called on the monitoring thread.
THREAD_CHECKER(hang_watcher_thread_checker_); THREAD_CHECKER(hang_watcher_thread_checker_);
......
...@@ -67,7 +67,7 @@ class BlockingThread : public DelegateSimpleThread::Delegate { ...@@ -67,7 +67,7 @@ class BlockingThread : public DelegateSimpleThread::Delegate {
// (Un)Register the thread here instead of in ctor/dtor so that the action // (Un)Register the thread here instead of in ctor/dtor so that the action
// happens on the right thread. // happens on the right thread.
base::ScopedClosureRunner unregister_closure = base::ScopedClosureRunner unregister_closure =
base::HangWatcher::GetInstance()->RegisterThread( base::HangWatcher::RegisterThread(
base::HangWatcher::ThreadType::kUIThread); base::HangWatcher::ThreadType::kUIThread);
HangWatchScopeEnabled scope(timeout_); HangWatchScopeEnabled scope(timeout_);
...@@ -207,7 +207,7 @@ TEST_F( ...@@ -207,7 +207,7 @@ TEST_F(
ScopeDisabledCreateScopeDisabledDestroyScopeEnabledCreateScopeEnabledDestroy) { ScopeDisabledCreateScopeDisabledDestroyScopeEnabledCreateScopeEnabledDestroy) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
{ {
HangWatchScopeEnabled expires_instantly(base::TimeDelta{}); HangWatchScopeEnabled expires_instantly(base::TimeDelta{});
...@@ -235,7 +235,7 @@ TEST_F( ...@@ -235,7 +235,7 @@ TEST_F(
ScopeEnabledCreateScopeDisabledCreateScopeEnabledDestroyScopeDisabledDestroy) { ScopeEnabledCreateScopeDisabledCreateScopeEnabledDestroyScopeDisabledDestroy) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
base::Optional<HangWatchScopeDisabled> disabler; base::Optional<HangWatchScopeDisabled> disabler;
...@@ -265,7 +265,7 @@ TEST_F( ...@@ -265,7 +265,7 @@ TEST_F(
ScopeDisabledCreateScopeEnabledCreateScopeDisabledDestroyScopeEnabledDestroy) { ScopeDisabledCreateScopeEnabledCreateScopeDisabledDestroyScopeEnabledDestroy) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
base::Optional<HangWatchScopeDisabled> disabler; base::Optional<HangWatchScopeDisabled> disabler;
...@@ -295,7 +295,7 @@ TEST_F( ...@@ -295,7 +295,7 @@ TEST_F(
ScopeDisabledCreateScopeEnabledCreateScopeEnabledDestroyScopeDisabledDestroy) { ScopeDisabledCreateScopeEnabledCreateScopeEnabledDestroyScopeDisabledDestroy) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
// De-activate hang watching, // De-activate hang watching,
HangWatchScopeDisabled disabler; HangWatchScopeDisabled disabler;
...@@ -317,7 +317,7 @@ TEST_F( ...@@ -317,7 +317,7 @@ TEST_F(
TEST_F(HangWatcherTest, ScopeCreateTempCreateTempDestroyScopeDestroy) { TEST_F(HangWatcherTest, ScopeCreateTempCreateTempDestroyScopeDestroy) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
{ {
// Start a HangWatchScopeEnabled that expires right away. Then advance // Start a HangWatchScopeEnabled that expires right away. Then advance
// time to make sure a hang is detected. // time to make sure a hang is detected.
...@@ -343,7 +343,7 @@ TEST_F( ...@@ -343,7 +343,7 @@ TEST_F(
ScopeEnabledCreateScopeDisabledCreateScopeDisabledDestroyScopeEnabledDestroy) { ScopeEnabledCreateScopeDisabledCreateScopeDisabledDestroyScopeEnabledDestroy) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
{ {
// Start a HangWatchScopeEnabled that expires right away. Then advance // Start a HangWatchScopeEnabled that expires right away. Then advance
// time to make sure a hang is detected. // time to make sure a hang is detected.
...@@ -367,7 +367,7 @@ TEST_F( ...@@ -367,7 +367,7 @@ TEST_F(
TEST_F(HangWatcherTest, ScopeDisabledObjectInnerScope) { TEST_F(HangWatcherTest, ScopeDisabledObjectInnerScope) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
// Start a HangWatchScopeEnabled that expires right away. Then advance // Start a HangWatchScopeEnabled that expires right away. Then advance
// time to make sure a hang is detected. // time to make sure a hang is detected.
...@@ -395,7 +395,7 @@ TEST_F(HangWatcherTest, ScopeDisabledObjectInnerScope) { ...@@ -395,7 +395,7 @@ TEST_F(HangWatcherTest, ScopeDisabledObjectInnerScope) {
TEST_F(HangWatcherTest, NewScopeAfterDisabling) { TEST_F(HangWatcherTest, NewScopeAfterDisabling) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
// Start a HangWatchScopeEnabled that expires right away. Then advance // Start a HangWatchScopeEnabled that expires right away. Then advance
// time to make sure a hang is detected. // time to make sure a hang is detected.
...@@ -646,7 +646,7 @@ TEST_F(HangWatcherSnapshotTest, NonActionableReport) { ...@@ -646,7 +646,7 @@ TEST_F(HangWatcherSnapshotTest, NonActionableReport) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
{ {
// Start a HangWatchScopeEnabled that expires right away. Ensures that // Start a HangWatchScopeEnabled that expires right away. Ensures that
// the first monitor will detect a hang. // the first monitor will detect a hang.
...@@ -694,7 +694,7 @@ TEST_F(HangWatcherSnapshotTest, DISABLED_HungThreadIDs) { ...@@ -694,7 +694,7 @@ TEST_F(HangWatcherSnapshotTest, DISABLED_HungThreadIDs) {
// Register the main test thread for hang watching. // Register the main test thread for hang watching.
auto unregister_thread_closure = auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
BlockingThread blocking_thread(&monitor_event_, base::TimeDelta{}); BlockingThread blocking_thread(&monitor_event_, base::TimeDelta{});
blocking_thread.StartAndWaitForScopeEntered(); blocking_thread.StartAndWaitForScopeEntered();
...@@ -849,7 +849,7 @@ TEST_F(HangWatcherPeriodicMonitoringTest, PeriodicCallsTakePlace) { ...@@ -849,7 +849,7 @@ TEST_F(HangWatcherPeriodicMonitoringTest, PeriodicCallsTakePlace) {
// Register a thread, // Register a thread,
unregister_thread_closure_ = unregister_thread_closure_ =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
run_loop.Run(); run_loop.Run();
...@@ -878,7 +878,7 @@ TEST_F(HangWatcherPeriodicMonitoringTest, NoMonitorOnOverSleep) { ...@@ -878,7 +878,7 @@ TEST_F(HangWatcherPeriodicMonitoringTest, NoMonitorOnOverSleep) {
// Register a thread. // Register a thread.
unregister_thread_closure_ = unregister_thread_closure_ =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
// Unblock the test thread. All waits were perceived as oversleeping so all // Unblock the test thread. All waits were perceived as oversleeping so all
// monitoring was inhibited. // monitoring was inhibited.
...@@ -922,7 +922,7 @@ class HangWatchScopeEnabledBlockingTest : public testing::Test { ...@@ -922,7 +922,7 @@ class HangWatchScopeEnabledBlockingTest : public testing::Test {
// Register the test main thread for hang watching. // Register the test main thread for hang watching.
unregister_thread_closure_ = unregister_thread_closure_ =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread); HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
} }
void TearDown() override { hang_watcher_.UnitializeOnMainThreadForTesting(); } void TearDown() override { hang_watcher_.UnitializeOnMainThreadForTesting(); }
......
...@@ -488,9 +488,8 @@ BrowserMainLoop::BrowserMainLoop( ...@@ -488,9 +488,8 @@ BrowserMainLoop::BrowserMainLoop(
// works since the UI thread running the message loop is the main thread and // works since the UI thread running the message loop is the main thread and
// that makes it the same as the current one. // that makes it the same as the current one.
if (base::HangWatcher::IsUIThreadHangWatchingEnabled()) { if (base::HangWatcher::IsUIThreadHangWatchingEnabled()) {
unregister_thread_closure_ = unregister_thread_closure_ = base::HangWatcher::RegisterThread(
base::HangWatcher::GetInstance()->RegisterThread( base::HangWatcher::ThreadType::kUIThread);
base::HangWatcher::ThreadType::kUIThread);
} }
if (GetContentClient()->browser()->ShouldCreateThreadPool()) { if (GetContentClient()->browser()->ShouldCreateThreadPool()) {
......
...@@ -137,9 +137,8 @@ NOINLINE void BrowserProcessSubThread::IOThreadRun(base::RunLoop* run_loop) { ...@@ -137,9 +137,8 @@ NOINLINE void BrowserProcessSubThread::IOThreadRun(base::RunLoop* run_loop) {
// up a closure to automatically unregister it when Run() returns. // up a closure to automatically unregister it when Run() returns.
base::ScopedClosureRunner unregister_thread_closure; base::ScopedClosureRunner unregister_thread_closure;
if (base::HangWatcher::IsIOThreadHangWatchingEnabled()) { if (base::HangWatcher::IsIOThreadHangWatchingEnabled()) {
unregister_thread_closure = unregister_thread_closure = base::HangWatcher::RegisterThread(
base::HangWatcher::GetInstance()->RegisterThread( base::HangWatcher::ThreadType::kIOThread);
base::HangWatcher::ThreadType::kIOThread);
} }
Thread::Run(run_loop); Thread::Run(run_loop);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment