Commit 92532073 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo
Browse files

perf pmus: Fake PMU clean up



Rather than passing a fake PMU around, just pass that the fake PMU
should be used - true when doing testing. Move the fake PMU into
pmus.[ch] and try to abstract the PMU's properties in pmu.c, ie so
there is less "if fake_pmu" in non-PMU code. Give the fake PMU a made
up type number.

Signed-off-by: default avatarIan Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Benjamin Gray <bgray@linux.ibm.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Clément Le Goffic <clement.legoffic@foss.st.com>
Cc: Colin Ian King <colin.i.king@gmail.com>
Cc: Dominique Martinet <asmadeus@codewreck.org>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Junhao He <hejunhao3@huawei.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Oliver Upton <oliver.upton@linux.dev>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Sun Haiyong <sunhaiyong@loongson.cn>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Cc: Veronika Molnarova <vmolnaro@redhat.com>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: Will Deacon <will@kernel.org>
Cc: Xu Yang <xu.yang_2@nxp.com>
Cc: Yang Jihong <yangjihong@bytedance.com>
Cc: Yicong Yang <yangyicong@hisilicon.com>
Cc: Ze Gao <zegao2021@gmail.com>
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/20240907050830.6752-3-irogers@google.com


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent d3d5c1a0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -2500,7 +2500,7 @@ static int test_event(const struct evlist_test *e)
		return TEST_FAIL;
	}
	parse_events_error__init(&err);
	ret = __parse_events(evlist, e->name, /*pmu_filter=*/NULL, &err, /*fake_pmu=*/NULL,
	ret = __parse_events(evlist, e->name, /*pmu_filter=*/NULL, &err, /*fake_pmu=*/false,
			     /*warn_if_reordered=*/true, /*fake_tp=*/true);
	if (ret) {
		pr_debug("failed to parse event '%s', err %d\n", e->name, ret);
@@ -2529,7 +2529,7 @@ static int test_event_fake_pmu(const char *str)

	parse_events_error__init(&err);
	ret = __parse_events(evlist, str, /*pmu_filter=*/NULL, &err,
			     &perf_pmu__fake, /*warn_if_reordered=*/true,
			     /*fake_pmu=*/true, /*warn_if_reordered=*/true,
			     /*fake_tp=*/true);
	if (ret) {
		pr_debug("failed to parse event '%s', err %d\n",
+5 −7
Original line number Diff line number Diff line
@@ -819,8 +819,7 @@ static bool is_number(const char *str)
	return errno == 0 && end_ptr != str;
}

static int check_parse_id(const char *id, struct parse_events_error *error,
			  struct perf_pmu *fake_pmu)
static int check_parse_id(const char *id, struct parse_events_error *error)
{
	struct evlist *evlist;
	int ret;
@@ -841,7 +840,7 @@ static int check_parse_id(const char *id, struct parse_events_error *error,
	for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
		*cur = '/';

	ret = __parse_events(evlist, dup, /*pmu_filter=*/NULL, error, fake_pmu,
	ret = __parse_events(evlist, dup, /*pmu_filter=*/NULL, error, /*fake_pmu=*/true,
			     /*warn_if_reordered=*/true, /*fake_tp=*/false);
	free(dup);

@@ -855,7 +854,7 @@ static int check_parse_fake(const char *id)
	int ret;

	parse_events_error__init(&error);
	ret = check_parse_id(id, &error, &perf_pmu__fake);
	ret = check_parse_id(id, &error);
	parse_events_error__exit(&error);
	return ret;
}
@@ -1051,9 +1050,8 @@ static int test__parsing_fake_callback(const struct pmu_metric *pm,
}

/*
 * Parse all the metrics for current architecture,
 * or all defined cpus via the 'fake_pmu'
 * in parse_events.
 * Parse all the metrics for current architecture, or all defined cpus via the
 * 'fake_pmu' in parse_events.
 */
static int test__parsing_fake(struct test_suite *test __maybe_unused,
			      int subtest __maybe_unused)
+5 −5
Original line number Diff line number Diff line
@@ -1436,7 +1436,7 @@ static int build_combined_expr_ctx(const struct list_head *metric_list,
 * parse_ids - Build the event string for the ids and parse them creating an
 *             evlist. The encoded metric_ids are decoded.
 * @metric_no_merge: is metric sharing explicitly disabled.
 * @fake_pmu: used when testing metrics not supported by the current CPU.
 * @fake_pmu: use a fake PMU when testing metrics not supported by the current CPU.
 * @ids: the event identifiers parsed from a metric.
 * @modifier: any modifiers added to the events.
 * @group_events: should events be placed in a weak group.
@@ -1444,7 +1444,7 @@ static int build_combined_expr_ctx(const struct list_head *metric_list,
 *               the overall list of metrics.
 * @out_evlist: the created list of events.
 */
static int parse_ids(bool metric_no_merge, struct perf_pmu *fake_pmu,
static int parse_ids(bool metric_no_merge, bool fake_pmu,
		     struct expr_parse_ctx *ids, const char *modifier,
		     bool group_events, const bool tool_events[PERF_TOOL_MAX],
		     struct evlist **out_evlist)
@@ -1528,7 +1528,7 @@ static int parse_groups(struct evlist *perf_evlist,
			bool metric_no_threshold,
			const char *user_requested_cpu_list,
			bool system_wide,
			struct perf_pmu *fake_pmu,
			bool fake_pmu,
			struct rblist *metric_events_list,
			const struct pmu_metrics_table *table)
{
@@ -1703,7 +1703,7 @@ int metricgroup__parse_groups(struct evlist *perf_evlist,

	return parse_groups(perf_evlist, pmu, str, metric_no_group, metric_no_merge,
			    metric_no_threshold, user_requested_cpu_list, system_wide,
			    /*fake_pmu=*/NULL, metric_events, table);
			    /*fake_pmu=*/false, metric_events, table);
}

int metricgroup__parse_groups_test(struct evlist *evlist,
@@ -1717,7 +1717,7 @@ int metricgroup__parse_groups_test(struct evlist *evlist,
			    /*metric_no_threshold=*/false,
			    /*user_requested_cpu_list=*/NULL,
			    /*system_wide=*/false,
			    &perf_pmu__fake, metric_events, table);
			    /*fake_pmu=*/true, metric_events, table);
}

struct metricgroup__has_metric_data {
+17 −16
Original line number Diff line number Diff line
@@ -1500,7 +1500,7 @@ static int parse_events_add_pmu(struct parse_events_state *parse_state,
	}

	/* Look for event names in the terms and rewrite into format based terms. */
	if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, &parsed_terms,
	if (perf_pmu__check_alias(pmu, &parsed_terms,
				  &info, &alias_rewrote_terms, err)) {
		parse_events_terms__exit(&parsed_terms);
		return -EINVAL;
@@ -1537,8 +1537,7 @@ static int parse_events_add_pmu(struct parse_events_state *parse_state,
		return -ENOMEM;
	}

	if (!parse_state->fake_pmu &&
	    perf_pmu__config(pmu, &attr, &parsed_terms, parse_state->error)) {
	if (perf_pmu__config(pmu, &attr, &parsed_terms, parse_state->error)) {
		free_config_terms(&config_terms);
		parse_events_terms__exit(&parsed_terms);
		return -EINVAL;
@@ -1558,11 +1557,6 @@ static int parse_events_add_pmu(struct parse_events_state *parse_state,

	evsel->percore = config_term_percore(&evsel->config_terms);

	if (parse_state->fake_pmu) {
		parse_events_terms__exit(&parsed_terms);
		return 0;
	}

	parse_events_terms__exit(&parsed_terms);
	free((char *)evsel->unit);
	evsel->unit = strdup(info.unit);
@@ -1638,13 +1632,13 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
	}

	if (parse_state->fake_pmu) {
		if (!parse_events_add_pmu(parse_state, list, parse_state->fake_pmu, &parsed_terms,
		if (!parse_events_add_pmu(parse_state, list, perf_pmus__fake_pmu(), &parsed_terms,
					  /*auto_merge_stats=*/true)) {
			struct strbuf sb;

			strbuf_init(&sb, /*hint=*/ 0);
			parse_events_terms__to_strbuf(&parsed_terms, &sb);
			pr_debug("%s -> %s/%s/\n", event_name, "fake_pmu", sb.buf);
			pr_debug("%s -> fake/%s/\n", event_name, sb.buf);
			strbuf_release(&sb);
			ok++;
		}
@@ -1678,11 +1672,18 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
	INIT_LIST_HEAD(*listp);

	/* Attempt to add to list assuming event_or_pmu is a PMU name. */
	pmu = parse_state->fake_pmu ?: perf_pmus__find(event_or_pmu);
	pmu = perf_pmus__find(event_or_pmu);
	if (pmu && !parse_events_add_pmu(parse_state, *listp, pmu, const_parsed_terms,
					/*auto_merge_stats=*/false))
		return 0;

	if (parse_state->fake_pmu) {
		if (!parse_events_add_pmu(parse_state, *listp, perf_pmus__fake_pmu(),
					  const_parsed_terms,
					  /*auto_merge_stats=*/false))
			return 0;
	}

	pmu = NULL;
	/* Failed to add, try wildcard expansion of event_or_pmu as a PMU name. */
	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
@@ -1983,8 +1984,8 @@ static int evsel__compute_group_pmu_name(struct evsel *evsel,
			}
		}
	}
	/* Assign the actual name taking care that the fake PMU lacks a name. */
	evsel->group_pmu_name = strdup(group_pmu_name ?: "fake");
	/* Record computed name. */
	evsel->group_pmu_name = strdup(group_pmu_name);
	return evsel->group_pmu_name ? 0 : -ENOMEM;
}

@@ -2146,7 +2147,7 @@ static int parse_events__sort_events_and_fix_groups(struct list_head *list)
}

int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
		   struct parse_events_error *err, struct perf_pmu *fake_pmu,
		   struct parse_events_error *err, bool fake_pmu,
		   bool warn_if_reordered, bool fake_tp)
{
	struct parse_events_state parse_state = {
@@ -2365,7 +2366,7 @@ int parse_events_option(const struct option *opt, const char *str,

	parse_events_error__init(&err);
	ret = __parse_events(*args->evlistp, str, args->pmu_filter, &err,
			     /*fake_pmu=*/NULL, /*warn_if_reordered=*/true,
			     /*fake_pmu=*/false, /*warn_if_reordered=*/true,
			     /*fake_tp=*/false);

	if (ret) {
+4 −4
Original line number Diff line number Diff line
@@ -31,14 +31,14 @@ int parse_events_option(const struct option *opt, const char *str, int unset);
int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset);
__attribute__((nonnull(1, 2, 4)))
int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
		   struct parse_events_error *error, struct perf_pmu *fake_pmu,
		   struct parse_events_error *error, bool fake_pmu,
		   bool warn_if_reordered, bool fake_tp);

__attribute__((nonnull(1, 2, 3)))
static inline int parse_events(struct evlist *evlist, const char *str,
			       struct parse_events_error *err)
{
	return __parse_events(evlist, str, /*pmu_filter=*/NULL, err, /*fake_pmu=*/NULL,
	return __parse_events(evlist, str, /*pmu_filter=*/NULL, err, /*fake_pmu=*/false,
			      /*warn_if_reordered=*/true, /*fake_tp=*/false);
}

@@ -150,8 +150,8 @@ struct parse_events_state {
	struct parse_events_terms *terms;
	/* Start token. */
	int			   stoken;
	/* Special fake PMU marker for testing. */
	struct perf_pmu		  *fake_pmu;
	/* Use the fake PMU marker for testing. */
	bool			   fake_pmu;
	/* Skip actual tracepoint processing for testing. */
	bool			   fake_tp;
	/* If non-null, when wildcard matching only match the given PMU. */
Loading