mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David E. Box" <david.e.box@linux.intel.com>
To: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Hans de Goede" <hansg@kernel.org>
Cc: "David E. Box" <david.e.box@linux.intel.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 01/12] tools/arch/x86/pmtctl: Add pmtctl skeleton
Date: Sun, 22 Nov 2026 11:33:56 -0800	[thread overview]
Message-ID: <bd7a852b721534fc09564587697d5b8727e7916a.1795374925.git.david.e.box@linux.intel.com> (raw)
In-Reply-To: <cover.1795374925.git.david.e.box@linux.intel.com>

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@linux.intel.com>
---
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@linux.intel.com>\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


  reply	other threads:[~2026-11-22 19:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 19:33 [PATCH v2 00/12] tools/arch/x86/pmtctl: Add Intel PMT command-line tool David E. Box
2026-11-22 19:33 ` David E. Box [this message]
2026-11-22 19:33 ` [PATCH v2 02/12] tools/arch/x86/pmtctl: Add PMT telemetry device discovery David E. Box
2026-11-22 19:33 ` [PATCH v2 03/12] tools/arch/x86/pmtctl: Add pmtxml2json conversion tool David E. Box
2026-11-22 19:33 ` [PATCH v2 04/12] tools/arch/x86/pmtctl: Add JSON metric definition loading David E. Box
2026-11-22 19:34 ` [PATCH v2 05/12] tools/arch/x86/pmtctl: Add 'list' command David E. Box
2026-11-22 19:34 ` [PATCH v2 06/12] tools/arch/x86/pmtctl: Add 'list --summary' David E. Box
2026-11-22 19:34 ` [PATCH v2 07/12] tools/arch/x86/pmtctl: Add 'stat' command David E. Box
2026-11-22 19:34 ` [PATCH v2 08/12] tools/arch/x86/pmtctl: Add 'stat --hex' David E. Box
2026-11-22 19:34 ` [PATCH v2 09/12] tools/arch/x86/pmtctl: Add 'stat --raw' David E. Box
2026-11-22 19:34 ` [PATCH v2 10/12] tools/arch/x86/pmtctl: Add README David E. Box
2026-11-22 19:34 ` [PATCH v2 11/12] tools/arch/x86/pmtctl: Add man page David E. Box
2026-11-22 19:34 ` [PATCH v2 12/12] tools/arch/x86/pmtctl: Add MAINTAINERS entry 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=bd7a852b721534fc09564587697d5b8727e7916a.1795374925.git.david.e.box@linux.intel.com \
    --to=david.e.box@linux.intel.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=hansg@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®