| 6 |
| 7 | #include <algorithm> |
| 8 | #include <atomic> |
| 9 | #include <utility> |
| 10 |
| 11 | #include "base/bind.h" |
| 12 | #include "base/callback_helpers.h" |
| 13 | #include "base/debug/dump_without_crashing.h" |
| 14 | #include "base/feature_list.h" |
| 15 | #include "base/no_destructor.h" |
| 16 | #include "base/synchronization/lock.h" |
| 17 | #include "base/synchronization/waitable_event.h" |
| 18 | #include "base/threading/thread_checker.h" |
| 19 | #include "base/threading/thread_restrictions.h" |
| 20 | #include "base/time/time.h" |
| 21 |
| 22 | namespace base { |
| 23 |
| 24 | // static |
| 25 | const base::Feature HangWatcher::kEnableHangWatcher{ |
| 26 | "EnableHangWatcher", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 27 |
| 28 | const base::TimeDelta HangWatchScope::kDefaultHangWatchTime = |
| 29 | base::TimeDelta::FromSeconds(10); |
| 30 |
| 31 | namespace { |
| 32 | HangWatcher* g_instance = nullptr; |
| 33 | } |
| 34 |
| 35 | constexpr const char* kThreadName = "HangWatcher"; |
| 36 |
| 37 | // The time that the HangWatcher thread will sleep for between calls to |
| 38 | // Monitor(). Increasing or decreasing this does not modify the type of hangs |
| 39 | // that can be detected. It instead increases the probability that a call to |
| 40 | // Monitor() will happen at the right time to catch a hang. This has to be |
| 41 | // balanced with power/cpu use concerns as busy looping would catch amost all |
| 42 | // hangs but present unacceptable overhead. |
| 43 | const base::TimeDelta kMonitoringPeriod = base::TimeDelta::FromSeconds(10); |
| 44 |
| 45 | HangWatchScope::HangWatchScope(TimeDelta timeout) { |
| 46 | internal::HangWatchState* current_hang_watch_state = |
| 47 | internal::HangWatchState::GetHangWatchStateForCurrentThread()->Get(); |
| 48 |
| 49 | // TODO(crbug.com/1034046): Remove when all threads using HangWatchScope are |
| 50 | // monitored. Thread is not monitored, noop. |
| 51 | if (!current_hang_watch_state) { |
| 52 | return; |
| 53 | } |
| 54 |
| 55 | DCHECK(current_hang_watch_state) |
| 56 | << "A scope can only be used on a thread that " |
| 57 | "registered for hang watching with HangWatcher::RegisterThread."; |
| 58 |
| 59 | #if DCHECK_IS_ON() |
| 60 | previous_scope_ = current_hang_watch_state->GetCurrentHangWatchScope(); |
| 61 | current_hang_watch_state->SetCurrentHangWatchScope(this); |
| 62 | #endif |
| 63 |
| 64 | // TODO(crbug.com/1034046): Check whether we are over deadline already for the |
| 65 | // previous scope here by issuing only one TimeTicks::Now() and resuing the |
| 66 | // value. |
| 67 |
| 68 | previous_deadline_ = current_hang_watch_state->GetDeadline(); |
| 69 | TimeTicks deadline = TimeTicks::Now() + timeout; |
| 70 | current_hang_watch_state->SetDeadline(deadline); |
| 71 | } |
| 72 |
| 73 | HangWatchScope::~HangWatchScope() { |
| 74 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 75 |
| 76 | internal::HangWatchState* current_hang_watch_state = |
| 77 | internal::HangWatchState::GetHangWatchStateForCurrentThread()->Get(); |
| 78 |
| 79 | // TODO(crbug.com/1034046): Remove when all threads using HangWatchScope are |
| 80 | // monitored. Thread is not monitored, noop. |
| 81 | if (!current_hang_watch_state) { |
| 82 | return; |
| 83 | } |
| 84 |
| 85 | // If a hang is currently being captured we should block here so execution |
| 86 | // stops and the relevant stack frames are recorded. |
| 87 | base::HangWatcher::GetInstance()->BlockIfCaptureInProgress(); |
| 88 |
| 89 | #if DCHECK_IS_ON() |
| 90 | // Verify that no Scope was destructed out of order. |
| 91 | DCHECK_EQ(this, current_hang_watch_state->GetCurrentHangWatchScope()); |
| 92 | current_hang_watch_state->SetCurrentHangWatchScope(previous_scope_); |
| 93 | #endif |
| 94 |
| 95 | // Reset the deadline to the value it had before entering this scope. |
| 96 | current_hang_watch_state->SetDeadline(previous_deadline_); |
| 97 | // TODO(crbug.com/1034046): Log when a HangWatchScope exits after its deadline |
| 98 | // and that went undetected by the HangWatcher. |
| 99 | } |
| 100 |
| 101 | HangWatcher::HangWatcher(RepeatingClosure on_hang_closure) |
| 102 | : monitor_period_(kMonitoringPeriod), |
| 103 | should_monitor_(WaitableEvent::ResetPolicy::AUTOMATIC), |
| 104 | on_hang_closure_(std::move(on_hang_closure)), |
| 105 | thread_(this, kThreadName) { |
| 106 | // |thread_checker_| should not be bound to the constructing thread. |
| 107 | DETACH_FROM_THREAD(thread_checker_); |
| 108 |
| 109 | should_monitor_.declare_only_used_while_idle(); |
| 110 |
| 111 | DCHECK(!g_instance); |
| 112 | g_instance = this; |
| 113 | Start(); |
| 114 | } |
| 115 |
| 116 | HangWatcher::~HangWatcher() { |
| 117 | DCHECK_EQ(g_instance, this); |
| 118 | DCHECK(watch_states_.empty()); |
| 119 | g_instance = nullptr; |
| 120 | Stop(); |
| 121 | } |
| 122 |
| 123 | void HangWatcher::Start() { |
| 124 | thread_.Start(); |
| 125 | } |
| 126 |
| 127 | void HangWatcher::Stop() { |
| 128 | keep_monitoring_.store(false, std::memory_order_relaxed); |
| 129 | should_monitor_.Signal(); |
| 130 | thread_.Join(); |
| 131 | } |
| 132 |
| 133 | bool HangWatcher::IsWatchListEmpty() { |
| 134 | AutoLock auto_lock(watch_state_lock_); |
| 135 | return watch_states_.empty(); |
| 136 | } |
| 137 |
| 138 | void HangWatcher::Run() { |
| 139 | // Monitor() should only run on |thread_|. Bind |thread_checker_| here to make |
| 140 | // sure of that. |
| 141 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 142 |
| 143 | while (keep_monitoring_.load(std::memory_order_relaxed)) { |
| 144 | // If there is nothing to watch sleep until there is. |
| 145 | if (IsWatchListEmpty()) { |
| 146 | should_monitor_.Wait(); |
| 147 | } else { |
| 148 | Monitor(); |
| 149 | } |
| 150 |
| 151 | if (keep_monitoring_.load(std::memory_order_relaxed)) { |
| 152 | // Sleep until next scheduled monitoring. |
| 153 | should_monitor_.TimedWait(monitor_period_); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 |
| 158 | // static |
| 159 | HangWatcher* HangWatcher::GetInstance() { |
| 160 | return g_instance; |
| 161 | } |
| 162 |
| 163 | // static |
| 164 | void HangWatcher::RecordHang() { |
| 165 | base::debug::DumpWithoutCrashing(); |
| 166 |
| 167 | // Defining |inhibit_tail_call_optimization| *after* calling |
| 168 | // DumpWithoutCrashing() prevents tail call optimization from omitting this |
| 169 | // function's address on the stack. |
| 170 | volatile int inhibit_tail_call_optimization = __LINE__; |
| 171 | ALLOW_UNUSED_LOCAL(inhibit_tail_call_optimization); |
| 172 | } |
| 173 |
| 174 | ScopedClosureRunner HangWatcher::RegisterThread() { |
| 175 | AutoLock auto_lock(watch_state_lock_); |
| 176 |
| 177 | watch_states_.push_back( |
| 178 | internal::HangWatchState::CreateHangWatchStateForCurrentThread()); |
| 179 |
| 180 | // Now that there is a thread to monitor we wake the HangWatcher thread. |
| 181 | if (watch_states_.size() == 1) { |
| 182 | should_monitor_.Signal(); |
| 183 | } |
| 184 |
| 185 | return ScopedClosureRunner(BindOnce(&HangWatcher::UnregisterThread, |
| 186 | Unretained(HangWatcher::GetInstance()))); |
| 187 | } |
| 188 |
| 189 | void HangWatcher::Monitor() { |
| 190 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 191 |
| 192 | bool must_invoke_hang_closure = false; |
| 193 | { |
| 194 | AutoLock auto_lock(watch_state_lock_); |
| 195 | for (const auto& watch_state : watch_states_) { |
| 196 | if (watch_state->IsOverDeadline()) { |
| 197 | must_invoke_hang_closure = true; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 |
| 203 | if (must_invoke_hang_closure) { |
| 204 | capture_in_progress.store(true, std::memory_order_relaxed); |
| 205 | base::AutoLock scope_lock(capture_lock_); |
| 206 |
| 207 | // Invoke the closure outside the scope of |watch_state_lock_| |
| 208 | // to prevent lock reentrancy. |
| 209 | on_hang_closure_.Run(); |
| 210 |
| 211 | capture_in_progress.store(false, std::memory_order_relaxed); |
| 212 | } |
| 213 |
| 214 | if (after_monitor_closure_for_testing_) { |
| 215 | after_monitor_closure_for_testing_.Run(); |
| 216 | } |
| 217 | } |
| 218 |
| 219 | void HangWatcher::SetAfterMonitorClosureForTesting( |
| 220 | base::RepeatingClosure closure) { |
| 221 | after_monitor_closure_for_testing_ = std::move(closure); |
| 222 | } |
| 223 |
| 224 | void HangWatcher::SetMonitoringPeriodForTesting(base::TimeDelta period) { |
| 225 | monitor_period_ = period; |
| 226 | } |
| 227 |
| 228 | void HangWatcher::SignalMonitorEventForTesting() { |
| 229 | should_monitor_.Signal(); |
| 230 | } |
| 231 |
| 232 | void HangWatcher::BlockIfCaptureInProgress() { |
| 233 | // Makes a best-effort attempt to block execution if a hang is currently being |
| 234 | // captured.Only block on |capture_lock| if |capture_in_progress| hints that |
| 235 | // it's already held to avoid serializing all threads on this function when no |
| 236 | // hang capture is in-progress. |
| 237 | if (capture_in_progress.load(std::memory_order_relaxed)) { |
| 238 | base::AutoLock hang_lock(capture_lock_); |
| 239 | } |
| 240 | } |
| 241 |
| 242 | void HangWatcher::UnregisterThread() { |
| 243 | AutoLock auto_lock(watch_state_lock_); |
| 244 |
| 245 | internal::HangWatchState* current_hang_watch_state = |
| 246 | internal::HangWatchState::GetHangWatchStateForCurrentThread()->Get(); |
| 247 |
| 248 | auto it = |
| 249 | std::find_if(watch_states_.cbegin(), watch_states_.cend(), |
| 250 | [current_hang_watch_state]( |
| 251 | const std::unique_ptr<internal::HangWatchState>& state) { |
| 252 | return state.get() == current_hang_watch_state; |
| 253 | }); |
| 254 |
| 255 | // Thread should be registered to get unregistered. |
| 256 | DCHECK(it != watch_states_.end()); |
| 257 |
| 258 | watch_states_.erase(it); |
| 259 | } |
| 260 |
| 261 | namespace internal { |
| 262 |
| 263 | // |deadline_| starts at Max() to avoid validation problems |
| 264 | // when setting the first legitimate value. |
| 265 | HangWatchState::HangWatchState() { |
| 266 | // There should not exist a state object for this thread already. |
| 267 | DCHECK(!GetHangWatchStateForCurrentThread()->Get()); |
| 268 |
| 269 | // Bind the new instance to this thread. |
| 270 | GetHangWatchStateForCurrentThread()->Set(this); |
| 271 | } |
| 272 |
| 273 | HangWatchState::~HangWatchState() { |
| 274 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 275 |
| 276 | DCHECK_EQ(GetHangWatchStateForCurrentThread()->Get(), this); |
| 277 | GetHangWatchStateForCurrentThread()->Set(nullptr); |
| 278 |
| 279 | #if DCHECK_IS_ON() |
| 280 | // Destroying the HangWatchState should not be done if there are live |
| 281 | // HangWatchScopes. |
| 282 | DCHECK(!current_hang_watch_scope_); |
| 283 | #endif |
| 284 | } |
| 285 |
| 286 | // static |
| 287 | std::unique_ptr<HangWatchState> |
| 288 | HangWatchState::CreateHangWatchStateForCurrentThread() { |
| 289 |
| 290 | // Allocate a watch state object for this thread. |
| 291 | std::unique_ptr<HangWatchState> hang_state = |
| 292 | std::make_unique<HangWatchState>(); |
| 293 |
| 294 | // Setting the thread local worked. |
| 295 | DCHECK_EQ(GetHangWatchStateForCurrentThread()->Get(), hang_state.get()); |
| 296 |
| 297 | // Transfer ownership to caller. |
| 298 | return hang_state; |
| 299 | } |
| 300 |
| 301 | TimeTicks HangWatchState::GetDeadline() const { |
| 302 | return deadline_.load(std::memory_order_relaxed); |
| 303 | } |
| 304 |
| 305 | void HangWatchState::SetDeadline(TimeTicks deadline) { |
| 306 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 307 | deadline_.store(deadline, std::memory_order_relaxed); |
| 308 | } |
| 309 |
| 310 | bool HangWatchState::IsOverDeadline() const { |
| 311 | return TimeTicks::Now() > deadline_.load(std::memory_order_relaxed); |
| 312 | } |
| 313 |
| 314 | #if DCHECK_IS_ON() |
| 315 | void HangWatchState::SetCurrentHangWatchScope(HangWatchScope* scope) { |
| 316 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 317 | current_hang_watch_scope_ = scope; |
| 318 | } |
| 319 |
| 320 | HangWatchScope* HangWatchState::GetCurrentHangWatchScope() { |
| 321 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 322 | return current_hang_watch_scope_; |
| 323 | } |
| 324 | #endif |
| 325 |
| 326 | // static |
| 327 | ThreadLocalPointer<HangWatchState>* |
| 328 | HangWatchState::GetHangWatchStateForCurrentThread() { |
| 329 | static NoDestructor<ThreadLocalPointer<HangWatchState>> hang_watch_state; |
| 330 | return hang_watch_state.get(); |
| 331 | } |
| 332 |
| 333 | } // namespace internal |
| 334 |
| 335 | } // namespace base |
| 336 |