OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_TRACE_EVENT_MEMORY_PROFILER_ALLOCATION_CONTEXT_H_ |
| 6 #define BASE_TRACE_EVENT_MEMORY_PROFILER_ALLOCATION_CONTEXT_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/atomicops.h" |
| 11 #include "base/base_export.h" |
| 12 #include "base/containers/small_map.h" |
| 13 |
| 14 namespace base { |
| 15 namespace trace_event { |
| 16 |
| 17 // When heap profiling is enabled, tracing keeps track of the allocation |
| 18 // context for each allocation intercepted. It is generated by the |
| 19 // |AllocationContextTracker| which keeps stacks of context in TLS. |
| 20 // The tracker is initialized lazily. |
| 21 |
| 22 using StackFrame = const char*; |
| 23 |
| 24 // A simple stack of |StackFrame| that unlike |std::stack| allows iterating |
| 25 // the stack and guards for underflow. |
| 26 class BASE_EXPORT AllocationStack { |
| 27 public: |
| 28 // Incrementing the iterator iterates down the stack. |
| 29 using ConstIterator = std::vector<StackFrame>::const_reverse_iterator; |
| 30 |
| 31 AllocationStack(); |
| 32 ~AllocationStack(); |
| 33 |
| 34 inline ConstIterator top() const { return stack_.rbegin(); } |
| 35 inline ConstIterator bottom() const { return stack_.rend(); } |
| 36 |
| 37 inline void push(StackFrame frame) { |
| 38 // Impose a limit on the height to verify that every push is popped, because |
| 39 // in practice the pseudo stack never grows higher than ~20 frames. |
| 40 DCHECK_LT(stack_.size(), 128u); |
| 41 stack_.push_back(frame); |
| 42 } |
| 43 |
| 44 inline void pop() { |
| 45 if (!stack_.empty()) |
| 46 stack_.pop_back(); |
| 47 } |
| 48 |
| 49 private: |
| 50 std::vector<StackFrame> stack_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(AllocationStack); |
| 53 }; |
| 54 |
| 55 class BASE_EXPORT AllocationContext { |
| 56 // TODO(ruuda): Fill this in a follow-up CL. |
| 57 }; |
| 58 |
| 59 // The allocation context tracker keeps track of thread-local context for heap |
| 60 // profiling. It includes a pseudo stack of trace events, and it might contain |
| 61 // arbitrary (key, value) context. On every allocation the tracker provides a |
| 62 // snapshot of its context in the form of an |AllocationContext| that is to be |
| 63 // stored together with the allocation details. |
| 64 class BASE_EXPORT AllocationContextTracker { |
| 65 public: |
| 66 // Globally enables capturing allocation context. |
| 67 // TODO(ruuda): Should this be replaced by |EnableCapturing| in the future? |
| 68 // Or at least have something that guards agains enable -> disable -> enable? |
| 69 static void SetCaptureEnabled(bool enabled); |
| 70 |
| 71 // Returns whether capturing allocation context is enabled globally. |
| 72 inline static bool capture_enabled() { |
| 73 // A little lag after heap profiling is enabled or disabled is fine, it is |
| 74 // more important that the check is as cheap as possible when capturing is |
| 75 // not enabled, so do not issue a memory barrier. |
| 76 return subtle::NoBarrier_Load(&capture_enabled_) != 0; |
| 77 } |
| 78 |
| 79 // Pushes a frame onto the thread-local pseudo stack. |
| 80 static void PushPseudoStackFrame(StackFrame frame); |
| 81 |
| 82 // Pops a frame from the thread-local pseudo stack. |
| 83 static void PopPseudoStackFrame(StackFrame frame); |
| 84 |
| 85 // Sets a thread-local (key, value) pair. |
| 86 static void SetContextField(const char* key, const char* value); |
| 87 |
| 88 // Removes the (key, value) pair with the specified key from the thread-local |
| 89 // context. |
| 90 static void UnsetContextField(const char* key); |
| 91 |
| 92 // Returns a snapshot of the current thread-local context. |
| 93 static AllocationContext GetContext(); |
| 94 |
| 95 // TODO(ruuda): Remove in a follow-up CL, this is only used for testing now. |
| 96 static AllocationStack* GetPseudoStackForTesting(); |
| 97 |
| 98 ~AllocationContextTracker(); |
| 99 |
| 100 private: |
| 101 AllocationContextTracker(); |
| 102 |
| 103 static AllocationContextTracker* GetThreadLocalTracker(); |
| 104 |
| 105 static subtle::Atomic32 capture_enabled_; |
| 106 |
| 107 // The pseudo stack where frames are |TRACE_EVENT| names. |
| 108 AllocationStack pseudo_stack_; |
| 109 |
| 110 // A dictionary of arbitrary context. |
| 111 SmallMap<std::map<const char*, const char*>> context_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(AllocationContextTracker); |
| 114 }; |
| 115 |
| 116 } // namespace trace_event |
| 117 } // namespace base |
| 118 |
| 119 #endif // BASE_TRACE_EVENT_MEMORY_PROFILER_ALLOCATION_CONTEXT_H_ |
OLD | NEW |