xref: /linux/tools/perf/scripts/perl/rw-by-file.pl (revision b5c00a3a412857d6f07970984068c450429e051c)
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 files read/written to for a given program
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
12use 5.010000;
13use strict;
14use warnings;
15
16use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
17use lib "./Perf-Trace-Util/lib";
18use Perf::Trace::Core;
19use Perf::Trace::Util;
20
21# change this to the comm of the program you're interested in
22my $for_comm = "perf";
23
24my %reads;
25my %writes;
26
27sub syscalls::sys_enter_read
28{
29    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
30	$common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;
31
32    if ($common_comm eq $for_comm) {
33	$reads{$fd}{bytes_requested} += $count;
34	$reads{$fd}{total_reads}++;
35    }
36}
37
38sub syscalls::sys_enter_write
39{
40    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
41	$common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;
42
43    if ($common_comm eq $for_comm) {
44	$writes{$fd}{bytes_written} += $count;
45	$writes{$fd}{total_writes}++;
46    }
47}
48
49sub trace_end
50{
51    printf("file read counts for $for_comm:\n\n");
52
53    printf("%6s  %10s  %10s\n", "fd", "# reads", "bytes_requested");
54    printf("%6s  %10s  %10s\n", "------", "----------", "-----------");
55
56    foreach my $fd (sort {$reads{$b}{bytes_requested} <=>
57			      $reads{$a}{bytes_requested}} keys %reads) {
58	my $total_reads = $reads{$fd}{total_reads};
59	my $bytes_requested = $reads{$fd}{bytes_requested};
60	printf("%6u  %10u  %10u\n", $fd, $total_reads, $bytes_requested);
61    }
62
63    printf("\nfile write counts for $for_comm:\n\n");
64
65    printf("%6s  %10s  %10s\n", "fd", "# writes", "bytes_written");
66    printf("%6s  %10s  %10s\n", "------", "----------", "-----------");
67
68    foreach my $fd (sort {$writes{$b}{bytes_written} <=>
69			      $writes{$a}{bytes_written}} keys %writes) {
70	my $total_writes = $writes{$fd}{total_writes};
71	my $bytes_written = $writes{$fd}{bytes_written};
72	printf("%6u  %10u  %10u\n", $fd, $total_writes, $bytes_written);
73    }
74
75    print_unhandled();
76}
77
78my %unhandled;
79
80sub print_unhandled
81{
82    if ((scalar keys %unhandled) == 0) {
83	return;
84    }
85
86    print "\nunhandled events:\n\n";
87
88    printf("%-40s  %10s\n", "event", "count");
89    printf("%-40s  %10s\n", "----------------------------------------",
90	   "-----------");
91
92    foreach my $event_name (keys %unhandled) {
93	printf("%-40s  %10d\n", $event_name, $unhandled{$event_name});
94    }
95}
96
97sub trace_unhandled
98{
99    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
100	$common_pid, $common_comm) = @_;
101
102    $unhandled{$event_name}++;
103}
104
105
106