Path: blob/master/tools/perf/scripts/perl/rw-by-pid.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 all processes56# 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 %reads;21my %writes;2223sub syscalls::sys_exit_read24{25my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,26$common_pid, $common_comm,27$nr, $ret) = @_;2829if ($ret > 0) {30$reads{$common_pid}{bytes_read} += $ret;31} else {32if (!defined ($reads{$common_pid}{bytes_read})) {33$reads{$common_pid}{bytes_read} = 0;34}35$reads{$common_pid}{errors}{$ret}++;36}37}3839sub syscalls::sys_enter_read40{41my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,42$common_pid, $common_comm,43$nr, $fd, $buf, $count) = @_;4445$reads{$common_pid}{bytes_requested} += $count;46$reads{$common_pid}{total_reads}++;47$reads{$common_pid}{comm} = $common_comm;48}4950sub syscalls::sys_exit_write51{52my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,53$common_pid, $common_comm,54$nr, $ret) = @_;5556if ($ret <= 0) {57$writes{$common_pid}{errors}{$ret}++;58}59}6061sub syscalls::sys_enter_write62{63my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,64$common_pid, $common_comm,65$nr, $fd, $buf, $count) = @_;6667$writes{$common_pid}{bytes_written} += $count;68$writes{$common_pid}{total_writes}++;69$writes{$common_pid}{comm} = $common_comm;70}7172sub trace_end73{74printf("read counts by pid:\n\n");7576printf("%6s %20s %10s %10s %10s\n", "pid", "comm",77"# reads", "bytes_requested", "bytes_read");78printf("%6s %-20s %10s %10s %10s\n", "------", "--------------------",79"-----------", "----------", "----------");8081foreach my $pid (sort { ($reads{$b}{bytes_read} || 0) <=>82($reads{$a}{bytes_read} || 0) } keys %reads) {83my $comm = $reads{$pid}{comm} || "";84my $total_reads = $reads{$pid}{total_reads} || 0;85my $bytes_requested = $reads{$pid}{bytes_requested} || 0;86my $bytes_read = $reads{$pid}{bytes_read} || 0;8788printf("%6s %-20s %10s %10s %10s\n", $pid, $comm,89$total_reads, $bytes_requested, $bytes_read);90}9192printf("\nfailed reads by pid:\n\n");9394printf("%6s %20s %6s %10s\n", "pid", "comm", "error #", "# errors");95printf("%6s %20s %6s %10s\n", "------", "--------------------",96"------", "----------");9798my @errcounts = ();99100foreach my $pid (keys %reads) {101foreach my $error (keys %{$reads{$pid}{errors}}) {102my $comm = $reads{$pid}{comm} || "";103my $errcount = $reads{$pid}{errors}{$error} || 0;104push @errcounts, [$pid, $comm, $error, $errcount];105}106}107108@errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;109110for my $i (0 .. $#errcounts) {111printf("%6d %-20s %6d %10s\n", $errcounts[$i][0],112$errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);113}114115printf("\nwrite counts by pid:\n\n");116117printf("%6s %20s %10s %10s\n", "pid", "comm",118"# writes", "bytes_written");119printf("%6s %-20s %10s %10s\n", "------", "--------------------",120"-----------", "----------");121122foreach my $pid (sort { ($writes{$b}{bytes_written} || 0) <=>123($writes{$a}{bytes_written} || 0)} keys %writes) {124my $comm = $writes{$pid}{comm} || "";125my $total_writes = $writes{$pid}{total_writes} || 0;126my $bytes_written = $writes{$pid}{bytes_written} || 0;127128printf("%6s %-20s %10s %10s\n", $pid, $comm,129$total_writes, $bytes_written);130}131132printf("\nfailed writes by pid:\n\n");133134printf("%6s %20s %6s %10s\n", "pid", "comm", "error #", "# errors");135printf("%6s %20s %6s %10s\n", "------", "--------------------",136"------", "----------");137138@errcounts = ();139140foreach my $pid (keys %writes) {141foreach my $error (keys %{$writes{$pid}{errors}}) {142my $comm = $writes{$pid}{comm} || "";143my $errcount = $writes{$pid}{errors}{$error} || 0;144push @errcounts, [$pid, $comm, $error, $errcount];145}146}147148@errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;149150for my $i (0 .. $#errcounts) {151printf("%6d %-20s %6d %10s\n", $errcounts[$i][0],152$errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);153}154155print_unhandled();156}157158my %unhandled;159160sub print_unhandled161{162if ((scalar keys %unhandled) == 0) {163return;164}165166print "\nunhandled events:\n\n";167168printf("%-40s %10s\n", "event", "count");169printf("%-40s %10s\n", "----------------------------------------",170"-----------");171172foreach my $event_name (keys %unhandled) {173printf("%-40s %10d\n", $event_name, $unhandled{$event_name});174}175}176177sub trace_unhandled178{179my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,180$common_pid, $common_comm) = @_;181182$unhandled{$event_name}++;183}184185186