summaryrefslogtreecommitdiffstats
path: root/source/DeadlockDetect.cpp
blob: de9a32d39cd8f28358679c3f1353eca37d018f61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

// DeadlockDetect.cpp

// Declares the cDeadlockDetect class that tries to detect deadlocks and aborts the server when it detects one

#include "Globals.h"
#include "DeadlockDetect.h"
#include "Root.h"
#include "World.h"





/// Number of milliseconds per cycle
const int CYCLE_MILLISECONDS = 100;

/// When the number of cycles for the same world age hits this value, it is considered a deadlock
const int NUM_CYCLES_LIMIT = 200;  // 200 = twenty seconds





cDeadlockDetect::cDeadlockDetect(void) :
	super("DeadlockDetect")
{
}





bool cDeadlockDetect::Start(void)
{
	// Read the initial world data:
	class cFillIn :
		public cWorldListCallback
	{
	public:
		cFillIn(cDeadlockDetect * a_Detect) :
			m_Detect(a_Detect)
		{
		}
		
		virtual bool Item(cWorld * a_World) override
		{
			m_Detect->SetWorldAge(a_World->GetName(), a_World->GetWorldAge());
			return false;
		}
		
	protected:
		cDeadlockDetect * m_Detect;
	} FillIn(this);
	cRoot::Get()->ForEachWorld(FillIn);
	return super::Start();
}





void cDeadlockDetect::Execute(void)
{
	// Loop until the event is signalled
	while (!m_ShouldTerminate)
	{
		// Check the world ages:
		class cChecker :
			public cWorldListCallback
		{
		public:
			cChecker(cDeadlockDetect * a_Detect) :
				m_Detect(a_Detect)
			{
			}
			
		protected:
			cDeadlockDetect * m_Detect;
			
			virtual bool Item(cWorld * a_World) override
			{
				m_Detect->CheckWorldAge(a_World->GetName(), a_World->GetWorldAge());
				return false;
			}
		} Checker(this);
		cRoot::Get()->ForEachWorld(Checker);
		
		cSleep::MilliSleep(CYCLE_MILLISECONDS);
	}  // while (should run)
}





void cDeadlockDetect::SetWorldAge(const AString & a_WorldName, Int64 a_Age)
{
	m_WorldAges[a_WorldName].m_Age = a_Age;
	m_WorldAges[a_WorldName].m_NumCyclesSame = 0;
}





void cDeadlockDetect::CheckWorldAge(const AString & a_WorldName, Int64 a_Age)
{
	WorldAges::iterator itr = m_WorldAges.find(a_WorldName);
	if (itr == m_WorldAges.end())
	{
		ASSERT(!"Unknown world in cDeadlockDetect");
		return;
	}
	if (itr->second.m_Age == a_Age)
	{
		itr->second.m_NumCyclesSame += 1;
		if (itr->second.m_NumCyclesSame > NUM_CYCLES_LIMIT)
		{
			DeadlockDetected();
			return;
		}
	}
	else
	{
		itr->second.m_Age = a_Age;
		itr->second.m_NumCyclesSame = 0;
	}
}





void cDeadlockDetect::DeadlockDetected(void)
{
	ASSERT(!"Deadlock detected");
	
	// TODO: Make a crashdump / coredump
	
	// Crash the server intentionally:
	*((int *)0) = 0;
}