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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "Watchdog.h"
#ifdef USE_VALGRIND
#include <valgrind/valgrind.h>
#endif
#include <algorithm>
#include <functional>
#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/Misc.h"
#include "System/Platform/Threading.h"
#include "System/Threading/SpringThreading.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
{
static const char* threadNames[] = {"main", "load", "audio"};
static spring::mutex wdmutex;
static unsigned int curorder = 0;
struct WatchDogThreadInfo {
WatchDogThreadInfo()
: threadid(0)
, numreg(0)
, timer(spring_notime)
{}
void ResetThreadControls() {
#ifndef WIN32
// this is not auto-destructed (!)
ctls.reset();
#endif
}
void SetThreadControls()
{
#ifndef WIN32
const auto& c = Threading::GetCurrentThreadControls();
assert(c.get() != nullptr);
// copy shared_ptr object, not shared_ptr*
ctls = c;
#endif
}
volatile Threading::NativeThreadHandle thread;
volatile Threading::NativeThreadId threadid;
volatile unsigned int numreg;
spring_time timer;
// not used on Windows
std::shared_ptr<Threading::ThreadControls> ctls;
};
struct WatchDogThreadSlot {
WatchDogThreadSlot()
: primary(false)
, active(false)
, regorder(0)
{}
volatile bool primary;
volatile bool active;
volatile unsigned int regorder;
};
// NOTE:
// these arrrays include one extra element so threads
// point somewhere non-NULL after being deregistered
static WatchDogThreadInfo registeredThreadsData[WDT_COUNT + 1];
static WatchDogThreadInfo* registeredThreads[WDT_COUNT + 1] = {nullptr};
static WatchDogThreadSlot threadSlots[WDT_COUNT + 1];
static std::map<std::string, unsigned int> threadNameToNum;
static spring::thread hangDetectorThread;
static spring_time hangTimeout = spring_msecs(0);
static volatile bool hangDetectorThreadInterrupted = false;
static inline void UpdateActiveThreads(Threading::NativeThreadId num) {
unsigned int active = WDT_COUNT;
for (unsigned int i = 0; i < WDT_COUNT; ++i) {
WatchDogThreadInfo* threadInfo = registeredThreads[i];
if (threadInfo->numreg == 0)
continue;
if (!Threading::NativeThreadIdsEqual(num, threadInfo->threadid))
continue;
threadSlots[i].active = false;
// find the active thread with maximum regorder
if (threadSlots[i].regorder > threadSlots[active].regorder)
active = i;
}
if (active == WDT_COUNT)
return;
threadSlots[active].active = true;
}
__FORCE_ALIGN_STACK__
static void HangDetectorLoop()
{
Threading::SetThreadName("watchdog");
Threading::SetWatchDogThread();
while (!hangDetectorThreadInterrupted) {
const spring_time curtime = spring_gettime();
bool hangDetected = false;
bool hangThreads[WDT_COUNT] = {false};
for (unsigned int i = 0; i < WDT_COUNT; ++i) {
hangThreads[i] = false;
if (!threadSlots[i].active)
continue;
WatchDogThreadInfo* threadInfo = registeredThreads[i];
const spring_time curwdt = threadInfo->timer;
if (spring_istime(curwdt) && (curtime - curwdt) > hangTimeout) {
hangDetected = true;
hangThreads[i] = true;
threadInfo->timer = curtime;
}
}
if (hangDetected) {
LOG_L(L_WARNING, "[Watchdog] Hang detection triggered for Spring %s.", SpringVersion::GetFull().c_str());
LOG_L(L_WARNING, "\t(in threads: {%s,%s,%s}={%d,%d,%d})",
threadNames[WDT_MAIN], threadNames[WDT_LOAD], threadNames[WDT_AUDIO],
hangThreads[WDT_MAIN], hangThreads[WDT_LOAD], hangThreads[WDT_AUDIO]
);
CrashHandler::PrepareStacktrace(LOG_LEVEL_WARNING);
// generate traces for all active (including non-hanged) threads
for (unsigned int i = 0; i < WDT_COUNT; ++i) {
if (!threadSlots[i].active)
continue;
#ifdef WIN32
CrashHandler::Stacktrace(registeredThreads[i]->thread, threadNames[i], LOG_LEVEL_WARNING);
#else
CrashHandler::SuspendedStacktrace(registeredThreads[i]->ctls.get(), std::string(threadNames[i]));
#endif
}
CrashHandler::CleanupStacktrace(LOG_LEVEL_WARNING);
}
spring::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void RegisterThread(WatchdogThreadnum num, bool primary)
{
std::lock_guard<spring::mutex> lock(wdmutex);
if (num >= WDT_COUNT || registeredThreads[num]->numreg != 0) {
LOG_L(L_ERROR, "[Watchdog::%s] Invalid thread number %u", __func__, num);
return;
}
Threading::NativeThreadHandle thread = Threading::GetCurrentThread();
Threading::NativeThreadId threadId = Threading::GetCurrentThreadId();
unsigned int inact = WDT_COUNT;
unsigned int i;
for (i = 0; i < WDT_COUNT; ++i) {
const WatchDogThreadInfo* threadInfo = ®isteredThreadsData[i];
if (threadInfo->numreg == 0)
inact = std::min(i, inact);
else if (Threading::NativeThreadIdsEqual(threadInfo->threadid, threadId))
break;
}
if (i >= WDT_COUNT)
i = inact;
if (i >= WDT_COUNT) {
LOG_L(L_ERROR, "[Watchdog::%s] Internal error", __func__);
return;
}
registeredThreads[num] = ®isteredThreadsData[i];
// caller optionally does this so it can choose arbitrary names
// Threading::SetThreadName(threadNames[num]);
WatchDogThreadInfo* threadInfo = registeredThreads[num];
threadInfo->thread = thread;
threadInfo->threadid = threadId;
threadInfo->timer = spring_gettime();
threadInfo->numreg += 1;
// note: WDT_MAIN and WDT_LOAD share the same controls if LoadingMT=0
LOG("[WatchDog::%s] registering controls for thread [%s]", __func__, threadNames[num]);
threadInfo->SetThreadControls();
threadSlots[num].primary = primary;
threadSlots[num].regorder = ++curorder;
UpdateActiveThreads(threadId);
}
bool DeregisterThread(WatchdogThreadnum num)
{
std::lock_guard<spring::mutex> lock(wdmutex);
WatchDogThreadInfo* threadInfo = nullptr;
if (num >= WDT_COUNT || registeredThreads[num] == nullptr || (threadInfo = registeredThreads[num])->numreg == 0) {
LOG_L(L_ERROR, "[Watchdog::%s] Invalid thread number %u", __func__, num);
return false;
}
threadSlots[num].primary = false;
threadSlots[num].regorder = 0;
UpdateActiveThreads(threadInfo->threadid);
// reset the main thread's controls only if actually called from it;
// otherwise Load would act in place of Main in the LoadingMT=0 case
if (num == WDT_MAIN || !Threading::IsMainThread()) {
LOG("[WatchDog::%s] deregistering controls for thread [%s]", __func__, threadNames[num]);
threadInfo->ResetThreadControls();
}
if (0 == --(threadInfo->numreg))
memset(threadInfo, 0, sizeof(WatchDogThreadInfo));
registeredThreads[num] = ®isteredThreadsData[WDT_COUNT];
return true;
}
void ClearTimer(bool disable, Threading::NativeThreadId* _threadId)
{
// bail if Watchdog isn't running
if (!hangDetectorThread.joinable())
return;
// calling thread can be the watchdog thread
// itself (see HangDetectorLoop), which does
// not count here and is unregistered anyway
if (Threading::IsWatchDogThread())
return;
Threading::NativeThreadId threadId;
if (_threadId) {
threadId = *_threadId;
} else {
threadId = Threading::GetCurrentThreadId();
}
unsigned int num = 0;
for (; num < WDT_COUNT; ++num) {
if (Threading::NativeThreadIdsEqual(registeredThreads[num]->threadid, threadId))
break;
}
WatchDogThreadInfo* threadInfo;
if (num >= WDT_COUNT || (threadInfo = registeredThreads[num])->numreg == 0) {
LOG_L(L_ERROR, "[Watchdog::%s(id)] Invalid thread %d (_threadId=%p)", __func__, num, _threadId);
return;
}
threadInfo->timer = disable ? spring_notime : spring_gettime();
}
void ClearTimer(WatchdogThreadnum num, bool disable)
{
if (!hangDetectorThread.joinable())
return;
if (Threading::IsWatchDogThread())
return;
WatchDogThreadInfo* threadInfo;
if (num >= WDT_COUNT || (threadInfo = registeredThreads[num])->numreg == 0) {
LOG_L(L_ERROR, "[Watchdog::%s(num)] Invalid thread %d", __func__, num);
return;
}
threadInfo->timer = disable ? spring_notime : spring_gettime();
}
void ClearTimer(const std::string& name, bool disable)
{
if (!hangDetectorThread.joinable())
return;
if (Threading::IsWatchDogThread())
return;
const auto i = threadNameToNum.find(name);
unsigned int num;
WatchDogThreadInfo* threadInfo;
if (i == threadNameToNum.end() || (num = i->second) >= WDT_COUNT || (threadInfo = registeredThreads[num])->numreg == 0) {
LOG_L(L_ERROR, "[Watchdog::%s(name)] Invalid thread name \"%s\"", __func__, name.c_str());
return;
}
threadInfo->timer = disable ? spring_notime : spring_gettime();
}
void ClearPrimaryTimers(bool disable)
{
if (!hangDetectorThread.joinable())
return; //! Watchdog isn't running
for (unsigned int i = 0; i < WDT_COUNT; ++i) {
WatchDogThreadInfo* threadInfo = registeredThreads[i];
if (threadSlots[i].primary)
threadInfo->timer = disable ? spring_notime : spring_gettime();
}
}
void Install()
{
std::lock_guard<spring::mutex> lock(wdmutex);
memset(registeredThreadsData, 0, sizeof(registeredThreadsData));
for (unsigned int i = 0; i < WDT_COUNT; ++i) {
registeredThreads[i] = ®isteredThreadsData[WDT_COUNT];
threadNameToNum[std::string(threadNames[i])] = i;
}
memset(threadSlots, 0, sizeof(threadSlots));
// disable if gdb is running
if (Platform::IsRunningInGDB()) {
LOG("[WatchDog::%s] disabled (gdb detected)", __func__);
return;
}
#ifdef USE_VALGRIND
if (RUNNING_ON_VALGRIND) {
LOG("[WatchDog::%s] disabled (Valgrind detected)", __func__);
return;
}
#endif
const int hangTimeoutSecs = configHandler->GetInt("HangTimeout");
// HangTimeout = -1 to force disable hang detection
if (hangTimeoutSecs <= 0) {
LOG("[WatchDog::%s] disabled", __func__);
return;
}
hangTimeout = spring_secs(hangTimeoutSecs);
// start the watchdog thread
hangDetectorThread = std::move(spring::thread(&HangDetectorLoop));
LOG("[WatchDog%s] Installed (HangTimeout: %isec)", __func__, hangTimeoutSecs);
}
void Uninstall()
{
LOG_L(L_INFO, "[WatchDog::%s][1] hangDetectorThread=%p (joinable=%d)", __func__, &hangDetectorThread, hangDetectorThread.joinable());
if (!hangDetectorThread.joinable())
return;
std::lock_guard<spring::mutex> lock(wdmutex);
hangDetectorThreadInterrupted = true;
LOG_L(L_INFO, "[WatchDog::%s][2]", __func__);
hangDetectorThread.join();
LOG_L(L_INFO, "[WatchDog::%s][3]", __func__);
memset(registeredThreadsData, 0, sizeof(registeredThreadsData));
for (unsigned int i = 0; i < WDT_COUNT; ++i)
registeredThreads[i] = ®isteredThreadsData[WDT_COUNT];
memset(threadSlots, 0, sizeof(threadSlots));
threadNameToNum.clear();
}
}
|