Skip to content

Commit 6f3e18a

Browse files
committed
Bug 1746361 - Gecko Profiler: Track wrapped runnables throughout the tree. r=gerald
The Runnable markers in the profilers are handy, but miss many types of runnables. This includes most of those that wrap another runnable and run that at a (possibly) later time. AbstractThread, TaskDispatcher and TaskQueue does this for e.g. tail dispatched tasks. TaskQueueWrapper does this when wrapping webrtc tasks (and Mozilla Runnables) to be run in a Mozilla TaskQueue with some overhead on the stack. DelayedRunnable wraps a runnable to be run after a timeout. It would perhaps be better in many cases to ignore the intermediate runnables, but I haven't seen a straight forward way to achieve this. More detailed data could be added on a case by case basis, for instance the delay for a DelayedRunnable (incl. actual vs. target delay) or the scope of a task in which a tail-dispatched runnable was dispatched. But this is also true for the status quo (for instance the time from dispatch to run) so I leave these ideas as future work. Differential Revision: https://phabricator.services.mozilla.com/D135027
1 parent 159966b commit 6f3e18a

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

dom/media/webrtc/libwebrtcglue/TaskQueueWrapper.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "api/task_queue/task_queue_factory.h"
1010
#include "mozilla/DataMutex.h"
11+
#include "mozilla/ProfilerRunnable.h"
1112
#include "mozilla/TaskQueue.h"
1213
#include "VideoUtils.h"
1314

