[27543] | 1 | #!/usr/bin/perl
|
---|
| 2 |
|
---|
| 3 | use strict;
|
---|
| 4 | use warnings;
|
---|
| 5 |
|
---|
| 6 | use Sort::Naturally;
|
---|
| 7 | use POSIX qw(floor strftime);
|
---|
| 8 |
|
---|
| 9 | print "\n===== Generate Timing (GANTT) =====\n";
|
---|
| 10 |
|
---|
[27551] | 11 | # 0. Configuration
|
---|
| 12 | my $debug = 0;
|
---|
| 13 | my $import_dir;
|
---|
| 14 |
|
---|
[27543] | 15 | # 1. Initialization
|
---|
[27551] | 16 | if (!defined $ARGV[0] || !-d $ARGV[0])
|
---|
[27543] | 17 | {
|
---|
[27551] | 18 | &printUsage('Directory not provided or doesn\'t exist');
|
---|
[27543] | 19 | }
|
---|
[27551] | 20 | my $dir = $ARGV[0];
|
---|
| 21 | my $timing_csv_path = &filenameCat($dir, 'timing.csv');
|
---|
| 22 | if (!-e $timing_csv_path)
|
---|
[27543] | 23 | {
|
---|
[27551] | 24 | &printUsage('Directory doesn\'t contain timing.csv: ' . $dir);
|
---|
[27543] | 25 | }
|
---|
[27551] | 26 | print 'Timing File: ' . $timing_csv_path . "\n";
|
---|
| 27 | my $chart_width = 1024;
|
---|
| 28 | if (defined $ARGV[1])
|
---|
[27543] | 29 | {
|
---|
[27551] | 30 | if ($ARGV[1] !~ /^\d+$/)
|
---|
[27543] | 31 | {
|
---|
[27551] | 32 | &printUsage('Chart width not a number');
|
---|
[27543] | 33 | }
|
---|
[27551] | 34 | $chart_width = $ARGV[1];
|
---|
[27543] | 35 | }
|
---|
[27551] | 36 | print "Chart Width: " . $chart_width . "px\n";
|
---|
| 37 | print "===================================\n\n";
|
---|
[27543] | 38 |
|
---|
[27551] | 39 | # Read in timing.csv and parse information into data structure
|
---|
| 40 | my $timing_data = {};
|
---|
| 41 | my $id_2_worker_id = {};
|
---|
| 42 | if (open(TIN, '<:utf8', $timing_csv_path))
|
---|
[27543] | 43 | {
|
---|
[27551] | 44 | my $line;
|
---|
| 45 | while ($line = <TIN>)
|
---|
[27543] | 46 | {
|
---|
[27551] | 47 | my @parts = split(/,/, $line);
|
---|
| 48 | if ($parts[1] eq 'M0')
|
---|
[27543] | 49 | {
|
---|
[27551] | 50 | $timing_data->{'M'} = {'N'=>$parts[2], 'S'=>$parts[3], 'E'=>$parts[4]};
|
---|
[27543] | 51 | }
|
---|
[27551] | 52 | elsif ($parts[1] =~ /W\d+/)
|
---|
[27543] | 53 | {
|
---|
[27551] | 54 | $timing_data->{$parts[1]} = {'N'=>$parts[2], 'S'=>$parts[3], 'E'=>$parts[4], 'F'=>{}};
|
---|
| 55 | $id_2_worker_id->{$parts[0]} = $parts[1];
|
---|
[27543] | 56 | }
|
---|
[27551] | 57 | elsif ($parts[1] =~ /T\d+/)
|
---|
| 58 | {
|
---|
| 59 | my $worker_id = $id_2_worker_id->{$parts[7]};
|
---|
| 60 | my $stop = $parts[4];
|
---|
| 61 | my $filepath = $parts[8];
|
---|
[27559] | 62 | $filepath =~ s/^\s+|\s+$//g;
|
---|
[27551] | 63 | $import_dir = &longestCommonPath($filepath, $import_dir);
|
---|
| 64 | $timing_data->{$worker_id}->{'F'}->{$parts[3]} = {'FN'=>$filepath, 'PS'=>($stop - $parts[5]), 'PE'=>$stop, 'E'=>$stop};
|
---|
| 65 | }
|
---|
[27543] | 66 | }
|
---|
[27551] | 67 | close(TIN);
|
---|
[27543] | 68 | }
|
---|
[27551] | 69 | else
|
---|
[27543] | 70 | {
|
---|
[27551] | 71 | die('Error! Failed to open file for reading: ' . $timing_csv_path);
|
---|
[27543] | 72 | }
|
---|
[27551] | 73 | my $number_of_workers = scalar(keys(%{$id_2_worker_id}));;
|
---|
[27543] | 74 |
|
---|
[27551] | 75 | # 3. Produce pretty HTML chart of timing information including jobs
|
---|
[27543] | 76 | print " * Generating timing information as HTML... ";
|
---|
[27551] | 77 | open(HTMLOUT, '>:utf8', $dir . '/gantt.html') or die('Error! Failed to open file for writing: gantt.html');
|
---|
[27543] | 78 | print HTMLOUT "<html>\n";
|
---|
| 79 | print HTMLOUT '<head>' . "\n";
|
---|
| 80 | print HTMLOUT '<style type="text/css">' . "\n";
|
---|
| 81 | print HTMLOUT 'div.thread {position:relative}' . "\n";
|
---|
| 82 | print HTMLOUT 'div.master {border:1px solid gray;color:white;font-weight:bold}' . "\n";
|
---|
[27551] | 83 | print HTMLOUT 'div.worker {border:1px solid black;background-color:green;color:white;font-weight:bold}' . "\n";
|
---|
[27543] | 84 | print HTMLOUT 'div.time {font-size:smaller;font-weight:normal}' . "\n";
|
---|
[27559] | 85 | 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}' . "\n";
|
---|
[27543] | 86 | print HTMLOUT 'span.process {z-index:-1;background-color:#C7C7C7;position:absolute}' . "\n";
|
---|
[27559] | 87 | print HTMLOUT 'span.label {z-index:1;background-color:transparent;overflow:hidden;white-space:nowrap;}' . "\n";
|
---|
[27543] | 88 | print HTMLOUT "th {text-align:left}\n";
|
---|
| 89 | print HTMLOUT '</style>' . "\n";
|
---|
| 90 | print HTMLOUT '</head>' . "\n";
|
---|
| 91 | print HTMLOUT "<body>\n";
|
---|
| 92 | print HTMLOUT "<h2>Statistics</h2>\n";
|
---|
| 93 | print HTMLOUT "<table>\n";
|
---|
| 94 |
|
---|
| 95 | my $total_duration = $timing_data->{'M'}->{'E'} - $timing_data->{'M'}->{'S'};
|
---|
| 96 | my $file_count = 0;
|
---|
| 97 | my $total_io_time = 0;
|
---|
| 98 | my $total_process_time = 0;
|
---|
| 99 | my $fastest_file = 0;
|
---|
| 100 | my $slowest_file = 0;
|
---|
| 101 | my $problem_files = 0;
|
---|
| 102 | foreach my $worker_id (keys %{$timing_data})
|
---|
| 103 | {
|
---|
| 104 | if ($worker_id ne 'M')
|
---|
| 105 | {
|
---|
| 106 | foreach my $job_start ( keys %{$timing_data->{$worker_id}->{'F'}} )
|
---|
| 107 | {
|
---|
| 108 | my $process_start = $timing_data->{$worker_id}->{'F'}->{$job_start}->{'PS'};
|
---|
| 109 | my $process_end = $timing_data->{$worker_id}->{'F'}->{$job_start}->{'PE'};
|
---|
| 110 | my $job_end = $timing_data->{$worker_id}->{'F'}->{$job_start}->{'E'};
|
---|
| 111 | if ($process_start == 0 || $process_end == 0 || $job_end == 0)
|
---|
| 112 | {
|
---|
| 113 | $problem_files++;
|
---|
| 114 | }
|
---|
| 115 | else
|
---|
| 116 | {
|
---|
| 117 | my $io_duration = ($process_start - $job_start) + ($job_end - $process_end);
|
---|
| 118 | my $process_duration = $process_end - $process_start;
|
---|
| 119 | my $total_duration = $io_duration + $process_duration;
|
---|
[27551] | 120 | &debugPrint("filename: " . $timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'});
|
---|
| 121 | &debugPrint("start: $job_start ps: $process_start pe: $process_end end: $job_end");
|
---|
| 122 | &debugPrint("io: $io_duration process: $process_duration duration: $total_duration");
|
---|
[27543] | 123 | # Running stats
|
---|
| 124 | $total_io_time += $io_duration;
|
---|
| 125 | $total_process_time += $process_duration;
|
---|
| 126 | if ($fastest_file == 0 || $total_duration < $fastest_file)
|
---|
| 127 | {
|
---|
| 128 | $fastest_file = $total_duration;
|
---|
| 129 | }
|
---|
| 130 | if ($slowest_file == 0 || $total_duration > $slowest_file)
|
---|
| 131 | {
|
---|
| 132 | $slowest_file = $total_duration;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
[27551] | 135 | # Shorten filename
|
---|
[27559] | 136 | if (defined $timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'})
|
---|
| 137 | {
|
---|
| 138 | $timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'} = substr($timing_data->{$worker_id}->{'F'}->{$job_start}->{'FN'}, length($import_dir) + 1);
|
---|
| 139 | }
|
---|
[27543] | 140 | $file_count++;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | my $avg_processing_time = floor(($total_io_time + $total_process_time) / $file_count);
|
---|
| 145 |
|
---|
[27551] | 146 | print HTMLOUT "<tr><th>Import Directory:</th><td>" . $import_dir . "</td></tr>\n";
|
---|
[27543] | 147 | print HTMLOUT "<tr><th>Processing Time:</th><td>" . &renderTime($total_duration) . "</td></tr>\n";
|
---|
| 148 | print HTMLOUT "<tr><th>Processing Threads:</th><td>" . $number_of_workers . "</td></tr>\n";
|
---|
| 149 | print HTMLOUT "<tr><th>Files Processed:</th><td>" . $file_count . "</td></tr>\n";
|
---|
| 150 | print HTMLOUT "<tr><th>Problem Files:</th><td>" . $problem_files . "</td></tr>\n";
|
---|
| 151 | print HTMLOUT "<tr><th>Serial Processing Time:</th><td>" . &renderTime($total_process_time) . "</td></tr>\n";
|
---|
| 152 | print HTMLOUT "<tr><th>Serial IO Time:</th><td>" . &renderTime($total_io_time) . "</td></tr>\n";
|
---|
| 153 | print HTMLOUT "<tr><th>Average File Processing Time:</th><td>" . &renderTime($avg_processing_time) . "</td></tr>\n";
|
---|
| 154 | print HTMLOUT "<tr><th>Fastest File:</th><td>" . &renderTime($fastest_file) . "</td></tr>\n";
|
---|
| 155 | print HTMLOUT "<tr><th>Slowest File:</th><td>" . &renderTime($slowest_file) . "</td></tr>\n";
|
---|
| 156 |
|
---|
| 157 | print HTMLOUT "</table>\n";
|
---|
| 158 | print HTMLOUT "<hr />\n";
|
---|
| 159 | print HTMLOUT "<h2>Timing Chart (Gannt)</h2>\n";
|
---|
| 160 | 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'}, {});
|
---|
| 161 | foreach my $worker_id (nsort keys %{$timing_data})
|
---|
| 162 | {
|
---|
| 163 | if ($worker_id ne 'M')
|
---|
| 164 | {
|
---|
| 165 | my $data = $timing_data->{$worker_id};
|
---|
| 166 | print HTMLOUT renderLine($chart_width, $timing_data->{'M'}->{'S'}, $timing_data->{'M'}->{'E'}, 'worker', $worker_id . ' [' . $data->{'N'} . ']', $data->{'S'}, $data->{'E'}, $data->{'F'});
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | print HTMLOUT '<div>' . "\n";
|
---|
| 170 | print HTMLOUT "</body>\n";
|
---|
| 171 | print HTMLOUT "</html>";
|
---|
| 172 | close(HTMLOUT);
|
---|
| 173 |
|
---|
| 174 | print "Done!\n";
|
---|
| 175 | print "Complete!\n\n";
|
---|
| 176 | exit;
|
---|
| 177 |
|
---|
[27551] | 178 |
|
---|
| 179 | ## @function debugPrint()
|
---|
| 180 | #
|
---|
| 181 | sub debugPrint
|
---|
| 182 | {
|
---|
| 183 | my $msg = shift(@_);
|
---|
| 184 | if ($debug)
|
---|
| 185 | {
|
---|
| 186 | print STDERR '[DEBUG] ' . $msg . "\n";
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | ## debugPrint() ##
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | ## @function filenameCat
|
---|
| 193 | #
|
---|
| 194 | sub filenameCat
|
---|
| 195 | {
|
---|
| 196 | my $path = join('/', @_);
|
---|
| 197 | $path =~ s/[\/\\]+/\//g;
|
---|
| 198 | # protocols
|
---|
| 199 | $path =~ s/^(HDFS|HDFSShell|HDThriftFS):\//$1:\/\//;
|
---|
| 200 | return $path;
|
---|
| 201 | }
|
---|
| 202 | ## filenameCat() ##
|
---|
| 203 |
|
---|
| 204 | ## @function printUsage()
|
---|
| 205 | #
|
---|
| 206 | sub printUsage
|
---|
| 207 | {
|
---|
| 208 | my $msg = shift(@_);
|
---|
| 209 | if (defined $msg)
|
---|
| 210 | {
|
---|
| 211 | print 'Error! ' . $msg . "\n";
|
---|
| 212 | }
|
---|
| 213 | die("Usage: generate_gantt.pl <results dir> [<width in pixels>]\n\n");
|
---|
| 214 | }
|
---|
| 215 | ## printUsage() ##
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | ## @function longestCommonPath
|
---|
| 219 | #
|
---|
| 220 | sub longestCommonPath
|
---|
| 221 | {
|
---|
| 222 | my ($path_new, $path_current) = @_;
|
---|
| 223 | my $result = '';
|
---|
| 224 | if (defined $path_current)
|
---|
| 225 | {
|
---|
| 226 | my @path_new_parts = split(/\//, $path_new);
|
---|
| 227 | my @path_current_parts = split(/\//, $path_current);
|
---|
| 228 | my @path_parts;
|
---|
| 229 | for (my $i = 0; $i < scalar(@path_current_parts); $i++)
|
---|
| 230 | {
|
---|
| 231 | if ($path_current_parts[$i] eq $path_new_parts[$i])
|
---|
| 232 | {
|
---|
| 233 | push(@path_parts, $path_new_parts[$i]);
|
---|
| 234 | }
|
---|
| 235 | else
|
---|
| 236 | {
|
---|
| 237 | last;
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | $result = &filenameCat(@path_parts);
|
---|
| 241 | }
|
---|
| 242 | else
|
---|
| 243 | {
|
---|
| 244 | $result = $path_new;
|
---|
| 245 | }
|
---|
| 246 | return $result;
|
---|
| 247 | }
|
---|
| 248 | ## longestCommonPath() ##
|
---|
| 249 |
|
---|
| 250 |
|
---|
| 251 | ## @function renderLine()
|
---|
| 252 | #
|
---|
[27543] | 253 | sub renderLine
|
---|
| 254 | {
|
---|
| 255 | my ($table_width, $start, $end, $class, $tname, $tstart, $tend, $jobs) = @_;
|
---|
[27551] | 256 | &debugPrint("renderLine($table_width, $start, $end, $class, $tname, $tstart, $tend, <jobs>)");
|
---|
[27543] | 257 | # All timings need to be relative to 0 (relative start)
|
---|
| 258 | my $duration = $end - $start;
|
---|
| 259 | my $rtstart = $tstart - $start;
|
---|
| 260 | my $rtend = $tend - $start;
|
---|
| 261 | # We need to scale these depending on the timing of this thread relative to
|
---|
| 262 | # the master thread
|
---|
| 263 | my $width = $chart_width;
|
---|
| 264 | my $left = 0;
|
---|
| 265 | if ($start != $tstart)
|
---|
| 266 | {
|
---|
| 267 | my $left_offset_percent = $rtstart / $duration;
|
---|
| 268 | $left = $left_offset_percent * $table_width;
|
---|
| 269 | }
|
---|
| 270 | # - subtract any left offset from width
|
---|
| 271 | $width = $width - $left;
|
---|
[27559] | 272 | # - right offset directly subtracted from width
|
---|
[27543] | 273 | if ($end != $tend)
|
---|
| 274 | {
|
---|
| 275 | my $right_offset_percent = ($duration - $rtend) / $duration;
|
---|
| 276 | my $right = $right_offset_percent * $table_width;
|
---|
| 277 | $width = $width - $right;
|
---|
| 278 | }
|
---|
[27559] | 279 | # Round things off (simple dutch rounding)
|
---|
| 280 | $left = int($left + 0.5);
|
---|
| 281 | $width = int($width + 0.5);
|
---|
| 282 | # Output the bar for this master/worker
|
---|
[27543] | 283 | my $html = '<div class="thread ' . $class . '" style="left:' . $left . 'px;width:' . $width . 'px;">';
|
---|
| 284 | if ($class eq 'master')
|
---|
| 285 | {
|
---|
| 286 | $html .= '<div style="background-color:blue">';
|
---|
| 287 | }
|
---|
| 288 | $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>';
|
---|
| 289 | my $previous_jright = 0;
|
---|
| 290 | foreach my $jstart (sort keys %{$jobs})
|
---|
| 291 | {
|
---|
| 292 | my $rjstart = $jstart - $start;
|
---|
| 293 | my $rpstart = $jobs->{$jstart}->{'PS'} - $start;
|
---|
| 294 | my $rpend = $jobs->{$jstart}->{'PE'} - $start;
|
---|
| 295 | my $rjend = $jobs->{$jstart}->{'E'} - $start;
|
---|
| 296 | my $jduration = $jobs->{$jstart}->{'E'} - $jstart;
|
---|
| 297 | # Scale Job co-ordinates
|
---|
| 298 | my $jleft_percent = $rjstart / $duration;
|
---|
[27559] | 299 | my $jleft = int(($jleft_percent * $table_width) + 0.5);
|
---|
[27543] | 300 | my $jwidth_percent = $jduration / $duration;
|
---|
[27559] | 301 | my $jwidth = int(($jwidth_percent * $table_width) + 0.5);
|
---|
[27543] | 302 | if ($jleft + $jwidth > $left + $width)
|
---|
| 303 | {
|
---|
| 304 | $jwidth = ($left + $width) - $jleft;
|
---|
| 305 | }
|
---|
| 306 | # Then scale process timings within that!
|
---|
| 307 | my $rpleft_percent = ($rpstart - $rjstart) / $duration;
|
---|
[27559] | 308 | my $rpleft = int(($rpleft_percent * $table_width) + 0.5);
|
---|
| 309 | my $rpwidth = $jwidth - $rpleft;
|
---|
| 310 | my $cpu_percent = int((($rpwidth / $jwidth) * 100) + 0.5);
|
---|
| 311 | $html .= '<div class="job" style="left:' . $jleft . 'px;width:' . $jwidth . 'px" title="FN:' . $jobs->{$jstart}->{'FN'} . ', S:' . renderTime($rjstart) . ', E:' . renderTime($rjend) . ', CPU: ' . $cpu_percent . '%"><span class="process" style="left:' . $rpleft . 'px;width:' . $rpwidth . 'px"> </span><span class="label">' . $jobs->{$jstart}->{'FN'} . '</span></div>';
|
---|
[27543] | 312 | }
|
---|
| 313 | return $html;
|
---|
| 314 | }
|
---|
[27551] | 315 | ## renderLine() ##
|
---|
[27543] | 316 |
|
---|
[27551] | 317 |
|
---|
| 318 | ## @function renderTime()
|
---|
| 319 | #
|
---|
[27543] | 320 | sub renderTime
|
---|
| 321 | {
|
---|
| 322 | my ($seconds) = @_;
|
---|
| 323 | my $time_str = '';
|
---|
| 324 | # determine how many hours
|
---|
| 325 | my $an_hour = 60 * 60;
|
---|
| 326 | my $hours = floor($seconds / $an_hour);
|
---|
| 327 | $seconds = $seconds - ($hours * $an_hour);
|
---|
| 328 | my $a_minute = 60;
|
---|
| 329 | my $minutes = floor($seconds / $a_minute);
|
---|
| 330 | $seconds = $seconds - ($minutes * $a_minute);
|
---|
| 331 | if ($hours > 0)
|
---|
| 332 | {
|
---|
| 333 | $time_str = sprintf('%dh%02dm%02ds', $hours, $minutes, $seconds);
|
---|
| 334 | }
|
---|
| 335 | elsif ($minutes > 0)
|
---|
| 336 | {
|
---|
| 337 | $time_str = sprintf('%dm%02ds', $minutes, $seconds);
|
---|
| 338 | }
|
---|
| 339 | else
|
---|
| 340 | {
|
---|
| 341 | $time_str = $seconds . 's';
|
---|
| 342 | }
|
---|
| 343 | return $time_str;
|
---|
| 344 | }
|
---|