[PATCH 04/10] perf jevents: Encapsulate big_c_string lookups behind pmu_events__get_string()

From: Ian Rogers

Date: Fri Nov 13 2026 - 16:09:38 EST


Replace direct &big_c_string[offset] indexing in decompress_event(),
decompress_metric(), and describe_metricgroup() with calls to
pmu_events__get_string(uint32_t offset), and change compact_pmu_event
and metricgroups offsets to uint32_t.

When generating a single file (such as empty-pmu-events.c), emit
pmu_events__get_string() as a static inline helper indexing
static const char big_c_string[]. When generating separate pmu-events.c
and pmu-events-string.c files, keep big_c_string static to
pmu-events-string.c and expose pmu_events__get_string() across the two
generated translation units. Also add explicit POSIX standard header
inclusions to the generated C files.

This cleanly isolates the string storage representation inside
pmu-events-string.c so subsequent patches can replace flat byte offsets
into big_c_string[] with block-indexed string compression (where each
uint32_t offset encodes (block_id << 16) | sub_offset into independently
compressed ~8 KiB blocks decompressed on demand) without changing
pmu-events.c.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/pmu-events/empty-pmu-events.c | 52 +++++++++++----
tools/perf/pmu-events/jevents.py | 81 ++++++++++++++++++------
2 files changed, 104 insertions(+), 29 deletions(-)

diff --git a/tools/perf/pmu-events/empty-pmu-events.c b/tools/perf/pmu-events/empty-pmu-events.c
index 6121ef3d2a98..c40fa30d7235 100644
--- a/tools/perf/pmu-events/empty-pmu-events.c
+++ b/tools/perf/pmu-events/empty-pmu-events.c
@@ -4,11 +4,15 @@
#include <pmu-events/pmu-events.h>
#include "util/header.h"
#include "util/pmu.h"
-#include <string.h>
+#include <stdbool.h>
#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>

struct compact_pmu_event {
- int offset;
+ uint32_t offset;
};

