#!/bin/bash
#
# Usage:
#  process_plot_taskthreadpools nprocess time-range-ms
#
# Description:
#  Process all the tasks thread and threadpool info in the current directory
#  creating graphs for steps and threads.
#
#  The input files are created by a run using the "-y interval" and 
#  "-Y interval" flags, with the same interval. The output files
#  from these stages will be named "thread_info-step<n>.dat" 
#  and "threadpool_info-step<n>.dat" in the current directory.
#  All located files will be processed using "nprocess" concurrent
#  processes and all plots will have the given time range. An output
#  HTML file "threadtaskpools.html" will be created to view all the plots.
#
# This file is part of SWIFT:
#
#  Copyright (C) 2017 Peter W. Draper (p.w.draper@durham.ac.uk)
#  All Rights Reserved.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

#  Handle command-line
if test "$2" == ""; then
    echo "Usage: $0 nprocess time-range-ms"
    exit 1
fi
NPROCS=$1
TIMERANGE=$2

#  Locate script.
SCRIPTHOME=$(dirname "$0")

#  Find all thread info files. Use version sort to get into correct order.
taskfiles=$(ls -v thread_info-step*.dat)
if test $? != 0; then
    echo "Failed to find any thread info files"
    exit 1
fi

#  Construct the expected names of the threadpool files and merge.
list=""
for f in $taskfiles; do
    s=$(echo $f| sed 's,thread_info-step\(.*\).dat,\1,')
    p="threadpool_info-step${s}.dat"
    if ! test -f "$p"; then
        exit "Cannot find threadpool file: $p, that matches $f"
    fi
    list="$list $f $p $s"
done

echo "Processing thread info files..."
echo $list | xargs -P $NPROCS -n 3 /bin/bash -c "${SCRIPTHOME}/process_plot_taskthreadpools_helper $TIMERANGE \$0 \$1 \$2 "

output="taskpool.html"
echo "Writing output $output file"
#  Construct document - serial.
cat <<EOF > $output
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>SWIFT task and threadpool graphs</title>
  </head>
  <body>
  <h1>SWIFT task and threadpool graphs</h1>
EOF

echo $list | xargs -n 3 | while read f p s; do
    cat <<EOF >> $output
<h2>Step $s</h2>
EOF
    cat <<EOF >> $output
<a href="taskpoolstep${s}r${i}.html"><img src="taskstep${s}r${i}.png" width=400px/></a>
<a href="taskpoolstep${s}r${i}.html"><img src="poolstep${s}r${i}.png" width=400px/></a>
EOF
    cat <<EOF > taskpoolstep${s}r${i}.html
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<img src="taskstep${s}r${i}.png">
<img src="poolstep${s}r${i}.png">
</body>
</html>
EOF

done

cat <<EOF >> $output
  </body>
</html>
EOF

echo "Finished"

exit
