tools/arch/x86/pmtctl: Add Intel PMT command-line tool
v2 cover tools/arch/x86/pmtctl: Add Intel PMT command-line tool
Intel Platform Monitoring Technology (PMT) exposes hardware telemetry through the pmt_telemetry auxiliary driver. Each telemetry device, telemN, provides a data file of 64-bit samples, and its GUID identifies the layout of the samples, which Intel publishes as XML in the Intel-PMT repository.
This series adds pmtctl, a command-line tool that lists the metrics of the telemetry devices on a system and samples them, similar to 'perf stat'. It lives under tools/arch/x86/pmtctl/ next to the other x86 platform tools.
pmtctl list # metrics of the devices, by GUID pmtctl list --summary # GUIDs and their devices pmtctl stat -e package_energy # sample a metric once a second pmtctl stat --raw id=2,msb=7 # sample bits 0-7 of sample 2
The metric definitions are not part of the tree. 'make defs-json-fetch' clones the Intel-PMT repository and converts its XML into one JSON file per GUID with scripts/pmtxml2json.py; 'make install' installs them, and pmtctl loads them at run time.
V2 is a rework of V1 following Ilpo's review, and about half its size (3.9k lines).
Testing: on a Panther Lake system, where the metrics of both published telemetry layouts were sampled and checked against --raw reads of the same samples, and on a QEMU guest with an emulated PMT device (KASAN kernel). Also with a fake sysfs tree under ASan/UBSan and allocation-failure injection. Reading telemetry needs root, because the kernel creates the data files with mode 0440; 'list' does not.
This rework was done with the help of an AI coding assistant (Claude Code). I reviewed every patch by hand, and the structure, conventions and behavior of the tool are my decisions.
Changes since v1, in summary (each patch's changelog has the details and the v1 patches it replaces):
- Restructured: the tool is built up in working steps, so every patch builds and adds one thing. v1 added library components bottom-up, and nothing built until patch 9.
- Smaller: no installed library, no pager, and no built-in definition tables (deferred to a follow-up). Definitions are JSON only, read from the installed directory or -J, in a format reduced to what pmtctl uses.
- Command line: per-command --guid and --device filters replace the -d selector; 'list --summary' replaces 'list --devices'; --guids, --once, --header, -q and --debug are gone. The tool says "device" throughout, as the sysfs ABI does.
- Code conventions follow the v1 review: errors are 0 or -errno and map to documented exit statuses, no exit() in library code, explicit context ownership, size_t for counts, and no hand-rolled arrays, string pools or magic numbers.
- Several v1 bugs are fixed, for example 7 published GUIDs converted under the wrong number, a directory load that depended on readdir order, and a time column computed instead of measured.
V1: https://lore.kernel.org/r/[email protected]
David E. Box (12):
tools/arch/x86/pmtctl: Add pmtctl skeleton tools/arch/x86/pmtctl: Add PMT telemetry device discovery tools/arch/x86/pmtctl: Add pmtxml2json conversion tool tools/arch/x86/pmtctl: Add JSON metric definition loading tools/arch/x86/pmtctl: Add 'list' command tools/arch/x86/pmtctl: Add 'list --summary' tools/arch/x86/pmtctl: Add 'stat' command tools/arch/x86/pmtctl: Add 'stat --hex' tools/arch/x86/pmtctl: Add 'stat --raw' tools/arch/x86/pmtctl: Add README tools/arch/x86/pmtctl: Add man page tools/arch/x86/pmtctl: Add MAINTAINERS entry
MAINTAINERS | 2 + tools/arch/x86/pmtctl/.gitignore | 3 + tools/arch/x86/pmtctl/Makefile | 107 +++ tools/arch/x86/pmtctl/README | 99 ++ tools/arch/x86/pmtctl/include/lib/common.h | 71 ++ tools/arch/x86/pmtctl/include/lib/device.h | 21 + tools/arch/x86/pmtctl/include/lib/log.h | 23 + tools/arch/x86/pmtctl/include/lib/pmtctl.h | 52 ++ tools/arch/x86/pmtctl/include/lib/tss.h | 43 + tools/arch/x86/pmtctl/include/pmtctl_cli.h | 19 + tools/arch/x86/pmtctl/lib/common.c | 81 ++ tools/arch/x86/pmtctl/lib/device_telem.c | 297 ++++++ tools/arch/x86/pmtctl/lib/log.c | 41 + tools/arch/x86/pmtctl/lib/pmtctl.c | 114 +++ tools/arch/x86/pmtctl/lib/tss.c | 45 + tools/arch/x86/pmtctl/lib/tss_json.c | 406 +++++++++ tools/arch/x86/pmtctl/pmtctl.8 | 284 ++++++ tools/arch/x86/pmtctl/scripts/pmtxml2json.md | 189 ++++ tools/arch/x86/pmtctl/scripts/pmtxml2json.py | 639 +++++++++++++ tools/arch/x86/pmtctl/src/cmd_list.c | 330 +++++++ tools/arch/x86/pmtctl/src/cmd_stat.c | 897 +++++++++++++++++++ tools/arch/x86/pmtctl/src/main.c | 91 ++ 22 files changed, 3854 insertions(+) create mode 100644 tools/arch/x86/pmtctl/.gitignore create mode 100644 tools/arch/x86/pmtctl/Makefile create mode 100644 tools/arch/x86/pmtctl/README create mode 100644 tools/arch/x86/pmtctl/include/lib/common.h create mode 100644 tools/arch/x86/pmtctl/include/lib/device.h create mode 100644 tools/arch/x86/pmtctl/include/lib/log.h create mode 100644 tools/arch/x86/pmtctl/include/lib/pmtctl.h create mode 100644 tools/arch/x86/pmtctl/include/lib/tss.h create mode 100644 tools/arch/x86/pmtctl/include/pmtctl_cli.h create mode 100644 tools/arch/x86/pmtctl/lib/common.c create mode 100644 tools/arch/x86/pmtctl/lib/device_telem.c create mode 100644 tools/arch/x86/pmtctl/lib/log.c create mode 100644 tools/arch/x86/pmtctl/lib/pmtctl.c create mode 100644 tools/arch/x86/pmtctl/lib/tss.c create mode 100644 tools/arch/x86/pmtctl/lib/tss_json.c create mode 100644 tools/arch/x86/pmtctl/pmtctl.8 create mode 100644 tools/arch/x86/pmtctl/scripts/pmtxml2json.md create mode 100755 tools/arch/x86/pmtctl/scripts/pmtxml2json.py create mode 100644 tools/arch/x86/pmtctl/src/cmd_list.c create mode 100644 tools/arch/x86/pmtctl/src/cmd_stat.c create mode 100644 tools/arch/x86/pmtctl/src/main.c
base-commit: 9c82959c37bf69c2ace8a0f975cf2129ec1fd982
Signature · 1 line
2.53.0
PATCH v2 1/12 tools/arch/x86/pmtctl: Add pmtctl skeleton
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>".
---
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
tools/arch/x86/pmtctl/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +pmtctl
tools/arch/x86/pmtctl/Makefile new file +25 -0
@@ -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
tools/arch/x86/pmtctl/include/lib/log.h new file +23 -0
@@ -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
tools/arch/x86/pmtctl/include/pmtctl_cli.h new file +12 -0
@@ -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
tools/arch/x86/pmtctl/lib/log.c new file +41 -0
41 lines not on this page · raw message
tools/arch/x86/pmtctl/src/main.c new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v2 2/12 tools/arch/x86/pmtctl: Add PMT telemetry device discovery
In reply to David E. BoxThe pmt_telemetry driver binds to Intel VSEC auxiliary devices and registers one telemetry device, telemN, per telemetry region under the auxiliary device's intel_pmt directory. Each telemetry device has a guid attribute that identifies its metric layout and a telem file with the sample data.
Add discovery of these telemetry devices. telem_scan() walks the auxiliary devices bound to pmt_telemetry and records the GUID, name and data file path of each telemetry device. A device whose GUID can't be read is skipped with a warning. If no devices are found, an error asks whether the driver is loaded. The devices are returned sorted by GUID and then by name, so the commands built on them don't depend on the order of the sysfs directories.
The results are held in struct pmtctl_ctx, which each command owns and releases with pmtctl_ctx_free(). auto_pmtctl_ctx frees it automatically at scope exit, and auto_free does the same for heap pointers.
Also add small helpers to lib/common.c, used by this and later patches:pmt_grow_array() grows a dynamic array by doubling its capacity, parse_u32() parses a whole string as an unsigned 32-bit number, and path_printf() builds a path, failing instead of truncating.
---
V2 changes:
- Replaces v1 05, with the context from v1 08 and pmt_grow_array()
from v1 03.
- Unused device fields and sysfs helpers dropped.
- A device whose GUID can't be read is skipped with a warning.
- Devices are returned sorted by GUID and name, independent of the order of the sysfs directories.
- pmt_grow_array() returns the array and uses reallocarray(), with no realloc(+1) growth or void ** cast.
- parse_u32() rejects a sign, leading whitespace and trailing characters; path_printf() fails instead of truncating.
tools/arch/x86/pmtctl/Makefile | 3 + tools/arch/x86/pmtctl/include/lib/common.h | 64 ++++++ tools/arch/x86/pmtctl/include/lib/device.h | 18 ++ tools/arch/x86/pmtctl/include/lib/pmtctl.h | 24 ++ tools/arch/x86/pmtctl/lib/common.c | 75 +++++++ tools/arch/x86/pmtctl/lib/device_telem.c | 248 +++++++++++++++++++++ tools/arch/x86/pmtctl/lib/pmtctl.c | 22 ++ 7 files changed, 454 insertions(+) create mode 100644 tools/arch/x86/pmtctl/include/lib/common.h create mode 100644 tools/arch/x86/pmtctl/include/lib/device.h create mode 100644 tools/arch/x86/pmtctl/include/lib/pmtctl.h create mode 100644 tools/arch/x86/pmtctl/lib/common.c create mode 100644 tools/arch/x86/pmtctl/lib/device_telem.c create mode 100644 tools/arch/x86/pmtctl/lib/pmtctl.c
tools/arch/x86/pmtctl/Makefile +3 -0
@@ -8,7 +8,10 @@ CFLAGS = -Wextra override CFLAGS += -O2 -Wall -D_GNU_SOURCE -Iinclude -I../../../include OBJS := \ + lib/common.o \ + lib/device_telem.o \ lib/log.o \ + lib/pmtctl.o \ src/main.o pmtctl: $(OBJS)
tools/arch/x86/pmtctl/include/lib/common.h new file +64 -0
64 lines not on this page · raw message
tools/arch/x86/pmtctl/include/lib/device.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef PMTCTL_DEVICE_H +#define PMTCTL_DEVICE_H + +#include <stddef.h> +#include <stdint.h> + +/* A PMT telemetry device registered by the pmt_telemetry driver */ +struct pmt_device { +uint32_t guid; +char *name;/* e.g. "telem0" */ +char *data_path;/* telemetry data file */ +}; + +int telem_scan (struct pmt_device **devices,size_t *num_devices); +void telem_free (struct pmt_device *devices,size_t num_devices); + +#endif
tools/arch/x86/pmtctl/include/lib/pmtctl.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef PMTCTL_H +#define PMTCTL_H + +#include <stddef.h> + +#include "lib/device.h" + +/* State for one pmtctl command, owned by the command */ +struct pmtctl_ctx { +struct pmt_device *devices; +size_t num_devices; +}; + +int pmtctl_ctx_init (struct pmtctl_ctx *ctx); +void pmtctl_ctx_free (struct pmtctl_ctx *ctx); + +/* +* Free the context with pmtctl_ctx_free() at scope exit: +* struct pmtctl_ctx ctx auto_pmtctl_ctx = {}; +*/ +#define auto_pmtctl_ctx __attribute__((cleanup(pmtctl_ctx_free))) + +#endif
tools/arch/x86/pmtctl/lib/common.c new file +75 -0
75 lines not on this page · raw message
tools/arch/x86/pmtctl/lib/device_telem.c new file +248 -0
248 lines not on this page · raw message
tools/arch/x86/pmtctl/lib/pmtctl.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "lib/device.h" +#include "lib/pmtctl.h" + +/** +* pmtctl_ctx_init - discover PMT devices +* @ctx: zero-initialized context +* +* Return: 0 on success or a negative errno. @ctx must be released with +* pmtctl_ctx_free() in either case. +*/ +int pmtctl_ctx_init (struct pmtctl_ctx *ctx) +{ +return telem_scan (&ctx->devices, &ctx->num_devices); +} + +void pmtctl_ctx_free (struct pmtctl_ctx *ctx) +{ +telem_free (ctx->devices, ctx->num_devices); + ctx->devices =NULL ; + ctx->num_devices =0 ; +}
Signature · 1 line
2.53.0
PATCH v2 3/12 tools/arch/x86/pmtctl: Add pmtxml2json conversion tool
In reply to David E. BoxThe layout of each PMT telemetry region is described by aggregator XML files published in the Intel-PMT repository
(https://github.com/intel/Intel-PMT). Each file lists the samples of one GUID with their sample ID, bit range and description.
Add pmtxml2json.py, which converts these files into JSON metric definitions that pmtctl can load at runtime. Each aggregator becomes a pmt_<guid>.json file holding the GUID, its platform and description, and one event per sample with its name, description, sample ID and bit range. Event names are derived from the sample names. Where a name would collide, the samples of the subgroups involved are all prefixed with their subgroup, so the members of a subgroup are named alike. Reserved samples are skipped. The rules are described in pmtxml2json.md.
The aggregator files include shared data type definitions from a *_common.xml file through an external XML entity. pmtxml2json doesn't need those definitions, so it parses the XML with lxml with entity resolution turned off, leaving the entity unresolved. The standard library parser can't do that.
Add Makefile targets to fetch the repository into a local cache and convert it into defs/, replacing the definitions of an earlier run:
make defs-json-fetch # clone (once) and convert make defs-json-pull # update the clone and convert again make defs-clean # remove defs/
The fetch targets are not part of the default build, to avoid unexpected network access.
---
V2 changes:
- Replaces v1 15.
- New output format: one pmt_<guid>.json per GUID with only what pmtctl uses. The perf-style PMU, ConfigCode and MetricGroup fields and the pmt_guids.json sidecar are gone.
- GUIDs written without 0x and with decimal digits only are read as hex; v1 converted 7 published GUIDs under the wrong number.
- Names that differ only in case are disambiguated, and all members of a subgroup with a colliding name are prefixed together.
- XML is parsed with entity resolution turned off; lxml is checked for and documented.
- Fetching again removes stale definitions; the Makefile has fetch targets only, with no C code generation.
- Dead code removed.
tools/arch/x86/pmtctl/.gitignore | 1 + tools/arch/x86/pmtctl/Makefile | 55 +- tools/arch/x86/pmtctl/scripts/pmtxml2json.md | 189 ++++++ tools/arch/x86/pmtctl/scripts/pmtxml2json.py | 639 +++++++++++++++++++ 4 files changed, 883 insertions(+), 1 deletion(-) create mode 100644 tools/arch/x86/pmtctl/scripts/pmtxml2json.md create mode 100755 tools/arch/x86/pmtctl/scripts/pmtxml2json.py
tools/arch/x86/pmtctl/.gitignore +1 -0
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only pmtctl +defs/
tools/arch/x86/pmtctl/Makefile +54 -1
61 lines not on this page · raw message
tools/arch/x86/pmtctl/scripts/pmtxml2json.md new file +189 -0
189 lines not on this page · raw message
tools/arch/x86/pmtctl/scripts/pmtxml2json.py new file +639 -0
639 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v2 4/12 tools/arch/x86/pmtctl: Add JSON metric definition loading
In reply to David E. BoxThe telemetry data of a PMT device is an array of 64-bit samples whose meaning, its telemetry semantic space (TSS), is identified by the device's GUID. pmtxml2json.py converts the published layouts into one JSON file per GUID, listing the metrics as bit ranges of samples.
Add loading of these files with libjansson. A path may name a single definition file or a directory of them. Each file becomes one entry of a TSS database in struct pmtctl_ctx, holding the GUID's platform, description and metrics, sorted by name, so the metrics of a device are found with a single lookup of its GUID. pmt_tss_db_load_json() loads the database, and pmtctl_ctx_free() frees it with the rest of the context.
Definition files are validated: each must have a GUID not defined by another file, and each event a unique name, a 32-bit sample ID and a bit range within the 64-bit sample (lsb <= msb <= 63). Otherwise the file is rejected with a message naming the event. In a directory every file is checked and any bad file fails the load, so a partial set of definitions is never used.
Building pmtctl now requires libjansson.
---
V2 changes:
- Replaces v1 04 and 07.
- A TSS database with one entry per GUID, sorted and searched with bsearch(), replaces the metric blocks, the flat-index accessors and the GUID registry.
- No string pool, casts or realloc(+1) growth.
- Files are validated strictly. A bad file fails a directory load, where v1's result depended on readdir order, and errors name the file and event.
tools/arch/x86/pmtctl/Makefile | 13 + tools/arch/x86/pmtctl/include/lib/pmtctl.h | 2 + tools/arch/x86/pmtctl/include/lib/tss.h | 43 +++ tools/arch/x86/pmtctl/lib/pmtctl.c | 2 + tools/arch/x86/pmtctl/lib/tss.c | 45 +++ tools/arch/x86/pmtctl/lib/tss_json.c | 406 +++++++++++++++++++++ 6 files changed, 511 insertions(+) create mode 100644 tools/arch/x86/pmtctl/include/lib/tss.h create mode 100644 tools/arch/x86/pmtctl/lib/tss.c create mode 100644 tools/arch/x86/pmtctl/lib/tss_json.c
tools/arch/x86/pmtctl/Makefile +13 -0
@@ -7,11 +7,24 @@ CFLAGS = -Wextra override CFLAGS += -O2 -Wall -D_GNU_SOURCE -Iinclude -I../../../include +# The jansson check is skipped for targets that don't compile anything +ifneq ($(filter-out clean defs-json-fetch defs-json-pull defs-clean,$(or $(MAKECMDGOALS),pmtctl)),) +JANSSON_CFLAGS := $(shell pkg-config --cflags jansson 2>/dev/null) +JANSSON_LIBS := $(shell pkg-config --libs jansson 2>/dev/null) +ifeq ($(JANSSON_LIBS),) +$(error jansson not found: install libjansson-dev (Debian/Ubuntu) or jansson-devel (Fedora/RHEL)) +endif +override CFLAGS += $(JANSSON_CFLAGS) +LDLIBS += $(JANSSON_LIBS) +endif + OBJS := \ lib/common.o \ lib/device_telem.o \ lib/log.o \ lib/pmtctl.o \ + lib/tss.o \ + lib/tss_json.o \ src/main.o pmtctl: $(OBJS)
tools/arch/x86/pmtctl/include/lib/pmtctl.h +2 -0
@@ -5,11 +5,13 @@#include <stddef.h> #include "lib/device.h" +#include "lib/tss.h" /* State for one pmtctl command, owned by the command */ struct pmtctl_ctx {struct pmt_device *devices;size_t num_devices; +struct pmt_tss_db tss; };int pmtctl_ctx_init (struct pmtctl_ctx *ctx);
tools/arch/x86/pmtctl/include/lib/tss.h new file +43 -0
43 lines not on this page · raw message
tools/arch/x86/pmtctl/lib/pmtctl.c +2 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-only #include "lib/device.h" #include "lib/pmtctl.h" +#include "lib/tss.h" /** * pmtctl_ctx_init - discover PMT devices
@@ -16,6 +17,7 @@ int pmtctl_ctx_init(struct pmtctl_ctx *ctx)void pmtctl_ctx_free (struct pmtctl_ctx *ctx) { +pmt_tss_db_free (&ctx->tss);telem_free (ctx->devices, ctx->num_devices); ctx->devices =NULL ; ctx->num_devices =0 ;
tools/arch/x86/pmtctl/lib/tss.c new file +45 -0
45 lines not on this page · raw message
tools/arch/x86/pmtctl/lib/tss_json.c new file +406 -0
406 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v2 5/12 tools/arch/x86/pmtctl: Add 'list' command
In reply to David E. BoxAdd the 'list' command, which shows the metric definitions of the PMT telemetry devices on the system: one group per GUID with its platform, number of metrics and description, followed by each metric's name and description. A GUID shared by several devices is listed once. On a terminal, descriptions are wrapped to its width; otherwise each is printed on one line so the output is easy to search.
Metric definitions are loaded by pmtctl_ctx_load_tss() from the path given with -J or, without it, from the directory they are installed in (/usr/share/pmtctl by default, set with DEFSDIR at build time). 'make install' installs the definitions converted by
'make defs-json-fetch', if any. A missing default directory is not an error, but definitions that fail to load are.
--guid and --device restrict the listing to the devices with a GUID or to one device. The selection is done by pmtctl_select_devices(), which keeps the device order and is shared with the commands added later. If none of the selected devices has metric definitions, 'list' fails with an error rather than printing nothing.
Also add print_chars() to lib/common.c for the indents and separator lines; 'stat' uses it as well.
---
V2 changes:
- Replaces the metric listing of v1 13, with -J from v1 12.
- Each metric is listed once per GUID; v1 listed it once per device sharing the GUID.
- Descriptions are wrapped only on a terminal.
- Definitions come from -J or the installed /usr/share/pmtctl.
- --guid and --device replace -d; the pager and -X/--keep are dropped.
- A selection without metric definitions is an error.
tools/arch/x86/pmtctl/Makefile | 9 + tools/arch/x86/pmtctl/include/lib/common.h | 7 + tools/arch/x86/pmtctl/include/lib/pmtctl.h | 26 ++ tools/arch/x86/pmtctl/include/pmtctl_cli.h | 6 + tools/arch/x86/pmtctl/lib/common.c | 6 + tools/arch/x86/pmtctl/lib/pmtctl.c | 90 +++++++ tools/arch/x86/pmtctl/src/cmd_list.c | 292 +++++++++++++++++++++ tools/arch/x86/pmtctl/src/main.c | 17 +- 8 files changed, 452 insertions(+), 1 deletion(-) create mode 100644 tools/arch/x86/pmtctl/src/cmd_list.c
tools/arch/x86/pmtctl/Makefile +9 -0
@@ -2,10 +2,12 @@ # Makefile for Intel Platform Monitoring Technology (PMT) tool BINDIR ?= /usr/sbin +DEFSDIR ?= /usr/share/pmtctl CFLAGS = -Wextra override CFLAGS += -O2 -Wall -D_GNU_SOURCE -Iinclude -I../../../include +override CFLAGS += -DPMTCTL_DEFS_DIR='"$(DEFSDIR)"' # The jansson check is skipped for targets that don't compile anything ifneq ($(filter-out clean defs-json-fetch defs-json-pull defs-clean,$(or $(MAKECMDGOALS),pmtctl)),)
@@ -25,6 +27,7 @@ OBJS := \ lib/pmtctl.o \ lib/tss.o \ lib/tss_json.o \ + src/cmd_list.o \ src/main.o pmtctl: $(OBJS)
@@ -92,3 +95,9 @@ clean: install: pmtctl install -d $(DESTDIR)$(BINDIR) install -m 755 -p pmtctl $(DESTDIR)$(BINDIR)/pmtctl + @# Metric definitions, if 'make defs-json-fetch' was run + @if ls $(DEFS_DIR)/*.json >/dev/null 2>&1; then \ + install -d $(DESTDIR)$(DEFSDIR); \ + install -m 644 -p $(DEFS_DIR)/*.json $(DESTDIR)$(DEFSDIR); \ + echo "Installed metric definitions to $(DESTDIR)$(DEFSDIR)"; \ + fi
tools/arch/x86/pmtctl/include/lib/common.h +7 -0
@@ -61,4 +61,11 @@ int parse_u32(const char *s, int base, uint32_t *val);*/ int path_printf(char *buf, size_t size, const char *fmt, ...) __printf(3, 4); +/** +* print_chars - print a character repeatedly to stdout +* @c: character to print +* @n: number of times to print it +*/ +void print_chars (int c,size_t n); +#endif
tools/arch/x86/pmtctl/include/lib/pmtctl.h +26 -0
@@ -2,11 +2,18 @@ #ifndef PMTCTL_H #define PMTCTL_H +#include <stdbool.h> #include <stddef.h> +#include <stdint.h> #include "lib/device.h" #include "lib/tss.h" +/* Installed metric definitions, used when no path is given */ +#ifndef PMTCTL_DEFS_DIR +#define PMTCTL_DEFS_DIR "/usr/share/pmtctl" +#endif + /* State for one pmtctl command, owned by the command */ struct pmtctl_ctx { struct pmt_device *devices;
@@ -14,8 +21,27 @@ struct pmtctl_ctx {struct pmt_tss_db tss; }; +/** +* struct pmt_device_filter - devices a command acts on +* @has_guid: select only the devices whose GUID is @guid +* @guid: the GUID to match, used only if @has_guid is set +* @name: if not NULL, select only the device with this name, e.g. "telem3" +* +* A device is selected if it matches every filter that is set, so a zeroed +* filter selects all devices. +*/ +struct pmt_device_filter { +bool has_guid; +uint32_t guid; +const char *name; +}; +int pmtctl_ctx_init (struct pmtctl_ctx *ctx); +int pmtctl_ctx_load_tss (struct pmtctl_ctx *ctx,const char *json_path);void pmtctl_ctx_free (struct pmtctl_ctx *ctx); +int pmtctl_select_devices (const struct pmtctl_ctx *ctx,const struct pmt_device_filter *filter, +const struct pmt_device ***devs,size_t *num_devs); +int pmt_device_filter_set_guid (struct pmt_device_filter *filter,const char *arg);/* * Free the context with pmtctl_ctx_free() at scope exit:
tools/arch/x86/pmtctl/include/pmtctl_cli.h +6 -0
@@ -9,4 +9,10 @@ enum pmtctl_exit { PMTCTL_EXIT_SYSTEM, }; +struct pmt_global_opts { +const char *json_path;/* -J, or NULL for the default directory */ +}; + +int cmd_list (int argc,char **argv,const struct pmt_global_opts *gopts); +#endif
tools/arch/x86/pmtctl/lib/common.c +6 -0
@@ -73,3 +73,9 @@ int path_printf(char *buf, size_t size, const char *fmt, ...)return 0 ; } + +void print_chars (int c,size_t n) +{ +while (n--) +putchar (c); +}
tools/arch/x86/pmtctl/lib/pmtctl.c +90 -0
104 lines not on this page · raw message
tools/arch/x86/pmtctl/src/cmd_list.c new file +292 -0
292 lines not on this page · raw message
tools/arch/x86/pmtctl/src/main.c +16 -1
@@ -7,6 +7,7 @@#include <string.h> #include "lib/log.h" +#include "lib/pmtctl.h" #include "pmtctl_cli.h" #define PMTCTL_VERSION "2026.11.22"
@@ -22,6 +23,12 @@ static void print_usage(FILE *out)" -h, --help Show this help and exit\n" " -V, --version Show version and exit\n" "\n" +" -J, --json-file <path> Metrics JSON file or directory\n" +" If omitted, " PMTCTL_DEFS_DIR " is used if it exists.\n" +"\n" +"Commands:\n" +" list List available PMT metrics\n" +"\n" "Run 'pmtctl <command> --help' for command-specific options.\n"); }
@@ -30,13 +37,15 @@ int main(int argc, char **argv)static const struct option long_opts[] = { {"help" , no_argument,NULL ,'h' }, {"version" , no_argument,NULL ,'V' }, + {"json-file" , required_argument,NULL ,'J' }, {} }; +struct pmt_global_opts gopts = {};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 ) { +while ((opt =getopt_long (argc, argv,"+hVJ:" , long_opts,NULL )) !=-1 ) {switch (opt) {case 'h' :print_usage (stdout);
@@ -45,6 +54,9 @@ int main(int argc, char **argv)printf ("pmtctl version %s - David E. Box <[email protected]>\n" ,PMTCTL_VERSION );return PMTCTL_EXIT_OK ; +case 'J' : + gopts.json_path = optarg; +break ;default :/* getopt_long() already printed an error */ fprintf (stderr,"Try 'pmtctl --help' for usage.\n" );
@@ -60,6 +72,9 @@ int main(int argc, char **argv) cmd = argv[optind]; +if (!strcmp (cmd,"list" )) +return cmd_list (argc - optind, argv + optind, &gopts); +if (!strcmp (cmd,"help" )) {print_usage (stdout);return PMTCTL_EXIT_OK ;
Signature · 1 line
2.53.0
PATCH v2 6/12 tools/arch/x86/pmtctl: Add 'list --summary'
In reply to David E. BoxAdd the --summary mode to 'list', which shows the GUIDs of the PMT telemetry devices on the system, each with its platform and description when its definitions are available, and the devices that have it. Unlike the metric listing it works without metric definitions, and like it, it can be restricted with --guid and --device.
---
V2 changes:
- Replaces 'list --devices' from v1 13; v1's 'list --guids' is dropped.
- Works without metric definitions (v1's failed without built-in ones), is sorted, and honors --guid and --device.
tools/arch/x86/pmtctl/src/cmd_list.c | 40 +++++++++++++++++++++++++++- tools/arch/x86/pmtctl/src/main.c | 2 +- 2 files changed, 40 insertions(+), 2 deletions(-)
tools/arch/x86/pmtctl/src/cmd_list.c +39 -1
@@ -28,6 +28,9 @@#define LIST_GROUP_DESC_INDENT 4#define LIST_METRIC_DESC_INDENT 8 +/* Indent of a device name under its GUID group header */ +#define LIST_DEVICE_INDENT 6 +/* Width limits of the line under a group header */ #define LIST_SEP_MIN ((size_t)8)#define LIST_SEP_MAX ((size_t)100)
@@ -36,11 +39,13 @@enum {OPT_LIST_GUID =CHAR_MAX +1 ,OPT_LIST_DEVICE , +OPT_LIST_SUMMARY , };struct list_opts {bool show_help;struct pmt_device_filter filter; +bool summary; };static void list_usage (FILE *out)
@@ -53,7 +58,8 @@ static void list_usage(FILE *out)"Options:\n" " -h, --help Show this help.\n" " --guid <hex> Restrict to devices with this GUID.\n" - " --device <name> Restrict to one device, e.g. telem3.\n"); +" --device <name> Restrict to one device, e.g. telem3.\n" + " --summary List GUIDs and their devices.\n"); }static int parse_list_opts (struct list_opts *opts,int argc,char **argv)
@@ -62,6 +68,7 @@ static int parse_list_opts(struct list_opts *opts, int argc, char **argv) { "help", no_argument, NULL, 'h' }, { "guid", required_argument, NULL, OPT_LIST_GUID }, { "device", required_argument, NULL, OPT_LIST_DEVICE }, + { "summary", no_argument, NULL, OPT_LIST_SUMMARY }, {} };int opt;
@@ -80,6 +87,9 @@ static int parse_list_opts(struct list_opts *opts, int argc, char **argv)case OPT_LIST_DEVICE : opts->filter.name = optarg;break ; +case OPT_LIST_SUMMARY : + opts->summary = true; +break ;default :return -EINVAL ; }
@@ -176,6 +186,29 @@ static void print_guid_group_line(uint32_t guid, const struct pmt_tss_info *infoprint_description (info->description,LIST_GROUP_DESC_INDENT , cols); } +/* List @devs, which are sorted by GUID as ctx->devices is */ +static void do_list_summary (const struct pmtctl_ctx *ctx,const struct pmt_device **devs, +size_t num_devs) +{ +size_t cols =output_columns (); + +printf ("PMT devices on this system:\n\n" ); + +for (size_t i =0 ; i < num_devs; i++) { +uint32_t guid = devs[i]->guid; + +if (!i || guid != devs[i -1 ]->guid) +print_guid_group_line (guid,pmt_tss_db_lookup (&ctx->tss, guid), cols); + +print_chars (' ' ,LIST_DEVICE_INDENT ); +printf ("%s\n" , devs[i]->name); + +/* A blank line ends each GUID group */ +if (i +1 == num_devs || devs[i +1 ]->guid != guid) +printf ("\n" ); + } +} +/* * Underline a group header up to where its longest metric name would end at * the description indent, within LIST_SEP_MIN and LIST_SEP_MAX columns
@@ -274,6 +307,11 @@ int cmd_list(int argc, char **argv, const struct pmt_global_opts *gopts)return PMTCTL_EXIT_USER ; } +if (opts.summary) { +do_list_summary (&ctx, devs, num_devs); +return PMTCTL_EXIT_OK ; + } +if (!ctx.tss.num_tss_info) {if (gopts->json_path)log_err ("no metric definitions in %s" , gopts->json_path);
tools/arch/x86/pmtctl/src/main.c +1 -1
@@ -27,7 +27,7 @@ static void print_usage(FILE *out)" If omitted, " PMTCTL_DEFS_DIR " is used if it exists.\n" "\n" "Commands:\n" -" list List available PMT metrics\n" +" list List available PMT devices and metrics\n" "\n" "Run 'pmtctl <command> --help' for command-specific options.\n"); }
Signature · 1 line
2.53.0
PATCH v2 7/12 tools/arch/x86/pmtctl: Add 'stat' command
In reply to David E. BoxAdd the 'stat' command, which samples metrics of the PMT telemetry devices at an interval, similar to 'perf stat'. Metrics are given by name with -e. The default layout has one row per device with the metric's value; --vertical has one row per device and metric, and is needed to view more than one metric. A device that has none of the requested metrics is left out; with --vertical, a metric a device doesn't have is shown as '-'. A value that can't be read is shown as NaN; stat only fails if no value could be read at all. --guid and --device restrict the devices, -i and -c set the interval and number of samples, and --no-header leaves out the header.
Samples are scheduled from the start time with CLOCK_MONOTONIC, so the interval doesn't drift, and the time column shows when each sample was taken. SIGINT (Ctrl-C) ends the sampling without waiting out the interval, whether it arrives during the wait or while values are being read. Before sampling, every requested sample is checked against the size of its telemetry region, and the data files are opened once, so a permission problem is reported up front.
Discovery also records the size of each device's telemetry region, from its size attribute, so that samples can be checked against it. A device whose size can't be read is skipped with a warning, like one whose GUID can't be read.
---
V2 changes:
- Replaces v1 14, without --hex and --raw, which follow.
- -i and -c are parsed strictly; v1 accepted '10abc'.
- The time column is measured and sampling doesn't drift; v1 printed sample x interval. SIGINT ends sampling without waiting out the interval.
- Columns fit the names; v1 cut them at 20 or 28 characters.
- Samples are checked against the region size before sampling, and the data files are opened once, up front.
- telem_read() rewritten.
- --once, --header and -d dropped in favor of -c 1, --no-header and
--guid/--device.
tools/arch/x86/pmtctl/Makefile | 1 + tools/arch/x86/pmtctl/include/lib/device.h | 3 + tools/arch/x86/pmtctl/include/pmtctl_cli.h | 1 + tools/arch/x86/pmtctl/lib/device_telem.c | 49 ++ tools/arch/x86/pmtctl/src/cmd_stat.c | 753 +++++++++++++++++++++ tools/arch/x86/pmtctl/src/main.c | 4 + 6 files changed, 811 insertions(+) create mode 100644 tools/arch/x86/pmtctl/src/cmd_stat.c
tools/arch/x86/pmtctl/Makefile +1 -0
@@ -28,6 +28,7 @@ OBJS := \ lib/tss.o \ lib/tss_json.o \ src/cmd_list.o \ + src/cmd_stat.o \ src/main.o pmtctl: $(OBJS)
tools/arch/x86/pmtctl/include/lib/device.h +3 -0
@@ -10,9 +10,12 @@ struct pmt_device {uint32_t guid;char *name;/* e.g. "telem0" */ char *data_path;/* telemetry data file */ + uint32_t size; /* telemetry region size in bytes */ };int telem_scan (struct pmt_device **devices,size_t *num_devices);void telem_free (struct pmt_device *devices,size_t num_devices); +int telem_open (const struct pmt_device *dev); +int telem_read (int fd,uint32_t sample_id,unsigned int lsb,unsigned int msb,uint64_t *val);#endif
tools/arch/x86/pmtctl/include/pmtctl_cli.h +1 -0
@@ -14,5 +14,6 @@ struct pmt_global_opts { };int cmd_list (int argc,char **argv,const struct pmt_global_opts *gopts); +int cmd_stat (int argc,char **argv,const struct pmt_global_opts *gopts);#endif
tools/arch/x86/pmtctl/lib/device_telem.c +49 -0
76 lines not on this page · raw message
tools/arch/x86/pmtctl/src/cmd_stat.c new file +753 -0
753 lines not on this page · raw message
tools/arch/x86/pmtctl/src/main.c +4 -0
@@ -28,6 +28,7 @@ static void print_usage(FILE *out)"\n" "Commands:\n" " list List available PMT devices and metrics\n" +" stat Sample metrics over time (perf stat-like)\n" "\n" "Run 'pmtctl <command> --help' for command-specific options.\n"); }
@@ -75,6 +76,9 @@ int main(int argc, char **argv)if (!strcmp (cmd,"list" ))return cmd_list (argc - optind, argv + optind, &gopts); +if (!strcmp (cmd,"stat" )) +return cmd_stat (argc - optind, argv + optind, &gopts); +if (!strcmp (cmd,"help" )) {print_usage (stdout);return PMTCTL_EXIT_OK ;
Signature · 1 line
2.53.0
PATCH v2 8/12 tools/arch/x86/pmtctl: Add 'stat --hex'
In reply to David E. BoxAdd --hex to 'stat' to print the sampled values in hexadecimal, which is easier to read for fields that hold flags, IDs or packed data.
---
V2 changes:
- Split out of v1 14; no change in behavior.
tools/arch/x86/pmtctl/src/cmd_stat.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
tools/arch/x86/pmtctl/src/cmd_stat.c +15 -2
@@ -50,6 +50,7 @@ enum {OPT_STAT_DEVICE ,OPT_STAT_NO_HEADER ,OPT_STAT_VERTICAL , + OPT_STAT_HEX, };/* A value to sample on each device */
@@ -67,6 +68,7 @@ struct stat_opts {uint32_t count;/* number of samples, 0 until interrupted */ bool no_header;bool vertical; +bool hex; };/* The value of one request on one device */
@@ -127,6 +129,7 @@ static void stat_usage_full(FILE *out)" -i, --interval <ms> Sampling interval (default 1000 ms).\n" " -c, --count <N> Number of samples (default infinite).\n" " --no-header Don't print the header.\n" +" --hex Output values in hex.\n" " --vertical One line per value per sample: sample time_ms device\n" " metric value. Required for more than one value.\n" "\n"
@@ -204,6 +207,7 @@ static int stat_parse_options(struct stat_opts *opts, int argc, char **argv) { "count", required_argument, NULL, 'c' }, { "no-header", no_argument, NULL, OPT_STAT_NO_HEADER }, { "vertical", no_argument, NULL, OPT_STAT_VERTICAL }, + { "hex", no_argument, NULL, OPT_STAT_HEX }, {} };int opt;
@@ -244,6 +248,9 @@ static int stat_parse_options(struct stat_opts *opts, int argc, char **argv)case OPT_STAT_VERTICAL : opts->vertical = true;break ; +case OPT_STAT_HEX : + opts->hex = true; +break ;default :return -EINVAL ; }
@@ -419,7 +426,7 @@ static void print_column(const char *s, size_t width, bool right)print_chars (' ' , pad); } -static const char *format_value (const struct stat_cell *cell,char *buf,size_t len) +static const char *format_value (const struct stat_cell *cell,bool hex,char *buf,size_t len) {if (!cell->present)return STAT_VAL_ABSENT ;
@@ -427,6 +434,11 @@ static const char *format_value(const struct stat_cell *cell, char *buf, size_tif (!cell->valid)return STAT_VAL_FAILED ; +if (hex) { +snprintf (buf, len,"0x%" PRIx64, cell->value); +return buf; + } +snprintf (buf, len,"%" PRIu64, cell->value);return buf;
@@ -505,7 +517,8 @@ static void print_sample(const struct stat_run *run, uint64_t sample, uint64_t tfor (size_t r =0 ; r < run->opts.num_requests; r++) {const char *value; - value =format_value (&row->cells[r], value_buf,sizeof (value_buf)); + value =format_value (&row->cells[r], run->opts.hex, value_buf, +sizeof (value_buf));print_line_start (run, sample_buf, time_buf, row->dev->name);if (run->opts.vertical) {
Signature · 1 line
2.53.0
PATCH v2 9/12 tools/arch/x86/pmtctl: Add 'stat --raw'
In reply to David E. BoxAdd --raw to 'stat' to sample a bit range of a telemetry sample by its position, id=<n>[,lsb=<n>][,msb=<n>], instead of by metric name. This works without metric definitions, for example for a GUID that has none or to check a definition against the data. --raw may be repeated and combined with -e; as for metrics, more than one value needs --vertical.
A raw sample is read on each selected device whose telemetry region contains it. A device whose region is too small is treated like one without a requested metric: it is left out, or shows '-' with --vertical. Requesting a sample that no selected device has is an error.
---
V2 changes:
- Split out of v1 14.
- Follows the -e rules: may be repeated and combined with -e, and more than one value needs --vertical. v1 allowed several raw columns in the one-value layout.
- A sample outside a device's region is treated like a metric the device doesn't have.
- One strict parser for id, lsb and msb (v1 had three copies that accepted a sign); its errors name --raw.
tools/arch/x86/pmtctl/src/cmd_stat.c | 145 +++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 7 deletions(-)
tools/arch/x86/pmtctl/src/cmd_stat.c +138 -7
@@ -44,6 +44,9 @@#define STAT_COL_GAP " " +/* Room for the label of a --raw request: "raw[id=4294967295,lsb=63,msb=63]" */ +#define STAT_RAW_LABEL_LEN 40 +/* Values for long-only options, above any short option character */ enum {OPT_STAT_GUID =CHAR_MAX +1 ,
@@ -51,11 +54,19 @@ enum {OPT_STAT_NO_HEADER ,OPT_STAT_VERTICAL ,OPT_STAT_HEX , + OPT_STAT_RAW, };/* A value to sample on each device */ struct stat_request {const char *name;/* metric name */ + +/* For --raw, which reads bits [lsb, msb] of sample sample_id instead */ +bool raw; +uint32_t sample_id; +unsigned int lsb; +unsigned int msb; +char raw_label[STAT_RAW_LABEL_LEN ]; }; struct stat_opts {
@@ -118,12 +129,18 @@ static void stat_usage_full(FILE *out) {fprintf (out,"Usage: pmtctl stat [options] -e <metric>[,<metric>...] ...\n" +" pmtctl stat [options] --raw id=<n>[,lsb=<n>][,msb=<n>] ...\n" "\n" "Collect metric values over time (similar to 'perf stat').\n" "\n" "Options:\n" " -h, --help Show this help.\n" " -e, --event <spec> Metric name or comma-separated list; may be repeated.\n" +" --raw <spec> Read a raw sample value by sample id, with optional\n" +" bit slicing; may be repeated and combined with -e.\n" +" <spec> is id=<n>[,lsb=<n>][,msb=<n>] without\n" +" spaces, where 0 <= lsb <= msb <= 63 (defaults:\n" +" lsb=0, msb=63).\n" " --guid <hex> Restrict to devices with this GUID.\n" " --device <name> Restrict to one device, e.g. telem3.\n" " -i, --interval <ms> Sampling interval (default 1000 ms).\n"
@@ -142,6 +159,9 @@ static void stat_usage_full(FILE *out)" pmtctl stat -e firmware_version -c 1\n" " pmtctl stat -e pch_temp --guid 03086000\n" " pmtctl stat --vertical -e package_energy,pch_temp\n" +" pmtctl stat --raw id=2\n" +" pmtctl stat --raw id=2,lsb=16,msb=31\n" +" pmtctl stat --vertical --raw id=2,msb=7 --raw id=3,lsb=8,msb=15\n" ); }
@@ -149,6 +169,7 @@ static void stat_usage_short(FILE *out) {fprintf (out,"Usage: pmtctl stat [options] -e <metric>[,<metric>...] ...\n" +" pmtctl stat [options] --raw id=<n>[,lsb=<n>][,msb=<n>] ...\n" "\n" "Run 'pmtctl stat --help' for full help.\n" );
@@ -196,6 +217,88 @@ static int parse_positive(const char *arg, const char *opt, uint32_t *val)return 0 ; } +/* Parse the value of --raw key @key, which must be given once and be at most @max */ +static int parse_raw_value (const char *key,const char *val,uint32_t max,bool *seen, +uint32_t *out) +{ +if (*seen) { +log_err ("duplicate key '%s' in --raw argument" , key); +return -EINVAL ; + } + *seen = true; + +if (parse_u32 (val,10 , out)) { +log_err ("--raw %s must be a decimal integer, got '%s'" , key, val); +return -EINVAL ; + } + +if (*out > max) { +log_err ("--raw %s must be at most %" PRIu32, key, max); +return -EINVAL ; + } + +return 0 ; +} + +/* Add the request of a --raw argument, which is split in place at the commas */ +static int add_raw_request (struct stat_opts *opts,char *arg) +{ +struct stat_request req = { .raw = true }; +uint32_t msb =PMT_SAMPLE_BITS -1 ; +uint32_t lsb =0 ; +bool seen_id = false; +bool seen_lsb = false; +bool seen_msb = false; +char *tok; +int ret; + +while ((tok =strsep (&arg,"," ))) { +char *val =strchr (tok,'=' ); + +if (!val) { +log_err ("invalid --raw token '%s' (expected key=value)" , tok); +return -EINVAL ; + } + *val++ ='\0' ; + +if (!strcmp (tok,"id" )) { + ret =parse_raw_value (tok, val,UINT32_MAX , &seen_id, &req.sample_id); + }else if (!strcmp (tok,"lsb" )) { + ret =parse_raw_value (tok, val,PMT_SAMPLE_BITS -1 , &seen_lsb, &lsb); + }else if (!strcmp (tok,"msb" )) { + ret =parse_raw_value (tok, val,PMT_SAMPLE_BITS -1 , &seen_msb, &msb); + }else { +log_err ("unknown key '%s' in --raw argument" , tok); + ret = -EINVAL ; + } + +if (ret) +return ret; + } + +if (!seen_id) { +log_err ("--raw requires at least 'id=<num>'" ); +return -EINVAL ; + } + +if (lsb > msb) { +log_err ("--raw lsb (%" PRIu32") cannot be greater than msb (%" PRIu32")" , + lsb, msb); +return -EINVAL ; + } + + req.lsb = lsb; + req.msb = msb; +snprintf (req.raw_label,sizeof (req.raw_label),"raw[id=%" PRIu32",lsb=%u,msb=%u]" , + req.sample_id, req.lsb, req.msb); + + ret =add_request (opts, &req); +if (ret) +return log_ret (ret,"unable to add --raw request" ); + +return 0 ; +} +static int stat_parse_options (struct stat_opts *opts,int argc,char **argv) {static const struct option long_opts[] = {
@@ -208,6 +311,7 @@ static int stat_parse_options(struct stat_opts *opts, int argc, char **argv) { "no-header", no_argument, NULL, OPT_STAT_NO_HEADER }, { "vertical", no_argument, NULL, OPT_STAT_VERTICAL }, { "hex", no_argument, NULL, OPT_STAT_HEX }, + { "raw", required_argument, NULL, OPT_STAT_RAW }, {} };int opt;
@@ -251,6 +355,11 @@ static int stat_parse_options(struct stat_opts *opts, int argc, char **argv)case OPT_STAT_HEX : opts->hex = true;break ; +case OPT_STAT_RAW : + ret =add_raw_request (opts, optarg); +if (ret) +return ret; +break ;default :return -EINVAL ; }
@@ -262,7 +371,7 @@ static int stat_parse_options(struct stat_opts *opts, int argc, char **argv) }if (!opts->num_requests) { -log_err ("requires -e/--event" ); +log_err ("requires -e/--event or --raw" );return -EINVAL ; }
@@ -274,6 +383,14 @@ static int stat_parse_options(struct stat_opts *opts, int argc, char **argv)return 0 ; } +static const char *request_label (const struct stat_request *req) +{ +if (req->raw) +return req->raw_label; + +return req->name; +} +static int metric_name_cmp (const void *key,const void *elem) {const struct pmt_metric_def *def = elem;
@@ -296,6 +413,18 @@ static void resolve_request(const struct pmtctl_ctx *ctx, const struct pmt_devic *cell = (struct stat_cell ){}; +/* A raw request names its location, which a device has if it's in its region */ +if (req->raw) { +if (!sample_in_region (dev, req->sample_id)) +return ; + + cell->present = true; + cell->sample_id = req->sample_id; + cell->lsb = req->lsb; + cell->msb = req->msb; +return ; + } + info =pmt_tss_db_lookup (&ctx->tss, dev->guid);if (!info || !info->num_metrics)return ;
@@ -354,7 +483,7 @@ static int build_rows(struct stat_run *run, const struct pmt_device **devs, sizefor (size_t r =0 ; r < num_requests; r++) {if (!found[r]) {log_err ("no selected device has metric '%s'" , - run->opts.requests[r].name); +request_label (&run->opts.requests[r]));return -ENOENT ; } }
@@ -381,7 +510,7 @@ static int check_bounds(const struct stat_run *run)if (!sample_in_region (row->dev, cell->sample_id)) {log_err ("%s: sample %" PRIu32" of '%s' is beyond the %" PRIu32"-byte telemetry region" , row->dev->name, cell->sample_id, - run->opts.requests[r].name, row->dev->size); +request_label (&run->opts.requests[r]), row->dev->size);return -ERANGE ; } }
@@ -448,7 +577,7 @@ static const char *format_value(const struct stat_cell *cell, bool hex, char *bustatic size_t value_width (const struct stat_run *run) {size_t width =STAT_VALUE_LEN ; -size_t len =strlen (run->opts.requests[0 ].name); +size_t len =strlen (request_label (&run->opts.requests[0 ]));if (!run->opts.vertical && len > width) width = len;
@@ -488,7 +617,7 @@ static void print_header(const struct stat_run *run)print_column (STAT_COL_METRIC , run->label_width, false); fputs(STAT_COL_GAP STAT_COL_VALUE "\n", stdout); }else { -print_column (run->opts.requests[0 ].name,value_width (run), true); +print_column (request_label (&run->opts.requests[0 ]),value_width (run), true);putchar ('\n' ); }
@@ -522,7 +651,8 @@ static void print_sample(const struct stat_run *run, uint64_t sample, uint64_t tprint_line_start (run, sample_buf, time_buf, row->dev->name);if (run->opts.vertical) { -print_column (run->opts.requests[r].name, run->label_width, false); +print_column (request_label (&run->opts.requests[r]), + run->label_width, false);fputs (STAT_COL_GAP , stdout);fputs (value, stdout); }else {
@@ -543,7 +673,8 @@ static void set_column_widths(struct stat_run *run) run->label_width =strlen (STAT_COL_METRIC );for (size_t r =0 ; r < run->opts.num_requests; r++) - run->label_width =max (run->label_width,strlen (run->opts.requests[r].name)); + run->label_width =max (run->label_width, +strlen (request_label (&run->opts.requests[r]))); }/* Read every present cell; return the last read error, or 0 */
Signature · 1 line
2.53.0
PATCH v2 10/12 tools/arch/x86/pmtctl: Add README
In reply to David E. BoxAdd a README that describes how to build and install pmtctl, how to get the metric definitions from the Intel-PMT repository, and how to use the 'list' and 'stat' commands, including the privileges they need and the exit status.
---
V2 changes:
- Replaces v1 16 (README.md), rewritten for v2 as plain text like the other READMEs in tools/.
tools/arch/x86/pmtctl/README | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tools/arch/x86/pmtctl/README
tools/arch/x86/pmtctl/README new file +96 -0
@@ -0,0 +1,96 @@ +pmtctl +====== + +pmtctl lists and samples the telemetry of Intel Platform Monitoring +Technology (PMT) devices. The pmt_telemetry driver registers a telemetry +device, telemN, for each telemetry region of a platform. Its data is an +array of 64-bit samples, and its GUID identifies the layout: which bits +of which sample hold which metric. pmtctl uses metric definitions +generated from the layouts Intel publishes to name and decode the data. + + +Building +-------- + + make + +This needs libjansson and its development files (libjansson-dev on +Debian and Ubuntu, jansson-devel on Fedora and RHEL). + + make install + +installs pmtctl to /usr/sbin, and the metric definitions, if they were +fetched, to /usr/share/pmtctl. BINDIR and DEFSDIR change the locations, +and DESTDIR is prefixed to both. + + +Metric definitions +------------------ + +The layouts are published as XML in the Intel-PMT repository +(https://github.com/intel/Intel-PMT). scripts/pmtxml2json.py converts +them into one JSON file per GUID: + + make defs-json-fetch # clone Intel-PMT once and convert it into defs/ + make defs-json-pull # update the clone and convert again + make defs-clean # remove defs/ + +These targets need Python 3 with lxml, and are not part of the default +build, so a plain 'make' never accesses the network. The clone is kept in +~/.cache/pmtctl (PMT_CACHE_DIR). The JSON format and how event names are +derived from the XML are described in scripts/pmtxml2json.md. + +pmtctl reads the definitions from the installed directory, or from the +file or directory given with -J: + + pmtctl -J defs list + + +Usage +----- + + pmtctl [global options] <command> [command options] + +'list' shows the metrics of the telemetry devices on the system, grouped +by GUID. 'list --summary' shows the GUIDs and their devices instead: + + pmtctl list + pmtctl list --summary + +'stat' samples metrics at an interval, once a second by default. -i sets +the interval in milliseconds and -c the number of samples: + + pmtctl stat -e package_energy + pmtctl stat -e package_energy -i 500 -c 10 + +The default layout has one row per device. More than one value needs +--vertical, which prints one line per device and value: + + pmtctl stat --vertical -e package_energy,pch_temp + +--raw samples a bit range of a sample by its position, without metric +definitions. --hex prints values in hex: + + pmtctl stat --raw id=2,lsb=16,msb=31 + pmtctl stat --raw id=0,msb=31 --hex + +--guid and --device restrict both commands to the devices with a GUID +or to one device: + + pmtctl list --guid 03086000 + pmtctl stat -e pch_temp --device telem3 + +The telemetry data files can only be read by root, so 'stat' must run +as root; 'list' doesn't need to. + +'pmtctl --help' and 'pmtctl <command> --help' describe all options. + + +Exit status +----------- + + 0 success + 1 the request is wrong: an invalid option or value, or nothing + matches the selection + 2 something failed while carrying it out, such as reading the devices + or the metric definitions
Signature · 1 line
2.53.0
PATCH v2 11/12 tools/arch/x86/pmtctl: Add man page
In reply to David E. BoxAdd the pmtctl(8) man page, which describes the global options, the 'list' and 'stat' commands with their options and output, the metric definition files and the exit status. 'make install' installs it to $(MANDIR)/man8, /usr/share/man/man8 by default, and the README refers to it.
---
V2 changes:
- Replaces v1 17, rewritten for v2. v1 wrongly said -e and --raw were mutually exclusive.
- Installed by 'make install'.
tools/arch/x86/pmtctl/Makefile | 3 + tools/arch/x86/pmtctl/README | 3 + tools/arch/x86/pmtctl/pmtctl.8 | 284 +++++++++++++++++++++++++++++++++ 3 files changed, 290 insertions(+) create mode 100644 tools/arch/x86/pmtctl/pmtctl.8
tools/arch/x86/pmtctl/Makefile +3 -0
@@ -3,6 +3,7 @@ BINDIR ?= /usr/sbin DEFSDIR ?= /usr/share/pmtctl +MANDIR ?= /usr/share/man CFLAGS = -Wextra
@@ -96,6 +97,8 @@ clean: install: pmtctl install -d $(DESTDIR)$(BINDIR) install -m 755 -p pmtctl $(DESTDIR)$(BINDIR)/pmtctl + install -d $(DESTDIR)$(MANDIR)/man8 + install -m 644 -p pmtctl.8 $(DESTDIR)$(MANDIR)/man8/pmtctl.8 @# Metric definitions, if 'make defs-json-fetch' was run @if ls $(DEFS_DIR)/*.json >/dev/null 2>&1; then \ install -d $(DESTDIR)$(DEFSDIR); \
tools/arch/x86/pmtctl/README +3 -0
@@ -22,6 +22,8 @@ Debian and Ubuntu, jansson-devel on Fedora and RHEL). installs pmtctl to /usr/sbin, and the metric definitions, if they were fetched, to /usr/share/pmtctl. BINDIR and DEFSDIR change the locations, and DESTDIR is prefixed to both. +The pmtctl(8) man page is installed to /usr/share/man/man8, under +DESTDIR too, and MANDIR changes its location. Metric definitions
@@ -84,6 +86,7 @@ The telemetry data files can only be read by root, so 'stat' must run as root; 'list' doesn't need to. 'pmtctl --help' and 'pmtctl <command> --help' describe all options. +The pmtctl(8) man page describes them in more detail. Exit status
tools/arch/x86/pmtctl/pmtctl.8 new file +284 -0
284 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v2 12/12 tools/arch/x86/pmtctl: Add MAINTAINERS entry
In reply to David E. BoxAdd tools/arch/x86/pmtctl/ to the INTEL PMT DRIVERS entry, so that patches to the tool reach the maintainer of the drivers it uses.
Also list [email protected] in the entry. The PMT driver files already reach the list through X86 PLATFORM DRIVERS, but tools/ is not covered by it, so without this the tool's patches would not be sent to the list where the drivers are reviewed.
---
V2 changes:
- Replaces v1 01, moved last so the path exists. The section is now INTEL PMT DRIVERS.
- Also lists platform-driver-x86, which tools/ otherwise doesn't reach.
MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+)
MAINTAINERS +2 -0
@@ -13716,10 +13716,12 @@ F: include/linux/mfd/intel_soc_pmic* INTEL PMT DRIVERS M: David E. Box <[email protected]> +L: [email protected] S: Supported F: Documentation/ABI/testing/sysfs-class-intel_pmt F: Documentation/ABI/testing/sysfs-class-intel_pmt-features F: drivers/platform/x86/intel/pmt/ +F: tools/arch/x86/pmtctl/ INTEL PRO/WIRELESS 2100, 2200BG, 2915ABG NETWORK CONNECTION SUPPORT M: Stanislav Yakovlev <[email protected]>
Signature · 1 line
2.53.0