Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "mozilla/ArrayUtils.h" |
michael@0 | 7 | #include "mozilla/BackgroundHangMonitor.h" |
michael@0 | 8 | #include "mozilla/LinkedList.h" |
michael@0 | 9 | #include "mozilla/Monitor.h" |
michael@0 | 10 | #include "mozilla/Move.h" |
michael@0 | 11 | #include "mozilla/StaticPtr.h" |
michael@0 | 12 | #include "mozilla/Telemetry.h" |
michael@0 | 13 | #include "mozilla/ThreadHangStats.h" |
michael@0 | 14 | #include "mozilla/ThreadLocal.h" |
michael@0 | 15 | #ifdef MOZ_NUWA_PROCESS |
michael@0 | 16 | #include "ipc/Nuwa.h" |
michael@0 | 17 | #endif |
michael@0 | 18 | |
michael@0 | 19 | #include "prinrval.h" |
michael@0 | 20 | #include "prthread.h" |
michael@0 | 21 | #include "ThreadStackHelper.h" |
michael@0 | 22 | |
michael@0 | 23 | #include <algorithm> |
michael@0 | 24 | |
michael@0 | 25 | namespace mozilla { |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * BackgroundHangManager is the global object that |
michael@0 | 29 | * manages all instances of BackgroundHangThread. |
michael@0 | 30 | */ |
michael@0 | 31 | class BackgroundHangManager |
michael@0 | 32 | { |
michael@0 | 33 | private: |
michael@0 | 34 | // Background hang monitor thread function |
michael@0 | 35 | static void MonitorThread(void* aData) |
michael@0 | 36 | { |
michael@0 | 37 | PR_SetCurrentThreadName("BgHangManager"); |
michael@0 | 38 | |
michael@0 | 39 | #ifdef MOZ_NUWA_PROCESS |
michael@0 | 40 | if (IsNuwaProcess()) { |
michael@0 | 41 | NS_ASSERTION(NuwaMarkCurrentThread != nullptr, |
michael@0 | 42 | "NuwaMarkCurrentThread is undefined!"); |
michael@0 | 43 | NuwaMarkCurrentThread(nullptr, nullptr); |
michael@0 | 44 | } |
michael@0 | 45 | #endif |
michael@0 | 46 | |
michael@0 | 47 | /* We do not hold a reference to BackgroundHangManager here |
michael@0 | 48 | because the monitor thread only exists as long as the |
michael@0 | 49 | BackgroundHangManager instance exists. We stop the monitor |
michael@0 | 50 | thread in the BackgroundHangManager destructor, and we can |
michael@0 | 51 | only get to the destructor if we don't hold a reference here. */ |
michael@0 | 52 | static_cast<BackgroundHangManager*>(aData)->RunMonitorThread(); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | // Hang monitor thread |
michael@0 | 56 | PRThread* mHangMonitorThread; |
michael@0 | 57 | // Stop hang monitoring |
michael@0 | 58 | bool mShutdown; |
michael@0 | 59 | |
michael@0 | 60 | BackgroundHangManager(const BackgroundHangManager&); |
michael@0 | 61 | BackgroundHangManager& operator=(const BackgroundHangManager&); |
michael@0 | 62 | void RunMonitorThread(); |
michael@0 | 63 | |
michael@0 | 64 | public: |
michael@0 | 65 | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BackgroundHangManager) |
michael@0 | 66 | static StaticRefPtr<BackgroundHangManager> sInstance; |
michael@0 | 67 | |
michael@0 | 68 | // Lock for access to members of this class |
michael@0 | 69 | Monitor mLock; |
michael@0 | 70 | // Current time as seen by hang monitors |
michael@0 | 71 | PRIntervalTime mIntervalNow; |
michael@0 | 72 | // List of BackgroundHangThread instances associated with each thread |
michael@0 | 73 | LinkedList<BackgroundHangThread> mHangThreads; |
michael@0 | 74 | |
michael@0 | 75 | void Shutdown() |
michael@0 | 76 | { |
michael@0 | 77 | MonitorAutoLock autoLock(mLock); |
michael@0 | 78 | mShutdown = true; |
michael@0 | 79 | autoLock.Notify(); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | void Wakeup() |
michael@0 | 83 | { |
michael@0 | 84 | // PR_CreateThread could have failed earlier |
michael@0 | 85 | if (mHangMonitorThread) { |
michael@0 | 86 | // Use PR_Interrupt to avoid potentially taking a lock |
michael@0 | 87 | PR_Interrupt(mHangMonitorThread); |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | BackgroundHangManager(); |
michael@0 | 92 | ~BackgroundHangManager(); |
michael@0 | 93 | }; |
michael@0 | 94 | |
michael@0 | 95 | /** |
michael@0 | 96 | * BackgroundHangThread is a per-thread object that is used |
michael@0 | 97 | * by all instances of BackgroundHangMonitor to monitor hangs. |
michael@0 | 98 | */ |
michael@0 | 99 | class BackgroundHangThread : public LinkedListElement<BackgroundHangThread> |
michael@0 | 100 | { |
michael@0 | 101 | private: |
michael@0 | 102 | static ThreadLocal<BackgroundHangThread*> sTlsKey; |
michael@0 | 103 | |
michael@0 | 104 | BackgroundHangThread(const BackgroundHangThread&); |
michael@0 | 105 | BackgroundHangThread& operator=(const BackgroundHangThread&); |
michael@0 | 106 | ~BackgroundHangThread(); |
michael@0 | 107 | |
michael@0 | 108 | /* Keep a reference to the manager, so we can keep going even |
michael@0 | 109 | after BackgroundHangManager::Shutdown is called. */ |
michael@0 | 110 | const RefPtr<BackgroundHangManager> mManager; |
michael@0 | 111 | // Unique thread ID for identification |
michael@0 | 112 | const PRThread* mThreadID; |
michael@0 | 113 | |
michael@0 | 114 | public: |
michael@0 | 115 | NS_INLINE_DECL_REFCOUNTING(BackgroundHangThread) |
michael@0 | 116 | static BackgroundHangThread* FindThread(); |
michael@0 | 117 | |
michael@0 | 118 | static void Startup() |
michael@0 | 119 | { |
michael@0 | 120 | /* We can tolerate init() failing. |
michael@0 | 121 | The if block turns off warn_unused_result. */ |
michael@0 | 122 | if (!sTlsKey.init()) {} |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | // Hang timeout in ticks |
michael@0 | 126 | const PRIntervalTime mTimeout; |
michael@0 | 127 | // PermaHang timeout in ticks |
michael@0 | 128 | const PRIntervalTime mMaxTimeout; |
michael@0 | 129 | // Time at last activity |
michael@0 | 130 | PRIntervalTime mInterval; |
michael@0 | 131 | // Time when a hang started |
michael@0 | 132 | PRIntervalTime mHangStart; |
michael@0 | 133 | // Is the thread in a hang |
michael@0 | 134 | bool mHanging; |
michael@0 | 135 | // Is the thread in a waiting state |
michael@0 | 136 | bool mWaiting; |
michael@0 | 137 | // Platform-specific helper to get hang stacks |
michael@0 | 138 | ThreadStackHelper mStackHelper; |
michael@0 | 139 | // Stack of current hang |
michael@0 | 140 | Telemetry::HangHistogram::Stack mHangStack; |
michael@0 | 141 | // Statistics for telemetry |
michael@0 | 142 | Telemetry::ThreadHangStats mStats; |
michael@0 | 143 | |
michael@0 | 144 | BackgroundHangThread(const char* aName, |
michael@0 | 145 | uint32_t aTimeoutMs, |
michael@0 | 146 | uint32_t aMaxTimeoutMs); |
michael@0 | 147 | |
michael@0 | 148 | // Report a hang; aManager->mLock IS locked |
michael@0 | 149 | void ReportHang(PRIntervalTime aHangTime); |
michael@0 | 150 | // Report a permanent hang; aManager->mLock IS locked |
michael@0 | 151 | void ReportPermaHang(); |
michael@0 | 152 | // Called by BackgroundHangMonitor::NotifyActivity |
michael@0 | 153 | void NotifyActivity(); |
michael@0 | 154 | // Called by BackgroundHangMonitor::NotifyWait |
michael@0 | 155 | void NotifyWait() |
michael@0 | 156 | { |
michael@0 | 157 | NotifyActivity(); |
michael@0 | 158 | mWaiting = true; |
michael@0 | 159 | } |
michael@0 | 160 | }; |
michael@0 | 161 | |
michael@0 | 162 | |
michael@0 | 163 | StaticRefPtr<BackgroundHangManager> BackgroundHangManager::sInstance; |
michael@0 | 164 | |
michael@0 | 165 | ThreadLocal<BackgroundHangThread*> BackgroundHangThread::sTlsKey; |
michael@0 | 166 | |
michael@0 | 167 | |
michael@0 | 168 | BackgroundHangManager::BackgroundHangManager() |
michael@0 | 169 | : mShutdown(false) |
michael@0 | 170 | , mLock("BackgroundHangManager") |
michael@0 | 171 | , mIntervalNow(0) |
michael@0 | 172 | { |
michael@0 | 173 | // Lock so we don't race against the new monitor thread |
michael@0 | 174 | MonitorAutoLock autoLock(mLock); |
michael@0 | 175 | mHangMonitorThread = PR_CreateThread( |
michael@0 | 176 | PR_USER_THREAD, MonitorThread, this, |
michael@0 | 177 | PR_PRIORITY_LOW, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); |
michael@0 | 178 | |
michael@0 | 179 | MOZ_ASSERT(mHangMonitorThread, |
michael@0 | 180 | "Failed to create monitor thread"); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | BackgroundHangManager::~BackgroundHangManager() |
michael@0 | 184 | { |
michael@0 | 185 | MOZ_ASSERT(mShutdown, |
michael@0 | 186 | "Destruction without Shutdown call"); |
michael@0 | 187 | MOZ_ASSERT(mHangThreads.isEmpty(), |
michael@0 | 188 | "Destruction with outstanding monitors"); |
michael@0 | 189 | MOZ_ASSERT(mHangMonitorThread, |
michael@0 | 190 | "No monitor thread"); |
michael@0 | 191 | |
michael@0 | 192 | // PR_CreateThread could have failed above due to resource limitation |
michael@0 | 193 | if (mHangMonitorThread) { |
michael@0 | 194 | // The monitor thread can only live as long as the instance lives |
michael@0 | 195 | PR_JoinThread(mHangMonitorThread); |
michael@0 | 196 | } |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | void |
michael@0 | 200 | BackgroundHangManager::RunMonitorThread() |
michael@0 | 201 | { |
michael@0 | 202 | // Keep us locked except when waiting |
michael@0 | 203 | MonitorAutoLock autoLock(mLock); |
michael@0 | 204 | |
michael@0 | 205 | /* mIntervalNow is updated at various intervals determined by waitTime. |
michael@0 | 206 | However, if an update latency is too long (due to CPU scheduling, system |
michael@0 | 207 | sleep, etc.), we don't update mIntervalNow at all. This is done so that |
michael@0 | 208 | long latencies in our timing are not detected as hangs. systemTime is |
michael@0 | 209 | used to track PR_IntervalNow() and determine our latency. */ |
michael@0 | 210 | |
michael@0 | 211 | PRIntervalTime systemTime = PR_IntervalNow(); |
michael@0 | 212 | // Default values for the first iteration of thread loop |
michael@0 | 213 | PRIntervalTime waitTime = PR_INTERVAL_NO_WAIT; |
michael@0 | 214 | PRIntervalTime recheckTimeout = PR_INTERVAL_NO_WAIT; |
michael@0 | 215 | |
michael@0 | 216 | while (!mShutdown) { |
michael@0 | 217 | |
michael@0 | 218 | PR_ClearInterrupt(); |
michael@0 | 219 | nsresult rv = autoLock.Wait(waitTime); |
michael@0 | 220 | |
michael@0 | 221 | PRIntervalTime newTime = PR_IntervalNow(); |
michael@0 | 222 | PRIntervalTime systemInterval = newTime - systemTime; |
michael@0 | 223 | systemTime = newTime; |
michael@0 | 224 | |
michael@0 | 225 | /* waitTime is a quarter of the shortest timeout value; If our timing |
michael@0 | 226 | latency is low enough (less than half the shortest timeout value), |
michael@0 | 227 | we can update mIntervalNow. */ |
michael@0 | 228 | if (MOZ_LIKELY(waitTime != PR_INTERVAL_NO_TIMEOUT && |
michael@0 | 229 | systemInterval < 2 * waitTime)) { |
michael@0 | 230 | mIntervalNow += systemInterval; |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | /* If it's before the next recheck timeout, and our wait did not |
michael@0 | 234 | get interrupted (either through Notify or PR_Interrupt), we can |
michael@0 | 235 | keep the current waitTime and skip iterating through hang monitors. */ |
michael@0 | 236 | if (MOZ_LIKELY(systemInterval < recheckTimeout && |
michael@0 | 237 | systemInterval >= waitTime && |
michael@0 | 238 | rv == NS_OK)) { |
michael@0 | 239 | recheckTimeout -= systemInterval; |
michael@0 | 240 | continue; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | /* We are in one of the following scenarios, |
michael@0 | 244 | - Hang or permahang recheck timeout |
michael@0 | 245 | - Thread added/removed |
michael@0 | 246 | - Thread wait or hang ended |
michael@0 | 247 | In all cases, we want to go through our list of hang |
michael@0 | 248 | monitors and update waitTime and recheckTimeout. */ |
michael@0 | 249 | waitTime = PR_INTERVAL_NO_TIMEOUT; |
michael@0 | 250 | recheckTimeout = PR_INTERVAL_NO_TIMEOUT; |
michael@0 | 251 | |
michael@0 | 252 | // Locally hold mIntervalNow |
michael@0 | 253 | PRIntervalTime intervalNow = mIntervalNow; |
michael@0 | 254 | |
michael@0 | 255 | // iterate through hang monitors |
michael@0 | 256 | for (BackgroundHangThread* currentThread = mHangThreads.getFirst(); |
michael@0 | 257 | currentThread; currentThread = currentThread->getNext()) { |
michael@0 | 258 | |
michael@0 | 259 | if (currentThread->mWaiting) { |
michael@0 | 260 | // Thread is waiting, not hanging |
michael@0 | 261 | continue; |
michael@0 | 262 | } |
michael@0 | 263 | PRIntervalTime interval = currentThread->mInterval; |
michael@0 | 264 | PRIntervalTime hangTime = intervalNow - interval; |
michael@0 | 265 | if (MOZ_UNLIKELY(hangTime >= currentThread->mMaxTimeout)) { |
michael@0 | 266 | // A permahang started |
michael@0 | 267 | // Skip subsequent iterations and tolerate a race on mWaiting here |
michael@0 | 268 | currentThread->mWaiting = true; |
michael@0 | 269 | currentThread->mHanging = false; |
michael@0 | 270 | currentThread->ReportPermaHang(); |
michael@0 | 271 | continue; |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | if (MOZ_LIKELY(!currentThread->mHanging)) { |
michael@0 | 275 | if (MOZ_UNLIKELY(hangTime >= currentThread->mTimeout)) { |
michael@0 | 276 | // A hang started |
michael@0 | 277 | currentThread->mStackHelper.GetStack(currentThread->mHangStack); |
michael@0 | 278 | currentThread->mHangStart = interval; |
michael@0 | 279 | currentThread->mHanging = true; |
michael@0 | 280 | } |
michael@0 | 281 | } else { |
michael@0 | 282 | if (MOZ_LIKELY(interval != currentThread->mHangStart)) { |
michael@0 | 283 | // A hang ended |
michael@0 | 284 | currentThread->ReportHang(intervalNow - currentThread->mHangStart); |
michael@0 | 285 | currentThread->mHanging = false; |
michael@0 | 286 | } |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | /* If we are hanging, the next time we check for hang status is when |
michael@0 | 290 | the hang turns into a permahang. If we're not hanging, the next |
michael@0 | 291 | recheck timeout is when we may be entering a hang. */ |
michael@0 | 292 | PRIntervalTime nextRecheck; |
michael@0 | 293 | if (currentThread->mHanging) { |
michael@0 | 294 | nextRecheck = currentThread->mMaxTimeout; |
michael@0 | 295 | } else { |
michael@0 | 296 | nextRecheck = currentThread->mTimeout; |
michael@0 | 297 | } |
michael@0 | 298 | recheckTimeout = std::min(recheckTimeout, nextRecheck - hangTime); |
michael@0 | 299 | |
michael@0 | 300 | /* We wait for a quarter of the shortest timeout |
michael@0 | 301 | value to give mIntervalNow enough granularity. */ |
michael@0 | 302 | waitTime = std::min(waitTime, currentThread->mTimeout / 4); |
michael@0 | 303 | } |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | /* We are shutting down now. |
michael@0 | 307 | Wait for all outstanding monitors to unregister. */ |
michael@0 | 308 | while (!mHangThreads.isEmpty()) { |
michael@0 | 309 | autoLock.Wait(PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 310 | } |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | |
michael@0 | 314 | BackgroundHangThread::BackgroundHangThread(const char* aName, |
michael@0 | 315 | uint32_t aTimeoutMs, |
michael@0 | 316 | uint32_t aMaxTimeoutMs) |
michael@0 | 317 | : mManager(BackgroundHangManager::sInstance) |
michael@0 | 318 | , mThreadID(PR_GetCurrentThread()) |
michael@0 | 319 | , mTimeout(aTimeoutMs == BackgroundHangMonitor::kNoTimeout |
michael@0 | 320 | ? PR_INTERVAL_NO_TIMEOUT |
michael@0 | 321 | : PR_MillisecondsToInterval(aTimeoutMs)) |
michael@0 | 322 | , mMaxTimeout(aMaxTimeoutMs == BackgroundHangMonitor::kNoTimeout |
michael@0 | 323 | ? PR_INTERVAL_NO_TIMEOUT |
michael@0 | 324 | : PR_MillisecondsToInterval(aMaxTimeoutMs)) |
michael@0 | 325 | , mInterval(mManager->mIntervalNow) |
michael@0 | 326 | , mHangStart(mInterval) |
michael@0 | 327 | , mHanging(false) |
michael@0 | 328 | , mWaiting(true) |
michael@0 | 329 | , mStats(aName) |
michael@0 | 330 | { |
michael@0 | 331 | if (sTlsKey.initialized()) { |
michael@0 | 332 | sTlsKey.set(this); |
michael@0 | 333 | } |
michael@0 | 334 | // Lock here because LinkedList is not thread-safe |
michael@0 | 335 | MonitorAutoLock autoLock(mManager->mLock); |
michael@0 | 336 | // Add to thread list |
michael@0 | 337 | mManager->mHangThreads.insertBack(this); |
michael@0 | 338 | // Wake up monitor thread to process new thread |
michael@0 | 339 | autoLock.Notify(); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | BackgroundHangThread::~BackgroundHangThread() |
michael@0 | 343 | { |
michael@0 | 344 | // Lock here because LinkedList is not thread-safe |
michael@0 | 345 | MonitorAutoLock autoLock(mManager->mLock); |
michael@0 | 346 | // Remove from thread list |
michael@0 | 347 | remove(); |
michael@0 | 348 | // Wake up monitor thread to process removed thread |
michael@0 | 349 | autoLock.Notify(); |
michael@0 | 350 | |
michael@0 | 351 | // We no longer have a thread |
michael@0 | 352 | if (sTlsKey.initialized()) { |
michael@0 | 353 | sTlsKey.set(nullptr); |
michael@0 | 354 | } |
michael@0 | 355 | |
michael@0 | 356 | // Move our copy of ThreadHangStats to Telemetry storage |
michael@0 | 357 | Telemetry::RecordThreadHangStats(mStats); |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | void |
michael@0 | 361 | BackgroundHangThread::ReportHang(PRIntervalTime aHangTime) |
michael@0 | 362 | { |
michael@0 | 363 | // Recovered from a hang; called on the monitor thread |
michael@0 | 364 | // mManager->mLock IS locked |
michael@0 | 365 | |
michael@0 | 366 | Telemetry::HangHistogram newHistogram(Move(mHangStack)); |
michael@0 | 367 | for (Telemetry::HangHistogram* oldHistogram = mStats.mHangs.begin(); |
michael@0 | 368 | oldHistogram != mStats.mHangs.end(); oldHistogram++) { |
michael@0 | 369 | if (newHistogram == *oldHistogram) { |
michael@0 | 370 | // New histogram matches old one |
michael@0 | 371 | oldHistogram->Add(aHangTime); |
michael@0 | 372 | return; |
michael@0 | 373 | } |
michael@0 | 374 | } |
michael@0 | 375 | // Add new histogram |
michael@0 | 376 | newHistogram.Add(aHangTime); |
michael@0 | 377 | mStats.mHangs.append(Move(newHistogram)); |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | void |
michael@0 | 381 | BackgroundHangThread::ReportPermaHang() |
michael@0 | 382 | { |
michael@0 | 383 | // Permanently hanged; called on the monitor thread |
michael@0 | 384 | // mManager->mLock IS locked |
michael@0 | 385 | |
michael@0 | 386 | // TODO: Add more detailed analysis for perma-hangs |
michael@0 | 387 | ReportHang(mMaxTimeout); |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | MOZ_ALWAYS_INLINE void |
michael@0 | 391 | BackgroundHangThread::NotifyActivity() |
michael@0 | 392 | { |
michael@0 | 393 | PRIntervalTime intervalNow = mManager->mIntervalNow; |
michael@0 | 394 | if (mWaiting) { |
michael@0 | 395 | mInterval = intervalNow; |
michael@0 | 396 | mWaiting = false; |
michael@0 | 397 | /* We have to wake up the manager thread because when all threads |
michael@0 | 398 | are waiting, the manager thread waits indefinitely as well. */ |
michael@0 | 399 | mManager->Wakeup(); |
michael@0 | 400 | } else { |
michael@0 | 401 | PRIntervalTime duration = intervalNow - mInterval; |
michael@0 | 402 | mStats.mActivity.Add(duration); |
michael@0 | 403 | if (MOZ_UNLIKELY(duration >= mTimeout)) { |
michael@0 | 404 | /* Wake up the manager thread to tell it that a hang ended */ |
michael@0 | 405 | mManager->Wakeup(); |
michael@0 | 406 | } |
michael@0 | 407 | mInterval = intervalNow; |
michael@0 | 408 | } |
michael@0 | 409 | } |
michael@0 | 410 | |
michael@0 | 411 | BackgroundHangThread* |
michael@0 | 412 | BackgroundHangThread::FindThread() |
michael@0 | 413 | { |
michael@0 | 414 | #ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR |
michael@0 | 415 | if (sTlsKey.initialized()) { |
michael@0 | 416 | // Use TLS if available |
michael@0 | 417 | return sTlsKey.get(); |
michael@0 | 418 | } |
michael@0 | 419 | // If TLS is unavailable, we can search through the thread list |
michael@0 | 420 | RefPtr<BackgroundHangManager> manager(BackgroundHangManager::sInstance); |
michael@0 | 421 | MOZ_ASSERT(manager, "Creating BackgroundHangMonitor after shutdown"); |
michael@0 | 422 | |
michael@0 | 423 | PRThread* threadID = PR_GetCurrentThread(); |
michael@0 | 424 | // Lock thread list for traversal |
michael@0 | 425 | MonitorAutoLock autoLock(manager->mLock); |
michael@0 | 426 | for (BackgroundHangThread* thread = manager->mHangThreads.getFirst(); |
michael@0 | 427 | thread; thread = thread->getNext()) { |
michael@0 | 428 | if (thread->mThreadID == threadID) { |
michael@0 | 429 | return thread; |
michael@0 | 430 | } |
michael@0 | 431 | } |
michael@0 | 432 | #endif |
michael@0 | 433 | // Current thread is not initialized |
michael@0 | 434 | return nullptr; |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | |
michael@0 | 438 | void |
michael@0 | 439 | BackgroundHangMonitor::Startup() |
michael@0 | 440 | { |
michael@0 | 441 | #ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR |
michael@0 | 442 | MOZ_ASSERT(!BackgroundHangManager::sInstance, "Already initialized"); |
michael@0 | 443 | ThreadStackHelper::Startup(); |
michael@0 | 444 | BackgroundHangThread::Startup(); |
michael@0 | 445 | BackgroundHangManager::sInstance = new BackgroundHangManager(); |
michael@0 | 446 | #endif |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | void |
michael@0 | 450 | BackgroundHangMonitor::Shutdown() |
michael@0 | 451 | { |
michael@0 | 452 | #ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR |
michael@0 | 453 | MOZ_ASSERT(BackgroundHangManager::sInstance, "Not initialized"); |
michael@0 | 454 | /* Scope our lock inside Shutdown() because the sInstance object can |
michael@0 | 455 | be destroyed as soon as we set sInstance to nullptr below, and |
michael@0 | 456 | we don't want to hold the lock when it's being destroyed. */ |
michael@0 | 457 | BackgroundHangManager::sInstance->Shutdown(); |
michael@0 | 458 | BackgroundHangManager::sInstance = nullptr; |
michael@0 | 459 | ThreadStackHelper::Shutdown(); |
michael@0 | 460 | #endif |
michael@0 | 461 | } |
michael@0 | 462 | |
michael@0 | 463 | BackgroundHangMonitor::BackgroundHangMonitor(const char* aName, |
michael@0 | 464 | uint32_t aTimeoutMs, |
michael@0 | 465 | uint32_t aMaxTimeoutMs) |
michael@0 | 466 | : mThread(BackgroundHangThread::FindThread()) |
michael@0 | 467 | { |
michael@0 | 468 | #ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR |
michael@0 | 469 | if (!mThread) { |
michael@0 | 470 | mThread = new BackgroundHangThread(aName, aTimeoutMs, aMaxTimeoutMs); |
michael@0 | 471 | } |
michael@0 | 472 | #endif |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | BackgroundHangMonitor::BackgroundHangMonitor() |
michael@0 | 476 | : mThread(BackgroundHangThread::FindThread()) |
michael@0 | 477 | { |
michael@0 | 478 | #ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR |
michael@0 | 479 | MOZ_ASSERT(mThread, "Thread not initialized for hang monitoring"); |
michael@0 | 480 | #endif |
michael@0 | 481 | } |
michael@0 | 482 | |
michael@0 | 483 | BackgroundHangMonitor::~BackgroundHangMonitor() |
michael@0 | 484 | { |
michael@0 | 485 | } |
michael@0 | 486 | |
michael@0 | 487 | void |
michael@0 | 488 | BackgroundHangMonitor::NotifyActivity() |
michael@0 | 489 | { |
michael@0 | 490 | #ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR |
michael@0 | 491 | mThread->NotifyActivity(); |
michael@0 | 492 | #endif |
michael@0 | 493 | } |
michael@0 | 494 | |
michael@0 | 495 | void |
michael@0 | 496 | BackgroundHangMonitor::NotifyWait() |
michael@0 | 497 | { |
michael@0 | 498 | #ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR |
michael@0 | 499 | mThread->NotifyWait(); |
michael@0 | 500 | #endif |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | |
michael@0 | 504 | /* Because we are iterating through the BackgroundHangThread linked list, |
michael@0 | 505 | we need to take a lock. Using MonitorAutoLock as a base class makes |
michael@0 | 506 | sure all of that is taken care of for us. */ |
michael@0 | 507 | BackgroundHangMonitor::ThreadHangStatsIterator::ThreadHangStatsIterator() |
michael@0 | 508 | : MonitorAutoLock(BackgroundHangManager::sInstance->mLock) |
michael@0 | 509 | , mThread(BackgroundHangManager::sInstance->mHangThreads.getFirst()) |
michael@0 | 510 | { |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | Telemetry::ThreadHangStats* |
michael@0 | 514 | BackgroundHangMonitor::ThreadHangStatsIterator::GetNext() |
michael@0 | 515 | { |
michael@0 | 516 | if (!mThread) { |
michael@0 | 517 | return nullptr; |
michael@0 | 518 | } |
michael@0 | 519 | Telemetry::ThreadHangStats* stats = &mThread->mStats; |
michael@0 | 520 | mThread = mThread->getNext(); |
michael@0 | 521 | return stats; |
michael@0 | 522 | } |
michael@0 | 523 | |
michael@0 | 524 | } // namespace mozilla |