From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: "David E. Box" <david.e.box@linux.intel.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
platform-driver-x86@vger.kernel.org
Subject: Re: [PATCH 04/17] tools/arch/x86/pmtctl: Add libpmtctl metric definition database
Date: Tue, 26 May 2026 13:06:46 +0300 (EEST) [thread overview]
Message-ID: <2927c82d-e4fc-8a6a-41d3-fd182b40b997@linux.intel.com> (raw)
In-Reply-To: <20260526014719.2248380-5-david.e.box@linux.intel.com>
On Mon, 25 May 2026, David E. Box wrote:
> Add a growable container for PMT metric definitions to support loading and
> querying hardware metric descriptors at runtime.
>
> PMT metrics vary by platform and are discovered dynamically from sysfs. A
> block-based database allows metric definitions to be appended as they are
> parsed, without requiring the final size to be known in advance, while
> bounding individual memory allocations.
>
> Assisted-by: GitHub-Copilot:claude-sonnet-4.6
> Signed-off-by: David E. Box <david.e.box@linux.intel.com>
> ---
> .../arch/x86/pmtctl/include/lib/metrics_db.h | 69 +++++++++++++++++++
> tools/arch/x86/pmtctl/lib/metrics_db.c | 62 +++++++++++++++++
> 2 files changed, 131 insertions(+)
> create mode 100644 tools/arch/x86/pmtctl/include/lib/metrics_db.h
> create mode 100644 tools/arch/x86/pmtctl/lib/metrics_db.c
>
> diff --git a/tools/arch/x86/pmtctl/include/lib/metrics_db.h b/tools/arch/x86/pmtctl/include/lib/metrics_db.h
> new file mode 100644
> index 000000000000..9dff27d8785a
> --- /dev/null
> +++ b/tools/arch/x86/pmtctl/include/lib/metrics_db.h
> @@ -0,0 +1,69 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +#ifndef PMTCTL_METRICS_DB_H
> +#define PMTCTL_METRICS_DB_H
> +
> +#include <stdbool.h>
> +#include <stddef.h>
> +#include <stdint.h>
> +
> +#include "lib/pmt_guid.h"
> +
> +/* Metric definition from builtin or JSON */
> +struct pmt_metric_def {
> + const char *event_name;
> + const char *description;
> + const char *group;
> + const char *platform_group;
> +
> + const struct pmt_guid *guid;
> + uint32_t sample_id;
> + uint8_t lsb;
> + uint8_t msb;
> +};
> +
> +struct pmt_metrics_block {
> + const struct pmt_metric_def *defs;
> + int count;
> + bool is_builtin;
> +};
> +
> +struct pmt_metrics_db {
> + struct pmt_metrics_block *blocks;
> + int nblocks;
> + int total; /* sum of all block->count */
> +};
> +
> +/* Treat DB as a flat array [0..total-1] */
> +static inline const struct pmt_metric_def *pmt_metrics_at(const struct pmt_metrics_db *db, int idx)
inline???
> +{
> + if (!db || idx < 0 || idx >= db->total)
> + return NULL;
> +
> + for (int i = 0; i < db->nblocks; i++) {
> + if (idx < db->blocks[i].count)
> + return &db->blocks[i].defs[idx];
> + idx -= db->blocks[i].count;
> + }
> + return NULL;
> +}
> +
> +static inline const struct pmt_metrics_block *
> +pmt_metrics_block_for(const struct pmt_metrics_db *db, int idx)
> +{
> + if (!db || idx < 0 || idx >= db->total)
> + return NULL;
> +
> + for (int i = 0; i < db->nblocks; i++) {
> + if (idx < db->blocks[i].count)
> + return &db->blocks[i];
> + idx -= db->blocks[i].count;
> + }
> +
> + return NULL;
> +}
Code duplication.
> +
> +int pmt_metrics_add_block(struct pmt_metrics_db *db, const struct pmt_metric_def *defs,
> + int count, bool is_builtin);
> +void pmt_metrics_free(struct pmt_metrics_db *db);
> +
> +#endif
> diff --git a/tools/arch/x86/pmtctl/lib/metrics_db.c b/tools/arch/x86/pmtctl/lib/metrics_db.c
> new file mode 100644
> index 000000000000..82e8121a1b98
> --- /dev/null
> +++ b/tools/arch/x86/pmtctl/lib/metrics_db.c
> @@ -0,0 +1,62 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include <errno.h>
> +#include <limits.h>
> +#include <stdlib.h>
> +#include <string.h>
> +
> +#include "lib/metrics_db.h"
> +
> +int pmt_metrics_add_block(struct pmt_metrics_db *db, const struct pmt_metric_def *defs,
> + int count, bool is_builtin)
> +{
> + struct pmt_metrics_block *blocks;
> + size_t alloc_size;
> + int new_cap;
> +
> + if (!db || !defs || count <= 0)
> + return -EINVAL;
> +
> + if (db->nblocks > INT_MAX - 1)
> + return -EOVERFLOW;
> +
> + if (db->total > INT_MAX - count)
> + return -EOVERFLOW;
> +
> + new_cap = db->nblocks + 1;
> +
> + if ((size_t)new_cap > SIZE_MAX / sizeof(*blocks))
Casts like this likely indicates some typing issue in your code.
> + return -EOVERFLOW;
> +
> + alloc_size = (size_t)new_cap * sizeof(*blocks);
> +
> + blocks = realloc(db->blocks, alloc_size);
So this reallocs + 1 every time??
> + if (!blocks)
> + return -ENOMEM;
> +
> + db->blocks = blocks;
> +
> + db->blocks[db->nblocks].defs = defs;
> + db->blocks[db->nblocks].count = count;
> + db->blocks[db->nblocks].is_builtin = is_builtin;
> +
> + db->nblocks++;
> + db->total += count;
> +
> + return 0;
> +}
> +
> +void pmt_metrics_free(struct pmt_metrics_db *db)
> +{
> + if (!db)
> + return;
> +
> + if (db->blocks) {
> + for (int i = 0; i < db->nblocks; i++) {
> + if (!db->blocks[i].is_builtin && db->blocks[i].defs)
> + free((void *)db->blocks[i].defs);
> + }
> + free(db->blocks);
> + }
> +
> + memset(db, 0, sizeof(*db));
--
i.
next prev parent reply other threads:[~2026-05-26 10:06 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 [this message]
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 ` [PATCH 06/17] tools/arch/x86/pmtctl: Add libpmtctl built-in metric provider David E. Box
2026-05-26 1:47 ` [PATCH 07/17] tools/arch/x86/pmtctl: Add libpmtctl JSON " 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=2927c82d-e4fc-8a6a-41d3-fd182b40b997@linux.intel.com \
--to=ilpo.jarvinen@linux.intel.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=david.e.box@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