source: gs2-extensions/parallel-building/trunk/src/bin/script/generate_gantt.pl@ 27753

Last change on this file since 27753 was 27753, checked in by jmt12, 11 years ago

Adding Handbrake's percentage complete to report - although this is sometime less than 100% even for files that successfully converted

  • Property svn:executable set to *
File size: 16.5 KB
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 my $percent_complete = $parts[9];
149 chomp($percent_complete);
150 $import_dir = &longestCommonPath($filepath, $import_dir);
151 $timing_data->{$worker_id}->{'F'}->{$parts[3]} = {'FN'=>$filepath, 'S'=>$parts[3], 'PS'=>($stop - $parts[5]), 'PE'=>$stop, 'E'=>$stop, 'DL'=>$parts[6], 'PC'=>$percent_complete};
152 }
153 }
154 close(TIN);
155 }
156 else
157 {
158 die('Error! Failed to open file for reading: ' . $timing_csv_path);
159 }
160 my $number_of_workers = scalar(keys(%{$id_2_worker_id}));;
161 print "Done\n";
162
163 # 3. Produce pretty HTML chart of timing information including jobs
164 print " - generating timing information as chart in HTML... ";
165 open(HTMLOUT, '>:utf8', $gantt_path) or die('Error! Failed to open file for writing: gantt.html');
166 print HTMLOUT "<html>\n";
167 print HTMLOUT '<head>' . "\n";
168 print HTMLOUT '<style type="text/css">' . "\n";
169 print HTMLOUT 'div.thread {position:relative}' . "\n";
170 print HTMLOUT 'div.master {border:1px solid gray;color:white;font-weight:bold}' . "\n";
171 print HTMLOUT 'div.worker {border:1px solid black;background-color:green;color:white;font-weight:bold;margin-bottom:1px;}' . "\n";
172 print HTMLOUT 'div.time {font-size:smaller;font-weight:normal}' . "\n";
173 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";
174 print HTMLOUT 'span.process {z-index:-1;background-color:#C7C7C7;position:absolute}' . "\n";
175 print HTMLOUT 'span.label {z-index:1;background-color:transparent;overflow:hidden;white-space:nowrap;}' . "\n";
176 print HTMLOUT "th {text-align:left}\n";
177 print HTMLOUT '</style>' . "\n";
178 print HTMLOUT '</head>' . "\n";
179 print HTMLOUT "<body>\n";
180 print HTMLOUT "<h2>Statistics</h2>\n";
181 print HTMLOUT "<table style=\"width:100%;\">\n";
182
183 my $total_duration = $timing_data->{'M'}->{'E'} - $timing_data->{'M'}->{'S'};
184 my $file_count = 0;
185 my $data_locality = 0;
186 my $total_io_time = 0;
187 my $total_process_time = 0;
188 my $fastest_file = 0;
189 my $slowest_file = 0;
190 my $problem_files = 0;
191 foreach my $worker_id (keys %{$timing_data})
192 {
193 if ($worker_id ne 'M')
194 {
195 foreach my $job_start ( keys %{$timing_data->{$worker_id}->{'F'}} )
196 {
197 my $process_start = $timing_data->{$worker_id}->{'F'}->{$job_start}->{'PS'};
198 my $process_end = $timing_data->{$worker_id}->{'F'}->{$job_start}->{'PE'};
199 my $job_end = $timing_data->{$worker_id}->{'F'}->{$job_start}->{'E'};
200 if ($process_start == 0 || $process_end == 0 || $job_end == 0)
201 {
202 $problem_files++;
203 }
204 else
205 {
206 my $io_duration = ($process_start - $job_start) + ($job_end - $process_end);
207 my $process_duration = $process_end - $process_start;
208 my $total_duration = $io_duration + $process_duration;
209 &debugPrint("filename: " . $timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'});
210 &debugPrint("start: $job_start ps: $process_start pe: $process_end end: $job_end");
211 &debugPrint("io: $io_duration process: $process_duration duration: $total_duration");
212 # Running stats
213 $total_io_time += $io_duration;
214 $total_process_time += $process_duration;
215 if ($fastest_file == 0 || $total_duration < $fastest_file)
216 {
217 $fastest_file = $total_duration;
218 }
219 if ($slowest_file == 0 || $total_duration > $slowest_file)
220 {
221 $slowest_file = $total_duration;
222 }
223 }
224 # Shorten filename
225 if (defined $timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'})
226 {
227 $timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'} = substr($timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'}, length($import_dir) + 1);
228 }
229 $file_count++;
230 if ($timing_data->{$worker_id}->{'F'}->{$job_start}->{'DL'} == 1)
231 {
232 $data_locality++;
233 }
234 }
235 }
236 }
237 my $avg_processing_time = floor(($total_io_time + $total_process_time) / $file_count);
238 my $avg_io_time = int(($total_io_time / $file_count) + 0.5);
239 my $avg_cpu_time = int(($total_process_time / $file_count) + 0.5);
240
241 print HTMLOUT "<tr>\n";
242 print HTMLOUT ' <th style="width:12%;">Import Directory:</th><td style="width:22%;" colspan="5">' . $import_dir . "</td>\n";
243 print HTMLOUT "</tr>\n";
244
245 print HTMLOUT "<tr>\n";
246 my ($sec, $min, $hour, $day, $month, $year) = (localtime($timing_data->{'M'}->{'S'}))[0,1,2,3,4,5];
247 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";
248 ($sec, $min, $hour, $day, $month, $year) = (localtime($timing_data->{'M'}->{'E'}))[0,1,2,3,4,5];
249 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";
250 print HTMLOUT " <th>Processing Time:</th><td>" . &renderTime($total_duration) . "</td>\n";
251 print HTMLOUT "</tr>\n";
252
253 print HTMLOUT "<tr>\n";
254 print HTMLOUT " <th>Processing Threads:</th><td>" . $number_of_workers . "</td>\n";
255 print HTMLOUT " <th>Files Processed:</th><td>" . $file_count . "</td>\n";
256 print HTMLOUT " <th>Problem Files:</th><td>" . $problem_files . "</td>\n";
257 print HTMLOUT "</tr>\n";
258
259 print HTMLOUT "<tr>\n";
260 print HTMLOUT ' <th>Serial Processing Time:</th><td>' . &renderTime($total_process_time) . "</td>\n";
261 print HTMLOUT ' <th>Serial IO Time:</th><td>' . &renderTime($total_io_time) . "</td>\n";
262 print HTMLOUT ' <th>IO Percentage:</th><td>' . sprintf('%d%%', (($total_io_time / $total_process_time) * 100)) . "</td>\n";
263 print HTMLOUT "</tr>\n";
264
265 print HTMLOUT "<tr>\n";
266 print HTMLOUT " <th>Average Processing Time:</th><td>" . &renderTime($avg_processing_time) . "</td>\n";
267 print HTMLOUT " <th>Average File IO Time:</th><td>" . &renderTime($avg_io_time) . "</td>\n";
268 print HTMLOUT " <th>Average File CPU Time:</th><td>" . &renderTime($avg_cpu_time) . "</td>\n";
269 print HTMLOUT "</tr>\n";
270
271 print HTMLOUT "<tr>\n";
272 print HTMLOUT " <th>Fastest File:</th><td>" . &renderTime($fastest_file) . "</td>\n";
273 print HTMLOUT " <th>Slowest File:</th><td>" . &renderTime($slowest_file) . "</td>\n";
274 if ($data_locality > 0)
275 {
276 print HTMLOUT " <th>Data Locality:</th><td>" . sprintf('%d%% [%d out of %d]', (($data_locality / $file_count) * 100), $data_locality, $file_count) . "</td>\n";
277 }
278 else
279 {
280 print HTMLOUT " <th>Data Locality:</th><td><i>Not Applicable</i></td>\n";
281 }
282 print HTMLOUT "</tr>\n";
283
284 print HTMLOUT "</table>\n";
285 print HTMLOUT "<hr />\n";
286 print HTMLOUT "<h2>Timing Chart (Gantt)</h2>\n";
287 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'}, {}, $data_locality);
288 foreach my $worker_id (nsort keys %{$timing_data})
289 {
290 if ($worker_id ne 'M')
291 {
292 my $data = $timing_data->{$worker_id};
293 print HTMLOUT renderLine($chart_width, $timing_data->{'M'}->{'S'}, $timing_data->{'M'}->{'E'}, 'worker', $worker_id . ' [' . $data->{'N'} . ']', $data->{'S'}, $data->{'E'}, $data->{'F'}, $data_locality);
294 }
295 }
296 print HTMLOUT '<div>' . "\n";
297 print HTMLOUT "</body>\n";
298 print HTMLOUT "</html>";
299 close(HTMLOUT);
300 print "Done!\n\n";
301 $chart_count++;
302}
303## generateChart() ##
304
305
306## @function debugPrint()
307#
308sub debugPrint
309{
310 my $msg = shift(@_);
311 if ($debug)
312 {
313 print STDERR '[DEBUG] ' . $msg . "\n";
314 }
315}
316## debugPrint() ##
317
318
319## @function filenameCat
320#
321sub filenameCat
322{
323 my $path = join('/', @_);
324 $path =~ s/[\/\\]+/\//g;
325 return $path;
326}
327## filenameCat() ##
328
329
330## @function printError()
331#
332sub printError
333{
334 my $msg = shift(@_);
335 die('Error! ' . $msg . "\n\n");
336}
337## printError() ##
338
339
340## @function printUsage()
341#
342sub printUsage
343{
344 my $msg = shift(@_);
345 if (defined $msg)
346 {
347 print 'Error! ' . $msg . "\n";
348 }
349 die("Usage: generate_gantt.pl [-width <width in pixels>] <dir> [<dir> ...]\n\n");
350}
351## printUsage() ##
352
353
354## @function longestCommonPath
355#
356sub longestCommonPath
357{
358 my ($path_new, $path_current) = @_;
359 my $result = '';
360 if (defined $path_current)
361 {
362 # Hide protocol before we split by slash
363 $path_new =~ s/:\/\//:/;
364 $path_current =~ s/:\/\//:/;
365 my @path_new_parts = split(/\//, $path_new);
366 my @path_current_parts = split(/\//, $path_current);
367 my @path_parts;
368 for (my $i = 0; $i < scalar(@path_current_parts); $i++)
369 {
370 if ($path_current_parts[$i] eq $path_new_parts[$i])
371 {
372 push(@path_parts, $path_new_parts[$i]);
373 }
374 else
375 {
376 last;
377 }
378 }
379 $result = &filenameCat(@path_parts);
380 # Restore protocol
381 $result =~ s/:/:\/\//;
382 }
383 else
384 {
385 $result = $path_new;
386 }
387 return $result;
388}
389## longestCommonPath() ##
390
391
392## @function renderLine()
393#
394sub renderLine
395{
396 my ($table_width, $start, $end, $class, $tname, $tstart, $tend, $jobs, $data_locality) = @_;
397 &debugPrint("renderLine($table_width, $start, $end, $class, $tname, $tstart, $tend, <jobs>)");
398 # All timings need to be relative to 0 (relative start)
399 my $duration = $end - $start;
400 my $rtstart = $tstart - $start;
401 my $rtend = $tend - $start;
402 # We need to scale these depending on the timing of this thread relative to
403 # the master thread
404 my $width = $chart_width;
405 my $left = 0;
406 if ($start != $tstart)
407 {
408 my $left_offset_percent = $rtstart / $duration;
409 $left = $left_offset_percent * $table_width;
410 }
411 # - subtract any left offset from width
412 $width = $width - $left;
413 # - right offset directly subtracted from width
414 if ($end != $tend)
415 {
416 my $right_offset_percent = ($duration - $rtend) / $duration;
417 my $right = $right_offset_percent * $table_width;
418 $width = $width - $right;
419 }
420 # Round things off (simple dutch rounding)
421 $left = int($left + 0.5);
422 $width = int($width + 0.5);
423 # Output the bar for this master/worker
424 my $html = '<div class="thread ' . $class . '" style="left:' . $left . 'px;width:' . $width . 'px;">';
425 if ($class eq 'master')
426 {
427 $html .= '<div style="background-color:blue;margin-bottom:1px">';
428 }
429 $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>';
430 my $previous_jright = 0;
431 foreach my $jstart (sort keys %{$jobs})
432 {
433 my $rjstart = $jstart - $start;
434 my $rpstart = $jobs->{$jstart}->{'PS'} - $start;
435 my $rpend = $jobs->{$jstart}->{'PE'} - $start;
436 my $rjend = $jobs->{$jstart}->{'E'} - $start;
437 my $jduration = $jobs->{$jstart}->{'E'} - $jstart;
438 my $io_duration = $rpstart - $rjstart;
439 my $cpu_duration = $rpend - $rpstart;
440 # Scale Job co-ordinates
441 my $jleft_percent = $rjstart / $duration;
442 my $jleft = int(($jleft_percent * $table_width) + 0.5);
443 my $jwidth_percent = $jduration / $duration;
444 # -2 for left and right 1 pixel border
445 my $jwidth = int(($jwidth_percent * $table_width) + 0.5) - 2;
446 if ($jleft + $jwidth > $left + $width)
447 {
448 $jwidth = ($left + $width) - $jleft;
449 }
450 # Then scale process timings within that!
451 my $rpleft_percent = ($rpstart - $rjstart) / $duration;
452 my $rpleft = int(($rpleft_percent * $table_width) + 0.5);
453 my $rpwidth = $jwidth - $rpleft;
454 my $cpu_percent = int((($rpwidth / $jwidth) * 100) + 0.5);
455 $html .= '<div class="job" style="left:' . $jleft . 'px;width:' . $jwidth . 'px;';
456 if ($data_locality > 1 && $jobs->{$jstart}->{'DL'} != 1)
457 {
458 $html .= 'border:1px dashed black;';
459 }
460 $html .= '" title="FN:' . $jobs->{$jstart}->{'FN'} . ', S:' . &renderTime($rjstart) . ', E:' . &renderTime($rjend) . ', CPU: ' . $cpu_percent . '% [' . &renderTime($io_duration) . ', ' . &renderTime($cpu_duration) . ', PC: ' . $jobs->{$jstart}->{'PC'} . '%]"><span class="process" style="left:' . $rpleft . 'px;width:' . $rpwidth . 'px">&nbsp;</span><span class="label"';
461 if ($data_locality > 1 && $jobs->{$jstart}->{'DL'} != 1)
462 {
463 $html .= ' style="color:#FF0000"';
464 }
465 $html .= '>' . $jobs->{$jstart}->{'FN'};
466 if ($jobs->{$jstart}->{'PC'} ne 'NA')
467 {
468 $html .= ' <small>[' . $jobs->{$jstart}->{'PC'} . '%]</small>';
469 }
470 if ($data_locality > 1 && $jobs->{$jstart}->{'DL'} != 1)
471 {
472 $html .= ' [NL]';
473 }
474 $html .= '</span></div>';
475 }
476 return $html;
477}
478## renderLine() ##
479
480
481## @function renderTime()
482#
483sub renderTime
484{
485 my ($seconds) = @_;
486 my $time_str = '';
487 # determine how many hours
488 my $an_hour = 60 * 60;
489 my $hours = floor($seconds / $an_hour);
490 $seconds = $seconds - ($hours * $an_hour);
491 my $a_minute = 60;
492 my $minutes = floor($seconds / $a_minute);
493 $seconds = $seconds - ($minutes * $a_minute);
494 if ($hours > 0)
495 {
496 $time_str = sprintf('%dh%02dm%02ds', $hours, $minutes, $seconds);
497 }
498 else
499 {
500 $time_str = sprintf('%dm%02ds', $minutes, $seconds);
501 }
502 return $time_str;
503}
Note: See TracBrowser for help on using the repository browser.