Bug 1686838 [wpt PR 27194] - [webcodecs] Deprecate VideoFrame.destroy()., a=testonly
[gecko.git] / xpcom / base / MemoryTelemetry.h
blob48a5ceccbca0bd2e9486c1cbd5a1d03df314e173
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 void GetUniqueSetSize(std::function<void(const int64_t&)>&& aCallback);
44 /**
45 * Does expensive initialization, which should happen only after startup has
46 * completed, and the event loop is idle.
48 nsresult DelayedInit();
50 nsresult Shutdown();
52 private:
53 MemoryTelemetry();
55 ~MemoryTelemetry() = default;
57 void Init();
59 static Result<uint32_t, nsresult> GetOpenTabsCount();
61 class TotalMemoryGatherer final : public nsITimerCallback {
62 public:
63 NS_DECL_THREADSAFE_ISUPPORTS
64 NS_DECL_NSITIMERCALLBACK
66 TotalMemoryGatherer() = default;
68 void CollectParentSize(int64_t aResident);
69 void CollectResult(int64_t aChildUSS);
70 void OnFailure(ipc::ResponseRejectReason aReason);
72 void Begin(nsIEventTarget* aThreadPool);
74 private:
75 ~TotalMemoryGatherer() = default;
77 nsresult MaybeFinish();
79 nsCOMPtr<nsITimer> mTimeout;
81 nsTArray<int64_t> mChildSizes;
83 int64_t mTotalMemory = 0;
84 uint32_t mRemainingChildCount = 0;
86 bool mHaveParentSize = false;
89 nsCOMPtr<nsIEventTarget> mThreadPool;
90 RefPtr<TotalMemoryGatherer> mTotalMemoryGatherer;
92 TimeStamp mLastPoll{};
95 } // namespace mozilla
97 #endif // defined mozilla_MemoryTelemetry_h