1010import platform
1111import unidiff
1212import argparse
13+ import shutil
14+ import glob
1315
1416def extract_test_case_output (cmake_root_path , test_case_name , scenario ):
1517 # Construct the path to the expected output file within the "gold" folder
@@ -38,7 +40,8 @@ def call_unidiff(scenario, output_dir, output_file_with_pid, expected_output_fil
3840 device_timeline_ref = unidiff .DeviceTimeline (ref_path )
3941 result = device_timeline_cur .compare (device_timeline_ref )
4042 print (f"[INFO] return code : { result } " )
41- device_timeline_cur .save_to_csv (device_timeline_ref , filename = "device-timeline_results.csv" )
43+ output_filename = os .path .join (output_dir , "device-timeline_results.csv" )
44+ device_timeline_cur .save_to_csv (device_timeline_ref , filename = output_filename )
4245 print (f"[INFO] Device timeline comparison complete. Result: { 'Passed' if result == 0 else 'Failed' } " )
4346 return result
4447
@@ -48,7 +51,8 @@ def call_unidiff(scenario, output_dir, output_file_with_pid, expected_output_fil
4851 device_timing_ref = unidiff .DeviceTiming (ref_path )
4952 result = device_timing_cur .compare (device_timing_ref )
5053 print (f"[INFO] return code : { result } " )
51- device_timing_cur .save_to_csv (device_timing_ref , filename = "device_timing_results.csv" )
54+ output_filename = os .path .join (output_dir , "device_timing_results.csv" )
55+ device_timing_cur .save_to_csv (device_timing_ref , filename = output_filename )
5256 print (f"[INFO] Device timing comparison complete. Result: { 'Passed' if result == 0 else 'Failed' } " )
5357 return result
5458
@@ -88,22 +92,78 @@ def run_unitrace(cmake_root_path, scenarios, test_case_name, args, extra_test_pr
8892 scenario_set = set (scenarios .split (" " ))
8993 for scenario in scenarios .split (" " ):
9094 command .append (scenario )
91- command += ["-o" , output_file_path , test_case ] + args
95+
96+ is_chrome_logging_present = False
97+ if scenario .startswith ("--chrome" ):
98+ is_chrome_logging_present = True
99+
100+ need_output_directory = False
101+ if scenario in ["--chrome-call-logging" , "--chrome-device-logging" ,"--chrome-kernel-logging" , "--chrome-sycl-logging" , "--chrome-itt-logging" ]:
102+ need_output_directory = True
103+
104+ if need_output_directory :
105+ command += ["--output-dir-path" , output_dir ]
106+ elif scenario in ["-k" , "-q" ]:
107+ command += ["-o" , "metric" ]
108+ else :
109+ command += ["-o" , output_file_path ]
110+
111+ command += [test_case ] + args
92112
93113 print (f"[INFO] Executing command: { ' ' .join (command )} " )
94114
95115 try :
116+ before_list_of_files = set (os .listdir (output_dir ))
96117 result = subprocess .run (command , text = True , capture_output = True ) # executing Unitrace command
97118 if result .returncode != 0 :
98119 print (f"[ERROR] Unitrace execution failed with return code { result .returncode } ." , file = sys .stderr )
99120 return 1 # Indicate failure due to Unitrace ERROR
100121
122+ # Metric logs need to be moved to result folder
123+ if scenario in ["-k" , "-q" ]:
124+ test_case_path = os .path .join (cmake_root_path , "build" , test_case_name )
125+ # Find all files starting with "metric" in source directory
126+ pattern = os .path .join (test_case_path , "metric*" )
127+ metric_files = glob .glob (pattern )
128+
129+ # Move each file to destination
130+ for file_path in metric_files :
131+ filename = os .path .basename (file_path )
132+ destination_path = os .path .join (output_dir , filename )
133+ shutil .move (file_path , destination_path )
134+ os .rename (destination_path , output_dir + "/" + filename + "_" + output_file )
135+
136+ after_list_of_files = set (os .listdir (output_dir ))
137+ new_files = list (after_list_of_files - before_list_of_files )
138+ # check if scenario is "chrome" logging
139+ # then test needs to modify the file name to reflect right test case.
140+ if is_chrome_logging_present :
141+ for idx , new_file in enumerate (new_files ):
142+ new_file = os .path .join (output_dir , new_file )
143+ new_file_name = ""
144+
145+ new_file_name += "_" + os .path .basename (new_file )
146+ new_name = os .path .join (output_dir , "output" ) + scenarios .replace (" " ,"" ).replace ("--" , "_" ).replace ("-" , "_" ).replace (" " ,"" ) + new_file_name
147+ try :
148+ os .rename (new_file , new_name )
149+ except Exception as e :
150+ print (f"[ERROR] Occurred while renaming the output file: { e } " , file = sys .stderr )
151+ return 1
152+ new_files [idx ] = os .path .basename (new_name )
153+
101154 # Check if the output file is generated
102155 output_files = []
103- for f in os . listdir ( output_dir ) :
156+ for f in new_files :
104157 result_file_pattern = f .split ("." )
105- if output_file .startswith (result_file_pattern [0 ]):
158+ if (is_chrome_logging_present and "chrome" in result_file_pattern [0 ]
159+ and test_case_name .replace ("/" ,"_" ).split ("." )[0 ] in result_file_pattern [0 ]
160+ ):
161+ output_files .append (f )
162+ elif scenario in ["-k" , "-q" ] and f .endswith (output_file ):
163+ output_files .append (f )
164+ elif output_file .startswith (result_file_pattern [0 ]):
106165 output_files .append (f )
166+ if len (output_files ) > 0 :
107167 break
108168 if output_files :
109169 print (f"[INFO] Output file '{ output_files [0 ]} ' generated successfully." )
@@ -141,6 +201,14 @@ def run_unitrace(cmake_root_path, scenarios, test_case_name, args, extra_test_pr
141201 return 1 # extra test prog failed
142202 else :
143203 return 0
204+ elif not scenario_set .isdisjoint (set (["-k" , "-q" ])):
205+ min_no_of_lines = 4 # Metric files has initial few lines for headers etc..
206+ for output_file in output_files :
207+ with open (os .path .join (output_dir , output_file ), 'r' ) as outfile :
208+ # Check to make sure file has counter values generated.
209+ if len (outfile .readlines ()) <= min_no_of_lines :
210+ print (f"[ERROR] Metric file '{ output_file } ' is not having counters present." )
211+ return 1 # Metric file should have more than 4 line present.
144212 elif scenario_set .isdisjoint (set (["--device-timing" , "-d" , "--device-timeline" , "-t" ])):
145213 print (f"[INFO] Nothing to compare for { scenario_set } hence exiting early." )
146214 # check if unidiff support validation for current scenario else return early as success
0 commit comments