| 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, stack depth <= 7. | 32 // - In 60 percent of the cases, pseudo stack depth <= 7. |
| 33 // - In 87 percent of the cases, stack depth <= 9. | 33 // - In 87 percent of the cases, pseudo stack depth <= 9. |
| 34 // - In 95 percent of the cases, stack depth <= 11. | 34 // - In 95 percent of the cases, pseudo 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) |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 static StackFrame FromTraceEventName(const char* name) { | 50 static StackFrame FromTraceEventName(const char* name) { |
| 50 return {Type::TRACE_EVENT_NAME, name}; | 51 return {Type::TRACE_EVENT_NAME, name}; |
| 51 } | 52 } |
| 52 static StackFrame FromThreadName(const char* name) { | 53 static StackFrame FromThreadName(const char* name) { |
| 53 return {Type::THREAD_NAME, name}; | 54 return {Type::THREAD_NAME, name}; |
| 54 } | 55 } |
| 56 static StackFrame FromProgramCounter(const void* pc) { |
| 57 return {Type::PROGRAM_COUNTER, pc}; |
| 58 } |
| 55 | 59 |
| 56 Type type; | 60 Type type; |
| 57 const void* value; | 61 const void* value; |
| 58 }; | 62 }; |
| 59 | 63 |
| 60 bool BASE_EXPORT operator < (const StackFrame& lhs, const StackFrame& rhs); | 64 bool BASE_EXPORT operator < (const StackFrame& lhs, const StackFrame& rhs); |
| 61 bool BASE_EXPORT operator == (const StackFrame& lhs, const StackFrame& rhs); | 65 bool BASE_EXPORT operator == (const StackFrame& lhs, const StackFrame& rhs); |
| 62 bool BASE_EXPORT operator != (const StackFrame& lhs, const StackFrame& rhs); | 66 bool BASE_EXPORT operator != (const StackFrame& lhs, const StackFrame& rhs); |
| 63 | 67 |
| 64 struct BASE_EXPORT Backtrace { | 68 struct BASE_EXPORT Backtrace { |
| 65 Backtrace(); | 69 Backtrace(); |
| 66 | 70 |
| 67 // If the stack is higher than what can be stored here, the bottom frames | 71 // If the stack is higher than what can be stored here, the bottom frames |
| 68 // (the ones closer to main()) are stored. Based on the data above, a depth | 72 // (the ones closer to main()) are stored. Depth of 12 is enough for most |
| 69 // of 12 captures the full stack in the vast majority of the cases. | 73 // pseudo traces (see above), but not for native traces, where we need more. |
| 70 enum { kMaxFrameCount = 12 }; | 74 enum { kMaxFrameCount = 24 }; |
| 71 StackFrame frames[kMaxFrameCount]; | 75 StackFrame frames[kMaxFrameCount]; |
| 72 size_t frame_count; | 76 size_t frame_count; |
| 73 }; | 77 }; |
| 74 | 78 |
| 75 bool BASE_EXPORT operator==(const Backtrace& lhs, const Backtrace& rhs); | 79 bool BASE_EXPORT operator==(const Backtrace& lhs, const Backtrace& rhs); |
| 76 | 80 |
| 77 // The |AllocationContext| is context metadata that is kept for every allocation | 81 // The |AllocationContext| is context metadata that is kept for every allocation |
| 78 // when heap profiling is enabled. To simplify memory management for book- | 82 // when heap profiling is enabled. To simplify memory management for book- |
| 79 // keeping, this struct has a fixed size. | 83 // keeping, this struct has a fixed size. |
| 80 struct BASE_EXPORT AllocationContext { | 84 struct BASE_EXPORT AllocationContext { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 }; | 118 }; |
| 115 | 119 |
| 116 template <> | 120 template <> |
| 117 struct BASE_EXPORT hash<base::trace_event::AllocationContext> { | 121 struct BASE_EXPORT hash<base::trace_event::AllocationContext> { |
| 118 size_t operator()(const base::trace_event::AllocationContext& context) const; | 122 size_t operator()(const base::trace_event::AllocationContext& context) const; |
| 119 }; | 123 }; |
| 120 | 124 |
| 121 } // BASE_HASH_NAMESPACE | 125 } // BASE_HASH_NAMESPACE |
| 122 | 126 |
| 123 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_ | 127 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_ |
| OLD | NEW |