1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_CPUUsageWatcher_h
8 #define mozilla_CPUUsageWatcher_h
12 #include "mozilla/HangAnnotations.h"
13 #include "mozilla/Result.h"
15 // We only support OSX and Windows, because on Linux we're forced to read
16 // from /proc/stat in order to get global CPU values. We would prefer to not
17 // eat that cost for this.
18 #if defined(NIGHTLY_BUILD) && (defined(XP_WIN) || defined(XP_MACOSX))
19 # define CPU_USAGE_WATCHER_ACTIVE
24 // Start error values at 1 to allow using the UnusedZero Result
26 enum CPUUsageWatcherError
: uint8_t {
27 ClockGetTimeError
= 1,
28 GetNumberOfProcessorsError
,
38 struct UnusedZero
<CPUUsageWatcherError
> : UnusedZeroEnum
<CPUUsageWatcherError
> {
43 class CPUUsageHangAnnotator
: public BackgroundHangAnnotator
{
47 class CPUUsageWatcher
: public BackgroundHangAnnotator
{
49 #ifdef CPU_USAGE_WATCHER_ACTIVE
51 : mInitialized(false),
52 mExternalUsageThreshold(0),
53 mExternalUsageRatio(0),
55 mProcessUpdateTime(0),
61 Result
<Ok
, CPUUsageWatcherError
> Init();
65 // Updates necessary values to allow AnnotateHang to function. This must be
66 // called on some semi-regular basis, as it will calculate the mean CPU
67 // usage values between now and the last time it was called.
68 Result
<Ok
, CPUUsageWatcherError
> CollectCPUUsage();
70 void AnnotateHang(BackgroundHangAnnotations
& aAnnotations
) final
;
73 #ifdef CPU_USAGE_WATCHER_ACTIVE
75 // The threshold above which we will mark a hang as occurring under high
76 // external CPU usage conditions
77 float mExternalUsageThreshold
;
78 // The CPU usage (0-1) external to our process, averaged between the two
79 // most recent monitor thread runs
80 float mExternalUsageRatio
;
81 // The total cumulative CPU usage time by our process as of the last
82 // CollectCPUUsage or Startup
83 uint64_t mProcessUsageTime
;
84 // A time value in the same units as mProcessUsageTime used to
85 // determine the ratio of CPU usage time to idle time
86 uint64_t mProcessUpdateTime
;
87 // The total cumulative CPU usage time by all processes as of the last
88 // CollectCPUUsage or Startup
89 uint64_t mGlobalUsageTime
;
90 // A time value in the same units as mGlobalUsageTime used to
91 // determine the ratio of CPU usage time to idle time
92 uint64_t mGlobalUpdateTime
;
93 // The number of virtual cores on our machine
98 } // namespace mozilla
100 #endif // mozilla_CPUUsageWatcher_h