Skip to content

Commit

Permalink
JingleThreadWrapper: override TaskQueueBase::Post[Delayed]Task().
Browse files Browse the repository at this point in the history
This change gets rid of overhead in std::map and locks (visible
in http://shortn/_tSjdrNiMky) by overriding and using the internal
|task_runner_| Chromium task queue directly, thus avoiding the
rtc::Thread ID and MessageHandler system.

The result can be seen in http://shortn/_BT1aflWMtm.

Fixed: 1223058
Change-Id: I5c93fcd63d27977b47fb2da1127bdfc0618fbf57
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2982659
Commit-Queue: Markus Handell <handellm@google.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Auto-Submit: Markus Handell <handellm@google.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tommi <tommi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#895669}
  • Loading branch information
handellm authored and Chromium LUCI CQ committed Jun 24, 2021
1 parent b36a6ac commit 8fd13d1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
47 changes: 44 additions & 3 deletions jingle/glue/thread_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,23 @@ void JingleThreadWrapper::PostTaskInternal(const rtc::Location& posted_from,
}
}

void JingleThreadWrapper::RunTask(int task_id) {
void JingleThreadWrapper::PostTask(std::unique_ptr<webrtc::QueuedTask> task) {
task_runner_->PostTask(FROM_HERE,
base::BindOnce(&JingleThreadWrapper::RunTaskQueueTask,
weak_ptr_, std::move(task)));
}

void JingleThreadWrapper::PostDelayedTask(
std::unique_ptr<webrtc::QueuedTask> task,
uint32_t milliseconds) {
task_runner_->PostDelayedTask(
FROM_HERE,
base::BindOnce(&JingleThreadWrapper::RunTaskQueueTask, weak_ptr_,
std::move(task)),
base::TimeDelta::FromMilliseconds(milliseconds));
}

absl::optional<base::TimeTicks> JingleThreadWrapper::PrepareRunTask() {
if (!latency_sampler_ && task_latency_callback_) {
latency_sampler_ = std::make_unique<PostTaskLatencySampler>(
task_runner_, std::move(task_latency_callback_));
Expand All @@ -336,10 +352,35 @@ void JingleThreadWrapper::RunTask(int task_id) {
latency_sampler_->ShouldSampleNextTaskDuration()) {
task_start_timestamp = base::TimeTicks::Now();
}
return task_start_timestamp;
}

void JingleThreadWrapper::RunTaskQueueTask(
std::unique_ptr<webrtc::QueuedTask> task) {
absl::optional<base::TimeTicks> task_start_timestamp = PrepareRunTask();

// Follow QueuedTask::Run() semantics: delete if it returns true, release
// otherwise.
if (task->Run())
task.reset();
else
task.release();

FinalizeRunTask(std::move(task_start_timestamp));
}

void JingleThreadWrapper::RunTask(int task_id) {
absl::optional<base::TimeTicks> task_start_timestamp = PrepareRunTask();

RunTaskInternal(task_id);
if (task_start_timestamp.has_value()) {

FinalizeRunTask(std::move(task_start_timestamp));
}

void JingleThreadWrapper::FinalizeRunTask(
absl::optional<base::TimeTicks> task_start_timestamp) {
if (task_start_timestamp.has_value())
task_duration_callback_.Run(base::TimeTicks::Now() - *task_start_timestamp);
}
}

void JingleThreadWrapper::RunTaskInternal(int task_id) {
Expand Down
17 changes: 17 additions & 0 deletions jingle/glue/thread_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "base/synchronization/waitable_event.h"
#include "base/task/current_thread.h"
#include "base/time/time.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/webrtc/rtc_base/thread.h"

namespace jingle_glue {
Expand Down Expand Up @@ -141,6 +142,22 @@ class JingleThreadWrapper : public base::CurrentThread::DestructionObserver,
void RunTaskInternal(int task_id);
void ProcessPendingSends();

// TaskQueueBase overrides.
void PostTask(std::unique_ptr<webrtc::QueuedTask> task) override;
void PostDelayedTask(std::unique_ptr<webrtc::QueuedTask> task,
uint32_t milliseconds) override;

// Executes WebRTC queued tasks from TaskQueueBase overrides on
// |task_runner_|.
void RunTaskQueueTask(std::unique_ptr<webrtc::QueuedTask> task);

// Called before a task runs, returns an opaque optional timestamp which
// should be passed into FinalizeRunTask.
absl::optional<base::TimeTicks> PrepareRunTask();
// Called after a task has run. Move the return value of PrepareRunTask as
// |task_start_timestamp|.
void FinalizeRunTask(absl::optional<base::TimeTicks> task_start_timestamp);

// Task runner used to execute messages posted on this thread.
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

Expand Down

0 comments on commit 8fd13d1

Please sign in to comment.