blob: 40f217f0a44d631849aaae443555ee937a150db6 [file] [log] [blame]
Gabriel Charette3fb9e4f2018-05-18 21:34:431// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/task_scheduler/service_thread.h"
6
7#include "base/debug/alias.h"
8#include "base/task_scheduler/post_task.h"
9#include "base/task_scheduler/task_tracker.h"
10#include "base/task_scheduler/task_traits.h"
11#include "base/time/time.h"
12
13namespace base {
14namespace internal {
15
16ServiceThread::ServiceThread(const TaskTracker* task_tracker)
17 : Thread("TaskSchedulerServiceThread"), task_tracker_(task_tracker) {}
18
19void ServiceThread::Init() {
20 if (task_tracker_) {
21 heartbeat_latency_timer_.Start(
22 FROM_HERE, TimeDelta::FromSeconds(5),
23 BindRepeating(&ServiceThread::PerformHeartbeatLatencyReport,
24 Unretained(this)));
25 }
26}
27
28NOINLINE void ServiceThread::Run(RunLoop* run_loop) {
29 const int line_number = __LINE__;
30 Thread::Run(run_loop);
31 base::debug::Alias(&line_number);
32}
33
34void ServiceThread::PerformHeartbeatLatencyReport() const {
35 static constexpr TaskTraits kReportedTraits[] = {
36 {TaskPriority::BACKGROUND}, {TaskPriority::BACKGROUND, MayBlock()},
37 {TaskPriority::USER_VISIBLE}, {TaskPriority::USER_VISIBLE, MayBlock()},
38 {TaskPriority::USER_BLOCKING}, {TaskPriority::USER_BLOCKING, MayBlock()}};
39
40 for (auto& traits : kReportedTraits) {
41 // Post through the static API to time the full stack. Use a new Now() for
42 // every set of traits in case PostTaskWithTraits() itself is slow.
43 base::PostTaskWithTraits(
44 FROM_HERE, traits,
45 BindOnce(&TaskTracker::RecordLatencyHistogram,
46 Unretained(task_tracker_),
47 TaskTracker::LatencyHistogramType::HEARTBEAT_LATENCY, traits,
48 TimeTicks::Now()));
49 }
50}
51
52} // namespace internal
53} // namespace base