summaryrefslogtreecommitdiffstats
path: root/toolkit/components/backgroundhangmonitor/BackgroundHangMonitor.cpp
blob: 5b032dfd39f8342394c025087bca1ea9d05561a2 (plain)
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/BackgroundHangMonitor.h"

#include <string_view>
#include <utility>

#include "GeckoProfiler.h"
#include "HangDetails.h"
#include "ThreadStackHelper.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/CPUUsageWatcher.h"
#include "mozilla/LinkedList.h"
#include "mozilla/Monitor.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_toolkit.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Telemetry.h"
#include "mozilla/ThreadLocal.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/RemoteType.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsIThread.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "prinrval.h"
#include "prthread.h"

#include <algorithm>

// Activate BHR only for one every BHR_BETA_MOD users.
// We're doing experimentation with collecting a lot more data from BHR, and
// don't want to enable it for beta users at the moment. We can scale this up in
// the future.
#define BHR_BETA_MOD INT32_MAX;

// Interval at which we check the global and per-process CPU usage in order to
// determine if there is high external CPU usage.
static const int32_t kCheckCPUIntervalMilliseconds = 2000;

// An utility comparator function used by std::unique to collapse "(* script)"
// entries in a vector representing a call stack.
bool StackScriptEntriesCollapser(const char* aStackEntry,
                                 const char* aAnotherStackEntry) {
  return !strcmp(aStackEntry, aAnotherStackEntry) &&
         (!strcmp(aStackEntry, "(chrome script)") ||
          !strcmp(aStackEntry, "(content script)"));
}

namespace mozilla {

/**
 * BackgroundHangManager is the global object that
 * manages all instances of BackgroundHangThread.
 */
class BackgroundHangManager : public nsIObserver {
 private:
  // Stop hang monitoring
  bool mShutdown;

  BackgroundHangManager(const BackgroundHangManager&);
  BackgroundHangManager& operator=(const BackgroundHangManager&);

 public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSIOBSERVER
  static StaticRefPtr<BackgroundHangManager> sInstance;
  static bool sDisabled;

  // Lock for access to members of this class
  Monitor mLock MOZ_UNANNOTATED;
  // List of BackgroundHangThread instances associated with each thread
  LinkedList<BackgroundHangThread> mHangThreads;

  // Unwinding and reporting of hangs is despatched to this thread.
  nsCOMPtr<nsIThread> mHangProcessingThread;

  // Hang monitor thread
  nsCOMPtr<nsIThread> mHangMonitorThread;

  ProfilerThreadId mHangMonitorProfilerThreadId;

  void SetMonitorThreadId() {
    mHangMonitorProfilerThreadId = profiler_current_thread_id();
  }

  // Used for recording a permahang in case we don't ever make it back to
  // the main thread to record/send it.
  nsCOMPtr<nsIFile> mPermahangFile;

  // Allows us to watch CPU usage and annotate hangs when the system is
  // under high external load.
  CPUUsageWatcher mCPUUsageWatcher;

  TimeStamp mLastCheckedCPUUsage;

  void CollectCPUUsage(TimeStamp aNow, bool aForce = false) {
    if (aForce ||
        aNow - mLastCheckedCPUUsage >
            TimeDuration::FromMilliseconds(kCheckCPUIntervalMilliseconds)) {
      Unused << NS_WARN_IF(mCPUUsageWatcher.CollectCPUUsage().isErr());
      mLastCheckedCPUUsage = aNow;
    }
  }

  void Shutdown() {
    MonitorAutoLock autoLock(mLock);
    mShutdown = true;
  }

  BackgroundHangManager();