@@ -25,8 +26,8 @@ namespace mozilla {
2526
*/
2627
class TaskQueueWrapper : public webrtc::TaskQueueBase {
2728
public:
28-
explicit TaskQueueWrapper(RefPtr<TaskQueue> aTaskQueue)
29-
: mTaskQueue(std::move(aTaskQueue)) {}
29+
TaskQueueWrapper(RefPtr<TaskQueue> aTaskQueue, nsCString aName)
30+
: mTaskQueue(std::move(aTaskQueue)), mName(std::move(aName)) {}
3031
~TaskQueueWrapper() = default;
3132

3233
void Delete() override {
@@ -44,18 +45,22 @@ class TaskQueueWrapper : public webrtc::TaskQueueBase {
4445

4546
already_AddRefed<Runnable> CreateTaskRunner(
4647
std::unique_ptr<webrtc::QueuedTask> aTask) {
47-
return NS_NewRunnableFunction("TaskQueueWrapper::CreateTaskRunner",
48-
[this, task = std::move(aTask)]() mutable {
49-
CurrentTaskQueueSetter current(this);
50-
auto hasShutdown = mHasShutdown.Lock();
51-
if (*hasShutdown) {
52-
return;
53-
}
54-
bool toDelete = task->Run();
55-
if (!toDelete) {
56-
task.release();
57-
}
58-
});
48+
return NS_NewRunnableFunction(
49+
"TaskQueueWrapper::CreateTaskRunner",
50+
[this, task = std::move(aTask),
51+
name = nsPrintfCString("TQ %s: webrtc::QueuedTask",
52+
mName.get())]() mutable {
53+
CurrentTaskQueueSetter current(this);
54+
auto hasShutdown = mHasShutdown.Lock();
55+
if (*hasShutdown) {
56+
return;
57+
}
58+
AUTO_PROFILE_FOLLOWING_RUNNABLE(name);
59+
bool toDelete = task->Run();
60+
if (!toDelete) {
61+
task.release();
62+
}
63+
});
5964
}
6065

6166
already_AddRefed<Runnable> CreateTaskRunner(nsCOMPtr<nsIRunnable> aRunnable) {
@@ -67,6 +72,7 @@ class TaskQueueWrapper : public webrtc::TaskQueueBase {
6772
if (*hasShutdown) {
6873
return;
6974
}
75+
AUTO_PROFILE_FOLLOWING_RUNNABLE(runnable);
7076
runnable->Run();
7177
});
7278
}
@@ -88,6 +94,7 @@ class TaskQueueWrapper : public webrtc::TaskQueueBase {
8894
}
8995

9096
const RefPtr<TaskQueue> mTaskQueue;
97+
const nsCString mName;
9198

9299
// This is a recursive mutex because a TaskRunner holding this mutex while
93100
// running its runnable may end up running other - tail dispatched - runnables
@@ -119,7 +126,7 @@ class SharedThreadPoolWebRtcTaskQueueFactory : public webrtc::TaskQueueFactory {
119126
nsCString name(aName.data(), aName.size());
120127
auto taskQueue = MakeRefPtr<TaskQueue>(GetMediaThreadPool(aThreadType),
121128
name.get(), aSupportTailDispatch);
122-
return MakeUnique<TaskQueueWrapper>(std::move(taskQueue));
129+
return MakeUnique<TaskQueueWrapper>(std::move(taskQueue), std::move(name));
123130
}
124131

125132
std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter>

media/webrtc/signaling/gtest/mediapipeline_unittest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ namespace {
5252
class MainAsCurrent : public TaskQueueWrapper {
5353
public:
5454
MainAsCurrent()
55-
: TaskQueueWrapper(MakeRefPtr<TaskQueue>(
56-
do_AddRef(GetMainThreadEventTarget()), "MainAsCurrentTaskQueue")),
55+
: TaskQueueWrapper(
56+
MakeRefPtr<TaskQueue>(do_AddRef(GetMainThreadEventTarget()),
57+
"MainAsCurrentTaskQueue"),
58+
"MainAsCurrent"_ns),
5759
mSetter(this) {
5860
MOZ_RELEASE_ASSERT(NS_IsMainThread());
5961
}

xpcom/threads/AbstractThread.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "mozilla/DelayedRunnable.h"
1111
#include "mozilla/Maybe.h"
1212
#include "mozilla/MozPromise.h" // We initialize the MozPromise logging in this file.
13+
#include "mozilla/ProfilerRunnable.h"
1314
#include "mozilla/StateWatching.h" // We initialize the StateWatching logging in this file.
1415
#include "mozilla/StaticPtr.h"
1516
#include "mozilla/TaskDispatcher.h"
@@ -208,6 +209,7 @@ class XPCOMThreadWrapper final : public AbstractThread,
208209
MOZ_ASSERT(mThread == AbstractThread::GetCurrent());
209210
MOZ_ASSERT(mThread->IsCurrentThreadIn());
210211
SerialEventTargetGuard guard(mThread);
212+
AUTO_PROFILE_FOLLOWING_RUNNABLE(mRunnable);
211213
return mRunnable->Run();
212214
}
213215

xpcom/threads/DelayedRunnable.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "DelayedRunnable.h"
88

9+
#include "mozilla/ProfilerRunnable.h"
10+
911
namespace mozilla {
1012

1113
DelayedRunnable::DelayedRunnable(already_AddRefed<nsIEventTarget> aTarget,
@@ -65,6 +67,7 @@ NS_IMETHODIMP DelayedRunnable::Notify(nsITimer* aTimer) {
6567

6668
nsresult DelayedRunnable::DoRun() {
6769
nsCOMPtr<nsIRunnable> r = std::move(mWrappedRunnable);
70+
AUTO_PROFILE_FOLLOWING_RUNNABLE(r);
6871
return r->Run();
6972
}
7073

xpcom/threads/TaskDispatcher.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
# include "mozilla/AbstractThread.h"
1313
# include "mozilla/Maybe.h"
14+
# include "mozilla/ProfilerRunnable.h"
1415
# include "mozilla/UniquePtr.h"
1516
# include "nsIDirectTaskDispatcher.h"
1617
# include "nsISupportsImpl.h"
@@ -39,6 +40,7 @@ class SimpleTaskQueue {
3940
while (!queue.empty()) {
4041
nsCOMPtr<nsIRunnable> r = std::move(queue.front());
4142
queue.pop();
43+
AUTO_PROFILE_FOLLOWING_RUNNABLE(r);
4244
r->Run();
4345
}
4446
}
@@ -224,6 +226,7 @@ class AutoTaskDispatcher : public TaskDispatcher {
224226
MaybeDrainDirectTasks();
225227

226228
for (size_t i = 0; i < mTasks->mRegularTasks.Length(); ++i) {
229+
AUTO_PROFILE_FOLLOWING_RUNNABLE(mTasks->mRegularTasks[i]);
227230
mTasks->mRegularTasks[i]->Run();
228231

229232
// Scope direct tasks tightly to the task that generated them.

xpcom/threads/TaskQueue.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "mozilla/TaskQueue.h"
88

99
#include "mozilla/DelayedRunnable.h"
10+
#include "mozilla/ProfilerRunnable.h"
1011
#include "nsThreadUtils.h"
1112

1213
namespace mozilla {
@@ -201,6 +202,7 @@ nsresult TaskQueue::Runner::Run() {
201202
{
202203
LogRunnable::Run log(event.event);
203204

205+
AUTO_PROFILE_FOLLOWING_RUNNABLE(event.event);
204206
event.event->Run();
205207

206208
// Drop the reference to event. The event will hold a reference to the

0 commit comments

Comments
 (0)