rw-by-file.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # (c) 2009, Tom Zanussi <tzanussi@gmail.com>
  2. # Licensed under the terms of the GNU GPL License version 2
  3. # Display r/w activity for files read/written to for a given program
  4. # The common_* event handler fields are the most useful fields common to
  5. # all events. They don't necessarily correspond to the 'common_*' fields
  6. # in the status files. Those fields not available as handler params can
  7. # be retrieved via script functions of the form get_common_*().
  8. use 5.010000;
  9. use strict;
  10. use warnings;
  11. use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
  12. use lib "./Perf-Trace-Util/lib";
  13. use Perf::Trace::Core;
  14. use Perf::Trace::Util;
  15. my $usage = "perf script -s rw-by-file.pl <comm>\n";
  16. my $for_comm = shift or die $usage;
  17. my %reads;
  18. my %writes;
  19. sub syscalls::sys_enter_read
  20. {
  21. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  22. $common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;
  23. if ($common_comm eq $for_comm) {
  24. $reads{$fd}{bytes_requested} += $count;
  25. $reads{$fd}{total_reads}++;
  26. }
  27. }
  28. sub syscalls::sys_enter_write
  29. {
  30. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  31. $common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;
  32. if ($common_comm eq $for_comm) {
  33. $writes{$fd}{bytes_written} += $count;
  34. $writes{$fd}{total_writes}++;
  35. }
  36. }
  37. sub trace_end
  38. {
  39. printf("file read counts for $for_comm:\n\n");
  40. printf("%6s %10s %10s\n", "fd", "# reads", "bytes_requested");
  41. printf("%6s %10s %10s\n", "------", "----------", "-----------");
  42. foreach my $fd (sort {$reads{$b}{bytes_requested} <=>
  43. $reads{$a}{bytes_requested}} keys %reads) {
  44. my $total_reads = $reads{$fd}{total_reads};
  45. my $bytes_requested = $reads{$fd}{bytes_requested};
  46. printf("%6u %10u %10u\n", $fd, $total_reads, $bytes_requested);
  47. }
  48. printf("\nfile write counts for $for_comm:\n\n");
  49. printf("%6s %10s %10s\n", "fd", "# writes", "bytes_written");
  50. printf("%6s %10s %10s\n", "------", "----------", "-----------");
  51. foreach my $fd (sort {$writes{$b}{bytes_written} <=>
  52. $writes{$a}{bytes_written}} keys %writes) {
  53. my $total_writes = $writes{$fd}{total_writes};
  54. my $bytes_written = $writes{$fd}{bytes_written};
  55. printf("%6u %10u %10u\n", $fd, $total_writes, $bytes_written);
  56. }
  57. print_unhandled();
  58. }
  59. my %unhandled;
  60. sub print_unhandled
  61. {
  62. if ((scalar keys %unhandled) == 0) {
  63. return;
  64. }
  65. print "\nunhandled events:\n\n";
  66. printf("%-40s %10s\n", "event", "count");
  67. printf("%-40s %10s\n", "----------------------------------------",
  68. "-----------");
  69. foreach my $event_name (keys %unhandled) {
  70. printf("%-40s %10d\n", $event_name, $unhandled{$event_name});
  71. }
  72. }
  73. sub trace_unhandled
  74. {
  75. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  76. $common_pid, $common_comm) = @_;
  77. $unhandled{$event_name}++;
  78. }