Path: blob/master/tools/perf/scripts/perl/rwtop.pl
10823 views
#!/usr/bin/perl -w1# (c) 2010, Tom Zanussi <[email protected]>2# Licensed under the terms of the GNU GPL License version 234# read/write top5#6# Periodically displays system-wide r/w call activity, broken down by7# pid. If an [interval] arg is specified, the display will be8# refreshed every [interval] seconds. The default interval is 39# seconds.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 $default_interval = 3;21my $nlines = 20;22my $print_thread;23my $print_pending = 0;2425my %reads;26my %writes;2728my $interval = shift;29if (!$interval) {30$interval = $default_interval;31}3233sub syscalls::sys_exit_read34{35my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,36$common_pid, $common_comm,37$nr, $ret) = @_;3839print_check();4041if ($ret > 0) {42$reads{$common_pid}{bytes_read} += $ret;43} else {44if (!defined ($reads{$common_pid}{bytes_read})) {45$reads{$common_pid}{bytes_read} = 0;46}47$reads{$common_pid}{errors}{$ret}++;48}49}5051sub syscalls::sys_enter_read52{53my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,54$common_pid, $common_comm,55$nr, $fd, $buf, $count) = @_;5657print_check();5859$reads{$common_pid}{bytes_requested} += $count;60$reads{$common_pid}{total_reads}++;61$reads{$common_pid}{comm} = $common_comm;62}6364sub syscalls::sys_exit_write65{66my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,67$common_pid, $common_comm,68$nr, $ret) = @_;6970print_check();7172if ($ret <= 0) {73$writes{$common_pid}{errors}{$ret}++;74}75}7677sub syscalls::sys_enter_write78{79my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,80$common_pid, $common_comm,81$nr, $fd, $buf, $count) = @_;8283print_check();8485$writes{$common_pid}{bytes_written} += $count;86$writes{$common_pid}{total_writes}++;87$writes{$common_pid}{comm} = $common_comm;88}8990sub trace_begin91{92$SIG{ALRM} = \&set_print_pending;93alarm 1;94}9596sub trace_end97{98print_unhandled();99print_totals();100}101102sub print_check()103{104if ($print_pending == 1) {105$print_pending = 0;106print_totals();107}108}109110sub set_print_pending()111{112$print_pending = 1;113alarm $interval;114}115116sub print_totals117{118my $count;119120$count = 0;121122clear_term();123124printf("\nread counts by pid:\n\n");125126printf("%6s %20s %10s %10s %10s\n", "pid", "comm",127"# reads", "bytes_req", "bytes_read");128printf("%6s %-20s %10s %10s %10s\n", "------", "--------------------",129"----------", "----------", "----------");130131foreach my $pid (sort { ($reads{$b}{bytes_read} || 0) <=>132($reads{$a}{bytes_read} || 0) } keys %reads) {133my $comm = $reads{$pid}{comm} || "";134my $total_reads = $reads{$pid}{total_reads} || 0;135my $bytes_requested = $reads{$pid}{bytes_requested} || 0;136my $bytes_read = $reads{$pid}{bytes_read} || 0;137138printf("%6s %-20s %10s %10s %10s\n", $pid, $comm,139$total_reads, $bytes_requested, $bytes_read);140141if (++$count == $nlines) {142last;143}144}145146$count = 0;147148printf("\nwrite counts by pid:\n\n");149150printf("%6s %20s %10s %13s\n", "pid", "comm",151"# writes", "bytes_written");152printf("%6s %-20s %10s %13s\n", "------", "--------------------",153"----------", "-------------");154155foreach my $pid (sort { ($writes{$b}{bytes_written} || 0) <=>156($writes{$a}{bytes_written} || 0)} keys %writes) {157my $comm = $writes{$pid}{comm} || "";158my $total_writes = $writes{$pid}{total_writes} || 0;159my $bytes_written = $writes{$pid}{bytes_written} || 0;160161printf("%6s %-20s %10s %13s\n", $pid, $comm,162$total_writes, $bytes_written);163164if (++$count == $nlines) {165last;166}167}168169%reads = ();170%writes = ();171}172173my %unhandled;174175sub print_unhandled176{177if ((scalar keys %unhandled) == 0) {178return;179}180181print "\nunhandled events:\n\n";182183printf("%-40s %10s\n", "event", "count");184printf("%-40s %10s\n", "----------------------------------------",185"-----------");186187foreach my $event_name (keys %unhandled) {188printf("%-40s %10d\n", $event_name, $unhandled{$event_name});189}190}191192sub trace_unhandled193{194my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,195$common_pid, $common_comm) = @_;196197$unhandled{$event_name}++;198}199200201