Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / base / MemoryTelemetry.h
blobb7c7fe8ad69de67f502b918bdb0ff729d6d433e4
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_MemoryTelemetry_h
8 #define mozilla_MemoryTelemetry_h
10 #include "mozilla/TimeStamp.h"
11 #include "mozilla/Result.h"
12 #include "nsIObserver.h"
13 #include "nsITimer.h"
14 #include "nsTArray.h"
15 #include "nsWeakReference.h"
17 #include <functional>
19 class nsIEventTarget;
21 namespace mozilla {
23 namespace ipc {
24 enum class ResponseRejectReason;
27 /**
28 * Periodically gathers memory usage metrics after cycle collection, and
29 * populates telemetry histograms with their values.
31 class MemoryTelemetry final : public nsIObserver,
32 public nsSupportsWeakReference {
33 public:
34 NS_DECL_ISUPPORTS
35 NS_DECL_NSIOBSERVER
37 static MemoryTelemetry& Get();
39 nsresult GatherReports(
40 const std::function<void()>& aCompletionCallback = nullptr);
42 /**
43 * Does expensive initialization, which should happen only after startup has
44 * completed, and the event loop is idle.
46 nsresult DelayedInit();
48 nsresult Shutdown();
50 private:
51 MemoryTelemetry();
53 ~MemoryTelemetry() = default;
55 void Init();
57 static Result<uint32_t, nsresult> GetOpenTabsCount();
59 void GatherTotalMemory();
60 nsresult FinishGatheringTotalMemory(int64_t aTotalMemory,
61 const nsTArray<int64_t>& aChildSizes);
63 nsCOMPtr<nsIEventTarget> mThreadPool;
65 bool mGatheringTotalMemory = false;
67 TimeStamp mLastPoll{};
70 } // namespace mozilla
72 #endif // defined mozilla_MemoryTelemetry_h