| 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 UnregisterFromTraceLog(); |
| 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); | |
| 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] = { |
| 67 std::move(snapshot)}; | 70 std::move(snapshot)}; |
| 68 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, | 71 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, |
| 69 category_group_enabled_, type_, scope_, id_, | 72 category_group_enabled_, type_, scope_, id_, |
| 70 kNumArgs, &kArgName, arg_types, nullptr, | 73 kNumArgs, &kArgName, arg_types, nullptr, |
| 71 arg_values, TRACE_EVENT_FLAG_HAS_ID); | 74 arg_values, TRACE_EVENT_FLAG_HAS_ID); |
| 72 } | 75 } |
| 73 | 76 |
| 77 // Called by TraceLog from a thread possibly different from the one where this |
| 78 // BlameContext instance was constructed. Since any access to TraceLog's list |
| 79 // of observers is lock-guarded, we can access members of BlameContext from a |
| 80 // different thread via a raw pointer safely without worrying about threading |
| 81 // issues or pointer validity. |
| 74 void BlameContext::OnTraceLogEnabled() { | 82 void BlameContext::OnTraceLogEnabled() { |
| 75 DCHECK(WasInitialized()); | 83 DCHECK(WasInitialized()); |
| 76 TakeSnapshot(); | 84 if (task_runner()->BelongsToCurrentThread()) |
| 85 TakeSnapshot(); |
| 86 else |
| 87 task_runner()->PostTask(FROM_HERE, |
| 88 Bind(&BlameContext::TakeSnapshot, get_weak())); |
| 77 } | 89 } |
| 78 | 90 |
| 79 void BlameContext::OnTraceLogDisabled() {} | 91 void BlameContext::OnTraceLogDisabled() {} |
| 80 | 92 |
| 81 void BlameContext::AsValueInto(trace_event::TracedValue* state) { | 93 void BlameContext::AsValueInto(trace_event::TracedValue* state) { |
| 82 DCHECK(WasInitialized()); | 94 DCHECK(WasInitialized()); |
| 83 if (!parent_id_) | 95 if (!parent_id_) |
| 84 return; | 96 return; |
| 85 state->BeginDictionary("parent"); | 97 state->BeginDictionary("parent"); |
| 86 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); | 98 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); |
| 87 state->SetString("scope", parent_scope_); | 99 state->SetString("scope", parent_scope_); |
| 88 state->EndDictionary(); | 100 state->EndDictionary(); |
| 89 } | 101 } |
| 90 | 102 |
| 91 void BlameContext::Initialize() { | 103 void BlameContext::Initialize() { |
| 92 category_group_enabled_ = | 104 category_group_enabled_ = |
| 93 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_); | 105 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_); |
| 94 TRACE_EVENT_API_ADD_TRACE_EVENT( | 106 TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 95 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, type_, scope_, | 107 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, type_, scope_, |
| 96 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); | 108 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
| 109 TakeSnapshot(); |
| 97 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); | 110 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| 98 TakeSnapshot(); | 111 registered_ = true; |
| 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 |
| 118 void BlameContext::UnregisterFromTraceLog() { |
| 119 DCHECK(WasInitialized()); |
| 120 if (!registered_) |
| 121 return; |
| 122 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
| 123 registered_ = false; |
| 124 } |
| 125 |
| 105 } // namespace trace_event | 126 } // namespace trace_event |
| 106 } // namespace base | 127 } // namespace base |
| OLD | NEW |