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"
15 #include "nsWeakReference.h"
24 enum class ResponseRejectReason
;
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
{
37 static MemoryTelemetry
& Get();
39 nsresult
GatherReports(
40 const std::function
<void()>& aCompletionCallback
= nullptr);
42 void GetUniqueSetSize(std::function
<void(const int64_t&)>&& aCallback
);
45 * Does expensive initialization, which should happen only after startup has
46 * completed, and the event loop is idle.
48 nsresult
DelayedInit();
55 ~MemoryTelemetry() = default;
59 static Result
<uint32_t, nsresult
> GetOpenTabsCount();
61 class TotalMemoryGatherer final
: public nsITimerCallback
{
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
);
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