linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Liang, Kan" <kan.liang@linux.intel.com>
To: weilin.wang@intel.com, Ian Rogers <irogers@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>
Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	Perry Taylor <perry.taylor@intel.com>,
	Samantha Alt <samantha.alt@intel.com>,
	Caleb Biggers <caleb.biggers@intel.com>,
	Mark Rutland <mark.rutland@arm.com>
Subject: Re: [RFC PATCH 07/25] perf stat: Add helper functions for hardware-grouping method
Date: Tue, 26 Sep 2023 11:28:54 -0400	[thread overview]
Message-ID: <f6805f28-9083-1369-3886-a9aeca2908c0@linux.intel.com> (raw)
In-Reply-To: <20230925061824.3818631-8-weilin.wang@intel.com>



On 2023-09-25 2:18 a.m., weilin.wang@intel.com wrote:
> From: Weilin Wang <weilin.wang@intel.com>
> 
> Add metricgroup__event_info data structure to represent an event in the
> metric grouping context; the list of counters and the PMU name an event
> should be collected with.
> 
> Add functions to parse event counter info from pmu-events and generate a
> list of metricgroup__event_info data to prepare grouping.
> 
> Signed-off-by: Weilin Wang <weilin.wang@intel.com>
> ---
>  tools/perf/util/metricgroup.c | 197 +++++++++++++++++++++++++++++++++-
>  tools/perf/util/metricgroup.h |  18 ++++
>  2 files changed, 212 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
> index 063c92c71..7f2c1b017 100644
> --- a/tools/perf/util/metricgroup.c
> +++ b/tools/perf/util/metricgroup.c
> @@ -1432,6 +1432,183 @@ static int build_combined_expr_ctx(const struct list_head *metric_list,
>  	return ret;
>  }
>  
> +/**
> + * set_counter_bitmap - The counter bit mapping: [8-15,0-7], e.g. the GP0 is the
> + * 8th bit and GP7 is the 1st bit in this 16-bits bitmap. The is helpful to
> + * assign GP4-7 before GP0-3 because some events can be collected using GP0-3
> + * only on some platforms.
> + */
> +static int set_counter_bitmap(int pos, unsigned long *bitmap)
> +{
> +	if (pos >= NR_COUNTERS || pos < 0)
> +		return -EINVAL;
> +	if (pos <= 7)
> +		pos = 7 - pos;
> +	else
> +		pos = 23 - pos;

What's 23? Please use macro to replace all the magic number.

Maybe it's easier to always assign from the last available counter. The
first several counters are usually the special counters.

> +	*bitmap |= 1ul << pos;
> +	return 0;
> +}
> +
> +static int parse_fixed_counter(const char *counter,
> +			      unsigned long *bitmap,
> +			      bool *fixed)
> +{
> +	int ret = -ENOENT;
> +	//TODO: this pattern is different on some other platforms
> +	const char *pattern = "Fixed counter ";
> +	int pos = 0;
> +
> +	if (!strncmp(counter, pattern, strlen(pattern))) {
> +		pos = atoi(counter + strlen(pattern));
> +		ret = set_counter_bitmap(pos, bitmap);


The fixed counter, e.g., cycles, instructions, ref-cycles (after SPR),
are usually expanded to GP counters as well in the kernel. You may want
to set both fixed counter and GP counter for them.

Thanks,
Kan

> +		if (ret)
> +			return ret;
> +		*fixed = true;
> +		return 0;
> +	}
> +	return ret;
> +}
> +
> +/**
> + * parse_counter - Parse event counter info from pmu-events and set up bitmap
> + * accordingly.
> + *
> + * @counter: counter info string to be parsed.
> + * @bitmap: bitmap to set based on counter info parsed.
> + * @fixed: is set to true if the event uses fixed counter.
> + */
> +static int parse_counter(const char *counter,
> +			unsigned long *bitmap,
> +			bool *fixed)
> +{
> +	int ret = 0;
> +	char *p;
> +	char *tok;
> +	int pos = 0;
> +
> +	ret = parse_fixed_counter(counter, bitmap, fixed);
> +	// ret==0 means matched with fixed counter
> +	if (ret == 0)
> +		return ret;
> +
> +	p = strdup(counter);
> +	tok = strtok(p, ",");
> +	if (!tok)
> +		return -ENOENT;
> +
> +	while (tok) {
> +		pos = atoi(tok);
> +		ret = set_counter_bitmap(pos, bitmap);
> +		if (ret)
> +			return ret;
> +		tok = strtok(NULL, ",");
> +	}
> +	return 0;
> +}
> +
> +static struct metricgroup__event_info *event_info__new(const char *name,
> +						      const char *pmu_name,
> +						      const char *counter,
> +						      bool free_counter)
> +{
> +	int ret = 0;
> +	char *bit_buf = malloc(NR_COUNTERS);
> +	bool fixed_counter = false;
> +	struct metricgroup__event_info *e;
> +	e = zalloc(sizeof(*e));
> +	if (!e)
> +		return NULL;
> +	if (!pmu_name) {
> +		pmu_name = "core";
> +	}
> +	e->name = name;
> +	e->free_counter = free_counter;
> +	e->pmu_name = strdup(pmu_name);
> +	if (free_counter) {
> +		ret = set_counter_bitmap(0, e->counters);
> +		if (ret)
> +			return NULL;
> +	} else {
> +		ret = parse_counter(counter, e->counters, &fixed_counter);
> +		if (ret)
> +			return NULL;
> +		e->fixed_counter = fixed_counter;
> +	}
> +
> +	bitmap_scnprintf(e->counters, NR_COUNTERS, bit_buf, NR_COUNTERS);
> +	pr_debug("Event %s requires pmu %s counter: %s bitmap %s, [pmu=%s]\n",
> +		e->name, e->pmu_name, counter, bit_buf, pmu_name);
> +
> +	return e;
> +}
> +
> +struct metricgroup__add_metric_event_data {
> +	struct list_head *list;
> +	/* pure event name, exclude umask and other info*/
> +	const char *event_name;
> +	/* event name and umask if applicable*/
> +	const char *event_id;
> +};
> +
> +static int metricgroup__add_metric_event_callback(const struct pmu_event *pe,
> +						 const struct pmu_events_table *table __maybe_unused,
> +						 void *data)
> +{
> +	struct metricgroup__event_info *event;
> +	struct metricgroup__add_metric_event_data *d = data;
> +
> +	if (!strcasecmp(pe->name, d->event_name)) {
> +		event = event_info__new(d->event_id, pe->pmu, pe->counter, /*free_counter=*/false);
> +		if (!event)
> +			return -ENOMEM;
> +		list_add(&event->nd, d->list);
> +	}
> +
> +	return 0;
> +}
> +
> +/**
> + * get_metricgroup_events - Find counter requirement of events from the
> + * pmu_events table
> + * @full_id: the full event identifiers.
> + * @table: pmu_events table that is searched for event data.
> + * @event_info_list: the list that the new event counter info added to.
> + */
> +static int get_metricgroup_events(const char *full_id,
> +				 const struct pmu_events_table *table,
> +				 struct list_head *event_info_list)
> +{
> +	LIST_HEAD(list);
> +	int ret = 0;
> +	const char *id;
> +	const char *rsep, *sep = strchr(full_id, '@');
> +
> +	if (sep) {
> +		rsep = strchr(full_id, ',');
> +		id = strndup(sep + 1, rsep - sep - 1);
> +		if (ret)
> +			goto out;
> +	} else {
> +		id = full_id;
> +	}
> +	{
> +		struct metricgroup__add_metric_event_data data = {
> +			.list = &list,
> +			.event_name = id,
> +			.event_id = full_id,
> +		};
> +		ret = pmu_events_table_for_each_event(table,
> +				metricgroup__add_metric_event_callback, &data);
> +		if (ret)
> +			goto out;
> +	}
> +
> +out:
> +	list_splice(&list, event_info_list);
> +	return ret;
> +}
> +
>  /**
>   * hw_aware_build_grouping - Build event groupings by reading counter
>   * requirement of the events and counter available on the system from
> @@ -1445,9 +1622,25 @@ static int hw_aware_build_grouping(struct expr_parse_ctx *ctx __maybe_unused,
>  				  const char *modifier __maybe_unused)
>  {
>  	int ret = 0;
> +	struct hashmap_entry *cur;
> +	LIST_HEAD(pmu_info_list);
> +	LIST_HEAD(event_info_list);
> +	size_t bkt;
> +	const struct pmu_events_table *etable = pmu_events_table__find();
> +
> +#define RETURN_IF_NON_ZERO(x) do { if (x) return x; } while (0)
> +	hashmap__for_each_entry(ctx->ids, cur, bkt) {
> +		const char *id = cur->pkey;
> +
> +		pr_debug("found event %s\n", id);
> +
> +		ret = get_metricgroup_events(id, etable, &event_info_list);
> +		if (ret)
> +			return ret;
> +	}
>  
> -	pr_debug("This is a placeholder\n");
>  	return ret;
> +#undef RETURN_IF_NON_ZERO
>  }
>  
>  static void group_str_free(struct metricgroup__group_strs *g)
> @@ -1521,8 +1714,6 @@ static int hw_aware_parse_ids(struct perf_pmu *fake_pmu,
>  	*out_evlist = parsed_evlist;
>  	parsed_evlist = NULL;
>  err_out:
> -	parse_events_error__exit(&parse_error);
> -	evlist__delete(parsed_evlist);
>  	metricgroup__free_grouping_strs(&groupings);
>  	return ret;
>  }
> diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h
> index 89809df85..e493f6965 100644
> --- a/tools/perf/util/metricgroup.h
> +++ b/tools/perf/util/metricgroup.h
> @@ -5,6 +5,7 @@
>  #include <linux/list.h>
>  #include <linux/rbtree.h>
>  #include <stdbool.h>
> +#include <linux/bitmap.h>
>  #include "pmu-events/pmu-events.h"
>  #include "strbuf.h"
>  
> @@ -67,6 +68,23 @@ struct metric_expr {
>  	int runtime;
>  };
>  
> +/* Maximum number of counters per PMU*/
> +#define NR_COUNTERS	16
> +/**
> + * An event used in a metric. This info is for metric grouping.
> + */
> +struct metricgroup__event_info {
> +	struct list_head nd;
> +	/** The name of the event. */
> +	const char *name;
> +	/** The name of the pmu the event be collected on. */
> +	const char *pmu_name;
> +	bool fixed_counter;
> +	bool free_counter;
> +	/** The counters the event allowed to be collected on. */
> +	DECLARE_BITMAP(counters, NR_COUNTERS);
> +};
> +
>  /**
>   * Each group is one node in the group string list.
>   */

  reply	other threads:[~2023-09-26 15:29 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-25  6:17 [RFC PATCH 00/25] Perf stat metric grouping with hardware information weilin.wang
2023-09-25  6:18 ` [RFC PATCH 01/25] perf stat: Add hardware-grouping cmd option to perf stat weilin.wang
2023-09-26 14:50   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 02/25] perf stat: Add basic functions for the hardware-grouping stat cmd option weilin.wang
2023-09-26 15:10   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 03/25] perf pmu-events: Add functions in jevent.py weilin.wang
2023-09-26 15:17   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 04/25] perf pmu-events: Add counter info into JSON files for SapphireRapids weilin.wang
2023-09-26 15:20   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 05/25] perf pmu-events: Add event counter data for Cascadelakex weilin.wang
2023-09-25  6:18 ` [RFC PATCH 06/25] perf pmu-events: Add event counter data for Icelakex weilin.wang
2023-09-25  6:18 ` [RFC PATCH 07/25] perf stat: Add helper functions for hardware-grouping method weilin.wang
2023-09-26 15:28   ` Liang, Kan [this message]
2023-09-25  6:18 ` [RFC PATCH 08/25] perf stat: Add functions to get counter info weilin.wang
2023-09-26 15:37   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 09/25] perf stat: Add helper functions for hardware-grouping method weilin.wang
2023-09-26  3:37   ` Yang Jihong
2023-09-26 20:51     ` Wang, Weilin
2023-09-25  6:18 ` [RFC PATCH 10/25] perf stat: Add helper functions to " weilin.wang
2023-09-26  3:44   ` Yang Jihong
2023-09-26 15:55   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 11/25] perf stat: Add utility " weilin.wang
2023-09-26 16:02   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 12/25] perf stat: Add more functions for " weilin.wang
2023-09-25  6:18 ` [RFC PATCH 13/25] perf stat: Add functions to " weilin.wang
2023-09-26 16:18   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 14/25] perf stat: Add build string function and topdown events handling in hardware-grouping weilin.wang
2023-09-26 16:21   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 15/25] perf stat: Add function to combine metrics for hardware-grouping weilin.wang
2023-09-25  6:18 ` [RFC PATCH 16/25] perf stat: Update keyword core to default_core to adjust to the changes for events with no unit weilin.wang
2023-09-26 16:25   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 17/25] perf stat: Handle taken alone in hardware-grouping weilin.wang
2023-09-25  6:18 ` [RFC PATCH 18/25] perf stat: Handle NMI " weilin.wang
2023-09-25  6:18 ` [RFC PATCH 19/25] perf stat: Handle grouping method fall back " weilin.wang
2023-09-25  6:18 ` [RFC PATCH 20/25] perf stat: Code refactoring " weilin.wang
2023-09-25  6:18 ` [RFC PATCH 21/25] perf stat: Add tool events support " weilin.wang
2023-09-25  6:18 ` [RFC PATCH 22/25] perf stat: Add TSC " weilin.wang
2023-09-26 16:35   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 23/25] perf stat: Fix a return error issue " weilin.wang
2023-09-26 16:36   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 24/25] perf stat: Add check to ensure correctness in platform that does not support hardware-grouping weilin.wang
2023-09-26 16:38   ` Liang, Kan
2023-09-25  6:18 ` [RFC PATCH 25/25] perf pmu-events: Add event counter data for Tigerlake weilin.wang
2023-09-26 16:41   ` Liang, Kan
2023-09-25 18:29 ` [RFC PATCH 00/25] Perf stat metric grouping with hardware information Ian Rogers
2023-09-26 20:40   ` Wang, Weilin
2023-09-26 14:43 ` Liang, Kan
2023-09-26 16:48   ` Liang, Kan
2023-09-26 20:40     ` Wang, Weilin

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=f6805f28-9083-1369-3886-a9aeca2908c0@linux.intel.com \
    --to=kan.liang@linux.intel.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=caleb.biggers@intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=perry.taylor@intel.com \
    --cc=peterz@infradead.org \
    --cc=samantha.alt@intel.com \
    --cc=weilin.wang@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;
as well as URLs for NNTP newsgroup(s).