Bug 1910362 - Create new Nimbus helper r=aaronmt,ohorvath
[gecko.git] / xpcom / base / MemoryTelemetry.h
blobf8c4bebb1aee0a7326924e4b4be7f69dfa10857b
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/Maybe.h"
12 #include "mozilla/Result.h"
13 #include "nsIObserver.h"
14 #include "nsITimer.h"
15 #include "nsTArray.h"
16 #include "nsWeakReference.h"
18 #include <functional>
20 class nsIEventTarget;
22 namespace mozilla {
24 namespace ipc {
25 enum class ResponseRejectReason;
28 /**
29 * Periodically gathers memory usage metrics after cycle collection, and
30 * populates telemetry histograms with their values.
32 class MemoryTelemetry final : public nsIObserver,
33 public nsSupportsWeakReference {
34 public:
35 NS_DECL_ISUPPORTS
36 NS_DECL_NSIOBSERVER
38 static MemoryTelemetry& Get();
40 nsresult GatherReports(
41 const std::function<void()>& aCompletionCallback = nullptr);
43 /**
44 * Called to signal that we can begin collecting telemetry.
46 void DelayedInit();
48 /**
49 * Notify that the browser is active and telemetry should be recorded soon.
51 void Poke();
53 nsresult Shutdown();
55 private:
56 MemoryTelemetry();
58 ~MemoryTelemetry() = default;
60 void Init();
62 static Result<uint32_t, nsresult> GetOpenTabsCount();
64 void GatherTotalMemory();
65 nsresult FinishGatheringTotalMemory(Maybe<int64_t> aTotalMemory,
66 const nsTArray<int64_t>& aChildSizes);
68 nsCOMPtr<nsIEventTarget> mThreadPool;
70 bool mGatheringTotalMemory = false;
72 TimeStamp mLastRun{};
73 TimeStamp mLastPoke{};
74 nsCOMPtr<nsITimer> mTimer;
76 // True if startup is finished and it's okay to start gathering telemetry.
77 bool mCanRun = false;
80 } // namespace mozilla
82 #endif // defined mozilla_MemoryTelemetry_h