Bug 1700051: part 49) Add some documentation to `Selection::GetRangesForInterval...
[gecko.git] / xpcom / threads / CPUUsageWatcher.h
blobc3a643378a4ccf8bb9129de0c25467811082c74e
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
10 #include <stdint.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
20 #endif
22 namespace mozilla {
24 // Start error values at 1 to allow using the UnusedZero Result
25 // optimization.
26 enum CPUUsageWatcherError : uint8_t {
27 ClockGetTimeError = 1,
28 GetNumberOfProcessorsError,
29 GetProcessTimesError,
30 GetSystemTimesError,
31 HostStatisticsError,
32 ProcStatError,
35 namespace detail {
37 template <>
38 struct UnusedZero<CPUUsageWatcherError> : UnusedZeroEnum<CPUUsageWatcherError> {
41 } // namespace detail
43 class CPUUsageHangAnnotator : public BackgroundHangAnnotator {
44 public:
47 class CPUUsageWatcher : public BackgroundHangAnnotator {
48 public:
49 #ifdef CPU_USAGE_WATCHER_ACTIVE
50 CPUUsageWatcher()
51 : mInitialized(false),
52 mExternalUsageThreshold(0),
53 mExternalUsageRatio(0),
54 mProcessUsageTime(0),
55 mProcessUpdateTime(0),
56 mGlobalUsageTime(0),
57 mGlobalUpdateTime(0),
58 mNumCPUs(0) {}
59 #endif
61 Result<Ok, CPUUsageWatcherError> Init();
63 void Uninit();
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;
72 private:
73 #ifdef CPU_USAGE_WATCHER_ACTIVE
74 bool mInitialized;
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
94 uint64_t mNumCPUs;
95 #endif
98 } // namespace mozilla
100 #endif // mozilla_CPUUsageWatcher_h