 private:
  virtual ~BackgroundHangManager();
};

NS_IMPL_ISUPPORTS(BackgroundHangManager, nsIObserver)

NS_IMETHODIMP
BackgroundHangManager::Observe(nsISupports* aSubject, const char* aTopic,
                               const char16_t* aData) {
  if (!strcmp(aTopic, "browser-delayed-startup-finished")) {
    MonitorAutoLock autoLock(mLock);
    nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
                                         getter_AddRefs(mPermahangFile));
    if (NS_SUCCEEDED(rv)) {
      mPermahangFile->AppendNative("last_permahang.bin"_ns);
    } else {
      mPermahangFile = nullptr;
    }

    if (mHangProcessingThread && mPermahangFile) {
      nsCOMPtr<nsIRunnable> submitRunnable =
          new SubmitPersistedPermahangRunnable(mPermahangFile);
      mHangProcessingThread->Dispatch(submitRunnable.forget());
    }
    nsCOMPtr<nsIObserverService> observerService =
        mozilla::services::GetObserverService();
    MOZ_ASSERT(observerService);
    observerService->RemoveObserver(BackgroundHangManager::sInstance,
                                    "browser-delayed-startup-finished");
  } else if (!strcmp(aTopic, "profile-after-change")) {
    BackgroundHangMonitor::DisableOnBeta();
    nsCOMPtr<nsIObserverService> observerService =
        mozilla::services::GetObserverService();
    MOZ_ASSERT(observerService);
    observerService->RemoveObserver(BackgroundHangManager::sInstance,
                                    "profile-after-change");
  } else {
    return NS_ERROR_UNEXPECTED;
  }

  return NS_OK;
}

/**
 * BackgroundHangThread is a per-thread object that is used
 * by all instances of BackgroundHangMonitor to monitor hangs.
 */
