From: Swapnil Sapkal <swapnil.sapkal@amd.com>
To: <tony.luck@intel.com>, <reinette.chatre@intel.com>,
<babu.moger@amd.com>, <fenghua.yu@intel.com>, <shuah@kernel.org>
Cc: <Dave.Martin@arm.com>, <james.morse@arm.com>,
<peternewman@google.com>, <eranian@google.com>,
<tan.shaopeng@fujitsu.com>, <linux-kernel@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>, <x86@kernel.org>,
<swapnil.sapkal@amd.com>
Subject: [PATCH v6 3/5] selftests/resctrl: Add support for MBM and MBA tests on AMD
Date: Tue, 1 Dec 2026 08:34:24 +0000 [thread overview]
Message-ID: <20261201083426.26730-4-swapnil.sapkal@amd.com> (raw)
In-Reply-To: <20261201083426.26730-1-swapnil.sapkal@amd.com>
From: Babu Moger <babu.moger@amd.com>
The MBM and MBA tests validate the resctrl memory bandwidth numbers
against the memory controller performance counters. On Intel these are
the iMC (Integrated Memory Controller) counters and the read event and
umask are discovered from the PMU's sysfs event files.
Commit 25e56847821f ("perf/x86/amd/uncore: Add memory controller
support") added support for the UMC (Unified Memory Controller) perf
events on AMD. Unlike Intel, the UMC PMU does not expose the CAS command
events via sysfs, so program the read event directly:
Number of CAS commands sent for reads: EventCode = 0x0a, umask = 0x1.
Keep all the vendor detection contained in enumerate_mcs() and dispatch
to a vendor specific routine to initialize the counter configuration.
Only reading the counter's perf "type" is common to both vendors, so
factor that out into a shared helper.
On AMD each memory controller (UMC) is exposed as its own PMU
(amd_umc_<n>) that is tied to a specific NUMA node, whereas on Intel
opening an iMC event on the benchmark CPU always resolves to the local
memory controllers. A UMC PMU can only be opened from a CPU that belongs
to the same node and returns -ENODEV otherwise. The benchmark is bound
to a single CPU for the duration of the test, so a UMC that cannot be
opened will never become usable. Drop such memory controllers from
mc_counters_list so that later runs do not retry them and the remaining
users of the list can rely on every entry being valid. This both avoids
the perf_event_open() failure and matches the local traffic accounted by
mbm_local_bytes.
Signed-off-by: Babu Moger <babu.moger@amd.com>
Co-developed-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
Signed-off-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
---
v5->v6:
- Named the function pointer read_from_mc_dir to avoid the unnecessary
churn at the call site. (Reinette)
- Drop memory controllers that fail perf_event_open() with -ENODEV from
mc_counters_list instead of leaving them with fd == -1. The benchmark
is bound to a single CPU, so such a counter can never become usable.
This avoids retrying them on every measurement and lets the remaining
list walkers rely on every entry being valid, removing the fd checks
in do_mc_read_mem_bw_test() and get_read_mem_bw_mc() and the 'opened'
tracking (now list_empty()). (Reinette)
v4->v5:
- Reworked for the linked-list counter management and the AMD per-node
UMC PMU topology.
tools/testing/selftests/resctrl/resctrl_val.c | 123 ++++++++++++++----
1 file changed, 101 insertions(+), 22 deletions(-)
diff --git a/tools/testing/selftests/resctrl/resctrl_val.c b/tools/testing/selftests/resctrl/resctrl_val.c
index a97604e33609..8e96a67b63ab 100644
--- a/tools/testing/selftests/resctrl/resctrl_val.c
+++ b/tools/testing/selftests/resctrl/resctrl_val.c
@@ -11,6 +11,7 @@
#include "resctrl.h"
#define INTEL_IMC "uncore_imc_"
+#define AMD_UMC "amd_umc_"
#define READ_FILE_NAME "cas_count_read"
#define DYN_PMU_PATH "/sys/bus/event_source/devices"
#define SCALE 0.00006103515625
@@ -94,12 +95,8 @@ static int open_perf_read_event(int cpu_no, struct mc_counter_config *mc_counter
mc_counter->fd = perf_event_open(&mc_counter->pe, -1, cpu_no, -1,
PERF_FLAG_FD_CLOEXEC);
- if (mc_counter->fd == -1) {
- fprintf(stderr, "Error opening leader %llx\n",
- mc_counter->pe.config);
-
- return -1;
- }
+ if (mc_counter->fd == -1)
+ return -errno;
return 0;
}
@@ -183,16 +180,14 @@ static int parse_mc_read_bw_events(char *mc_dir, unsigned int type)
return ret;
}
-/* Get type and config of an MC counter's read event. */
-static int read_from_mc_dir(char *mc_dir)
+/* Get the perf "type" of a memory controller counter. Shared by all vendors. */
+static int get_mc_counter_type(const char *mc_dir, unsigned int *type)
{
char mc_counter_type[PATH_MAX];
- unsigned int type;
int path_len;
FILE *fp;
int ret;
- /* Get type of MC counter */
path_len = snprintf(mc_counter_type, sizeof(mc_counter_type),
"%s%s", mc_dir, "type");
if (path_len >= sizeof(mc_counter_type)) {
@@ -206,12 +201,29 @@ static int read_from_mc_dir(char *mc_dir)
return -1;
}
- ret = fscanf(fp, "%u", &type);
+ ret = fscanf(fp, "%u", type);
fclose(fp);
if (ret <= 0) {
ksft_perror("Could not get MC type");
return -1;
}
+
+ return 0;
+}
+
+/*
+ * Intel: get type and config of an iMC counter's read event. The read event
+ * and umask are discovered from the PMU's sysfs event files.
+ */
+static int read_from_intel_mc_dir(char *mc_dir)
+{
+ unsigned int type;
+ int ret;
+
+ ret = get_mc_counter_type(mc_dir, &type);
+ if (ret)
+ return ret;
+
ret = parse_mc_read_bw_events(mc_dir, type);
if (ret) {
ksft_print_msg("Unable to parse bandwidth event and umask\n");
@@ -222,10 +234,42 @@ static int read_from_mc_dir(char *mc_dir)
}
/*
- * Discover the properties of the available MC (Memory Controller)
- * counters in support of needed performance measurement via perf. For each MC
- * counter get it's type and config. Also obtain each counter's event and umask
- * for the memory read events that will be measured.
+ * AMD: get type and config of a UMC counter's read event. The UMC PMU does
+ * not expose the CAS command events via sysfs, so program them directly:
+ * Number of CAS commands sent for reads: EventCode = 0x0a, umask = 0x1
+ */
+static int read_from_amd_mc_dir(char *mc_dir)
+{
+ struct mc_counter_config *mc_counter;
+ unsigned int type;
+ int ret;
+
+ ret = get_mc_counter_type(mc_dir, &type);
+ if (ret)
+ return ret;
+
+ mc_counter = calloc(1, sizeof(*mc_counter));
+ if (!mc_counter) {
+ ksft_perror("Unable to allocate memory for MC counters");
+ return -1;
+ }
+
+ mc_counter->type = type;
+ mc_counter->event = 0xa;
+ mc_counter->umask = 0x1;
+ list_add(&mc_counter->entry, &mc_counters_list);
+
+ return 0;
+}
+
+/*
+ * Discover the properties of the available MC (Memory Controller) counters.
+ * On Intel the memory controller is called Integrated Memory Controller (iMC)
+ * and on AMD it is called Unified Memory Controller (UMC). Discover the
+ * properties of the available counters in support of needed performance
+ * measurement via perf. For each MC counter get it's type and config. Also
+ * obtain each counter's event and umask for the memory read events that will
+ * be measured.
*
* Enumerate all these details into a linked list of structures.
*
@@ -233,6 +277,7 @@ static int read_from_mc_dir(char *mc_dir)
*/
static int enumerate_mcs(void)
{
+ int (*read_from_mc_dir)(char *mc_dir);
char mc_dir[512], *temp;
struct dirent *ep;
char *sysfs_name;
@@ -241,6 +286,10 @@ static int enumerate_mcs(void)
if (get_vendor() == ARCH_INTEL) {
sysfs_name = INTEL_IMC;
+ read_from_mc_dir = read_from_intel_mc_dir;
+ } else if (get_vendor() == ARCH_AMD) {
+ sysfs_name = AMD_UMC;
+ read_from_mc_dir = read_from_amd_mc_dir;
} else {
ksft_print_msg("Unsupported vendor\n");
return -1;
@@ -254,8 +303,9 @@ static int enumerate_mcs(void)
continue;
/*
- * mc counters are named as "uncore_imc_<n>", hence
- * increment the pointer to point to <n>.
+ * MC counters are named as "uncore_imc_<n>" on Intel
+ * and "amd_umc_<n>" on AMD, hence increment the pointer
+ * to point to <n>.
*/
temp = temp + strlen(sysfs_name);
@@ -277,7 +327,9 @@ static int enumerate_mcs(void)
}
closedir(dp);
if (list_empty(&mc_counters_list)) {
- ksft_print_msg("Unable to find MC counters\n");
+ ksft_print_msg("Unable to find memory controller counters\n");
+ if (get_vendor() == ARCH_AMD)
+ ksft_print_msg("Try loading amd_uncore module\n");
return -1;
}
@@ -334,16 +386,43 @@ static void perf_close_mc_read_mem_bw(void)
*/
static int perf_open_mc_read_mem_bw(int cpu_no)
{
- struct mc_counter_config *mc_counter;
+ struct mc_counter_config *mc_counter, *tmp;
int ret;
list_for_each_entry(mc_counter, &mc_counters_list, entry)
mc_counter->fd = -1;
- list_for_each_entry(mc_counter, &mc_counters_list, entry) {
+ list_for_each_entry_safe(mc_counter, tmp, &mc_counters_list, entry) {
ret = open_perf_read_event(cpu_no, mc_counter);
- if (ret)
- goto close_fds;
+ if (!ret)
+ continue;
+
+ /*
+ * On AMD each memory controller (UMC) has its own PMU that is
+ * tied to a specific NUMA node, unlike Intel where opening an
+ * iMC event on the benchmark CPU always resolves to the local
+ * memory controllers. A UMC PMU can only be opened from a CPU
+ * that belongs to the same node and returns -ENODEV otherwise.
+ * The benchmark is bound to a single CPU for the duration of
+ * the test, so a UMC that cannot be opened now will never be
+ * usable. Drop it to measure only the memory controllers local
+ * to the benchmark. This matches what mbm_local_bytes accounts
+ * for on the resctrl side.
+ */
+ if (ret == -ENODEV && get_vendor() == ARCH_AMD) {
+ list_del(&mc_counter->entry);
+ free(mc_counter);
+ continue;
+ }
+
+ ksft_print_msg("Error opening memory bandwidth performance counter: %s\n",
+ strerror(-ret));
+ goto close_fds;
+ }
+
+ if (list_empty(&mc_counters_list)) {
+ ksft_print_msg("Could not open any memory controller counters\n");
+ goto close_fds;
}
return 0;
--
2.43.0
next prev parent reply other threads:[~2026-12-01 8:36 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 8:34 [PATCH v6 0/5] selftests/resctrl: Add MBM and MBA test support for AMD Swapnil Sapkal
2026-12-01 8:34 ` [PATCH v6 1/5] selftests/resctrl: Rename variables and functions to generic names Swapnil Sapkal
2026-12-01 8:34 ` [PATCH v6 2/5] selftests/resctrl: Detect vendor to select memory controller sysfs name Swapnil Sapkal
2026-12-01 8:34 ` Swapnil Sapkal [this message]
2026-12-01 8:34 ` [PATCH v6 4/5] selftests/resctrl: Use bandwidth values for AMD MBA schemata Swapnil Sapkal
2026-12-01 8:34 ` [PATCH v6 5/5] selftests/resctrl: Enable MBM and MBA tests on AMD Swapnil Sapkal
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=20261201083426.26730-4-swapnil.sapkal@amd.com \
--to=swapnil.sapkal@amd.com \
--cc=Dave.Martin@arm.com \
--cc=babu.moger@amd.com \
--cc=eranian@google.com \
--cc=fenghua.yu@intel.com \
--cc=james.morse@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=peternewman@google.com \
--cc=reinette.chatre@intel.com \
--cc=shuah@kernel.org \
--cc=tan.shaopeng@fujitsu.com \
--cc=tony.luck@intel.com \
--cc=x86@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®