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