class BackgroundHangThread final
    : public LinkedListElement<BackgroundHangThread>,
      public nsITimerCallback,
      public nsINamed {
 private:
  static MOZ_THREAD_LOCAL(BackgroundHangThread*) sTlsKey;
  static bool sTlsKeyInitialized;

  BackgroundHangThread(const BackgroundHangThread&);
  BackgroundHangThread& operator=(const BackgroundHangThread&);
  ~BackgroundHangThread();

  /* Keep a reference to the manager, so we can keep going even
     after BackgroundHangManager::Shutdown is called. */
  const RefPtr<BackgroundHangManager> mManager;
  // Unique thread ID for identification
  const PRThread* mThreadID;
  RefPtr<nsITimer> mTimer;
  TimeStamp mExpectedTimerNotification;

 public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSITIMERCALLBACK
  NS_DECL_NSINAMED

  /**
   * Returns the BackgroundHangThread associated with the
   * running thread. Note that this will not find private
   * BackgroundHangThread threads.
   *
   * @return BackgroundHangThread*, or nullptr if no thread
   *         is found.
   */
  static BackgroundHangThread* FindThread();

  static void Startup() {
    /* We can tolerate init() failing. */
    sTlsKeyInitialized = sTlsKey.init();
  }

  // Hang timeout
  const TimeDuration mTimeout;
  // PermaHang timeout
  const TimeDuration mMaxTimeout;
  // Time at last activity
  TimeStamp mLastActivity;
  // Time when a hang started
  TimeStamp mHangStart;
  // Is the thread in a hang
  bool mHanging;
  // Is the thread in a waiting state
  bool mWaiting;
  // Is the thread dedicated to a single BackgroundHangMonitor
  BackgroundHangMonitor::ThreadType mThreadType;
#ifdef MOZ_GECKO_PROFILER
  // Platform-specific helper to get hang stacks
  ThreadStackHelper mStackHelper;
#endif
  // Stack of current hang
  HangStack mHangStack;
  // Annotations for the current hang
  BackgroundHangAnnotations mAnnotations;
  // Annotators registered for this thread
  BackgroundHangAnnotators mAnnotators;
  // The name of the runnable which is hanging the current process
  nsCString mRunnableName;
  // The name of the thread which is being monitored
  nsCString mThreadName;

  BackgroundHangThread(const char* aName, uint32_t aTimeoutMs,
                       uint32_t aMaxTimeoutMs,
                       BackgroundHangMonitor::ThreadType aThreadType =
                           BackgroundHangMonitor::THREAD_SHARED);

  // Report a hang; aManager->mLock IS locked. The hang will be processed
  // off-main-thread, and will then be submitted back.
  void ReportHang(TimeDuration aHangTime,
                  PersistedToDisk aPersistedToDisk = PersistedToDisk::No);
  // Report a permanent hang; aManager->mLock IS locked
  void ReportPermaHang();
  // Called by BackgroundHangMonitor::NotifyActivity
  void NotifyActivity() {
    if (MOZ_UNLIKELY(!mTimer)) {
      return;
    }

    MonitorAutoLock autoLock(mManager->mLock);
    PROFILER_MARKER_UNTYPED(
        "NotifyActivity", OTHER,
        MarkerThreadId(mManager->mHangMonitorProfilerThreadId));

    TimeStamp now = TimeStamp::Now();
    if (mWaiting) {
      mWaiting = false;
    } else if (mHanging) {
      // A hang ended.
      ReportHang(now - mHangStart);
      mHanging = false;
    }
    mLastActivity = now;
    BackgroundHangManager::sInstance->CollectCPUUsage(now);

    // Set or reset the timer.
    mExpectedTimerNotification = now + mTimeout;
    if (mTimeout != TimeDuration::Forever()) {
      mTimer->InitHighResolutionWithCallback(this, mTimeout,
                                             nsITimer::TYPE_ONE_SHOT);
    }
  }
  // Called by BackgroundHangMonitor::NotifyWait
  void NotifyWait() {
    if (MOZ_UNLIKELY(!mTimer)) {
      return;
    }

    MonitorAutoLock autoLock(mManager->mLock);
    PROFILER_MARKER_UNTYPED(
        "NotifyWait", OTHER,
        MarkerThreadId(mManager->mHangMonitorProfilerThreadId));

    if (mWaiting) {
      return;
    }

    mTimer->Cancel();

    mLastActivity = TimeStamp::Now();

    if (mHanging) {
      // We were hanging! We're done with that now, so let's report it.
      // ReportHang() doesn't do much work on the current thread, and is
      // safe to call from any thread as long as we're holding the lock.
      ReportHang(mLastActivity - mHangStart);
      mHanging = false;
    }
    mWaiting = true;
  }

  // Returns true if this thread is (or might be) shared between other
  // BackgroundHangMonitors for the monitored thread.
  bool IsShared() {
    return mThreadType == BackgroundHangMonitor::THREAD_SHARED;
  }
};

NS_IMPL_ISUPPORTS(BackgroundHangThread, nsITimerCallback, nsINamed)

NS_IMETHODIMP
BackgroundHangThread::GetName(nsACString& aName) {
  aName.AssignLiteral("BackgroundHangThread_timer");
  return NS_OK;
}

StaticRefPtr<BackgroundHangManager> BackgroundHangManager::sInstance;
bool BackgroundHangManager::sDisabled = false;

MOZ_THREAD_LOCAL(BackgroundHangThread*) BackgroundHangThread::sTlsKey;
bool BackgroundHangThread::sTlsKeyInitialized;

BackgroundHangManager::BackgroundHangManager()
    : mShutdown(false), mLock("BackgroundHangManager") {
  // Save a reference to sInstance now so that the destructor is not triggered
  // if the SetMonitorThreadId RunnableMethod is released before we are done.
  sInstance = this;

  DebugOnly<nsresult> rv =
      NS_NewNamedThread("BHMgr Monitor", getter_AddRefs(mHangMonitorThread),
                        mozilla::NewRunnableMethod(
                            "BackgroundHangManager::SetMonitorThreadId", this,
                            &BackgroundHangManager::SetMonitorThreadId));

  MOZ_ASSERT(NS_SUCCEEDED(rv) && mHangMonitorThread,
             "Failed to create BHR processing thread");

  rv = NS_NewNamedThread("BHMgr Processor",
                         getter_AddRefs(mHangProcessingThread));
  MOZ_ASSERT(NS_SUCCEEDED(rv) && mHangProcessingThread,
             "Failed to create BHR processing thread");
}

