1*635a8641SAndroid Build Coastguard Worker // Copyright 2018 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TASK_SCHEDULER_SERVICE_THREAD_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_TASK_SCHEDULER_SERVICE_THREAD_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h" 9*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 10*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread.h" 11*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h" 12*635a8641SAndroid Build Coastguard Worker #include "base/timer/timer.h" 13*635a8641SAndroid Build Coastguard Worker 14*635a8641SAndroid Build Coastguard Worker namespace base { 15*635a8641SAndroid Build Coastguard Worker namespace internal { 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker class TaskTracker; 18*635a8641SAndroid Build Coastguard Worker 19*635a8641SAndroid Build Coastguard Worker // The TaskScheduler's ServiceThread is a mostly idle thread that is responsible 20*635a8641SAndroid Build Coastguard Worker // for handling async events (e.g. delayed tasks and async I/O). Its role is to 21*635a8641SAndroid Build Coastguard Worker // merely forward such events to their destination (hence staying mostly idle 22*635a8641SAndroid Build Coastguard Worker // and highly responsive). 23*635a8641SAndroid Build Coastguard Worker // It aliases Thread::Run() to enforce that ServiceThread::Run() be on the stack 24*635a8641SAndroid Build Coastguard Worker // and make it easier to identify the service thread in stack traces. 25*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT ServiceThread : public Thread { 26*635a8641SAndroid Build Coastguard Worker public: 27*635a8641SAndroid Build Coastguard Worker // Constructs a ServiceThread which will report latency metrics through 28*635a8641SAndroid Build Coastguard Worker // |task_tracker| if non-null. In that case, this ServiceThread will assume a 29*635a8641SAndroid Build Coastguard Worker // registered TaskScheduler instance and that |task_tracker| will outlive this 30*635a8641SAndroid Build Coastguard Worker // ServiceThread. 31*635a8641SAndroid Build Coastguard Worker explicit ServiceThread(const TaskTracker* task_tracker); 32*635a8641SAndroid Build Coastguard Worker 33*635a8641SAndroid Build Coastguard Worker // Overrides the default interval at which |heartbeat_latency_timer_| fires. 34*635a8641SAndroid Build Coastguard Worker // Call this with a |heartbeat| of zero to undo the override. 35*635a8641SAndroid Build Coastguard Worker // Must not be called while the ServiceThread is running. 36*635a8641SAndroid Build Coastguard Worker static void SetHeartbeatIntervalForTesting(TimeDelta heartbeat); 37*635a8641SAndroid Build Coastguard Worker 38*635a8641SAndroid Build Coastguard Worker private: 39*635a8641SAndroid Build Coastguard Worker // Thread: 40*635a8641SAndroid Build Coastguard Worker void Init() override; 41*635a8641SAndroid Build Coastguard Worker void Run(RunLoop* run_loop) override; 42*635a8641SAndroid Build Coastguard Worker 43*635a8641SAndroid Build Coastguard Worker // Kicks off a single async task which will record a histogram on the latency 44*635a8641SAndroid Build Coastguard Worker // of a randomly chosen set of TaskTraits. 45*635a8641SAndroid Build Coastguard Worker void PerformHeartbeatLatencyReport() const; 46*635a8641SAndroid Build Coastguard Worker 47*635a8641SAndroid Build Coastguard Worker const TaskTracker* const task_tracker_; 48*635a8641SAndroid Build Coastguard Worker 49*635a8641SAndroid Build Coastguard Worker // Fires a recurring heartbeat task to record latency histograms which are 50*635a8641SAndroid Build Coastguard Worker // independent from any execution sequence. This is done on the service thread 51*635a8641SAndroid Build Coastguard Worker // to avoid all external dependencies (even main thread). 52*635a8641SAndroid Build Coastguard Worker base::RepeatingTimer heartbeat_latency_timer_; 53*635a8641SAndroid Build Coastguard Worker 54*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(ServiceThread); 55*635a8641SAndroid Build Coastguard Worker }; 56*635a8641SAndroid Build Coastguard Worker 57*635a8641SAndroid Build Coastguard Worker } // namespace internal 58*635a8641SAndroid Build Coastguard Worker } // namespace base 59*635a8641SAndroid Build Coastguard Worker 60*635a8641SAndroid Build Coastguard Worker #endif // BASE_TASK_SCHEDULER_SERVICE_THREAD_H_ 61