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

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

Issue 1916843003: Ensure Origin Thread Affinity for BlameContext (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use origin thread affinity Created 4 years, 8 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/thread_task_runner_handle.h"
8 #include "base/trace_event/trace_event.h" 9 #include "base/trace_event/trace_event.h"
9 #include "base/trace_event/trace_event_argument.h" 10 #include "base/trace_event/trace_event_argument.h"
10 11
11 namespace base { 12 namespace base {
12 namespace trace_event { 13 namespace trace_event {
13 14
14 BlameContext::BlameContext(const char* category, 15 BlameContext::BlameContext(const char* category,
15 const char* name, 16 const char* name,
16 const char* type, 17 const char* type,
17 const char* scope, 18 const char* scope,
18 int64_t id, 19 int64_t id,
19 const BlameContext* parent_context) 20 const BlameContext* parent_context)
20 : category_(category), 21 : category_(category),
21 name_(name), 22 name_(name),
22 type_(type), 23 type_(type),
23 scope_(scope), 24 scope_(scope),
24 id_(id), 25 id_(id),
25 parent_scope_(parent_context ? parent_context->scope() : nullptr), 26 parent_scope_(parent_context ? parent_context->scope() : nullptr),
26 parent_id_(parent_context ? parent_context->id() : 0), 27 parent_id_(parent_context ? parent_context->id() : 0),
27 category_group_enabled_(nullptr) { 28 category_group_enabled_(nullptr),
29 task_runner_(ThreadTaskRunnerHandle::Get()),
30 weak_factory_(this) {
28 DCHECK(!parent_context || !std::strcmp(name_, parent_context->name())) 31 DCHECK(!parent_context || !std::strcmp(name_, parent_context->name()))
29 << "Parent blame context must have the same name"; 32 << "Parent blame context must have the same name";
30 } 33 }
31 34
32 BlameContext::~BlameContext() { 35 BlameContext::~BlameContext() {
33 DCHECK(WasInitialized()); 36 DCHECK(WasInitialized());
34 TRACE_EVENT_API_ADD_TRACE_EVENT( 37 TRACE_EVENT_API_ADD_TRACE_EVENT(
35 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_, 38 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_,
36 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); 39 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID);
37 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); 40 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this);
38 } 41 }
39 42
40 void BlameContext::Enter() { 43 void BlameContext::Enter() {
41 DCHECK(WasInitialized()); 44 DCHECK(WasInitialized());
42 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT, 45 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT,
43 category_group_enabled_, name_, scope_, id_, 46 category_group_enabled_, name_, scope_, id_,
44 0 /* num_args */, nullptr, nullptr, nullptr, 47 0 /* num_args */, nullptr, nullptr, nullptr,
45 nullptr, TRACE_EVENT_FLAG_HAS_ID); 48 nullptr, TRACE_EVENT_FLAG_HAS_ID);
46 } 49 }
47 50
48 void BlameContext::Leave() { 51 void BlameContext::Leave() {
49 DCHECK(WasInitialized()); 52 DCHECK(WasInitialized());
50 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT, 53 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT,
51 category_group_enabled_, name_, scope_, id_, 54 category_group_enabled_, name_, scope_, id_,
52 0 /* num_args */, nullptr, nullptr, nullptr, 55 0 /* num_args */, nullptr, nullptr, nullptr,
53 nullptr, TRACE_EVENT_FLAG_HAS_ID); 56 nullptr, TRACE_EVENT_FLAG_HAS_ID);
54 } 57 }
55 58
56 void BlameContext::TakeSnapshot() { 59 void BlameContext::TakeSnapshot() {
60 DCHECK(thread_checker_.CalledOnValidThread());
57 DCHECK(WasInitialized()); 61 DCHECK(WasInitialized());
58 if (!*category_group_enabled_) 62 if (!*category_group_enabled_)
59 return; 63 return;
60 std::unique_ptr<trace_event::TracedValue> snapshot( 64 std::unique_ptr<trace_event::TracedValue> snapshot(
61 new trace_event::TracedValue); 65 new trace_event::TracedValue);
62 AsValueInto(snapshot.get()); 66 AsValueInto(snapshot.get());
63 static const char* kArgName = "snapshot"; 67 static const char* kArgName = "snapshot";
64 const int kNumArgs = 1; 68 const int kNumArgs = 1;
65 unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE}; 69 unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE};
66 std::unique_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = { 70 std::unique_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = {
67 std::move(snapshot)}; 71 std::move(snapshot)};
68 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, 72 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT,
69 category_group_enabled_, type_, scope_, id_, 73 category_group_enabled_, type_, scope_, id_,
70 kNumArgs, &kArgName, arg_types, nullptr, 74 kNumArgs, &kArgName, arg_types, nullptr,
71 arg_values, TRACE_EVENT_FLAG_HAS_ID); 75 arg_values, TRACE_EVENT_FLAG_HAS_ID);
72 } 76 }
73 77
78 // Called by TraceLog from a thread possibly different from the one where this
79 // BlameContext instance was constructed. Since any access to TraceLog's list
80 // of observers is lock-guarded, we can access members of BlameContext from a
81 // different thread via a raw pointer safely without worrying about threading
82 // issues or pointer validity.
74 void BlameContext::OnTraceLogEnabled() { 83 void BlameContext::OnTraceLogEnabled() {
75 DCHECK(WasInitialized()); 84 DCHECK(WasInitialized());
76 TakeSnapshot(); 85 if (task_runner_->BelongsToCurrentThread())
86 TakeSnapshot();
87 else
88 task_runner_->PostTask(FROM_HERE, Bind(&BlameContext::TakeSnapshot,
Primiano Tucci (use gerrit) 2016/04/26 15:32:15 What happens in the case that BlameContext is dest
Sami 2016/04/26 15:45:26 BlameContext removes itself as an observer in the
89 weak_factory_.GetWeakPtr()));
77 } 90 }
78 91
79 void BlameContext::OnTraceLogDisabled() {} 92 void BlameContext::OnTraceLogDisabled() {}
80 93
81 void BlameContext::AsValueInto(trace_event::TracedValue* state) { 94 void BlameContext::AsValueInto(trace_event::TracedValue* state) {
82 DCHECK(WasInitialized()); 95 DCHECK(WasInitialized());
83 if (!parent_id_) 96 if (!parent_id_)
84 return; 97 return;
85 state->BeginDictionary("parent"); 98 state->BeginDictionary("parent");
86 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); 99 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_));
(...skipping 10 matching lines...) Expand all
97 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); 110 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this);
98 TakeSnapshot(); 111 TakeSnapshot();
99 } 112 }
100 113
101 bool BlameContext::WasInitialized() const { 114 bool BlameContext::WasInitialized() const {
102 return category_group_enabled_ != nullptr; 115 return category_group_enabled_ != nullptr;
103 } 116 }
104 117
105 } // namespace trace_event 118 } // namespace trace_event
106 } // namespace base 119 } // 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