BackgroundHangManager::~BackgroundHangManager() {
  MOZ_ASSERT(mShutdown, "Destruction without Shutdown call");
  MOZ_ASSERT(mHangThreads.isEmpty(), "Destruction with outstanding monitors");
  MOZ_ASSERT(mHangMonitorThread, "No monitor thread");
  MOZ_ASSERT(mHangProcessingThread, "No processing thread");

  // NS_NewNamedThread could have failed above due to resource limitation
  if (mHangMonitorThread) {
    // The monitor thread can only live as long as the instance lives
    mHangMonitorThread->Shutdown();
  }

  // Similarly, NS_NewNamedThread above could have failed.
  if (mHangProcessingThread) {
    mHangProcessingThread->Shutdown();
  }
}

BackgroundHangThread::BackgroundHangThread(
    const char* aName, uint32_t aTimeoutMs, uint32_t aMaxTimeoutMs,
    BackgroundHangMonitor::ThreadType aThreadType)
    : mManager(BackgroundHangManager::sInstance),
      mThreadID(PR_GetCurrentThread()),
      mTimeout(aTimeoutMs == BackgroundHangMonitor::kNoTimeout
                   ? TimeDuration::Forever()
                   : TimeDuration::FromMilliseconds(aTimeoutMs)),
      mMaxTimeout(aMaxTimeoutMs == BackgroundHangMonitor::kNoTimeout
                      ? TimeDuration::Forever()
                      : TimeDuration::FromMilliseconds(aMaxTimeoutMs)),
      mHanging(false),
      mWaiting(true),
      mThreadType(aThreadType),
      mThreadName(aName) {
  if (sTlsKeyInitialized && IsShared()) {
    sTlsKey.set(this);
  }
  if (mManager->mHangMonitorThread) {
    mTimer = NS_NewTimer(mManager->mHangMonitorThread);
  }
  // Lock here because LinkedList is not thread-safe
  MonitorAutoLock autoLock(mManager->mLock);
  // Add to thread list
  mManager->mHangThreads.insertBack(this);
}

BackgroundHangThread::~BackgroundHangThread() {
  // Lock here because LinkedList is not thread-safe
  MonitorAutoLock autoLock(mManager->mLock);
  // Remove from thread list
  remove();

  // We no longer have a thread
  if (sTlsKeyInitialized && IsShared()) {
    sTlsKey.set(nullptr);
  }
}

void BackgroundHangThread::ReportHang(TimeDuration aHangTime,
                                      PersistedToDisk aPersistedToDisk) {
  // Recovered from a hang; called on the monitor thread
  // mManager->mLock IS locked

  HangDetails hangDetails(aHangTime,
                          nsDependentCString(XRE_GetProcessTypeString()),
                          NOT_REMOTE_TYPE, mThreadName, mRunnableName,
                          std::move(mHangStack), std::move(mAnnotations));

  PersistedToDisk persistedToDisk = aPersistedToDisk;
  if (aPersistedToDisk == PersistedToDisk::Yes && XRE_IsParentProcess() &&
      mManager->mPermahangFile) {
    auto res = WriteHangDetailsToFile(hangDetails, mManager->mPermahangFile);
    persistedToDisk = res.isOk() ? PersistedToDisk::Yes : PersistedToDisk::No;
  }

  // If the hang processing thread exists, we can process the native stack
  // on it. Otherwise, we are unable to report a native stack, so we just
  // report without one.
  if (mManager->mHangProcessingThread) {
    nsCOMPtr<nsIRunnable> processHangStackRunnable =
        new ProcessHangStackRunnable(std::move(hangDetails), persistedToDisk);
    mManager->mHangProcessingThread->Dispatch(
        processHangStackRunnable.forget());
  } else {
    NS_WARNING("Unable to report native stack without a BHR processing thread");
    RefPtr<nsHangDetails> hd =
        new nsHangDetails(std::move(hangDetails), persistedToDisk);
    hd->Submit();
  }

  // If the profiler is enabled, add a marker.
#ifdef MOZ_GECKO_PROFILER
  if (profiler_thread_is_being_profiled_for_markers(
          mStackHelper.GetThreadId())) {
    struct HangMarker {
      static constexpr Span<const char> MarkerTypeName() {
        return MakeStringSpan("BHR-detected hang");
      }
      static void StreamJSONMarkerData(
          baseprofiler::SpliceableJSONWriter& aWriter) {}
      static MarkerSchema MarkerTypeDisplay() {
        using MS = MarkerSchema;
        MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
        return schema;
      }
    };

    const TimeStamp endTime = TimeStamp::Now();
    const TimeStamp startTime = endTime - aHangTime;
    profiler_add_marker("BHR-detected hang", geckoprofiler::category::OTHER,
                        {MarkerThreadId(mStackHelper.GetThreadId()),
                         MarkerTiming::Interval(startTime, endTime)},
                        HangMarker{});
  }
#endif
}

