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