struct pmu_table_entry {
@@ -18,7 +22,7 @@ struct pmu_table_entry {
};

/* clang-format off */
-static const char *const big_c_string =
+static const char big_c_string[] =
/* offset=0 */
"alignment-faults\000software\000Number of kernel handled memory alignment faults\000config=7\000\00000\000\000\000\000\000"
/* offset=92 */
@@ -2635,6 +2639,11 @@ static const char *const big_c_string =
"stalled_cycles_per_instruction\000Default\000(max(stalled\\-cycles\\-frontend, stalled\\-cycles\\-backend) / instructions if has_event(stalled\\-cycles\\-frontend) & has_event(stalled\\-cycles\\-backend) else (stalled\\-cycles\\-frontend / instructions if has_event(stalled\\-cycles\\-frontend) else (stalled\\-cycles\\-backend / instructions if has_event(stalled\\-cycles\\-backend) else 0)))\000\000Max front or backend stalls per instruction\000\000\000\000\000\000001"
;

+static inline const char *pmu_events__get_string(uint32_t offset)
+{
+ return &big_c_string[offset];
+}
+
static const struct compact_pmu_event pmu_events__common_default_core[] = {
/* bpc\000legacy cache\000Branch prediction unit read accesses\000legacy-cache-conf... */
{ 216 },
@@ -5460,9 +5469,17 @@ static const struct pmu_sys_events pmu_sys_event_tables[] = {
},
};

-static void decompress_event(int offset, struct pmu_event *pe)
+static void decompress_event(uint32_t offset, struct pmu_event *pe)
{
- const char *p = &big_c_string[offset];
+ const char *p = pmu_events__get_string(offset);
+
+ if (!p) {
+ const char *pmu = pe->pmu;
+
+ memset(pe, 0, sizeof(*pe));
+ pe->pmu = pmu;
+ return;
+ }

pe->name = (*p == '\0' ? NULL : p);
while (*p++);
@@ -5489,9 +5506,17 @@ static void decompress_event(int offset, struct pmu_event *pe)
pe->long_desc = (*p == '\0' ? NULL : p);
}

-static void decompress_metric(int offset, struct pmu_metric *pm)
+static void decompress_metric(uint32_t offset, struct pmu_metric *pm)
{
- const char *p = &big_c_string[offset];
+ const char *p = pmu_events__get_string(offset);
+
+ if (!p) {
+ const char *pmu = pm->pmu;
+
+ memset(pm, 0, sizeof(*pm));
+ pm->pmu = pmu;
+ return;
+ }

pm->metric_name = (*p == '\0' ? NULL : p);
while (*p++);
@@ -6003,7 +6028,7 @@ int pmu_metrics_table__iterate_tables(pmu_metrics_table_iter_t fn, void *data)
return 0;
}

-static const int metricgroups[][2] = {
+static const uint32_t metricgroups[][2] = {

};

@@ -6013,11 +6038,16 @@ const char *describe_metricgroup(const char *group)

while (low <= high) {
int mid = (low + high) / 2;
- const char *mgroup = &big_c_string[metricgroups[mid][0]];
- int cmp = strcmp(mgroup, group);
+ const char *mgroup = pmu_events__get_string(metricgroups[mid][0]);
+ int cmp;
+
+ if (!mgroup)
+ return NULL;
+
+ cmp = strcmp(mgroup, group);

if (cmp == 0)
- return &big_c_string[metricgroups[mid][1]];
+ return pmu_events__get_string(metricgroups[mid][1]);
else if (cmp < 0)
low = mid + 1;
else
diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py
index 5d5f8fd79e11..783ce2331f44 100755
--- a/tools/perf/pmu-events/jevents.py
+++ b/tools/perf/pmu-events/jevents.py
@@ -899,9 +899,17 @@ static const struct pmu_sys_events pmu_sys_event_tables[] = {
\t},
};

-static void decompress_event(int offset, struct pmu_event *pe)
+static void decompress_event(uint32_t offset, struct pmu_event *pe)
{
-\tconst char *p = &big_c_string[offset];
+\tconst char *p = pmu_events__get_string(offset);
+
+\tif (!p) {
+\t\tconst char *pmu = pe->pmu;
+
+\t\tmemset(pe, 0, sizeof(*pe));
+\t\tpe->pmu = pmu;
+\t\treturn;
+\t}
""")
for attr in _json_event_attributes:
_args.output_file.write(f'\n\tpe->{attr} = ')
@@ -917,9 +925,17 @@ static void decompress_event(int offset, struct pmu_event *pe)
_args.output_file.write('\twhile (*p++);')
_args.output_file.write("""}

-static void decompress_metric(int offset, struct pmu_metric *pm)
+static void decompress_metric(uint32_t offset, struct pmu_metric *pm)
{
-\tconst char *p = &big_c_string[offset];
+\tconst char *p = pmu_events__get_string(offset);
+
+\tif (!p) {
+\t\tconst char *pmu = pm->pmu;
+
+\t\tmemset(pm, 0, sizeof(*pm));
+\t\tpm->pmu = pmu;
+\t\treturn;
+\t}
""")
for attr in _json_metric_attributes:
_args.output_file.write(f'\n\tpm->{attr} = ')
@@ -1382,7 +1398,7 @@ int pmu_for_each_sys_metric(pmu_metric_iter_fn fn, void *data)
def print_metricgroups() -> None:
assert _args is not None
_args.output_file.write("""
-static const int metricgroups[][2] = {
+static const uint32_t metricgroups[][2] = {
""")
for mgroup in sorted(_metricgroups):
description = _metricgroups[mgroup]
@@ -1400,11 +1416,16 @@ const char *describe_metricgroup(const char *group)

\twhile (low <= high) {
\t\tint mid = (low + high) / 2;
-\t\tconst char *mgroup = &big_c_string[metricgroups[mid][0]];
-\t\tint cmp = strcmp(mgroup, group);
+\t\tconst char *mgroup = pmu_events__get_string(metricgroups[mid][0]);
+\t\tint cmp;
+
+\t\tif (!mgroup)
+\t\t\treturn NULL;
+
+\t\tcmp = strcmp(mgroup, group);

\t\tif (cmp == 0)
-\t\t\treturn &big_c_string[metricgroups[mid][1]];
+\t\t\treturn pmu_events__get_string(metricgroups[mid][1]);
\t\telse if (cmp < 0)
\t\t\tlow = mid + 1;
\t\telse
@@ -1482,11 +1503,15 @@ such as "arm/cortex-a34".''',
#include <pmu-events/pmu-events.h>
#include "util/header.h"
#include "util/pmu.h"
-#include <string.h>
+#include <stdbool.h>
#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>

struct compact_pmu_event {
-\tint offset;
+\tuint32_t offset;
};

struct pmu_table_entry {
@@ -1534,19 +1559,39 @@ struct pmu_table_entry {
_bcs.compute()
_args.output_file.write('/* clang-format off */\n')
if not _args.output_string_file:
- _args.output_file.write('static const char *const big_c_string =\n')
- for s in _bcs.big_string:
- _args.output_file.write(s)
+ _args.output_file.write('static const char big_c_string[] =\n')
+ if _bcs.big_string:
+ for s in _bcs.big_string:
+ _args.output_file.write(s)
+ else:
+ _args.output_file.write('""\n')
_args.output_file.write(';\n\n')
+ _args.output_file.write("""static inline const char *pmu_events__get_string(uint32_t offset)
+{
+\treturn &big_c_string[offset];
+}
+
+""")
else:
_args.output_string_file.write('/* SPDX-License-Identifier: GPL-2.0 */\n')
_args.output_string_file.write('/* Autogenerated by jevents.py */\n')
- _args.output_string_file.write('extern const char big_c_string[];\n')
- _args.output_string_file.write('const char big_c_string[] =\n')
- for s in _bcs.big_string:
- _args.output_string_file.write(s)
+ _args.output_string_file.write('#include <stdint.h>\n\n')
+ _args.output_string_file.write('const char *pmu_events__get_string(uint32_t offset);\n\n')
+ _args.output_string_file.write('/* clang-format off */\n')
+ _args.output_string_file.write('static const char big_c_string[] =\n')
+ if _bcs.big_string:
+ for s in _bcs.big_string:
+ _args.output_string_file.write(s)
+ else:
+ _args.output_string_file.write('""\n')
_args.output_string_file.write(';\n')
- _args.output_file.write('extern const char big_c_string[];\n\n')
+ _args.output_string_file.write('/* clang-format on */\n\n')
+ _args.output_string_file.write("""const char *pmu_events__get_string(uint32_t offset)
+{
+\treturn &big_c_string[offset];
+}
+""")
+ _args.output_file.write('const char *pmu_events__get_string(uint32_t offset);\n\n')
for arch in archs:
arch_path = f'{_args.starting_dir}/{arch}'
ftw(arch_path, [], process_one_file)
--
2.56.1.703.gf848ed807c-goog