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