root/gs2-extensions/parallel-building/trunk/src/bin/script/generate_gantt.pl @ 27683

Revision 27683, 16.1 KB (checked in by jmt12, 6 years ago)

moving a few more headings around to help with information block layout

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2
3# Pragma
4use strict;
5use warnings;
6use 5.012; # so readdir assigns to $_ in a lone while test
7
8# Modules
9use Sort::Naturally;
10use POSIX qw(floor strftime);
11
12print "\n===== Generate Timing (GANTT) =====\n";
13
14# 0. Init
15# - configurables
16my $chart_width = 1600;
17my $debug = 0;
18# - globals
19my $chart_count = 0;
20my @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
21
22# 1. Parse options
23while (defined $ARGV[0] && $ARGV[0] =~ /^-/)
24{
25  my $option = shift(@ARGV);
26  if ($option eq '-debug')
27  {
28    $debug = 1;
29  }
30  elsif ($option eq '-width')
31  {
32    if (!defined $ARGV[0])
33    {
34      &printUsage('Error! No width value specified');
35    }
36    my $value = shift(@ARGV);
37    if ($value !~ /^\d+$/)
38    {
39      &printUsage('Error! Chart width not a number');
40    }
41    $chart_width = $value;
42  }
43  else
44  {
45    &printUsage('Error! Unknown option: ' . $option);
46  }
47}
48print "Chart Width: " . $chart_width . "px\n";
49print "Debug? " . ($debug ? 'Yes' : 'No') . "\n";
50print "===================================\n\n";
51
52# 2. Search for valid directories (containing timing.csv)
53while (defined $ARGV[0])
54{
55  my $dir = shift(@ARGV);
56  if (!-d $dir)
57  {
58    &printUsage('Error! Not a directory: ' . $dir);
59  }
60  &searchForTimingCSV($dir);
61}
62
63# 3. Done
64print "Complete!\n\n";
65print "===================================\n";
66print 'Generated ' . $chart_count . " charts\n";
67print "===================================\n\n";
68exit;
69## main() ##
70
71
72## @function searchForTimingCSV()
73#
74sub searchForTimingCSV
75{
76  my $dir = shift(@_);
77  # For every directory where we find a timing.csv we generate a gantt chart
78  my $timing_path = &filenameCat($dir, 'timing.csv');
79  if (-e $timing_path)
80  {
81    &generateChart($dir, $timing_path);
82  }
83  # We also recursively search for other directories containing timing.csv's
84  opendir(my $dh, $dir) or &printError('Failed to open directory for reading: ' . $dir);
85  while (readdir($dh))
86  {
87    my $file = $_;
88    if ($file !~ /^\./)
89    {
90      my $path = &filenameCat($dir, $file);
91      if (-d $path)
92      {
93        &searchForTimingCSV($path);
94      }
95    }
96  }
97}
98## searchForTimingCSV() ##
99
100
101## @function generateChart()
102#
103sub generateChart
104{
105  my $dir = shift(@_);
106  my $timing_csv_path = shift(@_);
107  my $import_dir;
108  my ($epoc) = $dir =~ /(\d+)$/;
109  my $gantt_path = $dir . '/' . $epoc . '-gantt.html';
110
111  print ' * Generating chart for: ' . $dir . "\n";
112  print ' - timing file: ' . $timing_csv_path . "\n";
113  print ' - gantt chart: ' . $gantt_path . "\n";
114
115  # Read in timing.csv and parse information into data structure
116  print ' - parsing timing.csv... ';
117  my $timing_data = {};
118  my $id_2_worker_id = {};
119  if (open(TIN, '<:utf8', $timing_csv_path))
120  {
121    my $line;
122    while ($line = <TIN>)
123    {
124      my @parts = split(/,/, $line);
125      if ($parts[1] eq 'M0')
126      {
127        $timing_data->{'M'} = {'N'=>$parts[2], 'S'=>$parts[3], 'E'=>$parts[4]};
128      }
129      elsif ($parts[1] =~ /W\d+/)
130      {
131        my $worker_id = $parts[1];
132        my $hostname = $parts[2];
133        # Alter the worker name for compute nodes so they can be naturally
134        # sorted
135        if ($hostname =~ /compute-0-(\d+)/)
136        {
137          $worker_id = 'W' . $1;
138        }
139        $timing_data->{$worker_id} = {'N'=>$hostname, 'S'=>$parts[3], 'E'=>$parts[4], 'F'=>{}};
140        $id_2_worker_id->{$parts[0]} = $worker_id;
141      }
142      elsif ($parts[1] =~ /T\d+/)
143      {
144        my $worker_id = $id_2_worker_id->{$parts[7]};
145        my $stop = $parts[4];
146        my $filepath = $parts[8];
147        $filepath =~ s/^\s+|\s+$//g;
148        $import_dir = &longestCommonPath($filepath, $import_dir);
149        $timing_data->{$worker_id}->{'F'}->{$parts[3]} = {'FN'=>$filepath, 'S'=>$parts[3], 'PS'=>($stop - $parts[5]), 'PE'=>$stop, 'E'=>$stop, 'DL'=>$parts[6]};
150      }
151    }
152    close(TIN);
153  }
154  else
155  {
156    die('Error! Failed to open file for reading: ' . $timing_csv_path);
157  }
158  my $number_of_workers = scalar(keys(%{$id_2_worker_id}));;
159  print "Done\n";
160
161  # 3. Produce pretty HTML chart of timing information including jobs
162  print " - generating timing information as chart in HTML... ";
163  open(HTMLOUT, '>:utf8', $gantt_path) or die('Error! Failed to open file for writing: gantt.html');
164  print HTMLOUT "<html>\n";
165  print HTMLOUT '<head>' . "\n";
166  print HTMLOUT '<style type="text/css">' . "\n";
167  print HTMLOUT 'div.thread {position:relative}' . "\n";
168  print HTMLOUT 'div.master {border:1px solid gray;color:white;font-weight:bold}' . "\n";
169  print HTMLOUT 'div.worker {border:1px solid black;background-color:green;color:white;font-weight:bold;margin-bottom:1px;}' . "\n";
170  print HTMLOUT 'div.time {font-size:smaller;font-weight:normal}' . "\n";
171  print HTMLOUT 'div.job {background-color:transparent;color:black;border:1px solid black;display:block;font-size:smaller;position:relative;text-align:center;overflow:hidden;margin-bottom:1px;}' . "\n";
172  print HTMLOUT 'span.process {z-index:-1;background-color:#C7C7C7;position:absolute}' . "\n";
173  print HTMLOUT 'span.label {z-index:1;background-color:transparent;overflow:hidden;white-space:nowrap;}' . "\n";
174  print HTMLOUT "th {text-align:left}\n";
175  print HTMLOUT '</style>' . "\n";
176  print HTMLOUT '</head>' . "\n";
177  print HTMLOUT "<body>\n";
178  print HTMLOUT "<h2>Statistics</h2>\n";
179  print HTMLOUT "<table style=\"width:100%;\">\n";
180
181  my $total_duration = $timing_data->{'M'}->{'E'} - $timing_data->{'M'}->{'S'};
182  my $file_count = 0;
183  my $data_locality = 0;
184  my $total_io_time = 0;
185  my $total_process_time = 0;
186  my $fastest_file = 0;
187  my $slowest_file = 0;
188  my $problem_files = 0;
189  foreach my $worker_id (keys %{$timing_data})
190  {
191    if ($worker_id ne 'M')
192    {
193      foreach my $job_start ( keys %{$timing_data->{$worker_id}->{'F'}} )
194      {
195        my $process_start = $timing_data->{$worker_id}->{'F'}->{$job_start}->{'PS'};
196        my $process_end = $timing_data->{$worker_id}->{'F'}->{$job_start}->{'PE'};
197        my $job_end = $timing_data->{$worker_id}->{'F'}->{$job_start}->{'E'};
198        if ($process_start == 0 || $process_end == 0 || $job_end == 0)
199        {
200          $problem_files++;
201        }
202        else
203        {
204          my $io_duration = ($process_start - $job_start) + ($job_end - $process_end);
205          my $process_duration = $process_end - $process_start;
206          my $total_duration = $io_duration + $process_duration;
207          &debugPrint("filename: " . $timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'});
208          &debugPrint("start: $job_start ps: $process_start pe: $process_end end: $job_end");
209          &debugPrint("io: $io_duration process: $process_duration duration: $total_duration");
210          # Running stats
211          $total_io_time += $io_duration;
212          $total_process_time += $process_duration;
213          if ($fastest_file == 0 || $total_duration < $fastest_file)
214          {
215            $fastest_file = $total_duration;
216          }
217          if ($slowest_file == 0 || $total_duration > $slowest_file)
218          {
219            $slowest_file = $total_duration;
220          }
221        }
222        # Shorten filename
223        if (defined $timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'})
224        {
225          $timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'} = substr($timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'}, length($import_dir) + 1);
226        }
227        $file_count++;
228        if ($timing_data->{$worker_id}->{'F'}->{$job_start}->{'DL'} == 1)
229        {
230          $data_locality++;
231        }
232      }
233    }
234  }
235  my $avg_processing_time = floor(($total_io_time + $total_process_time) / $file_count);
236  my $avg_io_time = int(($total_io_time / $file_count) + 0.5);
237  my $avg_cpu_time = int(($total_process_time / $file_count) + 0.5);
238
239  print HTMLOUT "<tr>\n";
240  print HTMLOUT ' <th style="width:12%;">Import Directory:</th><td style="width:22%;" colspan="5">' . $import_dir . "</td>\n";
241  print HTMLOUT "</tr>\n";
242
243  print HTMLOUT "<tr>\n";
244  my ($sec, $min, $hour, $day, $month, $year) = (localtime($timing_data->{'M'}->{'S'}))[0,1,2,3,4,5];
245  print HTMLOUT ' <th style="width:11%;">Start Time:</th><td style="width:22%;">' . sprintf('%04d%s%02d %02d:%02d:%02d', ($year+1900), $months[$month], $day, $hour, $min, $sec) . "</td>\n";
246  ($sec, $min, $hour, $day, $month, $year) = (localtime($timing_data->{'M'}->{'E'}))[0,1,2,3,4,5];
247  print HTMLOUT ' <th style="width:11%;">End Time:</th><td style="width:22%;">' . sprintf('%04d%s%02d %02d:%02d:%02d', ($year+1900), $months[$month], $day, $hour, $min, $sec) . "</td>\n";
248  print HTMLOUT "  <th>Processing Time:</th><td>" . &renderTime($total_duration) . "</td>\n";
249  print HTMLOUT "</tr>\n";
250
251  print HTMLOUT "<tr>\n";
252  print HTMLOUT "  <th>Processing Threads:</th><td>" . $number_of_workers . "</td>\n";
253  print HTMLOUT "  <th>Files Processed:</th><td>" . $file_count . "</td>\n";
254  print HTMLOUT "  <th>Problem Files:</th><td>" . $problem_files . "</td>\n";
255  print HTMLOUT "</tr>\n";
256
257  print HTMLOUT "<tr>\n";
258  print HTMLOUT '  <th>Serial Processing Time:</th><td>' . &renderTime($total_process_time) . "</td>\n";
259  print HTMLOUT '  <th>Serial IO Time:</th><td>' . &renderTime($total_io_time) . "</td>\n";
260  print HTMLOUT '  <th>IO Percentage:</th><td>' . sprintf('%d%%', (($total_io_time / $total_process_time) * 100)) . "</td>\n";
261  print HTMLOUT "</tr>\n";
262
263  print HTMLOUT "<tr>\n";
264  print HTMLOUT "  <th>Average Processing Time:</th><td>" . &renderTime($avg_processing_time) . "</td>\n";
265  print HTMLOUT "  <th>Average File IO Time:</th><td>" . &renderTime($avg_io_time) . "</td>\n";
266  print HTMLOUT "  <th>Average File CPU Time:</th><td>" . &renderTime($avg_cpu_time) . "</td>\n";
267  print HTMLOUT "</tr>\n";
268
269  print HTMLOUT "<tr>\n";
270  print HTMLOUT "  <th>Fastest File:</th><td>" . &renderTime($fastest_file) . "</td>\n";
271  print HTMLOUT "  <th>Slowest File:</th><td>" . &renderTime($slowest_file) . "</td>\n";
272  if ($data_locality > 0)
273  {
274    print HTMLOUT "  <th>Data Locality:</th><td>" . sprintf('%d%% [%d out of %d]', (($data_locality / $file_count) * 100), $data_locality, $file_count) . "</td>\n";
275  }
276  else
277  {
278    print HTMLOUT "  <th></th>\n";
279    print HTMLOUT "  <td></td>\n";
280  }
281  print HTMLOUT "</tr>\n";
282
283  print HTMLOUT "</table>\n";
284  print HTMLOUT "<hr />\n";
285  print HTMLOUT "<h2>Timing Chart (Gantt)</h2>\n";
286  print HTMLOUT renderLine($chart_width, $timing_data->{'M'}->{'S'}, $timing_data->{'M'}->{'E'}, 'master', $timing_data->{'M'}->{'N'}, $timing_data->{'M'}->{'S'}, $timing_data->{'M'}->{'E'}, {});
287  foreach my $worker_id (nsort keys %{$timing_data})
288  {
289    if ($worker_id ne 'M')
290    {
291      my $data = $timing_data->{$worker_id};
292      print HTMLOUT renderLine($chart_width, $timing_data->{'M'}->{'S'}, $timing_data->{'M'}->{'E'}, 'worker', $worker_id . ' [' . $data->{'N'} . ']', $data->{'S'}, $data->{'E'}, $data->{'F'});
293    }
294  }
295  print HTMLOUT '<div>' . "\n";
296  print HTMLOUT "</body>\n";
297  print HTMLOUT "</html>";
298  close(HTMLOUT);
299  print "Done!\n\n";
300  $chart_count++;
301}
302## generateChart() ##
303
304
305## @function debugPrint()
306#
307sub debugPrint
308{
309  my $msg = shift(@_);
310  if ($debug)
311  {
312    print STDERR '[DEBUG] ' . $msg . "\n";
313  }
314}
315## debugPrint() ##
316
317
318## @function filenameCat
319#
320sub filenameCat
321{
322  my $path = join('/', @_);
323  $path =~ s/[\/\\]+/\//g;
324  return $path;
325}
326## filenameCat() ##
327
328
329## @function printError()
330#
331sub printError
332{
333  my $msg = shift(@_);
334  die('Error! ' . $msg . "\n\n");
335}
336## printError() ##
337
338
339## @function printUsage()
340#
341sub printUsage
342{
343  my $msg = shift(@_);
344  if (defined $msg)
345  {
346    print 'Error! ' . $msg . "\n";
347  }
348  die("Usage: generate_gantt.pl [-width <width in pixels>] <dir> [<dir> ...]\n\n");
349}
350## printUsage() ##
351
352
353## @function longestCommonPath
354#
355sub longestCommonPath
356{
357  my ($path_new, $path_current) = @_;
358  my $result = '';
359  if (defined $path_current)
360  {
361    # Hide protocol before we split by slash
362    $path_new =~ s/:\/\//:/;
363    $path_current =~ s/:\/\//:/;
364    my @path_new_parts = split(/\//, $path_new);
365    my @path_current_parts = split(/\//, $path_current);
366    my @path_parts;
367    for (my $i = 0; $i < scalar(@path_current_parts); $i++)
368    {
369      if ($path_current_parts[$i] eq $path_new_parts[$i])
370      {
371        push(@path_parts, $path_new_parts[$i]);
372      }
373      else
374      {
375        last;
376      }
377    }
378    $result = &filenameCat(@path_parts);
379    # Restore protocol
380    $result =~ s/:/:\/\//;
381  }
382  else
383  {
384    $result = $path_new;
385  }
386  return $result;
387}
388## longestCommonPath() ##
389
390
391## @function renderLine()
392#
393sub renderLine
394{
395  my ($table_width, $start, $end, $class, $tname, $tstart, $tend, $jobs) = @_;
396  &debugPrint("renderLine($table_width, $start, $end, $class, $tname, $tstart, $tend, <jobs>)");
397  # All timings need to be relative to 0 (relative start)
398  my $duration = $end - $start;
399  my $rtstart = $tstart - $start;
400  my $rtend = $tend - $start;
401  # We need to scale these depending on the timing of this thread relative to
402  # the master thread
403  my $width = $chart_width;
404  my $left = 0;
405  if ($start != $tstart)
406  {
407    my $left_offset_percent = $rtstart / $duration;
408    $left = $left_offset_percent * $table_width;
409  }
410  # - subtract any left offset from width
411  $width = $width - $left;
412  # - right offset directly subtracted from width
413  if ($end != $tend)
414  {
415    my $right_offset_percent = ($duration - $rtend) / $duration;
416    my $right = $right_offset_percent * $table_width;
417    $width = $width - $right;
418  }
419  # Round things off (simple dutch rounding)
420  $left = int($left + 0.5);
421  $width = int($width + 0.5);
422  # Output the bar for this master/worker
423  my $html = '<div class="thread ' . $class . '" style="left:' . $left . 'px;width:' . $width . 'px;">';
424  if ($class eq 'master')
425  {
426    $html .= '<div style="background-color:blue;margin-bottom:1px">';
427  }
428  $html .= '<div class="time" style="display:table-cell">' . &renderTime($rtstart) . '</div><div style="display:table-cell;padding-left:20px;width:100%;">' . ucfirst($class) . ': ' . $tname . '</div><div class="time" style="display:table-cell">' . renderTime($rtend) . '</div></div>';
429  my $previous_jright = 0;
430  foreach my $jstart (sort keys %{$jobs})
431  {
432    my $rjstart = $jstart - $start;
433    my $rpstart = $jobs->{$jstart}->{'PS'} - $start;
434    my $rpend = $jobs->{$jstart}->{'PE'} - $start;
435    my $rjend = $jobs->{$jstart}->{'E'} - $start;
436    my $jduration = $jobs->{$jstart}->{'E'} - $jstart;
437    my $io_duration = $rpstart - $rjstart;
438    my $cpu_duration = $rpend - $rpstart;
439    # Scale Job co-ordinates
440    my $jleft_percent = $rjstart / $duration;
441    my $jleft = int(($jleft_percent * $table_width) + 0.5);
442    my $jwidth_percent = $jduration / $duration;
443    # -2 for left and right 1 pixel border
444    my $jwidth = int(($jwidth_percent * $table_width) + 0.5) - 2;
445    if ($jleft + $jwidth > $left + $width)
446    {
447      $jwidth = ($left + $width) - $jleft;
448    }
449    # Then scale process timings within that!
450    my $rpleft_percent = ($rpstart - $rjstart) / $duration;
451    my $rpleft = int(($rpleft_percent * $table_width) + 0.5);
452    my $rpwidth = $jwidth - $rpleft;
453    my $cpu_percent = int((($rpwidth / $jwidth) * 100) + 0.5);
454    $html .= '<div class="job" style="left:' . $jleft . 'px;width:' . $jwidth . 'px;';
455    if ($jobs->{$jstart}->{'DL'} != 1)
456    {
457      $html .= 'border:1px dashed black;';
458    }
459    $html .= '" title="FN:' . $jobs->{$jstart}->{'FN'} . ', S:' . &renderTime($rjstart) . ', E:' . &renderTime($rjend) . ', CPU: ' . $cpu_percent . '% [' . &renderTime($io_duration) . ', ' . &renderTime($cpu_duration) . ']"><span class="process" style="left:' . $rpleft . 'px;width:' . $rpwidth . 'px">&nbsp;</span><span class="label"';
460    if ($jobs->{$jstart}->{'DL'} != 1)
461    {
462      $html .= ' style="color:#FF0000"';
463    }
464    $html .=  '>' . $jobs->{$jstart}->{'FN'};
465    if ($jobs->{$jstart}->{'DL'} != 1)
466    {
467      $html .= ' [NL]';
468    }
469    $html .= '</span></div>';
470  }
471  return $html;
472}
473## renderLine() ##
474
475
476## @function renderTime()
477#
478sub renderTime
479{
480  my ($seconds) = @_;
481  my $time_str = '';
482  # determine how many hours
483  my $an_hour = 60 * 60;
484  my $hours = floor($seconds / $an_hour);
485  $seconds = $seconds - ($hours * $an_hour);
486  my $a_minute = 60;
487  my $minutes = floor($seconds / $a_minute);
488  $seconds = $seconds - ($minutes * $a_minute);
489  if ($hours > 0)
490  {
491    $time_str = sprintf('%dh%02dm%02ds', $hours, $minutes, $seconds);
492  }
493  else
494  {
495    $time_str = sprintf('%dm%02ds', $minutes, $seconds);
496  }
497  return $time_str;
498}
Note: See TracBrowser for help on using the browser.