OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Use trace_analyzer::Query and trace_analyzer::TraceAnalyzer to search for | 5 // Use trace_analyzer::Query and trace_analyzer::TraceAnalyzer to search for |
6 // specific trace events that were generated by the trace_event.h API. | 6 // specific trace events that were generated by the trace_event.h API. |
7 // | 7 // |
8 // Basic procedure: | 8 // Basic procedure: |
9 // - Get trace events JSON string from base::debug::TraceLog. | 9 // - Get trace events JSON string from base::debug::TraceLog. |
10 // - Create TraceAnalyzer with JSON string. | 10 // - Create TraceAnalyzer with JSON string. |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 std::map<std::string, double> arg_numbers; | 165 std::map<std::string, double> arg_numbers; |
166 | 166 |
167 std::map<std::string, std::string> arg_strings; | 167 std::map<std::string, std::string> arg_strings; |
168 | 168 |
169 // The other event associated with this event (or NULL). | 169 // The other event associated with this event (or NULL). |
170 const TraceEvent* other_event; | 170 const TraceEvent* other_event; |
171 }; | 171 }; |
172 | 172 |
173 typedef std::vector<const TraceEvent*> TraceEventVector; | 173 typedef std::vector<const TraceEvent*> TraceEventVector; |
174 | 174 |
175 // Pass these values to Query to compare with the corresponding member of a | |
176 // TraceEvent. Unless otherwise specfied, the usage is Query(ENUM_MEMBER). | |
177 enum TraceEventMember { | |
178 EVENT_INVALID, | |
179 // Use these to access the event members: | |
180 EVENT_PID, | |
181 EVENT_TID, | |
182 // Return the timestamp of the event in microseconds since epoch. | |
183 EVENT_TIME, | |
184 // Return the absolute time between event and other event in microseconds. | |
185 // Only works for events with associated BEGIN/END: Query(EVENT_HAS_OTHER). | |
186 EVENT_DURATION, | |
187 EVENT_PHASE, | |
188 EVENT_CATEGORY, | |
189 EVENT_NAME, | |
190 EVENT_ID, | |
191 | |
192 // Evaluates to true if arg exists and is a string. | |
193 // Usage: Query(EVENT_HAS_STRING_ARG, "arg_name") | |
194 EVENT_HAS_STRING_ARG, | |
195 // Evaluates to true if arg exists and is a number. | |
196 // Number arguments include types double, int and bool. | |
197 // Usage: Query(EVENT_HAS_NUMBER_ARG, "arg_name") | |
198 EVENT_HAS_NUMBER_ARG, | |
199 // Evaluates to arg value (string or number). | |
200 // Usage: Query(EVENT_ARG, "arg_name") | |
201 EVENT_ARG, | |
202 // Return true if associated event exists. | |
203 // (Typically BEGIN for END or END for BEGIN). | |
204 EVENT_HAS_OTHER, | |
205 | |
206 // Access the associated other_event's members: | |
207 OTHER_PID, | |
208 OTHER_TID, | |
209 OTHER_TIME, | |
210 OTHER_PHASE, | |
211 OTHER_CATEGORY, | |
212 OTHER_NAME, | |
213 OTHER_ID, | |
214 | |
215 // Evaluates to true if arg exists and is a string. | |
216 // Usage: Query(EVENT_HAS_STRING_ARG, "arg_name") | |
217 OTHER_HAS_STRING_ARG, | |
218 // Evaluates to true if arg exists and is a number. | |
219 // Number arguments include types double, int and bool. | |
220 // Usage: Query(EVENT_HAS_NUMBER_ARG, "arg_name") | |
221 OTHER_HAS_NUMBER_ARG, | |
222 // Evaluates to arg value (string or number). | |
223 // Usage: Query(EVENT_ARG, "arg_name") | |
224 OTHER_ARG, | |
225 }; | |
226 | |
227 class Query { | 175 class Query { |
228 public: | 176 public: |
229 // Compare with the given member. | |
230 Query(TraceEventMember member); | |
231 | |
232 // Compare with the given member argument value. | |
233 Query(TraceEventMember member, const std::string& arg_name); | |
234 | |
235 Query(const Query& query); | 177 Query(const Query& query); |
236 | 178 |
237 ~Query(); | 179 ~Query(); |
238 | 180 |
| 181 //////////////////////////////////////////////////////////////// |
| 182 // Query literal values |
| 183 |
239 // Compare with the given string. | 184 // Compare with the given string. |
240 static Query String(const std::string& str); | 185 static Query String(const std::string& str); |
241 | 186 |
242 // Compare with the given number. | 187 // Compare with the given number. |
243 static Query Double(double num); | 188 static Query Double(double num); |
244 static Query Int(int32 num); | 189 static Query Int(int32 num); |
245 static Query Uint(uint32 num); | 190 static Query Uint(uint32 num); |
246 | 191 |
247 // Compare with the given bool. | 192 // Compare with the given bool. |
248 static Query Bool(bool boolean); | 193 static Query Bool(bool boolean); |
249 | 194 |
250 // Compare with the given phase. | 195 // Compare with the given phase. |
251 static Query Phase(base::debug::TraceEventPhase phase); | 196 static Query Phase(base::debug::TraceEventPhase phase); |
252 | 197 |
253 // Compare with the given string pattern. Only works with == and != operators. | 198 // Compare with the given string pattern. Only works with == and != operators. |
254 // Example: Query(EVENT_NAME) == Query::Pattern("MyEvent*") | 199 // Example: Query(EVENT_NAME) == Query::Pattern("MyEvent*") |
255 static Query Pattern(const std::string& pattern); | 200 static Query Pattern(const std::string& pattern); |
256 | 201 |
| 202 //////////////////////////////////////////////////////////////// |
| 203 // Query event members |
| 204 |
| 205 static Query EventPid() { return Query(EVENT_PID); } |
| 206 |
| 207 static Query EventTid() { return Query(EVENT_TID); } |
| 208 |
| 209 // Return the timestamp of the event in microseconds since epoch. |
| 210 static Query EventTime() { return Query(EVENT_TIME); } |
| 211 |
| 212 // Return the absolute time between event and other event in microseconds. |
| 213 // Only works if Query::EventHasOther() == true. |
| 214 static Query EventDuration() { return Query(EVENT_DURATION); } |
| 215 |
| 216 static Query EventPhase() { return Query(EVENT_PHASE); } |
| 217 |
| 218 static Query EventCategory() { return Query(EVENT_CATEGORY); } |
| 219 |
| 220 static Query EventName() { return Query(EVENT_NAME); } |
| 221 |
| 222 static Query EventId() { return Query(EVENT_ID); } |
| 223 |
| 224 // Evaluates to true if arg exists and is a string. |
| 225 static Query EventHasStringArg(const std::string& arg_name) { |
| 226 return Query(EVENT_HAS_STRING_ARG, arg_name); |
| 227 } |
| 228 |
| 229 // Evaluates to true if arg exists and is a number. |
| 230 // Number arguments include types double, int and bool. |
| 231 static Query EventHasNumberArg(const std::string& arg_name) { |
| 232 return Query(EVENT_HAS_NUMBER_ARG, arg_name); |
| 233 } |
| 234 |
| 235 // Evaluates to arg value (string or number). |
| 236 static Query EventArg(const std::string& arg_name) { |
| 237 return Query(EVENT_ARG, arg_name); |
| 238 } |
| 239 |
| 240 // Return true if associated event exists. |
| 241 static Query EventHasOther() { return Query(EVENT_HAS_OTHER); } |
| 242 |
| 243 // Access the associated other_event's members: |
| 244 |
| 245 static Query OtherPid() { return Query(OTHER_PID); } |
| 246 |
| 247 static Query OtherTid() { return Query(OTHER_TID); } |
| 248 |
| 249 static Query OtherTime() { return Query(OTHER_TIME); } |
| 250 |
| 251 static Query OtherPhase() { return Query(OTHER_PHASE); } |
| 252 |
| 253 static Query OtherCategory() { return Query(OTHER_CATEGORY); } |
| 254 |
| 255 static Query OtherName() { return Query(OTHER_NAME); } |
| 256 |
| 257 static Query OtherId() { return Query(OTHER_ID); } |
| 258 |
| 259 static Query OtherHasStringArg(const std::string& arg_name) { |
| 260 return Query(OTHER_HAS_STRING_ARG, arg_name); |
| 261 } |
| 262 |
| 263 static Query OtherHasNumberArg(const std::string& arg_name) { |
| 264 return Query(OTHER_HAS_NUMBER_ARG, arg_name); |
| 265 } |
| 266 |
| 267 static Query OtherArg(const std::string& arg_name) { |
| 268 return Query(OTHER_ARG, arg_name); |
| 269 } |
| 270 |
| 271 //////////////////////////////////////////////////////////////// |
257 // Common queries: | 272 // Common queries: |
258 | 273 |
259 // Find BEGIN events that have a corresponding END event. | 274 // Find BEGIN events that have a corresponding END event. |
260 static Query MatchBeginWithEnd() { | 275 static Query MatchBeginWithEnd() { |
261 return (Query(EVENT_PHASE) == Query::Phase(TRACE_EVENT_PHASE_BEGIN)) && | 276 return (Query(EVENT_PHASE) == Query::Phase(TRACE_EVENT_PHASE_BEGIN)) && |
262 Query(EVENT_HAS_OTHER); | 277 Query(EVENT_HAS_OTHER); |
263 } | 278 } |
264 | 279 |
265 // Find START events that have a corresponding FINISH event. | 280 // Find START events that have a corresponding FINISH event. |
266 static Query MatchStartWithFinish() { | 281 static Query MatchStartWithFinish() { |
(...skipping 11 matching lines...) Expand all Loading... |
278 return (Query(EVENT_PID) == Query::Int(thread.process_id)) && | 293 return (Query(EVENT_PID) == Query::Int(thread.process_id)) && |
279 (Query(EVENT_TID) == Query::Int(thread.thread_id)); | 294 (Query(EVENT_TID) == Query::Int(thread.thread_id)); |
280 } | 295 } |
281 | 296 |
282 // Match event pair that spans multiple threads. | 297 // Match event pair that spans multiple threads. |
283 static Query MatchCrossThread() { | 298 static Query MatchCrossThread() { |
284 return (Query(EVENT_PID) != Query(OTHER_PID)) || | 299 return (Query(EVENT_PID) != Query(OTHER_PID)) || |
285 (Query(EVENT_TID) != Query(OTHER_TID)); | 300 (Query(EVENT_TID) != Query(OTHER_TID)); |
286 } | 301 } |
287 | 302 |
| 303 //////////////////////////////////////////////////////////////// |
| 304 // Operators: |
| 305 |
288 // Boolean operators: | 306 // Boolean operators: |
289 Query operator==(const Query& rhs) const; | 307 Query operator==(const Query& rhs) const; |
290 Query operator!=(const Query& rhs) const; | 308 Query operator!=(const Query& rhs) const; |
291 Query operator< (const Query& rhs) const; | 309 Query operator< (const Query& rhs) const; |
292 Query operator<=(const Query& rhs) const; | 310 Query operator<=(const Query& rhs) const; |
293 Query operator> (const Query& rhs) const; | 311 Query operator> (const Query& rhs) const; |
294 Query operator>=(const Query& rhs) const; | 312 Query operator>=(const Query& rhs) const; |
295 Query operator&&(const Query& rhs) const; | 313 Query operator&&(const Query& rhs) const; |
296 Query operator||(const Query& rhs) const; | 314 Query operator||(const Query& rhs) const; |
297 Query operator!() const; | 315 Query operator!() const; |
298 | 316 |
299 // Arithmetic operators: | 317 // Arithmetic operators: |
300 // Following operators are applied to double arguments: | 318 // Following operators are applied to double arguments: |
301 Query operator+(const Query& rhs) const; | 319 Query operator+(const Query& rhs) const; |
302 Query operator-(const Query& rhs) const; | 320 Query operator-(const Query& rhs) const; |
303 Query operator*(const Query& rhs) const; | 321 Query operator*(const Query& rhs) const; |
304 Query operator/(const Query& rhs) const; | 322 Query operator/(const Query& rhs) const; |
305 Query operator-() const; | 323 Query operator-() const; |
306 // Mod operates on int64 args (doubles are casted to int64 beforehand): | 324 // Mod operates on int64 args (doubles are casted to int64 beforehand): |
307 Query operator%(const Query& rhs) const; | 325 Query operator%(const Query& rhs) const; |
308 | 326 |
309 // Return true if the given event matches this query tree. | 327 // Return true if the given event matches this query tree. |
310 // This is a recursive method that walks the query tree. | 328 // This is a recursive method that walks the query tree. |
311 bool Evaluate(const TraceEvent& event) const; | 329 bool Evaluate(const TraceEvent& event) const; |
312 | 330 |
313 private: | 331 private: |
| 332 enum TraceEventMember { |
| 333 EVENT_INVALID, |
| 334 EVENT_PID, |
| 335 EVENT_TID, |
| 336 EVENT_TIME, |
| 337 EVENT_DURATION, |
| 338 EVENT_PHASE, |
| 339 EVENT_CATEGORY, |
| 340 EVENT_NAME, |
| 341 EVENT_ID, |
| 342 EVENT_HAS_STRING_ARG, |
| 343 EVENT_HAS_NUMBER_ARG, |
| 344 EVENT_ARG, |
| 345 EVENT_HAS_OTHER, |
| 346 OTHER_PID, |
| 347 OTHER_TID, |
| 348 OTHER_TIME, |
| 349 OTHER_PHASE, |
| 350 OTHER_CATEGORY, |
| 351 OTHER_NAME, |
| 352 OTHER_ID, |
| 353 OTHER_HAS_STRING_ARG, |
| 354 OTHER_HAS_NUMBER_ARG, |
| 355 OTHER_ARG, |
| 356 }; |
| 357 |
314 enum Operator { | 358 enum Operator { |
315 OP_INVALID, | 359 OP_INVALID, |
316 // Boolean operators: | 360 // Boolean operators: |
317 OP_EQ, | 361 OP_EQ, |
318 OP_NE, | 362 OP_NE, |
319 OP_LT, | 363 OP_LT, |
320 OP_LE, | 364 OP_LE, |
321 OP_GT, | 365 OP_GT, |
322 OP_GE, | 366 OP_GE, |
323 OP_AND, | 367 OP_AND, |
324 OP_OR, | 368 OP_OR, |
325 OP_NOT, | 369 OP_NOT, |
326 // Arithmetic operators: | 370 // Arithmetic operators: |
327 OP_ADD, | 371 OP_ADD, |
328 OP_SUB, | 372 OP_SUB, |
329 OP_MUL, | 373 OP_MUL, |
330 OP_DIV, | 374 OP_DIV, |
331 OP_MOD, | 375 OP_MOD, |
332 OP_NEGATE | 376 OP_NEGATE |
333 }; | 377 }; |
334 | 378 |
335 enum QueryType { | 379 enum QueryType { |
336 QUERY_BOOLEAN_OPERATOR, | 380 QUERY_BOOLEAN_OPERATOR, |
337 QUERY_ARITHMETIC_OPERATOR, | 381 QUERY_ARITHMETIC_OPERATOR, |
338 QUERY_EVENT_MEMBER, | 382 QUERY_EVENT_MEMBER, |
339 QUERY_NUMBER, | 383 QUERY_NUMBER, |
340 QUERY_STRING | 384 QUERY_STRING |
341 }; | 385 }; |
342 | 386 |
| 387 // Compare with the given member. |
| 388 Query(TraceEventMember member); |
| 389 |
| 390 // Compare with the given member argument value. |
| 391 Query(TraceEventMember member, const std::string& arg_name); |
| 392 |
343 // Compare with the given string. | 393 // Compare with the given string. |
344 Query(const std::string& str); | 394 Query(const std::string& str); |
345 | 395 |
346 // Compare with the given number. | 396 // Compare with the given number. |
347 Query(double num); | 397 Query(double num); |
348 | 398 |
349 // Construct a boolean Query that returns (left <binary_op> right). | 399 // Construct a boolean Query that returns (left <binary_op> right). |
350 Query(const Query& left, const Query& right, Operator binary_op); | 400 Query(const Query& left, const Query& right, Operator binary_op); |
351 | 401 |
352 // Construct a boolean Query that returns (<binary_op> left). | 402 // Construct a boolean Query that returns (<binary_op> left). |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 | 590 |
541 // Count all matches. | 591 // Count all matches. |
542 static inline size_t CountMatches(const TraceEventVector& events, | 592 static inline size_t CountMatches(const TraceEventVector& events, |
543 const Query& query) { | 593 const Query& query) { |
544 return CountMatches(events, query, 0u, events.size()); | 594 return CountMatches(events, query, 0u, events.size()); |
545 } | 595 } |
546 | 596 |
547 } // namespace trace_analyzer | 597 } // namespace trace_analyzer |
548 | 598 |
549 #endif // BASE_TEST_TRACE_EVENT_ANALYZER_H_ | 599 #endif // BASE_TEST_TRACE_EVENT_ANALYZER_H_ |
OLD | NEW |