#!/usr/bin/perl

use warnings;
use strict;

use File::Spec::Functions qw(splitpath);

my %procs;
my %cmds;
my %stats;

do "parse-data.pl";

&parse_data(\%procs, \%cmds);
&accumulate_stats(\%procs, \%stats, 1);


my $proc = shift @ARGV;

my @cmd = sort { $a <=> $b } keys %{$cmds{$proc}};
my $maxdepth = @ARGV ? shift @ARGV : 5;

print "Start $proc $cmd[0]\n";

&dump_procs($cmd[0], 0, "");

sub dump_procs {
    my $pid = shift;
    my $depth = shift;
    my $indent = shift;

    my $proc = $procs{$pid};
    my $cmd = $proc->{cmd}->[$#{$proc->{cmd}}];
    
    my $time = $proc->{end} ? "run ".  ($proc->{end}-$proc->{start}) . "ms" : "running";

    print "$indent $pid $cmd $time\n";

    if ($depth < $maxdepth)  {
	foreach my $kid (sort { $a <=> $b } keys %{$proc->{kids}}) {
	    &dump_procs($kid, $depth+1, "$indent  ");
	}
    }
}
