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

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

Issue 1776673002: base: Add blame context (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: debug/ => trace_event/ Created 4 years, 9 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
OLDNEW
(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_argument.h"
9
10 namespace base {
11 namespace trace_event {
12
13 BlameContext::BlameContext(const char* category,
14 const char* name,
15 const char* type,
16 const char* scope,
17 int64_t id)
18 : category_(category),
19 name_(name),
20 type_(type),
21 scope_(scope),
22 id_(id),
23 parent_scope_(nullptr),
24 parent_id_(0) {
25 Initialize();
26 }
27
28 BlameContext::BlameContext(const char* category,
29 const char* name,
30 const char* type,
31 const char* scope,
32 int64_t id,
33 const BlameContext& parent_context)
34 : category_(category),
35 name_(name),
36 type_(type),
37 scope_(scope),
38 id_(id),
39 parent_scope_(parent_context.scope()),
40 parent_id_(parent_context.id()) {
41 DCHECK(!std::strcmp(name_, parent_context.name()))
42 << "Parent blame context must have the same name";
43 Initialize();
44 }
45
46 void BlameContext::Enter() {
47 int num_args = 0;
48 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT,
49 category_group_enabled_, name_, scope_, id_,
50 num_args, nullptr, nullptr, nullptr, nullptr,
51 TRACE_EVENT_FLAG_HAS_ID);
52 }
53
54 void BlameContext::Leave() {
55 int num_args = 0;
56 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT,
57 category_group_enabled_, name_, scope_, id_,
58 num_args, nullptr, nullptr, nullptr, nullptr,
59 TRACE_EVENT_FLAG_HAS_ID);
60 }
61
62 void BlameContext::TakeSnapshot() {
63 if (!*category_group_enabled_)
64 return;
65 scoped_ptr<trace_event::TracedValue> snapshot(new trace_event::TracedValue);
66 AsValueInto(snapshot.get());
67 static const char* kArgName = "snapshot";
68 int num_args = 1;
69 unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE};
70 scoped_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = {
71 std::move(snapshot)};
72 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT,
73 category_group_enabled_, type_, scope_, id_,
74 num_args, &kArgName, arg_types, nullptr,
75 arg_values, TRACE_EVENT_FLAG_HAS_ID);
76 }
77
78 void BlameContext::OnTraceLogEnabled() {
79 TakeSnapshot();
80 }
81
82 void BlameContext::OnTraceLogDisabled() {}
83
84 BlameContext::~BlameContext() {
85 TRACE_EVENT_API_ADD_TRACE_EVENT(
86 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_,
87 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID);
88 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this);
89 }
90
91 void BlameContext::AsValueInto(trace_event::TracedValue* state) {
92 if (!parent_id_)
93 return;
94 state->BeginDictionary("parent");
95 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_));
96 state->SetString("scope", parent_scope_);
97 state->EndDictionary();
98 }
99
100 void BlameContext::Initialize() {
101 category_group_enabled_ =
102 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_);
103 TRACE_EVENT_API_ADD_TRACE_EVENT(
104 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, type_, scope_,
105 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID);
106 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this);
107 }
108
109 } // namespace trace_event
110 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698