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

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

Issue 1956333002: Change the base class of BlameContext into AsyncEnabledStateObserver (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ThreadSafeObserver
Patch Set: Add note on thread safety for Enter() and Leave() Created 4 years, 7 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
« no previous file with comments | « base/trace_event/blame_context.h ('k') | base/trace_event/blame_context_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/blame_context.h" 5 #include "base/trace_event/blame_context.h"
6 6
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "base/trace_event/trace_event.h" 8 #include "base/trace_event/trace_event.h"
9 #include "base/trace_event/trace_event_argument.h" 9 #include "base/trace_event/trace_event_argument.h"
10 10
11 namespace base { 11 namespace base {
12 namespace trace_event { 12 namespace trace_event {
13 13
14 BlameContext::BlameContext(const char* category, 14 BlameContext::BlameContext(const char* category,
15 const char* name, 15 const char* name,
16 const char* type, 16 const char* type,
17 const char* scope, 17 const char* scope,
18 int64_t id, 18 int64_t id,
19 const BlameContext* parent_context) 19 const BlameContext* parent_context)
20 : category_(category), 20 : category_(category),
21 name_(name), 21 name_(name),
22 type_(type), 22 type_(type),
23 scope_(scope), 23 scope_(scope),
24 id_(id), 24 id_(id),
25 parent_scope_(parent_context ? parent_context->scope() : nullptr), 25 parent_scope_(parent_context ? parent_context->scope() : nullptr),
26 parent_id_(parent_context ? parent_context->id() : 0), 26 parent_id_(parent_context ? parent_context->id() : 0),
27 category_group_enabled_(nullptr) { 27 category_group_enabled_(nullptr),
28 weak_factory_(this) {
28 DCHECK(!parent_context || !std::strcmp(name_, parent_context->name())) 29 DCHECK(!parent_context || !std::strcmp(name_, parent_context->name()))
29 << "Parent blame context must have the same name"; 30 << "Parent blame context must have the same name";
30 } 31 }
31 32
32 BlameContext::~BlameContext() { 33 BlameContext::~BlameContext() {
34 DCHECK(thread_checker_.CalledOnValidThread());
33 DCHECK(WasInitialized()); 35 DCHECK(WasInitialized());
34 TRACE_EVENT_API_ADD_TRACE_EVENT( 36 TRACE_EVENT_API_ADD_TRACE_EVENT(
35 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_, 37 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_,
36 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); 38 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID);
37 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); 39 trace_event::TraceLog::GetInstance()->RemoveAsyncEnabledStateObserver(this);
38 } 40 }
39 41
40 void BlameContext::Enter() { 42 void BlameContext::Enter() {
41 DCHECK(WasInitialized()); 43 DCHECK(WasInitialized());
42 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT, 44 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT,
43 category_group_enabled_, name_, scope_, id_, 45 category_group_enabled_, name_, scope_, id_,
44 0 /* num_args */, nullptr, nullptr, nullptr, 46 0 /* num_args */, nullptr, nullptr, nullptr,
45 nullptr, TRACE_EVENT_FLAG_HAS_ID); 47 nullptr, TRACE_EVENT_FLAG_HAS_ID);
46 } 48 }
47 49
48 void BlameContext::Leave() { 50 void BlameContext::Leave() {
49 DCHECK(WasInitialized()); 51 DCHECK(WasInitialized());
50 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT, 52 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT,
51 category_group_enabled_, name_, scope_, id_, 53 category_group_enabled_, name_, scope_, id_,
52 0 /* num_args */, nullptr, nullptr, nullptr, 54 0 /* num_args */, nullptr, nullptr, nullptr,
53 nullptr, TRACE_EVENT_FLAG_HAS_ID); 55 nullptr, TRACE_EVENT_FLAG_HAS_ID);
54 } 56 }
55 57
56 void BlameContext::TakeSnapshot() { 58 void BlameContext::TakeSnapshot() {
59 DCHECK(thread_checker_.CalledOnValidThread());
57 DCHECK(WasInitialized()); 60 DCHECK(WasInitialized());
58 if (!*category_group_enabled_) 61 if (!*category_group_enabled_)
59 return; 62 return;
60 std::unique_ptr<trace_event::TracedValue> snapshot( 63 std::unique_ptr<trace_event::TracedValue> snapshot(
61 new trace_event::TracedValue); 64 new trace_event::TracedValue);
62 AsValueInto(snapshot.get()); 65 AsValueInto(snapshot.get());
63 static const char* kArgName = "snapshot"; 66 static const char* kArgName = "snapshot";
64 const int kNumArgs = 1; 67 const int kNumArgs = 1;
65 unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE}; 68 unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE};
66 std::unique_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = { 69 std::unique_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = {
(...skipping 15 matching lines...) Expand all
82 DCHECK(WasInitialized()); 85 DCHECK(WasInitialized());
83 if (!parent_id_) 86 if (!parent_id_)
84 return; 87 return;
85 state->BeginDictionary("parent"); 88 state->BeginDictionary("parent");
86 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); 89 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_));
87 state->SetString("scope", parent_scope_); 90 state->SetString("scope", parent_scope_);
88 state->EndDictionary(); 91 state->EndDictionary();
89 } 92 }
90 93
91 void BlameContext::Initialize() { 94 void BlameContext::Initialize() {
95 DCHECK(thread_checker_.CalledOnValidThread());
92 category_group_enabled_ = 96 category_group_enabled_ =
93 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_); 97 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_);
94 TRACE_EVENT_API_ADD_TRACE_EVENT( 98 TRACE_EVENT_API_ADD_TRACE_EVENT(
95 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, type_, scope_, 99 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, type_, scope_,
96 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); 100 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID);
97 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); 101 trace_event::TraceLog::GetInstance()->AddAsyncEnabledStateObserver(
102 weak_factory_.GetWeakPtr());
98 TakeSnapshot(); 103 TakeSnapshot();
99 } 104 }
100 105
101 bool BlameContext::WasInitialized() const { 106 bool BlameContext::WasInitialized() const {
102 return category_group_enabled_ != nullptr; 107 return category_group_enabled_ != nullptr;
103 } 108 }
104 109
105 } // namespace trace_event 110 } // namespace trace_event
106 } // namespace base 111 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/blame_context.h ('k') | base/trace_event/blame_context_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698