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

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

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 #include "base/trace_event/memory_profiler_allocation_context.h"
6
7 #include "base/threading/thread_local_storage.h"
8
9 namespace base {
10 namespace trace_event {
11
12 subtle::Atomic32 AllocationContextManager::capture_enabled_ = 0;
13
14 namespace {
15
16 // The generation counter is incremented every time capturing is enabled. This
17 // is required to discard leftover context when capturing is enabled a second
18 // time. The generation is monotonically increasing.
19 int32_t g_capture_generation = 0;
20 ThreadLocalStorage::StaticSlot g_tls_alloc_ctx_mgr = TLS_INITIALIZER;
21
22 } // namespace
23
24 Stack::Stack() {}
25 Stack::~Stack() {}
26
27 AllocationContextManager::AllocationContextManager(int32_t generation)
28 : generation_(generation) {}
29 AllocationContextManager::~AllocationContextManager() {}
30
31 // static
32 void AllocationContextManager::SetCaptureEnabled(bool enabled) {
33 if (!!subtle::NoBarrier_Load(&capture_enabled_) == enabled)
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 Instead of re-using subtle just use capture_enable
Ruud van Asseldonk 2015/09/22 13:29:18 Done.
34 return;
35
36 // There is no memory barrier here for performance reasons, a little lag is
37 // not an issue.
38 subtle::NoBarrier_Store(&capture_enabled_, enabled);
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 You want to set this after the generation, right?
Ruud van Asseldonk 2015/09/22 13:29:18 Strictly speaking, yes. But both are nobarrier, so
39
40 // Assign a fresh generation number to this capture session.
41 if (enabled)
42 subtle::NoBarrier_AtomicIncrement(&g_capture_generation, 1);
Primiano Tucci (use gerrit) 2015/09/22 10:15:01 plz use the StaticAtomicCounter here. There is way
Ruud van Asseldonk 2015/09/22 13:29:18 Agreed offline to remove the generation altogether
43 }
44
45 // This function is added to the TLS slot to clean up the instance when the
46 // thread exits.
47 void DestructAllocationContextManager(void* alloc_ctx_mgr) {
48 delete static_cast<AllocationContextManager*>(alloc_ctx_mgr);
49 }
50
51 // static
52 AllocationContextManager* AllocationContextManager::GetInstance() {
53 if (!g_tls_alloc_ctx_mgr.initialized())
54 g_tls_alloc_ctx_mgr.Initialize(DestructAllocationContextManager);
55
56 // |mgr| will be null if it was not initialized.
57 auto mgr = static_cast<AllocationContextManager*>(g_tls_alloc_ctx_mgr.Get());
58
59 // If there is an allocation context manager, it might be left over from a
60 // previous capture, tracing was disabled and enabled again. Context still
61 // in the manager belongs to the previous capture, so start over clean.
62 if (mgr &&
63 mgr->generation_ != subtle::NoBarrier_Load(&g_capture_generation)) {
Primiano Tucci (use gerrit) 2015/09/22 10:15:00 Keep g_capture_generation as a plain int32 and rem
Ruud van Asseldonk 2015/09/22 13:29:18 Not relevant any more.
64 delete mgr;
65 mgr = nullptr;
66 }
67
68 if (!mgr) {
69 mgr = new AllocationContextManager(
70 subtle::NoBarrier_Load(&g_capture_generation));
71 g_tls_alloc_ctx_mgr.Set(static_cast<void*>(mgr));
Primiano Tucci (use gerrit) 2015/09/22 10:15:00 uh is this cast to void* required?
Ruud van Asseldonk 2015/09/22 13:29:18 The TLS slot stores |void*|, but apparently C++ ha
72 }
73
74 return mgr;
75 }
76
77 AllocationContext AllocationContextManager::GetContext() const {
78 // TODO(ruuda): Implement this in a follow-up CL.
79 return AllocationContext();
80 }
81
82 } // namespace trace_event
83 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698