void BackgroundHangThread::ReportPermaHang() {
  // Permanently hanged; called on the monitor thread
  // mManager->mLock IS locked

  // The significance of a permahang is that it's likely that we won't ever
  // recover and be allowed to submit this hang. On the parent thread, we
  // compensate for this by writing the hang details to disk on this thread,
  // and in our next session we'll try to read those details
  ReportHang(mMaxTimeout, PersistedToDisk::Yes);
}

NS_IMETHODIMP BackgroundHangThread::Notify(nsITimer* aTimer) {
  MOZ_ASSERT(profiler_current_thread_id() ==
             mManager->mHangMonitorProfilerThreadId);

  MonitorAutoLock autoLock(mManager->mLock);
  PROFILER_MARKER_UNTYPED("TimerNotify", OTHER, {});

  TimeStamp now = TimeStamp::Now();
  if (MOZ_UNLIKELY((now - mExpectedTimerNotification) * 2 > mTimeout)) {
    // If the timer notification has been delayed by more than half the timeout
    // time, assume the machine is not scheduling tasks correctly and ignore
    // this hang.
    mWaiting = true;
    mHanging = false;
    return NS_OK;
  }

  TimeDuration hangTime = now - mLastActivity;
  if (MOZ_UNLIKELY(hangTime >= mMaxTimeout)) {
    // A permahang started.  No point in trying to find its exact
    // duration, so avoid restarting the timer until there is new
    // activity.
    mWaiting = true;
    mHanging = false;
    ReportPermaHang();
    return NS_OK;
  }

  if (MOZ_LIKELY(!mHanging && hangTime >= mTimeout)) {
#ifdef MOZ_GECKO_PROFILER
    // A hang started, collect a stack
    mStackHelper.GetStack(mHangStack, mRunnableName, true);
#endif

    // If we hang immediately on waking, then the most recently collected
    // CPU usage is going to be an average across the whole time we were
    // sleeping. Accordingly, we want to make sure that when we hang, we
    // collect a fresh value.
    BackgroundHangManager::sInstance->CollectCPUUsage(now, true);

    mHangStart = mLastActivity;
    mHanging = true;
    mAnnotations = mAnnotators.GatherAnnotations();
  }

  TimeDuration nextRecheck = mMaxTimeout - hangTime;
  mExpectedTimerNotification = now + nextRecheck;
  return mTimer->InitHighResolutionWithCallback(this, nextRecheck,
                                                nsITimer::TYPE_ONE_SHOT);
}

