Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / tools / profiler / public / ProfilerParent.h
blob8bd5c71721093e7b667dbcade6914a90472641a6
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 ProfilerParent_h
8 #define ProfilerParent_h
10 #include "mozilla/PProfilerParent.h"
11 #include "mozilla/RefPtr.h"
13 class nsIProfilerStartParams;
15 namespace mozilla {
17 class ProfileBufferGlobalController;
18 class ProfilerParentTracker;
20 // This is the main process side of the PProfiler protocol.
21 // ProfilerParent instances only exist on the main thread of the main process.
22 // The other side (ProfilerChild) lives on a background thread in the other
23 // process.
24 // The creation of PProfiler actors is initiated from the main process, after
25 // the other process has been launched.
26 // ProfilerParent instances are destroyed once the message channel closes,
27 // which can be triggered by either process, depending on which one shuts down
28 // first.
29 // All ProfilerParent instances are registered with a manager class called
30 // ProfilerParentTracker, which has the list of living ProfilerParent instances
31 // and handles shutdown.
32 class ProfilerParent final : public PProfilerParent {
33 public:
34 NS_INLINE_DECL_REFCOUNTING(ProfilerParent, final)
36 static mozilla::ipc::Endpoint<PProfilerChild> CreateForProcess(
37 base::ProcessId aOtherPid);
39 #ifdef MOZ_GECKO_PROFILER
40 using SingleProcessProfilePromise =
41 MozPromise<IPCProfileAndAdditionalInformation, ResponseRejectReason,
42 true>;
44 struct SingleProcessProfilePromiseAndChildPid {
45 RefPtr<SingleProcessProfilePromise> profilePromise;
46 base::ProcessId childPid;
49 using SingleProcessProgressPromise =
50 MozPromise<GatherProfileProgress, ResponseRejectReason, true>;
52 // The following static methods can be called on any thread, but they are
53 // no-ops on anything other than the main thread.
54 // If called on the main thread, the call will be broadcast to all
55 // registered processes (all processes for which we have a ProfilerParent
56 // object).
57 // At the moment, the main process always calls these methods on the main
58 // thread, and that's the only process in which we need to forward these
59 // calls to other processes. The other processes will call these methods on
60 // the ProfilerChild background thread, but those processes don't need to
61 // forward these calls any further.
63 // Returns the profiles to expect, as promises and child pids.
64 static nsTArray<SingleProcessProfilePromiseAndChildPid> GatherProfiles();
66 // Send a request to get the GatherProfiles() progress update from one child
67 // process, returns a promise to be resolved with that progress.
68 // The promise RefPtr may be null if the child process is unknown.
69 // Progress may be invalid, if the request arrived after the child process
70 // had already responded to the main GatherProfile() IPC, or something went
71 // very wrong in that process.
72 static RefPtr<SingleProcessProgressPromise> RequestGatherProfileProgress(
73 base::ProcessId aChildPid);
75 // This will start the profiler in all child processes. The returned promise
76 // will be resolved when all child have completed their operation
77 // (successfully or not.)
78 [[nodiscard]] static RefPtr<GenericPromise> ProfilerStarted(
79 nsIProfilerStartParams* aParams);
80 static void ProfilerWillStopIfStarted();
81 [[nodiscard]] static RefPtr<GenericPromise> ProfilerStopped();
82 [[nodiscard]] static RefPtr<GenericPromise> ProfilerPaused();
83 [[nodiscard]] static RefPtr<GenericPromise> ProfilerResumed();
84 [[nodiscard]] static RefPtr<GenericPromise> ProfilerPausedSampling();
85 [[nodiscard]] static RefPtr<GenericPromise> ProfilerResumedSampling();
86 static void ClearAllPages();
88 [[nodiscard]] static RefPtr<GenericPromise> WaitOnePeriodicSampling();
90 // Create a "Final" update that the Child can return to its Parent.
91 static ProfileBufferChunkManagerUpdate MakeFinalUpdate();
93 // True if the ProfilerParent holds a lock on this thread.
94 static bool IsLockedOnCurrentThread();
96 private:
97 friend class ProfileBufferGlobalController;
98 friend class ProfilerParentTracker;
100 explicit ProfilerParent(base::ProcessId aChildPid);
102 void Init();
103 void ActorDestroy(ActorDestroyReason aActorDestroyReason) override;
105 void RequestChunkManagerUpdate();
107 base::ProcessId mChildPid;
108 nsTArray<MozPromiseHolder<SingleProcessProfilePromise>>
109 mPendingRequestedProfiles;
110 bool mDestroyed;
111 #endif // MOZ_GECKO_PROFILER
113 private:
114 virtual ~ProfilerParent();
117 } // namespace mozilla
119 #endif // ProfilerParent_h