[PATCH v2 01/12] tools/arch/x86/pmtctl: Add pmtctl skeleton
From: David E. Box
Date: Sun Nov 22 2026 - 14:34:25 EST
Intel Platform Monitoring Technology (PMT) exposes hardware telemetry
through the pmt_telemetry driver in sysfs. Each telemetry device
provides a binary data file whose layout is described by
platform-specific metric definitions that are published separately.
Add the skeleton of pmtctl, a command-line tool for discovering PMT
telemetry devices and sampling their metrics: the build, the logging
helpers and the main entry point with global option handling. The
commands are added in follow-up patches.
The tool exits with 0 on success, 1 if the request is wrong (an invalid
option or value, or nothing matches the selection) and 2 if carrying it
out fails. Library code returns 0 or a negative errno, and each command
maps errors to an exit status.
Errors and warnings go to stderr as "pmtctl: <level>: <message>".
Signed-off-by: David E. Box <david.e.box@xxxxxxxxxxxxxxx>
---
V2 changes:
- New patch: the Makefile, logging and main() from v1 03 and 12, so
the tool builds from the first patch.
- Logging is errors and warnings only; -q and --debug are dropped (v1
parsed them but never applied them).
- Library code returns 0 or -errno and never calls exit(); commands
map errors to the exit statuses 0/1/2.
- One plain Makefile, like the other tools in tools/arch/x86.
tools/arch/x86/pmtctl/.gitignore | 2 +
tools/arch/x86/pmtctl/Makefile | 25 ++++++++
tools/arch/x86/pmtctl/include/lib/log.h | 23 +++++++
tools/arch/x86/pmtctl/include/pmtctl_cli.h | 12 ++++
tools/arch/x86/pmtctl/lib/log.c | 41 ++++++++++++
tools/arch/x86/pmtctl/src/main.c | 72 ++++++++++++++++++++++
6 files changed, 175 insertions(+)
create mode 100644 tools/arch/x86/pmtctl/.gitignore
create mode 100644 tools/arch/x86/pmtctl/Makefile
create mode 100644 tools/arch/x86/pmtctl/include/lib/log.h
create mode 100644 tools/arch/x86/pmtctl/include/pmtctl_cli.h
create mode 100644 tools/arch/x86/pmtctl/lib/log.c
create mode 100644 tools/arch/x86/pmtctl/src/main.c
diff --git a/tools/arch/x86/pmtctl/.gitignore b/tools/arch/x86/pmtctl/.gitignore
new file mode 100644
index 000000000000..3bd0d7c1b61a
--- /dev/null
+++ b/tools/arch/x86/pmtctl/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+pmtctl
diff --git a/tools/arch/x86/pmtctl/Makefile b/tools/arch/x86/pmtctl/Makefile
new file mode 100644
index 000000000000..0595d6617920
--- /dev/null
+++ b/tools/arch/x86/pmtctl/Makefile
@@ -0,0 +1,25 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for Intel Platform Monitoring Technology (PMT) tool
+
+BINDIR ?= /usr/sbin
+
+CFLAGS = -Wextra
+
+override CFLAGS += -O2 -Wall -D_GNU_SOURCE -Iinclude -I../../../include
+
+OBJS := \
+ lib/log.o \
+ src/main.o
+
+pmtctl: $(OBJS)
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+$(OBJS): $(wildcard include/*.h include/lib/*.h)
+
+.PHONY: clean install
+clean:
+ @rm -f pmtctl $(OBJS)
+
+install: pmtctl
+ install -d $(DESTDIR)$(BINDIR)
+ install -m 755 -p pmtctl $(DESTDIR)$(BINDIR)/pmtctl
diff --git a/tools/arch/x86/pmtctl/include/lib/log.h b/tools/arch/x86/pmtctl/include/lib/log.h
new file mode 100644
index 000000000000..2006b5b50637
--- /dev/null
+++ b/tools/arch/x86/pmtctl/include/lib/log.h
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef PMTCTL_LOG_H
+#define PMTCTL_LOG_H
+
+#include <linux/compiler.h>
+
+enum pmtctl_log_level {
+ PMTCTL_LOG_ERROR,
+ PMTCTL_LOG_WARN,
+};
+
+void log_msg(enum pmtctl_log_level level, const char *fmt, ...) __printf(2, 3);
+
+/*
+ * Log an error with the strerror() text of @ret appended and return @ret.
+ * @ret must be a negative errno value.
+ */
+int log_ret(int ret, const char *fmt, ...) __printf(2, 3);
+
+#define log_err(fmt, ...) log_msg(PMTCTL_LOG_ERROR, fmt, ##__VA_ARGS__)
+#define log_warn(fmt, ...) log_msg(PMTCTL_LOG_WARN, fmt, ##__VA_ARGS__)
+
+#endif
diff --git a/tools/arch/x86/pmtctl/include/pmtctl_cli.h b/tools/arch/x86/pmtctl/include/pmtctl_cli.h
new file mode 100644
index 000000000000..550a61b97aca
--- /dev/null
+++ b/tools/arch/x86/pmtctl/include/pmtctl_cli.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef PMTCTL_CLI_H
+#define PMTCTL_CLI_H
+
+/* Process exit status, see EXIT STATUS in pmtctl(8) */
+enum pmtctl_exit {
+ PMTCTL_EXIT_OK,
+ PMTCTL_EXIT_USER,
+ PMTCTL_EXIT_SYSTEM,
+};
+
+#endif
diff --git a/tools/arch/x86/pmtctl/lib/log.c b/tools/arch/x86/pmtctl/lib/log.c
new file mode 100644
index 000000000000..87a24d076cb2
--- /dev/null
+++ b/tools/arch/x86/pmtctl/lib/log.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "lib/log.h"
+
+static const char * const log_tags[] = {
+ [PMTCTL_LOG_ERROR] = "error",
+ [PMTCTL_LOG_WARN] = "warning",
+};
+
+static __printf(3, 0)
+void log_vmsg(enum pmtctl_log_level level, int err, const char *fmt, va_list ap)
+{
+ fprintf(stderr, "pmtctl: %s: ", log_tags[level]);
+ vfprintf(stderr, fmt, ap);
+ if (err)
+ fprintf(stderr, ": %s", strerror(-err));
+ fputc('\n', stderr);
+}
+
+void log_msg(enum pmtctl_log_level level, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ log_vmsg(level, 0, fmt, ap);
+ va_end(ap);
+}
+
+int log_ret(int ret, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ log_vmsg(PMTCTL_LOG_ERROR, ret, fmt, ap);
+ va_end(ap);
+
+ return ret;
+}
diff --git a/tools/arch/x86/pmtctl/src/main.c b/tools/arch/x86/pmtctl/src/main.c
new file mode 100644
index 000000000000..25aedef59cf5
--- /dev/null
+++ b/tools/arch/x86/pmtctl/src/main.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * pmtctl: query Intel Platform Monitoring Technology (PMT) telemetry
+ */
+#include <getopt.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "lib/log.h"
+#include "pmtctl_cli.h"
+
+#define PMTCTL_VERSION "2026.11.22"
+
+static void print_usage(FILE *out)
+{
+ fprintf(out,
+ "Usage: pmtctl [global options] <command> [command options] ...\n"
+ "\n"
+ "Query Intel Platform Monitoring Technology (PMT) metrics.\n"
+ "\n"
+ "Global options:\n"
+ " -h, --help Show this help and exit\n"
+ " -V, --version Show version and exit\n"
+ "\n"
+ "Run 'pmtctl <command> --help' for command-specific options.\n");
+}
+
+int main(int argc, char **argv)
+{
+ static const struct option long_opts[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, 'V' },
+ {}
+ };
+ const char *cmd;
+ int opt;
+
+ /* '+' stops at the first non-option: the command name */
+ while ((opt = getopt_long(argc, argv, "+hV", long_opts, NULL)) != -1) {
+ switch (opt) {
+ case 'h':
+ print_usage(stdout);
+ return PMTCTL_EXIT_OK;
+ case 'V':
+ printf("pmtctl version %s - David E. Box <david.e.box@xxxxxxxxxxxxxxx>\n",
+ PMTCTL_VERSION);
+ return PMTCTL_EXIT_OK;
+ default:
+ /* getopt_long() already printed an error */
+ fprintf(stderr, "Try 'pmtctl --help' for usage.\n");
+ return PMTCTL_EXIT_USER;
+ }
+ }
+
+ if (optind >= argc) {
+ log_err("missing command");
+ print_usage(stderr);
+ return PMTCTL_EXIT_USER;
+ }
+
+ cmd = argv[optind];
+
+ if (!strcmp(cmd, "help")) {
+ print_usage(stdout);
+ return PMTCTL_EXIT_OK;
+ }
+
+ log_err("unknown command '%s'", cmd);
+ fprintf(stderr, "Run 'pmtctl --help' for a list of commands.\n");
+
+ return PMTCTL_EXIT_USER;
+}
--
2.53.0