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

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

Changed mime-type away from binary - I hope. Meanwhile, generate improvements to maths to reduce rounding errors, and also to allow multiple tasks to occur at once (a possibility under the more ambiguous Hadoop parallel processing)

  • Property svn:executable set to *
File size: 11.3 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Sort::Naturally;
7use POSIX qw(floor strftime);
8
9print "\n===== Generate Timing (GANTT) =====\n";
10
11# 0. Configuration
12my $debug = 0;
13my $import_dir;
14
15# 1. Initialization
16if (!defined $ARGV[0] || !-d $ARGV[0])
17{
18 &printUsage('Directory not provided or doesn\'t exist');
19}
20my $dir = $ARGV[0];
21my $timing_csv_path = &filenameCat($dir, 'timing.csv');
22if (!-e $timing_csv_path)
23{
24 &printUsage('Directory doesn\'t contain timing.csv: ' . $dir);
25}
26print 'Timing File: ' . $timing_csv_path . "\n";
27my $chart_width = 1024;
28if (defined $ARGV[1])
29{
30 if ($ARGV[1] !~ /^\d+$/)
31 {
32 &printUsage('Chart width not a number');
33 }
34 $chart_width = $ARGV[1];
35}
36print "Chart Width: " . $chart_width . "px\n";
37print "===================================\n\n";
38
39# Read in timing.csv and parse information into data structure
40my $timing_data = {};
41my $id_2_worker_id = {};
42if (open(TIN, '<:utf8', $timing_csv_path))
43{
44 my $line;
45 while ($line = <TIN>)
46 {
47 my @parts = split(/,/, $line);
48 if ($parts[1] eq 'M0')
49 {
50 $timing_data->{'M'} = {'N'=>$parts[2], 'S'=>$parts[3], 'E'=>$parts[4]};
51 }
52 elsif ($parts[1] =~ /W\d+/)
53 {
54 $timing_data->{$parts[1]} = {'N'=>$parts[2], 'S'=>$parts[3], 'E'=>$parts[4], 'F'=>{}};
55 $id_2_worker_id->{$parts[0]} = $parts[1];
56 }
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];
62 $filepath =~ s/^\s+|\s+$//g;
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 }
66 }
67 close(TIN);
68}
69else
70{
71 die('Error! Failed to open file for reading: ' . $timing_csv_path);
72}
73my $number_of_workers = scalar(keys(%{$id_2_worker_id}));;
74
75# 3. Produce pretty HTML chart of timing information including jobs
76print " * Generating timing information as HTML... ";
77open(HTMLOUT, '>:utf8', $dir . '/gantt.html') or die('Error! Failed to open file for writing: gantt.html');
78print HTMLOUT "<html>\n";
79print HTMLOUT '<head>' . "\n";
80print HTMLOUT '<style type="text/css">' . "\n";
81print HTMLOUT 'div.thread {position:relative}' . "\n";
82print HTMLOUT 'div.master {border:1px solid gray;color:white;font-weight:bold}' . "\n";
83print HTMLOUT 'div.worker {border:1px solid black;background-color:green;color:white;font-weight:bold}' . "\n";
84print HTMLOUT 'div.time {font-size:smaller;font-weight:normal}' . "\n";
85print 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";
86print HTMLOUT 'span.process {z-index:-1;background-color:#C7C7C7;position:absolute}' . "\n";
87print HTMLOUT 'span.label {z-index:1;background-color:transparent;overflow:hidden;white-space:nowrap;}' . "\n";
88print HTMLOUT "th {text-align:left}\n";
89print HTMLOUT '</style>' . "\n";
90print HTMLOUT '</head>' . "\n";
91print HTMLOUT "<body>\n";
92print HTMLOUT "<h2>Statistics</h2>\n";
93print HTMLOUT "<table>\n";
94
95my $total_duration = $timing_data->{'M'}->{'E'} - $timing_data->{'M'}->{'S'};
96my $file_count = 0;
97my $total_io_time = 0;
98my $total_process_time = 0;
99my $fastest_file = 0;
100my $slowest_file = 0;
101my $problem_files = 0;
102foreach 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;
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");
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 }
135 # Shorten filename
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 }
140 $file_count++;
141 }
142 }
143}
144my $avg_processing_time = floor(($total_io_time + $total_process_time) / $file_count);
145
146print HTMLOUT "<tr><th>Import Directory:</th><td>" . $import_dir . "</td></tr>\n";
147print HTMLOUT "<tr><th>Processing Time:</th><td>" . &renderTime($total_duration) . "</td></tr>\n";
148print HTMLOUT "<tr><th>Processing Threads:</th><td>" . $number_of_workers . "</td></tr>\n";
149print HTMLOUT "<tr><th>Files Processed:</th><td>" . $file_count . "</td></tr>\n";
150print HTMLOUT "<tr><th>Problem Files:</th><td>" . $problem_files . "</td></tr>\n";
151print HTMLOUT "<tr><th>Serial Processing Time:</th><td>" . &renderTime($total_process_time) . "</td></tr>\n";
152print HTMLOUT "<tr><th>Serial IO Time:</th><td>" . &renderTime($total_io_time) . "</td></tr>\n";
153print HTMLOUT "<tr><th>Average File Processing Time:</th><td>" . &renderTime($avg_processing_time) . "</td></tr>\n";
154print HTMLOUT "<tr><th>Fastest File:</th><td>" . &renderTime($fastest_file) . "</td></tr>\n";
155print HTMLOUT "<tr><th>Slowest File:</th><td>" . &renderTime($slowest_file) . "</td></tr>\n";
156
157print HTMLOUT "</table>\n";
158print HTMLOUT "<hr />\n";
159print HTMLOUT "<h2>Timing Chart (Gannt)</h2>\n";
160print 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'}, {});
161foreach 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}
169print HTMLOUT '<div>' . "\n";
170print HTMLOUT "</body>\n";
171print HTMLOUT "</html>";
172close(HTMLOUT);
173
174print "Done!\n";
175print "Complete!\n\n";
176exit;
177
178
179## @function debugPrint()
180#
181sub 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#
194sub 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#
206sub 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#
220sub 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#
253sub renderLine
254{
255 my ($table_width, $start, $end, $class, $tname, $tstart, $tend, $jobs) = @_;
256 &debugPrint("renderLine($table_width, $start, $end, $class, $tname, $tstart, $tend, <jobs>)");
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;
272 # - right offset directly subtracted from width
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 }
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
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;
299 my $jleft = int(($jleft_percent * $table_width) + 0.5);
300 my $jwidth_percent = $jduration / $duration;
301 my $jwidth = int(($jwidth_percent * $table_width) + 0.5);
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;
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">&nbsp;</span><span class="label">' . $jobs->{$jstart}->{'FN'} . '</span></div>';
312 }
313 return $html;
314}
315## renderLine() ##
316
317
318## @function renderTime()
319#
320sub 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}
Note: See TracBrowser for help on using the repository browser.