xref: /aosp_15_r20/external/cronet/base/trace_event/heap_profiler.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 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_TRACE_EVENT_HEAP_PROFILER_H_
6 #define BASE_TRACE_EVENT_HEAP_PROFILER_H_
7 
8 #include "base/compiler_specific.h"
9 #include "base/trace_event/heap_profiler_allocation_context_tracker.h"
10 
11 // This header file defines the set of macros that are used to track memory
12 // usage in the heap profiler. This is in addition to the macros defined in
13 // trace_event.h and are specific to heap profiler. This file also defines
14 // implementation details of these macros.
15 
16 // Scoped tracker for task execution context in the heap profiler.
17 #define TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION \
18   trace_event_internal::HeapProfilerScopedTaskExecutionTracker
19 
20 // Returns the current task context (c-string) tracked by heap profiler. This is
21 // useful along with TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION if a async
22 // system needs to track client's allocation context across post tasks. Use this
23 // macro to get the current context and use
24 // TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION in the posted task which
25 // allocates memory for a client.
26 #define TRACE_HEAP_PROFILER_API_GET_CURRENT_TASK_CONTEXT \
27   trace_event_internal::HeapProfilerCurrentTaskContext
28 
29 // A scoped ignore event used to tell heap profiler to ignore all the
30 // allocations in the scope. It is useful to exclude allocations made for
31 // tracing from the heap profiler dumps.
32 // TODO(https://crbug.com/1378619): This is a no-op since
33 // AllocationContextTracker::GetContextSnapshot was removed. Clean up the call
34 // sites.
35 #define HEAP_PROFILER_SCOPED_IGNORE ((void)0)
36 
37 namespace trace_event_internal {
38 
39 // HeapProfilerScopedTaskExecutionTracker records the current task's context in
40 // the heap profiler.
41 class HeapProfilerScopedTaskExecutionTracker {
42  public:
HeapProfilerScopedTaskExecutionTracker(const char * task_context)43   inline explicit HeapProfilerScopedTaskExecutionTracker(
44       const char* task_context)
45       : context_(task_context) {
46     using base::trace_event::AllocationContextTracker;
47     if (UNLIKELY(AllocationContextTracker::capture_mode() !=
48                  AllocationContextTracker::CaptureMode::kDisabled)) {
49       AllocationContextTracker::GetInstanceForCurrentThread()
50           ->PushCurrentTaskContext(context_);
51     }
52   }
53 
~HeapProfilerScopedTaskExecutionTracker()54   inline ~HeapProfilerScopedTaskExecutionTracker() {
55     using base::trace_event::AllocationContextTracker;
56     if (UNLIKELY(AllocationContextTracker::capture_mode() !=
57                  AllocationContextTracker::CaptureMode::kDisabled)) {
58       AllocationContextTracker::GetInstanceForCurrentThread()
59           ->PopCurrentTaskContext(context_);
60     }
61   }
62 
63  private:
64   const char* context_;
65 };
66 
HeapProfilerCurrentTaskContext()67 inline const char* HeapProfilerCurrentTaskContext() {
68   return base::trace_event::AllocationContextTracker::
69       GetInstanceForCurrentThread()
70           ->TaskContext();
71 }
72 
73 }  // namespace trace_event_internal
74 
75 #endif  // BASE_TRACE_EVENT_HEAP_PROFILER_H_
76