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/. */
6 
7 #ifndef nsProfiler_h
8 #define nsProfiler_h
9 
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/MozPromise.h"
13 #include "mozilla/ProfileJSONWriter.h"
14 #include "mozilla/TimeStamp.h"
15 #include "mozilla/Vector.h"
16 #include "nsIObserver.h"
17 #include "nsIProfiler.h"
18 #include "nsITimer.h"
19 #include "nsServiceManagerUtils.h"
20 #include "ProfilerCodeAddressService.h"
21 
22 class nsProfiler final : public nsIProfiler, public nsIObserver {
23  public:
24   nsProfiler();
25 
26   NS_DECL_ISUPPORTS
27   NS_DECL_NSIOBSERVER
28   NS_DECL_NSIPROFILER
29 
30   nsresult Init();
31 
GetOrCreate()32   static nsProfiler* GetOrCreate() {
33     nsCOMPtr<nsIProfiler> iprofiler =
34         do_GetService("@mozilla.org/tools/profiler;1");
35     return static_cast<nsProfiler*>(iprofiler.get());
36   }
37 
38   void GatheredOOPProfile(const nsACString& aProfile);
39 
40  private:
41   ~nsProfiler();
42 
43   typedef mozilla::MozPromise<nsCString, nsresult, false> GatheringPromise;
44   typedef mozilla::MozPromise<mozilla::SymbolTable, nsresult, true>
45       SymbolTablePromise;
46 
47   RefPtr<GatheringPromise> StartGathering(double aSinceTime);
48   void FinishGathering();
49   void ResetGathering();
50   static void GatheringTimerCallback(nsITimer* aTimer, void* aClosure);
51 
52   RefPtr<SymbolTablePromise> GetSymbolTableMozPromise(
53       const nsACString& aDebugPath, const nsACString& aBreakpadID);
54 
55   bool mLockedForPrivateBrowsing;
56 
57   struct ExitProfile {
58     nsCString mJSON;
59     uint64_t mBufferPositionAtGatherTime;
60   };
61 
62   // These fields are all related to profile gathering.
63   mozilla::Vector<ExitProfile> mExitProfiles;
64   mozilla::Maybe<mozilla::MozPromiseHolder<GatheringPromise>> mPromiseHolder;
65   nsCOMPtr<nsIThread> mSymbolTableThread;
66   mozilla::Maybe<SpliceableChunkedJSONWriter> mWriter;
67   uint32_t mPendingProfiles;
68   bool mGathering;
69   nsCOMPtr<nsITimer> mGatheringTimer;
70 };
71 
72 #endif  // nsProfiler_h
73