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

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: Address primiano comments 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 <map>
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 you can remove this include at this point
Ruud van Asseldonk 2015/09/22 13:29:19 Done.
9 #include <vector>
10
11 #include "base/atomicops.h"
12 #include "base/base_export.h"
13 #include "base/containers/small_map.h"
14 #include "base/trace_event/trace_log.h"
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 Just checking, what do you need this for?
Ruud van Asseldonk 2015/09/22 13:29:19 Nothing.
15
16 namespace base {
17 namespace trace_event {
18
19 // The allocation context is attached to every allocation when heap profiling
20 // is enabled. It is generated by the |AllocationContextManager| which keeps
21 // stacks of context in TLS. The manager is initialized lazily. Every time heap
22 // profiling is enabled, a global generation counter is incremented. This
23 // allows for context left over from a previous profiling session to be
24 // discarded.
25
26 typedef const char* StackFrame;
27
28 // A simple stack of |StackFrame| that unlike |std::stack| allows iterating
29 // the stack and guards for underflow.
30 class BASE_EXPORT Stack {
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 Stack feels a bit too generic as a name. Should th
Ruud van Asseldonk 2015/09/22 13:29:19 Done.
31 public:
32 typedef std::vector<StackFrame>::const_reverse_iterator ConstIterator;
33
34 Stack();
35 ~Stack();
36
37 inline ConstIterator Top() const { return stack_.rbegin(); }
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 nit: lowercase T, same below, as these are trivial
Ruud van Asseldonk 2015/09/22 13:29:19 Done.
38 inline ConstIterator Bottom() const { return stack_.rend(); }
39
40 inline void Push(StackFrame item) { stack_.push_back(item); }
41 inline void Pop() {
42 if (!stack_.empty())
43 stack_.pop_back();
44 }
45
46 private:
47 std::vector<StackFrame> stack_;
48
49 DISALLOW_COPY_AND_ASSIGN(Stack);
50 };
51
52 class BASE_EXPORT AllocationContext {
53 // TODO(ruuda): Fill this in a follow-up CL.
54 };
55
56 class BASE_EXPORT AllocationContextManager {
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 Please add a few lines comment explaining what thi
Ruud van Asseldonk 2015/09/22 13:29:18 Done.
57 public:
58 // Globally enables or disables capturing allocation context.
59 static void SetCaptureEnabled(bool enabled);
60
61 // Returns whether capturing allocation context is enabled globally.
62 inline static bool capture_enabled() {
63 // A little lag after heap profiling is enabled or disabled is fine, it is
64 // more important that the check is as cheap as possible when capturing is
65 // not enabled, so issue no memory barrier.
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 nit: don't issue any memory barrier
Ruud van Asseldonk 2015/09/22 13:29:18 Done.
66 return subtle::NoBarrier_Load(&capture_enabled_) != 0;
67 }
68
69 // Returns the thread-local allocation context manager for the current
70 // generation, creating it if it does not exist.
71 static AllocationContextManager* GetInstance();
72
73 // Returns the pseudo stack where frames are |TRACE_EVENT| names. These macros
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 I'd keep this a bit more generic and say: Returns
Ruud van Asseldonk 2015/09/22 13:29:19 Done. And then made irrelevant because the stack w
74 // automatically push and pop, the stack should not be modified manually.
75 inline Stack* pseudo_stack() { return &pseudo_stack_; }
76
77 inline void SetContextField(const char* key, const char* value) {
78 context_[key] = value;
79 }
80
81 inline void UnsetContextField(const char* key) {
82 auto pos = context_.find(key);
83 if (pos != context_.end())
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 isn't all this == to context_.erase(key) ?
Ruud van Asseldonk 2015/09/22 13:29:19 Yep. Somehow I was under the illusion that |erase(
84 context_.erase(pos);
85 }
86
87 // Returns a snapshot of the current context.
88 AllocationContext GetContext() const;
89
90 ~AllocationContextManager();
91
92 private:
93 AllocationContextManager(int32_t generation);
94
95 // The context in this manager is only valid when its generation matches the
96 // current generation.
97 const int32_t generation_;
98
99 // The pseudo stack where frames are |TRACE_EVENT| names.
100 Stack pseudo_stack_;
101
102 // A dictionary of arbitrary context.
103 SmallMap<std::map<const char*, const char*>> context_;
104
105 static subtle::Atomic32 capture_enabled_;
106
107 DISALLOW_COPY_AND_ASSIGN(AllocationContextManager);
108 };
109
110 } // namespace trace_event
111 } // namespace base
112
113 #endif // BASE_TRACE_EVENT_MEMORY_PROFILER_ALLOCATION_CONTEXT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698