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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "System/Platform/Win/win32.h"
#include "Watchdog.h"
#include "lib/gml/gml.h"
#ifndef _WIN32
#include <fstream>
#include <iostream>
#endif
#include <algorithm>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include "Game/GameVersion.h"
#include "System/Config/ConfigHandler.h"
#include "System/Log/ILog.h"
#include "System/maindefines.h"
#include "System/Misc/SpringTime.h"
#include "System/Platform/CrashHandler.h"
#include "System/Platform/Threading.h"
CONFIG(int, HangTimeout).defaultValue(10).minimumValue(-1).maximumValue(600)
.description("Number of seconds that, if spent in the same code segment, indicate a hang; -1 to disable.");
namespace Watchdog
{
const char *threadNames[] = {"main", "sim", "load", "audio"};
static boost::mutex wdmutex;
static unsigned int curorder = 0;
struct WatchDogThreadInfo {
WatchDogThreadInfo()
: threadid(0)
, timer(spring_notime)
, numreg(0)
{}
volatile Threading::NativeThreadHandle thread;
volatile Threading::NativeThreadId threadid;
#ifdef STATIC_SPRING_TIME
// spring_time is a class here, those can't be volatile
spring_time timer;
#else
volatile spring_time timer;
#endif
volatile unsigned int numreg;
};
static WatchDogThreadInfo registeredThreadsData[WDT_SIZE];
struct WatchDogThreadSlot {
WatchDogThreadSlot()
: primary(false)
, active(false)
, regorder(0)
{}
volatile bool primary;
volatile bool active;
volatile unsigned int regorder;
};
static WatchDogThreadInfo *registeredThreads[WDT_SIZE] = {NULL};
static WatchDogThreadSlot threadSlots[WDT_SIZE];
static std::map<std::string, unsigned int> threadNameToNum;
static boost::thread* hangDetectorThread = NULL;
static spring_time hangTimeout = spring_msecs(0);
static volatile bool hangDetectorThreadInterrupted = false;
static inline void UpdateActiveThreads(Threading::NativeThreadId num) {
unsigned int active = WDT_LAST;
for (unsigned int i = 0; i < WDT_LAST; ++i) {
WatchDogThreadInfo* th_info = registeredThreads[i];
if (th_info->numreg && Threading::NativeThreadIdsEqual(num, th_info->threadid)) {
threadSlots[i].active = false;
if (threadSlots[i].regorder > threadSlots[active].regorder)
active = i;
}
}
if (active < WDT_LAST)
threadSlots[active].active = true;
}
static void HangDetectorLoop()
{
Threading::SetThreadName("watchdog");
while (!hangDetectorThreadInterrupted) {
spring_time curtime = spring_gettime();
bool hangDetected = false;
for (unsigned int i = 0; i < WDT_LAST; ++i) {
if (!threadSlots[i].active)
continue;
WatchDogThreadInfo* th_info = registeredThreads[i];
spring_time curwdt = th_info->timer;
if (spring_istime(curwdt) && curtime > curwdt && (curtime - curwdt) > hangTimeout) {
if (!hangDetected) {
LOG_L(L_WARNING, "[Watchdog] Hang detection triggered for Spring %s.", SpringVersion::GetFull().c_str());
#ifdef USE_GML
LOG_L(L_WARNING, "MT with %d threads.", GML::ThreadCount());
#endif
}
LOG_L(L_WARNING, " (in thread: %s)", threadNames[i]);
hangDetected = true;
th_info->timer = curtime;
}
}
if (hangDetected) {
CrashHandler::PrepareStacktrace();
for (unsigned int i = 0; i < WDT_LAST; ++i) {
if (!threadSlots[i].active)
continue;
WatchDogThreadInfo* th_info = registeredThreads[i];
CrashHandler::Stacktrace(th_info->thread, threadNames[i]);
}
CrashHandler::CleanupStacktrace();
}
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
void RegisterThread(WatchdogThreadnum num, bool primary)
{
boost::mutex::scoped_lock lock(wdmutex);
if (num >= WDT_LAST || registeredThreads[num]->numreg) {
LOG_L(L_ERROR, "[Watchdog::RegisterThread] Invalid thread number");
return;
}
Threading::NativeThreadHandle thread = Threading::GetCurrentThread();
Threading::NativeThreadId threadId = Threading::GetCurrentThreadId();
unsigned int inact = WDT_LAST;
unsigned int i;
for (i = 0; i < WDT_LAST; ++i) {
WatchDogThreadInfo* th_info = ®isteredThreadsData[i];
if (!th_info->numreg)
inact = std::min(i, inact);
else if(Threading::NativeThreadIdsEqual(th_info->threadid, threadId))
break;
}
if (i >= WDT_LAST)
i = inact;
if (i >= WDT_LAST) {
LOG_L(L_ERROR, "[Watchdog::RegisterThread] Internal error");
return;
}
registeredThreads[num] = ®isteredThreadsData[i];
// set threadname
//Threading::SetThreadName(threadNames[num]);
WatchDogThreadInfo* th_info = registeredThreads[num];
th_info->thread = thread;
th_info->threadid = threadId;
th_info->timer = spring_gettime();
++th_info->numreg;
threadSlots[num].primary = primary;
threadSlots[num].regorder = ++curorder;
UpdateActiveThreads(threadId);
}
void DeregisterThread(WatchdogThreadnum num)
{
boost::mutex::scoped_lock lock(wdmutex);
WatchDogThreadInfo* th_info;
if (num >= WDT_LAST || !(th_info = registeredThreads[num])->numreg) {
LOG_L(L_ERROR, "[Watchdog::DeregisterThread] Invalid thread number");
return;
}
threadSlots[num].primary = false;
threadSlots[num].regorder = 0;
UpdateActiveThreads(th_info->threadid);
if(!--(th_info->numreg))
memset(th_info, 0, sizeof(WatchDogThreadInfo));
registeredThreads[num] = ®isteredThreadsData[WDT_LAST];
}
void ClearTimer(bool disable, Threading::NativeThreadId* _threadId)
{
if (hangDetectorThread == NULL)
return; //! Watchdog isn't running
Threading::NativeThreadId threadId;
if (_threadId)
threadId = *_threadId;
else
threadId = Threading::GetCurrentThreadId();
unsigned int num = 0;
for (; num < WDT_LAST; ++num)
if (Threading::NativeThreadIdsEqual(registeredThreads[num]->threadid, threadId))
break;
WatchDogThreadInfo* th_info;
if (num >= WDT_LAST || !(th_info = registeredThreads[num])->numreg) {
LOG_L(L_ERROR, "[Watchdog::ClearTimer] Invalid thread %d", num);
return;
}
th_info->timer = disable ? spring_notime : spring_gettime();
}
void ClearTimer(WatchdogThreadnum num, bool disable)
{
if (hangDetectorThread == NULL)
return; //! Watchdog isn't running
WatchDogThreadInfo* th_info;
if (num >= WDT_LAST || !(th_info = registeredThreads[num])->numreg) {
LOG_L(L_ERROR, "[Watchdog::ClearTimer] Invalid thread number %d", num);
return;
}
th_info->timer = disable ? spring_notime : spring_gettime();
}
void ClearTimer(const std::string &name, bool disable)
{
if (hangDetectorThread == NULL)
return; //! Watchdog isn't running
std::map<std::string, unsigned int>::iterator i = threadNameToNum.find(name);
unsigned int num;
WatchDogThreadInfo* th_info;
if (i == threadNameToNum.end() || (num = i->second) >= WDT_LAST || !(th_info = registeredThreads[num])->numreg) {
LOG_L(L_ERROR, "[Watchdog::ClearTimer] Invalid thread name");
return;
}
th_info->timer = disable ? spring_notime : spring_gettime();
}
void ClearPrimaryTimers(bool disable)
{
if (hangDetectorThread == NULL)
return; //! Watchdog isn't running
for (unsigned int i = 0; i < WDT_LAST; ++i) {
WatchDogThreadInfo* th_info = registeredThreads[i];
if (threadSlots[i].primary)
th_info->timer = disable ? spring_notime : spring_gettime();
}
}
void Install()
{
boost::mutex::scoped_lock lock(wdmutex);
memset(registeredThreadsData, 0, sizeof(registeredThreadsData));
for (unsigned int i = 0; i < WDT_LAST; ++i) {
registeredThreads[i] = ®isteredThreadsData[WDT_LAST];
threadNameToNum[std::string(threadNames[i])] = i;
}
memset(threadSlots, 0, sizeof(threadSlots));
#ifndef _WIN32
// disable if gdb is running
char buf[1024];
SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", getppid());
std::ifstream f(buf);
if (f.good()) {
f.read(buf,sizeof(buf));
f.close();
std::string str(buf);
if (str.find("gdb") != std::string::npos) {
LOG("[Watchdog] disabled (gdb detected)");
return;
}
}
#endif
const int hangTimeoutSecs = configHandler->GetInt("HangTimeout");
// HangTimeout = -1 to force disable hang detection
if (hangTimeoutSecs <= 0) {
LOG("[Watchdog] disabled");
return;
}
hangTimeout = spring_secs(hangTimeoutSecs);
// start the watchdog thread
hangDetectorThread = new boost::thread(&HangDetectorLoop);
LOG("[Watchdog] Installed (HangTimeout: %isec)", hangTimeoutSecs);
}
void Uninstall()
{
if (hangDetectorThread == NULL) {
return;
}
boost::mutex::scoped_lock lock(wdmutex);
hangDetectorThreadInterrupted = true;
hangDetectorThread->join();
delete hangDetectorThread;
hangDetectorThread = NULL;
memset(registeredThreadsData, 0, sizeof(registeredThreadsData));
for (unsigned int i = 0; i < WDT_LAST; ++i)
registeredThreads[i] = ®isteredThreadsData[WDT_LAST];
memset(threadSlots, 0, sizeof(threadSlots));
threadNameToNum.clear();
}
}
|