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_H_ | 5 #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_ |
6 #define BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_ | 6 #define BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 // The backtrace in the allocation context is a snapshot of the stack. For now, | 22 // The backtrace in the allocation context is a snapshot of the stack. For now, |
23 // this is the pseudo stack where frames are created by trace event macros. In | 23 // this is the pseudo stack where frames are created by trace event macros. In |
24 // the future, we might add the option to use the native call stack. In that | 24 // the future, we might add the option to use the native call stack. In that |
25 // case, |Backtrace| and |AllocationContextTracker::GetContextSnapshot| might | 25 // case, |Backtrace| and |AllocationContextTracker::GetContextSnapshot| might |
26 // have different implementations that can be selected by a compile time flag. | 26 // have different implementations that can be selected by a compile time flag. |
27 | 27 |
28 // The number of stack frames stored in the backtrace is a trade off between | 28 // The number of stack frames stored in the backtrace is a trade off between |
29 // memory used for tracing and accuracy. Measurements done on a prototype | 29 // memory used for tracing and accuracy. Measurements done on a prototype |
30 // revealed that: | 30 // revealed that: |
31 // | 31 // |
32 // - In 60 percent of the cases, pseudo stack depth <= 7. | 32 // - In 60 percent of the cases, stack depth <= 7. |
33 // - In 87 percent of the cases, pseudo stack depth <= 9. | 33 // - In 87 percent of the cases, stack depth <= 9. |
34 // - In 95 percent of the cases, pseudo stack depth <= 11. | 34 // - In 95 percent of the cases, stack depth <= 11. |
35 // | 35 // |
36 // See the design doc (https://goo.gl/4s7v7b) for more details. | 36 // See the design doc (https://goo.gl/4s7v7b) for more details. |
37 | 37 |
38 // Represents (pseudo) stack frame. Used in Backtrace class below. | 38 // Represents (pseudo) stack frame. Used in Backtrace class below. |
39 // | 39 // |
40 // Conceptually stack frame is identified by its value, and type is used | 40 // Conceptually stack frame is identified by its value, and type is used |
41 // mostly to properly format the value. Value is expected to be a valid | 41 // mostly to properly format the value. Value is expected to be a valid |
42 // pointer from process' address space. | 42 // pointer from process' address space. |
43 struct BASE_EXPORT StackFrame { | 43 struct BASE_EXPORT StackFrame { |
44 enum class Type { | 44 enum class Type { |
45 TRACE_EVENT_NAME, // const char* string | 45 TRACE_EVENT_NAME, // const char* string |
46 THREAD_NAME, // const char* thread name | 46 THREAD_NAME, // const char* thread name |
47 PROGRAM_COUNTER, // as returned by stack tracing (e.g. by StackTrace) | |
48 }; | 47 }; |
49 | 48 |
50 static StackFrame FromTraceEventName(const char* name) { | 49 static StackFrame FromTraceEventName(const char* name) { |
51 return {Type::TRACE_EVENT_NAME, name}; | 50 return {Type::TRACE_EVENT_NAME, name}; |
52 } | 51 } |
53 static StackFrame FromThreadName(const char* name) { | 52 static StackFrame FromThreadName(const char* name) { |
54 return {Type::THREAD_NAME, name}; | 53 return {Type::THREAD_NAME, name}; |
55 } | 54 } |
56 static StackFrame FromProgramCounter(const void* pc) { | |
57 return {Type::PROGRAM_COUNTER, pc}; | |
58 } | |
59 | 55 |
60 Type type; | 56 Type type; |
61 const void* value; | 57 const void* value; |
62 }; | 58 }; |
63 | 59 |
64 bool BASE_EXPORT operator < (const StackFrame& lhs, const StackFrame& rhs); | 60 bool BASE_EXPORT operator < (const StackFrame& lhs, const StackFrame& rhs); |
65 bool BASE_EXPORT operator == (const StackFrame& lhs, const StackFrame& rhs); | 61 bool BASE_EXPORT operator == (const StackFrame& lhs, const StackFrame& rhs); |
66 bool BASE_EXPORT operator != (const StackFrame& lhs, const StackFrame& rhs); | 62 bool BASE_EXPORT operator != (const StackFrame& lhs, const StackFrame& rhs); |
67 | 63 |
68 struct BASE_EXPORT Backtrace { | 64 struct BASE_EXPORT Backtrace { |
69 Backtrace(); | 65 Backtrace(); |
70 | 66 |
71 // If the stack is higher than what can be stored here, the bottom frames | 67 // If the stack is higher than what can be stored here, the bottom frames |
72 // (the ones closer to main()) are stored. Depth of 12 is enough for most | 68 // (the ones closer to main()) are stored. Based on the data above, a depth |
73 // pseudo traces (see above), but not for native traces, where we need more. | 69 // of 12 captures the full stack in the vast majority of the cases. |
74 enum { kMaxFrameCount = 24 }; | 70 enum { kMaxFrameCount = 12 }; |
75 StackFrame frames[kMaxFrameCount]; | 71 StackFrame frames[kMaxFrameCount]; |
76 size_t frame_count; | 72 size_t frame_count; |
77 }; | 73 }; |
78 | 74 |
79 bool BASE_EXPORT operator==(const Backtrace& lhs, const Backtrace& rhs); | 75 bool BASE_EXPORT operator==(const Backtrace& lhs, const Backtrace& rhs); |
80 | 76 |
81 // The |AllocationContext| is context metadata that is kept for every allocation | 77 // The |AllocationContext| is context metadata that is kept for every allocation |
82 // when heap profiling is enabled. To simplify memory management for book- | 78 // when heap profiling is enabled. To simplify memory management for book- |
83 // keeping, this struct has a fixed size. | 79 // keeping, this struct has a fixed size. |
84 struct BASE_EXPORT AllocationContext { | 80 struct BASE_EXPORT AllocationContext { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 }; | 114 }; |
119 | 115 |
120 template <> | 116 template <> |
121 struct BASE_EXPORT hash<base::trace_event::AllocationContext> { | 117 struct BASE_EXPORT hash<base::trace_event::AllocationContext> { |
122 size_t operator()(const base::trace_event::AllocationContext& context) const; | 118 size_t operator()(const base::trace_event::AllocationContext& context) const; |
123 }; | 119 }; |
124 | 120 |
125 } // BASE_HASH_NAMESPACE | 121 } // BASE_HASH_NAMESPACE |
126 | 122 |
127 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_ | 123 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_ |
OLD | NEW |