watchdog.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.
  4. #include "base/threading/watchdog.h"
  5. #include "base/compiler_specific.h"
  6. #include "base/logging.h"
  7. #include "base/no_destructor.h"
  8. #include "base/threading/platform_thread.h"
  9. namespace base {
  10. namespace {
  11. // When the debugger breaks (when we alarm), all the other alarms that are
  12. // armed will expire (also alarm). To diminish this effect, we track any
  13. // delay due to debugger breaks, and we *try* to adjust the effective start
  14. // time of other alarms to step past the debugging break.
  15. // Without this safety net, any alarm will typically trigger a host of follow
  16. // on alarms from callers that specify old times.
  17. struct StaticData {
  18. // Lock for access of static data...
  19. Lock lock;
  20. // When did we last alarm and get stuck (for a while) in a debugger?
  21. TimeTicks last_debugged_alarm_time;
  22. // How long did we sit on a break in the debugger?
  23. TimeDelta last_debugged_alarm_delay;
  24. };
  25. StaticData* GetStaticData() {
  26. static base::NoDestructor<StaticData> static_data;
  27. return static_data.get();
  28. }
  29. } // namespace
  30. // Start thread running in a Disarmed state.
  31. Watchdog::Watchdog(const TimeDelta& duration,
  32. const std::string& thread_watched_name,
  33. bool enabled)
  34. : enabled_(enabled),
  35. lock_(),
  36. condition_variable_(&lock_),
  37. state_(DISARMED),
  38. duration_(duration),
  39. thread_watched_name_(thread_watched_name),
  40. delegate_(this) {
  41. if (!enabled_)
  42. return; // Don't start thread, or doing anything really.
  43. enabled_ = PlatformThread::Create(0, // Default stack size.
  44. &delegate_,
  45. &handle_);
  46. DCHECK(enabled_);
  47. }
  48. // Notify watchdog thread, and wait for it to finish up.
  49. Watchdog::~Watchdog() {
  50. if (!enabled_)
  51. return;
  52. if (!IsJoinable())
  53. Cleanup();
  54. PlatformThread::Join(handle_);
  55. }
  56. void Watchdog::Cleanup() {
  57. if (!enabled_)
  58. return;
  59. AutoLock lock(lock_);
  60. state_ = SHUTDOWN;
  61. condition_variable_.Signal();
  62. }
  63. bool Watchdog::IsJoinable() {
  64. if (!enabled_)
  65. return true;
  66. AutoLock lock(lock_);
  67. return (state_ == JOINABLE);
  68. }
  69. void Watchdog::Arm() {
  70. ArmAtStartTime(TimeTicks::Now());
  71. }
  72. void Watchdog::ArmSomeTimeDeltaAgo(const TimeDelta& time_delta) {
  73. ArmAtStartTime(TimeTicks::Now() - time_delta);
  74. }
  75. // Start clock for watchdog.
  76. void Watchdog::ArmAtStartTime(const TimeTicks start_time) {
  77. AutoLock lock(lock_);
  78. start_time_ = start_time;
  79. state_ = ARMED;
  80. // Force watchdog to wake up, and go to sleep with the timer ticking with the
  81. // proper duration.
  82. condition_variable_.Signal();
  83. }
  84. // Disable watchdog so that it won't do anything when time expires.
  85. void Watchdog::Disarm() {
  86. AutoLock lock(lock_);
  87. state_ = DISARMED;
  88. // We don't need to signal, as the watchdog will eventually wake up, and it
  89. // will check its state and time, and act accordingly.
  90. }
  91. void Watchdog::Alarm() {
  92. DVLOG(1) << "Watchdog alarmed for " << thread_watched_name_;
  93. }
  94. //------------------------------------------------------------------------------
  95. // Internal private methods that the watchdog thread uses.
  96. void Watchdog::ThreadDelegate::ThreadMain() {
  97. SetThreadName();
  98. TimeDelta remaining_duration;
  99. StaticData* static_data = GetStaticData();
  100. while (true) {
  101. AutoLock lock(watchdog_->lock_);
  102. while (DISARMED == watchdog_->state_)
  103. watchdog_->condition_variable_.Wait();
  104. if (SHUTDOWN == watchdog_->state_) {
  105. watchdog_->state_ = JOINABLE;
  106. return;
  107. }
  108. DCHECK(ARMED == watchdog_->state_);
  109. remaining_duration = watchdog_->duration_ -
  110. (TimeTicks::Now() - watchdog_->start_time_);
  111. if (remaining_duration.InMilliseconds() > 0) {
  112. // Spurios wake? Timer drifts? Go back to sleep for remaining time.
  113. watchdog_->condition_variable_.TimedWait(remaining_duration);
  114. continue;
  115. }
  116. // We overslept, so this seems like a real alarm.
  117. // Watch out for a user that stopped the debugger on a different alarm!
  118. {
  119. AutoLock static_lock(static_data->lock);
  120. if (static_data->last_debugged_alarm_time > watchdog_->start_time_) {
  121. // False alarm: we started our clock before the debugger break (last
  122. // alarm time).
  123. watchdog_->start_time_ += static_data->last_debugged_alarm_delay;
  124. if (static_data->last_debugged_alarm_time > watchdog_->start_time_)
  125. // Too many alarms must have taken place.
  126. watchdog_->state_ = DISARMED;
  127. continue;
  128. }
  129. }
  130. watchdog_->state_ = DISARMED; // Only alarm at most once.
  131. TimeTicks last_alarm_time = TimeTicks::Now();
  132. {
  133. AutoUnlock unlock(watchdog_->lock_);
  134. watchdog_->Alarm(); // Set a break point here to debug on alarms.
  135. }
  136. TimeDelta last_alarm_delay = TimeTicks::Now() - last_alarm_time;
  137. if (last_alarm_delay <= Milliseconds(2))
  138. continue;
  139. // Ignore race of two alarms/breaks going off at roughly the same time.
  140. AutoLock static_lock(static_data->lock);
  141. // This was a real debugger break.
  142. static_data->last_debugged_alarm_time = last_alarm_time;
  143. static_data->last_debugged_alarm_delay = last_alarm_delay;
  144. }
  145. }
  146. void Watchdog::ThreadDelegate::SetThreadName() const {
  147. std::string name = watchdog_->thread_watched_name_ + " Watchdog";
  148. PlatformThread::SetName(name);
  149. DVLOG(1) << "Watchdog active: " << name;
  150. }
  151. // static
  152. void Watchdog::ResetStaticData() {
  153. StaticData* static_data = GetStaticData();
  154. AutoLock lock(static_data->lock);
  155. // See https://crbug.com/734232 for why this cannot be zero-initialized.
  156. static_data->last_debugged_alarm_time = TimeTicks::Min();
  157. static_data->last_debugged_alarm_delay = TimeDelta();
  158. }
  159. } // namespace base