#!/usr/bin/perl
# This script is designed to start & stop pbench tools while monitoring the
# output of a benchmark.
#
# For many of the benchmarks we use, we simply start the tools before the
# benchmark, then run the benchmark, then stop the tools.  For some
# benchmarks, this is not optimal, because the benchmark must do work before
# or after the measurement period, and tool's average results are then not
# quite accurate.  There are also other benchmarks which have multiple
# measurment periods, which may or may not correlate to different test
# iterations (SPECjbb is a good example, where there are several warehouse
# counts measured, each one of these correspnding to a pbench test iteration).
#
# This script registers the user's triggers for starting and stopping the
# registered tools.  If the trigger for starting a tool is encountered more
# than once, then the iteration is incremented, so the tool's results for each
# trigger are easily managed.
#
# The tools are given a unique results directory for each iteration by using
# the pattern "$benchmark_run_dir/${iteration}-default/sample1".

my $script="pbench-tool-trigger";

# turn on autoflushing
$| = 1;

my $iteration = $ARGV[0]; # which iteration number to start with (required)
my $benchmark_run_dir = $ARGV[1];
my $tool_group = $ARGV[2];
my $sample_num = 1;
if (scalar @ARGV == 4) {
	$sample_num = $ARGV[3];
}

my $start_t = "";
my $started = 0;
my $stop_t = "";

my $pbrun = $ENV{'pbench_run'};
if (! -d $pbrun) {
	die "the pbench run directory, [$pbrun], does not exist";
}
my $t_file =  $pbrun . "/tools-v1-" . $tool_group . "/__trigger__";

open(TRIG, "<$t_file") || die "Could not open $t_file\n";
while (<TRIG>) {
	if (/^#/) {
		next;
	}
	chomp;
	($start, $stop) = split(/:/);
	if ($start eq "" || $stop eq "") {
		die "Empty triggers found, start-trigger:\"\", stop-trigger:\"\"";
	}
	$start_t = $start;
	$stop_t = $stop;
}
close(TRIG);

print "[$script] starting trigger processing of STDIN using tool group $tool_group triggers at $t_file\n";
printf "[$script] start-trigger:\"$start_t\" stop-trigger:\"$stop_t\"\n";

# Initial reference result directory for tools
my $benchmark_results_dir = $benchmark_run_dir . '/' . $iteration . '-default/sample' . $sample_num;

while (<STDIN>) {
	# Echo the line we read
	printf;

	# Look for triggers
	if ((/$stop_t/) && $started) {
		$cmd="pbench-stop-tools --group=$tool_group --dir=$benchmark_results_dir\n";
		print "[$script]$cmd";
		system("$cmd");
		$started = 0;
		# Increment the iteration and adjust the benchmark results
		# directory now that this iteration is done.
		$iteration++;
		$benchmark_results_dir = $benchmark_run_dir . '/' . $iteration . '-default/sample' . $sample_num;
	}
	if ((/$start_t/) && !$started) {
		$cmd="pbench-start-tools --group=$tool_group --dir=$benchmark_results_dir\n";
		print "[$script]$cmd";
		system("$cmd");
		$started = 1;
	}
}

if ($started) {
	$cmd="pbench-stop-tools --group=$tool_group --dir=$benchmark_results_dir\n";
	print "[$script]$cmd";
	system("$cmd");
}
