rw-by-file.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/perl -w
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. # (c) 2009, Tom Zanussi <tzanussi@gmail.com>
  4. # Display r/w activity for files read/written to for a given program
  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 $usage = "perf script -s rw-by-file.pl <comm>\n";
  17. my $for_comm = shift or die $usage;
  18. my %reads;
  19. my %writes;
  20. sub syscalls::sys_enter_read
  21. {
  22. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  23. $common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;
  24. if ($common_comm eq $for_comm) {
  25. $reads{$fd}{bytes_requested} += $count;
  26. $reads{$fd}{total_reads}++;
  27. }
  28. }
  29. sub syscalls::sys_enter_write
  30. {
  31. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  32. $common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;
  33. if ($common_comm eq $for_comm) {
  34. $writes{$fd}{bytes_written} += $count;
  35. $writes{$fd}{total_writes}++;
  36. }
  37. }
  38. sub trace_end
  39. {
  40. printf("file read counts for $for_comm:\n\n");
  41. printf("%6s %10s %10s\n", "fd", "# reads", "bytes_requested");
  42. printf("%6s %10s %10s\n", "------", "----------", "-----------");
  43. foreach my $fd (sort {$reads{$b}{bytes_requested} <=>
  44. $reads{$a}{bytes_requested}} keys %reads) {
  45. my $total_reads = $reads{$fd}{total_reads};
  46. my $bytes_requested = $reads{$fd}{bytes_requested};
  47. printf("%6u %10u %10u\n", $fd, $total_reads, $bytes_requested);
  48. }
  49. printf("\nfile write counts for $for_comm:\n\n");
  50. printf("%6s %10s %10s\n", "fd", "# writes", "bytes_written");
  51. printf("%6s %10s %10s\n", "------", "----------", "-----------");
  52. foreach my $fd (sort {$writes{$b}{bytes_written} <=>
  53. $writes{$a}{bytes_written}} keys %writes) {
  54. my $total_writes = $writes{$fd}{total_writes};
  55. my $bytes_written = $writes{$fd}{bytes_written};
  56. printf("%6u %10u %10u\n", $fd, $total_writes, $bytes_written);
  57. }
  58. print_unhandled();
  59. }
  60. my %unhandled;
  61. sub print_unhandled
  62. {
  63. if ((scalar keys %unhandled) == 0) {
  64. return;
  65. }
  66. print "\nunhandled events:\n\n";
  67. printf("%-40s %10s\n", "event", "count");
  68. printf("%-40s %10s\n", "----------------------------------------",
  69. "-----------");
  70. foreach my $event_name (keys %unhandled) {
  71. printf("%-40s %10d\n", $event_name, $unhandled{$event_name});
  72. }
  73. }
  74. sub trace_unhandled
  75. {
  76. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  77. $common_pid, $common_comm) = @_;
  78. $unhandled{$event_name}++;
  79. }