xref: /aosp_15_r20/external/cronet/base/task/common/task_annotator.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
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_TASK_COMMON_TASK_ANNOTATOR_H_
6 #define BASE_TASK_COMMON_TASK_ANNOTATOR_H_
7 
8 #include <stdint.h>
9 
10 #include <string_view>
11 
12 #include "base/auto_reset.h"
13 #include "base/base_export.h"
14 #include "base/memory/raw_ptr_exclusion.h"
15 #include "base/pending_task.h"
16 #include "base/time/tick_clock.h"
17 #include "base/trace_event/base_tracing.h"
18 
19 namespace base {
20 
21 // Constant used to measure which long-running tasks should be traced.
22 constexpr TimeDelta kMaxTaskDurationTimeDelta = Milliseconds(4);
23 
24 // Implements common debug annotations for posted tasks. This includes data
25 // such as task origins, IPC message contexts, queueing durations and memory
26 // usage.
27 class BASE_EXPORT TaskAnnotator {
28  public:
29   class ObserverForTesting {
30    public:
31     // Invoked just before RunTask() in the scope in which the task is about to
32     // be executed.
33     virtual void BeforeRunTask(const PendingTask* pending_task) = 0;
34   };
35 
36   // This is used to set the |ipc_hash| field for PendingTasks. It is intended
37   // to be used only from within generated IPC handler dispatch code.
38   class ScopedSetIpcHash;
39 
40   // This is used to track long-running browser-UI tasks. It is intended to
41   // be used for low-overhead logging to produce longer traces, particularly to
42   // help the scroll jank reduction effort.
43   class LongTaskTracker;
44 
45   static const PendingTask* CurrentTaskForThread();
46 
47   static void OnIPCReceived(const char* interface_name,
48                             uint32_t (*method_info)(),
49                             bool is_response);
50 
51   static void MarkCurrentTaskAsInterestingForTracing();
52 
53   TaskAnnotator();
54 
55   TaskAnnotator(const TaskAnnotator&) = delete;
56   TaskAnnotator& operator=(const TaskAnnotator&) = delete;
57 
58   ~TaskAnnotator();
59 
60   // Called to indicate that a task is about to be queued to run in the future,
61   // giving one last chance for this TaskAnnotator to add metadata to
62   // |pending_task| before it is moved into the queue.
63   void WillQueueTask(perfetto::StaticString trace_event_name,
64                      TaskMetadata* pending_task);
65 
66   // Creates a process-wide unique ID to represent this task in trace events.
67   // This will be mangled with a Process ID hash to reduce the likelyhood of
68   // colliding with TaskAnnotator pointers on other processes. Callers may use
69   // this when generating their own flow events (i.e. when passing
70   // |queue_function == nullptr| in above methods).
71   uint64_t GetTaskTraceID(const TaskMetadata& task) const;
72 
73   // Run the given task, emitting the toplevel trace event and additional
74   // trace event arguments. Like for TRACE_EVENT macros, all of the arguments
75   // are used (i.e. lambdas are invoked) before this function exits, so it's
76   // safe to pass reference-capturing lambdas here.
77   template <typename... Args>
RunTask(perfetto::StaticString event_name,PendingTask & pending_task,Args &&...args)78   void RunTask(perfetto::StaticString event_name,
79                PendingTask& pending_task,
80                Args&&... args) {
81     TRACE_EVENT(
82         "toplevel", event_name,
83         [&](perfetto::EventContext& ctx) {
84           EmitTaskLocation(ctx, pending_task);
85           MaybeEmitDelayAndPolicy(ctx, pending_task);
86           MaybeEmitIncomingTaskFlow(ctx, pending_task);
87           MaybeEmitIPCHash(ctx, pending_task);
88         },
89         std::forward<Args>(args)...);
90     RunTaskImpl(pending_task);
91   }
92 
93  private:
94   friend class TaskAnnotatorBacktraceIntegrationTest;
95 
96   // Run a previously queued task.
97   NOT_TAIL_CALLED void RunTaskImpl(PendingTask& pending_task);
98 
99   // Registers an ObserverForTesting that will be invoked by all TaskAnnotators'
100   // RunTask(). This registration and the implementation of BeforeRunTask() are
101   // responsible to ensure thread-safety.
102   static void RegisterObserverForTesting(ObserverForTesting* observer);
103   static void ClearObserverForTesting();
104 
105 #if BUILDFLAG(ENABLE_BASE_TRACING)
106   // TRACE_EVENT argument helper, writing the task location data into
107   // EventContext.
108   static void EmitTaskLocation(perfetto::EventContext& ctx,
109                                const PendingTask& task);
110   static void MaybeEmitDelayAndPolicy(perfetto::EventContext& ctx,
111                                       const PendingTask& task);
112 
113   // TRACE_EVENT argument helper, writing the incoming task flow information
114   // into EventContext if toplevel.flow category is enabled.
115   void MaybeEmitIncomingTaskFlow(perfetto::EventContext& ctx,
116                                  const PendingTask& task) const;
117 
118   void MaybeEmitIPCHash(perfetto::EventContext& ctx,
119                         const PendingTask& task) const;
120 #endif  //  BUILDFLAG(ENABLE_BASE_TRACING)
121 };
122 
123 class BASE_EXPORT [[maybe_unused, nodiscard]] TaskAnnotator::ScopedSetIpcHash {
124  public:
125   explicit ScopedSetIpcHash(uint32_t ipc_hash);
126 
127   // Compile-time-const string identifying the current IPC context. Not always
128   // available due to binary size constraints, so IPC hash might be set instead.
129   explicit ScopedSetIpcHash(const char* ipc_interface_name);
130 
131   ScopedSetIpcHash(const ScopedSetIpcHash&) = delete;
132   ScopedSetIpcHash& operator=(const ScopedSetIpcHash&) = delete;
133 
134   ~ScopedSetIpcHash();
135 
GetIpcHash()136   uint32_t GetIpcHash() const { return ipc_hash_; }
GetIpcInterfaceName()137   const char* GetIpcInterfaceName() const { return ipc_interface_name_; }
138 
139   static uint32_t MD5HashMetricName(std::string_view name);
140 
141  private:
142   ScopedSetIpcHash(uint32_t ipc_hash, const char* ipc_interface_name);
143 
144   const AutoReset<ScopedSetIpcHash*> resetter_;
145   uint32_t ipc_hash_;
146   const char* ipc_interface_name_;
147 };
148 
149 class BASE_EXPORT [[maybe_unused, nodiscard]] TaskAnnotator::LongTaskTracker {
150  public:
151   explicit LongTaskTracker(const TickClock* tick_clock,
152                            PendingTask& pending_task,
153                            TaskAnnotator* task_annotator);
154 
155   LongTaskTracker(const LongTaskTracker&) = delete;
156 
157   ~LongTaskTracker();
158 
159   void SetIpcDetails(const char* interface_name,
160                      uint32_t (*method_info)(),
161                      bool is_response);
162 
163   void MaybeTraceInterestingTaskDetails();
164 
165   // In long-task tracking, not every task (including its queue time) will be
166   // recorded in a trace. If a particular task + queue time needs to be
167   // recorded, flag it explicitly. For example, input tasks are required for
168   // calculating scroll jank metrics.
169   bool is_interesting_task = false;
170 
171  private:
172   void EmitReceivedIPCDetails(perfetto::EventContext& ctx);
173 
174   const AutoReset<LongTaskTracker*> resetter_;
175 
176   // For tracking task duration.
177   //
178   // RAW_PTR_EXCLUSION: Performance reasons: based on analysis of sampling
179   // profiler data (TaskAnnotator::LongTaskTracker::~LongTaskTracker).
180   RAW_PTR_EXCLUSION const TickClock* tick_clock_;  // Not owned.
181   TimeTicks task_start_time_;
182   TimeTicks task_end_time_;
183 
184   // Tracing variables.
185 
186   // Use this to ensure that tracing and NowTicks() are not called
187   // unnecessarily.
188   bool is_tracing_;
189   const char* ipc_interface_name_ = nullptr;
190   uint32_t ipc_hash_ = 0;
191 
192   // IPC method info to retrieve IPC hash and method address from trace, if
193   // known. Note that this will not compile in the Native client.
194   uint32_t (*ipc_method_info_)();
195   bool is_response_ = false;
196   // RAW_PTR_EXCLUSION: Performance reasons: based on analysis of sampling
197   // profiler data (TaskAnnotator::LongTaskTracker::~LongTaskTracker).
198   [[maybe_unused]] RAW_PTR_EXCLUSION PendingTask& pending_task_;
199   [[maybe_unused]] RAW_PTR_EXCLUSION TaskAnnotator* task_annotator_;
200 };
201 
202 }  // namespace base
203 
204 #endif  // BASE_TASK_COMMON_TASK_ANNOTATOR_H_
205