Skip to content

Commit f85d120

Browse files
captain5050acmel
authored andcommitted
perf jevents: Sort strings in the big C string to reduce faults
Sort the strings within the big C string based on whether they were for a metric and then by when they were added. This helps group related strings and reduce minor faults by approximately 10 in 1740, about 0.57%. Signed-off-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Gaosheng Cui <cuigaosheng1@huawei.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@arm.com> Cc: Jing Zhang <renyu.zj@linux.alibaba.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: John Garry <john.g.garry@oracle.com> Cc: Kajol Jain <kjain@linux.ibm.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ravi Bangoria <ravi.bangoria@amd.com> Cc: Rob Herring <robh@kernel.org> Link: https://lore.kernel.org/r/20230824041330.266337-18-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 8d4b6d3 commit f85d120

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

tools/perf/pmu-events/jevents.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,24 @@ class BigCString:
113113
strings: Set[str]
114114
big_string: Sequence[str]
115115
offsets: Dict[str, int]
116+
insert_number: int
117+
insert_point: Dict[str, int]
118+
metrics: Set[str]
116119

117120
def __init__(self):
118121
self.strings = set()
122+
self.insert_number = 0;
123+
self.insert_point = {}
124+
self.metrics = set()
119125

120-
def add(self, s: str) -> None:
126+
def add(self, s: str, metric: bool) -> None:
121127
"""Called to add to the big string."""
122-
self.strings.add(s)
128+
if s not in self.strings:
129+
self.strings.add(s)
130+
self.insert_point[s] = self.insert_number
131+
self.insert_number += 1
132+
if metric:
133+
self.metrics.add(s)
123134

124135
def compute(self) -> None:
125136
"""Called once all strings are added to compute the string and offsets."""
@@ -160,8 +171,11 @@ def compute(self) -> None:
160171
self.big_string = []
161172
self.offsets = {}
162173

174+
def string_cmp_key(s: str) -> Tuple[bool, int, str]:
175+
return (s in self.metrics, self.insert_point[s], s)
176+
163177
# Emit all strings that aren't folded in a sorted manner.
164-
for s in sorted(self.strings):
178+
for s in sorted(self.strings, key=string_cmp_key):
165179
if s not in folded_strings:
166180
self.offsets[s] = big_string_offset
167181
self.big_string.append(f'/* offset={big_string_offset} */ "')
@@ -574,19 +588,20 @@ def preprocess_one_file(parents: Sequence[str], item: os.DirEntry) -> None:
574588
assert len(mgroup) > 1, parents
575589
description = f"{metricgroup_descriptions[mgroup]}\\000"
576590
mgroup = f"{mgroup}\\000"
577-
_bcs.add(mgroup)
578-
_bcs.add(description)
591+
_bcs.add(mgroup, metric=True)
592+
_bcs.add(description, metric=True)
579593
_metricgroups[mgroup] = description
580594
return
581595

582596
topic = get_topic(item.name)
583597
for event in read_json_events(item.path, topic):
584598
pmu_name = f"{event.pmu}\\000"
585-
_bcs.add(pmu_name)
586599
if event.name:
587-
_bcs.add(event.build_c_string(metric=False))
600+
_bcs.add(pmu_name, metric=False)
601+
_bcs.add(event.build_c_string(metric=False), metric=False)
588602
if event.metric_name:
589-
_bcs.add(event.build_c_string(metric=True))
603+
_bcs.add(pmu_name, metric=True)
604+
_bcs.add(event.build_c_string(metric=True), metric=True)
590605

591606
def process_one_file(parents: Sequence[str], item: os.DirEntry) -> None:
592607
"""Process a JSON file during the main walk."""

0 commit comments

Comments
 (0)