Path: blob/master/tools/perf/scripts/perl/rw-by-file.pl
10823 views
#!/usr/bin/perl -w1# (c) 2009, Tom Zanussi <[email protected]>2# Licensed under the terms of the GNU GPL License version 234# Display r/w activity for files read/written to for a given program56# The common_* event handler fields are the most useful fields common to7# all events. They don't necessarily correspond to the 'common_*' fields8# in the status files. Those fields not available as handler params can9# be retrieved via script functions of the form get_common_*().1011use 5.010000;12use strict;13use warnings;1415use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";16use lib "./Perf-Trace-Util/lib";17use Perf::Trace::Core;18use Perf::Trace::Util;1920my $usage = "perf script -s rw-by-file.pl <comm>\n";2122my $for_comm = shift or die $usage;2324my %reads;25my %writes;2627sub syscalls::sys_enter_read28{29my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,30$common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;3132if ($common_comm eq $for_comm) {33$reads{$fd}{bytes_requested} += $count;34$reads{$fd}{total_reads}++;35}36}3738sub syscalls::sys_enter_write39{40my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,41$common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;4243if ($common_comm eq $for_comm) {44$writes{$fd}{bytes_written} += $count;45$writes{$fd}{total_writes}++;46}47}4849sub trace_end50{51printf("file read counts for $for_comm:\n\n");5253printf("%6s %10s %10s\n", "fd", "# reads", "bytes_requested");54printf("%6s %10s %10s\n", "------", "----------", "-----------");5556foreach my $fd (sort {$reads{$b}{bytes_requested} <=>57$reads{$a}{bytes_requested}} keys %reads) {58my $total_reads = $reads{$fd}{total_reads};59my $bytes_requested = $reads{$fd}{bytes_requested};60printf("%6u %10u %10u\n", $fd, $total_reads, $bytes_requested);61}6263printf("\nfile write counts for $for_comm:\n\n");6465printf("%6s %10s %10s\n", "fd", "# writes", "bytes_written");66printf("%6s %10s %10s\n", "------", "----------", "-----------");6768foreach my $fd (sort {$writes{$b}{bytes_written} <=>69$writes{$a}{bytes_written}} keys %writes) {70my $total_writes = $writes{$fd}{total_writes};71my $bytes_written = $writes{$fd}{bytes_written};72printf("%6u %10u %10u\n", $fd, $total_writes, $bytes_written);73}7475print_unhandled();76}7778my %unhandled;7980sub print_unhandled81{82if ((scalar keys %unhandled) == 0) {83return;84}8586print "\nunhandled events:\n\n";8788printf("%-40s %10s\n", "event", "count");89printf("%-40s %10s\n", "----------------------------------------",90"-----------");9192foreach my $event_name (keys %unhandled) {93printf("%-40s %10d\n", $event_name, $unhandled{$event_name});94}95}9697sub trace_unhandled98{99my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,100$common_pid, $common_comm) = @_;101102$unhandled{$event_name}++;103}104105106107108