11#!/usr/bin/env python3
22
33# Copyright © 2019-2023
4- #
4+ #
55# Licensed under the Apache License, Version 2.0 (the "License");
66# you may not use this file except in compliance with the License.
77# You may obtain a copy of the License at
88# http://www.apache.org/licenses/LICENSE-2.0
9- #
9+ #
1010# Unless required by applicable law or agreed to in writing, software
1111# distributed under the License is distributed on an "AS IS" BASIS,
1212# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1717import argparse
1818import csv
1919import re
20-
20+
2121def parse_args ():
2222 parser = argparse .ArgumentParser (description = 'CPU trace log to CSV format converter.' )
2323 parser .add_argument ('-t' , '--type' , default = 'simx' , help = 'log type (rtlsim or simx)' )
2424 parser .add_argument ('-o' , '--csv' , default = 'trace.csv' , help = 'Output CSV file' )
2525 parser .add_argument ('log' , help = 'Input log file' )
2626 return parser .parse_args ()
2727
28- def parse_simx (log_filename ):
28+ def parse_simx (log_filename ):
2929 pc_pattern = r"PC=(0x[0-9a-fA-F]+)"
3030 instr_pattern = r"Instr (0x[0-9a-fA-F]+):"
3131 opcode_pattern = r"Instr 0x[0-9a-fA-F]+: ([0-9a-zA-Z_\.]+)"
@@ -42,12 +42,12 @@ def parse_simx(log_filename):
4242 if line .startswith ("DEBUG Fetch:" ):
4343 if instr_data :
4444 entries .append (instr_data )
45- instr_data = {}
45+ instr_data = {}
4646 instr_data ["lineno" ] = lineno
4747 instr_data ["PC" ] = re .search (pc_pattern , line ).group (1 )
4848 instr_data ["core_id" ] = re .search (core_id_pattern , line ).group (1 )
4949 instr_data ["warp_id" ] = re .search (warp_id_pattern , line ).group (1 )
50- instr_data ["tmask" ] = re .search (tmask_pattern , line ).group (1 )
50+ instr_data ["tmask" ] = re .search (tmask_pattern , line ).group (1 )
5151 instr_data ["uuid" ] = re .search (uuid_pattern , line ).group (1 )
5252 elif line .startswith ("DEBUG Instr" ):
5353 instr_data ["instr" ] = re .search (instr_pattern , line ).group (1 )
@@ -60,13 +60,13 @@ def parse_simx(log_filename):
6060 if instr_data :
6161 entries .append (instr_data )
6262 return entries
63-
63+
6464def reverse_binary (bin_str ):
6565 return bin_str [::- 1 ]
6666
6767def bin_to_array (bin_str ):
6868 return [int (bit ) for bit in bin_str ]
69-
69+
7070def append_reg (text , value , sep ):
7171 if sep :
7272 text += ", "
@@ -77,14 +77,7 @@ def append_reg(text, value, sep):
7777 text += "x" + value
7878 sep = True
7979 return text , sep
80-
81- def append_imm (text , value , sep ):
82- if sep :
83- text += ", "
84- text += value
85- sep = True
86- return text , sep
87-
80+
8881def append_value (text , reg , value , tmask_arr , sep ):
8982 text , sep = append_reg (text , reg , sep )
9083 text += "={"
@@ -97,8 +90,8 @@ def append_value(text, reg, value, tmask_arr, sep):
9790 text += "-"
9891 text += "}"
9992 return text , sep
100-
101- def parse_rtlsim (log_filename ):
93+
94+ def parse_rtlsim (log_filename ):
10295 line_pattern = r"\d+: core(\d+)-(decode|issue|commit)"
10396 pc_pattern = r"PC=(0x[0-9a-fA-F]+)"
10497 instr_pattern = r"instr=(0x[0-9a-fA-F]+)"
@@ -108,8 +101,6 @@ def parse_rtlsim(log_filename):
108101 tmask_pattern = r"tmask=(\d+)"
109102 wb_pattern = r"wb=(\d)"
110103 opds_pattern = r"opds=(\d+)"
111- use_imm_pattern = r"use_imm=(\d)"
112- imm_pattern = r"imm=(0x[0-9a-fA-F]+)"
113104 rd_pattern = r"rd=(\d+)"
114105 rs1_pattern = r"rs1=(\d+)"
115106 rs2_pattern = r"rs2=(\d+)"
@@ -120,24 +111,24 @@ def parse_rtlsim(log_filename):
120111 rd_data_pattern = r"data=\{(.+?)\}"
121112 eop_pattern = r"eop=(\d)"
122113 uuid_pattern = r"#(\d+)"
123- entries = []
114+ entries = []
124115 with open (log_filename , 'r' ) as log_file :
125116 instr_data = {}
126117 for lineno , line in enumerate (log_file , start = 1 ):
127118 line_match = re .search (line_pattern , line )
128119 if line_match :
129120 PC = re .search (pc_pattern , line ).group (1 )
130- warp_id = re .search (warp_id_pattern , line ).group (1 )
121+ warp_id = re .search (warp_id_pattern , line ).group (1 )
131122 tmask = re .search (tmask_pattern , line ).group (1 )
132- uuid = re .search (uuid_pattern , line ).group (1 )
123+ uuid = re .search (uuid_pattern , line ).group (1 )
133124 core_id = line_match .group (1 )
134125 stage = line_match .group (2 )
135- if stage == "decode" :
126+ if stage == "decode" :
136127 trace = {}
137128 trace ["uuid" ] = uuid
138- trace ["PC" ] = PC
129+ trace ["PC" ] = PC
139130 trace ["core_id" ] = core_id
140- trace ["warp_id" ] = warp_id
131+ trace ["warp_id" ] = warp_id
141132 trace ["tmask" ] = reverse_binary (tmask )
142133 trace ["instr" ] = re .search (instr_pattern , line ).group (1 )
143134 trace ["opcode" ] = re .search (op_pattern , line ).group (1 )
@@ -146,8 +137,6 @@ def parse_rtlsim(log_filename):
146137 trace ["rs1" ] = re .search (rs1_pattern , line ).group (1 )
147138 trace ["rs2" ] = re .search (rs2_pattern , line ).group (1 )
148139 trace ["rs3" ] = re .search (rs3_pattern , line ).group (1 )
149- trace ["use_imm" ] = re .search (use_imm_pattern , line ).group (1 ) == "1"
150- trace ["imm" ] = re .search (imm_pattern , line ).group (1 )
151140 instr_data [uuid ] = trace
152141 elif stage == "issue" :
153142 if uuid in instr_data :
@@ -162,7 +151,7 @@ def parse_rtlsim(log_filename):
162151 trace ["rs3_data" ] = re .search (rs3_data_pattern , line ).group (1 ).split (', ' )[::- 1 ]
163152 trace ["issued" ] = True
164153 instr_data [uuid ] = trace
165- elif stage == "commit" :
154+ elif stage == "commit" :
166155 if uuid in instr_data :
167156 trace = instr_data [uuid ]
168157 if "issued" in trace :
@@ -205,16 +194,14 @@ def parse_rtlsim(log_filename):
205194 del trace ["rs1" ]
206195 del trace ["rs2" ]
207196 del trace ["rs3" ]
208- del trace ["use_imm" ]
209- del trace ["imm" ]
210- del trace ["issued" ]
197+ del trace ["issued" ]
211198 del instr_data [uuid ]
212199 entries .append (trace )
213- return entries
200+ return entries
214201
215202def write_csv (log_filename , csv_filename , log_type ):
216203 entries = None
217-
204+
218205 # parse log file
219206 if log_type == "rtlsim" :
220207 entries = parse_rtlsim (log_filename )
@@ -223,19 +210,19 @@ def write_csv(log_filename, csv_filename, log_type):
223210 else :
224211 print ('Error: invalid log type' )
225212 sys .exit ()
226-
213+
227214 # sort entries by uuid
228215 entries .sort (key = lambda x : (int (x ['uuid' ])))
229216 for entry in entries :
230217 del entry ['lineno' ]
231-
218+
232219 # write to CSV
233220 with open (csv_filename , 'w' , newline = '' ) as csv_file :
234221 fieldnames = ["uuid" , "PC" , "opcode" , "instr" , "core_id" , "warp_id" , "tmask" , "operands" , "destination" ]
235222 writer = csv .DictWriter (csv_file , fieldnames = fieldnames )
236223 writer .writeheader ()
237224 for entry in entries :
238- writer .writerow (entry )
225+ writer .writerow (entry )
239226
240227def main ():
241228 args = parse_args ()
0 commit comments