^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/usr/bin/perl -w
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # (c) 2010, Tom Zanussi <tzanussi@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # read/write top
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # Periodically displays system-wide r/w call activity, broken down by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # pid. If an [interval] arg is specified, the display will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) # refreshed every [interval] seconds. The default interval is 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) # seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) use 5.010000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) use strict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) use warnings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) use lib "./Perf-Trace-Util/lib";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) use Perf::Trace::Core;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) use Perf::Trace::Util;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) use POSIX qw/SIGALRM SA_RESTART/;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) my $default_interval = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) my $nlines = 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) my $print_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) my $print_pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) my %reads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) my %writes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) my $interval = shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (!$interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) $interval = $default_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) sub syscalls::sys_exit_read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) $common_pid, $common_comm, $common_callchain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) $nr, $ret) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) print_check();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if ($ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) $reads{$common_pid}{bytes_read} += $ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (!defined ($reads{$common_pid}{bytes_read})) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) $reads{$common_pid}{bytes_read} = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) $reads{$common_pid}{errors}{$ret}++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) sub syscalls::sys_enter_read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) $common_pid, $common_comm, $common_callchain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) $nr, $fd, $buf, $count) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) print_check();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) $reads{$common_pid}{bytes_requested} += $count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) $reads{$common_pid}{total_reads}++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) $reads{$common_pid}{comm} = $common_comm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) sub syscalls::sys_exit_write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) $common_pid, $common_comm, $common_callchain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) $nr, $ret) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) print_check();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if ($ret <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) $writes{$common_pid}{errors}{$ret}++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) sub syscalls::sys_enter_write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) $common_pid, $common_comm, $common_callchain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) $nr, $fd, $buf, $count) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) print_check();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) $writes{$common_pid}{bytes_written} += $count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) $writes{$common_pid}{total_writes}++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) $writes{$common_pid}{comm} = $common_comm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) sub trace_begin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) my $sa = POSIX::SigAction->new(\&set_print_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) $sa->flags(SA_RESTART);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) $sa->safe(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) POSIX::sigaction(SIGALRM, $sa) or die "Can't set SIGALRM handler: $!\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) alarm 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) sub trace_end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) print_unhandled();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) print_totals();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) sub print_check()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if ($print_pending == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) $print_pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) print_totals();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) sub set_print_pending()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) $print_pending = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) alarm $interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) sub print_totals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) my $count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) $count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) clear_term();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) printf("\nread counts by pid:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) printf("%6s %20s %10s %10s %10s\n", "pid", "comm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) "# reads", "bytes_req", "bytes_read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) printf("%6s %-20s %10s %10s %10s\n", "------", "--------------------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) "----------", "----------", "----------");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) foreach my $pid (sort { ($reads{$b}{bytes_read} || 0) <=>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ($reads{$a}{bytes_read} || 0) } keys %reads) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) my $comm = $reads{$pid}{comm} || "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) my $total_reads = $reads{$pid}{total_reads} || 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) my $bytes_requested = $reads{$pid}{bytes_requested} || 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) my $bytes_read = $reads{$pid}{bytes_read} || 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) printf("%6s %-20s %10s %10s %10s\n", $pid, $comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) $total_reads, $bytes_requested, $bytes_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (++$count == $nlines) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) $count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) printf("\nwrite counts by pid:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) printf("%6s %20s %10s %13s\n", "pid", "comm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) "# writes", "bytes_written");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) printf("%6s %-20s %10s %13s\n", "------", "--------------------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) "----------", "-------------");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) foreach my $pid (sort { ($writes{$b}{bytes_written} || 0) <=>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ($writes{$a}{bytes_written} || 0)} keys %writes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) my $comm = $writes{$pid}{comm} || "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) my $total_writes = $writes{$pid}{total_writes} || 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) my $bytes_written = $writes{$pid}{bytes_written} || 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) printf("%6s %-20s %10s %13s\n", $pid, $comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) $total_writes, $bytes_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (++$count == $nlines) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) %reads = ();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) %writes = ();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) my %unhandled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) sub print_unhandled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if ((scalar keys %unhandled) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) print "\nunhandled events:\n\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) printf("%-40s %10s\n", "event", "count");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) printf("%-40s %10s\n", "----------------------------------------",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) "-----------");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) foreach my $event_name (keys %unhandled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) printf("%-40s %10d\n", $event_name, $unhandled{$event_name});
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) sub trace_unhandled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) $common_pid, $common_comm, $common_callchain) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) $unhandled{$event_name}++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }