[PATCH 06/17] tools/arch/x86/pmtctl: Add libpmtctl built-in metric provider
From: David E. Box
Date: Mon May 25 2026 - 21:50:32 EST
Add a unified metric definition provider interface to libpmtctl_core,
giving callers a single entry point for loading PMT metric definitions
regardless of their source.
pmt_metrics_load() abstracts whether definitions come from a built-in
table or an external JSON file. When no path is provided, the built-in
definitions are loaded. When a path is provided, loading is delegated to
the JSON provider when built with HAVE_JANSSON, otherwise
PMTCTL_ERR_UNSUPPORTED is returned.
Provide a stub built-in definition table so the library can build and run
before platform-specific definitions are generated. In that case,
pmt_metrics_load() returns PMTCTL_ERR_NOMETRICS, allowing callers to
report a meaningful error.
Add the remaining build-system integration in a later patch to keep the
library split into logically separate changes.
Assisted-by: GitHub-Copilot:claude-sonnet-4.6
Signed-off-by: David E. Box <david.e.box@xxxxxxxxxxxxxxx>
---
.../x86/pmtctl/include/lib/builtin_defs.h | 14 +++++++
.../x86/pmtctl/include/lib/metrics_provider.h | 21 ++++++++++
.../arch/x86/pmtctl/lib/builtin_defs_empty.c | 13 ++++++
tools/arch/x86/pmtctl/lib/metrics_provider.c | 42 +++++++++++++++++++
4 files changed, 90 insertions(+)
create mode 100644 tools/arch/x86/pmtctl/include/lib/builtin_defs.h
create mode 100644 tools/arch/x86/pmtctl/include/lib/metrics_provider.h
create mode 100644 tools/arch/x86/pmtctl/lib/builtin_defs_empty.c
create mode 100644 tools/arch/x86/pmtctl/lib/metrics_provider.c
diff --git a/tools/arch/x86/pmtctl/include/lib/builtin_defs.h b/tools/arch/x86/pmtctl/include/lib/builtin_defs.h
new file mode 100644
index 000000000000..432e4d87d0ac
--- /dev/null
+++ b/tools/arch/x86/pmtctl/include/lib/builtin_defs.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef PMTCTL_BUILTIN_DEFS_H
+#define PMTCTL_BUILTIN_DEFS_H
+
+#include "lib/metrics_db.h"
+#include "lib/pmt_guid.h"
+
+extern const struct pmt_metric_def builtin_defs[];
+extern const int builtin_defs_count;
+
+extern const struct pmt_guid builtin_guids[];
+extern const int builtin_guids_count;
+
+#endif
diff --git a/tools/arch/x86/pmtctl/include/lib/metrics_provider.h b/tools/arch/x86/pmtctl/include/lib/metrics_provider.h
new file mode 100644
index 000000000000..efe9bea817c8
--- /dev/null
+++ b/tools/arch/x86/pmtctl/include/lib/metrics_provider.h
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef PMTCTL_METRICS_PROVIDER_H
+#define PMTCTL_METRICS_PROVIDER_H
+
+#include "lib/metrics_db.h"
+
+/*
+ * Populate @db with metric definitions.
+ *
+ * If @json_path is NULL, the built-in provider is used (definitions linked
+ * into the library via builtin_defs[]). If @json_path is non-NULL, the JSON
+ * provider is used; that provider is only available when the library is
+ * built with HAVE_JANSSON.
+ */
+int pmt_metrics_load(const char *json_path, struct pmt_metrics_db *db);
+
+#ifdef HAVE_JANSSON
+int pmt_metrics_load_json(const char *json_path, struct pmt_metrics_db *db);
+#endif
+
+#endif
diff --git a/tools/arch/x86/pmtctl/lib/builtin_defs_empty.c b/tools/arch/x86/pmtctl/lib/builtin_defs_empty.c
new file mode 100644
index 000000000000..4496d85b230c
--- /dev/null
+++ b/tools/arch/x86/pmtctl/lib/builtin_defs_empty.c
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "lib/builtin_defs.h"
+
+/*
+ * Empty stub used when no generated/builtin_defs.c is present.
+ * The build system will pick this file unless a generated one exists.
+ */
+
+const struct pmt_guid builtin_guids[1];
+const int builtin_guids_count;
+
+const struct pmt_metric_def builtin_defs[1];
+const int builtin_defs_count;
diff --git a/tools/arch/x86/pmtctl/lib/metrics_provider.c b/tools/arch/x86/pmtctl/lib/metrics_provider.c
new file mode 100644
index 000000000000..6673145d4a9a
--- /dev/null
+++ b/tools/arch/x86/pmtctl/lib/metrics_provider.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#define LOG_PREFIX "metrics_provider"
+#include <string.h>
+
+#include "lib/builtin_defs.h"
+#include "lib/log.h"
+#include "lib/metrics_provider.h"
+
+static int pmt_metrics_load_builtin(struct pmt_metrics_db *db)
+{
+ if (builtin_defs_count == 0)
+ return PMTCTL_ERR_NOMETRICS;
+
+ /*
+ * Register the compiled-in GUID metadata table so device_telem and
+ * any runtime providers can resolve raw GUIDs to the same intern'd
+ * struct pmt_guid pointers that builtin_defs[] already references.
+ */
+ if (builtin_guids_count > 0)
+ pmt_guid_register(builtin_guids, builtin_guids_count);
+
+ return pmt_metrics_add_block(db, builtin_defs, builtin_defs_count, true);
+}
+
+int pmt_metrics_load(const char *json_path, struct pmt_metrics_db *db)
+{
+ if (!db)
+ log_bug_and_exit("invalid metric db pointer");
+
+ memset(db, 0, sizeof(*db));
+
+ if (!json_path)
+ return pmt_metrics_load_builtin(db);
+
+#ifdef HAVE_JANSSON
+ return pmt_metrics_load_json(json_path, db);
+#else
+ (void)json_path;
+ return log_ret(PMTCTL_ERR_UNSUPPORTED,
+ "JSON metric definitions requested but library built without jansson support");
+#endif
+}
--
2.43.0