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