#!/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);

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

    my $proc = $procs{$pid};
    
    my $cmd = $proc->{cmd}->[$#{$proc->{cmd}}];
    
    my $pad = 12 - length($indent);
    printf "%s %-4d %-30s %${pad}s", $indent, $pid, $cmd, "";
    
    my $stats = $stats{$pid};

    printf " read %3d write %3d\t", $stats->[4], $stats->[5], "\t";
    
    if ($proc->{end}) {
	printf "run %2.2fs\n", (($proc->{end}-$proc->{start})/1000);
    } else {
	print "running...\n";
    }
    
    # Don't recurse in to init scripts
    if ($cmd =~ /^S/) {
	return;
    }

    
    foreach my $kid (sort {$a <=> $b } keys %{$proc->{kids}}) {
	# Show any child still running,
	# or an init script
	# or taking > 1 second
	if (!$procs{$kid}->{end} || 
	    $procs{$kid}->{cmd}->[$#{$procs{$kid}->{cmd}}] =~ /^S/ ||
	    ($procs{$kid}->{end} - $procs{$kid}->{start}) > 1000) {
	    &dump_initscripts($kid,$depth+1, "$indent  ");
	}
    }
}

&accumulate_stats(1);

&dump_initscripts(1, 1, "");

