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