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