1- from typing import Dict , List , Tuple
1+ from typing import Dict , List , Tuple , Any
22
33from .O3 import PipelineStage , Instruction
4- from .utils import stable_hash
54from .events import MetadataEvent , DurationEvent
65from .thread_pool import ThreadPoolManager
7- from .config import Config
6+ from .config import IConfig
87from .parser import PipeViewParser
98
109class ChromeTracingConverter :
11- def __init__ (self , parser : PipeViewParser , config : Config , exclude_exec : bool = False , exclude_pipeline : bool = False ):
10+ def __init__ (self , parser : PipeViewParser , config : IConfig , exclude_exec : bool = False , exclude_pipeline : bool = False ):
1211 self .parser : PipeViewParser = parser
13- self .config : Config = config
12+ if not isinstance (config , IConfig ):
13+ raise TypeError (f"Unexpetcted Config type { type (config ).__name__ } . Please, derive you configuration class from { IConfig } " )
14+ self .config : IConfig = config
1415
1516 self .exclude_exec : bool = exclude_exec
1617 self .exclude_pipeline : bool = exclude_pipeline
1718
1819 self .metadata_events : List [MetadataEvent ] = []
1920 self .duration_events : List [DurationEvent ] = []
2021
21- settings = config .settings
22- self .func_units = config .func_units
23- self .colors = config .colors
24-
25- self .PID_PIPELINE_STAGES_BASE = settings .PID_PIPELINE_STAGES_BASE
26- self .PID_EXECUTION_UNITS_BASE = settings .PID_EXECUTION_UNITS_BASE
27- self .MAX_PIPE_WIDTH = settings .MAX_PIPE_WIDTH
28- self .MAX_EXEC_UNIT_WIDTH = settings .MAX_EXEC_UNIT_WIDTH
29-
30- self .default_colors = self .colors .default
31- self .stage_names = config .stage_names
32-
3322 self .stage_managers : Dict [PipelineStage , ThreadPoolManager ] = {}
34- self .exec_unit_managers : Dict [str , ThreadPoolManager ] = {}
23+ self .func_units_managers : Dict [str , ThreadPoolManager ] = {}
3524
3625 def convert (self ) -> List [dict ]:
3726 self ._add_metadata ()
@@ -46,15 +35,6 @@ def convert(self) -> List[dict]:
4635 def instructions_by_seq_num (self ):
4736 return sorted (self .parser .instructions .values (), key = lambda x : x .seq_num )
4837
49- def _opclass_to_unit (self , opclass : str ) -> str :
50- return self .func_units .get (opclass , "No_OpClass" )
51-
52- def _get_cname_for_instruction (self , instr : Instruction ) -> str :
53- unit = self ._opclass_to_unit (instr .opclass )
54- family = self .colors .get (unit , self .default_colors )
55- idx = stable_hash (instr .mnemonic , len (family ))
56- return family [idx ]
57-
5838 def _add_metadata (self ):
5939 if not self .exclude_pipeline :
6040 self ._add_pipeline_stages_metadata ()
@@ -63,12 +43,12 @@ def _add_metadata(self):
6343
6444 def _add_pipeline_stages_metadata (self ):
6545 for id , stage in enumerate (PipelineStage .order ()):
66- stage_name = self .stage_names [ stage . value ]
46+ stage_name = self .config . get_stage_name ( stage )
6747 process_name = f"{ (id + 1 ):02d} _{ stage_name } "
68- pid = self .PID_PIPELINE_STAGES_BASE + id
48+ pid = self .config . pipeline_pid + id
6949
7050 manager = ThreadPoolManager (
71- max_width = self .MAX_PIPE_WIDTH ,
51+ max_width = self .config . pipeline_width ,
7252 pid = pid ,
7353 thread_name_prefix = stage_name ,
7454 metadata_events = self .metadata_events
@@ -96,18 +76,18 @@ def _add_execution_units_metadata(self):
9676 unit_names = set ()
9777 for instr in self .instructions_by_seq_num ():
9878 if instr .opclass :
99- unit_names .add (self ._opclass_to_unit (instr .opclass ))
79+ unit_names .add (self .config . get_func_unit (instr .opclass ))
10080
10181 for i , unit_name in enumerate (sorted (unit_names )):
102- pid = self .PID_EXECUTION_UNITS_BASE + i
82+ pid = self .config . func_units_pid + i
10383
10484 manager = ThreadPoolManager (
105- max_width = self .MAX_EXEC_UNIT_WIDTH ,
85+ max_width = self .config . func_units_width ,
10686 pid = pid ,
10787 thread_name_prefix = unit_name ,
10888 metadata_events = self .metadata_events
10989 )
110- self .exec_unit_managers [unit_name ] = manager
90+ self .func_units_managers [unit_name ] = manager
11191 manager .add_initial_thread (0 )
11292
11393 self .metadata_events .append (MetadataEvent (
@@ -128,12 +108,12 @@ def _add_execution_units_metadata(self):
128108 def _assign_thread_for_stage (self , stage : PipelineStage , start_time : int , end_time : int ) -> Tuple [int , int ]:
129109 return self .stage_managers [stage ].assign_thread (start_time , end_time )
130110
131- def _assign_thread_for_exec_unit (self , unit_name : str , start_time : int , end_time : int ) -> Tuple [int , int ]:
132- return self .exec_unit_managers [unit_name ].assign_thread (start_time , end_time )
111+ def _assign_thread_for_func_units (self , unit_name : str , start_time : int , end_time : int ) -> Tuple [int , int ]:
112+ return self .func_units_managers [unit_name ].assign_thread (start_time , end_time )
133113
134114 def _add_pipeline_stage_events (self , instr : Instruction ):
135115 mnemonic = instr .mnemonic
136- cname = self ._get_cname_for_instruction (instr )
116+ cname = self .config . get_color_for_instr (instr )
137117
138118 active = [(st , instr .stages [st ]) for st in instr .stage_order if instr .stages .get (st , 0 ) > 0 ]
139119 if not active :
@@ -147,7 +127,7 @@ def _add_pipeline_stage_events(self, instr : Instruction):
147127
148128 self .duration_events .append (DurationEvent (
149129 name = mnemonic ,
150- cat = self .stage_names [ stage . value ] ,
130+ cat = self .config . get_stage_name ( stage ) ,
151131 ts = tick ,
152132 dur = dur ,
153133 pid = pid ,
@@ -156,7 +136,7 @@ def _add_pipeline_stage_events(self, instr : Instruction):
156136 args = {
157137 "PC" : instr .pc ,
158138 "SeqNum" : instr .seq_num ,
159- "Stage" : self .stage_names [ stage . value ] ,
139+ "Stage" : self .config . get_stage_name ( stage ) ,
160140 "OpClass" : instr .opclass ,
161141 "Disasm" : instr .disasm
162142 }
@@ -173,11 +153,11 @@ def _add_execution_unit_events(self, instr : Instruction):
173153 if issue <= 0 or complete <= 0 or issue >= complete :
174154 return
175155
176- unit = self ._opclass_to_unit (instr .opclass )
177- if unit not in self .exec_unit_managers :
156+ unit = self .config . get_func_unit (instr .opclass )
157+ if unit not in self .func_units_managers :
178158 return
179159
180- pid , tid = self ._assign_thread_for_exec_unit (unit , issue , complete )
160+ pid , tid = self ._assign_thread_for_func_units (unit , issue , complete )
181161 dur = complete - issue
182162
183163 self .duration_events .append (DurationEvent (
@@ -187,7 +167,7 @@ def _add_execution_unit_events(self, instr : Instruction):
187167 dur = dur ,
188168 pid = pid ,
189169 tid = tid ,
190- cname = self ._get_cname_for_instruction (instr ),
170+ cname = self .config . get_color_for_instr (instr ),
191171 args = {
192172 "PC" : instr .pc ,
193173 "SeqNum" : instr .seq_num ,
0 commit comments