Chromium Code Reviews| Index: base/trace_event/blame_context.cc |
| diff --git a/base/trace_event/blame_context.cc b/base/trace_event/blame_context.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4bb8aaae5f0565ed8f0c0b09f0b96422ee2cf108 |
| --- /dev/null |
| +++ b/base/trace_event/blame_context.cc |
| @@ -0,0 +1,111 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/trace_event/blame_context.h" |
| + |
| +#include "base/strings/stringprintf.h" |
| +#include "base/trace_event/trace_event.h" |
| +#include "base/trace_event/trace_event_argument.h" |
| + |
| +namespace base { |
| +namespace trace_event { |
| + |
| +BlameContext::BlameContext(const char* category, |
| + const char* name, |
| + const char* type, |
| + const char* scope, |
| + int64_t id) |
| + : category_(category), |
| + name_(name), |
| + type_(type), |
| + scope_(scope), |
| + id_(id), |
| + parent_scope_(nullptr), |
| + parent_id_(0) { |
| + Initialize(); |
| +} |
| + |
| +BlameContext::BlameContext(const char* category, |
| + const char* name, |
| + const char* type, |
| + const char* scope, |
| + int64_t id, |
| + const BlameContext& parent_context) |
| + : category_(category), |
| + name_(name), |
| + type_(type), |
| + scope_(scope), |
| + id_(id), |
| + parent_scope_(parent_context.scope()), |
| + parent_id_(parent_context.id()) { |
| + DCHECK(!std::strcmp(name_, parent_context.name())) |
| + << "Parent blame context must have the same name"; |
| + Initialize(); |
| +} |
| + |
| +BlameContext::~BlameContext() { |
| + TRACE_EVENT_API_ADD_TRACE_EVENT( |
| + TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_, |
| + id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
| + trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
| +} |
| + |
| +void BlameContext::Enter() { |
| + int num_args = 0; |
| + TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT, |
| + category_group_enabled_, name_, scope_, id_, |
| + num_args, nullptr, nullptr, nullptr, nullptr, |
|
Primiano Tucci (use gerrit)
2016/03/21 14:26:29
or just 0 /* num_args */ if you prefer :)
Sami
2016/03/21 15:05:08
Done.
|
| + TRACE_EVENT_FLAG_HAS_ID); |
| +} |
| + |
| +void BlameContext::Leave() { |
| + int num_args = 0; |
| + TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT, |
| + category_group_enabled_, name_, scope_, id_, |
| + num_args, nullptr, nullptr, nullptr, nullptr, |
| + TRACE_EVENT_FLAG_HAS_ID); |
| +} |
| + |
| +void BlameContext::TakeSnapshot() { |
| + if (!*category_group_enabled_) |
| + return; |
| + scoped_ptr<trace_event::TracedValue> snapshot(new trace_event::TracedValue); |
| + AsValueInto(snapshot.get()); |
| + static const char* kArgName = "snapshot"; |
| + int num_args = 1; |
|
Primiano Tucci (use gerrit)
2016/03/21 14:26:29
nit: kNumArgs?
Sami
2016/03/21 15:05:08
Done.
|
| + unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE}; |
| + scoped_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = { |
| + std::move(snapshot)}; |
| + TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, |
| + category_group_enabled_, type_, scope_, id_, |
| + num_args, &kArgName, arg_types, nullptr, |
| + arg_values, TRACE_EVENT_FLAG_HAS_ID); |
| +} |
| + |
| +void BlameContext::OnTraceLogEnabled() { |
| + TakeSnapshot(); |
|
Primiano Tucci (use gerrit)
2016/03/21 14:26:29
What is the reason for this? Is it to make it so t
Sami
2016/03/21 15:05:08
Changed to require users to call Initialize() like
|
| +} |
| + |
| +void BlameContext::OnTraceLogDisabled() {} |
| + |
| +void BlameContext::AsValueInto(trace_event::TracedValue* state) { |
| + if (!parent_id_) |
| + return; |
| + state->BeginDictionary("parent"); |
| + state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); |
| + state->SetString("scope", parent_scope_); |
| + state->EndDictionary(); |
| +} |
| + |
| +void BlameContext::Initialize() { |
| + category_group_enabled_ = |
| + TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_); |
| + TRACE_EVENT_API_ADD_TRACE_EVENT( |
| + TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, type_, scope_, |
| + id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
| + trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| +} |
| + |
| +} // namespace trace_event |
| +} // namespace base |