public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Reinette Chatre <reinette.chatre@intel.com>
Cc: shuah@kernel.org, Dave.Martin@arm.com, james.morse@arm.com,
	 tony.luck@intel.com, babu.moger@amd.com, fenghuay@nvidia.com,
	 peternewman@google.com, zide.chen@intel.com,
	dapeng1.mi@linux.intel.com,  ben.horgan@arm.com,
	yu.c.chen@intel.com, jason.zeng@intel.com,
	 linux-kselftest@vger.kernel.org,
	LKML <linux-kernel@vger.kernel.org>,
	 patches@lists.linux.dev
Subject: Re: [PATCH v3 05/10] selftests/resctrl: Support multiple events associated with iMC
Date: Fri, 27 Mar 2026 19:28:50 +0200 (EET)	[thread overview]
Message-ID: <cb4cc6f7-a7be-3d45-a484-5802b0ea0db3@linux.intel.com> (raw)
In-Reply-To: <f580adeab0d8369392f9cc1421aa0806a4f32e07.1773432891.git.reinette.chatre@intel.com>

[-- Attachment #1: Type: text/plain, Size: 7402 bytes --]

On Fri, 13 Mar 2026, Reinette Chatre wrote:

> The resctrl selftests discover needed parameters to perf_event_open() via
> sysfs. The PMU associated with every memory controller (iMC) is discovered
> via the /sys/bus/event_source/devices/uncore_imc_N/type file while
> the read memory bandwidth event type and umask is discovered via
> /sys/bus/event_source/devices/uncore_imc_N/events/cas_count_read.
> 
> Newer systems may have multiple events that expose read memory bandwidth.
> Running a recent kernel that includes
> commit 6a8a48644c4b ("perf/x86/intel/uncore: Add per-scheduler IMC CAS count events")
> on these systems expose the multiple events. For example,
>  /sys/bus/event_source/devices/uncore_imc_N/events/cas_count_read_sch0
>  /sys/bus/event_source/devices/uncore_imc_N/events/cas_count_read_sch1
> 
> Support parsing of iMC PMU properties when the PMU may have multiple events
> to measure read memory bandwidth. The PMU only needs to be discovered once.
> Split the parsing of event details from actual PMU discovery in order to
> loop over all events associated with the PMU. Match all events with the
> cas_count_read prefix instead of requiring there to be one file with that
> name.
> 
> Make the parsing code more robust. With strings passed around to create
> needed paths, use snprintf() instead of sprintf() to ensure there is
> always enough space to create the path while using the standard PATH_MAX
> for path lengths. Ensure there is enough room in imc_counters_config[]
> before attempting to add an entry.
> 
> Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
> Tested-by: Chen Yu <yu.c.chen@intel.com>
> Reviewed-by: Zide Chen <zide.chen@intel.com>

Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

-- 
 i.

> ---
> Changes since v1:
> - Add Zide Chen's RB tag.
> 
> Changes since v2:
> - Update changelog to note merged perf change that supports this change.
> - Use PATH_MAX instead of magic number for path lengths. (Ilpo)
> - Rename "org_count" -> "orig_count". (Ilpo)
> - Rework flow surrounding fscanf() used in both parse_imc_read_bw_events()
>   and read_from_imc_dir(). (Ilpo)
> - Handle error first to reduce indentation. (Ilpo)
> - Add Chen Yu's tag.
> ---
>  tools/testing/selftests/resctrl/resctrl_val.c | 118 ++++++++++++++----
>  1 file changed, 93 insertions(+), 25 deletions(-)
> 
> diff --git a/tools/testing/selftests/resctrl/resctrl_val.c b/tools/testing/selftests/resctrl/resctrl_val.c
> index 6d766347e3fc..f20d2194c35f 100644
> --- a/tools/testing/selftests/resctrl/resctrl_val.c
> +++ b/tools/testing/selftests/resctrl/resctrl_val.c
> @@ -11,10 +11,10 @@
>  #include "resctrl.h"
>  
>  #define UNCORE_IMC		"uncore_imc"
> -#define READ_FILE_NAME		"events/cas_count_read"
> +#define READ_FILE_NAME		"cas_count_read"
>  #define DYN_PMU_PATH		"/sys/bus/event_source/devices"
>  #define SCALE			0.00006103515625
> -#define MAX_IMCS		20
> +#define MAX_IMCS		40
>  #define MAX_TOKENS		5
>  
>  #define CON_MBM_LOCAL_BYTES_PATH		\
> @@ -109,46 +109,114 @@ static int open_perf_read_event(int i, int cpu_no)
>  	return 0;
>  }
>  
> -/* Get type and config of an iMC counter's read event. */
> -static int read_from_imc_dir(char *imc_dir, unsigned int *count)
> +static int parse_imc_read_bw_events(char *imc_dir, unsigned int type,
> +				    unsigned int *count)
>  {
> -	char cas_count_cfg[1024], imc_counter_cfg[1024], imc_counter_type[1024];
> +	char imc_events_dir[PATH_MAX], imc_counter_cfg[PATH_MAX];
> +	unsigned int orig_count = *count;
> +	char cas_count_cfg[1024];
> +	struct dirent *ep;
> +	int path_len;
> +	int ret = -1;
> +	int num_cfg;
>  	FILE *fp;
> +	DIR *dp;
>  
> -	/* Get type of iMC counter */
> -	sprintf(imc_counter_type, "%s%s", imc_dir, "type");
> -	fp = fopen(imc_counter_type, "r");
> -	if (!fp) {
> -		ksft_perror("Failed to open iMC counter type file");
> +	path_len = snprintf(imc_events_dir, sizeof(imc_events_dir), "%sevents",
> +			    imc_dir);
> +	if (path_len >= sizeof(imc_events_dir)) {
> +		ksft_print_msg("Unable to create path to %sevents\n", imc_dir);
> +		return -1;
> +	}
>  
> +	dp = opendir(imc_events_dir);
> +	if (!dp) {
> +		ksft_perror("Unable to open PMU events directory");
>  		return -1;
>  	}
> -	if (fscanf(fp, "%u", &imc_counters_config[*count].type) <= 0) {
> -		ksft_perror("Could not get iMC type");
> +
> +	while ((ep = readdir(dp))) {
> +		/*
> +		 * Parse all event files with READ_FILE_NAME prefix that
> +		 * contain the event number and umask. Skip files containing
> +		 * "." that contain unused properties of event.
> +		 */
> +		if (!strstr(ep->d_name, READ_FILE_NAME) ||
> +		    strchr(ep->d_name, '.'))
> +			continue;
> +
> +		path_len = snprintf(imc_counter_cfg, sizeof(imc_counter_cfg),
> +				    "%s/%s", imc_events_dir, ep->d_name);
> +		if (path_len >= sizeof(imc_counter_cfg)) {
> +			ksft_print_msg("Unable to create path to %s/%s\n",
> +				       imc_events_dir, ep->d_name);
> +			goto out_close;
> +		}
> +		fp = fopen(imc_counter_cfg, "r");
> +		if (!fp) {
> +			ksft_perror("Failed to open iMC config file");
> +			goto out_close;
> +		}
> +		num_cfg = fscanf(fp, "%1023s", cas_count_cfg);
>  		fclose(fp);
> +		if (num_cfg <= 0) {
> +			ksft_perror("Could not get iMC cas count read");
> +			goto out_close;
> +		}
> +		if (*count >= MAX_IMCS) {
> +			ksft_print_msg("Maximum iMC count exceeded\n");
> +			goto out_close;
> +		}
>  
> -		return -1;
> +		imc_counters_config[*count].type = type;
> +		get_read_event_and_umask(cas_count_cfg, *count);
> +		/* Do not fail after incrementing *count. */
> +		*count += 1;
>  	}
> -	fclose(fp);
> +	if (*count == orig_count) {
> +		ksft_print_msg("Unable to find events in %s\n", imc_events_dir);
> +		goto out_close;
> +	}
> +	ret = 0;
> +out_close:
> +	closedir(dp);
> +	return ret;
> +}
>  
> -	/* Get read config */
> -	sprintf(imc_counter_cfg, "%s%s", imc_dir, READ_FILE_NAME);
> -	fp = fopen(imc_counter_cfg, "r");
> -	if (!fp) {
> -		ksft_perror("Failed to open iMC config file");
> +/* Get type and config of an iMC counter's read event. */
> +static int read_from_imc_dir(char *imc_dir, unsigned int *count)
> +{
> +	char imc_counter_type[PATH_MAX];
> +	unsigned int type;
> +	int path_len;
> +	FILE *fp;
> +	int ret;
>  
> +	/* Get type of iMC counter */
> +	path_len = snprintf(imc_counter_type, sizeof(imc_counter_type),
> +			    "%s%s", imc_dir, "type");
> +	if (path_len >= sizeof(imc_counter_type)) {
> +		ksft_print_msg("Unable to create path to %s%s\n",
> +			       imc_dir, "type");
>  		return -1;
>  	}
> -	if (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
> -		ksft_perror("Could not get iMC cas count read");
> -		fclose(fp);
> +	fp = fopen(imc_counter_type, "r");
> +	if (!fp) {
> +		ksft_perror("Failed to open iMC counter type file");
>  
>  		return -1;
>  	}
> +	ret = fscanf(fp, "%u", &type);
>  	fclose(fp);
> -
> -	get_read_event_and_umask(cas_count_cfg, *count);
> -	*count += 1;
> +	if (ret <= 0) {
> +		ksft_perror("Could not get iMC type");
> +		return -1;
> +	}
> +	ret = parse_imc_read_bw_events(imc_dir, type, count);
> +	if (ret) {
> +		ksft_print_msg("Unable to parse bandwidth event and umask\n");
> +		return ret;
> +	}
>  
>  	return 0;
>  }
> 

  reply	other threads:[~2026-03-27 17:29 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-13 20:32 [PATCH v3 00/10] selftests/resctrl: Fixes and improvements focused on Intel platforms Reinette Chatre
2026-03-13 20:32 ` [PATCH v3 01/10] selftests/resctrl: Improve accuracy of cache occupancy test Reinette Chatre
2026-03-26 12:44   ` Ilpo Järvinen
2026-03-13 20:32 ` [PATCH v3 02/10] selftests/resctrl: Reduce interference from L2 occupancy during " Reinette Chatre
2026-03-26 12:56   ` Ilpo Järvinen
2026-03-13 20:32 ` [PATCH v3 03/10] selftests/resctrl: Do not store iMC counter value in counter config structure Reinette Chatre
2026-03-13 20:32 ` [PATCH v3 04/10] selftests/resctrl: Prepare for parsing multiple events per iMC Reinette Chatre
2026-03-26 13:03   ` Ilpo Järvinen
2026-03-26 14:34     ` Reinette Chatre
2026-03-13 20:32 ` [PATCH v3 05/10] selftests/resctrl: Support multiple events associated with iMC Reinette Chatre
2026-03-27 17:28   ` Ilpo Järvinen [this message]
2026-03-13 20:32 ` [PATCH v3 06/10] selftests/resctrl: Increase size of buffer used in MBM and MBA tests Reinette Chatre
2026-03-27 17:30   ` Ilpo Järvinen
2026-03-13 20:32 ` [PATCH v3 07/10] selftests/resctrl: Raise threshold at which MBM and PMU values are compared Reinette Chatre
2026-03-27 17:34   ` Ilpo Järvinen
2026-03-27 23:19     ` Reinette Chatre
2026-03-13 20:32 ` [PATCH v3 08/10] selftests/resctrl: Remove requirement on cache miss rate Reinette Chatre
2026-03-27 17:45   ` Ilpo Järvinen
2026-03-27 23:21     ` Reinette Chatre
2026-03-31  8:07       ` Ilpo Järvinen
2026-03-31 17:39         ` Reinette Chatre
2026-03-13 20:32 ` [PATCH v3 09/10] selftests/resctrl: Simplify perf usage in CAT test Reinette Chatre
2026-03-27 17:47   ` Ilpo Järvinen
2026-03-13 20:32 ` [PATCH v3 10/10] selftests/resctrl: Reduce L2 impact on " Reinette Chatre
2026-03-27 17:49   ` Ilpo Järvinen
2026-03-27 23:22     ` Reinette Chatre
2026-03-31 19:13 ` [PATCH v3 00/10] selftests/resctrl: Fixes and improvements focused on Intel platforms Shuah Khan
2026-03-31 20:22   ` Reinette Chatre

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=cb4cc6f7-a7be-3d45-a484-5802b0ea0db3@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=Dave.Martin@arm.com \
    --cc=babu.moger@amd.com \
    --cc=ben.horgan@arm.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=fenghuay@nvidia.com \
    --cc=james.morse@arm.com \
    --cc=jason.zeng@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=patches@lists.linux.dev \
    --cc=peternewman@google.com \
    --cc=reinette.chatre@intel.com \
    --cc=shuah@kernel.org \
    --cc=tony.luck@intel.com \
    --cc=yu.c.chen@intel.com \
    --cc=zide.chen@intel.com \
    /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