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 #include "base/test/trace_event_analyzer.h" | 5 #include "base/test/trace_event_analyzer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <math.h> | 8 #include <math.h> |
9 | 9 |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 static_cast<const base::DictionaryValue*>(event_value); | 34 static_cast<const base::DictionaryValue*>(event_value); |
35 | 35 |
36 std::string phase_str; | 36 std::string phase_str; |
37 base::DictionaryValue* args = NULL; | 37 base::DictionaryValue* args = NULL; |
38 | 38 |
39 if (!dictionary->GetString("ph", &phase_str)) { | 39 if (!dictionary->GetString("ph", &phase_str)) { |
40 LOG(ERROR) << "ph is missing from TraceEvent JSON"; | 40 LOG(ERROR) << "ph is missing from TraceEvent JSON"; |
41 return false; | 41 return false; |
42 } | 42 } |
43 | 43 |
44 phase = base::debug::TraceEvent::GetPhase(phase_str.c_str()); | 44 phase = *phase_str.data(); |
45 | 45 |
46 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA); | 46 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA); |
47 bool require_id = (phase == TRACE_EVENT_PHASE_START || | 47 bool require_id = (phase == TRACE_EVENT_PHASE_START || |
48 phase == TRACE_EVENT_PHASE_FINISH); | 48 phase == TRACE_EVENT_PHASE_FINISH); |
49 | 49 |
50 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) { | 50 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) { |
51 LOG(ERROR) << "pid is missing from TraceEvent JSON"; | 51 LOG(ERROR) << "pid is missing from TraceEvent JSON"; |
52 return false; | 52 return false; |
53 } | 53 } |
54 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) { | 54 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 } | 222 } |
223 | 223 |
224 Query Query::Uint(uint32 num) { | 224 Query Query::Uint(uint32 num) { |
225 return Query(static_cast<double>(num)); | 225 return Query(static_cast<double>(num)); |
226 } | 226 } |
227 | 227 |
228 Query Query::Bool(bool boolean) { | 228 Query Query::Bool(bool boolean) { |
229 return Query(boolean ? 1.0 : 0.0); | 229 return Query(boolean ? 1.0 : 0.0); |
230 } | 230 } |
231 | 231 |
232 Query Query::Phase(base::debug::TraceEventPhase phase) { | 232 Query Query::Phase(char phase) { |
233 return Query(static_cast<double>(phase)); | 233 return Query(static_cast<double>(phase)); |
234 } | 234 } |
235 | 235 |
236 Query Query::Pattern(const std::string& pattern) { | 236 Query Query::Pattern(const std::string& pattern) { |
237 Query query(pattern); | 237 Query query(pattern); |
238 query.is_pattern_ = true; | 238 query.is_pattern_ = true; |
239 return query; | 239 return query; |
240 } | 240 } |
241 | 241 |
242 bool Query::Evaluate(const TraceEvent& event) const { | 242 bool Query::Evaluate(const TraceEvent& event) const { |
(...skipping 18 matching lines...) Expand all Loading... | |
261 if (is_comparison_operator()) { | 261 if (is_comparison_operator()) { |
262 DCHECK(left().is_value() && right().is_value()) | 262 DCHECK(left().is_value() && right().is_value()) |
263 << "Invalid query: comparison operator used between event member and " | 263 << "Invalid query: comparison operator used between event member and " |
264 "value."; | 264 "value."; |
265 bool compare_result = false; | 265 bool compare_result = false; |
266 if (CompareAsDouble(event, &compare_result)) | 266 if (CompareAsDouble(event, &compare_result)) |
267 return compare_result; | 267 return compare_result; |
268 else if (CompareAsString(event, &compare_result)) | 268 else if (CompareAsString(event, &compare_result)) |
269 return compare_result; | 269 return compare_result; |
270 return false; | 270 return false; |
271 } else { | 271 } else { |
jar (doing other things)
2012/01/12 02:20:03
nit: with all the returns, you don't need an else
jbates
2012/01/12 19:41:29
Done.
| |
272 switch (operator_) { | 272 switch (operator_) { |
273 case OP_AND: | 273 case OP_AND: |
274 return left().Evaluate(event) && right().Evaluate(event); | 274 return left().Evaluate(event) && right().Evaluate(event); |
275 case OP_OR: | 275 case OP_OR: |
276 return left().Evaluate(event) || right().Evaluate(event); | 276 return left().Evaluate(event) || right().Evaluate(event); |
277 case OP_NOT: | 277 case OP_NOT: |
278 return !left().Evaluate(event); | 278 return !left().Evaluate(event); |
279 default: | 279 default: |
280 NOTREACHED(); | 280 NOTREACHED(); |
281 } | 281 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 case OP_NEGATE: | 386 case OP_NEGATE: |
387 *num = -lhs; | 387 *num = -lhs; |
388 return true; | 388 return true; |
389 default: | 389 default: |
390 NOTREACHED(); | 390 NOTREACHED(); |
391 return false; | 391 return false; |
392 } | 392 } |
393 } | 393 } |
394 | 394 |
395 bool Query::GetAsDouble(const TraceEvent& event, double* num) const { | 395 bool Query::GetAsDouble(const TraceEvent& event, double* num) const { |
396 base::debug::TraceValue value; | |
397 switch (type_) { | 396 switch (type_) { |
398 case QUERY_ARITHMETIC_OPERATOR: | 397 case QUERY_ARITHMETIC_OPERATOR: |
399 return EvaluateArithmeticOperator(event, num); | 398 return EvaluateArithmeticOperator(event, num); |
400 case QUERY_EVENT_MEMBER: | 399 case QUERY_EVENT_MEMBER: |
401 value = GetMemberValue(event); | 400 return GetMemberValueAsDouble(event, num); |
402 if (value.type() == base::debug::TraceValue::TRACE_TYPE_DOUBLE) { | |
403 *num = value.as_double(); | |
404 return true; | |
405 } | |
406 return false; | |
407 case QUERY_NUMBER: | 401 case QUERY_NUMBER: |
408 *num = number_; | 402 *num = number_; |
409 return true; | 403 return true; |
410 default: | 404 default: |
411 return false; | 405 return false; |
412 } | 406 } |
413 } | 407 } |
414 | 408 |
415 bool Query::GetAsString(const TraceEvent& event, std::string* str) const { | 409 bool Query::GetAsString(const TraceEvent& event, std::string* str) const { |
416 base::debug::TraceValue value; | |
417 switch (type_) { | 410 switch (type_) { |
418 case QUERY_EVENT_MEMBER: | 411 case QUERY_EVENT_MEMBER: |
419 value = GetMemberValue(event); | 412 return GetMemberValueAsString(event, str); |
420 if (value.is_string()) { | |
421 *str = value.as_string(); | |
422 return true; | |
423 } | |
424 return false; | |
425 case QUERY_STRING: | 413 case QUERY_STRING: |
426 *str = string_; | 414 *str = string_; |
427 return true; | 415 return true; |
428 default: | 416 default: |
429 return false; | 417 return false; |
430 } | 418 } |
431 } | 419 } |
432 | 420 |
433 base::debug::TraceValue Query::GetMemberValue(const TraceEvent& event) const { | 421 bool Query::GetMemberValueAsDouble(const TraceEvent& event, |
422 double* num) const { | |
434 DCHECK(type_ == QUERY_EVENT_MEMBER); | 423 DCHECK(type_ == QUERY_EVENT_MEMBER); |
435 | 424 |
436 // This could be a request for a member of |event| or a member of |event|'s | 425 // This could be a request for a member of |event| or a member of |event|'s |
437 // associated event. Store the target event in the_event: | 426 // associated event. Store the target event in the_event: |
438 const TraceEvent* the_event = (member_ < OTHER_PID) ? | 427 const TraceEvent* the_event = (member_ < OTHER_PID) ? |
439 &event : event.other_event; | 428 &event : event.other_event; |
440 | 429 |
441 // Request for member of associated event, but there is no associated event. | 430 // Request for member of associated event, but there is no associated event. |
442 if (!the_event) | 431 if (!the_event) |
443 return base::debug::TraceValue(); | 432 return false; |
444 | 433 |
445 switch (member_) { | 434 switch (member_) { |
446 case EVENT_PID: | 435 case EVENT_PID: |
447 case OTHER_PID: | 436 case OTHER_PID: |
448 return static_cast<double>(the_event->thread.process_id); | 437 *num = static_cast<double>(the_event->thread.process_id); |
438 return true; | |
449 case EVENT_TID: | 439 case EVENT_TID: |
450 case OTHER_TID: | 440 case OTHER_TID: |
451 return static_cast<double>(the_event->thread.thread_id); | 441 *num = static_cast<double>(the_event->thread.thread_id); |
442 return true; | |
452 case EVENT_TIME: | 443 case EVENT_TIME: |
453 case OTHER_TIME: | 444 case OTHER_TIME: |
454 return the_event->timestamp; | 445 *num = the_event->timestamp; |
446 return true; | |
455 case EVENT_DURATION: | 447 case EVENT_DURATION: |
456 { | 448 if (the_event->has_other_event()) { |
457 if (the_event->has_other_event()) | 449 *num = the_event->GetAbsTimeToOtherEvent(); |
458 return the_event->GetAbsTimeToOtherEvent(); | 450 return true; |
459 else | |
460 return base::debug::TraceValue(); | |
461 } | 451 } |
452 return false; | |
462 case EVENT_PHASE: | 453 case EVENT_PHASE: |
463 case OTHER_PHASE: | 454 case OTHER_PHASE: |
464 return static_cast<double>(the_event->phase); | 455 *num = static_cast<double>(the_event->phase); |
465 case EVENT_CATEGORY: | 456 return true; |
466 case OTHER_CATEGORY: | |
467 return the_event->category; | |
468 case EVENT_NAME: | |
469 case OTHER_NAME: | |
470 return the_event->name; | |
471 case EVENT_ID: | |
472 case OTHER_ID: | |
473 return the_event->id; | |
474 case EVENT_HAS_STRING_ARG: | 457 case EVENT_HAS_STRING_ARG: |
475 case OTHER_HAS_STRING_ARG: | 458 case OTHER_HAS_STRING_ARG: |
476 return (the_event->HasStringArg(string_) ? 1.0 : 0.0); | 459 *num = (the_event->HasStringArg(string_) ? 1.0 : 0.0); |
460 return true; | |
477 case EVENT_HAS_NUMBER_ARG: | 461 case EVENT_HAS_NUMBER_ARG: |
478 case OTHER_HAS_NUMBER_ARG: | 462 case OTHER_HAS_NUMBER_ARG: |
479 return (the_event->HasNumberArg(string_) ? 1.0 : 0.0); | 463 *num = (the_event->HasNumberArg(string_) ? 1.0 : 0.0); |
464 return true; | |
480 case EVENT_ARG: | 465 case EVENT_ARG: |
481 case OTHER_ARG: | 466 case OTHER_ARG: { |
482 { | 467 // Search for the argument name and return its value if found. |
483 // Search for the argument name and return its value if found. | 468 std::map<std::string, double>::const_iterator num_i = |
484 | 469 the_event->arg_numbers.find(string_); |
485 std::map<std::string, std::string>::const_iterator str_i = | 470 if (num_i != the_event->arg_numbers.end()) { |
486 the_event->arg_strings.find(string_); | 471 *num = num_i->second; |
487 if (str_i != the_event->arg_strings.end()) | 472 return true; |
488 return str_i->second; | |
489 | |
490 std::map<std::string, double>::const_iterator num_i = | |
491 the_event->arg_numbers.find(string_); | |
492 if (num_i != the_event->arg_numbers.end()) | |
493 return num_i->second; | |
494 | |
495 return base::debug::TraceValue(); | |
496 } | 473 } |
474 return false; | |
jar (doing other things)
2012/01/12 02:20:03
nit: I don't know that it is worth changing... but
jbates
2012/01/12 19:41:29
Done.
| |
475 } | |
497 case EVENT_HAS_OTHER: | 476 case EVENT_HAS_OTHER: |
498 { | 477 // return 1.0 (true) if the other event exists |
499 // return 1.0 (true) if the other event exists | 478 *num = event.other_event ? 1.0 : 0.0; |
500 double result = event.other_event ? 1.0 : 0.0; | 479 return true; |
501 return result; | |
502 } | |
503 default: | 480 default: |
504 NOTREACHED(); | 481 return false; |
505 return base::debug::TraceValue(); | |
506 } | 482 } |
507 } | 483 } |
508 | 484 |
485 bool Query::GetMemberValueAsString(const TraceEvent& event, | |
486 std::string* str) const { | |
487 DCHECK(type_ == QUERY_EVENT_MEMBER); | |
488 | |
489 // This could be a request for a member of |event| or a member of |event|'s | |
490 // associated event. Store the target event in the_event: | |
491 const TraceEvent* the_event = (member_ < OTHER_PID) ? | |
492 &event : event.other_event; | |
493 | |
494 // Request for member of associated event, but there is no associated event. | |
495 if (!the_event) | |
496 return false; | |
497 | |
498 switch (member_) { | |
499 case EVENT_CATEGORY: | |
500 case OTHER_CATEGORY: | |
501 *str = the_event->category; | |
502 return true; | |
503 case EVENT_NAME: | |
504 case OTHER_NAME: | |
505 *str = the_event->name; | |
506 return true; | |
507 case EVENT_ID: | |
508 case OTHER_ID: | |
509 *str = the_event->id; | |
510 return true; | |
511 case EVENT_ARG: | |
512 case OTHER_ARG: { | |
513 // Search for the argument name and return its value if found. | |
514 std::map<std::string, std::string>::const_iterator str_i = | |
515 the_event->arg_strings.find(string_); | |
516 if (str_i != the_event->arg_strings.end()) { | |
517 *str = str_i->second; | |
518 return true; | |
519 } | |
520 return false; | |
521 } | |
522 default: | |
523 return false; | |
524 } | |
525 } | |
526 | |
509 Query::Query(const std::string& str) | 527 Query::Query(const std::string& str) |
510 : type_(QUERY_STRING), | 528 : type_(QUERY_STRING), |
511 operator_(OP_INVALID), | 529 operator_(OP_INVALID), |
512 member_(EVENT_INVALID), | 530 member_(EVENT_INVALID), |
513 number_(0), | 531 number_(0), |
514 string_(str), | 532 string_(str), |
515 is_pattern_(false) { | 533 is_pattern_(false) { |
516 } | 534 } |
517 | 535 |
518 Query::Query(double num) | 536 Query::Query(double num) |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
904 size_t count = 0u; | 922 size_t count = 0u; |
905 for (size_t i = begin_position; i < end_position; ++i) { | 923 for (size_t i = begin_position; i < end_position; ++i) { |
906 if (query.Evaluate(*events.at(i))) | 924 if (query.Evaluate(*events.at(i))) |
907 ++count; | 925 ++count; |
908 } | 926 } |
909 return count; | 927 return count; |
910 } | 928 } |
911 | 929 |
912 } // namespace trace_analyzer | 930 } // namespace trace_analyzer |
913 | 931 |
OLD | NEW |