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 #include "base/task/common/task_annotator.h"
6
7 #include <stdint.h>
8
9 #include <algorithm>
10 #include <array>
11 #include <string_view>
12
13 #include "base/auto_reset.h"
14 #include "base/check_op.h"
15 #include "base/compiler_specific.h"
16 #include "base/containers/span.h"
17 #include "base/debug/alias.h"
18 #include "base/hash/md5.h"
19 #include "base/logging.h"
20 #include "base/metrics/metrics_hashes.h"
21 #include "base/ranges/algorithm.h"
22 #include "base/time/time.h"
23 #include "base/trace_event/base_tracing.h"
24 #include "base/tracing_buildflags.h"
25 #include "third_party/abseil-cpp/absl/base/attributes.h"
26
27 #if BUILDFLAG(ENABLE_BASE_TRACING)
28 #include "third_party/perfetto/protos/perfetto/trace/track_event/chrome_mojo_event_info.pbzero.h" // nogncheck
29 #endif
30
31 namespace base {
32
33 namespace {
34
35 TaskAnnotator::ObserverForTesting* g_task_annotator_observer = nullptr;
36
37 // The PendingTask currently in progress on each thread. Used to allow creating
38 // a breadcrumb of program counters on the stack to help identify a task's
39 // origin in crashes.
40 ABSL_CONST_INIT thread_local PendingTask* current_pending_task = nullptr;
41
42 // Scoped IPC-related data (IPC hash and/or IPC interface name). IPC hash or
43 // interface name can be known before the associated task object is created;
44 // thread-local so that this data can be affixed to the associated task.
45 ABSL_CONST_INIT thread_local TaskAnnotator::ScopedSetIpcHash*
46 current_scoped_ipc_hash = nullptr;
47
48 ABSL_CONST_INIT thread_local TaskAnnotator::LongTaskTracker*
49 current_long_task_tracker = nullptr;
50
51 // These functions can be removed, and the calls below replaced with direct
52 // variable accesses, once the MSAN workaround is not necessary.
GetCurrentScopedIpcHash()53 TaskAnnotator::ScopedSetIpcHash* GetCurrentScopedIpcHash() {
54 // Workaround false-positive MSAN use-of-uninitialized-value on
55 // thread_local storage for loaded libraries:
56 // https://github.com/google/sanitizers/issues/1265
57 MSAN_UNPOISON(¤t_scoped_ipc_hash,
58 sizeof(TaskAnnotator::ScopedSetIpcHash*));
59
60 return current_scoped_ipc_hash;
61 }
62
GetCurrentLongTaskTracker()63 TaskAnnotator::LongTaskTracker* GetCurrentLongTaskTracker() {
64 // Workaround false-positive MSAN use-of-uninitialized-value on
65 // thread_local storage for loaded libraries:
66 // https://github.com/google/sanitizers/issues/1265
67 MSAN_UNPOISON(¤t_long_task_tracker,
68 sizeof(TaskAnnotator::LongTaskTracker*));
69
70 return current_long_task_tracker;
71 }
72
73 #if BUILDFLAG(ENABLE_BASE_TRACING)
ToProtoEnum(subtle::DelayPolicy type)74 perfetto::protos::pbzero::ChromeTaskAnnotator::DelayPolicy ToProtoEnum(
75 subtle::DelayPolicy type) {
76 using ProtoType = perfetto::protos::pbzero::ChromeTaskAnnotator::DelayPolicy;
77 switch (type) {
78 case subtle::DelayPolicy::kFlexibleNoSooner:
79 return ProtoType::FLEXIBLE_NO_SOONER;
80 case subtle::DelayPolicy::kFlexiblePreferEarly:
81 return ProtoType::FLEXIBLE_PREFER_EARLY;
82 case subtle::DelayPolicy::kPrecise:
83 return ProtoType::PRECISE;
84 }
85 }
86 #endif // BUILDFLAG(ENABLE_BASE_TRACING)
87
88 } // namespace
89
CurrentTaskForThread()90 const PendingTask* TaskAnnotator::CurrentTaskForThread() {
91 // Workaround false-positive MSAN use-of-uninitialized-value on
92 // thread_local storage for loaded libraries:
93 // https://github.com/google/sanitizers/issues/1265
94 MSAN_UNPOISON(¤t_pending_task, sizeof(PendingTask*));
95
96 return current_pending_task;
97 }
98
OnIPCReceived(const char * interface_name,uint32_t (* method_info)(),bool is_response)99 void TaskAnnotator::OnIPCReceived(const char* interface_name,
100 uint32_t (*method_info)(),
101 bool is_response) {
102 auto* const tracker = GetCurrentLongTaskTracker();
103 if (!tracker) {
104 return;
105 }
106
107 tracker->SetIpcDetails(interface_name, method_info, is_response);
108 }
109
MarkCurrentTaskAsInterestingForTracing()110 void TaskAnnotator::MarkCurrentTaskAsInterestingForTracing() {
111 auto* const tracker = GetCurrentLongTaskTracker();
112 if (!tracker) {
113 return;
114 }
115
116 tracker->is_interesting_task = true;
117 }
118
119 TaskAnnotator::TaskAnnotator() = default;
120 TaskAnnotator::~TaskAnnotator() = default;
121
WillQueueTask(perfetto::StaticString trace_event_name,TaskMetadata * pending_task)122 void TaskAnnotator::WillQueueTask(perfetto::StaticString trace_event_name,
123 TaskMetadata* pending_task) {
124 DCHECK(pending_task);
125 TRACE_EVENT_INSTANT(
126 "toplevel.flow", trace_event_name,
127 perfetto::Flow::ProcessScoped(GetTaskTraceID(*pending_task)));
128
129 DCHECK(!pending_task->task_backtrace[0])
130 << "Task backtrace was already set, task posted twice??";
131 if (pending_task->task_backtrace[0])
132 return;
133
134 DCHECK(!pending_task->ipc_interface_name);
135 DCHECK(!pending_task->ipc_hash);
136 const auto* const hash = GetCurrentScopedIpcHash();
137 if (hash) {
138 pending_task->ipc_interface_name = hash->GetIpcInterfaceName();
139 pending_task->ipc_hash = hash->GetIpcHash();
140 }
141
142 const auto* parent_task = CurrentTaskForThread();
143 if (!parent_task)
144 return;
145
146 pending_task->task_backtrace[0] = parent_task->posted_from.program_counter();
147 std::copy(parent_task->task_backtrace.begin(),
148 parent_task->task_backtrace.end() - 1,
149 pending_task->task_backtrace.begin() + 1);
150 pending_task->task_backtrace_overflow =
151 parent_task->task_backtrace_overflow ||
152 parent_task->task_backtrace.back() != nullptr;
153 }
154
RunTaskImpl(PendingTask & pending_task)155 void TaskAnnotator::RunTaskImpl(PendingTask& pending_task) {
156 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION(
157 pending_task.posted_from.file_name());
158
159 // Before running the task, store the IPC context and the task backtrace with
160 // the chain of PostTasks that resulted in this call and deliberately alias it
161 // to ensure it is on the stack if the task crashes. Be careful not to assume
162 // that the variable itself will have the expected value when displayed by the
163 // optimizer in an optimized build. Look at a memory dump of the stack.
164 static constexpr int kStackTaskTraceSnapshotSize =
165 PendingTask::kTaskBacktraceLength + 4;
166 std::array<const void*, kStackTaskTraceSnapshotSize> task_backtrace;
167
168 // Store a marker to locate |task_backtrace| content easily on a memory
169 // dump. The layout is as follows:
170 //
171 // +------------ +----+---------+-----+-----------+----------+-------------+
172 // | Head Marker | PC | frame 0 | ... | frame N-1 | IPC hash | Tail Marker |
173 // +------------ +----+---------+-----+-----------+----------+-------------+
174 //
175 // Markers glossary (compliments of wez):
176 // cool code,do it dude!
177 // 0x c001 c0de d0 17 d00d
178 // o dude,i did it biig
179 // 0x 0 d00d 1 d1d 17 8119
180 task_backtrace.front() = reinterpret_cast<void*>(0xc001c0ded017d00d);
181 task_backtrace.back() = reinterpret_cast<void*>(0x0d00d1d1d178119);
182
183 task_backtrace[1] = pending_task.posted_from.program_counter();
184 ranges::copy(pending_task.task_backtrace, task_backtrace.begin() + 2);
185 task_backtrace[kStackTaskTraceSnapshotSize - 2] =
186 reinterpret_cast<void*>(pending_task.ipc_hash);
187 debug::Alias(&task_backtrace);
188
189 // Record the task time in convenient units. This can be compared to times
190 // stored in places like ReportThreadHang() and BrowserMain() when analyzing
191 // hangs.
192 const int64_t task_time =
193 pending_task.GetDesiredExecutionTime().since_origin().InSeconds();
194 base::debug::Alias(&task_time);
195
196 {
197 const AutoReset<PendingTask*> resetter(¤t_pending_task,
198 &pending_task);
199
200 if (g_task_annotator_observer) {
201 g_task_annotator_observer->BeforeRunTask(&pending_task);
202 }
203 std::move(pending_task.task).Run();
204 }
205
206 // Stomp the markers. Otherwise they can stick around on the unused parts of
207 // stack and cause |task_backtrace| to be associated with an unrelated stack
208 // sample on this thread later in the event of a crash. Alias once again after
209 // these writes to make sure the compiler doesn't optimize them out (unused
210 // writes to a local variable).
211 task_backtrace.front() = nullptr;
212 task_backtrace.back() = nullptr;
213 debug::Alias(&task_backtrace);
214 }
215
GetTaskTraceID(const TaskMetadata & task) const216 uint64_t TaskAnnotator::GetTaskTraceID(const TaskMetadata& task) const {
217 return (static_cast<uint64_t>(task.sequence_num) << 32) |
218 ((static_cast<uint64_t>(reinterpret_cast<intptr_t>(this)) << 32) >>
219 32);
220 }
221
222 // static
RegisterObserverForTesting(ObserverForTesting * observer)223 void TaskAnnotator::RegisterObserverForTesting(ObserverForTesting* observer) {
224 DCHECK(!g_task_annotator_observer);
225 g_task_annotator_observer = observer;
226 }
227
228 // static
ClearObserverForTesting()229 void TaskAnnotator::ClearObserverForTesting() {
230 g_task_annotator_observer = nullptr;
231 }
232
233 #if BUILDFLAG(ENABLE_BASE_TRACING)
234 // TRACE_EVENT argument helper, writing the task location data into
235 // EventContext.
EmitTaskLocation(perfetto::EventContext & ctx,const PendingTask & task)236 void TaskAnnotator::EmitTaskLocation(perfetto::EventContext& ctx,
237 const PendingTask& task) {
238 ctx.event()->set_task_execution()->set_posted_from_iid(
239 base::trace_event::InternedSourceLocation::Get(&ctx, task.posted_from));
240 }
241
242 // TRACE_EVENT argument helper, writing the incoming task flow information
243 // into EventContext if toplevel.flow category is enabled.
MaybeEmitIncomingTaskFlow(perfetto::EventContext & ctx,const PendingTask & task) const244 void TaskAnnotator::MaybeEmitIncomingTaskFlow(perfetto::EventContext& ctx,
245 const PendingTask& task) const {
246 static const uint8_t* flow_enabled =
247 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED("toplevel.flow");
248 if (!*flow_enabled)
249 return;
250
251 perfetto::Flow::ProcessScoped(GetTaskTraceID(task))(ctx);
252 }
253
254 // static
MaybeEmitDelayAndPolicy(perfetto::EventContext & ctx,const PendingTask & task)255 void TaskAnnotator::MaybeEmitDelayAndPolicy(perfetto::EventContext& ctx,
256 const PendingTask& task) {
257 if (task.delayed_run_time.is_null()) {
258 return;
259 }
260 auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
261 auto* annotator = event->set_chrome_task_annotator();
262 annotator->set_task_delay_us(static_cast<uint64_t>(
263 (task.delayed_run_time - task.queue_time).InMicroseconds()));
264 annotator->set_delay_policy(ToProtoEnum(task.delay_policy));
265 }
266
MaybeEmitIPCHash(perfetto::EventContext & ctx,const PendingTask & task) const267 void TaskAnnotator::MaybeEmitIPCHash(perfetto::EventContext& ctx,
268 const PendingTask& task) const {
269 static const uint8_t* toplevel_ipc_enabled =
270 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
271 TRACE_DISABLED_BY_DEFAULT("toplevel.ipc"));
272 if (!*toplevel_ipc_enabled)
273 return;
274
275 auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
276 auto* annotator = event->set_chrome_task_annotator();
277 annotator->set_ipc_hash(task.ipc_hash);
278 }
279 #endif // BUILDFLAG(ENABLE_BASE_TRACING)
280
ScopedSetIpcHash(uint32_t ipc_hash)281 TaskAnnotator::ScopedSetIpcHash::ScopedSetIpcHash(uint32_t ipc_hash)
282 : ScopedSetIpcHash(ipc_hash, nullptr) {}
283
ScopedSetIpcHash(const char * ipc_interface_name)284 TaskAnnotator::ScopedSetIpcHash::ScopedSetIpcHash(
285 const char* ipc_interface_name)
286 : ScopedSetIpcHash(0, ipc_interface_name) {}
287
ScopedSetIpcHash(uint32_t ipc_hash,const char * ipc_interface_name)288 TaskAnnotator::ScopedSetIpcHash::ScopedSetIpcHash(
289 uint32_t ipc_hash,
290 const char* ipc_interface_name)
291 : resetter_(¤t_scoped_ipc_hash, this),
292 ipc_hash_(ipc_hash),
293 ipc_interface_name_(ipc_interface_name) {}
294
295 // Static
MD5HashMetricName(std::string_view name)296 uint32_t TaskAnnotator::ScopedSetIpcHash::MD5HashMetricName(
297 std::string_view name) {
298 return HashMetricNameAs32Bits(name);
299 }
300
~ScopedSetIpcHash()301 TaskAnnotator::ScopedSetIpcHash::~ScopedSetIpcHash() {
302 DCHECK_EQ(this, GetCurrentScopedIpcHash());
303 }
304
LongTaskTracker(const TickClock * tick_clock,PendingTask & pending_task,TaskAnnotator * task_annotator)305 TaskAnnotator::LongTaskTracker::LongTaskTracker(const TickClock* tick_clock,
306 PendingTask& pending_task,
307 TaskAnnotator* task_annotator)
308 : resetter_(¤t_long_task_tracker, this),
309 tick_clock_(tick_clock),
310 pending_task_(pending_task),
311 task_annotator_(task_annotator) {
312 TRACE_EVENT_CATEGORY_GROUP_ENABLED("scheduler.long_tasks", &is_tracing_);
313 if (is_tracing_) {
314 task_start_time_ = tick_clock_->NowTicks();
315 }
316 }
317
~LongTaskTracker()318 TaskAnnotator::LongTaskTracker::~LongTaskTracker() {
319 DCHECK_EQ(this, GetCurrentLongTaskTracker());
320
321 if (!is_tracing_)
322 return;
323
324 task_end_time_ = tick_clock_->NowTicks();
325 MaybeTraceInterestingTaskDetails();
326
327 if ((task_end_time_ - task_start_time_) >= kMaxTaskDurationTimeDelta) {
328 TRACE_EVENT_BEGIN("scheduler.long_tasks", "LongTaskTracker",
329 perfetto::Track::ThreadScoped(task_annotator_),
330 task_start_time_, [&](perfetto::EventContext& ctx) {
331 TaskAnnotator::EmitTaskLocation(ctx, pending_task_);
332 EmitReceivedIPCDetails(ctx);
333 });
334 TRACE_EVENT_END("scheduler.long_tasks",
335 perfetto::Track::ThreadScoped(task_annotator_),
336 task_end_time_);
337 }
338 }
339
SetIpcDetails(const char * interface_name,uint32_t (* method_info)(),bool is_response)340 void TaskAnnotator::LongTaskTracker::SetIpcDetails(const char* interface_name,
341 uint32_t (*method_info)(),
342 bool is_response) {
343 ipc_interface_name_ = interface_name;
344 is_response_ = is_response;
345
346 if (!method_info)
347 return;
348
349 ipc_hash_ = (*method_info)();
350 ipc_method_info_ = method_info;
351 }
352
EmitReceivedIPCDetails(perfetto::EventContext & ctx)353 void TaskAnnotator::LongTaskTracker::EmitReceivedIPCDetails(
354 perfetto::EventContext& ctx) {
355 if (!ipc_interface_name_ || !ipc_hash_ || !ipc_method_info_)
356 return;
357 #if BUILDFLAG(ENABLE_BASE_TRACING) && !BUILDFLAG(IS_NACL)
358 // Emit all of the IPC hash information if this task
359 // comes from a mojo interface.
360 auto* info = ctx.event()->set_chrome_mojo_event_info();
361 info->set_mojo_interface_tag(ipc_interface_name_);
362 info->set_ipc_hash(ipc_hash_);
363 info->set_is_reply(is_response_);
364
365 // The Native client will not build as the relevant implementation of
366 // base::ModuleCache::CreateModuleForAddress is not implemented for it.
367 // Thus the below code must be included on a conditional basis.
368 const auto ipc_method_address = reinterpret_cast<uintptr_t>(ipc_method_info_);
369 const std::optional<size_t> location_iid =
370 base::trace_event::InternedUnsymbolizedSourceLocation::Get(
371 &ctx, ipc_method_address);
372 if (location_iid) {
373 info->set_mojo_interface_method_iid(*location_iid);
374 }
375 #endif
376 }
377
378 // This method is used to record the queueing time and task start time for tasks
379 // that may be of interest during a trace, even if they are not considered long
380 // tasks. For example, input - the queue time and flow information is required
381 // to calculate chrome input to browser intervals in perfetto, and further
382 // calculate the chrome tasks blocking input. We need LatencyInfo slices to be
383 // associated with the correct input IPCs, hence record in the LongTaskTracker.
MaybeTraceInterestingTaskDetails()384 void TaskAnnotator::LongTaskTracker::MaybeTraceInterestingTaskDetails() {
385 if (is_interesting_task && ipc_interface_name_) {
386 // Record the equivalent of a delayed instant trace event, acting as the
387 // start of the flow between task queue time and task execution start time.
388 TRACE_EVENT_INSTANT("scheduler.long_tasks", "InterestingTask_QueueingTime",
389 perfetto::Track::ThreadScoped(task_annotator_),
390 pending_task_.queue_time,
391 perfetto::Flow::ProcessScoped(
392 task_annotator_->GetTaskTraceID(pending_task_)));
393
394 // Record the equivalent of a top-level event with enough IPC information
395 // to calculate the input to browser interval. This event will be the
396 // termination of the event above, aka the start of task execution.
397 TRACE_EVENT_BEGIN(
398 "scheduler.long_tasks", "InterestingTask_ProcessingTime",
399 perfetto::Track::ThreadScoped(task_annotator_), task_start_time_,
400 [&](perfetto::EventContext& ctx) {
401 perfetto::TerminatingFlow::ProcessScoped(
402 task_annotator_->GetTaskTraceID(pending_task_))(ctx);
403 auto* info = ctx.event()->set_chrome_mojo_event_info();
404 info->set_mojo_interface_tag(ipc_interface_name_);
405 });
406
407 TRACE_EVENT_END("scheduler.long_tasks",
408 perfetto::Track::ThreadScoped(task_annotator_),
409 task_end_time_);
410 }
411 }
412
413 } // namespace base
414