OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ | 5 #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ |
6 #define BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ | 6 #define BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/debug/stack_trace.h" | 12 #include "base/debug/stack_trace.h" |
13 #include "base/logging.h" | |
14 #include "base/macros.h" | 13 #include "base/macros.h" |
15 #include "base/trace_event/heap_profiler_allocation_context.h" | 14 #include "base/trace_event/heap_profiler_allocation_context.h" |
16 | 15 |
17 namespace base { | 16 namespace base { |
18 namespace trace_event { | 17 namespace trace_event { |
19 | 18 |
20 // The allocation context tracker keeps track of thread-local context for heap | 19 // The allocation context tracker keeps track of thread-local context for heap |
21 // profiling. It includes a pseudo stack of trace events. On every allocation | 20 // profiling. It includes a pseudo stack of trace events. On every allocation |
22 // the tracker provides a snapshot of its context in the form of an | 21 // the tracker provides a snapshot of its context in the form of an |
23 // |AllocationContext| that is to be stored together with the allocation | 22 // |AllocationContext| that is to be stored together with the allocation |
24 // details. | 23 // details. |
25 class BASE_EXPORT AllocationContextTracker { | 24 class BASE_EXPORT AllocationContextTracker { |
26 public: | 25 public: |
27 enum class CaptureMode: int32_t { | 26 enum class CaptureMode: int32_t { |
28 DISABLED, // Don't capture anything | 27 DISABLED, // Don't capture anything |
29 PSEUDO_STACK, // GetContextSnapshot() returns pseudo stack trace | 28 PSEUDO_STACK, // GetContextSnapshot() returns pseudo stack trace |
30 NATIVE_STACK // GetContextSnapshot() returns native (real) stack trace | 29 NATIVE_STACK // GetContextSnapshot() returns native (real) stack trace |
31 }; | 30 }; |
32 | 31 |
| 32 // Stack frame constructed from trace events in codebase. |
| 33 struct BASE_EXPORT PseudoStackFrame { |
| 34 const char* trace_event_category; |
| 35 const char* trace_event_name; |
| 36 |
| 37 bool operator==(const PseudoStackFrame& other) const { |
| 38 return trace_event_category == other.trace_event_category && |
| 39 trace_event_name == other.trace_event_name; |
| 40 } |
| 41 }; |
| 42 |
33 // Globally sets capturing mode. | 43 // Globally sets capturing mode. |
34 // TODO(primiano): How to guard against *_STACK -> DISABLED -> *_STACK? | 44 // TODO(primiano): How to guard against *_STACK -> DISABLED -> *_STACK? |
35 static void SetCaptureMode(CaptureMode mode); | 45 static void SetCaptureMode(CaptureMode mode); |
36 | 46 |
37 // Returns global capturing mode. | 47 // Returns global capturing mode. |
38 inline static CaptureMode capture_mode() { | 48 inline static CaptureMode capture_mode() { |
39 // A little lag after heap profiling is enabled or disabled is fine, it is | 49 // A little lag after heap profiling is enabled or disabled is fine, it is |
40 // more important that the check is as cheap as possible when capturing is | 50 // more important that the check is as cheap as possible when capturing is |
41 // not enabled, so do not issue a memory barrier in the fast path. | 51 // not enabled, so do not issue a memory barrier in the fast path. |
42 if (subtle::NoBarrier_Load(&capture_mode_) == | 52 if (subtle::NoBarrier_Load(&capture_mode_) == |
(...skipping 19 matching lines...) Expand all Loading... |
62 // Starts and ends a new ignore scope between which the allocations are | 72 // Starts and ends a new ignore scope between which the allocations are |
63 // ignored in the heap profiler. A dummy context that short circuits to | 73 // ignored in the heap profiler. A dummy context that short circuits to |
64 // "tracing_overhead" is returned for these allocations. | 74 // "tracing_overhead" is returned for these allocations. |
65 void begin_ignore_scope() { ignore_scope_depth_++; } | 75 void begin_ignore_scope() { ignore_scope_depth_++; } |
66 void end_ignore_scope() { | 76 void end_ignore_scope() { |
67 if (ignore_scope_depth_) | 77 if (ignore_scope_depth_) |
68 ignore_scope_depth_--; | 78 ignore_scope_depth_--; |
69 } | 79 } |
70 | 80 |
71 // Pushes a frame onto the thread-local pseudo stack. | 81 // Pushes a frame onto the thread-local pseudo stack. |
72 void PushPseudoStackFrame(const char* trace_event_name); | 82 void PushPseudoStackFrame(PseudoStackFrame stack_frame); |
73 | 83 |
74 // Pops a frame from the thread-local pseudo stack. | 84 // Pops a frame from the thread-local pseudo stack. |
75 void PopPseudoStackFrame(const char* trace_event_name); | 85 void PopPseudoStackFrame(PseudoStackFrame stack_frame); |
76 | 86 |
77 // Push and pop current task's context. A stack is used to support nested | 87 // Push and pop current task's context. A stack is used to support nested |
78 // tasks and the top of the stack will be used in allocation context. | 88 // tasks and the top of the stack will be used in allocation context. |
79 void PushCurrentTaskContext(const char* context); | 89 void PushCurrentTaskContext(const char* context); |
80 void PopCurrentTaskContext(const char* context); | 90 void PopCurrentTaskContext(const char* context); |
81 | 91 |
82 // Returns a snapshot of the current thread-local context. | 92 // Returns a snapshot of the current thread-local context. |
83 AllocationContext GetContextSnapshot(); | 93 AllocationContext GetContextSnapshot(); |
84 | 94 |
85 ~AllocationContextTracker(); | 95 ~AllocationContextTracker(); |
86 | 96 |
87 private: | 97 private: |
88 AllocationContextTracker(); | 98 AllocationContextTracker(); |
89 | 99 |
90 static subtle::Atomic32 capture_mode_; | 100 static subtle::Atomic32 capture_mode_; |
91 | 101 |
92 // The pseudo stack where frames are |TRACE_EVENT| names. | 102 // The pseudo stack where frames are |TRACE_EVENT| names. |
93 std::vector<const char*> pseudo_stack_; | 103 std::vector<PseudoStackFrame> pseudo_stack_; |
94 | 104 |
95 // The thread name is used as the first entry in the pseudo stack. | 105 // The thread name is used as the first entry in the pseudo stack. |
96 const char* thread_name_; | 106 const char* thread_name_; |
97 | 107 |
98 // Stack of tasks' contexts. Context serves as a different dimension than | 108 // Stack of tasks' contexts. Context serves as a different dimension than |
99 // pseudo stack to cluster allocations. | 109 // pseudo stack to cluster allocations. |
100 std::vector<const char*> task_contexts_; | 110 std::vector<const char*> task_contexts_; |
101 | 111 |
102 uint32_t ignore_scope_depth_; | 112 uint32_t ignore_scope_depth_; |
103 | 113 |
104 DISALLOW_COPY_AND_ASSIGN(AllocationContextTracker); | 114 DISALLOW_COPY_AND_ASSIGN(AllocationContextTracker); |
105 }; | 115 }; |
106 | 116 |
107 } // namespace trace_event | 117 } // namespace trace_event |
108 } // namespace base | 118 } // namespace base |
109 | 119 |
110 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ | 120 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ |
OLD | NEW |