summaryrefslogtreecommitdiffstats
path: root/components/scheduler/child/scheduler_helper.cc
blob: 19e8d2b65120525765527af4775d9564d2590f51 (plain)
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
410
411
// Copyright 2015 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 "components/scheduler/child/scheduler_helper.h"

#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_event_argument.h"
#include "components/scheduler/child/nestable_single_thread_task_runner.h"
#include "components/scheduler/child/prioritizing_task_queue_selector.h"
#include "components/scheduler/child/time_source.h"

namespace scheduler {

SchedulerHelper::SchedulerHelper(
    scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner,
    SchedulerHelperDelegate* scheduler_helper_delegate,
    const char* tracing_category,
    const char* disabled_by_default_tracing_category,
    const char* idle_period_tracing_name,
    size_t total_task_queue_count,
    base::TimeDelta required_quiescence_duration_before_long_idle_period)
    : task_queue_selector_(new PrioritizingTaskQueueSelector()),
      task_queue_manager_(
          new TaskQueueManager(total_task_queue_count,
                               main_task_runner,
                               task_queue_selector_.get(),
                               disabled_by_default_tracing_category)),
      idle_period_state_(IdlePeriodState::NOT_IN_IDLE_PERIOD),
      scheduler_helper_delegate_(scheduler_helper_delegate),
      control_task_runner_(
          task_queue_manager_->TaskRunnerForQueue(QueueId::CONTROL_TASK_QUEUE)),
      control_task_after_wakeup_runner_(task_queue_manager_->TaskRunnerForQueue(
          QueueId::CONTROL_TASK_AFTER_WAKEUP_QUEUE)),
      default_task_runner_(
          task_queue_manager_->TaskRunnerForQueue(QueueId::DEFAULT_TASK_QUEUE)),
      quiescence_monitored_task_queue_mask_(
          ((1ull << total_task_queue_count) - 1ull) &
          ~(1ull << QueueId::IDLE_TASK_QUEUE) &
          ~(1ull << QueueId::CONTROL_TASK_QUEUE) &
          ~(1ull << QueueId::CONTROL_TASK_AFTER_WAKEUP_QUEUE)),
      required_quiescence_duration_before_long_idle_period_(
          required_quiescence_duration_before_long_idle_period),
      time_source_(new TimeSource),
      tracing_category_(tracing_category),
      disabled_by_default_tracing_category_(
          disabled_by_default_tracing_category),
      idle_period_tracing_name_(idle_period_tracing_name),
      weak_factory_(this) {
  DCHECK_GE(total_task_queue_count,
            static_cast<size_t>(QueueId::TASK_QUEUE_COUNT));
  weak_scheduler_ptr_ = weak_factory_.GetWeakPtr();
  end_idle_period_closure_.Reset(
      base::Bind(&SchedulerHelper::EndIdlePeriod, weak_scheduler_ptr_));
  enable_next_long_idle_period_closure_.Reset(
      base::Bind(&SchedulerHelper::EnableLongIdlePeriod, weak_scheduler_ptr_));
  enable_next_long_idle_period_after_wakeup_closure_.Reset(base::Bind(
      &SchedulerHelper::EnableLongIdlePeriodAfterWakeup, weak_scheduler_ptr_));

  idle_task_runner_ = make_scoped_refptr(new SingleThreadIdleTaskRunner(
      task_queue_manager_->TaskRunnerForQueue(QueueId::IDLE_TASK_QUEUE),
      control_task_after_wakeup_runner_,
      base::Bind(&SchedulerHelper::CurrentIdleTaskDeadlineCallback,
                 weak_scheduler_ptr_),
      tracing_category));

  task_queue_selector_->SetQueuePriority(
      QueueId::CONTROL_TASK_QUEUE,
      PrioritizingTaskQueueSelector::CONTROL_PRIORITY);

  task_queue_selector_->SetQueuePriority(
      QueueId::CONTROL_TASK_AFTER_WAKEUP_QUEUE,
      PrioritizingTaskQueueSelector::CONTROL_PRIORITY);
  task_queue_manager_->SetPumpPolicy(
      QueueId::CONTROL_TASK_AFTER_WAKEUP_QUEUE,
      TaskQueueManager::PumpPolicy::AFTER_WAKEUP);

  task_queue_selector_->DisableQueue(QueueId::IDLE_TASK_QUEUE);
  task_queue_manager_->SetPumpPolicy(QueueId::IDLE_TASK_QUEUE,
                                     TaskQueueManager::PumpPolicy::MANUAL);

  for (size_t i = 0; i < TASK_QUEUE_COUNT; i++) {
    task_queue_manager_->SetQueueName(
        i, TaskQueueIdToString(static_cast<QueueId>(i)));
  }

  // TODO(skyostil): Increase this to 4 (crbug.com/444764).
  task_queue_manager_->SetWorkBatchSize(1);
}

SchedulerHelper::~SchedulerHelper() {
}

SchedulerHelper::SchedulerHelperDelegate::SchedulerHelperDelegate() {
}

SchedulerHelper::SchedulerHelperDelegate::~SchedulerHelperDelegate() {
}

void SchedulerHelper::Shutdown() {
  CheckOnValidThread();
  task_queue_manager_.reset();
}

scoped_refptr<base::SingleThreadTaskRunner>
SchedulerHelper::DefaultTaskRunner() {
  CheckOnValidThread();
  return default_task_runner_;
}

scoped_refptr<SingleThreadIdleTaskRunner> SchedulerHelper::IdleTaskRunner() {
  CheckOnValidThread();
  return idle_task_runner_;
}

scoped_refptr<base::SingleThreadTaskRunner>
SchedulerHelper::ControlTaskRunner() {
  return control_task_runner_;
}

void SchedulerHelper::CurrentIdleTaskDeadlineCallback(
    base::TimeTicks* deadline_out) const {
  CheckOnValidThread();
  *deadline_out = idle_period_deadline_;
}

SchedulerHelper::IdlePeriodState SchedulerHelper::ComputeNewLongIdlePeriodState(
    const base::TimeTicks now,
    base::TimeDelta* next_long_idle_period_delay_out) {
  CheckOnValidThread();

  if (!scheduler_helper_delegate_->CanEnterLongIdlePeriod(
          now, next_long_idle_period_delay_out)) {
    return IdlePeriodState::NOT_IN_IDLE_PERIOD;
  }

  base::TimeTicks next_pending_delayed_task =
      task_queue_manager_->NextPendingDelayedTaskRunTime();
  base::TimeDelta max_long_idle_period_duration =
      base::TimeDelta::FromMilliseconds(kMaximumIdlePeriodMillis);
  base::TimeDelta long_idle_period_duration;
  if (next_pending_delayed_task.is_null()) {
    long_idle_period_duration = max_long_idle_period_duration;
  } else {
    // Limit the idle period duration to be before the next pending task.
    long_idle_period_duration = std::min(next_pending_delayed_task - now,
                                         max_long_idle_period_duration);
  }

  if (long_idle_period_duration > base::TimeDelta()) {
    *next_long_idle_period_delay_out = long_idle_period_duration;
    return long_idle_period_duration == max_long_idle_period_duration
               ? IdlePeriodState::IN_LONG_IDLE_PERIOD_WITH_MAX_DEADLINE
               : IdlePeriodState::IN_LONG_IDLE_PERIOD;
  } else {
    // If we can't start the idle period yet then try again after wakeup.
    *next_long_idle_period_delay_out = base::TimeDelta::FromMilliseconds(
        kRetryEnableLongIdlePeriodDelayMillis);
    return IdlePeriodState::NOT_IN_IDLE_PERIOD;
  }
}

bool SchedulerHelper::ShouldWaitForQuiescence() {
  CheckOnValidThread();

  if (!task_queue_manager_)
    return false;

  if (required_quiescence_duration_before_long_idle_period_ ==
      base::TimeDelta())
    return false;

  uint64 task_queues_run_since_last_check_bitmap =
      task_queue_manager_->GetAndClearTaskWasRunOnQueueBitmap() &
      quiescence_monitored_task_queue_mask_;

  TRACE_EVENT1(disabled_by_default_tracing_category_, "ShouldWaitForQuiescence",
               "task_queues_run_since_last_check_bitmap",
               task_queues_run_since_last_check_bitmap);

  // If anything was run on the queues we care about, then we're not quiescent
  // and we should wait.
  return task_queues_run_since_last_check_bitmap != 0;
}

void SchedulerHelper::EnableLongIdlePeriod() {
  TRACE_EVENT0(disabled_by_default_tracing_category_, "EnableLongIdlePeriod");
  CheckOnValidThread();

  // End any previous idle period.
  EndIdlePeriod();

  if (ShouldWaitForQuiescence()) {
    control_task_runner_->PostDelayedTask(
        FROM_HERE, enable_next_long_idle_period_closure_.callback(),
        required_quiescence_duration_before_long_idle_period_);
    scheduler_helper_delegate_->IsNotQuiescent();
    return;
  }

  base::TimeTicks now(Now());
  base::TimeDelta next_long_idle_period_delay;
  IdlePeriodState new_idle_period_state =
      ComputeNewLongIdlePeriodState(now, &next_long_idle_period_delay);
  if (IsInIdlePeriod(new_idle_period_state)) {
    StartIdlePeriod(new_idle_period_state, now,
                    now + next_long_idle_period_delay, false);
  }

  if (task_queue_manager_->IsQueueEmpty(QueueId::IDLE_TASK_QUEUE)) {
    // If there are no current idle tasks then post the call to initiate the
    // next idle for execution after wakeup (at which point after-wakeup idle
    // tasks might be eligible to run or more idle tasks posted).
    control_task_after_wakeup_runner_->PostDelayedTask(
        FROM_HERE,
        enable_next_long_idle_period_after_wakeup_closure_.callback(),
        next_long_idle_period_delay);
  } else {
    // Otherwise post on the normal control task queue.
    control_task_runner_->PostDelayedTask(
        FROM_HERE, enable_next_long_idle_period_closure_.callback(),
        next_long_idle_period_delay);
  }
}

void SchedulerHelper::EnableLongIdlePeriodAfterWakeup() {
  TRACE_EVENT0(disabled_by_default_tracing_category_,
               "EnableLongIdlePeriodAfterWakeup");
  CheckOnValidThread();

  if (IsInIdlePeriod(idle_period_state_)) {
    // Since we were asleep until now, end the async idle period trace event at
    // the time when it would have ended were we awake.
    TRACE_EVENT_ASYNC_END_WITH_TIMESTAMP0(
        tracing_category_, idle_period_tracing_name_, this,
        std::min(idle_period_deadline_, Now()).ToInternalValue());
    idle_period_state_ = IdlePeriodState::ENDING_LONG_IDLE_PERIOD;
    EndIdlePeriod();
  }

  // Post a task to initiate the next long idle period rather than calling it
  // directly to allow all pending PostIdleTaskAfterWakeup tasks to get enqueued
  // on the idle task queue before the next idle period starts so they are
  // eligible to be run during the new idle period.
  control_task_runner_->PostTask(
      FROM_HERE, enable_next_long_idle_period_closure_.callback());
}

void SchedulerHelper::StartIdlePeriod(IdlePeriodState new_state,
                                      base::TimeTicks now,
                                      base::TimeTicks idle_period_deadline,
                                      bool post_end_idle_period) {
  DCHECK_GT(idle_period_deadline, now);
  TRACE_EVENT_ASYNC_BEGIN0(tracing_category_, idle_period_tracing_name_, this);
  CheckOnValidThread();
  DCHECK(IsInIdlePeriod(new_state));

  task_queue_selector_->EnableQueue(
      QueueId::IDLE_TASK_QUEUE,
      PrioritizingTaskQueueSelector::BEST_EFFORT_PRIORITY);
  task_queue_manager_->PumpQueue(QueueId::IDLE_TASK_QUEUE);
  idle_period_state_ = new_state;

  idle_period_deadline_ = idle_period_deadline;
  if (post_end_idle_period) {
    control_task_runner_->PostDelayedTask(FROM_HERE,
                                          end_idle_period_closure_.callback(),
                                          idle_period_deadline_ - now);
  }
}

void SchedulerHelper::EndIdlePeriod() {
  CheckOnValidThread();

  end_idle_period_closure_.Cancel();
  enable_next_long_idle_period_closure_.Cancel();
  enable_next_long_idle_period_after_wakeup_closure_.Cancel();

  // If we weren't already within an idle period then early-out.
  if (!IsInIdlePeriod(idle_period_state_))
    return;

  // If we are in the ENDING_LONG_IDLE_PERIOD state we have already logged the
  // trace event.
  if (idle_period_state_ != IdlePeriodState::ENDING_LONG_IDLE_PERIOD) {
    bool is_tracing;
    TRACE_EVENT_CATEGORY_GROUP_ENABLED(tracing_category_, &is_tracing);
    if (is_tracing && !idle_period_deadline_.is_null() &&
        base::TimeTicks::Now() > idle_period_deadline_) {
      TRACE_EVENT_ASYNC_STEP_INTO_WITH_TIMESTAMP0(
          tracing_category_, idle_period_tracing_name_, this, "DeadlineOverrun",
          idle_period_deadline_.ToInternalValue());
    }
    TRACE_EVENT_ASYNC_END0(tracing_category_, idle_period_tracing_name_, this);
  }

  task_queue_selector_->DisableQueue(QueueId::IDLE_TASK_QUEUE);
  idle_period_state_ = IdlePeriodState::NOT_IN_IDLE_PERIOD;
  idle_period_deadline_ = base::TimeTicks();
}

// static
bool SchedulerHelper::IsInIdlePeriod(IdlePeriodState state) {
  return state != IdlePeriodState::NOT_IN_IDLE_PERIOD;
}

bool SchedulerHelper::CanExceedIdleDeadlineIfRequired() const {
  TRACE_EVENT0(tracing_category_, "CanExceedIdleDeadlineIfRequired");
  CheckOnValidThread();
  return idle_period_state_ ==
         IdlePeriodState::IN_LONG_IDLE_PERIOD_WITH_MAX_DEADLINE;
}

void SchedulerHelper::SetTimeSourceForTesting(
    scoped_ptr<TimeSource> time_source) {
  CheckOnValidThread();
  time_source_ = time_source.Pass();
}

void SchedulerHelper::SetWorkBatchSizeForTesting(size_t work_batch_size) {
  CheckOnValidThread();
  task_queue_manager_->SetWorkBatchSize(work_batch_size);
}

TaskQueueManager* SchedulerHelper::GetTaskQueueManagerForTesting() {
  CheckOnValidThread();
  return task_queue_manager_.get();
}

base::TimeTicks SchedulerHelper::Now() const {
  return time_source_->Now();
}

SchedulerHelper::IdlePeriodState SchedulerHelper::SchedulerIdlePeriodState()
    const {
  return idle_period_state_;
}

PrioritizingTaskQueueSelector* SchedulerHelper::SchedulerTaskQueueSelector()
    const {
  return task_queue_selector_.get();
}

scoped_refptr<base::SingleThreadTaskRunner> SchedulerHelper::TaskRunnerForQueue(
    size_t queue_index) const {
  CheckOnValidThread();
  return task_queue_manager_->TaskRunnerForQueue(queue_index);
}

void SchedulerHelper::SetQueueName(size_t queue_index, const char* name) {
  CheckOnValidThread();
  task_queue_manager_->SetQueueName(queue_index, name);
}

bool SchedulerHelper::IsQueueEmpty(size_t queue_index) const {
  CheckOnValidThread();
  return task_queue_manager_->IsQueueEmpty(queue_index);
}

// static
const char* SchedulerHelper::TaskQueueIdToString(QueueId queue_id) {
  switch (queue_id) {
    case DEFAULT_TASK_QUEUE:
      return "default_tq";
    case IDLE_TASK_QUEUE:
      return "idle_tq";
    case CONTROL_TASK_QUEUE:
      return "control_tq";
    case CONTROL_TASK_AFTER_WAKEUP_QUEUE:
      return "control_after_wakeup_tq";
    default:
      NOTREACHED();
      return nullptr;
  }
}

// static
const char* SchedulerHelper::IdlePeriodStateToString(
    IdlePeriodState idle_period_state) {
  switch (idle_period_state) {
    case IdlePeriodState::NOT_IN_IDLE_PERIOD:
      return "not_in_idle_period";
    case IdlePeriodState::IN_SHORT_IDLE_PERIOD:
      return "in_short_idle_period";
    case IdlePeriodState::IN_LONG_IDLE_PERIOD:
      return "in_long_idle_period";
    case IdlePeriodState::IN_LONG_IDLE_PERIOD_WITH_MAX_DEADLINE:
      return "in_long_idle_period_with_max_deadline";
    case IdlePeriodState::ENDING_LONG_IDLE_PERIOD:
      return "ending_long_idle_period";
    default:
      NOTREACHED();
      return nullptr;
  }
}

void SchedulerHelper::AddTaskObserver(
    base::MessageLoop::TaskObserver* task_observer) {
  CheckOnValidThread();
  if (task_queue_manager_)
    task_queue_manager_->AddTaskObserver(task_observer);
}

void SchedulerHelper::RemoveTaskObserver(
    base::MessageLoop::TaskObserver* task_observer) {
  CheckOnValidThread();
  if (task_queue_manager_)
    task_queue_manager_->RemoveTaskObserver(task_observer);
}

}  // namespace scheduler