18
18
_arch_std_events = {}
19
19
# Track whether an events table is currently being defined and needs closing.
20
20
_close_table = False
21
+ # Events to write out when the table is closed
22
+ _pending_events = []
21
23
22
24
23
25
def removesuffix (s : str , suffix : str ) -> str :
@@ -128,6 +130,7 @@ def unit_to_pmu(unit: str) -> Optional[str]:
128
130
eventcode |= int (jd ['ExtSel' ]) << 8
129
131
configcode = int (jd ['ConfigCode' ], 0 ) if 'ConfigCode' in jd else None
130
132
self .name = jd ['EventName' ].lower () if 'EventName' in jd else None
133
+ self .topic = ''
131
134
self .compat = jd .get ('Compat' )
132
135
self .desc = fixdesc (jd .get ('BriefDescription' ))
133
136
self .long_desc = fixdesc (jd .get ('PublicDescription' ))
@@ -200,7 +203,7 @@ def __repr__(self) -> str:
200
203
s += f'\t { attr } = { value } ,\n '
201
204
return s + '}'
202
205
203
- def to_c_string (self , topic_local : str ) -> str :
206
+ def to_c_string (self ) -> str :
204
207
"""Representation of the event as a C struct initializer."""
205
208
206
209
def attr_string (attr : str , value : str ) -> str :
@@ -212,33 +215,35 @@ def str_if_present(self, attr: str) -> str:
212
215
return attr_string (attr , getattr (self , attr ))
213
216
214
217
s = '{\n '
215
- s += f'\t .topic = "{ topic_local } ",\n '
216
218
for attr in [
217
219
'aggr_mode' , 'compat' , 'deprecated' , 'desc' , 'event' , 'long_desc' ,
218
220
'metric_constraint' , 'metric_expr' , 'metric_group' , 'metric_name' ,
219
- 'name' , 'perpkg' , 'pmu' , 'unit'
221
+ 'name' , 'perpkg' , 'pmu' , 'topic' , ' unit'
220
222
]:
221
223
s += str_if_present (self , attr )
222
224
s += '},\n '
223
225
return s
224
226
225
227
226
- def read_json_events (path : str ) -> Sequence [JsonEvent ]:
228
+ def read_json_events (path : str , topic : str ) -> Sequence [JsonEvent ]:
227
229
"""Read json events from the specified file."""
228
230
229
231
try :
230
- return json .load (open (path ), object_hook = lambda d : JsonEvent ( d ) )
232
+ result = json .load (open (path ), object_hook = JsonEvent )
231
233
except BaseException as err :
232
234
print (f"Exception processing { path } " )
233
235
raise
236
+ for event in result :
237
+ event .topic = topic
238
+ return result
234
239
235
240
236
241
def preprocess_arch_std_files (archpath : str ) -> None :
237
242
"""Read in all architecture standard events."""
238
243
global _arch_std_events
239
244
for item in os .scandir (archpath ):
240
245
if item .is_file () and item .name .endswith ('.json' ):
241
- for event in read_json_events (item .path ):
246
+ for event in read_json_events (item .path , topic = '' ):
242
247
if event .name :
243
248
_arch_std_events [event .name .lower ()] = event
244
249
@@ -252,19 +257,36 @@ def print_events_table_prefix(tblname: str) -> None:
252
257
_close_table = True
253
258
254
259
255
- def print_events_table_entries (item : os .DirEntry , topic : str ) -> None :
256
- """Create contents of an events table."""
260
+ def add_events_table_entries (item : os .DirEntry , topic : str ) -> None :
261
+ """Add contents of file to _pending_events table."""
257
262
if not _close_table :
258
263
raise IOError ('Table entries missing prefix' )
259
- for event in read_json_events (item .path ):
260
- _args . output_file . write ( event . to_c_string ( topic ) )
264
+ for e in read_json_events (item .path , topic ):
265
+ _pending_events . append ( e )
261
266
262
267
263
268
def print_events_table_suffix () -> None :
264
269
"""Optionally close events table."""
270
+
271
+ def event_cmp_key (j : JsonEvent ):
272
+ def fix_none (s : str ):
273
+ if s is None :
274
+ return ''
275
+ return s
276
+
277
+ return (not j .desc is None , fix_none (j .topic ), fix_none (j .name ), fix_none (j .pmu ),
278
+ fix_none (j .metric_name ))
279
+
265
280
global _close_table
266
- if _close_table :
267
- _args .output_file .write ("""{
281
+ if not _close_table :
282
+ return
283
+
284
+ global _pending_events
285
+ for event in sorted (_pending_events , key = event_cmp_key ):
286
+ _args .output_file .write (event .to_c_string ())
287
+ _pending_events = []
288
+
289
+ _args .output_file .write ("""{
268
290
\t .name = 0,
269
291
\t .event = 0,
270
292
\t .desc = 0,
@@ -307,7 +329,7 @@ def is_leaf_dir(path: str) -> bool:
307
329
if not item .is_file () or not item .name .endswith ('.json' ):
308
330
return
309
331
310
- print_events_table_entries (item , get_topic (item .name ))
332
+ add_events_table_entries (item , get_topic (item .name ))
311
333
312
334
313
335
def print_mapping_table (archs : Sequence [str ]) -> None :
0 commit comments