MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WatchDog.hpp
1 /*
2  Copyright (C) 2003, 2005, 2006, 2008 MySQL AB, 2008 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef WatchDog_H
20 #define WatchDog_H
21 
22 #include <kernel_types.h>
23 #include <NdbThread.h>
24 #include <NdbMutex.h>
25 #include <NdbTick.h>
26 
27 extern "C" void* runWatchDog(void* w);
28 
29 class WatchDog{
30  enum { MAX_WATCHED_THREADS = 64 };
31 
32  struct WatchedThread {
33  Uint32 *m_watchCounter;
34  Uint32 m_threadId;
35  /* This is the time that activity was last registered from thread. */
36  MicroSecondTimer m_startTime;
37  /*
38  During slow operation (memory allocation), warnings are output less
39  frequently, and this is the point when the next warning should be given.
40  */
41  Uint32 m_slowWarnDelay;
42  /*
43  This is the last counter value update seen, telling us what the thread
44  was doing when it got stuck.
45  */
46  Uint32 m_lastCounterValue;
47  };
48 
49 public:
50  WatchDog(Uint32 interval = 3000);
51  ~WatchDog();
52 
53  struct NdbThread* doStart();
54  void doStop();
55 
56  Uint32 setCheckInterval(Uint32 interval);
57 
58  /*
59  Register a thread for being watched.
60  Returns true if ok, false if out of slots.
61  */
62  bool registerWatchedThread(Uint32 *counter, Uint32 threadId);
63  /* Remove a thread from registration, identified by thread id. */
64  void unregisterWatchedThread(Uint32 threadId);
65 
66 protected:
70  friend void* runWatchDog(void* w);
71 
76 
77 private:
78  Uint32 theInterval;
79  /*
80  List of watched threads.
81  Threads are identified by the m_threadId.
82  Active entries are kept at the start of the entries.
83  Access to the list is protected by m_mutex.
84  */
85  WatchedThread m_watchedList[MAX_WATCHED_THREADS];
86  /* Number of active entries in m_watchedList. */
87  Uint32 m_watchedCount;
88  NdbMutex *m_mutex;
89 
90  bool theStop;
91 
92  void run();
93  void shutdownSystem(const char *last_stuck_action);
94 };
95 
96 #endif // WatchDog_H