xref: /linux/tools/perf/scripts/perl/rw-by-file.pl (revision 4f19048fd0a0036e02443237952db5bfa5b5cdf0)
1bcefe12eSTom Zanussi#!/usr/bin/perl -w
2*4f19048fSThomas 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 files read/written to for a given program
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
21765532c8SArnaldo Carvalho de Melomy $usage = "perf script -s rw-by-file.pl <comm>\n";
22586bc5ccSTom Zanussi
23586bc5ccSTom Zanussimy $for_comm = shift or die $usage;
24bcefe12eSTom Zanussi
25bcefe12eSTom Zanussimy %reads;
26bcefe12eSTom Zanussimy %writes;
27bcefe12eSTom Zanussi
28bcefe12eSTom Zanussisub syscalls::sys_enter_read
29bcefe12eSTom Zanussi{
30bcefe12eSTom Zanussi    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
31bcefe12eSTom Zanussi	$common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;
32bcefe12eSTom Zanussi
33bcefe12eSTom Zanussi    if ($common_comm eq $for_comm) {
34bcefe12eSTom Zanussi	$reads{$fd}{bytes_requested} += $count;
35bcefe12eSTom Zanussi	$reads{$fd}{total_reads}++;
36bcefe12eSTom Zanussi    }
37bcefe12eSTom Zanussi}
38bcefe12eSTom Zanussi
39bcefe12eSTom Zanussisub syscalls::sys_enter_write
40bcefe12eSTom Zanussi{
41bcefe12eSTom Zanussi    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
42bcefe12eSTom Zanussi	$common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;
43bcefe12eSTom Zanussi
44bcefe12eSTom Zanussi    if ($common_comm eq $for_comm) {
45bcefe12eSTom Zanussi	$writes{$fd}{bytes_written} += $count;
46bcefe12eSTom Zanussi	$writes{$fd}{total_writes}++;
47bcefe12eSTom Zanussi    }
48bcefe12eSTom Zanussi}
49bcefe12eSTom Zanussi
50bcefe12eSTom Zanussisub trace_end
51bcefe12eSTom Zanussi{
52bcefe12eSTom Zanussi    printf("file read counts for $for_comm:\n\n");
53bcefe12eSTom Zanussi
54bcefe12eSTom Zanussi    printf("%6s  %10s  %10s\n", "fd", "# reads", "bytes_requested");
55bcefe12eSTom Zanussi    printf("%6s  %10s  %10s\n", "------", "----------", "-----------");
56bcefe12eSTom Zanussi
57bcefe12eSTom Zanussi    foreach my $fd (sort {$reads{$b}{bytes_requested} <=>
58bcefe12eSTom Zanussi			      $reads{$a}{bytes_requested}} keys %reads) {
59bcefe12eSTom Zanussi	my $total_reads = $reads{$fd}{total_reads};
60bcefe12eSTom Zanussi	my $bytes_requested = $reads{$fd}{bytes_requested};
61bcefe12eSTom Zanussi	printf("%6u  %10u  %10u\n", $fd, $total_reads, $bytes_requested);
62bcefe12eSTom Zanussi    }
63bcefe12eSTom Zanussi
64bcefe12eSTom Zanussi    printf("\nfile write counts for $for_comm:\n\n");
65bcefe12eSTom Zanussi
66bcefe12eSTom Zanussi    printf("%6s  %10s  %10s\n", "fd", "# writes", "bytes_written");
67bcefe12eSTom Zanussi    printf("%6s  %10s  %10s\n", "------", "----------", "-----------");
68bcefe12eSTom Zanussi
69bcefe12eSTom Zanussi    foreach my $fd (sort {$writes{$b}{bytes_written} <=>
70bcefe12eSTom Zanussi			      $writes{$a}{bytes_written}} keys %writes) {
71bcefe12eSTom Zanussi	my $total_writes = $writes{$fd}{total_writes};
72bcefe12eSTom Zanussi	my $bytes_written = $writes{$fd}{bytes_written};
73bcefe12eSTom Zanussi	printf("%6u  %10u  %10u\n", $fd, $total_writes, $bytes_written);
74bcefe12eSTom Zanussi    }
75bcefe12eSTom Zanussi
76bcefe12eSTom Zanussi    print_unhandled();
77bcefe12eSTom Zanussi}
78bcefe12eSTom Zanussi
79bcefe12eSTom Zanussimy %unhandled;
80bcefe12eSTom Zanussi
81bcefe12eSTom Zanussisub print_unhandled
82bcefe12eSTom Zanussi{
83bcefe12eSTom Zanussi    if ((scalar keys %unhandled) == 0) {
84bcefe12eSTom Zanussi	return;
85bcefe12eSTom Zanussi    }
86bcefe12eSTom Zanussi
87bcefe12eSTom Zanussi    print "\nunhandled events:\n\n";
88bcefe12eSTom Zanussi
89bcefe12eSTom Zanussi    printf("%-40s  %10s\n", "event", "count");
90bcefe12eSTom Zanussi    printf("%-40s  %10s\n", "----------------------------------------",
91bcefe12eSTom Zanussi	   "-----------");
92bcefe12eSTom Zanussi
93bcefe12eSTom Zanussi    foreach my $event_name (keys %unhandled) {
94bcefe12eSTom Zanussi	printf("%-40s  %10d\n", $event_name, $unhandled{$event_name});
95bcefe12eSTom Zanussi    }
96bcefe12eSTom Zanussi}
97bcefe12eSTom Zanussi
98bcefe12eSTom Zanussisub trace_unhandled
99bcefe12eSTom Zanussi{
100bcefe12eSTom Zanussi    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
101bcefe12eSTom Zanussi	$common_pid, $common_comm) = @_;
102bcefe12eSTom Zanussi
103bcefe12eSTom Zanussi    $unhandled{$event_name}++;
104bcefe12eSTom Zanussi}
105bcefe12eSTom Zanussi
106bcefe12eSTom Zanussi
107