Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/trace_event/blame_context.h" | |
| 6 | |
| 7 #include "base/strings/stringprintf.h" | |
| 8 #include "base/trace_event/trace_event.h" | |
| 9 #include "base/trace_event/trace_event_argument.h" | |
| 10 | |
| 11 namespace base { | |
| 12 namespace trace_event { | |
| 13 | |
| 14 BlameContext::BlameContext(const char* category, | |
| 15 const char* name, | |
| 16 const char* type, | |
| 17 const char* scope, | |
| 18 int64_t id, | |
| 19 const BlameContext* parent_context) | |
| 20 : category_(category), | |
| 21 name_(name), | |
| 22 type_(type), | |
| 23 scope_(scope), | |
| 24 id_(id), | |
| 25 parent_scope_(parent_context ? parent_context->scope() : nullptr), | |
| 26 parent_id_(parent_context ? parent_context->id() : 0), | |
| 27 #ifndef NDEBUG | |
|
Primiano Tucci (use gerrit)
2016/03/21 17:12:55
Should this be just:
#if DCHECK_IS_ON() ?
and use
Sami
2016/03/21 18:26:10
Good idea, thanks. It turned out I still needed to
| |
| 28 was_initialized_(false), | |
| 29 #endif | |
| 30 category_group_enabled_(nullptr) { | |
| 31 DCHECK(!parent_context || !std::strcmp(name_, parent_context->name())) | |
| 32 << "Parent blame context must have the same name"; | |
| 33 } | |
| 34 | |
| 35 BlameContext::~BlameContext() { | |
| 36 #ifndef NDEBUG | |
| 37 DCHECK(was_initialized_); | |
| 38 #endif | |
| 39 TRACE_EVENT_API_ADD_TRACE_EVENT( | |
| 40 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_, | |
| 41 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); | |
| 42 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | |
| 43 } | |
| 44 | |
| 45 void BlameContext::Enter() { | |
| 46 #ifndef NDEBUG | |
| 47 DCHECK(was_initialized_); | |
| 48 #endif | |
| 49 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT, | |
| 50 category_group_enabled_, name_, scope_, id_, | |
| 51 0 /* num_args */, nullptr, nullptr, nullptr, | |
| 52 nullptr, TRACE_EVENT_FLAG_HAS_ID); | |
| 53 } | |
| 54 | |
| 55 void BlameContext::Leave() { | |
| 56 #ifndef NDEBUG | |
| 57 DCHECK(was_initialized_); | |
| 58 #endif | |
| 59 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT, | |
| 60 category_group_enabled_, name_, scope_, id_, | |
| 61 0 /* num_args */, nullptr, nullptr, nullptr, | |
| 62 nullptr, TRACE_EVENT_FLAG_HAS_ID); | |
| 63 } | |
| 64 | |
| 65 void BlameContext::TakeSnapshot() { | |
| 66 #ifndef NDEBUG | |
| 67 DCHECK(was_initialized_); | |
| 68 #endif | |
| 69 if (!*category_group_enabled_) | |
|
Primiano Tucci (use gerrit)
2016/03/21 17:12:55
Isn't TRACE_EVENT_API_ADD_TRACE_EVENT supposed to
Sami
2016/03/21 18:26:10
Yes, it'll be a no-op, but I also want to avoid th
| |
| 70 return; | |
| 71 scoped_ptr<trace_event::TracedValue> snapshot(new trace_event::TracedValue); | |
| 72 AsValueInto(snapshot.get()); | |
| 73 static const char* kArgName = "snapshot"; | |
| 74 const int kNumArgs = 1; | |
| 75 unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE}; | |
| 76 scoped_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = { | |
| 77 std::move(snapshot)}; | |
| 78 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, | |
| 79 category_group_enabled_, type_, scope_, id_, | |
| 80 kNumArgs, &kArgName, arg_types, nullptr, | |
| 81 arg_values, TRACE_EVENT_FLAG_HAS_ID); | |
| 82 } | |
| 83 | |
| 84 void BlameContext::OnTraceLogEnabled() { | |
| 85 #ifndef NDEBUG | |
| 86 DCHECK(was_initialized_); | |
| 87 #endif | |
| 88 TakeSnapshot(); | |
| 89 } | |
| 90 | |
| 91 void BlameContext::OnTraceLogDisabled() {} | |
| 92 | |
| 93 void BlameContext::AsValueInto(trace_event::TracedValue* state) { | |
| 94 #ifndef NDEBUG | |
| 95 DCHECK(was_initialized_); | |
| 96 #endif | |
| 97 if (!parent_id_) | |
| 98 return; | |
| 99 state->BeginDictionary("parent"); | |
| 100 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); | |
| 101 state->SetString("scope", parent_scope_); | |
| 102 state->EndDictionary(); | |
| 103 } | |
| 104 | |
| 105 void BlameContext::Initialize() { | |
| 106 #ifndef NDEBUG | |
| 107 was_initialized_ = true; | |
| 108 #endif | |
| 109 category_group_enabled_ = | |
| 110 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_); | |
| 111 TRACE_EVENT_API_ADD_TRACE_EVENT( | |
| 112 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, type_, scope_, | |
| 113 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); | |
| 114 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); | |
| 115 TakeSnapshot(); | |
| 116 } | |
| 117 | |
| 118 } // namespace trace_event | |
| 119 } // namespace base | |
| OLD | NEW |