[RFC,10/25] perf stat: Add helper functions to hardware-grouping method

Message ID 20230925061824.3818631-11-weilin.wang@intel.com
State New
Headers
Series Perf stat metric grouping with hardware information |

Commit Message

Wang, Weilin Sept. 25, 2023, 6:18 a.m. UTC
  From: Weilin Wang <weilin.wang@intel.com>

Add struct metricgroup__pmu_group_list to hold the lists of groups from
different PMUs. Each PMU has one separate list.

Add struct metricgroup__group as one node (one group in the grouping
result) of the metricgroup__pmu_group_list. It uses two bitmaps to log
counter availabilities(gp counters and fixed counters).

Add functions to create group and assign event into the groups based on the
event restrictions (struct metricgroup__event_info) and counter
availability (pmu_info_list and bitmaps). New group is inserted into the
list groups.

Signed-off-by: Weilin Wang <weilin.wang@intel.com>
---
 tools/perf/util/metricgroup.c | 72 +++++++++++++++++++++++++++++++++++
 tools/perf/util/metricgroup.h | 37 ++++++++++++++++++
 2 files changed, 109 insertions(+)
  

Comments

Yang Jihong Sept. 26, 2023, 3:44 a.m. UTC | #1
Hello,

On 2023/9/25 14:18, weilin.wang@intel.com wrote:
> From: Weilin Wang <weilin.wang@intel.com>
> 
> Add struct metricgroup__pmu_group_list to hold the lists of groups from
> different PMUs. Each PMU has one separate list.
> 
> Add struct metricgroup__group as one node (one group in the grouping
> result) of the metricgroup__pmu_group_list. It uses two bitmaps to log
> counter availabilities(gp counters and fixed counters).
> 
> Add functions to create group and assign event into the groups based on the
> event restrictions (struct metricgroup__event_info) and counter
> availability (pmu_info_list and bitmaps). New group is inserted into the
> list groups.
> 
> Signed-off-by: Weilin Wang <weilin.wang@intel.com>
> ---
>   tools/perf/util/metricgroup.c | 72 +++++++++++++++++++++++++++++++++++
>   tools/perf/util/metricgroup.h | 37 ++++++++++++++++++
>   2 files changed, 109 insertions(+)
> 
> diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
> index 0ca885a42..de6a6a1d7 100644
> --- a/tools/perf/util/metricgroup.c
> +++ b/tools/perf/util/metricgroup.c
> @@ -1681,6 +1681,76 @@ static int get_pmu_counter_layouts(struct list_head *pmu_info_list,
>   	return ret;
>   }
>   
> +/**
> + * assign_event_grouping - Assign an event into a group. If existing group
> + * cannot include it, create a new group and insert the event to it.
> + */
> +static int assign_event_grouping(struct metricgroup__event_info *e,
> +				struct list_head *pmu_info_list __maybe_unused,
> +				struct list_head *groups)
> +{
> +	int ret = 0;
> +
> +	struct metricgroup__pmu_group_list *g = NULL;
> +	struct metricgroup__pmu_group_list *pmu_group_head = NULL;
> +
> +	list_for_each_entry(g, groups, nd) {
> +		if (!strcasecmp(g->pmu_name, e->pmu_name)) {
> +			pr_debug("found group for event %s in pmu %s\n", e->name, g->pmu_name);
> +			pmu_group_head = g;
> +			break;
> +		}
> +	}
> +	if (!pmu_group_head) {
> +		struct metricgroup__pmu_counters *p;
> +
> +		pmu_group_head = malloc(sizeof(struct metricgroup__pmu_group_list));
Here may need to check the return value of malloc.

> +		INIT_LIST_HEAD(&pmu_group_head->group_head);
> +		pr_debug("create new group for event %s in pmu %s ", e->name, e->pmu_name);
> +		pmu_group_head->pmu_name = e->pmu_name;
> +		list_for_each_entry(p, pmu_info_list, nd) {
> +			if (!strcasecmp(p->name, e->pmu_name)) {
> +				pmu_group_head->size = p->size;
> +				pmu_group_head->fixed_size = p->fixed_size;
> +			}
> +		}
> +		list_add_tail(&pmu_group_head->nd, groups);
> +	}
> +
> +	//ret = insert_event_to_group(e, pmu_group_head, pmu_info_list);
This is also the commented out code. Can it delete?

> +	return ret;
> +}
> +
> +/**
> + * create_grouping - Create a list of groups and place all the events of
> + * event_info_list into these groups.
> + * @pmu_info_list: the list of PMU units info based on pmu-events data, used for
> + * creating new groups.
> + * @event_info_list: the list of events to be grouped.
> + * @groupings: the list of groups with events placed in.
> + * @modifier: any modifiers added to the events.
> + */
> +static int create_grouping(struct list_head *pmu_info_list,
> +			  struct list_head *event_info_list,
> +			  struct list_head *groupings __maybe_unused,
> +			  const char *modifier __maybe_unused)
> +{
> +	int ret = 0;
> +	struct metricgroup__event_info *e;
> +	LIST_HEAD(groups);
> +	char *bit_buf = malloc(NR_COUNTERS);
Similar to the above, may need to check the malloc return value.

Thanks,
Yang
  
Liang, Kan Sept. 26, 2023, 3:55 p.m. UTC | #2
On 2023-09-25 2:18 a.m., weilin.wang@intel.com wrote:
> From: Weilin Wang <weilin.wang@intel.com>
> 
> Add struct metricgroup__pmu_group_list to hold the lists of groups from
> different PMUs. Each PMU has one separate list.
> 
> Add struct metricgroup__group as one node (one group in the grouping
> result) of the metricgroup__pmu_group_list. It uses two bitmaps to log
> counter availabilities(gp counters and fixed counters).
> 
> Add functions to create group and assign event into the groups based on the
> event restrictions (struct metricgroup__event_info) and counter
> availability (pmu_info_list and bitmaps). New group is inserted into the
> list groups.
> 
> Signed-off-by: Weilin Wang <weilin.wang@intel.com>
> ---
>  tools/perf/util/metricgroup.c | 72 +++++++++++++++++++++++++++++++++++
>  tools/perf/util/metricgroup.h | 37 ++++++++++++++++++
>  2 files changed, 109 insertions(+)
> 
> diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
> index 0ca885a42..de6a6a1d7 100644
> --- a/tools/perf/util/metricgroup.c
> +++ b/tools/perf/util/metricgroup.c
> @@ -1681,6 +1681,76 @@ static int get_pmu_counter_layouts(struct list_head *pmu_info_list,
>  	return ret;
>  }
>  
> +/**
> + * assign_event_grouping - Assign an event into a group. If existing group
> + * cannot include it, create a new group and insert the event to it.
> + */
> +static int assign_event_grouping(struct metricgroup__event_info *e,
> +				struct list_head *pmu_info_list __maybe_unused,
> +				struct list_head *groups)
> +{
> +	int ret = 0;
> +
> +	struct metricgroup__pmu_group_list *g = NULL;
> +	struct metricgroup__pmu_group_list *pmu_group_head = NULL;
> +
> +	list_for_each_entry(g, groups, nd) {
> +		if (!strcasecmp(g->pmu_name, e->pmu_name)) {
> +			pr_debug("found group for event %s in pmu %s\n", e->name, g->pmu_name);
> +			pmu_group_head = g;
> +			break;
> +		}
> +	}
> +	if (!pmu_group_head) {
> +		struct metricgroup__pmu_counters *p;
> +
> +		pmu_group_head = malloc(sizeof(struct metricgroup__pmu_group_list));
> +		INIT_LIST_HEAD(&pmu_group_head->group_head);
> +		pr_debug("create new group for event %s in pmu %s ", e->name, e->pmu_name);
> +		pmu_group_head->pmu_name = e->pmu_name;
> +		list_for_each_entry(p, pmu_info_list, nd) {
> +			if (!strcasecmp(p->name, e->pmu_name)) {
> +				pmu_group_head->size = p->size;
> +				pmu_group_head->fixed_size = p->fixed_size;

Just need to fill the size and fixed_size once. Break if the PMU is found.

Thanks,
Kan

> +			}
> +		}
> +		list_add_tail(&pmu_group_head->nd, groups);
> +	}
> +
> +	//ret = insert_event_to_group(e, pmu_group_head, pmu_info_list);
> +	return ret;
> +}
> +
> +/**
> + * create_grouping - Create a list of groups and place all the events of
> + * event_info_list into these groups.
> + * @pmu_info_list: the list of PMU units info based on pmu-events data, used for
> + * creating new groups.
> + * @event_info_list: the list of events to be grouped.
> + * @groupings: the list of groups with events placed in.
> + * @modifier: any modifiers added to the events.
> + */
> +static int create_grouping(struct list_head *pmu_info_list,
> +			  struct list_head *event_info_list,
> +			  struct list_head *groupings __maybe_unused,
> +			  const char *modifier __maybe_unused)
> +{
> +	int ret = 0;
> +	struct metricgroup__event_info *e;
> +	LIST_HEAD(groups);
> +	char *bit_buf = malloc(NR_COUNTERS);
> +
> +	//TODO: for each new core group, we should consider to add events that uses fixed counters
> +	list_for_each_entry(e, event_info_list, nd) {
> +		bitmap_scnprintf(e->counters, NR_COUNTERS, bit_buf, NR_COUNTERS);
> +		pr_debug("Event name %s, [pmu]=%s, [counters]=%s\n", e->name,
> +			e->pmu_name, bit_buf);
> +		ret = assign_event_grouping(e, pmu_info_list, &groups);
> +	}
> +
> +	return ret;
> +};
> +
>  /**
>   * hw_aware_build_grouping - Build event groupings by reading counter
>   * requirement of the events and counter available on the system from
> @@ -1714,6 +1784,8 @@ static int hw_aware_build_grouping(struct expr_parse_ctx *ctx __maybe_unused,
>  	ret = get_pmu_counter_layouts(&pmu_info_list, ltable);
>  	if (ret)
>  		goto err_out;
> +	ret = create_grouping(&pmu_info_list, &event_info_list, groupings,
> +			     modifier);
>  
>  err_out:
>  	metricgroup__free_event_info(&event_info_list);
> diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h
> index 8ee7b434e..f9f8b7498 100644
> --- a/tools/perf/util/metricgroup.h
> +++ b/tools/perf/util/metricgroup.h
> @@ -100,6 +100,43 @@ struct metricgroup__pmu_counters {
>  	size_t fixed_size;
>  };
>  
> +/**
> + * A list of groups for this pmu.
> + * This is updated during the grouping.
> + */
> +struct metricgroup__pmu_group_list {
> +	struct list_head nd;
> +	/** The name of the pmu(/core) the events collected on. */
> +	const char *pmu_name;
> +	/** The number of gp counters in the pmu(/core). */
> +	size_t size;
> +	/** The number of fixed counters in the pmu(/core) if applicable. */
> +	size_t fixed_size;
> +	/** Head to the list of groups using this pmu(/core)*/
> +	struct list_head group_head;
> +};
> +
> +/**
> + * This is one node in the metricgroup__pmu_group_list.
> + * It represents on group.
> + */
> +struct metricgroup__group {
> +	struct list_head nd;
> +	/** The bitmaps represent availability of the counters.
> +	 *  They are updated once the corresponding counter is used by
> +	 *  an event (event inserted into the group).
> +	 */
> +	DECLARE_BITMAP(gp_counters, NR_COUNTERS);
> +	DECLARE_BITMAP(fixed_counters, NR_COUNTERS);
> +	/** Head to the list of event names in this group*/
> +	struct list_head event_head;
> +};
> +
> +struct metricgroup__group_events {
> +	struct list_head nd;
> +	const char *event_name;
> +};
> +
>  /**
>   * Each group is one node in the group string list.
>   */
  

Patch

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 0ca885a42..de6a6a1d7 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -1681,6 +1681,76 @@  static int get_pmu_counter_layouts(struct list_head *pmu_info_list,
 	return ret;
 }
 
+/**
+ * assign_event_grouping - Assign an event into a group. If existing group
+ * cannot include it, create a new group and insert the event to it.
+ */
+static int assign_event_grouping(struct metricgroup__event_info *e,
+				struct list_head *pmu_info_list __maybe_unused,
+				struct list_head *groups)
+{
+	int ret = 0;
+
+	struct metricgroup__pmu_group_list *g = NULL;
+	struct metricgroup__pmu_group_list *pmu_group_head = NULL;
+
+	list_for_each_entry(g, groups, nd) {
+		if (!strcasecmp(g->pmu_name, e->pmu_name)) {
+			pr_debug("found group for event %s in pmu %s\n", e->name, g->pmu_name);
+			pmu_group_head = g;
+			break;
+		}
+	}
+	if (!pmu_group_head) {
+		struct metricgroup__pmu_counters *p;
+
+		pmu_group_head = malloc(sizeof(struct metricgroup__pmu_group_list));
+		INIT_LIST_HEAD(&pmu_group_head->group_head);
+		pr_debug("create new group for event %s in pmu %s ", e->name, e->pmu_name);
+		pmu_group_head->pmu_name = e->pmu_name;
+		list_for_each_entry(p, pmu_info_list, nd) {
+			if (!strcasecmp(p->name, e->pmu_name)) {
+				pmu_group_head->size = p->size;
+				pmu_group_head->fixed_size = p->fixed_size;
+			}
+		}
+		list_add_tail(&pmu_group_head->nd, groups);
+	}
+
+	//ret = insert_event_to_group(e, pmu_group_head, pmu_info_list);
+	return ret;
+}
+
+/**
+ * create_grouping - Create a list of groups and place all the events of
+ * event_info_list into these groups.
+ * @pmu_info_list: the list of PMU units info based on pmu-events data, used for
+ * creating new groups.
+ * @event_info_list: the list of events to be grouped.
+ * @groupings: the list of groups with events placed in.
+ * @modifier: any modifiers added to the events.
+ */
+static int create_grouping(struct list_head *pmu_info_list,
+			  struct list_head *event_info_list,
+			  struct list_head *groupings __maybe_unused,
+			  const char *modifier __maybe_unused)
+{
+	int ret = 0;
+	struct metricgroup__event_info *e;
+	LIST_HEAD(groups);
+	char *bit_buf = malloc(NR_COUNTERS);
+
+	//TODO: for each new core group, we should consider to add events that uses fixed counters
+	list_for_each_entry(e, event_info_list, nd) {
+		bitmap_scnprintf(e->counters, NR_COUNTERS, bit_buf, NR_COUNTERS);
+		pr_debug("Event name %s, [pmu]=%s, [counters]=%s\n", e->name,
+			e->pmu_name, bit_buf);
+		ret = assign_event_grouping(e, pmu_info_list, &groups);
+	}
+
+	return ret;
+};
+
 /**
  * hw_aware_build_grouping - Build event groupings by reading counter
  * requirement of the events and counter available on the system from
@@ -1714,6 +1784,8 @@  static int hw_aware_build_grouping(struct expr_parse_ctx *ctx __maybe_unused,
 	ret = get_pmu_counter_layouts(&pmu_info_list, ltable);
 	if (ret)
 		goto err_out;
+	ret = create_grouping(&pmu_info_list, &event_info_list, groupings,
+			     modifier);
 
 err_out:
 	metricgroup__free_event_info(&event_info_list);
diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h
index 8ee7b434e..f9f8b7498 100644
--- a/tools/perf/util/metricgroup.h
+++ b/tools/perf/util/metricgroup.h
@@ -100,6 +100,43 @@  struct metricgroup__pmu_counters {
 	size_t fixed_size;
 };
 
+/**
+ * A list of groups for this pmu.
+ * This is updated during the grouping.
+ */
+struct metricgroup__pmu_group_list {
+	struct list_head nd;
+	/** The name of the pmu(/core) the events collected on. */
+	const char *pmu_name;
+	/** The number of gp counters in the pmu(/core). */
+	size_t size;
+	/** The number of fixed counters in the pmu(/core) if applicable. */
+	size_t fixed_size;
+	/** Head to the list of groups using this pmu(/core)*/
+	struct list_head group_head;
+};
+
+/**
+ * This is one node in the metricgroup__pmu_group_list.
+ * It represents on group.
+ */
+struct metricgroup__group {
+	struct list_head nd;
+	/** The bitmaps represent availability of the counters.
+	 *  They are updated once the corresponding counter is used by
+	 *  an event (event inserted into the group).
+	 */
+	DECLARE_BITMAP(gp_counters, NR_COUNTERS);
+	DECLARE_BITMAP(fixed_counters, NR_COUNTERS);
+	/** Head to the list of event names in this group*/
+	struct list_head event_head;
+};
+
+struct metricgroup__group_events {
+	struct list_head nd;
+	const char *event_name;
+};
+
 /**
  * Each group is one node in the group string list.
  */