OLD | NEW |
---|---|
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 } | |
32 | |
33 TraceEvent::TraceEvent(TraceEvent&& other) { | |
danakj
2016/03/18 21:34:56
does = default work for this?
Sami
2016/03/21 11:34:15
Looks like it does -- done.
| |
34 *this = std::move(other); | |
35 } | |
30 | 36 |
31 TraceEvent::~TraceEvent() { | 37 TraceEvent::~TraceEvent() { |
32 } | 38 } |
33 | 39 |
40 TraceEvent& TraceEvent::operator=(const TraceEvent& rhs) { | |
41 thread = rhs.thread; | |
42 timestamp = rhs.timestamp; | |
43 duration = rhs.duration; | |
44 phase = rhs.phase; | |
45 category = rhs.category; | |
46 name = rhs.name; | |
47 id = rhs.id; | |
48 arg_numbers = rhs.arg_numbers; | |
49 arg_strings = rhs.arg_strings; | |
50 for (const auto& pair : rhs.arg_values) | |
51 arg_values[pair.first] = pair.second->CreateDeepCopy(); | |
52 other_event = rhs.other_event; | |
53 return *this; | |
54 } | |
55 | |
56 TraceEvent& TraceEvent::operator=(TraceEvent&& rhs) { | |
danakj
2016/03/18 21:34:56
does = default work for this?
vmpstr
2016/03/18 21:36:18
iirc msvc2013 might not like it
danakj
2016/03/18 21:39:10
Well, we're on 2015 now :)
Sami
2016/03/21 11:34:15
Let's see if the trybots agree :)
| |
57 thread = rhs.thread; | |
58 timestamp = rhs.timestamp; | |
59 duration = rhs.duration; | |
60 phase = rhs.phase; | |
61 category = rhs.category; | |
62 name = rhs.name; | |
63 id = rhs.id; | |
64 arg_numbers.swap(rhs.arg_numbers); | |
65 arg_strings.swap(rhs.arg_strings); | |
66 arg_values.swap(rhs.arg_values); | |
67 other_event = rhs.other_event; | |
68 return *this; | |
69 } | |
70 | |
34 bool TraceEvent::SetFromJSON(const base::Value* event_value) { | 71 bool TraceEvent::SetFromJSON(const base::Value* event_value) { |
35 if (event_value->GetType() != base::Value::TYPE_DICTIONARY) { | 72 if (event_value->GetType() != base::Value::TYPE_DICTIONARY) { |
36 LOG(ERROR) << "Value must be TYPE_DICTIONARY"; | 73 LOG(ERROR) << "Value must be TYPE_DICTIONARY"; |
37 return false; | 74 return false; |
38 } | 75 } |
39 const base::DictionaryValue* dictionary = | 76 const base::DictionaryValue* dictionary = |
40 static_cast<const base::DictionaryValue*>(event_value); | 77 static_cast<const base::DictionaryValue*>(event_value); |
41 | 78 |
42 std::string phase_str; | 79 std::string phase_str; |
43 const base::DictionaryValue* args = NULL; | 80 const base::DictionaryValue* args = NULL; |
44 | 81 |
45 if (!dictionary->GetString("ph", &phase_str)) { | 82 if (!dictionary->GetString("ph", &phase_str)) { |
46 LOG(ERROR) << "ph is missing from TraceEvent JSON"; | 83 LOG(ERROR) << "ph is missing from TraceEvent JSON"; |
47 return false; | 84 return false; |
48 } | 85 } |
49 | 86 |
50 phase = *phase_str.data(); | 87 phase = *phase_str.data(); |
51 | 88 |
52 bool may_have_duration = (phase == TRACE_EVENT_PHASE_COMPLETE); | 89 bool may_have_duration = (phase == TRACE_EVENT_PHASE_COMPLETE); |
53 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA); | 90 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA); |
54 bool require_id = (phase == TRACE_EVENT_PHASE_ASYNC_BEGIN || | 91 bool require_id = (phase == TRACE_EVENT_PHASE_ASYNC_BEGIN || |
55 phase == TRACE_EVENT_PHASE_ASYNC_STEP_INTO || | 92 phase == TRACE_EVENT_PHASE_ASYNC_STEP_INTO || |
56 phase == TRACE_EVENT_PHASE_ASYNC_STEP_PAST || | 93 phase == TRACE_EVENT_PHASE_ASYNC_STEP_PAST || |
94 phase == TRACE_EVENT_PHASE_MEMORY_DUMP || | |
95 phase == TRACE_EVENT_PHASE_ENTER_CONTEXT || | |
96 phase == TRACE_EVENT_PHASE_LEAVE_CONTEXT || | |
97 phase == TRACE_EVENT_PHASE_CREATE_OBJECT || | |
98 phase == TRACE_EVENT_PHASE_DELETE_OBJECT || | |
99 phase == TRACE_EVENT_PHASE_SNAPSHOT_OBJECT || | |
57 phase == TRACE_EVENT_PHASE_ASYNC_END); | 100 phase == TRACE_EVENT_PHASE_ASYNC_END); |
58 | 101 |
59 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) { | 102 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) { |
60 LOG(ERROR) << "pid is missing from TraceEvent JSON"; | 103 LOG(ERROR) << "pid is missing from TraceEvent JSON"; |
61 return false; | 104 return false; |
62 } | 105 } |
63 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) { | 106 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) { |
64 LOG(ERROR) << "tid is missing from TraceEvent JSON"; | 107 LOG(ERROR) << "tid is missing from TraceEvent JSON"; |
65 return false; | 108 return false; |
66 } | 109 } |
(...skipping 29 matching lines...) Expand all Loading... | |
96 int int_num = 0; | 139 int int_num = 0; |
97 double double_num = 0.0; | 140 double double_num = 0.0; |
98 if (it.value().GetAsString(&str)) { | 141 if (it.value().GetAsString(&str)) { |
99 arg_strings[it.key()] = str; | 142 arg_strings[it.key()] = str; |
100 } else if (it.value().GetAsInteger(&int_num)) { | 143 } else if (it.value().GetAsInteger(&int_num)) { |
101 arg_numbers[it.key()] = static_cast<double>(int_num); | 144 arg_numbers[it.key()] = static_cast<double>(int_num); |
102 } else if (it.value().GetAsBoolean(&boolean)) { | 145 } else if (it.value().GetAsBoolean(&boolean)) { |
103 arg_numbers[it.key()] = static_cast<double>(boolean ? 1 : 0); | 146 arg_numbers[it.key()] = static_cast<double>(boolean ? 1 : 0); |
104 } else if (it.value().GetAsDouble(&double_num)) { | 147 } else if (it.value().GetAsDouble(&double_num)) { |
105 arg_numbers[it.key()] = double_num; | 148 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 } | 149 } |
150 // Record all arguments as values. | |
151 arg_values[it.key()] = it.value().CreateDeepCopy(); | |
111 } | 152 } |
112 | 153 |
113 return true; | 154 return true; |
114 } | 155 } |
115 | 156 |
116 double TraceEvent::GetAbsTimeToOtherEvent() const { | 157 double TraceEvent::GetAbsTimeToOtherEvent() const { |
117 return fabs(other_event->timestamp - timestamp); | 158 return fabs(other_event->timestamp - timestamp); |
118 } | 159 } |
119 | 160 |
120 bool TraceEvent::GetArgAsString(const std::string& name, | 161 bool TraceEvent::GetArgAsString(const std::string& name, |
121 std::string* arg) const { | 162 std::string* arg) const { |
122 std::map<std::string, std::string>::const_iterator i = arg_strings.find(name); | 163 const auto it = arg_strings.find(name); |
123 if (i != arg_strings.end()) { | 164 if (it != arg_strings.end()) { |
124 *arg = i->second; | 165 *arg = it->second; |
125 return true; | 166 return true; |
126 } | 167 } |
127 return false; | 168 return false; |
128 } | 169 } |
129 | 170 |
130 bool TraceEvent::GetArgAsNumber(const std::string& name, | 171 bool TraceEvent::GetArgAsNumber(const std::string& name, |
131 double* arg) const { | 172 double* arg) const { |
132 std::map<std::string, double>::const_iterator i = arg_numbers.find(name); | 173 const auto it = arg_numbers.find(name); |
133 if (i != arg_numbers.end()) { | 174 if (it != arg_numbers.end()) { |
134 *arg = i->second; | 175 *arg = it->second; |
135 return true; | 176 return true; |
136 } | 177 } |
137 return false; | 178 return false; |
179 } | |
180 | |
181 bool TraceEvent::GetArgAsValue(const std::string& name, | |
182 scoped_ptr<base::Value>* arg) const { | |
183 const auto it = arg_values.find(name); | |
184 if (it != arg_values.end()) { | |
185 *arg = it->second->CreateDeepCopy(); | |
186 return true; | |
187 } | |
188 return false; | |
138 } | 189 } |
139 | 190 |
140 bool TraceEvent::HasStringArg(const std::string& name) const { | 191 bool TraceEvent::HasStringArg(const std::string& name) const { |
141 return (arg_strings.find(name) != arg_strings.end()); | 192 return (arg_strings.find(name) != arg_strings.end()); |
142 } | 193 } |
143 | 194 |
144 bool TraceEvent::HasNumberArg(const std::string& name) const { | 195 bool TraceEvent::HasNumberArg(const std::string& name) const { |
145 return (arg_numbers.find(name) != arg_numbers.end()); | 196 return (arg_numbers.find(name) != arg_numbers.end()); |
146 } | 197 } |
147 | 198 |
199 bool TraceEvent::HasArg(const std::string& name) const { | |
200 return (arg_values.find(name) != arg_values.end()); | |
201 } | |
202 | |
148 std::string TraceEvent::GetKnownArgAsString(const std::string& name) const { | 203 std::string TraceEvent::GetKnownArgAsString(const std::string& name) const { |
149 std::string arg_string; | 204 std::string arg_string; |
150 bool result = GetArgAsString(name, &arg_string); | 205 bool result = GetArgAsString(name, &arg_string); |
151 DCHECK(result); | 206 DCHECK(result); |
152 return arg_string; | 207 return arg_string; |
153 } | 208 } |
154 | 209 |
155 double TraceEvent::GetKnownArgAsDouble(const std::string& name) const { | 210 double TraceEvent::GetKnownArgAsDouble(const std::string& name) const { |
156 double arg_double = 0; | 211 double arg_double = 0; |
157 bool result = GetArgAsNumber(name, &arg_double); | 212 bool result = GetArgAsNumber(name, &arg_double); |
158 DCHECK(result); | 213 DCHECK(result); |
159 return arg_double; | 214 return arg_double; |
160 } | 215 } |
161 | 216 |
162 int TraceEvent::GetKnownArgAsInt(const std::string& name) const { | 217 int TraceEvent::GetKnownArgAsInt(const std::string& name) const { |
163 double arg_double = 0; | 218 double arg_double = 0; |
164 bool result = GetArgAsNumber(name, &arg_double); | 219 bool result = GetArgAsNumber(name, &arg_double); |
165 DCHECK(result); | 220 DCHECK(result); |
166 return static_cast<int>(arg_double); | 221 return static_cast<int>(arg_double); |
167 } | 222 } |
168 | 223 |
169 bool TraceEvent::GetKnownArgAsBool(const std::string& name) const { | 224 bool TraceEvent::GetKnownArgAsBool(const std::string& name) const { |
170 double arg_double = 0; | 225 double arg_double = 0; |
171 bool result = GetArgAsNumber(name, &arg_double); | 226 bool result = GetArgAsNumber(name, &arg_double); |
172 DCHECK(result); | 227 DCHECK(result); |
173 return (arg_double != 0.0); | 228 return (arg_double != 0.0); |
174 } | 229 } |
175 | 230 |
231 scoped_ptr<base::Value> TraceEvent::GetKnownArgAsValue( | |
232 const std::string& name) const { | |
233 scoped_ptr<base::Value> arg_value; | |
234 bool result = GetArgAsValue(name, &arg_value); | |
235 DCHECK(result); | |
236 return arg_value; | |
237 } | |
238 | |
176 // QueryNode | 239 // QueryNode |
177 | 240 |
178 QueryNode::QueryNode(const Query& query) : query_(query) { | 241 QueryNode::QueryNode(const Query& query) : query_(query) { |
179 } | 242 } |
180 | 243 |
181 QueryNode::~QueryNode() { | 244 QueryNode::~QueryNode() { |
182 } | 245 } |
183 | 246 |
184 // Query | 247 // Query |
185 | 248 |
(...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
962 end_position = (end_position < events.size()) ? end_position : events.size(); | 1025 end_position = (end_position < events.size()) ? end_position : events.size(); |
963 size_t count = 0u; | 1026 size_t count = 0u; |
964 for (size_t i = begin_position; i < end_position; ++i) { | 1027 for (size_t i = begin_position; i < end_position; ++i) { |
965 if (query.Evaluate(*events.at(i))) | 1028 if (query.Evaluate(*events.at(i))) |
966 ++count; | 1029 ++count; |
967 } | 1030 } |
968 return count; | 1031 return count; |
969 } | 1032 } |
970 | 1033 |
971 } // namespace trace_analyzer | 1034 } // namespace trace_analyzer |
OLD | NEW |