Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: base/trace_event/memory_profiler_allocation_context.h

Issue 1340013002: Add thread-local allocation context for tracing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make GetThreadLocalTracker a private static method Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
Primiano Tucci (use gerrit) 2015/09/24 14:45:20 I'd probably reword this a bit. By reading it feel
Ruud van Asseldonk 2015/09/24 15:16:21 Done.
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;
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() {
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 public:
58 // Globally enables capturing allocation context.
59 // TODO(ruuda): Should this be replaced by |EnableCapturing| in the future?
60 // Or at least have something that guards agains enable -> disable -> enable?
61 static void SetCaptureEnabled(bool enabled);
62
63 // Returns whether capturing allocation context is enabled globally.
64 inline static bool capture_enabled() {
65 // A little lag after heap profiling is enabled or disabled is fine, it is
66 // more important that the check is as cheap as possible when capturing is
67 // not enabled, so do not issue a memory barrier.
68 return subtle::NoBarrier_Load(&capture_enabled_) != 0;
69 }
70
71 // Pushes a frame onto the thread-local pseudo stack.
72 static void PushPseudoStackFrame(StackFrame frame);
73
74 // Pops a frame from the thread-local pseudo stack.
75 static void PopPseudoStackFrame();
76
77 // Sets a thread-local (key, value) pair.
78 static void SetContextField(const char* key, const char* value);
79
80 // Removes the (key, value) pair with the specified key from the thread-local
81 // context.
82 static void UnsetContextField(const char* key);
83
84 // Returns a snapshot of the current thread-local context.
85 static AllocationContext GetContext();
86
87 // TODO(ruuda): Remove in a follow-up CL, this is only used for testing now.
88 static AllocationStack* GetPseudoStackForTesting();
89
90 ~AllocationContextTracker();
91
92 private:
93 AllocationContextTracker();
94
95 static AllocationContextTracker* GetThreadLocalTracker();
96
97 static subtle::Atomic32 capture_enabled_;
98
99 // The pseudo stack where frames are |TRACE_EVENT| names.
100 AllocationStack pseudo_stack_;
101
102 // A dictionary of arbitrary context.
103 SmallMap<std::map<const char*, const char*>> context_;
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698