wakeup-latency.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/perl -w
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. # (c) 2009, Tom Zanussi <tzanussi@gmail.com>
  4. # Display avg/min/max wakeup latency
  5. # The common_* event handler fields are the most useful fields common to
  6. # all events. They don't necessarily correspond to the 'common_*' fields
  7. # in the status files. Those fields not available as handler params can
  8. # be retrieved via script functions of the form get_common_*().
  9. use 5.010000;
  10. use strict;
  11. use warnings;
  12. use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
  13. use lib "./Perf-Trace-Util/lib";
  14. use Perf::Trace::Core;
  15. use Perf::Trace::Util;
  16. my %last_wakeup;
  17. my $max_wakeup_latency;
  18. my $min_wakeup_latency;
  19. my $total_wakeup_latency = 0;
  20. my $total_wakeups = 0;
  21. sub sched::sched_switch
  22. {
  23. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  24. $common_pid, $common_comm,
  25. $prev_comm, $prev_pid, $prev_prio, $prev_state, $next_comm, $next_pid,
  26. $next_prio) = @_;
  27. my $wakeup_ts = $last_wakeup{$common_cpu}{ts};
  28. if ($wakeup_ts) {
  29. my $switch_ts = nsecs($common_secs, $common_nsecs);
  30. my $wakeup_latency = $switch_ts - $wakeup_ts;
  31. if ($wakeup_latency > $max_wakeup_latency) {
  32. $max_wakeup_latency = $wakeup_latency;
  33. }
  34. if ($wakeup_latency < $min_wakeup_latency) {
  35. $min_wakeup_latency = $wakeup_latency;
  36. }
  37. $total_wakeup_latency += $wakeup_latency;
  38. $total_wakeups++;
  39. }
  40. $last_wakeup{$common_cpu}{ts} = 0;
  41. }
  42. sub sched::sched_wakeup
  43. {
  44. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  45. $common_pid, $common_comm,
  46. $comm, $pid, $prio, $success, $target_cpu) = @_;
  47. $last_wakeup{$target_cpu}{ts} = nsecs($common_secs, $common_nsecs);
  48. }
  49. sub trace_begin
  50. {
  51. $min_wakeup_latency = 1000000000;
  52. $max_wakeup_latency = 0;
  53. }
  54. sub trace_end
  55. {
  56. printf("wakeup_latency stats:\n\n");
  57. print "total_wakeups: $total_wakeups\n";
  58. if ($total_wakeups) {
  59. printf("avg_wakeup_latency (ns): %u\n",
  60. avg($total_wakeup_latency, $total_wakeups));
  61. } else {
  62. printf("avg_wakeup_latency (ns): N/A\n");
  63. }
  64. printf("min_wakeup_latency (ns): %u\n", $min_wakeup_latency);
  65. printf("max_wakeup_latency (ns): %u\n", $max_wakeup_latency);
  66. print_unhandled();
  67. }
  68. my %unhandled;
  69. sub print_unhandled
  70. {
  71. if ((scalar keys %unhandled) == 0) {
  72. return;
  73. }
  74. print "\nunhandled events:\n\n";
  75. printf("%-40s %10s\n", "event", "count");
  76. printf("%-40s %10s\n", "----------------------------------------",
  77. "-----------");
  78. foreach my $event_name (keys %unhandled) {
  79. printf("%-40s %10d\n", $event_name, $unhandled{$event_name});
  80. }
  81. }
  82. sub trace_unhandled
  83. {
  84. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  85. $common_pid, $common_comm) = @_;
  86. $unhandled{$event_name}++;
  87. }