Skip to content

Commit 7b2f844

Browse files
captain5050acmel
authored andcommitted
perf jevents: Sort JSON files entries
Sort the JSON files entries on conversion to C. The sort order tries to replicated cmp_sevent from pmu.c so that the input there is already sorted except for sysfs events. Specifically, the sort order is given by the tuple: (not j.desc is None, fix_none(j.topic), fix_none(j.name), fix_none(j.pmu), fix_none(j.metric_name)) which is putting events with descriptions and topics before those without, then sorting by name, then pmu and finally metric_name Add the topic to JsonEvent on reading to simplify. Remove an unnecessary lambda in the JSON reading. Signed-off-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: John Garry <[email protected]> Cc: Kan Liang <[email protected]> Cc: Leo Yan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Mike Leach <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Ravi Bangoria <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Will Deacon <[email protected]> Cc: Xing Zhengjun <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent ee2ce6f commit 7b2f844

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

tools/perf/pmu-events/jevents.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
_arch_std_events = {}
1919
# Track whether an events table is currently being defined and needs closing.
2020
_close_table = False
21+
# Events to write out when the table is closed
22+
_pending_events = []
2123

2224

2325
def removesuffix(s: str, suffix: str) -> str:
@@ -128,6 +130,7 @@ def unit_to_pmu(unit: str) -> Optional[str]:
128130
eventcode |= int(jd['ExtSel']) << 8
129131
configcode = int(jd['ConfigCode'], 0) if 'ConfigCode' in jd else None
130132
self.name = jd['EventName'].lower() if 'EventName' in jd else None
133+
self.topic = ''
131134
self.compat = jd.get('Compat')
132135
self.desc = fixdesc(jd.get('BriefDescription'))
133136
self.long_desc = fixdesc(jd.get('PublicDescription'))
@@ -200,7 +203,7 @@ def __repr__(self) -> str:
200203
s += f'\t{attr} = {value},\n'
201204
return s + '}'
202205

203-
def to_c_string(self, topic_local: str) -> str:
206+
def to_c_string(self) -> str:
204207
"""Representation of the event as a C struct initializer."""
205208

206209
def attr_string(attr: str, value: str) -> str:
@@ -212,33 +215,35 @@ def str_if_present(self, attr: str) -> str:
212215
return attr_string(attr, getattr(self, attr))
213216

214217
s = '{\n'
215-
s += f'\t.topic = "{topic_local}",\n'
216218
for attr in [
217219
'aggr_mode', 'compat', 'deprecated', 'desc', 'event', 'long_desc',
218220
'metric_constraint', 'metric_expr', 'metric_group', 'metric_name',
219-
'name', 'perpkg', 'pmu', 'unit'
221+
'name', 'perpkg', 'pmu', 'topic', 'unit'
220222
]:
221223
s += str_if_present(self, attr)
222224
s += '},\n'
223225
return s
224226

225227

226-
def read_json_events(path: str) -> Sequence[JsonEvent]:
228+
def read_json_events(path: str, topic: str) -> Sequence[JsonEvent]:
227229
"""Read json events from the specified file."""
228230

229231
try:
230-
return json.load(open(path), object_hook=lambda d: JsonEvent(d))
232+
result = json.load(open(path), object_hook=JsonEvent)
231233
except BaseException as err:
232234
print(f"Exception processing {path}")
233235
raise
236+
for event in result:
237+
event.topic = topic
238+
return result
234239

235240

236241
def preprocess_arch_std_files(archpath: str) -> None:
237242
"""Read in all architecture standard events."""
238243
global _arch_std_events
239244
for item in os.scandir(archpath):
240245
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=''):
242247
if event.name:
243248
_arch_std_events[event.name.lower()] = event
244249

@@ -252,19 +257,36 @@ def print_events_table_prefix(tblname: str) -> None:
252257
_close_table = True
253258

254259

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."""
257262
if not _close_table:
258263
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)
261266

262267

263268
def print_events_table_suffix() -> None:
264269
"""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+
265280
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("""{
268290
\t.name = 0,
269291
\t.event = 0,
270292
\t.desc = 0,
@@ -307,7 +329,7 @@ def is_leaf_dir(path: str) -> bool:
307329
if not item.is_file() or not item.name.endswith('.json'):
308330
return
309331

310-
print_events_table_entries(item, get_topic(item.name))
332+
add_events_table_entries(item, get_topic(item.name))
311333

312334

313335
def print_mapping_table(archs: Sequence[str]) -> None:

0 commit comments

Comments
 (0)