#!/usr/bin/perl

my %vfscache;

sub GetVFSName {
    my ($vfsid) = @_;

    if (!exists($vfscache{$vfsid})){
	open(VF, "vldbTest localhost rec_lookup -vfsid $vfsid|");
	while (<VF>){
	    next unless /^RES/ && /vfsname\s+\'([^\']+)\'/;
	    $vfscache{$vfsid} = $1;
	}
	close(VF);
    }

    $vfscache{$vfsid};
}

while (1){
    my $printed = 0;
    my $lines = 0;
    my %hist;
    sub ByValue {
	return $hist{$a} <=> $hist{$b};
    }

    open(IF, "/proc/spin/stream.d/channels");
    while (<IF>){
	$lines++;
	my ($chan, $u0, $u1, $state, $vfs, $addr, $wait) = split;
	if ($wait > 1){
	    my $dvfsid = hex("0x".$vfs);
	    print GetVFSName($dvfsid) . "($dvfsid) - ";
	    $hist{$dvfsid}++;
	    print;
	    $printed++;
	}
    }
    close(IF);
    if (keys %hist){
	foreach my $key (reverse sort ByValue keys %hist){
	    print "  " . GetVFSName($key) . ":$hist{$key}";
	}
	print "\n";
    }
    print "========== " . (scalar localtime) . " - $printed/$lines ==========\n" if $printed;
    sleep 1;
}
__END__
1937 1984 1335 WaitD 000614fc cfbc0700      0.0
1947 2007 1337 WaitD 0000076b cfba55e0      0.0
