Chromium Code Reviews| Index: base/trace_event/pseudo_stack.cc |
| diff --git a/base/trace_event/pseudo_stack.cc b/base/trace_event/pseudo_stack.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f7839e5ef8b22a4409ed683aef7c8a2c2a7510cb |
| --- /dev/null |
| +++ b/base/trace_event/pseudo_stack.cc |
| @@ -0,0 +1,77 @@ |
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/trace_event/pseudo_stack.h" |
| + |
| +#include "base/atomicops.h" |
| +#include "base/threading/thread_local_storage.h" |
| +#include "base/trace_event/trace_event.h" |
| + |
| +namespace base { |
| +namespace trace_event { |
| + |
| +static ThreadLocalStorage::StaticSlot tls_pseudo_stack = TLS_INITIALIZER; |
| +static int32_t g_capture_generation = 0; |
| + |
| +PseudoStack::PseudoStack(int32_t generation) : generation_(generation) {} |
| +PseudoStack::~PseudoStack() {} |
| + |
| +void DestructPseudoStack(void* pseudo_stack) { |
| + delete static_cast<PseudoStack*>(pseudo_stack); |
| +} |
| + |
| +PseudoStack* PseudoStack::GetInstance() { |
| + PseudoStack* ps = nullptr; |
| + |
| + if (tls_pseudo_stack.initialized()) { |
| + ps = static_cast<PseudoStack*>(tls_pseudo_stack.Get()); |
| + } |
| + |
| + // If there is a stack, it might be left over from a previous capture, tracing |
| + // was disabled and enabled again. Frames still on the stack belong to the |
| + // previous capture, so start over with an empty stack. |
| + if (ps && ps->generation() != subtle::NoBarrier_Load(&g_capture_generation)) { |
|
picksi
2015/09/14 12:52:12
nit: Should this 'if' move inside the previous if?
Ruud van Asseldonk
2015/09/14 13:16:52
Yes.
|
| + delete ps; |
| + ps = nullptr; |
| + } |
| + |
| + if (ps == nullptr) { |
| + ps = new PseudoStack(subtle::NoBarrier_Load(&g_capture_generation)); |
| + tls_pseudo_stack.Initialize(DestructPseudoStack); |
| + tls_pseudo_stack.Set(static_cast<void*>(ps)); |
| + } |
| + |
| + return ps; |
| +} |
| + |
| +bool PseudoStackController::capture_enabled_ = false; |
|
picksi
2015/09/14 12:52:12
Should this be defined a the top of the file?
Ruud van Asseldonk
2015/09/14 13:16:52
If that is the Chrome style, then yes. I put every
|
| + |
| +PseudoStackController::PseudoStackController() { |
| + TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| +} |
| + |
| +PseudoStackController::~PseudoStackController() { |
| + TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
| +} |
| + |
| +void PseudoStackController::OnTraceLogEnabled() { |
| + // TODO(ruuda): For the moment, couple this to the memory-infra category. |
| + // This can later be changed to enable more fine-grained control based on the |
| + // trace config. |
| + bool category_enabled; |
| + TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("memory-infra"), |
| + &category_enabled); |
| + |
| + if (category_enabled) { |
| + capture_enabled_ = true; |
| + subtle::NoBarrier_AtomicIncrement(&g_capture_generation, 1); |
| + } |
| +} |
| + |
| +void PseudoStackController::OnTraceLogDisabled() { |
| + capture_enabled_ = false; |
|
picksi
2015/09/14 12:52:12
Does this require the opposite of the AtomicIncrem
Ruud van Asseldonk
2015/09/14 13:16:52
No, the generation is not a refcount, it is a mono
picksi
2015/09/14 13:21:02
OK, that makes sense. Is it worth adding _UID to t
|
| +} |
| + |
| +} // namespace trace_event |
| +} // namespace base |