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

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

Issue 1340013002: Add thread-local allocation context for tracing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Do not allow frame metadata, hook begin/end macros 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 #include "base/trace_event/pseudo_stack.h"
6
7 #include "base/atomicops.h"
8 #include "base/threading/thread_local_storage.h"
9 #include "base/trace_event/trace_event.h"
10
11 namespace base {
12 namespace trace_event {
13
14 // The generation counter is incremented every time capturing is enabled. This
15 // is required to discard leftovers on the stack when capturing is enabled a
16 // second time. The generation is monotonically increasing.
17 static int32_t g_capture_generation = 0;
dsinclair 2015/09/17 14:22:32 Should this be in an anonymous namespace?
Ruud van Asseldonk 2015/09/17 15:19:56 The static makes it visible only to the current tr
18 bool g_pseudo_stack_capture_enabled = false;
19
20 static ThreadLocalStorage::StaticSlot tls_pseudo_stack = TLS_INITIALIZER;
21
22 void SetPseudoStackCaptureEnabled(bool enabled) {
23 if (g_pseudo_stack_capture_enabled == enabled)
24 return;
25
26 g_pseudo_stack_capture_enabled = enabled;
27
28 // Assign a fresh generation number to this capture session.
29 if (enabled)
30 subtle::NoBarrier_AtomicIncrement(&g_capture_generation, 1);
dsinclair 2015/09/17 14:22:31 Instead of a generation number, why not just dump
Ruud van Asseldonk 2015/09/17 15:19:56 Because capturing is enabled from one thread, but
31 }
32
33 PseudoStack::PseudoStack(int32_t generation) : generation_(generation) {}
34 PseudoStack::~PseudoStack() {}
35
36 void DestructPseudoStack(void* pseudo_stack) {
37 delete static_cast<PseudoStack*>(pseudo_stack);
38 }
39
40 PseudoStack* PseudoStack::GetInstance() {
41 PseudoStack* ps = nullptr;
42
43 if (tls_pseudo_stack.initialized()) {
44 ps = static_cast<PseudoStack*>(tls_pseudo_stack.Get());
45
46 // If there is a stack, it might be left over from a previous capture,
47 // tracing was disabled and enabled again. Frames still on the stack belong
48 // to the previous capture, so start over with an empty stack.
49 if (ps->generation() != subtle::NoBarrier_Load(&g_capture_generation)) {
50 delete ps;
dsinclair 2015/09/17 14:22:32 Why do you delete the stack and create a new one i
Ruud van Asseldonk 2015/09/17 15:19:56 Because it means that the generation can be an imm
51 ps = nullptr;
52 }
53 }
54
55 if (ps == nullptr) {
56 ps = new PseudoStack(subtle::NoBarrier_Load(&g_capture_generation));
57 tls_pseudo_stack.Initialize(DestructPseudoStack);
58 tls_pseudo_stack.Set(static_cast<void*>(ps));
59 }
60
61 return ps;
62 }
63
64 } // namespace trace_event
65 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698