summaryrefslogtreecommitdiffstats
path: root/tools/profiler/gecko/nsProfiler.h
blob: 3757df30793c6100f8bc9016cc73be2cce929a91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsProfiler_h
#define nsProfiler_h

#include "base/process.h"
#include "mozilla/Attributes.h"
#include "mozilla/Maybe.h"
#include "mozilla/MozPromise.h"
#include "mozilla/ProfileJSONWriter.h"
#include "mozilla/ProportionValue.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Vector.h"
#include "nsIProfiler.h"
#include "nsITimer.h"
#include "nsServiceManagerUtils.h"
#include "ProfilerCodeAddressService.h"
#include "ProfileAdditionalInformation.h"

namespace Json {
class Value;
}  // namespace Json

class nsProfiler final : public nsIProfiler {
 public:
  nsProfiler();

  NS_DECL_ISUPPORTS
  NS_DECL_NSIPROFILER

  nsresult Init();

  static nsProfiler* GetOrCreate() {
    nsCOMPtr<nsIProfiler> iprofiler =
        do_GetService("@mozilla.org/tools/profiler;1");
    return static_cast<nsProfiler*>(iprofiler.get());
  }

 private:
  ~nsProfiler();

  using GatheringPromiseAndroid =
      mozilla::MozPromise<FallibleTArray<uint8_t>, nsresult, true>;
  using GatheringPromise =
      mozilla::MozPromise<mozilla::ProfileAndAdditionalInformation, nsresult,
                          false>;
  using SymbolTablePromise =
      mozilla::MozPromise<mozilla::SymbolTable, nsresult, true>;

  RefPtr<GatheringPromise> StartGathering(double aSinceTime);
  void GatheredOOPProfile(
      base::ProcessId aChildPid, const nsACString& aProfile,
      mozilla::Maybe<mozilla::ProfileGenerationAdditionalInformation>&&
          aAdditionalInformation);
  void FinishGathering();
  void ResetGathering(nsresult aPromiseRejectionIfPending);
  static void GatheringTimerCallback(nsITimer* aTimer, void* aClosure);
  void RestartGatheringTimer();

  RefPtr<SymbolTablePromise> GetSymbolTableMozPromise(
      const nsACString& aDebugPath, const nsACString& aBreakpadID);

  struct ExitProfile {
    nsCString mJSON;
    uint64_t mBufferPositionAtGatherTime;
  };

  struct PendingProfile {
    base::ProcessId childPid;

    mozilla::ProportionValue progressProportion;
    nsCString progressLocation;

    mozilla::TimeStamp lastProgressRequest;
    mozilla::TimeStamp lastProgressResponse;
    mozilla::TimeStamp lastProgressChange;

    explicit PendingProfile(base::ProcessId aChildPid) : childPid(aChildPid) {}
  };

  PendingProfile* GetPendingProfile(base::ProcessId aChildPid);
  // Returns false if the request could not be sent.
  bool SendProgressRequest(PendingProfile& aPendingProfile);

  // If the log is active, call aJsonLogObjectUpdater(Json::Value&) on the log's
  // root object.
  template <typename JsonLogObjectUpdater>
  void Log(JsonLogObjectUpdater&& aJsonLogObjectUpdater);
  // If the log is active, call aJsonArrayAppender(Json::Value&) on a Json
  // array that already contains a timestamp, and to which event-related
  // elements may be appended.
  template <typename JsonArrayAppender>
  void LogEvent(JsonArrayAppender&& aJsonArrayAppender);
  void LogEventLiteralString(const char* aEventString);

  // These fields are all related to profile gathering.
  mozilla::Vector<ExitProfile> mExitProfiles;
  mozilla::Maybe<mozilla::MozPromiseHolder<GatheringPromise>> mPromiseHolder;
  nsCOMPtr<nsIThread> mSymbolTableThread;
  mozilla::Maybe<mozilla::FailureLatchSource> mFailureLatchSource;
  mozilla::Maybe<SpliceableChunkedJSONWriter> mWriter;
  mozilla::Maybe<mozilla::ProfileGenerationAdditionalInformation>
      mProfileGenerationAdditionalInformation;
  mozilla::Vector<PendingProfile> mPendingProfiles;
  bool mGathering;
  nsCOMPtr<nsITimer> mGatheringTimer;
  // Supplemental log to the profiler's "profilingLog" (which has already been
  // completed in JSON profiles that are gathered).
  mozilla::UniquePtr<Json::Value> mGatheringLog;
};

#endif  // nsProfiler_h