#!/usr/bin/perl
#
# Copyright by The HDF Group.
# Copyright by the Board of Trustees of the University of Illinois.
# All rights reserved.
#
# This file is part of HDF5.  The full HDF5 copyright notice, including
# terms governing use, modification, and redistribution, is contained in
# the COPYING file, which can be found at the root of the source code
# distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.
# If you do not have access to either file, you may request a copy from
# help@hdfgroup.org.
#
# Usage: pipe the output of Linux's `strace' program into the stdin of
# this command, and the output of this command into gnuplot.
my ($fast,$npasses);
if ($ARGV[0] =~ /^--?fast$/) {
  $fast = 1;
  shift;
}
my $filename = shift || "tstab2.h5";
my $total = 0;
my %What;           # What{pos}{nbytes}{r|w} = naccesses
my($total_writes, $total_bytes_out, $total_reads, $total_bytes_in);
while (<>) {
  if (!defined $fd) {
    if (/^open\("(.*?)".*=\s+(\d+)/ && $1 eq $filename) {
      $fd = $2;
      $pos = 0;
    }
  } elsif (/^close\((\d+)/ && $1==$fd) {
    $fd = undef;
  } elsif (!$fast &&
       /^lseek\((\d+), -?\d+,.*= (\d+)/ &&
       $1==$fd && $2>=0) {
    $pos = $2;
  } elsif (!$fast && /^lseek\((\d+),/ && $1==$fd) {
    die $_;
  } elsif (/^write\((\d+), ".*?"(\.\.\.)?, \d+\)\s*= (\d+)/ &&
       $1==$fd && $3>=0) {
    my $nbytes = $3;
    if ($fast) {
      $total_writes++;
      $total_bytes_out += $nbytes;
    } else {
      $What{$pos}{$nbytes}{w}++;
      printf "%d %d\n", $total, $pos;