Chromium Code Reviews| 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_TRACKER_H_ | 5 #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ |
| 6 #define BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ | 6 #define BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| 11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/trace_event/heap_profiler_allocation_context.h" | 14 #include "base/trace_event/heap_profiler_allocation_context.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 namespace trace_event { | 17 namespace trace_event { |
| 18 | 18 |
| 19 // The allocation context tracker keeps track of thread-local context for heap | 19 // The allocation context tracker keeps track of thread-local context for heap |
| 20 // profiling. It includes a pseudo stack of trace events. On every allocation | 20 // profiling. It includes a pseudo stack of trace events. On every allocation |
| 21 // the tracker provides a snapshot of its context in the form of an | 21 // the tracker provides a snapshot of its context in the form of an |
| 22 // |AllocationContext| that is to be stored together with the allocation | 22 // |AllocationContext| that is to be stored together with the allocation |
| 23 // details. | 23 // details. |
| 24 class BASE_EXPORT AllocationContextTracker { | 24 class BASE_EXPORT AllocationContextTracker { |
| 25 public: | 25 public: |
| 26 // Globally enables capturing allocation context. | 26 enum CaptureMode { |
|
Primiano Tucci (use gerrit)
2016/04/07 15:51:56
enum class CaptureMode { DISABLED, PSEUDO_STACK, N
Dmitry Skiba
2016/04/12 18:22:11
Done.
| |
| 27 // TODO(ruuda): Should this be replaced by |EnableCapturing| in the future? | 27 DONT_CAPTURE = 0, // must be 0 |
| 28 // Or at least have something that guards agains enable -> disable -> enable? | 28 CAPTURE_PSEUDO_STACK, |
| 29 static void SetCaptureEnabled(bool enabled); | 29 CAPTURE_NATIVE_STACK |
| 30 }; | |
| 31 | |
| 32 // Globally sets capturing mode. | |
| 33 // TODO: How to guards against CAPTURE_* -> DONT_CAPTURE -> CAPTURE_*? | |
|
Primiano Tucci (use gerrit)
2016/04/07 15:51:56
TODO want always a name ;)
you can put mine if you
Dmitry Skiba
2016/04/12 18:22:11
Done.
| |
| 34 static void SetCaptureMode(CaptureMode mode); | |
| 30 | 35 |
| 31 // Returns whether capturing allocation context is enabled globally. | 36 // Returns whether capturing allocation context is enabled globally. |
| 32 inline static bool capture_enabled() { | 37 inline static bool capture_enabled() { |
|
Primiano Tucci (use gerrit)
2016/04/07 15:51:56
Hmm I think you want to expose the capture mode he
Dmitry Skiba
2016/04/12 18:22:11
Yes, this is intended. I want us to still maintain
| |
| 33 // A little lag after heap profiling is enabled or disabled is fine, it is | 38 // A little lag after heap profiling is enabled or disabled is fine, it is |
| 34 // more important that the check is as cheap as possible when capturing is | 39 // more important that the check is as cheap as possible when capturing is |
| 35 // not enabled, so do not issue a memory barrier in the fast path. | 40 // not enabled, so do not issue a memory barrier in the fast path. |
| 36 if (subtle::NoBarrier_Load(&capture_enabled_) == 0) | 41 if (subtle::NoBarrier_Load(&capture_mode_) == DONT_CAPTURE) |
| 37 return false; | 42 return false; |
| 38 | 43 |
| 39 // In the slow path, an acquire load is required to pair with the release | 44 // In the slow path, an acquire load is required to pair with the release |
| 40 // store in |SetCaptureEnabled|. This is to ensure that the TLS slot for | 45 // store in |SetCaptureMode|. This is to ensure that the TLS slot for |
| 41 // the thread-local allocation context tracker has been initialized if | 46 // the thread-local allocation context tracker has been initialized if |
| 42 // |capture_enabled| returns true. | 47 // |capture_enabled| returns true. |
| 43 return subtle::Acquire_Load(&capture_enabled_) != 0; | 48 return subtle::Acquire_Load(&capture_mode_) != DONT_CAPTURE; |
| 44 } | 49 } |
| 45 | 50 |
| 46 // Returns the thread-local instance, creating one if necessary. Returns | 51 // Returns the thread-local instance, creating one if necessary. Returns |
| 47 // always a valid instance, unless it is called re-entrantly, in which case | 52 // always a valid instance, unless it is called re-entrantly, in which case |
| 48 // returns nullptr in the nested calls. | 53 // returns nullptr in the nested calls. |
| 49 static AllocationContextTracker* GetInstanceForCurrentThread(); | 54 static AllocationContextTracker* GetInstanceForCurrentThread(); |
| 50 | 55 |
| 51 // Set the thread name in the AllocationContextTracker of the current thread | 56 // Set the thread name in the AllocationContextTracker of the current thread |
| 52 // if capture is enabled. | 57 // if capture is enabled. |
| 53 static void SetCurrentThreadName(const char* name); | 58 static void SetCurrentThreadName(const char* name); |
| 54 | 59 |
| 55 // Pushes a frame onto the thread-local pseudo stack. | 60 // Pushes a frame onto the thread-local pseudo stack. |
| 61 void PushPseudoStackFrame(const char* frame_symbol_value); | |
|
Primiano Tucci (use gerrit)
2016/04/07 15:51:56
s/frame_symbol_value/trace_event_name/ :) it makes
Dmitry Skiba
2016/04/12 18:22:11
Done.
| |
| 56 void PushPseudoStackFrame(StackFrame frame); | 62 void PushPseudoStackFrame(StackFrame frame); |
|
Primiano Tucci (use gerrit)
2016/04/07 15:51:56
Do we really need both versions here?
It seem that
Dmitry Skiba
2016/04/12 18:22:11
Done.
| |
| 57 | 63 |
| 58 // Pops a frame from the thread-local pseudo stack. | 64 // Pops a frame from the thread-local pseudo stack. |
| 59 void PopPseudoStackFrame(StackFrame frame); | 65 void PopPseudoStackFrame(const void* frame_value); |
| 60 | 66 |
| 61 // Push and pop current task's context. A stack is used to support nested | 67 // Push and pop current task's context. A stack is used to support nested |
| 62 // tasks and the top of the stack will be used in allocation context. | 68 // tasks and the top of the stack will be used in allocation context. |
| 63 void PushCurrentTaskContext(const char* context); | 69 void PushCurrentTaskContext(const char* context); |
| 64 void PopCurrentTaskContext(const char* context); | 70 void PopCurrentTaskContext(const char* context); |
| 65 | 71 |
| 66 // Returns a snapshot of the current thread-local context. | 72 // Returns a snapshot of the current thread-local context. |
| 67 AllocationContext GetContextSnapshot(); | 73 AllocationContext GetContextSnapshot(); |
| 68 | 74 |
| 69 ~AllocationContextTracker(); | 75 ~AllocationContextTracker(); |
| 70 | 76 |
| 71 private: | 77 private: |
| 72 AllocationContextTracker(); | 78 AllocationContextTracker(); |
| 73 | 79 |
| 74 static subtle::Atomic32 capture_enabled_; | 80 static subtle::Atomic32 capture_mode_; |
| 75 | 81 |
| 76 // The pseudo stack where frames are |TRACE_EVENT| names. | 82 // The pseudo stack where frames are |TRACE_EVENT| names. |
| 77 std::vector<StackFrame> pseudo_stack_; | 83 std::vector<StackFrame> pseudo_stack_; |
| 78 | 84 |
| 79 // The thread name is used as the first entry in the pseudo stack. | 85 // The thread name is used as the first entry in the pseudo stack. |
| 80 const char* thread_name_; | 86 const char* thread_name_; |
| 81 | 87 |
| 82 // Stack of tasks' contexts. Context serves as a different dimension than | 88 // Stack of tasks' contexts. Context serves as a different dimension than |
| 83 // pseudo stack to cluster allocations. | 89 // pseudo stack to cluster allocations. |
| 84 std::vector<const char*> task_contexts_; | 90 std::vector<const char*> task_contexts_; |
| 85 | 91 |
| 86 DISALLOW_COPY_AND_ASSIGN(AllocationContextTracker); | 92 DISALLOW_COPY_AND_ASSIGN(AllocationContextTracker); |
| 87 }; | 93 }; |
| 88 | 94 |
| 89 } // namespace trace_event | 95 } // namespace trace_event |
| 90 } // namespace base | 96 } // namespace base |
| 91 | 97 |
| 92 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ | 98 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ |
| OLD | NEW |