tools/perf/tests/parse-metric.c

Source file repositories/reference/linux-study-clean/tools/perf/tests/parse-metric.c

File Facts

System
Linux kernel
Corpus path
tools/perf/tests/parse-metric.c
Extension
.c
Size
8221 bytes
Lines
306
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct value {
	const char	*event;
	u64		 val;
};

static u64 find_value(const char *name, struct value *values)
{
	struct value *v = values;

	while (v->event) {
		if (!strcmp(name, v->event))
			return v->val;
		v++;
	}
	return 0;
}

static void load_runtime_stat(struct evlist *evlist, struct value *vals)
{
	struct evsel *evsel;
	u64 count;

	evlist__alloc_aggr_stats(evlist, 1);
	evlist__for_each_entry(evlist, evsel) {
		count = find_value(evsel->name, vals);
		evsel->supported = true;
		evsel->stats->aggr->counts.val = count;
		evsel->stats->aggr->counts.ena = 1;
		evsel->stats->aggr->counts.run = 1;
	}
}

static double compute_single(struct evlist *evlist, const char *name)
{
	struct metric_expr *mexp;
	struct metric_event *me;
	struct evsel *evsel;

	evlist__for_each_entry(evlist, evsel) {
		me = metricgroup__lookup(&evlist->metric_events, evsel, false);
		if (me != NULL) {
			list_for_each_entry (mexp, &me->head, nd) {
				if (strcmp(mexp->metric_name, name))
					continue;
				return test_generic_metric(mexp, 0);
			}
		}
	}
	return 0.;
}

static int __compute_metric(const char *name, struct value *vals,
			    const char *name1, double *ratio1,
			    const char *name2, double *ratio2)
{
	const struct pmu_metrics_table *pme_test;
	struct perf_cpu_map *cpus;
	struct evlist *evlist;
	int err;

	/*
	 * We need to prepare evlist for stat mode running on CPU 0
	 * because that's where all the stats are going to be created.
	 */
	evlist = evlist__new();
	if (!evlist)
		return -ENOMEM;

	cpus = perf_cpu_map__new("0");
	if (!cpus) {
		evlist__delete(evlist);
		return -ENOMEM;
	}

	perf_evlist__set_maps(&evlist->core, cpus, NULL);

	/* Parse the metric into metric_events list. */
	pme_test = find_core_metrics_table("testarch", "testcpu");
	err = metricgroup__parse_groups_test(evlist, pme_test, name);
	if (err)
		goto out;

	err = evlist__alloc_stats(/*config=*/NULL, evlist, /*alloc_raw=*/false);
	if (err)
		goto out;

	/* Load the runtime stats with given numbers for events. */
	load_runtime_stat(evlist, vals);

	/* And execute the metric */

Annotation

Implementation Notes