1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 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_DEBUG_TASK_ANNOTATOR_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_DEBUG_TASK_ANNOTATOR_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <stdint.h> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h" 11*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 12*635a8641SAndroid Build Coastguard Worker 13*635a8641SAndroid Build Coastguard Worker namespace base { 14*635a8641SAndroid Build Coastguard Worker struct PendingTask; 15*635a8641SAndroid Build Coastguard Worker namespace debug { 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker // Implements common debug annotations for posted tasks. This includes data 18*635a8641SAndroid Build Coastguard Worker // such as task origins, queueing durations and memory usage. 19*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT TaskAnnotator { 20*635a8641SAndroid Build Coastguard Worker public: 21*635a8641SAndroid Build Coastguard Worker class ObserverForTesting { 22*635a8641SAndroid Build Coastguard Worker public: 23*635a8641SAndroid Build Coastguard Worker virtual ~ObserverForTesting() = default; 24*635a8641SAndroid Build Coastguard Worker // Invoked just before RunTask() in the scope in which the task is about to 25*635a8641SAndroid Build Coastguard Worker // be executed. 26*635a8641SAndroid Build Coastguard Worker virtual void BeforeRunTask(const PendingTask* pending_task) = 0; 27*635a8641SAndroid Build Coastguard Worker }; 28*635a8641SAndroid Build Coastguard Worker 29*635a8641SAndroid Build Coastguard Worker TaskAnnotator(); 30*635a8641SAndroid Build Coastguard Worker ~TaskAnnotator(); 31*635a8641SAndroid Build Coastguard Worker 32*635a8641SAndroid Build Coastguard Worker // Called to indicate that a task is about to be queued to run in the future, 33*635a8641SAndroid Build Coastguard Worker // giving one last chance for this TaskAnnotator to add metadata to 34*635a8641SAndroid Build Coastguard Worker // |pending_task| before it is moved into the queue. |queue_function| is used 35*635a8641SAndroid Build Coastguard Worker // as the trace flow event name. |queue_function| can be null if the caller 36*635a8641SAndroid Build Coastguard Worker // doesn't want trace flow events logged to toplevel.flow. 37*635a8641SAndroid Build Coastguard Worker void WillQueueTask(const char* queue_function, PendingTask* pending_task); 38*635a8641SAndroid Build Coastguard Worker 39*635a8641SAndroid Build Coastguard Worker // Run a previously queued task. |queue_function| should match what was 40*635a8641SAndroid Build Coastguard Worker // passed into |DidQueueTask| for this task. 41*635a8641SAndroid Build Coastguard Worker void RunTask(const char* queue_function, PendingTask* pending_task); 42*635a8641SAndroid Build Coastguard Worker 43*635a8641SAndroid Build Coastguard Worker // Creates a process-wide unique ID to represent this task in trace events. 44*635a8641SAndroid Build Coastguard Worker // This will be mangled with a Process ID hash to reduce the likelyhood of 45*635a8641SAndroid Build Coastguard Worker // colliding with TaskAnnotator pointers on other processes. Callers may use 46*635a8641SAndroid Build Coastguard Worker // this when generating their own flow events (i.e. when passing 47*635a8641SAndroid Build Coastguard Worker // |queue_function == nullptr| in above methods). 48*635a8641SAndroid Build Coastguard Worker uint64_t GetTaskTraceID(const PendingTask& task) const; 49*635a8641SAndroid Build Coastguard Worker 50*635a8641SAndroid Build Coastguard Worker private: 51*635a8641SAndroid Build Coastguard Worker friend class TaskAnnotatorBacktraceIntegrationTest; 52*635a8641SAndroid Build Coastguard Worker 53*635a8641SAndroid Build Coastguard Worker // Registers an ObserverForTesting that will be invoked by all TaskAnnotators' 54*635a8641SAndroid Build Coastguard Worker // RunTask(). This registration and the implementation of BeforeRunTask() are 55*635a8641SAndroid Build Coastguard Worker // responsible to ensure thread-safety. 56*635a8641SAndroid Build Coastguard Worker static void RegisterObserverForTesting(ObserverForTesting* observer); 57*635a8641SAndroid Build Coastguard Worker static void ClearObserverForTesting(); 58*635a8641SAndroid Build Coastguard Worker 59*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(TaskAnnotator); 60*635a8641SAndroid Build Coastguard Worker }; 61*635a8641SAndroid Build Coastguard Worker 62*635a8641SAndroid Build Coastguard Worker } // namespace debug 63*635a8641SAndroid Build Coastguard Worker } // namespace base 64*635a8641SAndroid Build Coastguard Worker 65*635a8641SAndroid Build Coastguard Worker #endif // BASE_DEBUG_TASK_ANNOTATOR_H_ 66