#!/usr/bin/perl

use strict;
use warnings;

print '===== Analyze poll-gsdl.pl Output =====' . "\n";

if (!defined($ARGV[0]) || !-f 'memory-' . $ARGV[0] . '.log')
{
  print 'Error! Memory report not specified or file not found.' . "\n";
  print 'Usage: poll-report.pl <pid>' . "\n\n";
  exit(0);
}

my $polling_pid = 0;
if ($ARGV[0] =~ /^(\d+)$/)
{
  $polling_pid = $1;
}
else
{
  print 'Error! Memory report filename not valid.' . "\n";
  print 'Usage: memreport.pl <pid>' . "\n\n";
  exit(0);
}

open(FIN, '<:utf8', 'memory-' . $polling_pid . '.log') or die('Error! Failed to open file for reading: memory-' . $ARGV[0] . '.log');

# We'll write out the results to a CSV for easy graphing!
my $filename = 'memory-' . $polling_pid . '.csv';
open(FOUT, '>:utf8', $filename) or die('Error! Failed to open file for writing: ' . $filename);
print FOUT "Time,Type,CPU,PCPU,PMEM,Type,CPU,PCPU,PMEM\n";

# We'll store the details for both interesting child processes of Greenstone
# importing, namely import.pl and txt2tdb.
my $starttime = 0;
my $timestamp = -1;
my $import_data = {'cpu'=>-1,'pcpu'=>0.0, 'pmem'=>0.0};
my $tdb_data = {'cpu'=>-1,'pcpu'=>0.0, 'pmem'=>0.0};
my $line = '';
print 'Parsing log: ';
while ($line = <FIN>)
{
  # Look for a timestamp to begin a polling entry
  if ($line =~ /\[(\d+.\d+)\]/)
  {
    # Write any existing data to file
    if ($import_data->{'cpu'} > -1)
    {
      &printRecord($timestamp, $import_data, $tdb_data);
    }
    if ($starttime == 0)
    {
      $starttime = $1;
    }
    $timestamp = $1 - $starttime;
    print 'T';
    # Reset the data
    $import_data = {'cpu'=>-1,'pcpu'=>0.0, 'pmem'=>0.0};
    $tdb_data = {'cpu'=>-1,'pcpu'=>0.0, 'pmem'=>0.0};
  }
  # We only process other lines if we are in an entry
  # - lines of the form: <pid> <cpu> <%cpu> <%mem> <cmd>
  elsif ($timestamp >= 0 && $line =~ /^\s*(\d+)\s+(\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(.*)\r?\n$/)
  {
    my $pid = $1;
    my $cpu = $2;
    my $pcpu = $3;
    my $pmem = $4;
    my $cmd = $5;

    # There are two processes we are interested in:
    # 1. The import process itself
    if ($cmd =~ /^\/usr\/bin\/perl\s+.*import\.pl.+/)
    {
      print '.';
      $import_data->{'cpu'} = $cpu;
      $import_data->{'pcpu'} += $pcpu;
      $import_data->{'pmem'} += $pmem;
    }
    # 2. The TDB process
    elsif ($cmd =~ /^txt2tdb/)
    {
      print '.';
      $tdb_data->{'cpu'} = $cpu;
      $tdb_data->{'pcpu'} += $pcpu;
      $tdb_data->{'pmem'} += $pmem;
    }
  }
}
print "\n";
# Write any remaining data to file
if ($import_data->{'cpu'} > -1)
{
  &printRecord($timestamp, $import_data, $tdb_data);
}

close(FOUT);
close(FIN);

print '===== Complete =====' . "\n";
exit(0);

# /**
#  */
sub printRecord
{
  my ($timestamp, $import_data, $tdb_data) = @_;
  print FOUT sprintf('% 5.1f', $timestamp) . ',';
  print FOUT 'IMPORT,';
  print FOUT sprintf('% 2d', $import_data->{'cpu'}) . ',';
  print FOUT sprintf('% 5.1f', $import_data->{'pcpu'}) . ',';
  print FOUT sprintf('% 5.1f', $import_data->{'pmem'}) . ',';
  print FOUT 'TDB,';
  print FOUT sprintf('% 2d', $tdb_data->{'cpu'}) . ',';
  print FOUT sprintf('% 5.1f', $tdb_data->{'pcpu'}) . ',';
  print FOUT sprintf('% 5.1f', $tdb_data->{'pmem'}) . "\n";

}
# /** printRecord() **/
