Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 non-main thread. Since access to TraceLog's list | |
|
Sami
2016/04/26 11:00:32
I'd suggest talking about an "origin" thread (i.e.
Xiaocheng
2016/04/26 12:48:21
Yeah, that's a better idea.
| |
| 79 // of observers is lock-guarded, we can use task_runner_ and weak_factory_ from | |
| 80 // a different thread safely without worrying about threading issues. | |
|
Sami
2016/04/26 11:00:32
nit: task_runner_ is thread safe, as is getting a
Xiaocheng
2016/04/26 12:48:21
Sorry for the ambiguity.
I just want to say that
Sami
2016/04/26 12:57:12
I see. When a BlameContext is deleted, it removes
| |
| 74 void BlameContext::OnTraceLogEnabled() { | 81 void BlameContext::OnTraceLogEnabled() { |
| 75 DCHECK(WasInitialized()); | 82 DCHECK(WasInitialized()); |
| 76 TakeSnapshot(); | 83 task_runner_->PostTask(FROM_HERE, Bind(&BlameContext::TakeSnapshot, |
| 84 weak_factory_.GetWeakPtr())); | |
| 77 } | 85 } |
| 78 | 86 |
| 79 void BlameContext::OnTraceLogDisabled() {} | 87 void BlameContext::OnTraceLogDisabled() {} |
| 80 | 88 |
| 81 void BlameContext::AsValueInto(trace_event::TracedValue* state) { | 89 void BlameContext::AsValueInto(trace_event::TracedValue* state) { |
| 82 DCHECK(WasInitialized()); | 90 DCHECK(WasInitialized()); |
| 83 if (!parent_id_) | 91 if (!parent_id_) |
| 84 return; | 92 return; |
| 85 state->BeginDictionary("parent"); | 93 state->BeginDictionary("parent"); |
| 86 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); | 94 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 97 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); | 105 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| 98 TakeSnapshot(); | 106 TakeSnapshot(); |
| 99 } | 107 } |
| 100 | 108 |
| 101 bool BlameContext::WasInitialized() const { | 109 bool BlameContext::WasInitialized() const { |
| 102 return category_group_enabled_ != nullptr; | 110 return category_group_enabled_ != nullptr; |
| 103 } | 111 } |
| 104 | 112 |
| 105 } // namespace trace_event | 113 } // namespace trace_event |
| 106 } // namespace base | 114 } // namespace base |
| OLD | NEW |