Annotation of /trunk/indra/newview/llwatchdog.cpp
Parent Directory
|
Revision Log
Revision 137 - (view) (download)
| 1 : | mjm | 135 | /** |
| 2 : | * @file llthreadwatchdog.cpp | ||
| 3 : | * @brief The LLThreadWatchdog class definitions | ||
| 4 : | * | ||
| 5 : | * $LicenseInfo:firstyear=2007&license=viewergpl$ | ||
| 6 : | * | ||
| 7 : | mjm | 137 | * Copyright (c) 2007-2010, Linden Research, Inc. |
| 8 : | mjm | 135 | * |
| 9 : | * Second Life Viewer Source Code | ||
| 10 : | * The source code in this file ("Source Code") is provided by Linden Lab | ||
| 11 : | * to you under the terms of the GNU General Public License, version 2.0 | ||
| 12 : | * ("GPL"), unless you have obtained a separate licensing agreement | ||
| 13 : | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
| 14 : | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
| 15 : | * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2 | ||
| 16 : | * | ||
| 17 : | * There are special exceptions to the terms and conditions of the GPL as | ||
| 18 : | * it is applied to this Source Code. View the full text of the exception | ||
| 19 : | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
| 20 : | * online at | ||
| 21 : | * http://secondlifegrid.net/programs/open_source/licensing/flossexception | ||
| 22 : | * | ||
| 23 : | * By copying, modifying or distributing this software, you acknowledge | ||
| 24 : | * that you have read and understood your obligations described above, | ||
| 25 : | * and agree to abide by those obligations. | ||
| 26 : | * | ||
| 27 : | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
| 28 : | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
| 29 : | * COMPLETENESS OR PERFORMANCE. | ||
| 30 : | * $/LicenseInfo$ | ||
| 31 : | */ | ||
| 32 : | |||
| 33 : | |||
| 34 : | #include "llviewerprecompiledheaders.h" | ||
| 35 : | #include "llwatchdog.h" | ||
| 36 : | |||
| 37 : | const U32 WATCHDOG_SLEEP_TIME_USEC = 1000000; | ||
| 38 : | |||
| 39 : | void default_killer_callback() | ||
| 40 : | { | ||
| 41 : | #ifdef LL_WINDOWS | ||
| 42 : | RaiseException(0,0,0,0); | ||
| 43 : | #else | ||
| 44 : | raise(SIGQUIT); | ||
| 45 : | #endif | ||
| 46 : | } | ||
| 47 : | |||
| 48 : | // This class runs the watchdog timing thread. | ||
| 49 : | class LLWatchdogTimerThread : public LLThread | ||
| 50 : | { | ||
| 51 : | public: | ||
| 52 : | LLWatchdogTimerThread() : | ||
| 53 : | LLThread("Watchdog"), | ||
| 54 : | mSleepMsecs(0), | ||
| 55 : | mStopping(false) | ||
| 56 : | { | ||
| 57 : | } | ||
| 58 : | |||
| 59 : | ~LLWatchdogTimerThread() {} | ||
| 60 : | |||
| 61 : | void setSleepTime(long ms) { mSleepMsecs = ms; } | ||
| 62 : | void stop() | ||
| 63 : | { | ||
| 64 : | mStopping = true; | ||
| 65 : | mSleepMsecs = 1; | ||
| 66 : | } | ||
| 67 : | |||
| 68 : | /* virtual */ void run() | ||
| 69 : | { | ||
| 70 : | while(!mStopping) | ||
| 71 : | { | ||
| 72 : | LLWatchdog::getInstance()->run(); | ||
| 73 : | ms_sleep(mSleepMsecs); | ||
| 74 : | } | ||
| 75 : | } | ||
| 76 : | |||
| 77 : | private: | ||
| 78 : | long mSleepMsecs; | ||
| 79 : | bool mStopping; | ||
| 80 : | }; | ||
| 81 : | |||
| 82 : | // LLWatchdogEntry | ||
| 83 : | LLWatchdogEntry::LLWatchdogEntry() | ||
| 84 : | { | ||
| 85 : | } | ||
| 86 : | |||
| 87 : | LLWatchdogEntry::~LLWatchdogEntry() | ||
| 88 : | { | ||
| 89 : | stop(); | ||
| 90 : | } | ||
| 91 : | |||
| 92 : | void LLWatchdogEntry::start() | ||
| 93 : | { | ||
| 94 : | LLWatchdog::getInstance()->add(this); | ||
| 95 : | } | ||
| 96 : | |||
| 97 : | void LLWatchdogEntry::stop() | ||
| 98 : | { | ||
| 99 : | LLWatchdog::getInstance()->remove(this); | ||
| 100 : | } | ||
| 101 : | |||
| 102 : | // LLWatchdogTimeout | ||
| 103 : | const std::string UNINIT_STRING = "uninitialized"; | ||
| 104 : | |||
| 105 : | LLWatchdogTimeout::LLWatchdogTimeout() : | ||
| 106 : | mTimeout(0.0f), | ||
| 107 : | mPingState(UNINIT_STRING) | ||
| 108 : | { | ||
| 109 : | } | ||
| 110 : | |||
| 111 : | LLWatchdogTimeout::~LLWatchdogTimeout() | ||
| 112 : | { | ||
| 113 : | } | ||
| 114 : | |||
| 115 : | bool LLWatchdogTimeout::isAlive() const | ||
| 116 : | { | ||
| 117 : | return (mTimer.getStarted() && !mTimer.hasExpired()); | ||
| 118 : | } | ||
| 119 : | |||
| 120 : | void LLWatchdogTimeout::reset() | ||
| 121 : | { | ||
| 122 : | mTimer.setTimerExpirySec(mTimeout); | ||
| 123 : | } | ||
| 124 : | |||
| 125 : | void LLWatchdogTimeout::setTimeout(F32 d) | ||
| 126 : | { | ||
| 127 : | mTimeout = d; | ||
| 128 : | } | ||
| 129 : | |||
| 130 : | void LLWatchdogTimeout::start(const std::string& state) | ||
| 131 : | { | ||
| 132 : | // Order of operation is very impmortant here. | ||
| 133 : | // After LLWatchdogEntry::start() is called | ||
| 134 : | // LLWatchdogTimeout::isAlive() will be called asynchronously. | ||
| 135 : | mTimer.start(); | ||
| 136 : | ping(state); | ||
| 137 : | LLWatchdogEntry::start(); | ||
| 138 : | } | ||
| 139 : | |||
| 140 : | void LLWatchdogTimeout::stop() | ||
| 141 : | { | ||
| 142 : | LLWatchdogEntry::stop(); | ||
| 143 : | mTimer.stop(); | ||
| 144 : | } | ||
| 145 : | |||
| 146 : | void LLWatchdogTimeout::ping(const std::string& state) | ||
| 147 : | { | ||
| 148 : | if(!state.empty()) | ||
| 149 : | { | ||
| 150 : | mPingState = state; | ||
| 151 : | } | ||
| 152 : | reset(); | ||
| 153 : | } | ||
| 154 : | |||
| 155 : | // LLWatchdog | ||
| 156 : | LLWatchdog::LLWatchdog() : | ||
| 157 : | mSuspectsAccessMutex(NULL), | ||
| 158 : | mTimer(NULL), | ||
| 159 : | mLastClockCount(0), | ||
| 160 : | mKillerCallback(&default_killer_callback) | ||
| 161 : | { | ||
| 162 : | } | ||
| 163 : | |||
| 164 : | LLWatchdog::~LLWatchdog() | ||
| 165 : | { | ||
| 166 : | } | ||
| 167 : | |||
| 168 : | void LLWatchdog::add(LLWatchdogEntry* e) | ||
| 169 : | { | ||
| 170 : | lockThread(); | ||
| 171 : | mSuspects.insert(e); | ||
| 172 : | unlockThread(); | ||
| 173 : | } | ||
| 174 : | |||
| 175 : | void LLWatchdog::remove(LLWatchdogEntry* e) | ||
| 176 : | { | ||
| 177 : | lockThread(); | ||
| 178 : | mSuspects.erase(e); | ||
| 179 : | unlockThread(); | ||
| 180 : | } | ||
| 181 : | |||
| 182 : | void LLWatchdog::init(killer_event_callback func) | ||
| 183 : | { | ||
| 184 : | mKillerCallback = func; | ||
| 185 : | if(!mSuspectsAccessMutex && !mTimer) | ||
| 186 : | { | ||
| 187 : | mSuspectsAccessMutex = new LLMutex(NULL); | ||
| 188 : | mTimer = new LLWatchdogTimerThread(); | ||
| 189 : | mTimer->setSleepTime(WATCHDOG_SLEEP_TIME_USEC / 1000); | ||
| 190 : | mLastClockCount = LLTimer::getTotalTime(); | ||
| 191 : | |||
| 192 : | // mTimer->start() kicks off the thread, any code after | ||
| 193 : | // start needs to use the mSuspectsAccessMutex | ||
| 194 : | mTimer->start(); | ||
| 195 : | } | ||
| 196 : | } | ||
| 197 : | |||
| 198 : | void LLWatchdog::cleanup() | ||
| 199 : | { | ||
| 200 : | if(mTimer) | ||
| 201 : | { | ||
| 202 : | mTimer->stop(); | ||
| 203 : | delete mTimer; | ||
| 204 : | mTimer = NULL; | ||
| 205 : | } | ||
| 206 : | |||
| 207 : | if(mSuspectsAccessMutex) | ||
| 208 : | { | ||
| 209 : | delete mSuspectsAccessMutex; | ||
| 210 : | mSuspectsAccessMutex = NULL; | ||
| 211 : | } | ||
| 212 : | |||
| 213 : | mLastClockCount = 0; | ||
| 214 : | } | ||
| 215 : | |||
| 216 : | void LLWatchdog::run() | ||
| 217 : | { | ||
| 218 : | lockThread(); | ||
| 219 : | |||
| 220 : | // Check the time since the last call to run... | ||
| 221 : | // If the time elapsed is two times greater than the regualr sleep time | ||
| 222 : | // reset the active timeouts. | ||
| 223 : | const U32 TIME_ELAPSED_MULTIPLIER = 2; | ||
| 224 : | U64 current_time = LLTimer::getTotalTime(); | ||
| 225 : | U64 current_run_delta = current_time - mLastClockCount; | ||
| 226 : | mLastClockCount = current_time; | ||
| 227 : | |||
| 228 : | if(current_run_delta > (WATCHDOG_SLEEP_TIME_USEC * TIME_ELAPSED_MULTIPLIER)) | ||
| 229 : | { | ||
| 230 : | llinfos << "Watchdog thread delayed: resetting entries." << llendl; | ||
| 231 : | std::for_each(mSuspects.begin(), | ||
| 232 : | mSuspects.end(), | ||
| 233 : | std::mem_fun(&LLWatchdogEntry::reset) | ||
| 234 : | ); | ||
| 235 : | } | ||
| 236 : | else | ||
| 237 : | { | ||
| 238 : | SuspectsRegistry::iterator result = | ||
| 239 : | std::find_if(mSuspects.begin(), | ||
| 240 : | mSuspects.end(), | ||
| 241 : | std::not1(std::mem_fun(&LLWatchdogEntry::isAlive)) | ||
| 242 : | ); | ||
| 243 : | |||
| 244 : | if(result != mSuspects.end()) | ||
| 245 : | { | ||
| 246 : | // error!!! | ||
| 247 : | if(mTimer) | ||
| 248 : | { | ||
| 249 : | mTimer->stop(); | ||
| 250 : | } | ||
| 251 : | |||
| 252 : | llinfos << "Watchdog detected error:" << llendl; | ||
| 253 : | mKillerCallback(); | ||
| 254 : | } | ||
| 255 : | } | ||
| 256 : | |||
| 257 : | |||
| 258 : | unlockThread(); | ||
| 259 : | } | ||
| 260 : | |||
| 261 : | void LLWatchdog::lockThread() | ||
| 262 : | { | ||
| 263 : | if(mSuspectsAccessMutex != NULL) | ||
| 264 : | { | ||
| 265 : | mSuspectsAccessMutex->lock(); | ||
| 266 : | } | ||
| 267 : | } | ||
| 268 : | |||
| 269 : | void LLWatchdog::unlockThread() | ||
| 270 : | { | ||
| 271 : | if(mSuspectsAccessMutex != NULL) | ||
| 272 : | { | ||
| 273 : | mSuspectsAccessMutex->unlock(); | ||
| 274 : | } | ||
| 275 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

