| 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/debug/stack_trace.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/trace_event/heap_profiler_allocation_context.h" | 15 #include "base/trace_event/heap_profiler_allocation_context.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 namespace trace_event { | 18 namespace trace_event { |
| 18 | 19 |
| 19 // The allocation context tracker keeps track of thread-local context for heap | 20 // 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 | 21 // 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 | 22 // the tracker provides a snapshot of its context in the form of an |
| 22 // |AllocationContext| that is to be stored together with the allocation | 23 // |AllocationContext| that is to be stored together with the allocation |
| 23 // details. | 24 // details. |
| 24 class BASE_EXPORT AllocationContextTracker { | 25 class BASE_EXPORT AllocationContextTracker { |
| 25 public: | 26 public: |
| 26 // Globally enables capturing allocation context. | 27 enum class CaptureMode: int32_t { |
| 27 // TODO(ruuda): Should this be replaced by |EnableCapturing| in the future? | 28 DISABLED, // Don't capture anything |
| 28 // Or at least have something that guards agains enable -> disable -> enable? | 29 PSEUDO_STACK, // GetContextSnapshot() returns pseudo stack trace |
| 29 static void SetCaptureEnabled(bool enabled); | 30 NATIVE_STACK // GetContextSnapshot() returns native (real) stack trace |
| 31 }; |
| 30 | 32 |
| 31 // Returns whether capturing allocation context is enabled globally. | 33 // Globally sets capturing mode. |
| 32 inline static bool capture_enabled() { | 34 // TODO(primiano): How to guard against *_STACK -> DISABLED -> *_STACK? |
| 35 static void SetCaptureMode(CaptureMode mode); |
| 36 |
| 37 // Returns global capturing mode. |
| 38 inline static CaptureMode capture_mode() { |
| 33 // A little lag after heap profiling is enabled or disabled is fine, it is | 39 // 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 | 40 // 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. | 41 // not enabled, so do not issue a memory barrier in the fast path. |
| 36 if (subtle::NoBarrier_Load(&capture_enabled_) == 0) | 42 if (subtle::NoBarrier_Load(&capture_mode_) == |
| 37 return false; | 43 static_cast<int32_t>(CaptureMode::DISABLED)) |
| 44 return CaptureMode::DISABLED; |
| 38 | 45 |
| 39 // In the slow path, an acquire load is required to pair with the release | 46 // 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 | 47 // store in |SetCaptureMode|. This is to ensure that the TLS slot for |
| 41 // the thread-local allocation context tracker has been initialized if | 48 // the thread-local allocation context tracker has been initialized if |
| 42 // |capture_enabled| returns true. | 49 // |capture_mode| returns something other than DISABLED. |
| 43 return subtle::Acquire_Load(&capture_enabled_) != 0; | 50 return static_cast<CaptureMode>(subtle::Acquire_Load(&capture_mode_)); |
| 44 } | 51 } |
| 45 | 52 |
| 46 // Returns the thread-local instance, creating one if necessary. Returns | 53 // Returns the thread-local instance, creating one if necessary. Returns |
| 47 // always a valid instance, unless it is called re-entrantly, in which case | 54 // always a valid instance, unless it is called re-entrantly, in which case |
| 48 // returns nullptr in the nested calls. | 55 // returns nullptr in the nested calls. |
| 49 static AllocationContextTracker* GetInstanceForCurrentThread(); | 56 static AllocationContextTracker* GetInstanceForCurrentThread(); |
| 50 | 57 |
| 51 // Set the thread name in the AllocationContextTracker of the current thread | 58 // Set the thread name in the AllocationContextTracker of the current thread |
| 52 // if capture is enabled. | 59 // if capture is enabled. |
| 53 static void SetCurrentThreadName(const char* name); | 60 static void SetCurrentThreadName(const char* name); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 73 void PopCurrentTaskContext(const char* context); | 80 void PopCurrentTaskContext(const char* context); |
| 74 | 81 |
| 75 // Returns a snapshot of the current thread-local context. | 82 // Returns a snapshot of the current thread-local context. |
| 76 AllocationContext GetContextSnapshot(); | 83 AllocationContext GetContextSnapshot(); |
| 77 | 84 |
| 78 ~AllocationContextTracker(); | 85 ~AllocationContextTracker(); |
| 79 | 86 |
| 80 private: | 87 private: |
| 81 AllocationContextTracker(); | 88 AllocationContextTracker(); |
| 82 | 89 |
| 83 static subtle::Atomic32 capture_enabled_; | 90 static subtle::Atomic32 capture_mode_; |
| 84 | 91 |
| 85 // The pseudo stack where frames are |TRACE_EVENT| names. | 92 // The pseudo stack where frames are |TRACE_EVENT| names. |
| 86 std::vector<const char*> pseudo_stack_; | 93 std::vector<const char*> pseudo_stack_; |
| 87 | 94 |
| 88 // The thread name is used as the first entry in the pseudo stack. | 95 // The thread name is used as the first entry in the pseudo stack. |
| 89 const char* thread_name_; | 96 const char* thread_name_; |
| 90 | 97 |
| 91 // Stack of tasks' contexts. Context serves as a different dimension than | 98 // Stack of tasks' contexts. Context serves as a different dimension than |
| 92 // pseudo stack to cluster allocations. | 99 // pseudo stack to cluster allocations. |
| 93 std::vector<const char*> task_contexts_; | 100 std::vector<const char*> task_contexts_; |
| 94 | 101 |
| 95 uint32_t ignore_scope_depth_; | 102 uint32_t ignore_scope_depth_; |
| 96 | 103 |
| 97 DISALLOW_COPY_AND_ASSIGN(AllocationContextTracker); | 104 DISALLOW_COPY_AND_ASSIGN(AllocationContextTracker); |
| 98 }; | 105 }; |
| 99 | 106 |
| 100 } // namespace trace_event | 107 } // namespace trace_event |
| 101 } // namespace base | 108 } // namespace base |
| 102 | 109 |
| 103 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ | 110 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_TRACKER_H_ |
| OLD | NEW |