[PATCH v2 09/12] tools/arch/x86/pmtctl: Add 'stat --raw'

From: David E. Box

Date: Sun Nov 22 2026 - 14:35:02 EST


Add --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.

Signed-off-by: David E. Box <david.e.box@xxxxxxxxxxxxxxx>
---
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(-)

diff --git a/tools/arch/x86/pmtctl/src/cmd_stat.c b/tools/arch/x86/pmtctl/src/cmd_stat.c
index 8377c2d1d194..643686d067e6 100644
--- a/tools/arch/x86/pmtctl/src/cmd_stat.c
+++ b/tools/arch/x86/pmtctl/src/cmd_stat.c
@@ -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, size
for (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 *bu
static 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 t

print_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 */
--
2.53.0