linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	 James Clark <james.clark@linaro.org>,
	Kan Liang <kan.liang@linux.intel.com>,
	 linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Ian Rogers <irogers@google.com>
Subject: [PATCH 04/10] perf jevents: Encapsulate big_c_string lookups behind pmu_events__get_string()
Date: Fri, 13 Nov 2026 13:08:16 -0800	[thread overview]
Message-ID: <20261113210822.3112700-5-irogers@google.com> (raw)
In-Reply-To: <20261113210822.3112700-1-irogers@google.com>

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@google.com>
---
 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


  parent reply	other threads:[~2026-11-13 21:08 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 21:08 [PATCH 00/10] perf jevents: Block-indexed string compression and default JEVENTS_ARCH=all Ian Rogers
2026-11-13 21:08 ` [PATCH 01/10] perf jevents: Store raw bytes in BigCString instead of C-escaped strings Ian Rogers
2026-11-13 21:21   ` sashiko-bot
2026-11-13 21:08 ` [PATCH 02/10] perf jevents: Sort BigCString entries alphabetically Ian Rogers
2026-11-13 21:12   ` sashiko-bot
2026-11-13 21:08 ` [PATCH 03/10] perf jevents: Store PMU names as string literals in struct pmu_table_entry Ian Rogers
2026-11-13 21:13   ` sashiko-bot
2026-11-13 21:08 ` Ian Rogers [this message]
2026-11-13 21:12   ` [PATCH 04/10] perf jevents: Encapsulate big_c_string lookups behind pmu_events__get_string() sashiko-bot
2026-11-13 21:08 ` [PATCH 05/10] perf jevents: Add block-indexed string packing and --compress=zlib Ian Rogers
2026-11-13 21:16   ` sashiko-bot
2026-11-13 21:08 ` [PATCH 06/10] perf jevents: Add --compress=lzma block compression backend Ian Rogers
2026-11-13 21:13   ` sashiko-bot
2026-11-13 21:08 ` [PATCH 07/10] perf jevents: Add --compress=zstd " Ian Rogers
2026-11-13 21:13   ` sashiko-bot
2026-11-13 21:08 ` [PATCH 08/10] perf build: Enable jevents string compression when available Ian Rogers
2026-11-13 21:17   ` sashiko-bot
2026-11-13 21:08 ` [PATCH 09/10] perf tests pmu-events: Test metricgroup descriptions and concurrent lookups Ian Rogers
2026-11-13 21:20   ` sashiko-bot
2026-11-13 21:08 ` [PATCH 10/10] perf build: Default JEVENTS_ARCH to all Ian Rogers
2026-11-13 21:21   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261113210822.3112700-5-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).