Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 static ThreadLocalStorage::StaticSlot tls_pseudo_stack = TLS_INITIALIZER; | |
| 15 static int32_t g_capture_generation = 0; | |
| 16 | |
| 17 PseudoStack::PseudoStack(int32_t generation) : generation_(generation) {} | |
| 18 PseudoStack::~PseudoStack() {} | |
| 19 | |
| 20 void DestructPseudoStack(void* pseudo_stack) { | |
| 21 delete static_cast<PseudoStack*>(pseudo_stack); | |
| 22 } | |
| 23 | |
| 24 PseudoStack* PseudoStack::GetInstance() { | |
| 25 PseudoStack* ps = nullptr; | |
| 26 | |
| 27 if (tls_pseudo_stack.initialized()) { | |
| 28 ps = static_cast<PseudoStack*>(tls_pseudo_stack.Get()); | |
| 29 } | |
| 30 | |
| 31 // If there is a stack, it might be left over from a previous capture, tracing | |
| 32 // was disabled and enabled again. Frames still on the stack belong to the | |
| 33 // previous capture, so start over with an empty stack. | |
| 34 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.
| |
| 35 delete ps; | |
| 36 ps = nullptr; | |
| 37 } | |
| 38 | |
| 39 if (ps == nullptr) { | |
| 40 ps = new PseudoStack(subtle::NoBarrier_Load(&g_capture_generation)); | |
| 41 tls_pseudo_stack.Initialize(DestructPseudoStack); | |
| 42 tls_pseudo_stack.Set(static_cast<void*>(ps)); | |
| 43 } | |
| 44 | |
| 45 return ps; | |
| 46 } | |
| 47 | |
| 48 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
| |
| 49 | |
| 50 PseudoStackController::PseudoStackController() { | |
| 51 TraceLog::GetInstance()->AddEnabledStateObserver(this); | |
| 52 } | |
| 53 | |
| 54 PseudoStackController::~PseudoStackController() { | |
| 55 TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | |
| 56 } | |
| 57 | |
| 58 void PseudoStackController::OnTraceLogEnabled() { | |
| 59 // TODO(ruuda): For the moment, couple this to the memory-infra category. | |
| 60 // This can later be changed to enable more fine-grained control based on the | |
| 61 // trace config. | |
| 62 bool category_enabled; | |
| 63 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("memory-infra"), | |
| 64 &category_enabled); | |
| 65 | |
| 66 if (category_enabled) { | |
| 67 capture_enabled_ = true; | |
| 68 subtle::NoBarrier_AtomicIncrement(&g_capture_generation, 1); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void PseudoStackController::OnTraceLogDisabled() { | |
| 73 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
| |
| 74 } | |
| 75 | |
| 76 } // namespace trace_event | |
| 77 } // namespace base | |
| OLD | NEW |