BackgroundHangThread* BackgroundHangThread::FindThread() {
#ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR
  if (BackgroundHangManager::sInstance == nullptr) {
    MOZ_ASSERT(BackgroundHangManager::sDisabled,
               "BackgroundHandleManager is not initialized");
    return nullptr;
  }

  if (sTlsKeyInitialized) {
    // Use TLS if available
    return sTlsKey.get();
  }
  // If TLS is unavailable, we can search through the thread list
  RefPtr<BackgroundHangManager> manager(BackgroundHangManager::sInstance);
  MOZ_ASSERT(manager, "Creating BackgroundHangMonitor after shutdown");

  PRThread* threadID = PR_GetCurrentThread();
  // Lock thread list for traversal
  MonitorAutoLock autoLock(manager->mLock);
  for (BackgroundHangThread* thread = manager->mHangThreads.getFirst(); thread;
       thread = thread->getNext()) {
    if (thread->mThreadID == threadID && thread->IsShared()) {
      return thread;
    }
  }
#endif
  // Current thread is not initialized
  return nullptr;
}

bool BackgroundHangMonitor::ShouldDisableOnBeta(const nsCString& clientID) {
  MOZ_ASSERT(clientID.Length() == 36, "clientID is invalid");
  const char* suffix = clientID.get() + clientID.Length() - 4;
  return strtol(suffix, NULL, 16) % BHR_BETA_MOD;
}

bool BackgroundHangMonitor::DisableOnBeta() {
  nsAutoCString clientID;
  nsresult rv =
      Preferences::GetCString("toolkit.telemetry.cachedClientID", clientID);
  bool telemetryEnabled = Telemetry::CanRecordPrereleaseData();

  if (!telemetryEnabled || NS_FAILED(rv) ||
      BackgroundHangMonitor::ShouldDisableOnBeta(clientID)) {
    if (XRE_IsParentProcess()) {
      BackgroundHangMonitor::Shutdown();
    } else {
      BackgroundHangManager::sDisabled = true;
    }
    return true;
  }

  return false;
}

void BackgroundHangMonitor::Startup() {
  MOZ_RELEASE_ASSERT(NS_IsMainThread());
#ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR
  MOZ_ASSERT(!BackgroundHangManager::sInstance, "Already initialized");

  if (XRE_IsContentProcess() &&
      StaticPrefs::toolkit_content_background_hang_monitor_disabled()) {
    BackgroundHangManager::sDisabled = true;
    return;
  }

  nsCOMPtr<nsIObserverService> observerService =
      mozilla::services::GetObserverService();
  MOZ_ASSERT(observerService);

#  ifdef __clang__
#    pragma clang diagnostic push
#    pragma clang diagnostic ignored "-Wunreachable-code"
#  endif
  if constexpr (std::string_view(MOZ_STRINGIFY(MOZ_UPDATE_CHANNEL)) == "beta") {
    if (XRE_IsParentProcess()) {  // cached ClientID hasn't been read yet
      BackgroundHangThread::Startup();
      new BackgroundHangManager();
      Unused << NS_WARN_IF(
          BackgroundHangManager::sInstance->mCPUUsageWatcher.Init().isErr());
      observerService->AddObserver(BackgroundHangManager::sInstance,
                                   "profile-after-change", false);
      return;
    } else if (DisableOnBeta()) {
      return;
    }
  }
#  ifdef __clang__
#    pragma clang diagnostic pop
#  endif

  BackgroundHangThread::Startup();
  new BackgroundHangManager();
  Unused << NS_WARN_IF(
      BackgroundHangManager::sInstance->mCPUUsageWatcher.Init().isErr());
  if (XRE_IsParentProcess()) {
    observerService->AddObserver(BackgroundHangManager::sInstance,
                                 "browser-delayed-startup-finished", false);
  }
#endif
}

