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

Side by Side Diff: base/test/trace_event_analyzer.cc

Issue 1776673002: base: Add blame context (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split into BlameContext and TracedBlameContext. 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/test/trace_event_analyzer.h" 5 #include "base/test/trace_event_analyzer.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <set> 10 #include <set>
11 11
12 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/pattern.h" 14 #include "base/strings/pattern.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 16
17 namespace trace_analyzer { 17 namespace trace_analyzer {
18 18
19 // TraceEvent 19 // TraceEvent
20 20
21 TraceEvent::TraceEvent() 21 TraceEvent::TraceEvent()
22 : thread(0, 0), 22 : thread(0, 0),
23 timestamp(0), 23 timestamp(0),
24 duration(0), 24 duration(0),
25 phase(TRACE_EVENT_PHASE_BEGIN), 25 phase(TRACE_EVENT_PHASE_BEGIN),
26 other_event(NULL) { 26 other_event(NULL) {
27 } 27 }
28 28
29 TraceEvent::TraceEvent(const TraceEvent& other) = default; 29 TraceEvent::TraceEvent(const TraceEvent& other) {
30 *this = other;
31 }
30 32
31 TraceEvent::~TraceEvent() { 33 TraceEvent::~TraceEvent() {
32 } 34 }
33 35
36 void TraceEvent::operator=(const TraceEvent& rhs) {
37 thread = rhs.thread;
38 timestamp = rhs.timestamp;
39 duration = rhs.duration;
40 phase = rhs.phase;
41 category = rhs.category;
42 name = rhs.name;
43 id = rhs.id;
44 arg_numbers = rhs.arg_numbers;
45 arg_strings = rhs.arg_strings;
46 for (auto it = rhs.arg_values.cbegin(); it != rhs.arg_values.cend(); ++it) {
danakj 2016/03/16 18:29:49 for (const auto& pair: rhs.arg_values) arg_value
Sami 2016/03/17 12:27:13 Much neater, thanks.
47 arg_values[it->first] = it->second->CreateDeepCopy();
48 }
49 other_event = rhs.other_event;
50 }
51
34 bool TraceEvent::SetFromJSON(const base::Value* event_value) { 52 bool TraceEvent::SetFromJSON(const base::Value* event_value) {
35 if (event_value->GetType() != base::Value::TYPE_DICTIONARY) { 53 if (event_value->GetType() != base::Value::TYPE_DICTIONARY) {
36 LOG(ERROR) << "Value must be TYPE_DICTIONARY"; 54 LOG(ERROR) << "Value must be TYPE_DICTIONARY";
37 return false; 55 return false;
38 } 56 }
39 const base::DictionaryValue* dictionary = 57 const base::DictionaryValue* dictionary =
40 static_cast<const base::DictionaryValue*>(event_value); 58 static_cast<const base::DictionaryValue*>(event_value);
41 59
42 std::string phase_str; 60 std::string phase_str;
43 const base::DictionaryValue* args = NULL; 61 const base::DictionaryValue* args = NULL;
44 62
45 if (!dictionary->GetString("ph", &phase_str)) { 63 if (!dictionary->GetString("ph", &phase_str)) {
46 LOG(ERROR) << "ph is missing from TraceEvent JSON"; 64 LOG(ERROR) << "ph is missing from TraceEvent JSON";
47 return false; 65 return false;
48 } 66 }
49 67
50 phase = *phase_str.data(); 68 phase = *phase_str.data();
51 69
52 bool may_have_duration = (phase == TRACE_EVENT_PHASE_COMPLETE); 70 bool may_have_duration = (phase == TRACE_EVENT_PHASE_COMPLETE);
53 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA); 71 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA);
54 bool require_id = (phase == TRACE_EVENT_PHASE_ASYNC_BEGIN || 72 bool require_id = (phase == TRACE_EVENT_PHASE_ASYNC_BEGIN ||
55 phase == TRACE_EVENT_PHASE_ASYNC_STEP_INTO || 73 phase == TRACE_EVENT_PHASE_ASYNC_STEP_INTO ||
56 phase == TRACE_EVENT_PHASE_ASYNC_STEP_PAST || 74 phase == TRACE_EVENT_PHASE_ASYNC_STEP_PAST ||
75 phase == TRACE_EVENT_PHASE_MEMORY_DUMP ||
76 phase == TRACE_EVENT_PHASE_ENTER_CONTEXT ||
77 phase == TRACE_EVENT_PHASE_LEAVE_CONTEXT ||
78 phase == TRACE_EVENT_PHASE_CREATE_OBJECT ||
79 phase == TRACE_EVENT_PHASE_DELETE_OBJECT ||
80 phase == TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ||
57 phase == TRACE_EVENT_PHASE_ASYNC_END); 81 phase == TRACE_EVENT_PHASE_ASYNC_END);
58 82
59 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) { 83 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) {
60 LOG(ERROR) << "pid is missing from TraceEvent JSON"; 84 LOG(ERROR) << "pid is missing from TraceEvent JSON";
61 return false; 85 return false;
62 } 86 }
63 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) { 87 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) {
64 LOG(ERROR) << "tid is missing from TraceEvent JSON"; 88 LOG(ERROR) << "tid is missing from TraceEvent JSON";
65 return false; 89 return false;
66 } 90 }
(...skipping 29 matching lines...) Expand all
96 int int_num = 0; 120 int int_num = 0;
97 double double_num = 0.0; 121 double double_num = 0.0;
98 if (it.value().GetAsString(&str)) { 122 if (it.value().GetAsString(&str)) {
99 arg_strings[it.key()] = str; 123 arg_strings[it.key()] = str;
100 } else if (it.value().GetAsInteger(&int_num)) { 124 } else if (it.value().GetAsInteger(&int_num)) {
101 arg_numbers[it.key()] = static_cast<double>(int_num); 125 arg_numbers[it.key()] = static_cast<double>(int_num);
102 } else if (it.value().GetAsBoolean(&boolean)) { 126 } else if (it.value().GetAsBoolean(&boolean)) {
103 arg_numbers[it.key()] = static_cast<double>(boolean ? 1 : 0); 127 arg_numbers[it.key()] = static_cast<double>(boolean ? 1 : 0);
104 } else if (it.value().GetAsDouble(&double_num)) { 128 } else if (it.value().GetAsDouble(&double_num)) {
105 arg_numbers[it.key()] = double_num; 129 arg_numbers[it.key()] = double_num;
106 } else {
107 LOG(WARNING) << "Value type of argument is not supported: " <<
108 static_cast<int>(it.value().GetType());
109 continue; // Skip non-supported arguments.
110 } 130 }
131 // Record all arguments as values.
132 arg_values[it.key()] = it.value().CreateDeepCopy();
111 } 133 }
112 134
113 return true; 135 return true;
114 } 136 }
115 137
116 double TraceEvent::GetAbsTimeToOtherEvent() const { 138 double TraceEvent::GetAbsTimeToOtherEvent() const {
117 return fabs(other_event->timestamp - timestamp); 139 return fabs(other_event->timestamp - timestamp);
118 } 140 }
119 141
120 bool TraceEvent::GetArgAsString(const std::string& name, 142 bool TraceEvent::GetArgAsString(const std::string& name,
121 std::string* arg) const { 143 std::string* arg) const {
122 std::map<std::string, std::string>::const_iterator i = arg_strings.find(name); 144 std::map<std::string, std::string>::const_iterator i = arg_strings.find(name);
123 if (i != arg_strings.end()) { 145 if (i != arg_strings.end()) {
124 *arg = i->second; 146 *arg = i->second;
125 return true; 147 return true;
126 } 148 }
127 return false; 149 return false;
128 } 150 }
129 151
130 bool TraceEvent::GetArgAsNumber(const std::string& name, 152 bool TraceEvent::GetArgAsNumber(const std::string& name,
131 double* arg) const { 153 double* arg) const {
132 std::map<std::string, double>::const_iterator i = arg_numbers.find(name); 154 std::map<std::string, double>::const_iterator i = arg_numbers.find(name);
133 if (i != arg_numbers.end()) { 155 if (i != arg_numbers.end()) {
134 *arg = i->second; 156 *arg = i->second;
135 return true; 157 return true;
136 } 158 }
137 return false; 159 return false;
138 } 160 }
139 161
162 bool TraceEvent::GetArgAsValue(const std::string& name,
163 scoped_ptr<base::Value>* arg) const {
164 std::map<std::string, scoped_ptr<base::Value>>::const_iterator i =
danakj 2016/03/16 18:29:49 auto please. |it| instead of |i|
Sami 2016/03/17 12:27:13 Done. Fixed the others too.
165 arg_values.find(name);
166 if (i != arg_values.end()) {
167 *arg = i->second->CreateDeepCopy();
168 return true;
169 }
170 return false;
171 }
172
140 bool TraceEvent::HasStringArg(const std::string& name) const { 173 bool TraceEvent::HasStringArg(const std::string& name) const {
141 return (arg_strings.find(name) != arg_strings.end()); 174 return (arg_strings.find(name) != arg_strings.end());
142 } 175 }
143 176
144 bool TraceEvent::HasNumberArg(const std::string& name) const { 177 bool TraceEvent::HasNumberArg(const std::string& name) const {
145 return (arg_numbers.find(name) != arg_numbers.end()); 178 return (arg_numbers.find(name) != arg_numbers.end());
146 } 179 }
147 180
181 bool TraceEvent::HasArg(const std::string& name) const {
182 return (arg_values.find(name) != arg_values.end());
183 }
184
148 std::string TraceEvent::GetKnownArgAsString(const std::string& name) const { 185 std::string TraceEvent::GetKnownArgAsString(const std::string& name) const {
149 std::string arg_string; 186 std::string arg_string;
150 bool result = GetArgAsString(name, &arg_string); 187 bool result = GetArgAsString(name, &arg_string);
151 DCHECK(result); 188 DCHECK(result);
152 return arg_string; 189 return arg_string;
153 } 190 }
154 191
155 double TraceEvent::GetKnownArgAsDouble(const std::string& name) const { 192 double TraceEvent::GetKnownArgAsDouble(const std::string& name) const {
156 double arg_double = 0; 193 double arg_double = 0;
157 bool result = GetArgAsNumber(name, &arg_double); 194 bool result = GetArgAsNumber(name, &arg_double);
158 DCHECK(result); 195 DCHECK(result);
159 return arg_double; 196 return arg_double;
160 } 197 }
161 198
162 int TraceEvent::GetKnownArgAsInt(const std::string& name) const { 199 int TraceEvent::GetKnownArgAsInt(const std::string& name) const {
163 double arg_double = 0; 200 double arg_double = 0;
164 bool result = GetArgAsNumber(name, &arg_double); 201 bool result = GetArgAsNumber(name, &arg_double);
165 DCHECK(result); 202 DCHECK(result);
166 return static_cast<int>(arg_double); 203 return static_cast<int>(arg_double);
167 } 204 }
168 205
169 bool TraceEvent::GetKnownArgAsBool(const std::string& name) const { 206 bool TraceEvent::GetKnownArgAsBool(const std::string& name) const {
170 double arg_double = 0; 207 double arg_double = 0;
171 bool result = GetArgAsNumber(name, &arg_double); 208 bool result = GetArgAsNumber(name, &arg_double);
172 DCHECK(result); 209 DCHECK(result);
173 return (arg_double != 0.0); 210 return (arg_double != 0.0);
174 } 211 }
175 212
213 scoped_ptr<base::Value> TraceEvent::GetKnownArgAsValue(
214 const std::string& name) const {
215 scoped_ptr<base::Value> arg_value;
216 bool result = GetArgAsValue(name, &arg_value);
217 DCHECK(result);
218 return arg_value;
219 }
220
176 // QueryNode 221 // QueryNode
177 222
178 QueryNode::QueryNode(const Query& query) : query_(query) { 223 QueryNode::QueryNode(const Query& query) : query_(query) {
179 } 224 }
180 225
181 QueryNode::~QueryNode() { 226 QueryNode::~QueryNode() {
182 } 227 }
183 228
184 // Query 229 // Query
185 230
(...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 end_position = (end_position < events.size()) ? end_position : events.size(); 1007 end_position = (end_position < events.size()) ? end_position : events.size();
963 size_t count = 0u; 1008 size_t count = 0u;
964 for (size_t i = begin_position; i < end_position; ++i) { 1009 for (size_t i = begin_position; i < end_position; ++i) {
965 if (query.Evaluate(*events.at(i))) 1010 if (query.Evaluate(*events.at(i)))
966 ++count; 1011 ++count;
967 } 1012 }
968 return count; 1013 return count;
969 } 1014 }
970 1015
971 } // namespace trace_analyzer 1016 } // namespace trace_analyzer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698