/src/mozilla-central/tools/profiler/gecko/ChildProfilerController.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "ChildProfilerController.h" |
8 | | #include "nsThreadUtils.h" |
9 | | #include "ProfilerChild.h" |
10 | | |
11 | | using namespace mozilla::ipc; |
12 | | |
13 | | namespace mozilla { |
14 | | |
15 | | /* static */ already_AddRefed<ChildProfilerController> |
16 | | ChildProfilerController::Create(mozilla::ipc::Endpoint<PProfilerChild>&& aEndpoint) |
17 | 0 | { |
18 | 0 | MOZ_RELEASE_ASSERT(NS_IsMainThread()); |
19 | 0 | RefPtr<ChildProfilerController> cpc = new ChildProfilerController(); |
20 | 0 | cpc->Init(std::move(aEndpoint)); |
21 | 0 | return cpc.forget(); |
22 | 0 | } |
23 | | |
24 | | ChildProfilerController::ChildProfilerController() |
25 | 0 | { |
26 | 0 | MOZ_COUNT_CTOR(ChildProfilerController); |
27 | 0 | } |
28 | | |
29 | | void |
30 | | ChildProfilerController::Init(Endpoint<PProfilerChild>&& aEndpoint) |
31 | 0 | { |
32 | 0 | if (NS_SUCCEEDED(NS_NewNamedThread("ProfilerChild", getter_AddRefs(mThread)))) { |
33 | 0 | // Now that mThread has been set, run SetupProfilerChild on the thread. |
34 | 0 | mThread->Dispatch(NewRunnableMethod<Endpoint<PProfilerChild>&&>( |
35 | 0 | "ChildProfilerController::SetupProfilerChild", |
36 | 0 | this, |
37 | 0 | &ChildProfilerController::SetupProfilerChild, |
38 | 0 | std::move(aEndpoint)), |
39 | 0 | NS_DISPATCH_NORMAL); |
40 | 0 | } |
41 | 0 | } |
42 | | |
43 | | nsCString |
44 | | ChildProfilerController::GrabShutdownProfileAndShutdown() |
45 | 0 | { |
46 | 0 | nsCString shutdownProfile; |
47 | 0 | ShutdownAndMaybeGrabShutdownProfileFirst(&shutdownProfile); |
48 | 0 | return shutdownProfile; |
49 | 0 | } |
50 | | |
51 | | void |
52 | | ChildProfilerController::Shutdown() |
53 | 0 | { |
54 | 0 | ShutdownAndMaybeGrabShutdownProfileFirst(nullptr); |
55 | 0 | } |
56 | | |
57 | | void |
58 | | ChildProfilerController::ShutdownAndMaybeGrabShutdownProfileFirst(nsCString* aOutShutdownProfile) |
59 | 0 | { |
60 | 0 | if (mThread) { |
61 | 0 | mThread->Dispatch(NewRunnableMethod<nsCString*>( |
62 | 0 | "ChildProfilerController::ShutdownProfilerChild", |
63 | 0 | this, |
64 | 0 | &ChildProfilerController::ShutdownProfilerChild, |
65 | 0 | aOutShutdownProfile), |
66 | 0 | NS_DISPATCH_NORMAL); |
67 | 0 | // Shut down the thread. This call will spin until all runnables (including |
68 | 0 | // the ShutdownProfilerChild runnable) have been processed. |
69 | 0 | mThread->Shutdown(); |
70 | 0 | mThread = nullptr; |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | ChildProfilerController::~ChildProfilerController() |
75 | 0 | { |
76 | 0 | MOZ_COUNT_DTOR(ChildProfilerController); |
77 | 0 |
|
78 | 0 | MOZ_ASSERT(!mThread, "Please call Shutdown before destroying ChildProfilerController"); |
79 | 0 | MOZ_ASSERT(!mProfilerChild); |
80 | 0 | } |
81 | | |
82 | | void |
83 | | ChildProfilerController::SetupProfilerChild(Endpoint<PProfilerChild>&& aEndpoint) |
84 | 0 | { |
85 | 0 | MOZ_RELEASE_ASSERT(mThread == NS_GetCurrentThread()); |
86 | 0 | MOZ_ASSERT(aEndpoint.IsValid()); |
87 | 0 |
|
88 | 0 | mProfilerChild = new ProfilerChild(); |
89 | 0 | Endpoint<PProfilerChild> endpoint = std::move(aEndpoint); |
90 | 0 |
|
91 | 0 | if (!endpoint.Bind(mProfilerChild)) { |
92 | 0 | MOZ_CRASH("Failed to bind ProfilerChild!"); |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | | void |
97 | | ChildProfilerController::ShutdownProfilerChild(nsCString* aOutShutdownProfile) |
98 | 0 | { |
99 | 0 | MOZ_RELEASE_ASSERT(mThread == NS_GetCurrentThread()); |
100 | 0 |
|
101 | 0 | if (aOutShutdownProfile) { |
102 | 0 | *aOutShutdownProfile = mProfilerChild->GrabShutdownProfile(); |
103 | 0 | } |
104 | 0 | mProfilerChild->Destroy(); |
105 | 0 | mProfilerChild = nullptr; |
106 | 0 | } |
107 | | |
108 | | } // namespace mozilla |