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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/* -*- 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/. */
#include "ProfilerChild.h"
#include "GeckoProfiler.h"
#include "platform.h"
#include "ProfilerParent.h"
#include "nsThreadUtils.h"
namespace mozilla {
/* static */ DataMutexBase<ProfilerChild::ProfilerChildAndUpdate,
baseprofiler::detail::BaseProfilerMutex>
ProfilerChild::sPendingChunkManagerUpdate{
"ProfilerChild::sPendingChunkManagerUpdate"};
ProfilerChild::ProfilerChild()
: mThread(NS_GetCurrentThread()), mDestroyed(false) {
MOZ_COUNT_CTOR(ProfilerChild);
}
ProfilerChild::~ProfilerChild() { MOZ_COUNT_DTOR(ProfilerChild); }
void ProfilerChild::ResolveChunkUpdate(
PProfilerChild::AwaitNextChunkManagerUpdateResolver& aResolve) {
MOZ_ASSERT(!!aResolve,
"ResolveChunkUpdate should only be called when there's a pending "
"resolver");
MOZ_ASSERT(
!mChunkManagerUpdate.IsNotUpdate(),
"ResolveChunkUpdate should only be called with a real or final update");
MOZ_ASSERT(
!mDestroyed,
"ResolveChunkUpdate should not be called if the actor was destroyed");
if (mChunkManagerUpdate.IsFinal()) {
// Final update, send a special "unreleased value", but don't clear the
// local copy so we know we got the final update.
std::move(aResolve)(ProfilerParent::MakeFinalUpdate());
} else {
// Optimization note: The ProfileBufferChunkManagerUpdate constructor takes
// the newly-released chunks nsTArray by reference-to-const, therefore
// constructing and then moving the array here would make a copy. So instead
// we first give it an empty array, and then we can write the data directly
// into the update's array.
ProfileBufferChunkManagerUpdate update{
mChunkManagerUpdate.UnreleasedBytes(),
mChunkManagerUpdate.ReleasedBytes(),
mChunkManagerUpdate.OldestDoneTimeStamp(),
{}};
update.newlyReleasedChunks().SetCapacity(
mChunkManagerUpdate.NewlyReleasedChunksRef().size());
for (const ProfileBufferControlledChunkManager::ChunkMetadata& chunk :
mChunkManagerUpdate.NewlyReleasedChunksRef()) {
update.newlyReleasedChunks().EmplaceBack(chunk.mDoneTimeStamp,
chunk.mBufferBytes);
}
std::move(aResolve)(update);
// Clear the update we just sent, so it's ready for later updates to be
// folded into it.
mChunkManagerUpdate.Clear();
}
// Discard the resolver, so it's empty next time there's a new request.
aResolve = nullptr;
}
void ProfilerChild::ProcessChunkManagerUpdate(
ProfileBufferControlledChunkManager::Update&& aUpdate) {
if (mDestroyed) {
return;
}
// Always store the data, it could be the final update.
mChunkManagerUpdate.Fold(std::move(aUpdate));
if (mAwaitNextChunkManagerUpdateResolver) {
// There is already a pending resolver, give it the info now.
ResolveChunkUpdate(mAwaitNextChunkManagerUpdateResolver);
}
}
/* static */ void ProfilerChild::ProcessPendingUpdate() {
auto lockedUpdate = sPendingChunkManagerUpdate.Lock();
if (!lockedUpdate->mProfilerChild || lockedUpdate->mUpdate.IsNotUpdate()) {
return;
}
lockedUpdate->mProfilerChild->mThread->Dispatch(NS_NewRunnableFunction(
"ProfilerChild::ProcessPendingUpdate", []() mutable {
auto lockedUpdate = sPendingChunkManagerUpdate.Lock();
if (!lockedUpdate->mProfilerChild ||
lockedUpdate->mUpdate.IsNotUpdate()) {
return;
}
lockedUpdate->mProfilerChild->ProcessChunkManagerUpdate(
std::move(lockedUpdate->mUpdate));
lockedUpdate->mUpdate.Clear();
}));
}
/* static */ bool ProfilerChild::IsLockedOnCurrentThread() {
return sPendingChunkManagerUpdate.Mutex().IsLockedOnCurrentThread();
}
void ProfilerChild::SetupChunkManager() {
mChunkManager = profiler_get_controlled_chunk_manager();
if (NS_WARN_IF(!mChunkManager)) {
return;
}
// Make sure there are no updates (from a previous run).
mChunkManagerUpdate.Clear();
{
auto lockedUpdate = sPendingChunkManagerUpdate.Lock();
lockedUpdate->mProfilerChild = this;
lockedUpdate->mUpdate.Clear();
}
mChunkManager->SetUpdateCallback(
[](ProfileBufferControlledChunkManager::Update&& aUpdate) {
// Updates from the chunk manager are stored for later processing.
// We avoid dispatching a task, as this could deadlock (if the queueing
// mutex is held elsewhere).
auto lockedUpdate = sPendingChunkManagerUpdate.Lock();
if (!lockedUpdate->mProfilerChild) {
return;
}
lockedUpdate->mUpdate.Fold(std::move(aUpdate));
});
}
void ProfilerChild::ResetChunkManager() {
if (!mChunkManager) {
return;
}
// We have a chunk manager, reset the callback, which will add a final
// pending update.
mChunkManager->SetUpdateCallback({});
// Clear the pending update.
auto lockedUpdate = sPendingChunkManagerUpdate.Lock();
lockedUpdate->mProfilerChild = nullptr;
lockedUpdate->mUpdate.Clear();
// And process a final update right now.
ProcessChunkManagerUpdate(
ProfileBufferControlledChunkManager::Update(nullptr));
mChunkManager = nullptr;
mAwaitNextChunkManagerUpdateResolver = nullptr;
}
mozilla::ipc::IPCResult ProfilerChild::RecvStart(
const ProfilerInitParams& params) {
nsTArray<const char*> filterArray;
for (size_t i = 0; i < params.filters().Length(); ++i) {
filterArray.AppendElement(params.filters()[i].get());
}
profiler_start(PowerOfTwo32(params.entries()), params.interval(),
params.features(), filterArray.Elements(),
filterArray.Length(), params.activeBrowsingContextID(),
params.duration());
SetupChunkManager();
return IPC_OK();
}
mozilla::ipc::IPCResult ProfilerChild::RecvEnsureStarted(
const ProfilerInitParams& params) {
nsTArray<const char*> filterArray;
for (size_t i = 0; i < params.filters().Length(); ++i) {
filterArray.AppendElement(params.filters()[i].get());
}
profiler_ensure_started(PowerOfTwo32(params.entries()), params.interval(),
params.features(), filterArray.Elements(),
filterArray.Length(),
params.activeBrowsingContextID(), params.duration());
SetupChunkManager();
return IPC_OK();
}
mozilla::ipc::IPCResult ProfilerChild::RecvStop() {
ResetChunkManager();
profiler_stop();
return IPC_OK();
}
mozilla::ipc::IPCResult ProfilerChild::RecvPause() {
profiler_pause();
return IPC_OK();
}
mozilla::ipc::IPCResult ProfilerChild::RecvResume() {
profiler_resume();
return IPC_OK();
}
mozilla::ipc::IPCResult ProfilerChild::RecvPauseSampling() {
profiler_pause_sampling();
return IPC_OK();
}
mozilla::ipc::IPCResult ProfilerChild::RecvResumeSampling() {
profiler_resume_sampling();
return IPC_OK();
}
mozilla::ipc::IPCResult ProfilerChild::RecvClearAllPages() {
profiler_clear_all_pages();
return IPC_OK();
}
static nsCString CollectProfileOrEmptyString(bool aIsShuttingDown) {
nsCString profileCString;
UniquePtr<char[]> profile =
profiler_get_profile(/* aSinceTime */ 0, aIsShuttingDown);
if (profile) {
size_t len = strlen(profile.get());
profileCString.Adopt(profile.release(), len);
}
return profileCString;
}
mozilla::ipc::IPCResult ProfilerChild::RecvAwaitNextChunkManagerUpdate(
AwaitNextChunkManagerUpdateResolver&& aResolve) {
MOZ_ASSERT(!mDestroyed,
"Recv... should not be called if the actor was destroyed");
// Pick up pending updates if any.
{
auto lockedUpdate = sPendingChunkManagerUpdate.Lock();
if (lockedUpdate->mProfilerChild && !lockedUpdate->mUpdate.IsNotUpdate()) {
mChunkManagerUpdate.Fold(std::move(lockedUpdate->mUpdate));
lockedUpdate->mUpdate.Clear();
}
}
if (mChunkManagerUpdate.IsNotUpdate()) {
// No data yet, store the resolver for later.
mAwaitNextChunkManagerUpdateResolver = std::move(aResolve);
} else {
// We have data, send it now.
ResolveChunkUpdate(aResolve);
}
return IPC_OK();
}
mozilla::ipc::IPCResult ProfilerChild::RecvDestroyReleasedChunksAtOrBefore(
const TimeStamp& aTimeStamp) {
if (mChunkManager) {
mChunkManager->DestroyChunksAtOrBefore(aTimeStamp);
}
return IPC_OK();
}
mozilla::ipc::IPCResult ProfilerChild::RecvGatherProfile(
GatherProfileResolver&& aResolve) {
mozilla::ipc::Shmem shmem;
profiler_get_profile_json_into_lazily_allocated_buffer(
[&](size_t allocationSize) -> char* {
if (AllocShmem(allocationSize,
mozilla::ipc::Shmem::SharedMemory::TYPE_BASIC, &shmem)) {
return shmem.get<char>();
}
return nullptr;
},
/* aSinceTime */ 0,
/* aIsShuttingDown */ false);
aResolve(std::move(shmem));
return IPC_OK();
}
void ProfilerChild::ActorDestroy(ActorDestroyReason aActorDestroyReason) {
mDestroyed = true;
}
void ProfilerChild::Destroy() {
ResetChunkManager();
if (!mDestroyed) {
Close();
}
}
nsCString ProfilerChild::GrabShutdownProfile() {
return CollectProfileOrEmptyString(/* aIsShuttingDown */ true);
}
} // namespace mozilla
|