source: gs2-extensions/parallel-building/trunk/src/bin/script/hadoop_report.pl@ 28356

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

Support the legacy version of taskno in the data_locality.csv file (we now have a complete taskid)

  • Property svn:executable set to *
File size: 10.1 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6# Requires setup.bash to have been sourced
7BEGIN
8{
9 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
10 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
11 # Ensure Greenstone Perl locations are in INC
12 unshift (@INC, $ENV{'GSDLHOME'} . '/perllib');
13 unshift (@INC, $ENV{'GSDLHOME'} . '/perllib/cpan');
14 # we'll need the perl version number
15 my ($version_number) = `perl-version.pl`;
16 if (defined $ENV{'GSDLEXTS'})
17 {
18 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
19 foreach my $e (@extensions)
20 {
21 my $ext_prefix = $ENV{'GSDLHOME'} . '/ext/' . $e;
22 unshift (@INC, $ext_prefix . '/perllib');
23 unshift (@INC, $ext_prefix . '/perllib/cpan');
24 unshift (@INC, $ext_prefix . '/' . $ENV{'GSDLOS'} . '/lib/perl/' . $version_number);
25 }
26 }
27}
28
29use Devel::Peek;
30use Sort::Key::Natural qw(natsort);
31use Time::Local;
32
33print "\n===== GS Hadoop Report =====\n";
34print "Generate a report (CSV) from the output of a parallel processing Green-\n";
35print "stone import, suitable for feeding into the Gantt chart generator.\n";
36print "============================\n\n";
37
38# Configuration
39if (!defined $ARGV[0] || !-d $ARGV[0])
40{
41 die("usage: hadoop_report.pl <path to results>\n");
42}
43my $results_path = $ARGV[0];
44
45# Read in hadoop.log and parse top level record
46print ' * Reading and parsing "hadoop.log"... ';
47my $job_record = {'host'=>'', 'job'=>'', 'start'=>0, 'end'=>0, 'cpu_time'=>0};
48my $hadoop_log_path = $results_path . '/hadoop.log';
49if (open(HLIN, '<:utf8', $hadoop_log_path))
50{
51 while (my $line = <HLIN>)
52 {
53 if ($line =~ /host:(.+)/)
54 {
55 $job_record->{'host'} = $1;
56 }
57 elsif ($line =~ /Running job: job_(\d+_\d+)/)
58 {
59 $job_record->{'job'} = $1;
60 }
61 elsif ($line =~ /CPU time spent \(ms\)=(\d+)/)
62 {
63 $job_record->{'cpu_time'} = $1;
64 }
65 elsif ($job_record->{'start'} == 0 && $line =~ /(\d\d)\/(\d\d)\/(\d\d) (\d\d):(\d\d):(\d\d)/)
66 {
67 $job_record->{'start'} = timelocal($6, $5, $4, $3, ($2 - 1), $1);
68 }
69 elsif ($line =~ /(\d\d)\/(\d\d)\/(\d\d) (\d\d):(\d\d):(\d\d)/)
70 {
71 my $end = timelocal($6, $5, $4, $3, ($2 - 1), $1);
72 if ($end > $job_record->{'end'})
73 {
74 $job_record->{'end'} = $end;
75 }
76 }
77 }
78 close(HLIN);
79 if ($job_record->{'start'} == 0 || $job_record->{'end'} == 0)
80 {
81 die('Error! Failed to parse timing information from log: ' . $hadoop_log_path);
82 }
83}
84else
85{
86 die('Error! Failed to open file for reading: ' . $hadoop_log_path);
87}
88print "Done!\n";
89
90# Read in data_locality.csv (will be matched to task logs)
91my $data_was_local = {};
92my $data_locality_csv_path = $results_path . '/data_locality.csv';
93if (-f $data_locality_csv_path)
94{
95 print ' * Reading and parsing "data_locality.csv"... ';
96 if (open(DLIN, '<:utf8', $data_locality_csv_path))
97 {
98 while (my $line = <DLIN>)
99 {
100 # note that the line may begin with a taskid or just a taskno (legacy)
101 if ($line =~ /(\d+),\d,(\d)/)
102 {
103 $data_was_local->{$1} = $2;
104 }
105 }
106 close(DLIN);
107 }
108 else
109 {
110 die('Error! Failed to open file for reading: ' . $data_locality_csv_path);
111 }
112 print "Done!\n";
113}
114else
115{
116 print " * Data locality not available or not applicable\n";
117}
118
119# Read in all task logs and parse task records
120my $task_records;
121print " * Locating task logs...\n";
122if (opendir(DH, $results_path))
123{
124 my @files = readdir(DH);
125 foreach my $file (sort @files)
126 {
127 if ($file =~ /import-hadoop-(\d+_\d+)_m_(\d+)_\d+\.log/)
128 {
129 my $job_no = $1;
130 my $task_no = $2;
131 my $is_data_local = 0;
132 if (defined ($data_was_local->{$task_no}))
133 {
134 $is_data_local = $data_was_local->{$task_no};
135 }
136 my $task_record = {'host'=>'', 'cpu'=>0, 'job' => $job_no, 'task' => $task_no, 'start'=>0, 'end'=>0, 'cpu_time'=>0, 'data_locality'=>$is_data_local, 'file'=>'', 'percom'=>'NA'};
137 print ' - Reading and parsing "' . $file . '"... ';
138 my $task_log_path = $results_path . '/' . $file;
139 my $io_time = 0;
140 if (open(TIN, '<:utf8', $task_log_path))
141 {
142 my $io_start_time = 0;
143 while (my $line = <TIN>)
144 {
145 if ($line =~ /\[Started:(\d+(?:\.\d+)?)\]/)
146 {
147 $task_record->{'start'} = $1;
148 }
149 elsif ($line =~ /\[Host:([^\]]+)\]/)
150 {
151 $task_record->{'host'} = $1;
152 }
153 elsif ($line =~ /\[CPU:(\d+)\]/)
154 {
155 $task_record->{'cpu'} = $1;
156 }
157 elsif ($line =~ /\[Map:([^\>]+)=>/)
158 {
159 $task_record->{'file'} = $1;
160 }
161 elsif ($line =~ /\[IOS:(\d+)\]/)
162 {
163 $io_start_time = $1;
164 }
165 elsif ($io_start_time > 0 && $line =~ /\[IOE:(\d+)\]/)
166 {
167 $io_time += ($1 - $io_start_time);
168 $io_start_time = 0;
169 }
170 elsif ($line =~ /\[Completed:(\d+(?:\.\d+)?)\]/)
171 {
172 my $end_time = $1;
173 $task_record->{'end'} = $end_time;
174 if ($io_start_time > 0)
175 {
176 $io_time += ($end_time - $io_start_time);
177 }
178 }
179 }
180 close(TIN);
181 # Calculate CPU time (total time - IO time)
182 $task_record->{'cpu_time'} = $task_record->{'end'} - $task_record->{'start'} - $io_time;
183
184 # We should now have the filename - use this and try and locate a
185 # convert log for this item (assuming it is multimedia, which it may
186 # not be)
187 if (defined $task_record->{'file'} && $task_record->{'file'} =~ /\/([^\/]+)\.ts/)
188 {
189 my $filename_sans_extension = $1;
190 my $convert_log = $results_path . '/convert-' . $filename_sans_extension . '.log';
191 if (-f $convert_log)
192 {
193 print '[Reading and parsing convert log]... ';
194 if (open(CLIN, '<:utf8', $convert_log))
195 {
196 my $max_percent = 0.00;
197 while (my $line = <CLIN>)
198 {
199 if ($line =~ /.*Encoding: task 1 of 1, (\d+\.\d\d) \%/)
200 {
201 my $percent = $1;
202 if ($percent > $max_percent)
203 {
204 $max_percent = $percent;
205 }
206 }
207 }
208 close(CLIN);
209 $task_record->{'percom'} = $max_percent;
210 }
211 else
212 {
213 print STDERR "Warning! Failed to open log file for reading: " . $convert_log . "\n";
214 }
215 }
216 }
217
218 # Store this record
219 $task_records->{$task_no} = $task_record;
220 }
221 else
222 {
223 die('Error! Failed to open file for reading: ' . $task_log_path);
224 }
225 print "Done!\n";
226 }
227 }
228 close(DH);
229}
230else
231{
232 die('Error! Failed to open directory for reading: ' . $results_path);
233}
234print ' - Processed ' . scalar(keys(%{$task_records})) . " records\n";
235
236# Generate compute-node records
237print ' * Generating compute node records... ';
238my $node_records;
239foreach my $taskno (sort keys %{$task_records})
240{
241 my $task_record = $task_records->{$taskno};
242 my $node_record = {'host'=>'', 'cpu'=>0, 'job' => '', 'start'=>0, 'end'=>0, 'cpu_time'=>0};
243 # - retrieve any existing record
244 my $worker_id = $task_record->{'host'} . '#' . $task_record->{'cpu'};
245 if (defined $node_records->{$worker_id})
246 {
247 $node_record = $node_records->{$worker_id};
248 }
249 if ($node_record->{'host'} eq '')
250 {
251 $node_record->{'host'} = $task_record->{'host'};
252 }
253 if ($node_record->{'cpu'} == 0)
254 {
255 $node_record->{'cpu'} = $task_record->{'cpu'};
256 }
257 if ($node_record->{'job'} eq '')
258 {
259 $node_record->{'job'} = $task_record->{'job'};
260 }
261 if ($node_record->{'start'} == 0 || $task_record->{'start'} < $node_record->{'start'})
262 {
263 $node_record->{'start'} = $task_record->{'start'};
264 }
265 if ($node_record->{'end'} == 0 || $task_record->{'end'} > $node_record->{'end'})
266 {
267 $node_record->{'end'} = $task_record->{'end'};
268 }
269 $node_record->{'cpu_time'} += $task_record->{'cpu_time'};
270 # - store it
271 $node_records->{$worker_id} = $node_record;
272}
273print "Done!\n";
274
275# Write out CSV of all information
276my $report_csv_path = $results_path . '/timing.csv';
277if (open(CSVOUT, '>:utf8', $report_csv_path))
278{
279 my $row_counter = 1;
280 # Header
281 print CSVOUT "id,name,hostname,start,end,cputime,dl,pid,filename,percom\n";
282 # Master Record
283 print CSVOUT $row_counter . ',M0,' . $job_record->{'host'} . ',' . $job_record->{'start'} . ',' . $job_record->{'end'} . ',' . ($job_record->{'cpu_time'} / 1000) . ",0,0,NA,NA\n";
284 $row_counter++;
285 # For each compute node record
286 my $known_workers = {};
287 foreach my $worker_id (natsort(keys(%{$node_records})))
288 {
289 my $node_record = $node_records->{$worker_id};
290 my $node_id = $row_counter;
291 $row_counter++;
292 my $csv_worker_id = 'W' . $node_record->{'cpu'};
293 # Ensure we haven't used this id before - this should never trigger for
294 # multicore CPUs, but will for clusters (as nearly all nodes will report
295 # themselves as 'W0')
296 if (defined $known_workers->{$csv_worker_id})
297 {
298 # Find a different worker id as this one is already in use
299 my $counter = 0;
300 $csv_worker_id = 'W' . $counter;
301 while (defined $known_workers->{$csv_worker_id})
302 {
303 $counter++;
304 $csv_worker_id = 'W' . $counter;
305 }
306 }
307 $known_workers->{$csv_worker_id} = 1;
308 print CSVOUT $node_id . ',' . $csv_worker_id . ',' . $node_record->{'host'} . ',' . $node_record->{'start'} . ',' . $node_record->{'end'} . ',' . $node_record->{'cpu_time'} . ",0,1,NA,NA\n";
309 # List the child task records
310 foreach my $taskno (sort keys %{$task_records})
311 {
312 my $task_record = $task_records->{$taskno};
313 if ($task_record->{'host'} . '#' . $task_record->{'cpu'} eq $worker_id)
314 {
315 print CSVOUT $row_counter . ',T' . ($task_record->{'task'} + 0) . ',' . $task_record->{'host'} . ',' . $task_record->{'start'} . ',' . $task_record->{'end'} . ',' . $task_record->{'cpu_time'} . ',' . $task_record->{'data_locality'} . ',' . $node_id . ',' . $task_record->{'file'} . ',' . $task_record->{'percom'} . "\n";
316 $row_counter++;
317 }
318 }
319 }
320 close(CSVOUT);
321}
322else
323{
324 die('Error! Failed to open file for writing: ' . $report_csv_path);
325}
326
327print "Complete!\n\n";
328exit;
329
Note: See TracBrowser for help on using the repository browser.