From: "David E. Box" <david.e.box@linux.intel.com>
To: linux-kernel@vger.kernel.org, david.e.box@linux.intel.com,
ilpo.jarvinen@linux.intel.com, andriy.shevchenko@linux.intel.com,
platform-driver-x86@vger.kernel.org
Subject: [PATCH 06/17] tools/arch/x86/pmtctl: Add libpmtctl built-in metric provider
Date: Mon, 25 May 2026 18:47:04 -0700 [thread overview]
Message-ID: <20260526014719.2248380-7-david.e.box@linux.intel.com> (raw)
In-Reply-To: <20260526014719.2248380-1-david.e.box@linux.intel.com>
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@linux.intel.com>
---
.../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
next prev parent reply other threads:[~2026-05-26 1:47 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-26 1:46 [PATCH 00/17] tools/arch/x86/pmtctl: Add Intel PMT command-line tool David E. Box
2026-05-26 1:46 ` [PATCH 01/17] tools/arch/x86/pmtctl: Add MAINTAINERS entry David E. Box
2026-05-26 1:47 ` [PATCH 02/17] tools/arch/x86/pmtctl: Add libpmtctl shared type enumerations David E. Box
2026-05-26 9:20 ` Ilpo Järvinen
2026-05-26 1:47 ` [PATCH 03/17] tools/arch/x86/pmtctl: Add libpmtctl internal logging and utility functions David E. Box
2026-05-26 9:59 ` Ilpo Järvinen
2026-05-26 1:47 ` [PATCH 04/17] tools/arch/x86/pmtctl: Add libpmtctl metric definition database David E. Box
2026-05-26 10:06 ` Ilpo Järvinen
2026-05-26 1:47 ` [PATCH 05/17] tools/arch/x86/pmtctl: Add libpmtctl device enumeration backend David E. Box
2026-05-26 10:35 ` Ilpo Järvinen
2026-05-26 1:47 ` David E. Box [this message]
2026-05-26 1:47 ` [PATCH 07/17] tools/arch/x86/pmtctl: Add libpmtctl JSON metric provider David E. Box
2026-05-26 11:04 ` Ilpo Järvinen
2026-05-26 1:47 ` [PATCH 08/17] tools/arch/x86/pmtctl: Add libpmtctl public API and context David E. Box
2026-05-26 11:25 ` Ilpo Järvinen
2026-05-26 17:44 ` David Box
2026-05-26 1:47 ` [PATCH 09/17] tools/arch/x86/pmtctl: Add libpmtctl Makefile + pc + README David E. Box
2026-05-26 1:47 ` [PATCH 10/17] tools/arch/x86/pmtctl: Add libpmtctl usage sample David E. Box
2026-05-26 1:47 ` [PATCH 11/17] tools/arch/x86/pmtctl: Add libpmtctl built-in metric definition support David E. Box
2026-05-26 1:47 ` [PATCH 12/17] tools/arch/x86/pmtctl: Add pmtctl CLI entry point and pager David E. Box
2026-05-26 1:47 ` [PATCH 13/17] tools/arch/x86/pmtctl: Add pmtctl 'list' command David E. Box
2026-05-26 1:47 ` [PATCH 14/17] tools/arch/x86/pmtctl: Add pmtctl 'stat' command David E. Box
2026-05-26 1:47 ` [PATCH 15/17] tools/arch/x86/pmtctl: Add pmtxml2json conversion tool David E. Box
2026-05-26 1:47 ` [PATCH 16/17] tools/arch/x86/pmtctl: Add README.md David E. Box
2026-05-26 1:47 ` [PATCH 17/17] tools/arch/x86/pmtctl: Add man page David E. Box
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=20260526014719.2248380-7-david.e.box@linux.intel.com \
--to=david.e.box@linux.intel.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=platform-driver-x86@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.