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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/trace_event/pseudo_stack.h ('k') | base/trace_event/pseudo_stack_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « base/trace_event/pseudo_stack.h ('k') | base/trace_event/pseudo_stack_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698