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

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

Issue 1340013002: Add thread-local allocation context for tracing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix typo 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 (c) 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_PSEUDO_STACK_H_
6 #define BASE_TRACE_EVENT_PSEUDO_STACK_H_
7
8 #include <vector>
9
10 #include "base/base_export.h"
11 #include "base/trace_event/trace_log.h"
12
13 namespace base {
14 namespace trace_event {
15
16 struct PseudoStackFrame {
17 const char* name;
18 // TODO(ruuda): Add metadata fields so we can add more context to the stack
19 // frames. For instance, having TaskQueueManager::PostTask at the top of the
20 // pseudo stack is not very helpful, but the scheduler has valuable metadata
21 // like the location that the task was posted from.
22 };
23
24 // A stack that is kept per thread in TLS to keep track of trace events. Unlike
25 // |std::stack| it allows iterating the stack and it guards for underflow.
26 // Furthermore it has a generation to prevent mixing stacks from different
27 // traces.
28 class BASE_EXPORT PseudoStack {
29 public:
30 typedef std::vector<PseudoStackFrame>::const_reverse_iterator ConstIterator;
31
32 PseudoStack(int32_t generation);
33 ~PseudoStack();
34
35 inline ConstIterator Top() const { return stack_.rbegin(); }
36 inline ConstIterator Bottom() const { return stack_.rend(); }
37
38 inline void Push(PseudoStackFrame frame) { stack_.push_back(frame); }
39 inline void Pop() {
40 if (!stack_.empty())
41 stack_.pop_back();
42 }
43
44 // Returns the thread-local pseudo stack for the current generation, creating
45 // it if it does not exist.
46 static PseudoStack* GetInstance();
47
48 // The pseudo stack is only valid when its generation matches the current
49 // generation. A new generation starts when tracing is enabled, and ends when
50 // tracing is disabled.
51 inline int32_t generation() const { return generation_; }
52
53 private:
54 std::vector<PseudoStackFrame> stack_;
55 const int32_t generation_;
56 };
57
58 class BASE_EXPORT PseudoStackController final
59 : public TraceLog::EnabledStateObserver {
60 public:
61 PseudoStackController();
62 ~PseudoStackController() override;
63
64 void OnTraceLogEnabled() override;
65 void OnTraceLogDisabled() override;
66
67 // This has no memory barriers for performance reasons. A little lag after
68 // enabling or disabling capturing is not a problem, and pop needs to deal
69 // with underflow either way.
70 static bool capture_enabled() { return capture_enabled_; }
71
72 private:
73 // This is a static member instead of a member of a singleton, to avoid the
74 // extra indirection.
75 static bool capture_enabled_;
76 };
77
78 } // namespace trace_event
79 } // namespace base
80
81 #endif // BASE_TRACE_EVENT_PSEUDO_STACK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698