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