1# Copyright 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4import json 5import os 6import sys 7import re 8 9from perf_insights import map_single_trace 10from perf_insights.mre import failure 11import perf_insights_project 12import vinn 13 14_REDUCE_MAP_RESULTS_CMDLINE_PATH = os.path.join( 15 perf_insights_project.PerfInsightsProject.perf_insights_src_path, 16 'mre', 'reduce_map_results_cmdline.html') 17 18 19def ReduceMapResults(job_results, key, file_handle, job): 20 project = perf_insights_project.PerfInsightsProject() 21 22 all_source_paths = list(project.source_paths) 23 all_source_paths.append(project.perf_insights_root_path) 24 25 with file_handle.PrepareFileForProcessing() as prepared_file_handle: 26 js_args = [ 27 key, 28 json.dumps(prepared_file_handle.AsDict()), 29 json.dumps(job.AsDict()), 30 ] 31 32 res = vinn.RunFile(_REDUCE_MAP_RESULTS_CMDLINE_PATH, 33 source_paths=all_source_paths, js_args=js_args) 34 35 if res.returncode != 0: 36 try: 37 sys.stderr.write(res.stdout) 38 except Exception: 39 pass 40 job_results.AddFailure(failure.Failure( 41 job, job.map_function_handle, None, 'Error', 42 'vinn runtime error while reducing results.', 'Unknown stack')) 43 return 44 45 for line in res.stdout.split('\n'): 46 m = re.match('^JOB_(RESULTS|FAILURE): (.+)', line, re.DOTALL) 47 if m: 48 found_type = m.group(1) 49 found_dict = json.loads(m.group(2)) 50 if found_type == 'FAILURE': 51 try: 52 sys.stderr.write(res.stdout) 53 except Exception: 54 pass 55 job_results.AddFailure(failure.Failure( 56 job, job.map_function_handle, None, 'Error', 57 'vinn runtime error while reducing results.', 'Unknown stack')) 58 59 elif found_type == 'RESULTS': 60 job_results.AddPair(key, found_dict[key]) 61 else: 62 if len(line) > 0: 63 sys.stderr.write(line) 64 sys.stderr.write('\n') 65 66 if len(job_results.pairs) == 0 and len(job_results.failures) == 0: 67 raise map_single_trace.InternalMapError( 68 'Internal error: No results were produced!') 69