#!/usr/bin/perl

use strict;
use warnings;

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

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

my $sort = 0;
if (defined $ARGV[1] && $ARGV[1] eq 'sort')
{
  $sort = 1;
}

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

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

# We'll write out the results to a CSV for easy graphing!
my $filename = 'processor-' . $polling_pid . '.csv';
open(FOUT, '>:utf8', $filename) or die('Error! Failed to open file for writing: ' . $filename);
print FOUT "Time,CPU0,CPU1,CPU2,CPU3,CPU4,CPU5,CPU6,CPU7\n";

my $starttime = 0;
my $timestamp = -1;
my $found_data = 0;
my $processor_data = {'cpu0'=>0.0,'cpu1'=>0.0,'cpu2'=>0.0,'cpu3'=>0.0,'cpu4'=>0.0,'cpu5'=>0.0,'cpu6'=>0.0,'cpu7'=>0.0};
my $line = '';
print 'Parsing log: ';
while ($line = <FIN>)
{
  # Look for a timestamp to begin a polling entry
  if ($line =~ /^(\d+):(\d+):(\d+)\s+(?:AM|PM)\s+0/)
  {
    # Write any existing data to file
    if ($found_data == 1)
    {
      &printRecord($timestamp, $processor_data);
      $found_data = 0;
    }
    if ($starttime == 0)
    {
      $starttime = $1 * 3600 + $2 * 60 + $3;
    }
    $timestamp = ($1 * 3600 + $2 * 60 + $3) - $starttime;
    print 'T';
    # Reset the data
    $processor_data = {'cpu0'=>0.0,'cpu1'=>0.0,'cpu2'=>0.0,'cpu3'=>0.0,'cpu4'=>0.0,'cpu5'=>0.0,'cpu6'=>0.0,'cpu7'=>0.0};
  }
  # We only process other lines if we are in an entry
  if ($timestamp >= 0 && $line =~ /^(?:\d+:\d+:\d+)\s+(?:AM|PM)\s+(\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+.*\r?\n$/)
  {
    my $cpu = $1;
    my $pusr = $2;
    my $pnice = $3;
    my $psys = $4;
    $processor_data->{'cpu' . $cpu} = $pusr + $pnice + $psys;
    $found_data = 1;
  }
}
print "\n";
# Write any remaining data to file
if ($found_data == 1)
{
  &printRecord($timestamp, $processor_data);
  $found_data = 0;
}

close(FOUT);
close(FIN);

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

# /**
#  */
sub printRecord
{
  my ($time_elapsed, $processor_data) = @_;
  print FOUT sprintf('% 5.1f', $time_elapsed) . ',';
  # Unsorted just prints out the actual CPU loads
  if (!$sort)
  {
    print FOUT sprintf('% 5.1f', $processor_data->{'cpu0'}) . ',';
    print FOUT sprintf('% 5.1f', $processor_data->{'cpu1'}) . ',';
    print FOUT sprintf('% 5.1f', $processor_data->{'cpu2'}) . ',';
    print FOUT sprintf('% 5.1f', $processor_data->{'cpu3'}) . ',';
    print FOUT sprintf('% 5.1f', $processor_data->{'cpu4'}) . ',';
    print FOUT sprintf('% 5.1f', $processor_data->{'cpu5'}) . ',';
    print FOUT sprintf('% 5.1f', $processor_data->{'cpu6'}) . ',';
    print FOUT sprintf('% 5.1f', $processor_data->{'cpu7'});
    print FOUT "\n";
  }
  else
  {
    my @values = (sprintf('% 5.1f', $processor_data->{'cpu0'}),
                  sprintf('% 5.1f', $processor_data->{'cpu1'}),
                  sprintf('% 5.1f', $processor_data->{'cpu2'}),
                  sprintf('% 5.1f', $processor_data->{'cpu3'}),
                  sprintf('% 5.1f', $processor_data->{'cpu4'}),
                  sprintf('% 5.1f', $processor_data->{'cpu5'}),
                  sprintf('% 5.1f', $processor_data->{'cpu6'}),
                  sprintf('% 5.1f', $processor_data->{'cpu7'})
                 );
    print FOUT join(',', sort {$b <=> $a} @values) . "\n";
  }
}
# /** printRecord() **/
