Cuberite
A lightweight, fast and extensible game server for Minecraft
DeadlockDetect.h
Go to the documentation of this file.
1 
2 // DeadlockDetect.h
3 
4 // Declares the cDeadlockDetect class that tries to detect deadlocks and aborts the server when it detects one
5 
6 /*
7 This class simply monitors each world's m_WorldAge, which is expected to grow on each tick.
8 If the world age doesn't grow for several seconds, it's either because the server is Super-overloaded,
9 or because the world tick thread hangs in a deadlock. We presume the latter and therefore kill the server.
10 Once we learn to write crashdumps programmatically, we should do so just before killing, to enable debugging.
11 */
12 
13 
14 
15 #pragma once
16 
17 #include "OSSupport/IsThread.h"
18 
19 
20 
21 
22 
24  public cIsThread
25 {
26  using Super = cIsThread;
27 
28 public:
29 
31  virtual ~cDeadlockDetect() override;
32 
34  void Start(int a_IntervalSec);
35 
40  void TrackCriticalSection(cCriticalSection & a_CS, const AString & a_Name);
41 
44 
45 protected:
46  struct sWorldAge
47  {
50 
53  } ;
54 
56  typedef std::map<AString, sWorldAge> WorldAges;
57 
60 
63  std::vector<std::pair<cCriticalSection *, AString>> m_TrackedCriticalSections;
64 
66 
69 
70 
71  // cIsThread overrides:
72  virtual void Execute(void) override;
73 
75  void SetWorldAge(const AString & a_WorldName, cTickTimeLong a_Age);
76 
78  void CheckWorldAge(const AString & a_WorldName, cTickTimeLong a_Age);
79 
83  [[noreturn]] void DeadlockDetected(const AString & a_WorldName, cTickTimeLong a_WorldAge);
84 
86  void ListTrackedCSs();
87 } ;
88 
89 
90 
91 
std::chrono::duration< signed long long int, cTickTime::period > cTickTimeLong
Definition: Globals.h:367
std::string AString
Definition: StringUtils.h:11
void UntrackCriticalSection(cCriticalSection &a_CS)
Removes the CS from the tracking.
void CheckWorldAge(const AString &a_WorldName, cTickTimeLong a_Age)
Checks if the world's age has changed, updates the world's stats; calls DeadlockDetected() if deadloc...
int m_IntervalSec
Number of secods for which the ages must be the same for the detection to trigger.
void ListTrackedCSs()
Outputs a listing of the tracked CSs, together with their name and state.
WorldAges m_WorldAges
virtual ~cDeadlockDetect() override
cCriticalSection m_CS
Protects m_TrackedCriticalSections from multithreaded access.
void SetWorldAge(const AString &a_WorldName, cTickTimeLong a_Age)
Sets the initial world age.
void DeadlockDetected(const AString &a_WorldName, cTickTimeLong a_WorldAge)
Called when a deadlock is detected in a world.
std::map< AString, sWorldAge > WorldAges
Maps world name -> sWorldAge.
void TrackCriticalSection(cCriticalSection &a_CS, const AString &a_Name)
Adds the critical section for tracking.
std::vector< std::pair< cCriticalSection *, AString > > m_TrackedCriticalSections
CriticalSections that are tracked (their status output on deadlock).
virtual void Execute(void) override
This function, overloaded by the descendants, is called in the new thread.
cTickTimeLong m_Age
Last m_WorldAge that has been detected in this world.
int m_NumCyclesSame
Number of cycles for which the age has been the same.
cIsThread(AString &&a_ThreadName)
Definition: IsThread.cpp:16
void Start(void)
Starts the thread; returns without waiting for the actual start.
Definition: IsThread.cpp:35