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 // The allocation context is attached to every allocation when heap profiling | |
| 18 // is enabled. It is generated by the |AllocationContextTracker| which keeps | |
| 19 // stacks of context in TLS. The tracker is initialized lazily. | |
| 20 | |
| 21 typedef const char* StackFrame; | |
| 22 | |
| 23 // A simple stack of |StackFrame| that unlike |std::stack| allows iterating | |
| 24 // the stack and guards for underflow. | |
| 25 class BASE_EXPORT AllocationStack { | |
| 26 public: | |
| 27 typedef std::vector<StackFrame>::const_reverse_iterator ConstIterator; | |
|
dsinclair
2015/09/22 17:05:21
Should this be ConstReverseIterator to make it cle
Ruud van Asseldonk
2015/09/23 09:39:03
I don't think that would make anything clearer. I
| |
| 28 | |
| 29 AllocationStack(); | |
| 30 ~AllocationStack(); | |
| 31 | |
| 32 inline ConstIterator top() const { return stack_.rbegin(); } | |
| 33 inline ConstIterator bottom() const { return stack_.rend(); } | |
| 34 | |
| 35 inline void Push(StackFrame frame) { stack_.push_back(frame); } | |
| 36 inline void Pop() { | |
|
dsinclair
2015/09/22 17:05:21
nit: Should these be push/pop to be consistent wit
Ruud van Asseldonk
2015/09/23 09:39:03
Done.
| |
| 37 if (!stack_.empty()) | |
| 38 stack_.pop_back(); | |
| 39 } | |
| 40 | |
| 41 private: | |
| 42 std::vector<StackFrame> stack_; | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(AllocationStack); | |
| 45 }; | |
| 46 | |
| 47 class BASE_EXPORT AllocationContext { | |
| 48 // TODO(ruuda): Fill this in a follow-up CL. | |
| 49 }; | |
| 50 | |
| 51 // The allocation context tracker keeps track of thread-local context for heap | |
| 52 // profiling. It includes a pseudo stack of trace events, and it might contain | |
| 53 // arbitrary (key, value) context. On every allocation the tracker provides a | |
| 54 // snapshot of its context in the form of an |AllocationContext| that is to be | |
| 55 // stored together with the allocation details. | |
| 56 class BASE_EXPORT AllocationContextTracker { | |
| 57 friend AllocationContextTracker* GetThreadLocalTracker(); | |
|
dsinclair
2015/09/22 17:05:21
Why is this a friend function and not just in an a
Ruud van Asseldonk
2015/09/23 09:39:03
Because it has to construct the |AllocationContext
dsinclair
2015/09/23 19:35:21
So why not make GetThreadLocalTracker a static met
| |
| 58 | |
| 59 public: | |
| 60 // Globally enables capturing allocation context. | |
| 61 // TODO(ruuda): Should this be replaced by |EnableCapturing| in the future? | |
| 62 // Or at least have something that guards agains enable -> disable -> enable? | |
| 63 static void SetCaptureEnabled(bool enabled); | |
| 64 | |
| 65 // Returns whether capturing allocation context is enabled globally. | |
| 66 inline static bool capture_enabled() { | |
| 67 // A little lag after heap profiling is enabled or disabled is fine, it is | |
| 68 // more important that the check is as cheap as possible when capturing is | |
| 69 // not enabled, so do not issue a memory barrier. | |
| 70 return subtle::NoBarrier_Load(&capture_enabled_) != 0; | |
| 71 } | |
| 72 | |
| 73 // Pushes a frame onto the thread-local pseudo stack. | |
| 74 static void PushPseudoStackFrame(StackFrame frame); | |
| 75 | |
| 76 // Pops a frame from the thread-local pseudo stack. | |
| 77 static void PopPseudoStackFrame(); | |
| 78 | |
| 79 // Sets a thread-local (key, value) pair. | |
| 80 static void SetContextField(const char* key, const char* value); | |
| 81 | |
| 82 // Removes the (key, value) pair with the specified key from the thread-local | |
| 83 // context. | |
| 84 static void UnsetContextField(const char* key); | |
| 85 | |
| 86 // Returns a snapshot of the current thread-local context. | |
| 87 static AllocationContext GetContext(); | |
| 88 | |
| 89 ~AllocationContextTracker(); | |
|
dsinclair
2015/09/22 17:05:21
Move this below the GetPseudoStack so all the stat
Ruud van Asseldonk
2015/09/23 09:39:03
Done.
| |
| 90 | |
| 91 // TODO(ruuda): Remove in a follow-up CL, this is only used for testing now. | |
| 92 static AllocationStack* GetPseudoStack(); | |
|
dsinclair
2015/09/22 17:05:21
nit: s/GetPseudoStack/GetPseudoStackForTesting
Ruud van Asseldonk
2015/09/23 09:39:03
Done.
| |
| 93 | |
| 94 private: | |
| 95 AllocationContextTracker(); | |
| 96 | |
| 97 // The pseudo stack where frames are |TRACE_EVENT| names. | |
| 98 AllocationStack pseudo_stack_; | |
| 99 | |
| 100 // A dictionary of arbitrary context. | |
| 101 SmallMap<std::map<const char*, const char*>> context_; | |
| 102 | |
| 103 static subtle::Atomic32 capture_enabled_; | |
| 104 | |
| 105 DISALLOW_COPY_AND_ASSIGN(AllocationContextTracker); | |
| 106 }; | |
| 107 | |
| 108 } // namespace trace_event | |
| 109 } // namespace base | |
| 110 | |
| 111 #endif // BASE_TRACE_EVENT_MEMORY_PROFILER_ALLOCATION_CONTEXT_H_ | |
| OLD | NEW |