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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/scheduler/task_queue_manager.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/debug/trace_event_argument.h"
#include "content/renderer/scheduler/task_queue_selector.h"
namespace content {
namespace internal {
class TaskQueue : public base::SingleThreadTaskRunner {
public:
TaskQueue(TaskQueueManager* task_queue_manager);
// base::SingleThreadTaskRunner implementation.
bool RunsTasksOnCurrentThread() const override;
bool PostDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) override {
return PostDelayedTaskImpl(from_here, task, delay, true);
}
bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) override {
return PostDelayedTaskImpl(from_here, task, delay, false);
}
// Adds a task at the end of the incoming task queue and schedules a call to
// TaskQueueManager::DoWork() if the incoming queue was empty and automatic
// pumping is enabled. Can be called on an arbitrary thread.
void EnqueueTask(const base::PendingTask& pending_task);
bool IsQueueEmpty() const;
void SetAutoPump(bool auto_pump);
void PumpQueue();
bool UpdateWorkQueue();
base::PendingTask TakeTaskFromWorkQueue();
void WillDeleteTaskQueueManager();
base::TaskQueue& work_queue() { return work_queue_; }
void set_name(const char* name) { name_ = name; }
void AsValueInto(base::debug::TracedValue* state) const;
private:
~TaskQueue() override;
bool PostDelayedTaskImpl(const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay,
bool nestable);
void PumpQueueLocked();
void EnqueueTaskLocked(const base::PendingTask& pending_task);
void TraceWorkQueueSize() const;
static void QueueAsValueInto(const base::TaskQueue& queue,
base::debug::TracedValue* state);
static void TaskAsValueInto(const base::PendingTask& task,
base::debug::TracedValue* state);
// This lock protects all members except the work queue.
mutable base::Lock lock_;
TaskQueueManager* task_queue_manager_;
base::TaskQueue incoming_queue_;
bool auto_pump_;
const char* name_;
base::TaskQueue work_queue_;
DISALLOW_COPY_AND_ASSIGN(TaskQueue);
};
TaskQueue::TaskQueue(TaskQueueManager* task_queue_manager)
: task_queue_manager_(task_queue_manager),
auto_pump_(true),
name_(nullptr) {
}
TaskQueue::~TaskQueue() {
}
void TaskQueue::WillDeleteTaskQueueManager() {
base::AutoLock lock(lock_);
task_queue_manager_ = nullptr;
}
bool TaskQueue::RunsTasksOnCurrentThread() const {
base::AutoLock lock(lock_);
if (!task_queue_manager_)
return false;
return task_queue_manager_->RunsTasksOnCurrentThread();
}
bool TaskQueue::PostDelayedTaskImpl(const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay,
bool nestable) {
base::AutoLock lock(lock_);
if (!task_queue_manager_)
return false;
base::PendingTask pending_task(from_here, task, base::TimeTicks(), nestable);
task_queue_manager_->DidQueueTask(&pending_task);
if (delay > base::TimeDelta()) {
return task_queue_manager_->PostDelayedTask(
from_here, Bind(&TaskQueue::EnqueueTask, this, pending_task), delay);
}
EnqueueTaskLocked(pending_task);
return true;
}
bool TaskQueue::IsQueueEmpty() const {
if (!work_queue_.empty())
return false;
{
base::AutoLock lock(lock_);
return incoming_queue_.empty();
}
}
bool TaskQueue::UpdateWorkQueue() {
if (!work_queue_.empty())
return true;
{
base::AutoLock lock(lock_);
if (!auto_pump_ || incoming_queue_.empty())
return false;
work_queue_.Swap(&incoming_queue_);
TraceWorkQueueSize();
return true;
}
}
base::PendingTask TaskQueue::TakeTaskFromWorkQueue() {
base::PendingTask pending_task = work_queue_.front();
work_queue_.pop();
TraceWorkQueueSize();
return pending_task;
}
void TaskQueue::TraceWorkQueueSize() const {
if (!name_)
return;
TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), name_,
work_queue_.size());
}
void TaskQueue::EnqueueTask(const base::PendingTask& pending_task) {
base::AutoLock lock(lock_);
EnqueueTaskLocked(pending_task);
}
void TaskQueue::EnqueueTaskLocked(const base::PendingTask& pending_task) {
lock_.AssertAcquired();
if (!task_queue_manager_)
return;
if (auto_pump_ && incoming_queue_.empty())
task_queue_manager_->MaybePostDoWorkOnMainRunner();
incoming_queue_.push(pending_task);
}
void TaskQueue::SetAutoPump(bool auto_pump) {
base::AutoLock lock(lock_);
if (auto_pump) {
auto_pump_ = true;
PumpQueueLocked();
} else {
auto_pump_ = false;
}
}
void TaskQueue::PumpQueueLocked() {
lock_.AssertAcquired();
while (!incoming_queue_.empty()) {
work_queue_.push(incoming_queue_.front());
incoming_queue_.pop();
}
if (!work_queue_.empty())
task_queue_manager_->MaybePostDoWorkOnMainRunner();
}
void TaskQueue::PumpQueue() {
base::AutoLock lock(lock_);
PumpQueueLocked();
}
void TaskQueue::AsValueInto(base::debug::TracedValue* state) const {
base::AutoLock lock(lock_);
state->BeginDictionary();
if (name_)
state->SetString("name", name_);
state->SetBoolean("auto_pump", auto_pump_);
state->BeginArray("incoming_queue");
QueueAsValueInto(incoming_queue_, state);
state->EndArray();
state->BeginArray("work_queue");
QueueAsValueInto(work_queue_, state);
state->EndArray();
state->EndDictionary();
}
// static
void TaskQueue::QueueAsValueInto(const base::TaskQueue& queue,
base::debug::TracedValue* state) {
base::TaskQueue queue_copy(queue);
while (!queue_copy.empty()) {
TaskAsValueInto(queue_copy.front(), state);
queue_copy.pop();
}
}
// static
void TaskQueue::TaskAsValueInto(const base::PendingTask& task,
base::debug::TracedValue* state) {
state->BeginDictionary();
state->SetString("posted_from", task.posted_from.ToString());
state->SetInteger("sequence_num", task.sequence_num);
state->SetBoolean("nestable", task.nestable);
state->SetBoolean("is_high_res", task.is_high_res);
state->SetDouble(
"delayed_run_time",
(task.delayed_run_time - base::TimeTicks()).InMicroseconds() / 1000.0L);
state->EndDictionary();
}
} // namespace internal
TaskQueueManager::TaskQueueManager(
size_t task_queue_count,
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
TaskQueueSelector* selector)
: main_task_runner_(main_task_runner),
selector_(selector),
pending_dowork_count_(0),
weak_factory_(this) {
DCHECK(main_task_runner->RunsTasksOnCurrentThread());
TRACE_EVENT_OBJECT_CREATED_WITH_ID(
TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "TaskQueueManager",
this);
task_queue_manager_weak_ptr_ = weak_factory_.GetWeakPtr();
for (size_t i = 0; i < task_queue_count; i++) {
scoped_refptr<internal::TaskQueue> queue(
make_scoped_refptr(new internal::TaskQueue(this)));
queues_.push_back(queue);
}
std::vector<const base::TaskQueue*> work_queues;
for (const auto& queue: queues_)
work_queues.push_back(&queue->work_queue());
selector_->RegisterWorkQueues(work_queues);
}
TaskQueueManager::~TaskQueueManager() {
TRACE_EVENT_OBJECT_DELETED_WITH_ID(
TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "TaskQueueManager",
this);
for (auto& queue : queues_)
queue->WillDeleteTaskQueueManager();
}
internal::TaskQueue* TaskQueueManager::Queue(size_t queue_index) const {
DCHECK_LT(queue_index, queues_.size());
return queues_[queue_index].get();
}
scoped_refptr<base::SingleThreadTaskRunner>
TaskQueueManager::TaskRunnerForQueue(size_t queue_index) const {
return Queue(queue_index);
}
bool TaskQueueManager::IsQueueEmpty(size_t queue_index) const {
internal::TaskQueue* queue = Queue(queue_index);
return queue->IsQueueEmpty();
}
void TaskQueueManager::SetAutoPump(size_t queue_index, bool auto_pump) {
main_thread_checker_.CalledOnValidThread();
internal::TaskQueue* queue = Queue(queue_index);
queue->SetAutoPump(auto_pump);
}
void TaskQueueManager::PumpQueue(size_t queue_index) {
main_thread_checker_.CalledOnValidThread();
internal::TaskQueue* queue = Queue(queue_index);
queue->PumpQueue();
}
bool TaskQueueManager::UpdateWorkQueues() {
// TODO(skyostil): This is not efficient when the number of queues grows very
// large due to the number of locks taken. Consider optimizing when we get
// there.
main_thread_checker_.CalledOnValidThread();
bool has_work = false;
for (auto& queue : queues_)
has_work |= queue->UpdateWorkQueue();
return has_work;
}
void TaskQueueManager::MaybePostDoWorkOnMainRunner() {
bool on_main_thread = main_task_runner_->BelongsToCurrentThread();
if (on_main_thread) {
// We only want one pending DoWork posted from the main thread, or we risk
// an explosion of pending DoWorks which could starve out everything else.
if (pending_dowork_count_ > 0) {
return;
}
pending_dowork_count_++;
}
main_task_runner_->PostTask(
FROM_HERE, Bind(&TaskQueueManager::DoWork, task_queue_manager_weak_ptr_,
on_main_thread));
}
void TaskQueueManager::DoWork(bool posted_from_main_thread) {
if (posted_from_main_thread) {
pending_dowork_count_--;
DCHECK_GE(pending_dowork_count_, 0);
}
main_thread_checker_.CalledOnValidThread();
if (!UpdateWorkQueues())
return;
size_t queue_index;
if (!SelectWorkQueueToService(&queue_index))
return;
MaybePostDoWorkOnMainRunner();
ProcessTaskFromWorkQueue(queue_index);
}
bool TaskQueueManager::SelectWorkQueueToService(size_t* out_queue_index) {
bool should_run = selector_->SelectWorkQueueToService(out_queue_index);
TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "TaskQueueManager", this,
AsValueWithSelectorResult(should_run, *out_queue_index));
return should_run;
}
void TaskQueueManager::DidQueueTask(base::PendingTask* pending_task) {
pending_task->sequence_num = task_sequence_num_.GetNext();
task_annotator_.DidQueueTask("TaskQueueManager::PostTask", *pending_task);
}
void TaskQueueManager::ProcessTaskFromWorkQueue(size_t queue_index) {
main_thread_checker_.CalledOnValidThread();
internal::TaskQueue* queue = Queue(queue_index);
base::PendingTask pending_task = queue->TakeTaskFromWorkQueue();
if (!pending_task.nestable) {
// Defer non-nestable work to the main task runner. NOTE these tasks can be
// arbitrarily delayed so the additional delay should not be a problem.
main_task_runner_->PostNonNestableTask(pending_task.posted_from,
pending_task.task);
} else {
task_annotator_.RunTask("TaskQueueManager::PostTask",
"TaskQueueManager::RunTask", pending_task);
}
}
bool TaskQueueManager::RunsTasksOnCurrentThread() const {
return main_task_runner_->RunsTasksOnCurrentThread();
}
bool TaskQueueManager::PostDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) {
DCHECK(delay > base::TimeDelta());
return main_task_runner_->PostDelayedTask(from_here, task, delay);
}
void TaskQueueManager::SetQueueName(size_t queue_index, const char* name) {
main_thread_checker_.CalledOnValidThread();
internal::TaskQueue* queue = Queue(queue_index);
queue->set_name(name);
}
scoped_refptr<base::debug::ConvertableToTraceFormat>
TaskQueueManager::AsValueWithSelectorResult(bool should_run,
size_t selected_queue) const {
main_thread_checker_.CalledOnValidThread();
scoped_refptr<base::debug::TracedValue> state =
new base::debug::TracedValue();
state->BeginArray("queues");
for (auto& queue : queues_)
queue->AsValueInto(state.get());
state->EndArray();
state->BeginDictionary("selector");
selector_->AsValueInto(state.get());
state->EndDictionary();
if (should_run)
state->SetInteger("selected_queue", selected_queue);
return state;
}
} // namespace content
|