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