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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // All numbers and bool values from TraceEvent args are cast to double. | 163 // All numbers and bool values from TraceEvent args are cast to double. |
164 // bool becomes 1.0 (true) or 0.0 (false). | 164 // bool becomes 1.0 (true) or 0.0 (false). |
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; |
| 174 |
173 // Pass these values to Query to compare with the corresponding member of a | 175 // Pass these values to Query to compare with the corresponding member of a |
174 // TraceEvent. Unless otherwise specfied, the usage is Query(ENUM_MEMBER). | 176 // TraceEvent. Unless otherwise specfied, the usage is Query(ENUM_MEMBER). |
175 enum TraceEventMember { | 177 enum TraceEventMember { |
176 EVENT_INVALID, | 178 EVENT_INVALID, |
177 // Use these to access the event members: | 179 // Use these to access the event members: |
178 EVENT_PID, | 180 EVENT_PID, |
179 EVENT_TID, | 181 EVENT_TID, |
180 // Return the timestamp of the event in microseconds since epoch. | 182 // Return the timestamp of the event in microseconds since epoch. |
181 EVENT_TIME, | 183 EVENT_TIME, |
182 // Return the absolute time between event and other event in microseconds. | 184 // Return the absolute time between event and other event in microseconds. |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 private: | 413 private: |
412 friend class base::RefCounted<QueryNode>; | 414 friend class base::RefCounted<QueryNode>; |
413 ~QueryNode(); | 415 ~QueryNode(); |
414 | 416 |
415 Query query_; | 417 Query query_; |
416 }; | 418 }; |
417 | 419 |
418 // TraceAnalyzer helps tests search for trace events. | 420 // TraceAnalyzer helps tests search for trace events. |
419 class TraceAnalyzer { | 421 class TraceAnalyzer { |
420 public: | 422 public: |
421 typedef std::vector<const TraceEvent*> TraceEventVector; | |
422 | |
423 struct Stats { | |
424 double min_us; | |
425 double max_us; | |
426 double mean_us; | |
427 double standard_deviation_us; | |
428 }; | |
429 | |
430 ~TraceAnalyzer(); | 423 ~TraceAnalyzer(); |
431 | 424 |
432 // Use trace events from JSON string generated by tracing API. | 425 // Use trace events from JSON string generated by tracing API. |
433 // Returns non-NULL if the JSON is successfully parsed. | 426 // Returns non-NULL if the JSON is successfully parsed. |
434 static TraceAnalyzer* Create(const std::string& json_events) | 427 static TraceAnalyzer* Create(const std::string& json_events) |
435 WARN_UNUSED_RESULT; | 428 WARN_UNUSED_RESULT; |
436 | 429 |
437 // Associate BEGIN and END events with each other. This allows Query(OTHER_*) | 430 // Associate BEGIN and END events with each other. This allows Query(OTHER_*) |
438 // to access the associated event and enables Query(EVENT_DURATION). | 431 // to access the associated event and enables Query(EVENT_DURATION). |
439 // An end event will match the most recent begin event with the same name, | 432 // An end event will match the most recent begin event with the same name, |
(...skipping 27 matching lines...) Expand all Loading... |
467 // | 460 // |
468 // NOTE: AssociateEvents will overwrite existing other_event associations if | 461 // NOTE: AssociateEvents will overwrite existing other_event associations if |
469 // the queries pass for events that already had a previous association. | 462 // the queries pass for events that already had a previous association. |
470 // | 463 // |
471 // After calling FindEvents or FindOneEvent, it is not allowed to call | 464 // After calling FindEvents or FindOneEvent, it is not allowed to call |
472 // AssociateEvents again. | 465 // AssociateEvents again. |
473 void AssociateEvents(const Query& first, | 466 void AssociateEvents(const Query& first, |
474 const Query& second, | 467 const Query& second, |
475 const Query& match); | 468 const Query& match); |
476 | 469 |
| 470 // For each event, copy its arguments to the other_event argument map. If |
| 471 // argument name already exists, it will not be overwritten. |
| 472 void MergeAssociatedEventArgs(); |
| 473 |
477 // Find all events that match query and replace output vector. | 474 // Find all events that match query and replace output vector. |
478 size_t FindEvents(const Query& query, TraceEventVector* output); | 475 size_t FindEvents(const Query& query, TraceEventVector* output); |
479 | 476 |
480 // Helper method: find first event that matches query | 477 // Helper method: find first event that matches query |
481 const TraceEvent* FindOneEvent(const Query& query); | 478 const TraceEvent* FindOneEvent(const Query& query); |
482 | 479 |
483 const std::string& GetThreadName(const TraceEvent::ProcessThreadID& thread); | 480 const std::string& GetThreadName(const TraceEvent::ProcessThreadID& thread); |
484 | 481 |
485 // Calculate min/max/mean and standard deviation from the times between | |
486 // adjacent events. | |
487 static bool GetRateStats(const TraceEventVector& events, Stats* stats); | |
488 | |
489 private: | 482 private: |
490 TraceAnalyzer(); | 483 TraceAnalyzer(); |
491 | 484 |
492 bool SetEvents(const std::string& json_events) WARN_UNUSED_RESULT; | 485 bool SetEvents(const std::string& json_events) WARN_UNUSED_RESULT; |
493 | 486 |
494 // Read metadata (thread names, etc) from events. | 487 // Read metadata (thread names, etc) from events. |
495 void ParseMetadata(); | 488 void ParseMetadata(); |
496 | 489 |
497 std::map<TraceEvent::ProcessThreadID, std::string> thread_names_; | 490 std::map<TraceEvent::ProcessThreadID, std::string> thread_names_; |
498 std::vector<TraceEvent> raw_events_; | 491 std::vector<TraceEvent> raw_events_; |
499 bool allow_assocation_changes_; | 492 bool allow_assocation_changes_; |
500 | 493 |
501 DISALLOW_COPY_AND_ASSIGN(TraceAnalyzer); | 494 DISALLOW_COPY_AND_ASSIGN(TraceAnalyzer); |
502 }; | 495 }; |
503 | 496 |
| 497 // Utility functions for TraceEventVector. |
| 498 |
| 499 struct RateStats { |
| 500 double min_us; |
| 501 double max_us; |
| 502 double mean_us; |
| 503 double standard_deviation_us; |
| 504 }; |
| 505 |
| 506 // Calculate min/max/mean and standard deviation from the times between |
| 507 // adjacent events. |
| 508 bool GetRateStats(const TraceEventVector& events, RateStats* stats); |
| 509 |
| 510 // Starting from |position|, find the first event that matches |query|. |
| 511 // Returns true if found, false otherwise. |
| 512 bool FindFirstOf(const TraceEventVector& events, |
| 513 const Query& query, |
| 514 size_t position, |
| 515 size_t* return_index); |
| 516 |
| 517 // Starting from |position|, find the last event that matches |query|. |
| 518 // Returns true if found, false otherwise. |
| 519 bool FindLastOf(const TraceEventVector& events, |
| 520 const Query& query, |
| 521 size_t position, |
| 522 size_t* return_index); |
| 523 |
| 524 // Find the closest events to |position| in time that match |query|. |
| 525 // return_second_closest may be NULL. Closeness is determined by comparing |
| 526 // with the event timestamp. |
| 527 // Returns true if found, false otherwise. If both return parameters are |
| 528 // requested, both must be found for a successful result. |
| 529 bool FindClosest(const TraceEventVector& events, |
| 530 const Query& query, |
| 531 size_t position, |
| 532 size_t* return_closest, |
| 533 size_t* return_second_closest); |
| 534 |
| 535 // Count matches, inclusive of |begin_position|, exclusive of |end_position|. |
| 536 size_t CountMatches(const TraceEventVector& events, |
| 537 const Query& query, |
| 538 size_t begin_position, |
| 539 size_t end_position); |
| 540 |
| 541 // Count all matches. |
| 542 static inline size_t CountMatches(const TraceEventVector& events, |
| 543 const Query& query) { |
| 544 return CountMatches(events, query, 0u, events.size()); |
| 545 } |
| 546 |
504 } // namespace trace_analyzer | 547 } // namespace trace_analyzer |
505 | 548 |
506 #endif // BASE_TEST_TRACE_EVENT_ANALYZER_H_ | 549 #endif // BASE_TEST_TRACE_EVENT_ANALYZER_H_ |
OLD | NEW |