| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 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. | |
| 7 // | |
| 8 // Basic procedure: | |
| 9 // - Get trace events JSON string from base::trace_event::TraceLog. | |
| 10 // - Create TraceAnalyzer with JSON string. | |
| 11 // - Call TraceAnalyzer::AssociateBeginEndEvents (optional). | |
| 12 // - Call TraceAnalyzer::AssociateEvents (zero or more times). | |
| 13 // - Call TraceAnalyzer::FindEvents with queries to find specific events. | |
| 14 // | |
| 15 // A Query is a boolean expression tree that evaluates to true or false for a | |
| 16 // given trace event. Queries can be combined into a tree using boolean, | |
| 17 // arithmetic and comparison operators that refer to data of an individual trace | |
| 18 // event. | |
| 19 // | |
| 20 // The events are returned as trace_analyzer::TraceEvent objects. | |
| 21 // TraceEvent contains a single trace event's data, as well as a pointer to | |
| 22 // a related trace event. The related trace event is typically the matching end | |
| 23 // of a begin event or the matching begin of an end event. | |
| 24 // | |
| 25 // The following examples use this basic setup code to construct TraceAnalyzer | |
| 26 // with the json trace string retrieved from TraceLog and construct an event | |
| 27 // vector for retrieving events: | |
| 28 // | |
| 29 // TraceAnalyzer analyzer(json_events); | |
| 30 // TraceEventVector events; | |
| 31 // | |
| 32 // EXAMPLE 1: Find events named "my_event". | |
| 33 // | |
| 34 // analyzer.FindEvents(Query(EVENT_NAME) == "my_event", &events); | |
| 35 // | |
| 36 // EXAMPLE 2: Find begin events named "my_event" with duration > 1 second. | |
| 37 // | |
| 38 // Query q = (Query(EVENT_NAME) == Query::String("my_event") && | |
| 39 // Query(EVENT_PHASE) == Query::Phase(TRACE_EVENT_PHASE_BEGIN) && | |
| 40 // Query(EVENT_DURATION) > Query::Double(1000000.0)); | |
| 41 // analyzer.FindEvents(q, &events); | |
| 42 // | |
| 43 // EXAMPLE 3: Associating event pairs across threads. | |
| 44 // | |
| 45 // If the test needs to analyze something that starts and ends on different | |
| 46 // threads, the test needs to use INSTANT events. The typical procedure is to | |
| 47 // specify the same unique ID as a TRACE_EVENT argument on both the start and | |
| 48 // finish INSTANT events. Then use the following procedure to associate those | |
| 49 // events. | |
| 50 // | |
| 51 // Step 1: instrument code with custom begin/end trace events. | |
| 52 // [Thread 1 tracing code] | |
| 53 // TRACE_EVENT_INSTANT1("test_latency", "timing1_begin", "id", 3); | |
| 54 // [Thread 2 tracing code] | |
| 55 // TRACE_EVENT_INSTANT1("test_latency", "timing1_end", "id", 3); | |
| 56 // | |
| 57 // Step 2: associate these custom begin/end pairs. | |
| 58 // Query begin(Query(EVENT_NAME) == Query::String("timing1_begin")); | |
| 59 // Query end(Query(EVENT_NAME) == Query::String("timing1_end")); | |
| 60 // Query match(Query(EVENT_ARG, "id") == Query(OTHER_ARG, "id")); | |
| 61 // analyzer.AssociateEvents(begin, end, match); | |
| 62 // | |
| 63 // Step 3: search for "timing1_begin" events with existing other event. | |
| 64 // Query q = (Query(EVENT_NAME) == Query::String("timing1_begin") && | |
| 65 // Query(EVENT_HAS_OTHER)); | |
| 66 // analyzer.FindEvents(q, &events); | |
| 67 // | |
| 68 // Step 4: analyze events, such as checking durations. | |
| 69 // for (size_t i = 0; i < events.size(); ++i) { | |
| 70 // double duration; | |
| 71 // EXPECT_TRUE(events[i].GetAbsTimeToOtherEvent(&duration)); | |
| 72 // EXPECT_LT(duration, 1000000.0/60.0); // expect less than 1/60 second. | |
| 73 // } | |
| 74 | |
| 75 | |
| 76 #ifndef BASE_TEST_TRACE_EVENT_ANALYZER_H_ | |
| 77 #define BASE_TEST_TRACE_EVENT_ANALYZER_H_ | |
| 78 | |
| 79 #include <map> | |
| 80 | |
| 81 #include "base/memory/ref_counted.h" | |
| 82 #include "base/trace_event/trace_event.h" | |
| 83 | |
| 84 namespace base { | |
| 85 class Value; | |
| 86 } | |
| 87 | |
| 88 namespace trace_analyzer { | |
| 89 class QueryNode; | |
| 90 | |
| 91 // trace_analyzer::TraceEvent is a more convenient form of the | |
| 92 // base::trace_event::TraceEvent class to make tracing-based tests easier to | |
| 93 // write. | |
| 94 struct TraceEvent { | |
| 95 // ProcessThreadID contains a Process ID and Thread ID. | |
| 96 struct ProcessThreadID { | |
| 97 ProcessThreadID() : process_id(0), thread_id(0) {} | |
| 98 ProcessThreadID(int process_id, int thread_id) | |
| 99 : process_id(process_id), thread_id(thread_id) {} | |
| 100 bool operator< (const ProcessThreadID& rhs) const { | |
| 101 if (process_id != rhs.process_id) | |
| 102 return process_id < rhs.process_id; | |
| 103 return thread_id < rhs.thread_id; | |
| 104 } | |
| 105 int process_id; | |
| 106 int thread_id; | |
| 107 }; | |
| 108 | |
| 109 TraceEvent(); | |
| 110 ~TraceEvent(); | |
| 111 | |
| 112 bool SetFromJSON(const base::Value* event_value) WARN_UNUSED_RESULT; | |
| 113 | |
| 114 bool operator< (const TraceEvent& rhs) const { | |
| 115 return timestamp < rhs.timestamp; | |
| 116 } | |
| 117 | |
| 118 bool has_other_event() const { return other_event; } | |
| 119 | |
| 120 // Returns absolute duration in microseconds between this event and other | |
| 121 // event. Must have already verified that other_event exists by | |
| 122 // Query(EVENT_HAS_OTHER) or by calling has_other_event(). | |
| 123 double GetAbsTimeToOtherEvent() const; | |
| 124 | |
| 125 // Return the argument value if it exists and it is a string. | |
| 126 bool GetArgAsString(const std::string& name, std::string* arg) const; | |
| 127 // Return the argument value if it exists and it is a number. | |
| 128 bool GetArgAsNumber(const std::string& name, double* arg) const; | |
| 129 | |
| 130 // Check if argument exists and is string. | |
| 131 bool HasStringArg(const std::string& name) const; | |
| 132 // Check if argument exists and is number (double, int or bool). | |
| 133 bool HasNumberArg(const std::string& name) const; | |
| 134 | |
| 135 // Get known existing arguments as specific types. | |
| 136 // Useful when you have already queried the argument with | |
| 137 // Query(HAS_NUMBER_ARG) or Query(HAS_STRING_ARG). | |
| 138 std::string GetKnownArgAsString(const std::string& name) const; | |
| 139 double GetKnownArgAsDouble(const std::string& name) const; | |
| 140 int GetKnownArgAsInt(const std::string& name) const; | |
| 141 bool GetKnownArgAsBool(const std::string& name) const; | |
| 142 | |
| 143 // Process ID and Thread ID. | |
| 144 ProcessThreadID thread; | |
| 145 | |
| 146 // Time since epoch in microseconds. | |
| 147 // Stored as double to match its JSON representation. | |
| 148 double timestamp; | |
| 149 | |
| 150 double duration; | |
| 151 | |
| 152 char phase; | |
| 153 | |
| 154 std::string category; | |
| 155 | |
| 156 std::string name; | |
| 157 | |
| 158 std::string id; | |
| 159 | |
| 160 // All numbers and bool values from TraceEvent args are cast to double. | |
| 161 // bool becomes 1.0 (true) or 0.0 (false). | |
| 162 std::map<std::string, double> arg_numbers; | |
| 163 | |
| 164 std::map<std::string, std::string> arg_strings; | |
| 165 | |
| 166 // The other event associated with this event (or NULL). | |
| 167 const TraceEvent* other_event; | |
| 168 }; | |
| 169 | |
| 170 typedef std::vector<const TraceEvent*> TraceEventVector; | |
| 171 | |
| 172 class Query { | |
| 173 public: | |
| 174 Query(const Query& query); | |
| 175 | |
| 176 ~Query(); | |
| 177 | |
| 178 //////////////////////////////////////////////////////////////// | |
| 179 // Query literal values | |
| 180 | |
| 181 // Compare with the given string. | |
| 182 static Query String(const std::string& str); | |
| 183 | |
| 184 // Compare with the given number. | |
| 185 static Query Double(double num); | |
| 186 static Query Int(int32 num); | |
| 187 static Query Uint(uint32 num); | |
| 188 | |
| 189 // Compare with the given bool. | |
| 190 static Query Bool(bool boolean); | |
| 191 | |
| 192 // Compare with the given phase. | |
| 193 static Query Phase(char phase); | |
| 194 | |
| 195 // Compare with the given string pattern. Only works with == and != operators. | |
| 196 // Example: Query(EVENT_NAME) == Query::Pattern("MyEvent*") | |
| 197 static Query Pattern(const std::string& pattern); | |
| 198 | |
| 199 //////////////////////////////////////////////////////////////// | |
| 200 // Query event members | |
| 201 | |
| 202 static Query EventPid() { return Query(EVENT_PID); } | |
| 203 | |
| 204 static Query EventTid() { return Query(EVENT_TID); } | |
| 205 | |
| 206 // Return the timestamp of the event in microseconds since epoch. | |
| 207 static Query EventTime() { return Query(EVENT_TIME); } | |
| 208 | |
| 209 // Return the absolute time between event and other event in microseconds. | |
| 210 // Only works if Query::EventHasOther() == true. | |
| 211 static Query EventDuration() { return Query(EVENT_DURATION); } | |
| 212 | |
| 213 // Return the duration of a COMPLETE event. | |
| 214 static Query EventCompleteDuration() { | |
| 215 return Query(EVENT_COMPLETE_DURATION); | |
| 216 } | |
| 217 | |
| 218 static Query EventPhase() { return Query(EVENT_PHASE); } | |
| 219 | |
| 220 static Query EventCategory() { return Query(EVENT_CATEGORY); } | |
| 221 | |
| 222 static Query EventName() { return Query(EVENT_NAME); } | |
| 223 | |
| 224 static Query EventId() { return Query(EVENT_ID); } | |
| 225 | |
| 226 static Query EventPidIs(int process_id) { | |
| 227 return Query(EVENT_PID) == Query::Int(process_id); | |
| 228 } | |
| 229 | |
| 230 static Query EventTidIs(int thread_id) { | |
| 231 return Query(EVENT_TID) == Query::Int(thread_id); | |
| 232 } | |
| 233 | |
| 234 static Query EventThreadIs(const TraceEvent::ProcessThreadID& thread) { | |
| 235 return EventPidIs(thread.process_id) && EventTidIs(thread.thread_id); | |
| 236 } | |
| 237 | |
| 238 static Query EventTimeIs(double timestamp) { | |
| 239 return Query(EVENT_TIME) == Query::Double(timestamp); | |
| 240 } | |
| 241 | |
| 242 static Query EventDurationIs(double duration) { | |
| 243 return Query(EVENT_DURATION) == Query::Double(duration); | |
| 244 } | |
| 245 | |
| 246 static Query EventPhaseIs(char phase) { | |
| 247 return Query(EVENT_PHASE) == Query::Phase(phase); | |
| 248 } | |
| 249 | |
| 250 static Query EventCategoryIs(const std::string& category) { | |
| 251 return Query(EVENT_CATEGORY) == Query::String(category); | |
| 252 } | |
| 253 | |
| 254 static Query EventNameIs(const std::string& name) { | |
| 255 return Query(EVENT_NAME) == Query::String(name); | |
| 256 } | |
| 257 | |
| 258 static Query EventIdIs(const std::string& id) { | |
| 259 return Query(EVENT_ID) == Query::String(id); | |
| 260 } | |
| 261 | |
| 262 // Evaluates to true if arg exists and is a string. | |
| 263 static Query EventHasStringArg(const std::string& arg_name) { | |
| 264 return Query(EVENT_HAS_STRING_ARG, arg_name); | |
| 265 } | |
| 266 | |
| 267 // Evaluates to true if arg exists and is a number. | |
| 268 // Number arguments include types double, int and bool. | |
| 269 static Query EventHasNumberArg(const std::string& arg_name) { | |
| 270 return Query(EVENT_HAS_NUMBER_ARG, arg_name); | |
| 271 } | |
| 272 | |
| 273 // Evaluates to arg value (string or number). | |
| 274 static Query EventArg(const std::string& arg_name) { | |
| 275 return Query(EVENT_ARG, arg_name); | |
| 276 } | |
| 277 | |
| 278 // Return true if associated event exists. | |
| 279 static Query EventHasOther() { return Query(EVENT_HAS_OTHER); } | |
| 280 | |
| 281 // Access the associated other_event's members: | |
| 282 | |
| 283 static Query OtherPid() { return Query(OTHER_PID); } | |
| 284 | |
| 285 static Query OtherTid() { return Query(OTHER_TID); } | |
| 286 | |
| 287 static Query OtherTime() { return Query(OTHER_TIME); } | |
| 288 | |
| 289 static Query OtherPhase() { return Query(OTHER_PHASE); } | |
| 290 | |
| 291 static Query OtherCategory() { return Query(OTHER_CATEGORY); } | |
| 292 | |
| 293 static Query OtherName() { return Query(OTHER_NAME); } | |
| 294 | |
| 295 static Query OtherId() { return Query(OTHER_ID); } | |
| 296 | |
| 297 static Query OtherPidIs(int process_id) { | |
| 298 return Query(OTHER_PID) == Query::Int(process_id); | |
| 299 } | |
| 300 | |
| 301 static Query OtherTidIs(int thread_id) { | |
| 302 return Query(OTHER_TID) == Query::Int(thread_id); | |
| 303 } | |
| 304 | |
| 305 static Query OtherThreadIs(const TraceEvent::ProcessThreadID& thread) { | |
| 306 return OtherPidIs(thread.process_id) && OtherTidIs(thread.thread_id); | |
| 307 } | |
| 308 | |
| 309 static Query OtherTimeIs(double timestamp) { | |
| 310 return Query(OTHER_TIME) == Query::Double(timestamp); | |
| 311 } | |
| 312 | |
| 313 static Query OtherPhaseIs(char phase) { | |
| 314 return Query(OTHER_PHASE) == Query::Phase(phase); | |
| 315 } | |
| 316 | |
| 317 static Query OtherCategoryIs(const std::string& category) { | |
| 318 return Query(OTHER_CATEGORY) == Query::String(category); | |
| 319 } | |
| 320 | |
| 321 static Query OtherNameIs(const std::string& name) { | |
| 322 return Query(OTHER_NAME) == Query::String(name); | |
| 323 } | |
| 324 | |
| 325 static Query OtherIdIs(const std::string& id) { | |
| 326 return Query(OTHER_ID) == Query::String(id); | |
| 327 } | |
| 328 | |
| 329 // Evaluates to true if arg exists and is a string. | |
| 330 static Query OtherHasStringArg(const std::string& arg_name) { | |
| 331 return Query(OTHER_HAS_STRING_ARG, arg_name); | |
| 332 } | |
| 333 | |
| 334 // Evaluates to true if arg exists and is a number. | |
| 335 // Number arguments include types double, int and bool. | |
| 336 static Query OtherHasNumberArg(const std::string& arg_name) { | |
| 337 return Query(OTHER_HAS_NUMBER_ARG, arg_name); | |
| 338 } | |
| 339 | |
| 340 // Evaluates to arg value (string or number). | |
| 341 static Query OtherArg(const std::string& arg_name) { | |
| 342 return Query(OTHER_ARG, arg_name); | |
| 343 } | |
| 344 | |
| 345 //////////////////////////////////////////////////////////////// | |
| 346 // Common queries: | |
| 347 | |
| 348 // Find BEGIN events that have a corresponding END event. | |
| 349 static Query MatchBeginWithEnd() { | |
| 350 return (Query(EVENT_PHASE) == Query::Phase(TRACE_EVENT_PHASE_BEGIN)) && | |
| 351 Query(EVENT_HAS_OTHER); | |
| 352 } | |
| 353 | |
| 354 // Find COMPLETE events. | |
| 355 static Query MatchComplete() { | |
| 356 return (Query(EVENT_PHASE) == Query::Phase(TRACE_EVENT_PHASE_COMPLETE)); | |
| 357 } | |
| 358 | |
| 359 // Find ASYNC_BEGIN events that have a corresponding ASYNC_END event. | |
| 360 static Query MatchAsyncBeginWithNext() { | |
| 361 return (Query(EVENT_PHASE) == | |
| 362 Query::Phase(TRACE_EVENT_PHASE_ASYNC_BEGIN)) && | |
| 363 Query(EVENT_HAS_OTHER); | |
| 364 } | |
| 365 | |
| 366 // Find BEGIN events of given |name| which also have associated END events. | |
| 367 static Query MatchBeginName(const std::string& name) { | |
| 368 return (Query(EVENT_NAME) == Query(name)) && MatchBeginWithEnd(); | |
| 369 } | |
| 370 | |
| 371 // Find COMPLETE events of given |name|. | |
| 372 static Query MatchCompleteName(const std::string& name) { | |
| 373 return (Query(EVENT_NAME) == Query(name)) && MatchComplete(); | |
| 374 } | |
| 375 | |
| 376 // Match given Process ID and Thread ID. | |
| 377 static Query MatchThread(const TraceEvent::ProcessThreadID& thread) { | |
| 378 return (Query(EVENT_PID) == Query::Int(thread.process_id)) && | |
| 379 (Query(EVENT_TID) == Query::Int(thread.thread_id)); | |
| 380 } | |
| 381 | |
| 382 // Match event pair that spans multiple threads. | |
| 383 static Query MatchCrossThread() { | |
| 384 return (Query(EVENT_PID) != Query(OTHER_PID)) || | |
| 385 (Query(EVENT_TID) != Query(OTHER_TID)); | |
| 386 } | |
| 387 | |
| 388 //////////////////////////////////////////////////////////////// | |
| 389 // Operators: | |
| 390 | |
| 391 // Boolean operators: | |
| 392 Query operator==(const Query& rhs) const; | |
| 393 Query operator!=(const Query& rhs) const; | |
| 394 Query operator< (const Query& rhs) const; | |
| 395 Query operator<=(const Query& rhs) const; | |
| 396 Query operator> (const Query& rhs) const; | |
| 397 Query operator>=(const Query& rhs) const; | |
| 398 Query operator&&(const Query& rhs) const; | |
| 399 Query operator||(const Query& rhs) const; | |
| 400 Query operator!() const; | |
| 401 | |
| 402 // Arithmetic operators: | |
| 403 // Following operators are applied to double arguments: | |
| 404 Query operator+(const Query& rhs) const; | |
| 405 Query operator-(const Query& rhs) const; | |
| 406 Query operator*(const Query& rhs) const; | |
| 407 Query operator/(const Query& rhs) const; | |
| 408 Query operator-() const; | |
| 409 // Mod operates on int64 args (doubles are casted to int64 beforehand): | |
| 410 Query operator%(const Query& rhs) const; | |
| 411 | |
| 412 // Return true if the given event matches this query tree. | |
| 413 // This is a recursive method that walks the query tree. | |
| 414 bool Evaluate(const TraceEvent& event) const; | |
| 415 | |
| 416 private: | |
| 417 enum TraceEventMember { | |
| 418 EVENT_INVALID, | |
| 419 EVENT_PID, | |
| 420 EVENT_TID, | |
| 421 EVENT_TIME, | |
| 422 EVENT_DURATION, | |
| 423 EVENT_COMPLETE_DURATION, | |
| 424 EVENT_PHASE, | |
| 425 EVENT_CATEGORY, | |
| 426 EVENT_NAME, | |
| 427 EVENT_ID, | |
| 428 EVENT_HAS_STRING_ARG, | |
| 429 EVENT_HAS_NUMBER_ARG, | |
| 430 EVENT_ARG, | |
| 431 EVENT_HAS_OTHER, | |
| 432 OTHER_PID, | |
| 433 OTHER_TID, | |
| 434 OTHER_TIME, | |
| 435 OTHER_PHASE, | |
| 436 OTHER_CATEGORY, | |
| 437 OTHER_NAME, | |
| 438 OTHER_ID, | |
| 439 OTHER_HAS_STRING_ARG, | |
| 440 OTHER_HAS_NUMBER_ARG, | |
| 441 OTHER_ARG, | |
| 442 }; | |
| 443 | |
| 444 enum Operator { | |
| 445 OP_INVALID, | |
| 446 // Boolean operators: | |
| 447 OP_EQ, | |
| 448 OP_NE, | |
| 449 OP_LT, | |
| 450 OP_LE, | |
| 451 OP_GT, | |
| 452 OP_GE, | |
| 453 OP_AND, | |
| 454 OP_OR, | |
| 455 OP_NOT, | |
| 456 // Arithmetic operators: | |
| 457 OP_ADD, | |
| 458 OP_SUB, | |
| 459 OP_MUL, | |
| 460 OP_DIV, | |
| 461 OP_MOD, | |
| 462 OP_NEGATE | |
| 463 }; | |
| 464 | |
| 465 enum QueryType { | |
| 466 QUERY_BOOLEAN_OPERATOR, | |
| 467 QUERY_ARITHMETIC_OPERATOR, | |
| 468 QUERY_EVENT_MEMBER, | |
| 469 QUERY_NUMBER, | |
| 470 QUERY_STRING | |
| 471 }; | |
| 472 | |
| 473 // Compare with the given member. | |
| 474 explicit Query(TraceEventMember member); | |
| 475 | |
| 476 // Compare with the given member argument value. | |
| 477 Query(TraceEventMember member, const std::string& arg_name); | |
| 478 | |
| 479 // Compare with the given string. | |
| 480 explicit Query(const std::string& str); | |
| 481 | |
| 482 // Compare with the given number. | |
| 483 explicit Query(double num); | |
| 484 | |
| 485 // Construct a boolean Query that returns (left <binary_op> right). | |
| 486 Query(const Query& left, const Query& right, Operator binary_op); | |
| 487 | |
| 488 // Construct a boolean Query that returns (<binary_op> left). | |
| 489 Query(const Query& left, Operator unary_op); | |
| 490 | |
| 491 // Try to compare left_ against right_ based on operator_. | |
| 492 // If either left or right does not convert to double, false is returned. | |
| 493 // Otherwise, true is returned and |result| is set to the comparison result. | |
| 494 bool CompareAsDouble(const TraceEvent& event, bool* result) const; | |
| 495 | |
| 496 // Try to compare left_ against right_ based on operator_. | |
| 497 // If either left or right does not convert to string, false is returned. | |
| 498 // Otherwise, true is returned and |result| is set to the comparison result. | |
| 499 bool CompareAsString(const TraceEvent& event, bool* result) const; | |
| 500 | |
| 501 // Attempt to convert this Query to a double. On success, true is returned | |
| 502 // and the double value is stored in |num|. | |
| 503 bool GetAsDouble(const TraceEvent& event, double* num) const; | |
| 504 | |
| 505 // Attempt to convert this Query to a string. On success, true is returned | |
| 506 // and the string value is stored in |str|. | |
| 507 bool GetAsString(const TraceEvent& event, std::string* str) const; | |
| 508 | |
| 509 // Evaluate this Query as an arithmetic operator on left_ and right_. | |
| 510 bool EvaluateArithmeticOperator(const TraceEvent& event, | |
| 511 double* num) const; | |
| 512 | |
| 513 // For QUERY_EVENT_MEMBER Query: attempt to get the double value of the Query. | |
| 514 bool GetMemberValueAsDouble(const TraceEvent& event, double* num) const; | |
| 515 | |
| 516 // For QUERY_EVENT_MEMBER Query: attempt to get the string value of the Query. | |
| 517 bool GetMemberValueAsString(const TraceEvent& event, std::string* num) const; | |
| 518 | |
| 519 // Does this Query represent a value? | |
| 520 bool is_value() const { return type_ != QUERY_BOOLEAN_OPERATOR; } | |
| 521 | |
| 522 bool is_unary_operator() const { | |
| 523 return operator_ == OP_NOT || operator_ == OP_NEGATE; | |
| 524 } | |
| 525 | |
| 526 bool is_comparison_operator() const { | |
| 527 return operator_ != OP_INVALID && operator_ < OP_AND; | |
| 528 } | |
| 529 | |
| 530 const Query& left() const; | |
| 531 const Query& right() const; | |
| 532 | |
| 533 QueryType type_; | |
| 534 Operator operator_; | |
| 535 scoped_refptr<QueryNode> left_; | |
| 536 scoped_refptr<QueryNode> right_; | |
| 537 TraceEventMember member_; | |
| 538 double number_; | |
| 539 std::string string_; | |
| 540 bool is_pattern_; | |
| 541 }; | |
| 542 | |
| 543 // Implementation detail: | |
| 544 // QueryNode allows Query to store a ref-counted query tree. | |
| 545 class QueryNode : public base::RefCounted<QueryNode> { | |
| 546 public: | |
| 547 explicit QueryNode(const Query& query); | |
| 548 const Query& query() const { return query_; } | |
| 549 | |
| 550 private: | |
| 551 friend class base::RefCounted<QueryNode>; | |
| 552 ~QueryNode(); | |
| 553 | |
| 554 Query query_; | |
| 555 }; | |
| 556 | |
| 557 // TraceAnalyzer helps tests search for trace events. | |
| 558 class TraceAnalyzer { | |
| 559 public: | |
| 560 ~TraceAnalyzer(); | |
| 561 | |
| 562 // Use trace events from JSON string generated by tracing API. | |
| 563 // Returns non-NULL if the JSON is successfully parsed. | |
| 564 static TraceAnalyzer* Create(const std::string& json_events) | |
| 565 WARN_UNUSED_RESULT; | |
| 566 | |
| 567 void SetIgnoreMetadataEvents(bool ignore) { ignore_metadata_events_ = true; } | |
| 568 | |
| 569 // Associate BEGIN and END events with each other. This allows Query(OTHER_*) | |
| 570 // to access the associated event and enables Query(EVENT_DURATION). | |
| 571 // An end event will match the most recent begin event with the same name, | |
| 572 // category, process ID and thread ID. This matches what is shown in | |
| 573 // about:tracing. After association, the BEGIN event will point to the | |
| 574 // matching END event, but the END event will not point to the BEGIN event. | |
| 575 void AssociateBeginEndEvents(); | |
| 576 | |
| 577 // Associate ASYNC_BEGIN, ASYNC_STEP and ASYNC_END events with each other. | |
| 578 // An ASYNC_END event will match the most recent ASYNC_BEGIN or ASYNC_STEP | |
| 579 // event with the same name, category, and ID. This creates a singly linked | |
| 580 // list of ASYNC_BEGIN->ASYNC_STEP...->ASYNC_END. | |
| 581 void AssociateAsyncBeginEndEvents(); | |
| 582 | |
| 583 // AssociateEvents can be used to customize event associations by setting the | |
| 584 // other_event member of TraceEvent. This should be used to associate two | |
| 585 // INSTANT events. | |
| 586 // | |
| 587 // The assumptions are: | |
| 588 // - |first| events occur before |second| events. | |
| 589 // - the closest matching |second| event is the correct match. | |
| 590 // | |
| 591 // |first| - Eligible |first| events match this query. | |
| 592 // |second| - Eligible |second| events match this query. | |
| 593 // |match| - This query is run on the |first| event. The OTHER_* EventMember | |
| 594 // queries will point to an eligible |second| event. The query | |
| 595 // should evaluate to true if the |first|/|second| pair is a match. | |
| 596 // | |
| 597 // When a match is found, the pair will be associated by having the first | |
| 598 // event's other_event member point to the other. AssociateEvents does not | |
| 599 // clear previous associations, so it is possible to associate multiple pairs | |
| 600 // of events by calling AssociateEvents more than once with different queries. | |
| 601 // | |
| 602 // NOTE: AssociateEvents will overwrite existing other_event associations if | |
| 603 // the queries pass for events that already had a previous association. | |
| 604 // | |
| 605 // After calling any Find* method, it is not allowed to call AssociateEvents | |
| 606 // again. | |
| 607 void AssociateEvents(const Query& first, | |
| 608 const Query& second, | |
| 609 const Query& match); | |
| 610 | |
| 611 // For each event, copy its arguments to the other_event argument map. If | |
| 612 // argument name already exists, it will not be overwritten. | |
| 613 void MergeAssociatedEventArgs(); | |
| 614 | |
| 615 // Find all events that match query and replace output vector. | |
| 616 size_t FindEvents(const Query& query, TraceEventVector* output); | |
| 617 | |
| 618 // Find first event that matches query or NULL if not found. | |
| 619 const TraceEvent* FindFirstOf(const Query& query); | |
| 620 | |
| 621 // Find last event that matches query or NULL if not found. | |
| 622 const TraceEvent* FindLastOf(const Query& query); | |
| 623 | |
| 624 const std::string& GetThreadName(const TraceEvent::ProcessThreadID& thread); | |
| 625 | |
| 626 private: | |
| 627 TraceAnalyzer(); | |
| 628 | |
| 629 bool SetEvents(const std::string& json_events) WARN_UNUSED_RESULT; | |
| 630 | |
| 631 // Read metadata (thread names, etc) from events. | |
| 632 void ParseMetadata(); | |
| 633 | |
| 634 std::map<TraceEvent::ProcessThreadID, std::string> thread_names_; | |
| 635 std::vector<TraceEvent> raw_events_; | |
| 636 bool ignore_metadata_events_; | |
| 637 bool allow_assocation_changes_; | |
| 638 | |
| 639 DISALLOW_COPY_AND_ASSIGN(TraceAnalyzer); | |
| 640 }; | |
| 641 | |
| 642 // Utility functions for TraceEventVector. | |
| 643 | |
| 644 struct RateStats { | |
| 645 double min_us; | |
| 646 double max_us; | |
| 647 double mean_us; | |
| 648 double standard_deviation_us; | |
| 649 }; | |
| 650 | |
| 651 struct RateStatsOptions { | |
| 652 RateStatsOptions() : trim_min(0u), trim_max(0u) {} | |
| 653 // After the times between events are sorted, the number of specified elements | |
| 654 // will be trimmed before calculating the RateStats. This is useful in cases | |
| 655 // where extreme outliers are tolerable and should not skew the overall | |
| 656 // average. | |
| 657 size_t trim_min; // Trim this many minimum times. | |
| 658 size_t trim_max; // Trim this many maximum times. | |
| 659 }; | |
| 660 | |
| 661 // Calculate min/max/mean and standard deviation from the times between | |
| 662 // adjacent events. | |
| 663 bool GetRateStats(const TraceEventVector& events, | |
| 664 RateStats* stats, | |
| 665 const RateStatsOptions* options); | |
| 666 | |
| 667 // Starting from |position|, find the first event that matches |query|. | |
| 668 // Returns true if found, false otherwise. | |
| 669 bool FindFirstOf(const TraceEventVector& events, | |
| 670 const Query& query, | |
| 671 size_t position, | |
| 672 size_t* return_index); | |
| 673 | |
| 674 // Starting from |position|, find the last event that matches |query|. | |
| 675 // Returns true if found, false otherwise. | |
| 676 bool FindLastOf(const TraceEventVector& events, | |
| 677 const Query& query, | |
| 678 size_t position, | |
| 679 size_t* return_index); | |
| 680 | |
| 681 // Find the closest events to |position| in time that match |query|. | |
| 682 // return_second_closest may be NULL. Closeness is determined by comparing | |
| 683 // with the event timestamp. | |
| 684 // Returns true if found, false otherwise. If both return parameters are | |
| 685 // requested, both must be found for a successful result. | |
| 686 bool FindClosest(const TraceEventVector& events, | |
| 687 const Query& query, | |
| 688 size_t position, | |
| 689 size_t* return_closest, | |
| 690 size_t* return_second_closest); | |
| 691 | |
| 692 // Count matches, inclusive of |begin_position|, exclusive of |end_position|. | |
| 693 size_t CountMatches(const TraceEventVector& events, | |
| 694 const Query& query, | |
| 695 size_t begin_position, | |
| 696 size_t end_position); | |
| 697 | |
| 698 // Count all matches. | |
| 699 static inline size_t CountMatches(const TraceEventVector& events, | |
| 700 const Query& query) { | |
| 701 return CountMatches(events, query, 0u, events.size()); | |
| 702 } | |
| 703 | |
| 704 } // namespace trace_analyzer | |
| 705 | |
| 706 #endif // BASE_TEST_TRACE_EVENT_ANALYZER_H_ | |
| OLD | NEW |