Home | History | Annotate | Download | only in perl
      1 #!/usr/bin/perl -w
      2 # (c) 2009, Tom Zanussi <tzanussi (at] gmail.com>
      3 # Licensed under the terms of the GNU GPL License version 2
      4 
      5 # Display r/w activity for all processes
      6 
      7 # The common_* event handler fields are the most useful fields common to
      8 # all events.  They don't necessarily correspond to the 'common_*' fields
      9 # in the status files.  Those fields not available as handler params can
     10 # be retrieved via script functions of the form get_common_*().
     11 
     12 use 5.010000;
     13 use strict;
     14 use warnings;
     15 
     16 use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
     17 use lib "./Perf-Trace-Util/lib";
     18 use Perf::Trace::Core;
     19 use Perf::Trace::Util;
     20 
     21 my %reads;
     22 my %writes;
     23 
     24 sub syscalls::sys_exit_read
     25 {
     26     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
     27 	$common_pid, $common_comm,
     28 	$nr, $ret) = @_;
     29 
     30     if ($ret > 0) {
     31 	$reads{$common_pid}{bytes_read} += $ret;
     32     } else {
     33 	if (!defined ($reads{$common_pid}{bytes_read})) {
     34 	    $reads{$common_pid}{bytes_read} = 0;
     35 	}
     36 	$reads{$common_pid}{errors}{$ret}++;
     37     }
     38 }
     39 
     40 sub syscalls::sys_enter_read
     41 {
     42     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
     43 	$common_pid, $common_comm,
     44 	$nr, $fd, $buf, $count) = @_;
     45 
     46     $reads{$common_pid}{bytes_requested} += $count;
     47     $reads{$common_pid}{total_reads}++;
     48     $reads{$common_pid}{comm} = $common_comm;
     49 }
     50 
     51 sub syscalls::sys_exit_write
     52 {
     53     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
     54 	$common_pid, $common_comm,
     55 	$nr, $ret) = @_;
     56 
     57     if ($ret <= 0) {
     58 	$writes{$common_pid}{errors}{$ret}++;
     59     }
     60 }
     61 
     62 sub syscalls::sys_enter_write
     63 {
     64     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
     65 	$common_pid, $common_comm,
     66 	$nr, $fd, $buf, $count) = @_;
     67 
     68     $writes{$common_pid}{bytes_written} += $count;
     69     $writes{$common_pid}{total_writes}++;
     70     $writes{$common_pid}{comm} = $common_comm;
     71 }
     72 
     73 sub trace_end
     74 {
     75     printf("read counts by pid:\n\n");
     76 
     77     printf("%6s  %20s  %10s  %10s  %10s\n", "pid", "comm",
     78 	   "# reads", "bytes_requested", "bytes_read");
     79     printf("%6s  %-20s  %10s  %10s  %10s\n", "------", "--------------------",
     80 	   "-----------", "----------", "----------");
     81 
     82     foreach my $pid (sort { ($reads{$b}{bytes_read} || 0) <=>
     83 				($reads{$a}{bytes_read} || 0) } keys %reads) {
     84 	my $comm = $reads{$pid}{comm} || "";
     85 	my $total_reads = $reads{$pid}{total_reads} || 0;
     86 	my $bytes_requested = $reads{$pid}{bytes_requested} || 0;
     87 	my $bytes_read = $reads{$pid}{bytes_read} || 0;
     88 
     89 	printf("%6s  %-20s  %10s  %10s  %10s\n", $pid, $comm,
     90 	       $total_reads, $bytes_requested, $bytes_read);
     91     }
     92 
     93     printf("\nfailed reads by pid:\n\n");
     94 
     95     printf("%6s  %20s  %6s  %10s\n", "pid", "comm", "error #", "# errors");
     96     printf("%6s  %20s  %6s  %10s\n", "------", "--------------------",
     97 	   "------", "----------");
     98 
     99     my @errcounts = ();
    100 
    101     foreach my $pid (keys %reads) {
    102 	foreach my $error (keys %{$reads{$pid}{errors}}) {
    103 	    my $comm = $reads{$pid}{comm} || "";
    104 	    my $errcount = $reads{$pid}{errors}{$error} || 0;
    105 	    push @errcounts, [$pid, $comm, $error, $errcount];
    106 	}
    107     }
    108 
    109     @errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;
    110 
    111     for my $i (0 .. $#errcounts) {
    112 	printf("%6d  %-20s  %6d  %10s\n", $errcounts[$i][0],
    113 	       $errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);
    114     }
    115 
    116     printf("\nwrite counts by pid:\n\n");
    117 
    118     printf("%6s  %20s  %10s  %10s\n", "pid", "comm",
    119 	   "# writes", "bytes_written");
    120     printf("%6s  %-20s  %10s  %10s\n", "------", "--------------------",
    121 	   "-----------", "----------");
    122 
    123     foreach my $pid (sort { ($writes{$b}{bytes_written} || 0) <=>
    124 			($writes{$a}{bytes_written} || 0)} keys %writes) {
    125 	my $comm = $writes{$pid}{comm} || "";
    126 	my $total_writes = $writes{$pid}{total_writes} || 0;
    127 	my $bytes_written = $writes{$pid}{bytes_written} || 0;
    128 
    129 	printf("%6s  %-20s  %10s  %10s\n", $pid, $comm,
    130 	       $total_writes, $bytes_written);
    131     }
    132 
    133     printf("\nfailed writes by pid:\n\n");
    134 
    135     printf("%6s  %20s  %6s  %10s\n", "pid", "comm", "error #", "# errors");
    136     printf("%6s  %20s  %6s  %10s\n", "------", "--------------------",
    137 	   "------", "----------");
    138 
    139     @errcounts = ();
    140 
    141     foreach my $pid (keys %writes) {
    142 	foreach my $error (keys %{$writes{$pid}{errors}}) {
    143 	    my $comm = $writes{$pid}{comm} || "";
    144 	    my $errcount = $writes{$pid}{errors}{$error} || 0;
    145 	    push @errcounts, [$pid, $comm, $error, $errcount];
    146 	}
    147     }
    148 
    149     @errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;
    150 
    151     for my $i (0 .. $#errcounts) {
    152 	printf("%6d  %-20s  %6d  %10s\n", $errcounts[$i][0],
    153 	       $errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);
    154     }
    155 
    156     print_unhandled();
    157 }
    158 
    159 my %unhandled;
    160 
    161 sub print_unhandled
    162 {
    163     if ((scalar keys %unhandled) == 0) {
    164 	return;
    165     }
    166 
    167     print "\nunhandled events:\n\n";
    168 
    169     printf("%-40s  %10s\n", "event", "count");
    170     printf("%-40s  %10s\n", "----------------------------------------",
    171 	   "-----------");
    172 
    173     foreach my $event_name (keys %unhandled) {
    174 	printf("%-40s  %10d\n", $event_name, $unhandled{$event_name});
    175     }
    176 }
    177 
    178 sub trace_unhandled
    179 {
    180     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
    181 	$common_pid, $common_comm) = @_;
    182 
    183     $unhandled{$event_name}++;
    184 }
    185