#!/bin/bash
#
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/scripts/Create-ParaSim
# ROCK Linux is Copyright (C) 1998 - 2004 Clifford Wolf
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---

config=default
jobs="" ; x11=0
 
while [ "$1" ] ; do
        case "$1" in
                -cfg)   config=$2 ; shift ; shift ;;
                -jobs)  jobs=$2   ; shift ; shift ;;
                -x11)   x11=1     ; shift ;;
                *)      echo "Usage: $0 [ -cfg <config> ] [ -x11 ]" \
			     "[ -jobs <N>[,<N>[,...]]]"
			echo
			echo " \"Simulate\" a parallel build and output a graph showing how many parallel"
			echo " jobs are available for building in which phase of the build."
			echo
			echo "	-cfg <config>		the configuration to use"
			echo "	-x11			graph the results with 'gnuplot'"
			echo "	-jobs <N>[,<N>[,...]]]  compare the reference build to cluster builds"
			echo "				with N nodes"
			echo
			echo " For details, see Documentation/BUILD-CLUSTER."
			exit 1 ;;
        esac
done    

. scripts/parse-config

dir=src.parasim.$(cksum config/$config/packages | cut -f1 -d' ')
echo "Building parasim in $dir."

if [ "$jobs" ] ; then
	maxjobs=0
	for x in $( echo $jobs | tr , ' ' ) ; do
		[ $maxjobs -lt $x ] && maxjobs=$x
		if [ ! -f "$dir/parasim_$x.dat" ] ; then
			rm -rf $dir/logs_$x
			perl scripts/parasim2.pl $dir $config $x
		else
			echo "Using cached data in $dir/parasim_$x.dat."
		fi
	done
	{
		jobstotal=$( echo `wc -l < $dir/parasim_$maxjobs.dat` )
		echo 'set data style lines'
		echo 'set title "ROCK Linux Parallel Build' \
				'Simulation ('$jobstotal' Jobs Total)"'
		echo 'set xlabel "Hours (on reference hardware)"'
		echo 'set ylabel "Parallel Jobs"'
		echo "plot [0:*] [0:$(( $maxjobs + 1 ))] \\"
		next=""
		for x in $( echo $jobs | tr , ' ' ) ; do
			tm=$( tail -n 1 $dir/parasim_$x.dat | cut -f1 | \
				awk -F. '{ printf("%02d:%02d\n",
						$1, 60*("0." $2)); }' )
			echo -en "$next" ; next=', \\\n'
			echo -n "  \"parasim_$x.dat\" title \"Build with" \
				"$x parallel jobs ($tm)\" with steps lw 3"
		done
	} > $dir/parasim_$jobs.gnuplot

	cmd="perl scripts/parasim3.pl"
	for x in $( echo $jobs | tr , ' ' )
	do cmd="$cmd $dir/parasim_$x.dat" ; done
	eval "$cmd" > $dir/parasim_$jobs.txt

	if [ "$x11" = 1 ] ; then
		cd $dir ; gnuplot -persist parasim_$jobs.gnuplot
	else
		cat $dir/parasim_$jobs.txt
	fi
	exit
fi

mkdir -p $dir/logs

if [ ! -f $dir/parasim.dat ] ; then
    echo -n "Simulation running ..."
    rm -rf $dir/logs $dir/parasim.new
    mkdir -p $dir/logs
    while
	./scripts/Create-PkgQueue -cfg $config \
			-logdir $dir/logs | sort -r -n -k2 > $dir/queue.txt
	[ -s $dir/queue.txt ]
    do
	next="`head -n 1 $dir/queue.txt`" ; set $next
	qid="$1-$6" ; touch $dir/logs/$qid.log

	printf "%5d %5d %-30s %s\n" $(wc -l < $dir/queue.txt) \
	       $2 $qid "$( cut -f1,7 -d' ' $dir/queue.txt | \
	                   tail -n +2 | tr '\n ' ' -' )" >> $dir/parasim.new

	echo -n .
    done
    mv $dir/parasim.new $dir/parasim.dat
    echo
else
    echo "Using cached data in $dir/parasim.dat."
fi

jobstotal=$( echo `wc -l < $dir/parasim.dat` )
cat > $dir/parasim.gnuplot <<- EOT
	set title "ROCK Linux Parallel Build Simulation ($jobstotal Jobs Total)"
	set xlabel "Number of Jobs build so far"
	set ylabel "Possible Parallel Jobs"
	plot 'parasim.dat' using 1 title "Parallel Jobs" with steps lw 3
EOT

perl scripts/parasim1.pl $dir/parasim.dat 0 > $dir/parasim.txt

if [ "$x11" = 1 ] ; then
	cd $dir ; gnuplot -persist parasim.gnuplot
else
	cat $dir/parasim.txt
fi