void BackgroundHangMonitor::Shutdown() {
#ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR
  if (BackgroundHangManager::sDisabled) {
    MOZ_ASSERT(!BackgroundHangManager::sInstance, "Initialized");
    return;
  }

  MOZ_ASSERT(BackgroundHangManager::sInstance, "Not initialized");
  BackgroundHangManager::sInstance->mCPUUsageWatcher.Uninit();
  /* Scope our lock inside Shutdown() because the sInstance object can
     be destroyed as soon as we set sInstance to nullptr below, and
     we don't want to hold the lock when it's being destroyed. */
  BackgroundHangManager::sInstance->Shutdown();
  BackgroundHangManager::sInstance = nullptr;
  BackgroundHangManager::sDisabled = true;
#endif
}

BackgroundHangMonitor::BackgroundHangMonitor(const char* aName,
                                             uint32_t aTimeoutMs,
                                             uint32_t aMaxTimeoutMs,
                                             ThreadType aThreadType)
    : mThread(aThreadType == THREAD_SHARED ? BackgroundHangThread::FindThread()
                                           : nullptr) {
#ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR
#  ifdef MOZ_VALGRIND
  // If we're running on Valgrind, we'll be making forward progress at a
  // rate of somewhere between 1/25th and 1/50th of normal.  This causes the
  // BHR to capture a lot of stacks, which slows us down even more.  As an
  // attempt to avoid the worst of this, scale up all presented timeouts by
  // a factor of thirty, and add six seconds so as to impose a six second
  // floor on all timeouts.  For a non-Valgrind-enabled build, or for an
  // enabled build which isn't running on Valgrind, the timeouts are
  // unchanged.
  if (RUNNING_ON_VALGRIND) {
    const uint32_t scaleUp = 30;
    const uint32_t extraMs = 6000;
    if (aTimeoutMs != BackgroundHangMonitor::kNoTimeout) {
      aTimeoutMs *= scaleUp;
      aTimeoutMs += extraMs;
    }
    if (aMaxTimeoutMs != BackgroundHangMonitor::kNoTimeout) {
      aMaxTimeoutMs *= scaleUp;
      aMaxTimeoutMs += extraMs;
    }
  }
#  endif

  if (!BackgroundHangManager::sDisabled && !mThread) {
    mThread =
        new BackgroundHangThread(aName, aTimeoutMs, aMaxTimeoutMs, aThreadType);
  }
#endif
}

BackgroundHangMonitor::BackgroundHangMonitor()
    : mThread(BackgroundHangThread::FindThread()) {
#ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR
  if (BackgroundHangManager::sDisabled) {
    return;
  }
#endif
}

BackgroundHangMonitor::~BackgroundHangMonitor() = default;

void BackgroundHangMonitor::NotifyActivity() {
#ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR
  if (mThread == nullptr) {
    MOZ_ASSERT(BackgroundHangManager::sDisabled,
               "This thread is not initialized for hang monitoring");
    return;
  }

  if (Telemetry::CanRecordExtended()) {
    mThread->NotifyActivity();
  }
#endif
}

void BackgroundHangMonitor::NotifyWait() {
#ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR
  if (mThread == nullptr) {
    MOZ_ASSERT(BackgroundHangManager::sDisabled,
               "This thread is not initialized for hang monitoring");
    return;
  }

  if (Telemetry::CanRecordExtended()) {
    mThread->NotifyWait();
  }
#endif
}

bool BackgroundHangMonitor::RegisterAnnotator(
    BackgroundHangAnnotator& aAnnotator) {
#ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR
  BackgroundHangThread* thisThread = BackgroundHangThread::FindThread();
  if (!thisThread) {
    return false;
  }
  return thisThread->mAnnotators.Register(aAnnotator);
#else
  return false;
#endif
}

bool BackgroundHangMonitor::UnregisterAnnotator(
    BackgroundHangAnnotator& aAnnotator) {
#ifdef MOZ_ENABLE_BACKGROUND_HANG_MONITOR
  BackgroundHangThread* thisThread = BackgroundHangThread::FindThread();
  if (!thisThread) {
    return false;
  }
  return thisThread->mAnnotators.Unregister(aAnnotator);
#else
  return false;
#endif
}

}  // namespace mozilla