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 17 matching lines...) Expand all Loading... |
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, stack depth <= 7. |
33 // - In 87 percent of the cases, stack depth <= 9. | 33 // - In 87 percent of the cases, stack depth <= 9. |
34 // - In 95 percent of the cases, 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 using StackFrame = const char*; |
39 // | |
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 | |
42 // pointer from process' address space. | |
43 struct BASE_EXPORT StackFrame { | |
44 enum class Type { | |
45 TRACE_EVENT_NAME, // const char* string | |
46 THREAD_NAME, // const char* thread name | |
47 }; | |
48 | 39 |
49 static StackFrame FromTraceEventName(const char* name) { | 40 struct BASE_EXPORT Backtrace { |
50 return {Type::TRACE_EVENT_NAME, name}; | 41 // Unused backtrace frames are filled with nullptr frames. If the stack is |
51 } | 42 // higher than what can be stored here, the bottom frames are stored. Based |
52 static StackFrame FromThreadName(const char* name) { | 43 // on the data above, a depth of 12 captures the full stack in the vast |
53 return {Type::THREAD_NAME, name}; | 44 // majority of the cases. |
54 } | 45 StackFrame frames[12]; |
55 | |
56 Type type; | |
57 const void* value; | |
58 }; | 46 }; |
59 | 47 |
60 bool BASE_EXPORT operator < (const StackFrame& lhs, const StackFrame& rhs); | 48 // Struct to store the size and count of the allocations. |
61 bool BASE_EXPORT operator == (const StackFrame& lhs, const StackFrame& rhs); | 49 struct AllocationMetrics { |
62 bool BASE_EXPORT operator != (const StackFrame& lhs, const StackFrame& rhs); | 50 size_t size; |
63 | 51 size_t count; |
64 struct BASE_EXPORT Backtrace { | |
65 Backtrace(); | |
66 | |
67 // 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 | |
69 // of 12 captures the full stack in the vast majority of the cases. | |
70 enum { kMaxFrameCount = 12 }; | |
71 StackFrame frames[kMaxFrameCount]; | |
72 size_t frame_count; | |
73 }; | 52 }; |
74 | 53 |
75 bool BASE_EXPORT operator==(const Backtrace& lhs, const Backtrace& rhs); | 54 bool BASE_EXPORT operator==(const Backtrace& lhs, const Backtrace& rhs); |
76 | 55 |
77 // The |AllocationContext| is context metadata that is kept for every allocation | 56 // The |AllocationContext| is context metadata that is kept for every allocation |
78 // when heap profiling is enabled. To simplify memory management for book- | 57 // when heap profiling is enabled. To simplify memory management for book- |
79 // keeping, this struct has a fixed size. | 58 // keeping, this struct has a fixed size. All |const char*|s here must have |
| 59 // static lifetime. |
80 struct BASE_EXPORT AllocationContext { | 60 struct BASE_EXPORT AllocationContext { |
81 AllocationContext(); | 61 public: |
| 62 // An allocation context with empty backtrace and unknown type. |
| 63 static AllocationContext Empty(); |
82 | 64 |
83 Backtrace backtrace; | 65 Backtrace backtrace; |
84 | 66 |
85 // Type name of the type stored in the allocated memory. A null pointer | 67 // Type name of the type stored in the allocated memory. A null pointer |
86 // indicates "unknown type". Grouping is done by comparing pointers, not by | 68 // indicates "unknown type". Grouping is done by comparing pointers, not by |
87 // deep string comparison. In a component build, where a type name can have a | 69 // deep string comparison. In a component build, where a type name can have a |
88 // string literal in several dynamic libraries, this may distort grouping. | 70 // string literal in several dynamic libraries, this may distort grouping. |
89 const char* type_name; | 71 const char* type_name; |
| 72 |
| 73 private: |
| 74 friend class AllocationContextTracker; |
| 75 |
| 76 // Don't allow uninitialized instances except inside the allocation context |
| 77 // tracker. Except in tests, an |AllocationContext| should only be obtained |
| 78 // from the tracker. In tests, paying the overhead of initializing the struct |
| 79 // to |Empty| and then overwriting the members is not such a big deal. |
| 80 AllocationContext(); |
90 }; | 81 }; |
91 | 82 |
92 bool BASE_EXPORT operator==(const AllocationContext& lhs, | 83 bool BASE_EXPORT operator==(const AllocationContext& lhs, |
93 const AllocationContext& rhs); | 84 const AllocationContext& rhs); |
94 | 85 |
95 // Struct to store the size and count of the allocations. | |
96 struct AllocationMetrics { | |
97 size_t size; | |
98 size_t count; | |
99 }; | |
100 | |
101 } // namespace trace_event | 86 } // namespace trace_event |
102 } // namespace base | 87 } // namespace base |
103 | 88 |
104 namespace BASE_HASH_NAMESPACE { | 89 namespace BASE_HASH_NAMESPACE { |
105 | 90 |
106 template <> | 91 template <> |
107 struct BASE_EXPORT hash<base::trace_event::StackFrame> { | |
108 size_t operator()(const base::trace_event::StackFrame& frame) const; | |
109 }; | |
110 | |
111 template <> | |
112 struct BASE_EXPORT hash<base::trace_event::Backtrace> { | 92 struct BASE_EXPORT hash<base::trace_event::Backtrace> { |
113 size_t operator()(const base::trace_event::Backtrace& backtrace) const; | 93 size_t operator()(const base::trace_event::Backtrace& backtrace) const; |
114 }; | 94 }; |
115 | 95 |
116 template <> | 96 template <> |
117 struct BASE_EXPORT hash<base::trace_event::AllocationContext> { | 97 struct BASE_EXPORT hash<base::trace_event::AllocationContext> { |
118 size_t operator()(const base::trace_event::AllocationContext& context) const; | 98 size_t operator()(const base::trace_event::AllocationContext& context) const; |
119 }; | 99 }; |
120 | 100 |
121 } // BASE_HASH_NAMESPACE | 101 } // BASE_HASH_NAMESPACE |
122 | 102 |
123 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_ | 103 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_ |
OLD | NEW |