Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/perl -w
0002 # SPDX-License-Identifier: GPL-2.0-only
0003 # (c) 2009, Tom Zanussi <tzanussi@gmail.com>
0004 
0005 # Display r/w activity for all processes
0006 
0007 # The common_* event handler fields are the most useful fields common to
0008 # all events.  They don't necessarily correspond to the 'common_*' fields
0009 # in the status files.  Those fields not available as handler params can
0010 # be retrieved via script functions of the form get_common_*().
0011 
0012 use 5.010000;
0013 use strict;
0014 use warnings;
0015 
0016 use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
0017 use lib "./Perf-Trace-Util/lib";
0018 use Perf::Trace::Core;
0019 use Perf::Trace::Util;
0020 
0021 my %reads;
0022 my %writes;
0023 
0024 sub syscalls::sys_exit_read
0025 {
0026     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
0027     $common_pid, $common_comm, $common_callchain,
0028     $nr, $ret) = @_;
0029 
0030     if ($ret > 0) {
0031     $reads{$common_pid}{bytes_read} += $ret;
0032     } else {
0033     if (!defined ($reads{$common_pid}{bytes_read})) {
0034         $reads{$common_pid}{bytes_read} = 0;
0035     }
0036     $reads{$common_pid}{errors}{$ret}++;
0037     }
0038 }
0039 
0040 sub syscalls::sys_enter_read
0041 {
0042     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
0043     $common_pid, $common_comm, $common_callchain,
0044     $nr, $fd, $buf, $count) = @_;
0045 
0046     $reads{$common_pid}{bytes_requested} += $count;
0047     $reads{$common_pid}{total_reads}++;
0048     $reads{$common_pid}{comm} = $common_comm;
0049 }
0050 
0051 sub syscalls::sys_exit_write
0052 {
0053     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
0054     $common_pid, $common_comm, $common_callchain,
0055     $nr, $ret) = @_;
0056 
0057     if ($ret <= 0) {
0058     $writes{$common_pid}{errors}{$ret}++;
0059     }
0060 }
0061 
0062 sub syscalls::sys_enter_write
0063 {
0064     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
0065     $common_pid, $common_comm, $common_callchain,
0066     $nr, $fd, $buf, $count) = @_;
0067 
0068     $writes{$common_pid}{bytes_written} += $count;
0069     $writes{$common_pid}{total_writes}++;
0070     $writes{$common_pid}{comm} = $common_comm;
0071 }
0072 
0073 sub trace_end
0074 {
0075     printf("read counts by pid:\n\n");
0076 
0077     printf("%6s  %20s  %10s  %10s  %10s\n", "pid", "comm",
0078        "# reads", "bytes_requested", "bytes_read");
0079     printf("%6s  %-20s  %10s  %10s  %10s\n", "------", "--------------------",
0080        "-----------", "----------", "----------");
0081 
0082     foreach my $pid (sort { ($reads{$b}{bytes_read} || 0) <=>
0083                 ($reads{$a}{bytes_read} || 0) } keys %reads) {
0084     my $comm = $reads{$pid}{comm} || "";
0085     my $total_reads = $reads{$pid}{total_reads} || 0;
0086     my $bytes_requested = $reads{$pid}{bytes_requested} || 0;
0087     my $bytes_read = $reads{$pid}{bytes_read} || 0;
0088 
0089     printf("%6s  %-20s  %10s  %10s  %10s\n", $pid, $comm,
0090            $total_reads, $bytes_requested, $bytes_read);
0091     }
0092 
0093     printf("\nfailed reads by pid:\n\n");
0094 
0095     printf("%6s  %20s  %6s  %10s\n", "pid", "comm", "error #", "# errors");
0096     printf("%6s  %20s  %6s  %10s\n", "------", "--------------------",
0097        "------", "----------");
0098 
0099     my @errcounts = ();
0100 
0101     foreach my $pid (keys %reads) {
0102     foreach my $error (keys %{$reads{$pid}{errors}}) {
0103         my $comm = $reads{$pid}{comm} || "";
0104         my $errcount = $reads{$pid}{errors}{$error} || 0;
0105         push @errcounts, [$pid, $comm, $error, $errcount];
0106     }
0107     }
0108 
0109     @errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;
0110 
0111     for my $i (0 .. $#errcounts) {
0112     printf("%6d  %-20s  %6d  %10s\n", $errcounts[$i][0],
0113            $errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);
0114     }
0115 
0116     printf("\nwrite counts by pid:\n\n");
0117 
0118     printf("%6s  %20s  %10s  %10s\n", "pid", "comm",
0119        "# writes", "bytes_written");
0120     printf("%6s  %-20s  %10s  %10s\n", "------", "--------------------",
0121        "-----------", "----------");
0122 
0123     foreach my $pid (sort { ($writes{$b}{bytes_written} || 0) <=>
0124             ($writes{$a}{bytes_written} || 0)} keys %writes) {
0125     my $comm = $writes{$pid}{comm} || "";
0126     my $total_writes = $writes{$pid}{total_writes} || 0;
0127     my $bytes_written = $writes{$pid}{bytes_written} || 0;
0128 
0129     printf("%6s  %-20s  %10s  %10s\n", $pid, $comm,
0130            $total_writes, $bytes_written);
0131     }
0132 
0133     printf("\nfailed writes by pid:\n\n");
0134 
0135     printf("%6s  %20s  %6s  %10s\n", "pid", "comm", "error #", "# errors");
0136     printf("%6s  %20s  %6s  %10s\n", "------", "--------------------",
0137        "------", "----------");
0138 
0139     @errcounts = ();
0140 
0141     foreach my $pid (keys %writes) {
0142     foreach my $error (keys %{$writes{$pid}{errors}}) {
0143         my $comm = $writes{$pid}{comm} || "";
0144         my $errcount = $writes{$pid}{errors}{$error} || 0;
0145         push @errcounts, [$pid, $comm, $error, $errcount];
0146     }
0147     }
0148 
0149     @errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;
0150 
0151     for my $i (0 .. $#errcounts) {
0152     printf("%6d  %-20s  %6d  %10s\n", $errcounts[$i][0],
0153            $errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);
0154     }
0155 
0156     print_unhandled();
0157 }
0158 
0159 my %unhandled;
0160 
0161 sub print_unhandled
0162 {
0163     if ((scalar keys %unhandled) == 0) {
0164     return;
0165     }
0166 
0167     print "\nunhandled events:\n\n";
0168 
0169     printf("%-40s  %10s\n", "event", "count");
0170     printf("%-40s  %10s\n", "----------------------------------------",
0171        "-----------");
0172 
0173     foreach my $event_name (keys %unhandled) {
0174     printf("%-40s  %10d\n", $event_name, $unhandled{$event_name});
0175     }
0176 }
0177 
0178 sub trace_unhandled
0179 {
0180     my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
0181     $common_pid, $common_comm, $common_callchain) = @_;
0182 
0183     $unhandled{$event_name}++;
0184 }