xref: /linux/tools/perf/scripts/perl/rw-by-pid.pl (revision ead5d1f4d877e92c051e1a1ade623d0d30e71619)
1bcefe12eSTom Zanussi#!/usr/bin/perl -w
24f19048fSThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only
3bcefe12eSTom Zanussi# (c) 2009, Tom Zanussi <tzanussi@gmail.com>
4bcefe12eSTom Zanussi
5bcefe12eSTom Zanussi# Display r/w activity for all processes
6bcefe12eSTom Zanussi
7bcefe12eSTom Zanussi# The common_* event handler fields are the most useful fields common to
8bcefe12eSTom Zanussi# all events.  They don't necessarily correspond to the 'common_*' fields
9bcefe12eSTom Zanussi# in the status files.  Those fields not available as handler params can
10bcefe12eSTom Zanussi# be retrieved via script functions of the form get_common_*().
11bcefe12eSTom Zanussi
12bcefe12eSTom Zanussiuse 5.010000;
13bcefe12eSTom Zanussiuse strict;
14bcefe12eSTom Zanussiuse warnings;
15bcefe12eSTom Zanussi
16bcefe12eSTom Zanussiuse lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
17bcefe12eSTom Zanussiuse lib "./Perf-Trace-Util/lib";
18bcefe12eSTom Zanussiuse Perf::Trace::Core;
19bcefe12eSTom Zanussiuse Perf::Trace::Util;
20bcefe12eSTom Zanussi
21bcefe12eSTom Zanussimy %reads;
22bcefe12eSTom Zanussimy %writes;
23bcefe12eSTom Zanussi
24bcefe12eSTom Zanussisub syscalls::sys_exit_read
25bcefe12eSTom Zanussi{
26bcefe12eSTom Zanussi    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
27*67439d55SMichael Petlan	$common_pid, $common_comm, $common_callchain,
28bcefe12eSTom Zanussi	$nr, $ret) = @_;
29bcefe12eSTom Zanussi
30bcefe12eSTom Zanussi    if ($ret > 0) {
31bcefe12eSTom Zanussi	$reads{$common_pid}{bytes_read} += $ret;
32bcefe12eSTom Zanussi    } else {
33bcefe12eSTom Zanussi	if (!defined ($reads{$common_pid}{bytes_read})) {
34bcefe12eSTom Zanussi	    $reads{$common_pid}{bytes_read} = 0;
35bcefe12eSTom Zanussi	}
36bcefe12eSTom Zanussi	$reads{$common_pid}{errors}{$ret}++;
37bcefe12eSTom Zanussi    }
38bcefe12eSTom Zanussi}
39bcefe12eSTom Zanussi
40bcefe12eSTom Zanussisub syscalls::sys_enter_read
41bcefe12eSTom Zanussi{
42bcefe12eSTom Zanussi    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
43*67439d55SMichael Petlan	$common_pid, $common_comm, $common_callchain,
44bcefe12eSTom Zanussi	$nr, $fd, $buf, $count) = @_;
45bcefe12eSTom Zanussi
46bcefe12eSTom Zanussi    $reads{$common_pid}{bytes_requested} += $count;
47bcefe12eSTom Zanussi    $reads{$common_pid}{total_reads}++;
48bcefe12eSTom Zanussi    $reads{$common_pid}{comm} = $common_comm;
49bcefe12eSTom Zanussi}
50bcefe12eSTom Zanussi
51bcefe12eSTom Zanussisub syscalls::sys_exit_write
52bcefe12eSTom Zanussi{
53bcefe12eSTom Zanussi    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
54*67439d55SMichael Petlan	$common_pid, $common_comm, $common_callchain,
55bcefe12eSTom Zanussi	$nr, $ret) = @_;
56bcefe12eSTom Zanussi
57bcefe12eSTom Zanussi    if ($ret <= 0) {
58bcefe12eSTom Zanussi	$writes{$common_pid}{errors}{$ret}++;
59bcefe12eSTom Zanussi    }
60bcefe12eSTom Zanussi}
61bcefe12eSTom Zanussi
62bcefe12eSTom Zanussisub syscalls::sys_enter_write
63bcefe12eSTom Zanussi{
64bcefe12eSTom Zanussi    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
65*67439d55SMichael Petlan	$common_pid, $common_comm, $common_callchain,
66bcefe12eSTom Zanussi	$nr, $fd, $buf, $count) = @_;
67bcefe12eSTom Zanussi
68bcefe12eSTom Zanussi    $writes{$common_pid}{bytes_written} += $count;
69bcefe12eSTom Zanussi    $writes{$common_pid}{total_writes}++;
70bcefe12eSTom Zanussi    $writes{$common_pid}{comm} = $common_comm;
71bcefe12eSTom Zanussi}
72bcefe12eSTom Zanussi
73bcefe12eSTom Zanussisub trace_end
74bcefe12eSTom Zanussi{
75bcefe12eSTom Zanussi    printf("read counts by pid:\n\n");
76bcefe12eSTom Zanussi
77bcefe12eSTom Zanussi    printf("%6s  %20s  %10s  %10s  %10s\n", "pid", "comm",
78bcefe12eSTom Zanussi	   "# reads", "bytes_requested", "bytes_read");
79bcefe12eSTom Zanussi    printf("%6s  %-20s  %10s  %10s  %10s\n", "------", "--------------------",
80bcefe12eSTom Zanussi	   "-----------", "----------", "----------");
81bcefe12eSTom Zanussi
826922c3d7STom Zanussi    foreach my $pid (sort { ($reads{$b}{bytes_read} || 0) <=>
836922c3d7STom Zanussi				($reads{$a}{bytes_read} || 0) } keys %reads) {
846922c3d7STom Zanussi	my $comm = $reads{$pid}{comm} || "";
856922c3d7STom Zanussi	my $total_reads = $reads{$pid}{total_reads} || 0;
866922c3d7STom Zanussi	my $bytes_requested = $reads{$pid}{bytes_requested} || 0;
876922c3d7STom Zanussi	my $bytes_read = $reads{$pid}{bytes_read} || 0;
88bcefe12eSTom Zanussi
89bcefe12eSTom Zanussi	printf("%6s  %-20s  %10s  %10s  %10s\n", $pid, $comm,
90bcefe12eSTom Zanussi	       $total_reads, $bytes_requested, $bytes_read);
91bcefe12eSTom Zanussi    }
92bcefe12eSTom Zanussi
93bcefe12eSTom Zanussi    printf("\nfailed reads by pid:\n\n");
94bcefe12eSTom Zanussi
95bcefe12eSTom Zanussi    printf("%6s  %20s  %6s  %10s\n", "pid", "comm", "error #", "# errors");
96bcefe12eSTom Zanussi    printf("%6s  %20s  %6s  %10s\n", "------", "--------------------",
97bcefe12eSTom Zanussi	   "------", "----------");
98bcefe12eSTom Zanussi
996922c3d7STom Zanussi    my @errcounts = ();
100bcefe12eSTom Zanussi
1016922c3d7STom Zanussi    foreach my $pid (keys %reads) {
1026922c3d7STom Zanussi	foreach my $error (keys %{$reads{$pid}{errors}}) {
1036922c3d7STom Zanussi	    my $comm = $reads{$pid}{comm} || "";
1046922c3d7STom Zanussi	    my $errcount = $reads{$pid}{errors}{$error} || 0;
1056922c3d7STom Zanussi	    push @errcounts, [$pid, $comm, $error, $errcount];
106bcefe12eSTom Zanussi	}
107bcefe12eSTom Zanussi    }
108bcefe12eSTom Zanussi
1096922c3d7STom Zanussi    @errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;
1106922c3d7STom Zanussi
1116922c3d7STom Zanussi    for my $i (0 .. $#errcounts) {
1126922c3d7STom Zanussi	printf("%6d  %-20s  %6d  %10s\n", $errcounts[$i][0],
1136922c3d7STom Zanussi	       $errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);
1146922c3d7STom Zanussi    }
1156922c3d7STom Zanussi
116bcefe12eSTom Zanussi    printf("\nwrite counts by pid:\n\n");
117bcefe12eSTom Zanussi
118bcefe12eSTom Zanussi    printf("%6s  %20s  %10s  %10s\n", "pid", "comm",
119bcefe12eSTom Zanussi	   "# writes", "bytes_written");
120bcefe12eSTom Zanussi    printf("%6s  %-20s  %10s  %10s\n", "------", "--------------------",
121bcefe12eSTom Zanussi	   "-----------", "----------");
122bcefe12eSTom Zanussi
1236922c3d7STom Zanussi    foreach my $pid (sort { ($writes{$b}{bytes_written} || 0) <=>
1246922c3d7STom Zanussi			($writes{$a}{bytes_written} || 0)} keys %writes) {
1256922c3d7STom Zanussi	my $comm = $writes{$pid}{comm} || "";
1266922c3d7STom Zanussi	my $total_writes = $writes{$pid}{total_writes} || 0;
1276922c3d7STom Zanussi	my $bytes_written = $writes{$pid}{bytes_written} || 0;
128bcefe12eSTom Zanussi
129bcefe12eSTom Zanussi	printf("%6s  %-20s  %10s  %10s\n", $pid, $comm,
130bcefe12eSTom Zanussi	       $total_writes, $bytes_written);
131bcefe12eSTom Zanussi    }
132bcefe12eSTom Zanussi
133bcefe12eSTom Zanussi    printf("\nfailed writes by pid:\n\n");
134bcefe12eSTom Zanussi
135bcefe12eSTom Zanussi    printf("%6s  %20s  %6s  %10s\n", "pid", "comm", "error #", "# errors");
136bcefe12eSTom Zanussi    printf("%6s  %20s  %6s  %10s\n", "------", "--------------------",
137bcefe12eSTom Zanussi	   "------", "----------");
138bcefe12eSTom Zanussi
1396922c3d7STom Zanussi    @errcounts = ();
140bcefe12eSTom Zanussi
1416922c3d7STom Zanussi    foreach my $pid (keys %writes) {
1426922c3d7STom Zanussi	foreach my $error (keys %{$writes{$pid}{errors}}) {
1436922c3d7STom Zanussi	    my $comm = $writes{$pid}{comm} || "";
1446922c3d7STom Zanussi	    my $errcount = $writes{$pid}{errors}{$error} || 0;
1456922c3d7STom Zanussi	    push @errcounts, [$pid, $comm, $error, $errcount];
146bcefe12eSTom Zanussi	}
147bcefe12eSTom Zanussi    }
148bcefe12eSTom Zanussi
1496922c3d7STom Zanussi    @errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;
1506922c3d7STom Zanussi
1516922c3d7STom Zanussi    for my $i (0 .. $#errcounts) {
1526922c3d7STom Zanussi	printf("%6d  %-20s  %6d  %10s\n", $errcounts[$i][0],
1536922c3d7STom Zanussi	       $errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);
1546922c3d7STom Zanussi    }
1556922c3d7STom Zanussi
156bcefe12eSTom Zanussi    print_unhandled();
157bcefe12eSTom Zanussi}
158bcefe12eSTom Zanussi
159bcefe12eSTom Zanussimy %unhandled;
160bcefe12eSTom Zanussi
161bcefe12eSTom Zanussisub print_unhandled
162bcefe12eSTom Zanussi{
163bcefe12eSTom Zanussi    if ((scalar keys %unhandled) == 0) {
164bcefe12eSTom Zanussi	return;
165bcefe12eSTom Zanussi    }
166bcefe12eSTom Zanussi
167bcefe12eSTom Zanussi    print "\nunhandled events:\n\n";
168bcefe12eSTom Zanussi
169bcefe12eSTom Zanussi    printf("%-40s  %10s\n", "event", "count");
170bcefe12eSTom Zanussi    printf("%-40s  %10s\n", "----------------------------------------",
171bcefe12eSTom Zanussi	   "-----------");
172bcefe12eSTom Zanussi
173bcefe12eSTom Zanussi    foreach my $event_name (keys %unhandled) {
174bcefe12eSTom Zanussi	printf("%-40s  %10d\n", $event_name, $unhandled{$event_name});
175bcefe12eSTom Zanussi    }
176bcefe12eSTom Zanussi}
177bcefe12eSTom Zanussi
178bcefe12eSTom Zanussisub trace_unhandled
179bcefe12eSTom Zanussi{
180bcefe12eSTom Zanussi    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
181*67439d55SMichael Petlan	$common_pid, $common_comm, $common_callchain) = @_;
182bcefe12eSTom Zanussi
183bcefe12eSTom Zanussi    $unhandled{$event_name}++;
184bcefe12eSTom Zanussi}
185