#!/usr/bin/perl -w
# (c) 2009, Tom Zanussi <tzanussi@gmail.com>
# Licensed under the terms of the GNU GPL License version 2

# Display r/w activity for files read/written to for a given program

# The common_* event handler fields are the most useful fields common to
# all events.  They don't necessarily correspond to the 'common_*' fields
# in the status files.  Those fields not available as handler params can
# be retrieved via script functions of the form get_common_*().

use 5.010000;
use strict;
use warnings;

use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
use lib "./Perf-Trace-Util/lib";
use Perf::Trace::Core;
use Perf::Trace::Util;

my $usage = "perf script -s rw-by-file.pl <comm>\n";

my $for_comm = shift or die $usage;

my %reads;
my %writes;

sub syscalls::sys_enter_read
{
    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
	$common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;

    if ($common_comm eq $for_comm) {
	$reads{$fd}{bytes_requested} += $count;
	$reads{$fd}{total_reads}++;
    }
}

sub syscalls::sys_enter_write
{
    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
	$common_pid, $common_comm, $nr, $fd, $buf, $count) = @_;

    if ($common_comm eq $for_comm) {
	$writes{$fd}{bytes_written} += $count;
	$writes{$fd}{total_writes}++;
    }
}

sub trace_end
{
    printf("file read counts for $for_comm:\n\n");

    printf("%6s  %10s  %10s\n", "fd", "# reads", "bytes_requested");
    printf("%6s  %10s  %10s\n", "------", "----------", "-----------");

    foreach my $fd (sort {$reads{$b}{bytes_requested} <=>
			      $reads{$a}{bytes_requested}} keys %reads) {
	my $total_reads = $reads{$fd}{total_reads};
	my $bytes_requested = $reads{$fd}{bytes_requested};
	printf("%6u  %10u  %10u\n", $fd, $total_reads, $bytes_requested);
    }

    printf("\nfile write counts for $for_comm:\n\n");

    printf("%6s  %10s  %10s\n", "fd", "# writes", "bytes_written");
    printf("%6s  %10s  %10s\n", "------", "----------", "-----------");

    foreach my $fd (sort {$writes{$b}{bytes_written} <=>
			      $writes{$a}{bytes_written}} keys %writes) {
	my $total_writes = $writes{$fd}{total_writes};
	my $bytes_written = $writes{$fd}{bytes_written};
	printf("%6u  %10u  %10u\n", $fd, $total_writes, $bytes_written);
    }

    print_unhandled();
}

my %unhandled;

sub print_unhandled
{
    if ((scalar keys %unhandled) == 0) {
	return;
    }

    print "\nunhandled events:\n\n";

    printf("%-40s  %10s\n", "event", "count");
    printf("%-40s  %10s\n", "----------------------------------------",
	   "-----------");

    foreach my $event_name (keys %unhandled) {
	printf("%-40s  %10d\n", $event_name, $unhandled{$event_name});
    }
}

sub trace_unhandled
{
    my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
	$common_pid, $common_comm) = @_;

    $unhandled{$event_name}++;
}


/net/sched?id=83b5d1e3d3013dbf90645a5d07179d018c8243fa'>sched</a>/<a href='/cgit.cgi/linux/net-next.git/commit/net/sched/em_text.c?id=83b5d1e3d3013dbf90645a5d07179d018c8243fa'>em_text.c</a></div><div class='content'><div class='cgit-panel'><b>diff options</b><form method='get'><input type='hidden' name='id' value='83b5d1e3d3013dbf90645a5d07179d018c8243fa'/><table><tr><td colspan='2'/></tr><tr><td class='label'>context:</td><td class='ctrl'><select name='context' onchange='this.form.submit();'><option value='1'>1</option><option value='2'>2</option><option value='3' selected='selected'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><table summary='commit info' class='commit-info'>
<tr><th>author</th><td>Helge Deller &lt;deller@gmx.de&gt;</td><td class='right'>2017-01-03 22:55:50 +0100</td></tr>
<tr><th>committer</th><td>Helge Deller &lt;deller@gmx.de&gt;</td><td class='right'>2017-01-28 21:54:21 +0100</td></tr>
<tr><th>commit</th><td colspan='2' class='oid'><a href='/cgit.cgi/linux/net-next.git/commit/net/sched/em_text.c?id=83b5d1e3d3013dbf90645a5d07179d018c8243fa'>83b5d1e3d3013dbf90645a5d07179d018c8243fa</a> (<a href='/cgit.cgi/linux/net-next.git/patch/net/sched/em_text.c?id=83b5d1e3d3013dbf90645a5d07179d018c8243fa'>patch</a>)</td></tr>
<tr><th>tree</th><td colspan='2' class='oid'><a href='/cgit.cgi/linux/net-next.git/tree/?id=83b5d1e3d3013dbf90645a5d07179d018c8243fa'>831d7125df83a827408def371df72b1360e94078</a> /<a href='/cgit.cgi/linux/net-next.git/tree/net/sched/em_text.c?id=83b5d1e3d3013dbf90645a5d07179d018c8243fa'>net/sched/em_text.c</a></td></tr>
<tr><th>parent</th><td colspan='2' class='oid'><a href='/cgit.cgi/linux/net-next.git/commit/net/sched/em_text.c?id=d56a5ca366e785f463b4782f65daac883612a2b2'>d56a5ca366e785f463b4782f65daac883612a2b2</a> (<a href='/cgit.cgi/linux/net-next.git/diff/net/sched/em_text.c?id=83b5d1e3d3013dbf90645a5d07179d018c8243fa&amp;id2=d56a5ca366e785f463b4782f65daac883612a2b2'>diff</a>)</td></tr></table>
<div class='commit-subject'>parisc, parport_gsc: Fixes for printk continuation lines</div><div class='commit-msg'>Signed-off-by: Helge Deller &lt;deller@gmx.de&gt;
</div><div class='diffstat-header'><a href='/cgit.cgi/linux/net-next.git/diff/?id=83b5d1e3d3013dbf90645a5d07179d018c8243fa'>Diffstat</a> (limited to 'net/sched/em_text.c')</div><table summary='diffstat' class='diffstat'>